| 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_SERVICE_H |
| 19 |
#define KEEPASSXC_FDOSECRETS_SERVICE_H |
| 20 |
|
| 21 |
#include "fdosecrets/dbus/DBusClient.h" |
| 22 |
#include "fdosecrets/dbus/DBusObject.h" |
| 23 |
|
| 24 |
class DatabaseTabWidget; |
| 25 |
class DatabaseWidget; |
| 26 |
class Group; |
| 27 |
|
| 28 |
class FdoSecretsPlugin; |
| 29 |
|
| 30 |
namespace FdoSecrets |
| 31 |
{ |
| 32 |
|
| 33 |
class Collection; |
| 34 |
class Item; |
| 35 |
class PromptBase; |
| 36 |
class Session; |
| 37 |
|
| 38 |
class Service : public DBusObject // clazy: exclude=ctor-missing-parent-argument |
| 39 |
{ |
| 40 |
Q_OBJECT |
| 41 |
Q_CLASSINFO("D-Bus Interface", DBUS_INTERFACE_SECRET_SERVICE_LITERAL) |
| 42 |
|
| 43 |
explicit Service(FdoSecretsPlugin* plugin, QPointer<DatabaseTabWidget> dbTabs, QSharedPointer<DBusMgr> dbus); |
| 44 |
|
| 45 |
public: |
| 46 |
/** |
| 47 |
* @brief Create a new instance of `Service`. Its parent is set to `null` |
| 48 |
* @return pointer to newly created Service, or nullptr if error |
| 49 |
* This may be caused by |
| 50 |
* - failed initialization |
| 51 |
*/ |
| 52 |
static QSharedPointer<Service> |
| 53 |
Create(FdoSecretsPlugin* plugin, QPointer<DatabaseTabWidget> dbTabs, QSharedPointer<DBusMgr> dbus); |
| 54 |
~Service() override; |
| 55 |
|
| 56 |
Q_INVOKABLE DBusResult openSession(const DBusClientPtr& client, |
| 57 |
const QString& algorithm, |
| 58 |
const QVariant& input, |
| 59 |
QVariant& output, |
| 60 |
Session*& result); |
| 61 |
Q_INVOKABLE DBusResult createCollection(const QVariantMap& properties, |
| 62 |
const QString& alias, |
| 63 |
Collection*& collection, |
| 64 |
PromptBase*& prompt); |
| 65 |
Q_INVOKABLE DBusResult searchItems(const DBusClientPtr& client, |
| 66 |
const StringStringMap& attributes, |
| 67 |
QList<Item*>& unlocked, |
| 68 |
QList<Item*>& locked); |
| 69 |
|
| 70 |
Q_INVOKABLE DBusResult unlock(const DBusClientPtr& client, |
| 71 |
const QList<DBusObject*>& objects, |
| 72 |
QList<DBusObject*>& unlocked, |
| 73 |
PromptBase*& prompt); |
| 74 |
|
| 75 |
Q_INVOKABLE DBusResult lock(const QList<DBusObject*>& objects, QList<DBusObject*>& locked, PromptBase*& prompt); |
| 76 |
|
| 77 |
Q_INVOKABLE DBusResult getSecrets(const DBusClientPtr& client, |
| 78 |
const QList<Item*>& items, |
| 79 |
Session* session, |
| 80 |
ItemSecretMap& secrets) const; |
| 81 |
|
| 82 |
Q_INVOKABLE DBusResult readAlias(const QString& name, Collection*& collection) const; |
| 83 |
|
| 84 |
Q_INVOKABLE DBusResult setAlias(const QString& name, Collection* collection); |
| 85 |
|
| 86 |
/** |
| 87 |
* List of collections |
| 88 |
* @return |
| 89 |
*/ |
| 90 |
Q_INVOKABLE DBUS_PROPERTY DBusResult collections(QList<Collection*>& collections) const; |
| 91 |
|
| 92 |
signals: |
| 93 |
void collectionCreated(Collection* collection); |
| 94 |
void collectionDeleted(Collection* collection); |
| 95 |
void collectionChanged(Collection* collection); |
| 96 |
|
| 97 |
/** |
| 98 |
* Finish signal for async action doUnlockDatabaseInDialog |
| 99 |
* @param accepted If false, the action is cancelled by the user |
| 100 |
* @param dbWidget The dbWidget the action is on |
| 101 |
*/ |
| 102 |
void doneUnlockDatabaseInDialog(bool accepted, DatabaseWidget* dbWidget); |
| 103 |
|
| 104 |
public: |
| 105 |
/** |
| 106 |
* List of sessions |
| 107 |
* @return |
| 108 |
*/ |
| 109 |
QList<Session*> sessions() const; |
| 110 |
|
| 111 |
FdoSecretsPlugin* plugin() const |
| 112 |
{ |
| 113 |
return m_plugin; |
| 114 |
} |
| 115 |
|
| 116 |
public slots: |
| 117 |
bool doLockDatabase(DatabaseWidget* dbWidget); |
| 118 |
bool doCloseDatabase(DatabaseWidget* dbWidget); |
| 119 |
Collection* doNewDatabase(); |
| 120 |
void doSwitchToDatabaseSettings(DatabaseWidget* dbWidget); |
| 121 |
|
| 122 |
/** |
| 123 |
* Async, connect to signal doneUnlockDatabaseInDialog for finish notification |
| 124 |
* @param dbWidget |
| 125 |
*/ |
| 126 |
void doUnlockDatabaseInDialog(DatabaseWidget* dbWidget); |
| 127 |
|
| 128 |
/** |
| 129 |
* Async, connect to signal doneUnlockDatabaseInDialog for finish notification |
| 130 |
* @param dbWidget |
| 131 |
*/ |
| 132 |
void doUnlockAnyDatabaseInDialog(); |
| 133 |
|
| 134 |
private slots: |
| 135 |
void ensureDefaultAlias(); |
| 136 |
|
| 137 |
void onDatabaseUnlockDialogFinished(bool accepted, DatabaseWidget* dbWidget); |
| 138 |
void onDatabaseTabOpened(DatabaseWidget* dbWidget, bool emitSignal); |
| 139 |
void monitorDatabaseExposedGroup(DatabaseWidget* dbWidget); |
| 140 |
|
| 141 |
void onCollectionAliasAboutToAdd(const QString& alias); |
| 142 |
void onCollectionAliasAdded(const QString& alias); |
| 143 |
|
| 144 |
void onCollectionAliasRemoved(const QString& alias); |
| 145 |
|
| 146 |
private: |
| 147 |
bool initialize(); |
| 148 |
|
| 149 |
/** |
| 150 |
* Find collection by alias name |
| 151 |
* @param alias |
| 152 |
* @return the collection under alias |
| 153 |
*/ |
| 154 |
Collection* findCollection(const QString& alias) const; |
| 155 |
|
| 156 |
/** |
| 157 |
* Find collection by dbWidget |
| 158 |
* @param db |
| 159 |
* @return the collection corresponding to the db |
| 160 |
*/ |
| 161 |
Collection* findCollection(const DatabaseWidget* db) const; |
| 162 |
|
| 163 |
DBusResult unlockedCollections(QList<Collection*>& unlocked) const; |
| 164 |
|
| 165 |
private: |
| 166 |
FdoSecretsPlugin* m_plugin{nullptr}; |
| 167 |
QPointer<DatabaseTabWidget> m_databases{}; |
| 168 |
|
| 169 |
QHash<QString, Collection*> m_aliases{}; |
| 170 |
QList<Collection*> m_collections{}; |
| 171 |
QHash<const DatabaseWidget*, Collection*> m_dbToCollection{}; |
| 172 |
|
| 173 |
QList<Session*> m_sessions{}; |
| 174 |
|
| 175 |
bool m_insideEnsureDefaultAlias{false}; |
| 176 |
bool m_unlockingAnyDatabase{false}; |
| 177 |
// list of db currently has unlock dialog shown |
| 178 |
QHash<const DatabaseWidget*, QMetaObject::Connection> m_unlockingDb{}; |
| 179 |
QSet<const DatabaseWidget*> m_lockingDb{}; // list of db being locking |
| 180 |
}; |
| 181 |
|
| 182 |
} // namespace FdoSecrets |
| 183 |
|
| 184 |
#endif // KEEPASSXC_FDOSECRETS_SERVICE_H |