KWallet

secretserviceclient.h
1/*
2 SPDX-FileCopyrightText: 2024 Marco Martin <notmart@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <QDBusObjectPath>
10#include <QObject>
11
12#include <libsecret/secret.h>
13
15class QTimer;
16
17// To allow gobject derived things with std::unique_ptr
18struct GObjectDeleter {
19 template<typename T>
20 void operator()(T *obj) const
21 {
22 if (obj) {
23 g_object_unref(obj);
24 }
25 }
26};
27
28template<typename T>
29using GObjectPtr = std::unique_ptr<T, GObjectDeleter>;
30using SecretServicePtr = GObjectPtr<SecretService>;
31using SecretCollectionPtr = GObjectPtr<SecretCollection>;
32using SecretItemPtr = GObjectPtr<SecretItem>;
33
34class SecretServiceClient : public QObject
35{
37
38public:
39 enum Type {
40 PlainText = 0,
41 Base64,
42 Binary,
43 Map,
44 Unknown
45 };
46 Q_ENUM(Type);
47
48 SecretServiceClient(QObject *parent = nullptr);
49
50 bool useKSecretBackend() const;
51 bool isAvailable() const;
52
53 bool unlockCollection(const QString &collectionName, bool *ok);
54
55 QString defaultCollection(bool *ok);
56 void setDefaultCollection(const QString &collectionName, bool *ok);
57 QStringList listCollections(bool *ok);
58 QStringList listFolders(const QString &collectionName, bool *ok);
59
60 QStringList listEntries(const QString &folder, const QString &collectionName, bool *ok);
61
62 QHash<QString, QString> readMetadata(const QString &key, const QString &folder, const QString &collectionName, bool *ok);
63
64 void createCollection(const QString &collectionName, bool *ok);
65
66 void deleteCollection(const QString &collectionName, bool *ok);
67
68 void deleteFolder(const QString &folder, const QString &collectionName, bool *ok);
69
70 QByteArray readEntry(const QString &key, const Type type, const QString &folder, const QString &collectionName, bool *ok);
71
72 void renameEntry(const QString &display_name, const QString &oldKey, const QString &newKey, const QString &folder, const QString &collectionName, bool *ok);
73
74 void writeEntry(const QString &itemName,
75 const QString &key,
76 const QByteArray &value,
77 const SecretServiceClient::Type type,
78 const QString &folder,
79 const QString &collectionName,
80 bool *ok);
81
82 void deleteEntry(const QString &key, const QString &folder, const QString &collectionName, bool *ok);
83
85 // Emitted when the service availability changed, or the service owner of secretservice has changed to a new one
86 void serviceChanged();
87 void promptClosed(bool accepted);
88 void collectionListDirty();
89 void collectionDirty(const QString &collection);
90 void collectionCreated(const QString &collection);
91 void collectionDeleted(const QString &collection);
92
93protected:
94 bool attemptConnection();
95 void watchCollection(const QString &collectionName, bool *ok);
96 void onServiceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner);
97
98 QString collectionLabelForPath(const QDBusObjectPath &path);
99 SecretCollection *retrieveCollection(const QString &name);
100 SecretItemPtr retrieveItem(const QString &key, const SecretServiceClient::Type type, const QString &folder, const QString &collectionName, bool *ok);
101
102protected Q_SLOTS:
103 void handlePrompt(bool dismissed);
104 void onCollectionCreated(const QDBusObjectPath &path);
105 void onCollectionDeleted(const QDBusObjectPath &path);
106 void onSecretItemChanged(const QDBusObjectPath &path);
107
108private:
109 SecretServicePtr m_service;
110 std::map<QString, SecretCollectionPtr> m_openCollections;
111 QDBusServiceWatcher *m_serviceWatcher;
112 QSet<QString> m_watchedCollections;
113 QSet<QString> m_dirtyCollections;
114 QTimer *m_collectionDirtyTimer;
115 QString m_serviceBusName;
116 bool m_useKSecretBackend = false;
117 bool m_updateInProgress = false;
118};
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 25 2025 11:53:00 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.