Debian Codegraph @

src/fdosecrets/objects/Collection.h

raw
1 /*
2 * Copyright (C) 2018 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_COLLECTION_H
19 #define KEEPASSXC_FDOSECRETS_COLLECTION_H
20
21 #include "fdosecrets/dbus/DBusClient.h"
22 #include "fdosecrets/dbus/DBusObject.h"
23
24 #include "core/EntrySearcher.h"
25
26 class Database;
27 class DatabaseWidget;
28 class Entry;
29 class Group;
30
31 namespace FdoSecrets
32 {
33 class Item;
34 class PromptBase;
35 class Service;
36 class Collection : public DBusObject
37 {
38 Q_OBJECT
39 Q_CLASSINFO("D-Bus Interface", DBUS_INTERFACE_SECRET_COLLECTION_LITERAL)
40
41 explicit Collection(Service* parent, DatabaseWidget* backend);
42
43 public:
44 /**
45 * @brief Create a new instance of `Collection`
46 * @param parent the owning Service
47 * @param backend the widget containing the database
48 * @return pointer to created instance, or nullptr when error happens.
49 * This may be caused by
50 * - DBus path registration error
51 * - database has no exposed group
52 */
53 static Collection* Create(Service* parent, DatabaseWidget* backend);
54
55 Q_INVOKABLE DBUS_PROPERTY DBusResult items(QList<Item*>& items) const;
56
57 Q_INVOKABLE DBUS_PROPERTY DBusResult label(QString& label) const;
58 Q_INVOKABLE DBusResult setLabel(const QString& label);
59
60 Q_INVOKABLE DBUS_PROPERTY DBusResult locked(bool& locked) const;
61
62 Q_INVOKABLE DBUS_PROPERTY DBusResult created(qulonglong& created) const;
63
64 Q_INVOKABLE DBUS_PROPERTY DBusResult modified(qulonglong& modified) const;
65
66 Q_INVOKABLE DBusResult remove(PromptBase*& prompt);
67 Q_INVOKABLE DBusResult searchItems(const DBusClientPtr& client,
68 const StringStringMap& attributes,
69 QList<Item*>& items);
70 Q_INVOKABLE DBusResult
71 createItem(const QVariantMap& properties, const Secret& secret, bool replace, Item*& item, PromptBase*& prompt);
72
73 signals:
74 void itemCreated(Item* item);
75 void itemDeleted(Item* item);
76 void itemChanged(Item* item);
77
78 void collectionChanged();
79 void collectionAboutToDelete();
80 void collectionLockChanged(bool newLocked);
81
82 void aliasAboutToAdd(const QString& alias);
83 void aliasAdded(const QString& alias);
84 void aliasRemoved(const QString& alias);
85
86 void doneUnlockCollection(bool accepted);
87
88 public:
89 DBusResult setProperties(const QVariantMap& properties);
90
91 bool isValid() const
92 {
93 return backend();
94 }
95
96 DBusResult removeAlias(QString alias);
97 DBusResult addAlias(QString alias);
98 const QSet<QString> aliases() const;
99
100 /**
101 * A human readable name of the collection, available even if the db is locked
102 * @return
103 */
104 QString name() const;
105
106 Group* exposedRootGroup() const;
107 DatabaseWidget* backend() const;
108 QString backendFilePath() const;
109 Service* service() const;
110
111 static EntrySearcher::SearchTerm attributeToTerm(const QString& key, const QString& value);
112
113 public slots:
114 // expose some methods for Prompt to use
115
116 bool doLock();
117 // actually only closes database tab in KPXC
118 bool doDelete();
119 // Async, finish signal doneUnlockCollection
120 void doUnlock();
121
122 bool doDeleteEntry(Entry* entry);
123 Item* doNewItem(const DBusClientPtr& client, QString itemPath);
124
125 // Only delete from dbus, will remove self. Do not affect database in KPXC
126 void removeFromDBus();
127
128 // force reload info from backend, potentially delete self
129 bool reloadBackend();
130
131 private slots:
132 void onDatabaseLockChanged();
133 void onDatabaseExposedGroupChanged();
134
135 // calls reloadBackend, delete self when error
136 void reloadBackendOrDelete();
137
138 private:
139 friend class DeleteCollectionPrompt;
140 friend class CreateCollectionPrompt;
141
142 void onEntryAdded(Entry* entry, bool emitSignal);
143 void populateContents();
144 void connectGroupSignalRecursive(Group* group);
145 void cleanupConnections();
146
147 bool backendLocked() const;
148
149 /**
150 * Check if the backend is a valid object, send error reply if not.
151 * @return true if the backend is valid.
152 */
153 DBusResult ensureBackend() const;
154
155 /**
156 * Ensure the database is unlocked, send error reply if locked.
157 * @return true if the database is locked
158 */
159 DBusResult ensureUnlocked() const;
160
161 /**
162 * Like mkdir -p, find or create the group by path, under m_exposedGroup
163 * @param groupPath
164 * @return created or found group
165 */
166 Group* findCreateGroupByPath(const QString& groupPath);
167
168 private:
169 QPointer<DatabaseWidget> m_backend;
170 QString m_backendPath;
171 QPointer<Group> m_exposedGroup;
172
173 QSet<QString> m_aliases;
174 QList<Item*> m_items;
175 QMap<const Entry*, Item*> m_entryToItem;
176 };
177
178 } // namespace FdoSecrets
179
180 #endif // KEEPASSXC_FDOSECRETS_COLLECTION_H