File: kgpgfirstassistant.cpp

package info (click to toggle)
kgpg 4:18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 12,168 kB
  • sloc: cpp: 18,911; xml: 159; makefile: 8; sh: 4
file content (379 lines) | stat: -rw-r--r-- 11,457 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* Copyright 2008,2010,2012  Rolf Eike Beer <kde@opensource.sf-tec.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "kgpgfirstassistant.h"

#include "gpgproc.h"
#include "kgpginterface.h"
#include "core/kgpgkey.h"

#include <KLocalizedString>
#include <KMessageBox>
#include <KUrlRequester>

#include <QCheckBox>
#include <QComboBox>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpacerItem>
#include <QStandardPaths>
#include <QTextStream>
#include <QWidget>
#include <QUrl>

using namespace KgpgCore;

KGpgFirstAssistant::KGpgFirstAssistant(QWidget *parent)
		: KAssistantDialog(parent)
{
	setWindowTitle(i18n("KGpg Assistant"));

	QWidget *page = new QWidget(this);
	QGridLayout *gridLayout = new QGridLayout(page);
	gridLayout->setSpacing(6);
	gridLayout->setMargin(11);
	gridLayout->setContentsMargins(0, 0, 0, 0);

	QLabel *label = new QLabel(page);
	QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
	sizePolicy.setHorizontalStretch(0);
	sizePolicy.setVerticalStretch(0);
	sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
	label->setSizePolicy(sizePolicy);
	label->setFrameShape(QFrame::NoFrame);
	label->setFrameShadow(QFrame::Plain);
	label->setScaledContents(false);
	label->setAlignment(Qt::AlignTop);
	label->setWordWrap(false);

	gridLayout->addWidget(label, 0, 0, 3, 1);

	label = new QLabel(page);
	label->setAlignment(Qt::AlignTop);
	label->setWordWrap(true);
	label->setText(i18n("This assistant will first setup some basic configuration options required for KGpg to work properly. Next, it will allow you to create your own key pair, enabling you to encrypt your files and emails."));

	gridLayout->addWidget(label, 0, 1, 1, 1);

	QSpacerItem *spacerItem = new QSpacerItem(20, 41, QSizePolicy::Minimum, QSizePolicy::Expanding);

	gridLayout->addItem(spacerItem, 1, 1, 1, 1);

	page_welcome = addPage(page, i18n("Welcome to the KGpg Assistant"));

	page = new QWidget(this);

	gridLayout = new QGridLayout(page);
	gridLayout->setSpacing(6);
	gridLayout->setMargin(11);
	gridLayout->setContentsMargins(0, 0, 0, 0);

	label = new QLabel(page);
	label->setAlignment(Qt::AlignTop);
	label->setWordWrap(true);
	label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	label->setText(i18n("KGpg needs to know which GnuPG binary to use."));

	gridLayout->addWidget(label, 0, 1, 1, 1);

	label = new QLabel(page);
	label->setAlignment(Qt::AlignTop);
	label->setWordWrap(true);
	label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
	label->setText(i18n("Unless you want to try some unusual settings, just click on the \"next\" button."));

	gridLayout->addWidget(label, 1, 1, 1, 1);

	spacerItem = new QSpacerItem(20, 60, QSizePolicy::Minimum, QSizePolicy::Expanding);

	gridLayout->addItem(spacerItem, 4, 1, 1, 1);

	txtGpgVersion = new QLabel(page);
	txtGpgVersion->setWordWrap(true);

	gridLayout->addWidget(txtGpgVersion, 3, 1, 1, 1);

	binURL = new KUrlRequester(page);
	binURL->setFilter(i18nc("search filter for gpg binary", "gpg|GnuPG binary\n*|All files"));
	QString gpgBin = QStandardPaths::findExecutable(QLatin1String("gpg2"));
	if (gpgBin.isEmpty())
		gpgBin = QStandardPaths::findExecutable(QLatin1String("gpg"));
	if (gpgBin.isEmpty())
		gpgBin = QLatin1String("gpg");
	binURL->setUrl(QUrl::fromLocalFile(gpgBin));

	connect(binURL, &KUrlRequester::textChanged, this, &KGpgFirstAssistant::slotBinaryChanged);
	slotBinaryChanged(gpgBin);

	gridLayout->addWidget(binURL, 2, 1, 1, 1);

	page_binary = addPage(page, i18n("GnuPG Binary"));

	page = new QWidget(this);

	gridLayout = new QGridLayout(page);
	gridLayout->setSpacing(6);
	gridLayout->setMargin(11);
	gridLayout->setContentsMargins(0, 0, 0, 0);
	text_optionsfound = new QLabel(page);
	text_optionsfound->setAlignment(Qt::AlignTop);
	text_optionsfound->setWordWrap(true);
	text_optionsfound->setText(i18n("Unless you want to try some unusual settings, just click on the \"next\" button."));

	gridLayout->addWidget(text_optionsfound, 1, 1, 1, 1);

	label = new QLabel(page);
	label->setAlignment(Qt::AlignTop);
	label->setWordWrap(true);
	label->setText(i18n("KGpg needs to know where your GnuPG configuration file is stored."));

	gridLayout->addWidget(label, 0, 1, 1, 1);

	spacerItem = new QSpacerItem(20, 60, QSizePolicy::Minimum, QSizePolicy::Expanding);

	gridLayout->addItem(spacerItem, 4, 1, 1, 1);

	pathURL = new KUrlRequester(page);

	gridLayout->addWidget(pathURL, 3, 1, 1, 1);

	label = new QLabel(page);
	label->setAlignment(Qt::AlignVCenter);
	label->setWordWrap(true);
	label->setText(i18n("Path to your GnuPG configuration file:"));

	gridLayout->addWidget(label, 2, 1, 1, 1);

	label = new QLabel(page);
	sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
	label->setSizePolicy(sizePolicy);
	label->setFrameShape(QFrame::NoFrame);
	label->setFrameShadow(QFrame::Plain);
	label->setScaledContents(false);
	label->setAlignment(Qt::AlignTop);
	label->setWordWrap(false);

	gridLayout->addWidget(label, 0, 0, 5, 1);
	page_config = addPage(page, i18n("Configuration File"));

	page = new QWidget(this);
	gridLayout = new QGridLayout(page);
	gridLayout->setSpacing(6);
	gridLayout->setMargin(11);
	gridLayout->setContentsMargins(0, 0, 0, 0);

	QHBoxLayout *hboxLayout = new QHBoxLayout();
	hboxLayout->setSpacing(6);
	label = new QLabel(page);
	label->setText(i18n("Your default key:"));

	hboxLayout->addWidget(label);

	CBdefault = new QComboBox(page);
	QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Fixed);
	sizePolicy1.setHorizontalStretch(0);
	sizePolicy1.setVerticalStretch(0);
	sizePolicy1.setHeightForWidth(CBdefault->sizePolicy().hasHeightForWidth());
	CBdefault->setSizePolicy(sizePolicy1);

	hboxLayout->addWidget(CBdefault);

	gridLayout->addLayout(hboxLayout, 0, 1, 1, 1);

	spacerItem = new QSpacerItem(20, 30, QSizePolicy::Minimum, QSizePolicy::Expanding);

	gridLayout->addItem(spacerItem, 1, 1, 1, 1);

	page_defaultkey = addPage(page, i18n("Default Key"));

	page = new QWidget(this);

	gridLayout = new QGridLayout(page);
	gridLayout->setSpacing(6);
	gridLayout->setMargin(11);
	gridLayout->setContentsMargins(0, 0, 0, 0);

	binlabel = new QLabel(page);

	gridLayout->addWidget(binlabel, 0, 1, 1, 1);

	versionLabel = new QLabel(page);

	gridLayout->addWidget(versionLabel, 1, 1, 1, 1);

	defaultkeylabel = new QLabel(page);

	gridLayout->addWidget(defaultkeylabel, 2, 1, 1, 1);

	generateCB = new QCheckBox(page);
	generateCB->setText(i18n("Generate new key"));

	gridLayout->addWidget(generateCB, 3, 1, 1, 1);

	spacerItem = new QSpacerItem(20, 30, QSizePolicy::Minimum, QSizePolicy::Expanding);

	gridLayout->addItem(spacerItem, 4, 1, 1, 1);

	autostartCB = new QCheckBox(page);
	autostartCB->setChecked(true);
	autostartCB->setText(i18n("Start KGpg automatically at KDE startup."));

	gridLayout->addWidget(autostartCB, 5, 1, 1, 1);

	page_done = addPage(page, i18n("Done"));
}

void
KGpgFirstAssistant::findConfigPath(const QString &gpgBinary)
{
	const QString gpgHome = GPGProc::getGpgHome(gpgBinary);
	QString confPath = gpgHome + QLatin1String( "gpg.conf" );

	if (!QFile(confPath).exists()) {
		const QString confPathOpt = gpgHome + QLatin1String("options");
		if (QFile(confPathOpt).exists()) {
			confPath = confPathOpt;
		} else {
			if (KMessageBox::questionYesNo(0, i18n("<qt><b>The GnuPG configuration file was not found</b>. Should KGpg try to create a config file ?</qt>"), QString(), KGuiItem(i18n("Create Config")), KGuiItem(i18n("Do Not Create"))) == KMessageBox::Yes) {
				QFile file(confPath);
				if (file.open(QIODevice::WriteOnly)) {
				    QTextStream stream(&file);
				    stream << "# GnuPG config file created by KGpg\n";
				    file.close();
				}
			} else {
				text_optionsfound->setText(i18n("<qt><b>The GnuPG configuration file was not found</b>.</qt>"));
				confPath.clear();
			}
		}
	}

	pathURL->setUrl(QUrl::fromLocalFile(confPath));

	const QStringList secids = KgpgInterface::readSecretKeys();

	bool noSecKeys = secids.isEmpty();
	generateCB->setChecked(noSecKeys);
	setAppropriate(page_defaultkey, !noSecKeys);

	if (noSecKeys) {
		defaultkeylabel->setVisible(false);
		return;
	}

	const KgpgKeyList publiclist = KgpgInterface::readPublicKeys(secids);

	CBdefault->clear();

	for (const KgpgKey &k : publiclist) {
		QString s;

		if (k.email().isEmpty())
			s = i18nc("Name: ID", "%1: %2", k.name(), k.id());
		else
			s = i18nc("Name (Email): ID", "%1 (%2): %3", k.name(), k.email(), k.id());

		CBdefault->addItem(s, k.fingerprint());
	}

	CBdefault->setCurrentIndex(0);
}

void
KGpgFirstAssistant::next()
{
	if (currentPage() == page_binary) {
		const QString &gpgbin = binURL->url().path();
		binlabel->setText(i18n("Your GnuPG binary is: %1", gpgbin));
		findConfigPath(gpgbin);
	} else if (currentPage() == page_config) {
		QString tst, name;
		m_confPath = pathURL->url().path();

		QString defaultID = KgpgInterface::getGpgSetting(QLatin1String( "default-key" ), m_confPath);

		if (!defaultID.isEmpty()) {
			for (int i = 0; i < CBdefault->count(); i++) {
				if (defaultID == CBdefault->itemData(i).toString().right(defaultID.length())) {
					CBdefault->setCurrentIndex(i);
					break;
				}
			}
		}
		versionLabel->setText(i18n("You have GnuPG version: %1", m_gpgVersion));
	} else if (currentPage() == page_defaultkey) {
		defaultkeylabel->setVisible(true);
		defaultkeylabel->setText(i18n("Your default key is: %1", CBdefault->currentText()));
		int i = CBdefault->currentIndex();
		if (i >= 0) {
			defaultkeylabel->setToolTip(CBdefault->itemData(i).toString());
		}
	}
	KAssistantDialog::next();
}

bool
KGpgFirstAssistant::runKeyGenerate() const
{
	return generateCB->isChecked();
}

QString
KGpgFirstAssistant::getConfigPath() const
{
	return m_confPath;
}

QString
KGpgFirstAssistant::getDefaultKey() const
{
	int i = CBdefault->currentIndex();
	if (i < 0)
		return QString();
	else
		return CBdefault->itemData(i).toString();
}

bool
KGpgFirstAssistant::getAutoStart() const
{
	return autostartCB->isChecked();
}

void
KGpgFirstAssistant::slotBinaryChanged(const QString &binary)
{
	if (binary.isEmpty()) {
		setValid(page_binary, false);
		return;
	}

	m_gpgVersion = GPGProc::gpgVersionString(binary);
	setValid(page_binary, !m_gpgVersion.isEmpty());
	if (!m_gpgVersion.isEmpty()) {
		const int gpgver = GPGProc::gpgVersion(m_gpgVersion);

		if (gpgver < 0x10400) {
			txtGpgVersion->setText(i18n("Your GnuPG version (%1) seems to be too old.<br />Compatibility with versions before 1.4.0 is no longer guaranteed.", m_gpgVersion));
		} else {
			txtGpgVersion->setText(i18n("You have GnuPG version: %1", m_gpgVersion));
		}
	}
}