File: kgpgexternalactions.cpp

package info (click to toggle)
kgpg 4:18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,168 kB
  • sloc: cpp: 18,911; xml: 159; makefile: 8; sh: 4
file content (495 lines) | stat: -rw-r--r-- 16,291 bytes parent folder | download | duplicates (3)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 * Copyright (C) 2002 Jean-Baptiste Mardelle <bj@altern.org>
 * Copyright (C) 2008,2009,2010,2011,2012,2013 Rolf Eike Beer <kde@opensource.sf-tec.de>
 * Copyright (C) 2016 Andrius Ć tikoans <andrius@stikonas.eu>
 */

/***************************************************************************
 *                                                                         *
 *   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) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kgpgexternalactions.h"

#include "detailedconsole.h"
#include "foldercompressjob.h"
#include "keyservers.h"
#include "keysmanager.h"
#include "kgpgfirstassistant.h"
#include "kgpginterface.h"
#include "kgpgsettings.h"
#include "kgpgtextinterface.h"
#include "selectpublickeydialog.h"
#include "selectsecretkey.h"
#include "core/images.h"
#include "editor/kgpgeditor.h"
#include "editor/kgpgtextedit.h"
#include "transactions/kgpgdecrypt.h"
#include "transactions/kgpgencrypt.h"
#include "transactions/kgpgsigntext.h"
#include "transactions/kgpgtransactionjob.h"
#include "transactions/kgpgverify.h"

#include <KActionCollection>
#include <KHelpClient>
#include <KMessageBox>

#include <QComboBox>
#include <QFont>
#include <QHBoxLayout>
#include <QProcess>
#include <QStringListModel>
#include <QTemporaryFile>
#include <kio/global.h>
#include <kio/renamedialog.h>
#include <kjobtrackerinterface.h>

KGpgExternalActions::KGpgExternalActions(KeysManager *parent, KGpgItemModel *model)
	: QObject(parent),
	compressionScheme(0),
	m_model(model),
	m_kgpgfoldertmp(nullptr),
	m_keysmanager(parent)
{
	readOptions();
}

KGpgExternalActions::~KGpgExternalActions()
{
	delete m_kgpgfoldertmp;
}

void KGpgExternalActions::encryptFiles(KeysManager *parent, const QList<QUrl> &urls)
{
	Q_ASSERT(!urls.isEmpty());

	KGpgExternalActions *encActions = new KGpgExternalActions(parent, parent->getModel());

	KgpgSelectPublicKeyDlg *dialog = new KgpgSelectPublicKeyDlg(parent, parent->getModel(), encActions->goDefaultKey(), false, urls);
	connect(dialog, &KgpgSelectPublicKeyDlg::accepted, encActions, &KGpgExternalActions::slotEncryptionKeySelected);
	connect(dialog, &KgpgSelectPublicKeyDlg::rejected, dialog, &KgpgSelectPublicKeyDlg::deleteLater);
	connect(dialog, &KgpgSelectPublicKeyDlg::rejected, encActions, &KGpgExternalActions::deleteLater);
	dialog->show();
}

void KGpgExternalActions::slotEncryptionKeySelected()
{
	KgpgSelectPublicKeyDlg *dialog = qobject_cast<KgpgSelectPublicKeyDlg *>(sender());
	Q_ASSERT(dialog != nullptr);
	sender()->deleteLater();

	QStringList opts;
	QString defaultKey;

	if (KGpgSettings::encryptFilesTo()) {
		if (KGpgSettings::pgpCompatibility())
			opts << QLatin1String( "--pgp6" );

		defaultKey = KGpgSettings::fileEncryptionKey();
	}

	KGpgEncrypt::EncryptOptions eopt = KGpgEncrypt::DefaultEncryption;

	if (dialog->getUntrusted())
		eopt |= KGpgEncrypt::AllowUntrustedEncryption;
	if (dialog->getArmor())
		eopt |= KGpgEncrypt::AsciiArmored;
	if (dialog->getHideId())
		eopt |= KGpgEncrypt::HideKeyId;

	if (KGpgSettings::allowCustomEncryptionOptions()) {
		const QString customopts(dialog->getCustomOptions().isEmpty());

		if (!customopts.isEmpty())
			opts << customopts.split(QLatin1Char(' '), QString::SkipEmptyParts);
	}

	QStringList keys = dialog->selectedKeys();

	if (!defaultKey.isEmpty() && !keys.contains(defaultKey))
		keys.append(defaultKey);

	if (dialog->getSymmetric())
		keys.clear();

	KGpgEncrypt *enc = new KGpgEncrypt(dialog->parent(), keys, dialog->getFiles(), eopt, opts);
	KGpgTransactionJob *encjob = new KGpgTransactionJob(enc);

	KIO::getJobTracker()->registerJob(encjob);
	encjob->start();

	deleteLater();
}

void KGpgExternalActions::encryptFolders(KeysManager *parent, const QList<QUrl> &urls)
{
	QTemporaryFile *tmpfolder = new QTemporaryFile();

	if (!tmpfolder->open()) {
		delete tmpfolder;
		KMessageBox::sorry(parent, i18n("Cannot create temporary file for folder compression."), i18n("Temporary File Creation"));
		return;
	}

	if (KMessageBox::Continue != KMessageBox::warningContinueCancel(parent,
				i18n("<qt>KGpg will now create a temporary archive file:<br /><b>%1</b> to process the encryption. "
				"The file will be deleted after the encryption is finished.</qt>",
				tmpfolder->fileName()), i18n("Temporary File Creation"), KStandardGuiItem::cont(),
				KStandardGuiItem::cancel(), QLatin1String( "FolderTmpFile" ))) {
		delete tmpfolder;
		return;
	}

	KGpgExternalActions *encActions = new KGpgExternalActions(parent, parent->getModel());
	KgpgSelectPublicKeyDlg *dialog = new KgpgSelectPublicKeyDlg(parent, parent->getModel(), encActions->goDefaultKey(), false, urls);
	encActions->m_kgpgfoldertmp = tmpfolder;

	QWidget *bGroup = new QWidget(dialog->optionsbox);
	QHBoxLayout *bGroupHBoxLayout = new QHBoxLayout(bGroup);
	bGroupHBoxLayout->setMargin(0);

	(void) new QLabel(i18n("Compression method for archive:"), bGroup);

	QComboBox *optionbx = new QComboBox(bGroup);
	bGroupHBoxLayout->addWidget(optionbx);
	optionbx->setModel(new QStringListModel(FolderCompressJob::archiveNames(), bGroup));

	connect(optionbx, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), encActions, &KGpgExternalActions::slotSetCompression);
	connect(dialog, &KgpgSelectPublicKeyDlg::accepted, encActions, &KGpgExternalActions::startFolderEncode);
	connect(dialog, &KgpgSelectPublicKeyDlg::rejected, encActions, &KGpgExternalActions::deleteLater);
	connect(dialog, &KgpgSelectPublicKeyDlg::rejected, dialog, &KgpgSelectPublicKeyDlg::deleteLater);
	
	dialog->show();
}

void KGpgExternalActions::slotSetCompression(int cp)
{
	compressionScheme = cp;
}

void KGpgExternalActions::startFolderEncode()
{
	KgpgSelectPublicKeyDlg *dialog = qobject_cast<KgpgSelectPublicKeyDlg *>(sender());
	Q_ASSERT(dialog != nullptr);
	dialog->deleteLater();

	const QList<QUrl> urls = dialog->getFiles();

	QStringList selec = dialog->selectedKeys();
	KGpgEncrypt::EncryptOptions encOptions = KGpgEncrypt::DefaultEncryption;
	const QStringList encryptOptions = dialog->getCustomOptions().split(QLatin1Char(' '),  QString::SkipEmptyParts);
	if (dialog->getSymmetric()) {
		selec.clear();
	} else {
		Q_ASSERT(!selec.isEmpty());
	}

	QString extension = FolderCompressJob::extensionForArchive(compressionScheme);

	if (dialog->getArmor())
		extension += QLatin1String( ".asc" );
	else if (KGpgSettings::pgpExtension())
		extension += QLatin1String( ".pgp" );
	else
		extension += QLatin1String( ".gpg" );

	if (dialog->getArmor())
		encOptions |= KGpgEncrypt::AsciiArmored;
	if (dialog->getHideId())
		encOptions |= KGpgEncrypt::HideKeyId;
	if (dialog->getUntrusted())
		encOptions |= KGpgEncrypt::AllowUntrustedEncryption;

	QUrl encryptedFile(QUrl::fromLocalFile(urls.first().adjusted(QUrl::StripTrailingSlash).path() + extension));
	QFile encryptedFolder(encryptedFile.path());
	dialog->hide();
	if (encryptedFolder.exists()) {
		QPointer<KIO::RenameDialog> over = new KIO::RenameDialog(m_keysmanager, i18n("File Already Exists"),
				QUrl(), encryptedFile, KIO::RenameDialog_Overwrite);
		if (over->exec() == QDialog::Rejected) {
			dialog = nullptr;
			delete over;
			deleteLater();
			return;
		}
		encryptedFile = over->newDestUrl();
		delete over;
	}

	FolderCompressJob *trayinfo = new FolderCompressJob(m_keysmanager, urls, encryptedFile, m_kgpgfoldertmp,
			selec, encryptOptions, encOptions, compressionScheme);
	connect(trayinfo, &FolderCompressJob::result, this, &KGpgExternalActions::slotFolderFinished);
	KIO::getJobTracker()->registerJob(trayinfo);
	trayinfo->start();
}

void KGpgExternalActions::slotFolderFinished(KJob *job)
{
	FolderCompressJob *trayinfo = qobject_cast<FolderCompressJob *>(job);
	Q_ASSERT(trayinfo != nullptr);

	if (trayinfo->error())
		KMessageBox::sorry(m_keysmanager, trayinfo->errorString());

	deleteLater();
}

void KGpgExternalActions::verifyFile(QUrl url)
{
	// check file signature
	if (url.isEmpty())
		return;

	QString sigfile;
	// try to find detached signature.
	if (!url.fileName().endsWith(QLatin1String(".sig"))) {
		sigfile = url.path() + QLatin1String( ".sig" );
		QFile fsig(sigfile);
		if (!fsig.exists()) {
			sigfile = url.path() + QLatin1String( ".asc" );
			QFile fsig(sigfile);
			// if no .asc or .sig signature file included, assume the file is internally signed
			if (!fsig.exists())
				sigfile.clear();
		}
	} else {
		sigfile = url.path();
		url = QUrl(sigfile.left(sigfile.length() - 4));
	}

	KGpgVerify *kgpv = new KGpgVerify(parent(), QList<QUrl>({QUrl(sigfile)}));
	connect(kgpv, &KGpgVerify::done, this, &KGpgExternalActions::slotVerificationDone);
	kgpv->start();
}

void KGpgExternalActions::slotVerificationDone(int result)
{
	KGpgVerify *kgpv = qobject_cast<KGpgVerify *>(sender());
	Q_ASSERT(kgpv != nullptr);
	kgpv->deleteLater();

	if (result == KGpgVerify::TS_MISSING_KEY) {
		KeyServer *kser = new KeyServer(m_keysmanager, m_model);
		kser->slotSetText(kgpv->missingId());
		kser->slotImport();
	} else {
		const QStringList messages = kgpv->getMessages();

		if (messages.isEmpty())
			return;

		QStringList msglist;
		for (QString rawmsg : messages)
			msglist << rawmsg.replace(QLatin1Char('<'), QLatin1String("&lt;"));

		(void) new KgpgDetailedInfo(m_keysmanager, KGpgVerify::getReport(messages, m_model),
				msglist.join(QLatin1String("<br/>")),
				QStringList(), i18nc("Caption of message box", "Verification Finished"));
	}
}

void KGpgExternalActions::signFiles(KeysManager* parent, const QList<QUrl>& urls)
{
	Q_ASSERT(!urls.isEmpty());

	KGpgExternalActions *signActions = new KGpgExternalActions(parent, parent->getModel());

	signActions->droppedUrls = urls;

	KgpgSelectSecretKey *keydlg = new KgpgSelectSecretKey(parent, parent->getModel(), false);
	connect(keydlg, &KgpgSelectSecretKey::accepted, signActions, &KGpgExternalActions::slotSignFiles);
	connect(keydlg, &KgpgSelectSecretKey::rejected, keydlg, &KgpgSelectSecretKey::deleteLater);
	connect(keydlg, &KgpgSelectSecretKey::rejected, signActions, &KGpgExternalActions::deleteLater);
	keydlg->show();
}

void KGpgExternalActions::slotSignFiles()
{
	KgpgSelectSecretKey *keydlg = qobject_cast<KgpgSelectSecretKey *>(sender());
	Q_ASSERT(keydlg != nullptr);
	sender()->deleteLater();

	const QString signKeyID = keydlg->getKeyID();

	QStringList Options;
	KGpgSignText::SignOptions sopts = KGpgSignText::DetachedSignature;
	if (KGpgSettings::asciiArmor()) {
		Options << QLatin1String( "--armor" );
		sopts |= KGpgSignText::AsciiArmored;
	}
	if (KGpgSettings::pgpCompatibility())
		Options << QLatin1String( "--pgp6" );

	if (droppedUrls.count() > 1) {
		KGpgTextInterface *signFileProcess = new KGpgTextInterface(parent(), signKeyID, Options);
		connect(signFileProcess, &KGpgTextInterface::fileSignFinished, signFileProcess, &KGpgTextInterface::deleteLater);
		signFileProcess->signFiles(droppedUrls);
	} else {
		KGpgSignText *signt = new KGpgSignText(parent(), signKeyID, droppedUrls, sopts);
		connect(signt, &KGpgSignText::done, signt, &KGpgSignText::deleteLater);
		signt->start();
	}

	deleteLater();
}

void KGpgExternalActions::decryptFiles(KeysManager* parent, const QList<QUrl> &urls)
{
	KGpgExternalActions *decActions = new KGpgExternalActions(parent, parent->getModel());

	decActions->decryptFile(urls);
}

void KGpgExternalActions::decryptFile(QList<QUrl> urls)
{
	if (urls.isEmpty()) {
		deleteLater();
		return;
	}

	while (!urls.first().isLocalFile()) {
		showDroppedFile(urls.takeFirst());
	}

	QUrl first = urls.first();

	QString oldname(first.fileName());
	if (oldname.endsWith(QLatin1String(".gpg"), Qt::CaseInsensitive) ||
			oldname.endsWith(QLatin1String(".asc"), Qt::CaseInsensitive) ||
			oldname.endsWith(QLatin1String(".pgp"), Qt::CaseInsensitive))
		oldname.chop(4);
	else
		oldname.append(QLatin1String( ".clear" ));

	QUrl swapname = QUrl::fromLocalFile(first.adjusted(QUrl::RemoveFilename).path() + oldname);
	QFile fgpg(swapname.path());
	if (fgpg.exists()) {
		QPointer<KIO::RenameDialog> over = new KIO::RenameDialog(m_keysmanager,
				i18n("File Already Exists"), QUrl(), swapname, KIO::RenameDialog_Overwrite);
		if (over->exec() != QDialog::Accepted) {
			delete over;
			urls.pop_front();
			decryptFile(urls);
			return;
		}

		swapname = over->newDestUrl();
		delete over;
	}

	droppedUrls = urls;
	KGpgDecrypt *decr = new KGpgDecrypt(this, droppedUrls.first(), swapname);
	connect(decr, &KGpgDecrypt::done, this, &KGpgExternalActions::slotDecryptionDone);
	decr->start();
}

void KGpgExternalActions::slotDecryptionDone(int status)
{
	KGpgDecrypt *decr = qobject_cast<KGpgDecrypt *>(sender());
	Q_ASSERT(decr != nullptr);

	if (status != KGpgTransaction::TS_OK)
		m_decryptionFailed << droppedUrls.first();

	decr->deleteLater();

	droppedUrls.pop_front();

	if (!droppedUrls.isEmpty()) {
		decryptFile(droppedUrls);
	} else {
		if (!m_decryptionFailed.isEmpty()) {
			QStringList failedFiles;
			for (const QUrl &url : m_decryptionFailed)
				failedFiles.append(url.toDisplayString());
			KMessageBox::errorList(nullptr,
					i18np("Decryption of this file failed:", "Decryption of these files failed:",
					m_decryptionFailed.count()), failedFiles,
					i18n("Decryption failed."));
		}
		deleteLater();
	}
}

void KGpgExternalActions::showDroppedFile(const QUrl &file)
{
	KgpgEditor *kgpgtxtedit = new KgpgEditor(m_keysmanager, m_model, 0);
	connect(m_keysmanager, &KeysManager::fontChanged, kgpgtxtedit, &KgpgEditor::slotSetFont);

	kgpgtxtedit->m_editor->openDroppedFile(file, false);

	kgpgtxtedit->show();
}

void KGpgExternalActions::readOptions()
{
	if (KGpgSettings::firstRun()) {
		firstRun();
	} else if (KGpgSettings::gpgConfigPath().isEmpty()) {
		if (KMessageBox::Yes == KMessageBox::questionYesNo(0,
				i18n("<qt>You have not set a path to your GnuPG config file.<br />This may cause some surprising results in KGpg's execution."
				"<br />Would you like to start KGpg's assistant to fix this problem?</qt>"),
				QString(), KGuiItem(i18n("Start Assistant")), KGuiItem(i18n("Do Not Start"))))
			startAssistant();
	}
}

void KGpgExternalActions::firstRun()
{
	QProcess *createConfigProc = new QProcess(this);
	QStringList args;
	args << QLatin1String( "--no-tty" ) << QLatin1String( "--list-secret-keys" );
	createConfigProc->start(QLatin1String( "gpg" ), args);	// start GnuPG so that it will create a config file
	createConfigProc->waitForFinished();
	startAssistant();
}

void KGpgExternalActions::startAssistant()
{
	if (m_assistant.isNull()) {
		m_assistant = new KGpgFirstAssistant(m_keysmanager);

		connect(m_assistant.data(), &KGpgFirstAssistant::accepted, this, &KGpgExternalActions::slotSaveOptionsPath);
		connect(m_assistant.data(), &KGpgFirstAssistant::rejected, m_assistant.data(), &KGpgFirstAssistant::deleteLater);
		connect(m_assistant->button(QDialogButtonBox::Help), &QPushButton::clicked, this, &KGpgExternalActions::help);
	}

	m_assistant->show();
}

void KGpgExternalActions::slotSaveOptionsPath()
{
	KGpgSettings::setAutoStart(m_assistant->getAutoStart());
	KGpgSettings::setGpgConfigPath(m_assistant->getConfigPath());
	KGpgSettings::setFirstRun(false);

	const QString gpgConfServer(KgpgInterface::getGpgSetting(QLatin1String( "keyserver" ), KGpgSettings::gpgConfigPath()));
	if (!gpgConfServer.isEmpty()) {
		// The user already had configured a keyserver, set this one as default.
		QStringList serverList(KGpgSettings::keyServers());
		serverList.prepend(gpgConfServer);
		KGpgSettings::setKeyServers(serverList);
	}

	const QString defaultID(m_assistant->getDefaultKey());

	KGpgSettings::self()->save();
	emit updateDefault(defaultID);
	if (m_assistant->runKeyGenerate())
		emit createNewKey();
	m_assistant->deleteLater();
}

void KGpgExternalActions::help()
{
	KHelpClient::invokeHelp(QString(), QLatin1String( "kgpg" ));
}

QKeySequence KGpgExternalActions::goDefaultKey() const
{
	return QKeySequence(qobject_cast<QAction *>(m_keysmanager->actionCollection()->action(QLatin1String( "go_default_key" )))->shortcut());
}