Debian Codegraph @

src/fdosecrets/objects/Prompt.h

raw
1 /*
2 * Copyright (C) 2019 Aetf <[email protected]>
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
18 #ifndef KEEPASSXC_FDOSECRETS_PROMPT_H
19 #define KEEPASSXC_FDOSECRETS_PROMPT_H
20
21 #include "core/Global.h"
22 #include "fdosecrets/dbus/DBusClient.h"
23 #include "fdosecrets/dbus/DBusObject.h"
24
25 class QWindow;
26
27 class DatabaseWidget;
28 class Entry;
29
30 namespace FdoSecrets
31 {
32
33 class Service;
34
35 // a simple helper class to auto convert
36 // true/false, DBusResult and Pending values
37 class PromptResult
38 {
39 enum Value
40 {
41 Accepted,
42 Dismissed,
43 AsyncPending,
44 };
45 const Value value;
46
47 explicit PromptResult(Value v) noexcept
48 : value(v)
49 {
50 }
51 explicit PromptResult(bool accepted)
52 : value(accepted ? Accepted : Dismissed)
53 {
54 }
55
56 public:
57 PromptResult()
58 : PromptResult(true)
59 {
60 }
61 PromptResult(const DBusResult& res) // NOLINT(google-explicit-constructor)
62 : PromptResult(res.ok())
63 {
64 }
65
66 static const PromptResult Pending;
67 static PromptResult accepted(bool accepted)
68 {
69 return PromptResult{accepted};
70 }
71
72 bool isDismiss() const
73 {
74 return value == Dismissed;
75 }
76 bool isPending() const
77 {
78 return value == AsyncPending;
79 }
80 };
81
82 class PromptBase : public DBusObject
83 {
84 Q_OBJECT
85 Q_CLASSINFO("D-Bus Interface", DBUS_INTERFACE_SECRET_PROMPT_LITERAL)
86 public:
87 Q_INVOKABLE DBusResult prompt(const DBusClientPtr& client, const QString& windowId);
88 Q_INVOKABLE DBusResult dismiss();
89
90 template <typename PROMPT, typename... ARGS> static PromptBase* Create(Service* parent, ARGS&&... args)
91 {
92 QScopedPointer<PROMPT> res{new PROMPT(parent, std::forward<ARGS>(args)...)};
93 if (!res->dbus()->registerObject(res.data())) {
94 // internal error;
95 return nullptr;
96 }
97 return res.take();
98 }
99
100 signals:
101 void completed(bool dismissed, const QVariant& result);
102
103 protected:
104 explicit PromptBase(Service* parent);
105
106 virtual PromptResult promptSync(const DBusClientPtr& client, const QString& windowId) = 0;
107 virtual QVariant currentResult() const;
108
109 QWindow* findWindow(const QString& windowId);
110 Service* service() const;
111 void finishPrompt(bool dismissed);
112
113 private:
114 bool m_signalSent = false;
115 };
116
117 class Collection;
118
119 class DeleteCollectionPrompt : public PromptBase
120 {
121 Q_OBJECT
122 friend class PromptBase;
123
124 explicit DeleteCollectionPrompt(Service* parent, Collection* coll);
125
126 PromptResult promptSync(const DBusClientPtr& client, const QString& windowId) override;
127
128 QPointer<Collection> m_collection;
129 };
130
131 class CreateCollectionPrompt : public PromptBase
132 {
133 Q_OBJECT
134 friend class PromptBase;
135
136 explicit CreateCollectionPrompt(Service* parent, QVariantMap properties, QString alias);
137
138 PromptResult promptSync(const DBusClientPtr& client, const QString& windowId) override;
139 QVariant currentResult() const override;
140
141 QVariantMap m_properties;
142 QString m_alias;
143 Collection* m_coll{};
144 };
145
146 class LockCollectionsPrompt : public PromptBase
147 {
148 Q_OBJECT
149 friend class PromptBase;
150
151 explicit LockCollectionsPrompt(Service* parent, const QList<Collection*>& colls);
152
153 PromptResult promptSync(const DBusClientPtr& client, const QString& windowId) override;
154 QVariant currentResult() const override;
155
156 QList<QPointer<Collection>> m_collections;
157 QList<QDBusObjectPath> m_locked;
158 };
159
160 class DBusClient;
161 class UnlockPrompt : public PromptBase
162 {
163 Q_OBJECT
164 friend class PromptBase;
165
166 explicit UnlockPrompt(Service* parent, const QSet<Collection*>& colls, const QSet<Item*>& items);
167
168 PromptResult promptSync(const DBusClientPtr& client, const QString& windowId) override;
169 QVariant currentResult() const override;
170
171 void collectionUnlockFinished(bool accepted);
172 void itemUnlockFinished(const QHash<Entry*, AuthDecision>& results, AuthDecision forFutureEntries);
173 void unlockItems();
174
175 QList<QPointer<Collection>> m_collections;
176 QHash<Collection*, QList<QPointer<Item>>> m_items;
177 QHash<QUuid, Item*> m_entryToItems;
178
179 QList<QDBusObjectPath> m_unlocked;
180 int m_numRejected = 0;
181
182 // info about calling client
183 QWeakPointer<DBusClient> m_client;
184 QString m_windowId;
185 };
186
187 class Item;
188 class DeleteItemPrompt : public PromptBase
189 {
190 Q_OBJECT
191 friend class PromptBase;
192
193 explicit DeleteItemPrompt(Service* parent, Item* item);
194
195 PromptResult promptSync(const DBusClientPtr& client, const QString& windowId) override;
196
197 QPointer<Item> m_item;
198 };
199
200 class CreateItemPrompt : public PromptBase
201 {
202 Q_OBJECT
203 friend class PromptBase;
204
205 explicit CreateItemPrompt(Service* parent,
206 Collection* coll,
207 QVariantMap properties,
208 Secret secret,
209 bool replace);
210
211 PromptResult promptSync(const DBusClientPtr& client, const QString& windowId) override;
212 QVariant currentResult() const override;
213
214 DBusResult createItem(const QString& windowId);
215 DBusResult updateItem();
216
217 QPointer<Collection> m_coll;
218 QVariantMap m_properties;
219 Secret m_secret;
220 bool m_replace;
221
222 QPointer<Item> m_item;
223
224 QPointer<const Session> m_sess;
225 QWeakPointer<DBusClient> m_client;
226 };
227
228 } // namespace FdoSecrets
229
230 #endif // KEEPASSXC_FDOSECRETS_PROMPT_H