1 | /** |
2 | * This file is part of the KDE project |
3 | * Copyright (C) 2013 Valentin Rusu <kde@rusu.info> |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public |
7 | * License version 2 as published by the Free Software Foundation. |
8 | * |
9 | * This library 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 GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public License |
15 | * along with this library; see the file COPYING.LIB. If not, write to |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "knewwalletdialog.h" |
21 | #include <klocalizedstring.h> |
22 | #include <QLabel> |
23 | #include <QTextDocument> |
24 | #include <QTimer> |
25 | #include <QTableWidget> |
26 | #include <QTableWidgetItem> |
27 | #include <gpgme.h> |
28 | #include <gpgme++/context.h> |
29 | #include <gpgme++/key.h> |
30 | #include <gpgme++/keylistresult.h> |
31 | #include <kmessagebox.h> |
32 | #include <kdebug.h> |
33 | |
34 | Q_DECLARE_METATYPE(GpgME::Key) |
35 | |
36 | namespace KWallet { |
37 | |
38 | KNewWalletDialog::KNewWalletDialog(const QString &appName, const QString &walletName, QWidget* parent): |
39 | QWizard(parent), _intro(0), _introId(0), _gpg(0), _gpgId(0) |
40 | { |
41 | setOption(HaveFinishButtonOnEarlyPages); |
42 | _intro = new KNewWalletDialogIntro(appName, walletName, this); |
43 | _introId = addPage(_intro); |
44 | |
45 | _gpg = new KNewWalletDialogGpg(appName, walletName, this); |
46 | _gpgId = addPage(_gpg); |
47 | } |
48 | |
49 | bool KNewWalletDialog::isBlowfish() const |
50 | { |
51 | return _intro->isBlowfish(); |
52 | } |
53 | |
54 | GpgME::Key KNewWalletDialog::gpgKey() const |
55 | { |
56 | QVariant varKey = field("key" ); |
57 | return varKey.value< GpgME::Key >(); |
58 | } |
59 | |
60 | KNewWalletDialogIntro::KNewWalletDialogIntro(const QString& appName, const QString& walletName, QWidget* parent): QWizardPage(parent) |
61 | { |
62 | _ui.setupUi(this); |
63 | if (appName.isEmpty()){ |
64 | _ui.labelIntro->setText(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 new wallet's type below or click cancel to deny the application's request.</qt>" , Qt::escape(walletName))); |
65 | } else { |
66 | _ui.labelIntro->setText(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 fashion. Please choose the new wallet's type below or click cancel to deny the application's request.</qt>" , Qt::escape(appName), Qt::escape(walletName))); |
67 | } |
68 | } |
69 | |
70 | void KNewWalletDialogIntro::onBlowfishToggled(bool blowfish) |
71 | { |
72 | setFinalPage(blowfish); |
73 | } |
74 | |
75 | |
76 | bool KNewWalletDialogIntro::isBlowfish() const |
77 | { |
78 | return _ui.radioBlowfish->isChecked(); |
79 | } |
80 | |
81 | int KNewWalletDialogIntro::nextId() const |
82 | { |
83 | if (isBlowfish()){ |
84 | return -1; |
85 | } else { |
86 | return qobject_cast< const KNewWalletDialog* >(wizard())->gpgId(); |
87 | } |
88 | } |
89 | |
90 | KNewWalletDialogGpg::KNewWalletDialogGpg(const QString& appName, const QString& walletName, QWidget* parent): |
91 | QWizardPage(parent), _alreadyInitialized(false), _complete(false) |
92 | { |
93 | _ui.setupUi(this); |
94 | } |
95 | |
96 | struct AddKeyToList { |
97 | QTableWidget *_list; |
98 | int _row; |
99 | AddKeyToList(QTableWidget *list) : _list(list), _row(0) {} |
100 | void operator()( const GpgME::Key &k) { |
101 | GpgME::UserID uid = k.userID(0); |
102 | QString name(uid.name()); |
103 | if (uid.comment()){ |
104 | name = QString("%1 (%2)" ).arg(name).arg(uid.comment()); |
105 | } |
106 | _list->setItem(_row, 0, new QTableWidgetItem(name)); |
107 | _list->setItem(_row, 1, new QTableWidgetItem(uid.email())); |
108 | _list->setItem(_row, 2, new QTableWidgetItem(k.shortKeyID())); |
109 | QVariant varKey; |
110 | varKey.setValue(k); |
111 | _list->item(_row, 0)->setData(Qt::UserRole, varKey); |
112 | ++_row; |
113 | } |
114 | }; |
115 | |
116 | void KNewWalletDialogGpg::initializePage() |
117 | { |
118 | if (_alreadyInitialized) |
119 | return; |
120 | |
121 | registerField("key" , this); |
122 | |
123 | GpgME::initializeLibrary(); |
124 | GpgME::Error err = GpgME::checkEngine(GpgME::OpenPGP); |
125 | if (err){ |
126 | kDebug() << "OpenPGP not supported on your system!" ; |
127 | KMessageBox::error(this, i18n("The QGpgME library failed to initialize for the OpenPGP protocol. Please check your system's configuration then try again." )); |
128 | emit completeChanged(); |
129 | return; |
130 | } |
131 | boost::shared_ptr< GpgME::Context > _ctx( GpgME::Context::createForProtocol(GpgME::OpenPGP) ); |
132 | if (0 == _ctx) { |
133 | KMessageBox::error(this, i18n("The QGpgME library failed to initialize for the OpenPGP protocol. Please check your system's configuration then try again." )); |
134 | emit completeChanged(); |
135 | return; |
136 | } |
137 | _ctx->setKeyListMode(GpgME::Local); |
138 | |
139 | std::vector< GpgME::Key > keys; |
140 | int row =0; |
141 | err = _ctx->startKeyListing(); |
142 | while (!err) { |
143 | GpgME::Key k = _ctx->nextKey(err); |
144 | if (err) |
145 | break; |
146 | if (!k.isInvalid() && k.canEncrypt() && (k.ownerTrust() == GpgME::Key::Ultimate)) { |
147 | keys.push_back(k); |
148 | } |
149 | } |
150 | _ctx->endKeyListing(); |
151 | |
152 | if (keys.size() == 0) { |
153 | KMessageBox::error(this, i18n("Seems that your system has no keys suitable for encryption. Please set-up at least an encryption key, then try again." )); |
154 | 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 | |
166 | void KNewWalletDialogGpg::onItemSelectionChanged() |
167 | { |
168 | _complete = _ui.listCertificates->currentRow() >= 0; |
169 | QVariant varKey = _ui.listCertificates->item(_ui.listCertificates->currentRow(), 0)->data(Qt::UserRole); |
170 | setField("key" , varKey); |
171 | emit completeChanged(); |
172 | } |
173 | |
174 | bool KNewWalletDialogGpg::isComplete() const |
175 | { |
176 | return _complete; |
177 | } |
178 | |
179 | bool KNewWalletDialogGpg::validateCurrentPage() |
180 | { |
181 | return false; |
182 | } |
183 | |
184 | |
185 | } // namespace |
186 | #include "moc_knewwalletdialog.cpp" |
187 | |