KWallet

knewwalletdialog.cpp
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2013 Valentin Rusu <kde@rusu.info>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7
8#include "knewwalletdialog.h"
9#include "kwalletd_debug.h"
10#include <KLocalizedString>
11#include <KMessageBox>
12#include <QTableWidget>
13#include <QTableWidgetItem>
14#include <gpgme++/context.h>
15#include <gpgme++/key.h>
16#include <gpgme++/keylistresult.h>
17
18Q_DECLARE_METATYPE(GpgME::Key)
19
20namespace KWallet
21{
22KNewWalletDialog::KNewWalletDialog(const QString &appName, const QString &walletName, QWidget *parent)
23 : QWizard(parent)
24{
25 setOption(HaveFinishButtonOnEarlyPages);
26 _intro = new KNewWalletDialogIntro(appName, walletName, this);
27 _introId = addPage(_intro);
28
29 _gpg = new KNewWalletDialogGpg(this);
30 _gpgId = addPage(_gpg);
31}
32
33bool KNewWalletDialog::isBlowfish() const
34{
35 return _intro->isBlowfish();
36}
37
38GpgME::Key KNewWalletDialog::gpgKey() const
39{
40 QVariant varKey = field(QStringLiteral("key"));
41 return varKey.value<GpgME::Key>();
42}
43
44KNewWalletDialogIntro::KNewWalletDialogIntro(const QString &appName, const QString &walletName, QWidget *parent)
45 : QWizardPage(parent)
46{
47 _ui.setupUi(this);
48 if (appName.isEmpty()) {
49 _ui.labelIntro->setText(
50 i18n("<qt>KDE has requested to create a new wallet named '<b>%1</b>'. This is used to store sensitive data in a secure fashion. Please choose the "
51 "new wallet's type below or click cancel to deny the application's request.</qt>",
52 walletName.toHtmlEscaped()));
53 } else {
54 _ui.labelIntro->setText(
55 i18n("<qt>The application '<b>%1</b>' has requested to create a new wallet named '<b>%2</b>'. This is used to store sensitive data in a secure "
56 "fashion. Please choose the new wallet's type below or click cancel to deny the application's request.</qt>",
57 appName.toHtmlEscaped(),
58 walletName.toHtmlEscaped()));
59 }
60}
61
62void KNewWalletDialogIntro::onBlowfishToggled(bool blowfish)
63{
64 setFinalPage(blowfish);
65}
66
67bool KNewWalletDialogIntro::isBlowfish() const
68{
69 return _ui.radioBlowfish->isChecked();
70}
71
72int KNewWalletDialogIntro::nextId() const
73{
74 if (isBlowfish()) {
75 return -1;
76 } else {
77 return qobject_cast<const KNewWalletDialog *>(wizard())->gpgId();
78 }
79}
80
81KNewWalletDialogGpg::KNewWalletDialogGpg(QWidget *parent)
82 : QWizardPage(parent)
83{
84 _ui.setupUi(this);
85}
86
87struct AddKeyToList {
88 QTableWidget *_list;
89 int _row;
90 AddKeyToList(QTableWidget *list)
91 : _list(list)
92 , _row(0)
93 {
94 }
95 void operator()(const GpgME::Key &k)
96 {
97 GpgME::UserID uid = k.userID(0);
98 QString name(uid.name());
99 if (uid.comment()) {
100 name = QStringLiteral("%1 (%2)").arg(name, uid.comment());
101 }
102 _list->setItem(_row, 0, new QTableWidgetItem(name));
103 _list->setItem(_row, 1, new QTableWidgetItem(uid.email()));
104 _list->setItem(_row, 2, new QTableWidgetItem(k.shortKeyID()));
105 QVariant varKey;
106 varKey.setValue(k);
107 _list->item(_row, 0)->setData(Qt::UserRole, varKey);
108 ++_row;
109 }
110};
111
112void KNewWalletDialogGpg::initializePage()
113{
114 if (_alreadyInitialized) {
115 return;
116 }
117
118 registerField(QStringLiteral("key"), this);
119
120 GpgME::initializeLibrary();
121 GpgME::Error err = GpgME::checkEngine(GpgME::OpenPGP);
122 if (err) {
123 qCDebug(KWALLETD_LOG) << "OpenPGP not supported on your system!";
125 i18n("The GpgME library failed to initialize for the OpenPGP protocol. Please check your system's configuration then try again."));
126 Q_EMIT completeChanged();
127 return;
128 }
129 std::shared_ptr<GpgME::Context> _ctx(GpgME::Context::createForProtocol(GpgME::OpenPGP));
130 if (!_ctx) {
132 i18n("The GpgME library failed to initialize for the OpenPGP protocol. Please check your system's configuration then try again."));
133 Q_EMIT completeChanged();
134 return;
135 }
136 _ctx->setKeyListMode(GpgME::Local);
137
138 std::vector<GpgME::Key> keys;
139 err = _ctx->startKeyListing();
140 while (!err) {
141 GpgME::Key k = _ctx->nextKey(err);
142 if (err) {
143 break;
144 }
145 if (!k.isInvalid() && k.canEncrypt() && (k.ownerTrust() == GpgME::Key::Ultimate)) {
146 keys.push_back(k);
147 }
148 }
149 _ctx->endKeyListing();
150
151 if (keys.size() == 0) {
153 i18n("Seems that your system has no keys suitable for encryption. Please set-up at least one encryption key, then try again."));
154 Q_EMIT completeChanged();
155 return;
156 }
157
158 _ui.listCertificates->setRowCount(keys.size());
159 std::for_each(keys.begin(), keys.end(), AddKeyToList(_ui.listCertificates));
160 _ui.listCertificates->resizeColumnsToContents();
161 _ui.listCertificates->setCurrentCell(0, 0);
162
163 _alreadyInitialized = true;
164}
165
166void KNewWalletDialogGpg::onItemSelectionChanged()
167{
168 _complete = _ui.listCertificates->currentRow() >= 0;
169 QVariant varKey = _ui.listCertificates->item(_ui.listCertificates->currentRow(), 0)->data(Qt::UserRole);
170 setField(QStringLiteral("key"), varKey);
171 Q_EMIT completeChanged();
172}
173
174bool KNewWalletDialogGpg::isComplete() const
175{
176 return _complete;
177}
178
179bool KNewWalletDialogGpg::validateCurrentPage()
180{
181 return false;
182}
183
184} // namespace
185#include "moc_knewwalletdialog.cpp"
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString name(StandardAction id)
QCA_EXPORT QString appName()
QString arg(Args &&... args) const const
bool isEmpty() const const
UserRole
QTableWidgetItem * item(int row, int column) const const
void setItem(int row, int column, QTableWidgetItem *item)
virtual void setData(int role, const QVariant &value)
void * data()
void setValue(QVariant &&value)
T value() const const
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.