Changeset View
Changeset View
Standalone View
Standalone View
plugins/messageviewer/bodypartformatter/gnupgwks/pgpkeyurlhandler.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or modify it | ||||
5 | under the terms of the GNU Library General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||||
7 | option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||||
12 | 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 the | ||||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
17 | 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "pgpkeyurlhandler.h" | ||||
21 | | ||||
22 | #include <QString> | ||||
23 | #include <QUrlQuery> | ||||
24 | #include <QProcess> | ||||
25 | | ||||
26 | #include <KLocalizedString> | ||||
27 | | ||||
28 | #include <MimeTreeParser/NodeHelper> | ||||
29 | #include <MimeTreeParser/BodyPart> | ||||
30 | #include <MessageViewer/Viewer> | ||||
31 | | ||||
32 | #include <QGpgME/Protocol> | ||||
33 | #include <QGpgME/ImportJob> | ||||
34 | #include <gpgme++/importresult.h> | ||||
35 | #include <gpgme++/error.h> | ||||
36 | | ||||
37 | #include <KMessageBox> | ||||
38 | | ||||
39 | using namespace MimeTreeParser::Interface; | ||||
40 | | ||||
41 | QUrlQuery ApplicationPgpKeyUrlHandler::decodePath(const QString &path) const | ||||
42 | { | ||||
43 | if (!path.startsWith(QLatin1String("pgpkey?"))) { | ||||
44 | return QUrlQuery(); | ||||
45 | } | ||||
46 | | ||||
47 | return QUrlQuery(path.mid(sizeof("pgpkey?") - 1)); | ||||
48 | } | ||||
49 | | ||||
50 | | ||||
51 | | ||||
52 | bool ApplicationPgpKeyUrlHandler::handleContextMenuRequest(BodyPart *, const QString &, const QPoint &) const | ||||
53 | { | ||||
54 | return false; | ||||
55 | } | ||||
56 | | ||||
57 | QString ApplicationPgpKeyUrlHandler::statusBarMessage(BodyPart *part, const QString &path) const | ||||
58 | { | ||||
59 | const QUrlQuery q = decodePath(path); | ||||
60 | if (q.queryItemValue(QStringLiteral("action")) == QLatin1String("import")) { | ||||
61 | return i18n("Import the key"); | ||||
62 | } | ||||
63 | | ||||
64 | return QString(); | ||||
65 | } | ||||
66 | | ||||
67 | bool ApplicationPgpKeyUrlHandler::handleClick(MessageViewer::Viewer *v, | ||||
68 | BodyPart *part, const QString &path) const | ||||
69 | { | ||||
70 | const QUrlQuery q = decodePath(path); | ||||
71 | if (q.queryItemValue(QStringLiteral("action")) == QLatin1String("import")) { | ||||
72 | auto job = QGpgME::openpgp()->importJob(); | ||||
73 | auto res = job->exec(part->content()->decodedContent()); | ||||
74 | if (res.error()) { | ||||
75 | KMessageBox::detailedError(v, | ||||
76 | i18n("An error occured while importing the key."), | ||||
77 | QString::fromUtf8(res.error().asString()), | ||||
78 | i18n("Import error")); | ||||
79 | } else { | ||||
80 | KMessageBox::information(v, | ||||
81 | i18n("The key has been succesfully imported."), | ||||
82 | i18n("Import finished")); | ||||
83 | } | ||||
84 | | ||||
85 | return true; | ||||
86 | } | ||||
87 | | ||||
88 | return false; | ||||
89 | } | ||||
90 | |