Libkleo

keycache.h
1/* -*- mode: c++; c-basic-offset:4 -*-
2 models/keycache.h
3
4 This file is part of Kleopatra, the KDE keymanager
5 SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#pragma once
11
12#include "kleo_export.h"
13
14#include <QObject>
15
16#include <gpgme++/global.h>
17
18#include <memory>
19#include <string>
20#include <vector>
21
22namespace GpgME
23{
24class Key;
25class DecryptionResult;
26class VerificationResult;
27class KeyListResult;
28class Subkey;
29}
30
31namespace KMime
32{
33namespace Types
34{
35class Mailbox;
36}
37}
38
39namespace Kleo
40{
41
42class FileSystemWatcher;
43class KeyGroup;
44class KeyGroupConfig;
45
46class KeyCacheAutoRefreshSuspension;
47
48class KLEO_EXPORT KeyCache : public QObject
49{
50 Q_OBJECT
51
52protected:
53 explicit KeyCache();
54
55public:
56 enum class KeyUsage {
57 AnyUsage,
58 Sign,
59 Encrypt,
60 Certify,
61 Authenticate,
62 };
63
64 enum ReloadOption {
65 Reload, //< if a reload is already in progress then ignore the reload request
66 ForceReload, //< if a reload is already in progress then cancel it and start another reload
67 };
68
69 static std::shared_ptr<const KeyCache> instance();
70 static std::shared_ptr<KeyCache> mutableInstance();
71
72 ~KeyCache() override;
73
74 void setGroupsEnabled(bool enabled);
75 void setGroupConfig(const std::shared_ptr<KeyGroupConfig> &groupConfig);
76
77 void insert(const GpgME::Key &key);
78 void insert(const std::vector<GpgME::Key> &keys);
79 bool insert(const KeyGroup &group);
80
81 void refresh(const std::vector<GpgME::Key> &keys);
82 bool update(const KeyGroup &group);
83
84 void remove(const GpgME::Key &key);
85 void remove(const std::vector<GpgME::Key> &keys);
86 bool remove(const KeyGroup &group);
87
88 void addFileSystemWatcher(const std::shared_ptr<FileSystemWatcher> &watcher);
89
90 void enableFileSystemWatcher(bool enable);
91
92 void setRefreshInterval(int hours);
93 int refreshInterval() const;
94
95 std::shared_ptr<KeyCacheAutoRefreshSuspension> suspendAutoRefresh();
96
97 void enableRemarks(bool enable);
98 bool remarksEnabled() const;
99
100 const std::vector<GpgME::Key> &keys() const;
101 std::vector<GpgME::Key> secretKeys() const;
102
103 KeyGroup group(const QString &id) const;
104 std::vector<KeyGroup> groups() const;
105 std::vector<KeyGroup> configurableGroups() const;
106 void saveConfigurableGroups(const std::vector<KeyGroup> &groups);
107
108 const GpgME::Key &findByFingerprint(const char *fpr) const;
109 const GpgME::Key &findByFingerprint(const std::string &fpr) const;
110
111 std::vector<GpgME::Key> findByFingerprint(const std::vector<std::string> &fprs) const;
112
113 std::vector<GpgME::Key> findByEMailAddress(const char *email) const;
114 std::vector<GpgME::Key> findByEMailAddress(const std::string &email) const;
115
116 /** Look through the cache and search for the best key for a mailbox.
117 *
118 * The best key is the key with a UID for the provided mailbox that
119 * has the highest validity and a subkey that is capable for the given
120 * usage.
121 * If more then one key have a UID with the same validity
122 * the most recently created key is taken.
123 *
124 * @returns the "best" key for the mailbox. */
125 GpgME::Key findBestByMailBox(const char *addr, GpgME::Protocol proto, KeyUsage usage) const;
126
127 /**
128 * Looks for a group named @a name which contains keys with protocol @a protocol
129 * that are suitable for the usage @a usage.
130 *
131 * If @a protocol is GpgME::OpenPGP or GpgME::CMS, then only groups consisting of keys
132 * matching this protocol are considered. Use @a protocol GpgME::UnknownProtocol to consider
133 * any groups regardless of the protocol including mixed-protocol groups.
134 *
135 * If @a usage is not KeyUsage::AnyUsage, then only groups consisting of keys supporting this usage
136 * are considered.
137 * The validity of keys and the presence of a private key (necessary for signing, certification, and
138 * authentication) is not taken into account.
139 *
140 * The first group that fulfills all conditions is returned.
141 *
142 * @returns a matching group or a null group if no matching group is found.
143 */
144 KeyGroup findGroup(const QString &name, GpgME::Protocol protocol, KeyUsage usage) const;
145
146 const GpgME::Key &findByShortKeyID(const char *id) const;
147 const GpgME::Key &findByShortKeyID(const std::string &id) const;
148
149 const GpgME::Key &findByKeyIDOrFingerprint(const char *id) const;
150 const GpgME::Key &findByKeyIDOrFingerprint(const std::string &id) const;
151
152 std::vector<GpgME::Key> findByKeyIDOrFingerprint(const std::vector<std::string> &ids) const;
153
154 const GpgME::Subkey &findSubkeyByKeyGrip(const char *grip, GpgME::Protocol protocol = GpgME::UnknownProtocol) const;
155 const GpgME::Subkey &findSubkeyByKeyGrip(const std::string &grip, GpgME::Protocol protocol = GpgME::UnknownProtocol) const;
156
157 std::vector<GpgME::Subkey> findSubkeysByKeyID(const std::vector<std::string> &ids) const;
158
159 std::vector<GpgME::Key> findRecipients(const GpgME::DecryptionResult &result) const;
160 std::vector<GpgME::Key> findSigners(const GpgME::VerificationResult &result) const;
161
162 std::vector<GpgME::Key> findSigningKeysByMailbox(const QString &mb) const;
163 std::vector<GpgME::Key> findEncryptionKeysByMailbox(const QString &mb) const;
164
165 /** Check for group keys.
166 *
167 * @returns A list of keys configured for groupName. Empty if no group cached.*/
168 std::vector<GpgME::Key> getGroupKeys(const QString &groupName) const;
169
170 enum Option {
171 // clang-format off
172 NoOption = 0,
173 RecursiveSearch = 1,
174 IncludeSubject = 2,
175 // clang-format on
176 };
177 Q_DECLARE_FLAGS(Options, Option)
178
179 std::vector<GpgME::Key> findSubjects(const GpgME::Key &key, Options option = RecursiveSearch) const;
180 std::vector<GpgME::Key> findSubjects(const std::vector<GpgME::Key> &keys, Options options = RecursiveSearch) const;
181
182 std::vector<GpgME::Key> findIssuers(const GpgME::Key &key, Options options = RecursiveSearch) const;
183
184 /** Check if at least one keylisting was finished. */
185 bool initialized() const;
186
187 /** Check if all keys have OpenPGP Protocol. */
188 bool pgpOnly() const;
189
190 /** Set the keys the cache shall contain. Marks cache as initialized. Use for tests only. */
191 void setKeys(const std::vector<GpgME::Key> &keys);
192
193 void setGroups(const std::vector<KeyGroup> &groups);
194
195public Q_SLOTS:
196 void clear();
197 void startKeyListing(GpgME::Protocol proto = GpgME::UnknownProtocol)
198 {
199 reload(proto);
200 }
201 void reload(GpgME::Protocol proto = GpgME::UnknownProtocol, ReloadOption option = Reload);
202 void cancelKeyListing();
203
204Q_SIGNALS:
205 void keyListingDone(const GpgME::KeyListResult &result);
206 void keysMayHaveChanged();
207 void groupAdded(const Kleo::KeyGroup &group);
208 void groupUpdated(const Kleo::KeyGroup &group);
209 void groupRemoved(const Kleo::KeyGroup &group);
210
211private:
212 class RefreshKeysJob;
213
214 class Private;
216};
217
218}
219
220Q_DECLARE_OPERATORS_FOR_FLAGS(Kleo::KeyCache::Options)
void update(Part *part, const QByteArray &data, qint64 dataSize)
KGuiItem remove()
KGuiItem insert()
KGuiItem clear()
const QList< QKeySequence > & reload()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:12 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.