FdoSecretsProxy.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (C) 2020 Aetf <aetf@unlimitedcodeworks.xyz>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef KEEPASSXC_FDOSECRETSPROXY_H
  18. #define KEEPASSXC_FDOSECRETSPROXY_H
  19. #include "fdosecrets/dbus/DBusTypes.h"
  20. #include <QtDBus>
  21. /**
  22. * Mimic the interface of QDBusPendingReply so the same code can be used in test
  23. */
  24. template <typename T> class PropertyReply
  25. {
  26. QDBusPendingReply<QDBusVariant> m_reply;
  27. public:
  28. /*implicit*/ PropertyReply(const QDBusMessage& reply)
  29. : m_reply(reply)
  30. {
  31. }
  32. bool isFinished() const
  33. {
  34. return m_reply.isFinished();
  35. }
  36. bool isValid() const
  37. {
  38. return m_reply.isValid();
  39. }
  40. bool isError() const
  41. {
  42. return m_reply.isError();
  43. }
  44. QDBusError error() const
  45. {
  46. return m_reply.error();
  47. }
  48. T value() const
  49. {
  50. return qdbus_cast<T>(m_reply.value().variant());
  51. }
  52. template <int> T argumentAt() const
  53. {
  54. return value();
  55. }
  56. };
  57. #define IMPL_GET_PROPERTY(name) \
  58. QDBusMessage msg = QDBusMessage::createMethodCall( \
  59. service(), path(), QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("Get")); \
  60. msg << interface() << QStringLiteral(#name); \
  61. return \
  62. { \
  63. connection().call(msg, QDBus::BlockWithGui) \
  64. }
  65. #define IMPL_SET_PROPERTY(name, value) \
  66. QDBusMessage msg = QDBusMessage::createMethodCall( \
  67. service(), path(), QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("Set")); \
  68. msg << interface() << QStringLiteral(#name) << QVariant::fromValue(QDBusVariant(QVariant::fromValue(value))); \
  69. return \
  70. { \
  71. connection().call(msg, QDBus::BlockWithGui) \
  72. }
  73. /*
  74. * Proxy class for interface org.freedesktop.Secret.Service
  75. */
  76. class ServiceProxy : public QDBusAbstractInterface
  77. {
  78. Q_OBJECT
  79. public:
  80. static inline const char* staticInterfaceName()
  81. {
  82. return "org.freedesktop.Secret.Service";
  83. }
  84. public:
  85. ServiceProxy(const QString& service,
  86. const QString& path,
  87. const QDBusConnection& connection,
  88. QObject* parent = nullptr);
  89. ~ServiceProxy() override;
  90. inline PropertyReply<QList<QDBusObjectPath>> collections() const
  91. {
  92. IMPL_GET_PROPERTY(Collections);
  93. }
  94. public Q_SLOTS: // METHODS
  95. inline QDBusPendingReply<QDBusObjectPath, QDBusObjectPath> CreateCollection(const QVariantMap& properties,
  96. const QString& alias)
  97. {
  98. QList<QVariant> argumentList;
  99. argumentList << QVariant::fromValue(properties) << QVariant::fromValue(alias);
  100. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("CreateCollection"), argumentList)};
  101. }
  102. inline QDBusPendingReply<FdoSecrets::wire::ObjectPathSecretMap> GetSecrets(const QList<QDBusObjectPath>& items,
  103. const QDBusObjectPath& session)
  104. {
  105. QList<QVariant> argumentList;
  106. argumentList << QVariant::fromValue(items) << QVariant::fromValue(session);
  107. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("GetSecrets"), argumentList)};
  108. }
  109. inline QDBusPendingReply<QList<QDBusObjectPath>, QDBusObjectPath> Lock(const QList<QDBusObjectPath>& paths)
  110. {
  111. QList<QVariant> argumentList;
  112. argumentList << QVariant::fromValue(paths);
  113. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("Lock"), argumentList)};
  114. }
  115. inline QDBusPendingReply<QDBusVariant, QDBusObjectPath> OpenSession(const QString& algorithm,
  116. const QDBusVariant& input)
  117. {
  118. QList<QVariant> argumentList;
  119. argumentList << QVariant::fromValue(algorithm) << QVariant::fromValue(input);
  120. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("OpenSession"), argumentList)};
  121. }
  122. inline QDBusPendingReply<QDBusObjectPath> ReadAlias(const QString& name)
  123. {
  124. QList<QVariant> argumentList;
  125. argumentList << QVariant::fromValue(name);
  126. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("ReadAlias"), argumentList)};
  127. }
  128. inline QDBusPendingReply<QList<QDBusObjectPath>, QList<QDBusObjectPath>>
  129. SearchItems(FdoSecrets::wire::StringStringMap attributes)
  130. {
  131. QList<QVariant> argumentList;
  132. argumentList << QVariant::fromValue(attributes);
  133. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("SearchItems"), argumentList)};
  134. }
  135. inline QDBusPendingReply<> SetAlias(const QString& name, const QDBusObjectPath& collection)
  136. {
  137. QList<QVariant> argumentList;
  138. argumentList << QVariant::fromValue(name) << QVariant::fromValue(collection);
  139. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("SetAlias"), argumentList)};
  140. }
  141. inline QDBusPendingReply<QList<QDBusObjectPath>, QDBusObjectPath> Unlock(const QList<QDBusObjectPath>& paths)
  142. {
  143. QList<QVariant> argumentList;
  144. argumentList << QVariant::fromValue(paths);
  145. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("Unlock"), argumentList)};
  146. }
  147. Q_SIGNALS: // SIGNALS
  148. void CollectionChanged(const QDBusObjectPath& collection);
  149. void CollectionCreated(const QDBusObjectPath& collection);
  150. void CollectionDeleted(const QDBusObjectPath& collection);
  151. };
  152. /*
  153. * Proxy class for interface org.freedesktop.Secret.Collection
  154. */
  155. class CollectionProxy : public QDBusAbstractInterface
  156. {
  157. Q_OBJECT
  158. public:
  159. static inline const char* staticInterfaceName()
  160. {
  161. return "org.freedesktop.Secret.Collection";
  162. }
  163. public:
  164. CollectionProxy(const QString& service,
  165. const QString& path,
  166. const QDBusConnection& connection,
  167. QObject* parent = nullptr);
  168. ~CollectionProxy() override;
  169. inline PropertyReply<qulonglong> created() const
  170. {
  171. IMPL_GET_PROPERTY(Created);
  172. }
  173. inline PropertyReply<QList<QDBusObjectPath>> items() const
  174. {
  175. IMPL_GET_PROPERTY(Items);
  176. }
  177. inline PropertyReply<QString> label() const
  178. {
  179. IMPL_GET_PROPERTY(Label);
  180. }
  181. inline QDBusPendingReply<> setLabel(const QString& value)
  182. {
  183. IMPL_SET_PROPERTY(Label, value);
  184. }
  185. inline PropertyReply<bool> locked() const
  186. {
  187. IMPL_GET_PROPERTY(Locked);
  188. }
  189. inline PropertyReply<qulonglong> modified() const
  190. {
  191. IMPL_GET_PROPERTY(Modified);
  192. }
  193. public Q_SLOTS: // METHODS
  194. inline QDBusPendingReply<QDBusObjectPath, QDBusObjectPath>
  195. CreateItem(const QVariantMap& properties, FdoSecrets::wire::Secret secret, bool replace)
  196. {
  197. QList<QVariant> argumentList;
  198. argumentList << QVariant::fromValue(properties) << QVariant::fromValue(secret) << QVariant::fromValue(replace);
  199. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("CreateItem"), argumentList)};
  200. }
  201. inline QDBusPendingReply<QDBusObjectPath> Delete()
  202. {
  203. QList<QVariant> argumentList;
  204. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("Delete"), argumentList)};
  205. }
  206. inline QDBusPendingReply<QList<QDBusObjectPath>> SearchItems(FdoSecrets::wire::StringStringMap attributes)
  207. {
  208. QList<QVariant> argumentList;
  209. argumentList << QVariant::fromValue(attributes);
  210. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("SearchItems"), argumentList)};
  211. }
  212. Q_SIGNALS: // SIGNALS
  213. void ItemChanged(const QDBusObjectPath& item);
  214. void ItemCreated(const QDBusObjectPath& item);
  215. void ItemDeleted(const QDBusObjectPath& item);
  216. };
  217. /*
  218. * Proxy class for interface org.freedesktop.Secret.Item
  219. */
  220. class ItemProxy : public QDBusAbstractInterface
  221. {
  222. Q_OBJECT
  223. public:
  224. static inline const char* staticInterfaceName()
  225. {
  226. return "org.freedesktop.Secret.Item";
  227. }
  228. public:
  229. ItemProxy(const QString& service,
  230. const QString& path,
  231. const QDBusConnection& connection,
  232. QObject* parent = nullptr);
  233. ~ItemProxy() override;
  234. inline PropertyReply<FdoSecrets::wire::StringStringMap> attributes() const
  235. {
  236. IMPL_GET_PROPERTY(Attributes);
  237. }
  238. inline QDBusPendingReply<> setAttributes(FdoSecrets::wire::StringStringMap value)
  239. {
  240. IMPL_SET_PROPERTY(Attributes, value);
  241. }
  242. inline PropertyReply<qulonglong> created() const
  243. {
  244. IMPL_GET_PROPERTY(Created);
  245. }
  246. inline PropertyReply<QString> label() const
  247. {
  248. IMPL_GET_PROPERTY(Label);
  249. }
  250. inline QDBusPendingReply<> setLabel(const QString& value)
  251. {
  252. IMPL_SET_PROPERTY(Label, value);
  253. }
  254. inline PropertyReply<bool> locked() const
  255. {
  256. IMPL_GET_PROPERTY(Locked);
  257. }
  258. inline PropertyReply<qulonglong> modified() const
  259. {
  260. IMPL_GET_PROPERTY(Modified);
  261. }
  262. public Q_SLOTS: // METHODS
  263. inline QDBusPendingReply<QDBusObjectPath> Delete()
  264. {
  265. QList<QVariant> argumentList;
  266. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("Delete"), argumentList)};
  267. }
  268. inline QDBusPendingReply<FdoSecrets::wire::Secret> GetSecret(const QDBusObjectPath& session)
  269. {
  270. QList<QVariant> argumentList;
  271. argumentList << QVariant::fromValue(session);
  272. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("GetSecret"), argumentList)};
  273. }
  274. inline QDBusPendingReply<> SetSecret(FdoSecrets::wire::Secret secret)
  275. {
  276. QList<QVariant> argumentList;
  277. argumentList << QVariant::fromValue(secret);
  278. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("SetSecret"), argumentList)};
  279. }
  280. Q_SIGNALS: // SIGNALS
  281. };
  282. /*
  283. * Proxy class for interface org.freedesktop.Secret.Session
  284. */
  285. class SessionProxy : public QDBusAbstractInterface
  286. {
  287. Q_OBJECT
  288. public:
  289. static inline const char* staticInterfaceName()
  290. {
  291. return "org.freedesktop.Secret.Session";
  292. }
  293. public:
  294. SessionProxy(const QString& service,
  295. const QString& path,
  296. const QDBusConnection& connection,
  297. QObject* parent = nullptr);
  298. ~SessionProxy() override;
  299. public Q_SLOTS: // METHODS
  300. inline QDBusPendingReply<> Close()
  301. {
  302. QList<QVariant> argumentList;
  303. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("Close"), argumentList)};
  304. }
  305. Q_SIGNALS: // SIGNALS
  306. };
  307. /*
  308. * Proxy class for interface org.freedesktop.Secret.Prompt
  309. */
  310. class PromptProxy : public QDBusAbstractInterface
  311. {
  312. Q_OBJECT
  313. public:
  314. static inline const char* staticInterfaceName()
  315. {
  316. return "org.freedesktop.Secret.Prompt";
  317. }
  318. public:
  319. PromptProxy(const QString& service,
  320. const QString& path,
  321. const QDBusConnection& connection,
  322. QObject* parent = nullptr);
  323. ~PromptProxy() override;
  324. public Q_SLOTS: // METHODS
  325. inline QDBusPendingReply<> Dismiss()
  326. {
  327. QList<QVariant> argumentList;
  328. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("Dismiss"), argumentList)};
  329. }
  330. inline QDBusPendingReply<> Prompt(const QString& windowId)
  331. {
  332. QList<QVariant> argumentList;
  333. argumentList << QVariant::fromValue(windowId);
  334. return {callWithArgumentList(QDBus::BlockWithGui, QStringLiteral("Prompt"), argumentList)};
  335. }
  336. Q_SIGNALS: // SIGNALS
  337. void Completed(bool dismissed, const QDBusVariant& result);
  338. };
  339. #undef IMPL_GET_PROPERTY
  340. #undef IMPL_SET_PROPERTY
  341. #endif // KEEPASSXC_FDOSECRETSPROXY_H