1
/**************************************************************************
2
* Copyright (C) 2009-2011 Matthias Fuchs <mat69@gmx.net> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program 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 *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18
***************************************************************************/
20
#include "keydownloader.h"
22
#include "signature_p.h"
27
#include <KMessageBox>
30
#include <gpgme++/context.h>
31
#include <gpgme++/data.h>
32
#include <gpgme++/importresult.h>
33
#include <qgpgme/dataprovider.h>
36
KeyDownloader::KeyDownloader(QObject *parent)
41
bool KeyDownloader::isValid() const
50
void KeyDownloader::downloadKey(QString fingerprint, Signature *sig)
52
downloadKey(fingerprint, sig, false);
55
void KeyDownloader::downloadKey(QString fingerprint, Signature *sig, bool mirrorFailed)
57
if (fingerprint.isEmpty() || (!sig && !mirrorFailed)) {
61
if (!fingerprint.startsWith(QLatin1String("0x"))) {
62
fingerprint = "0x" + fingerprint;
65
if (m_downloading.contains(fingerprint) && !mirrorFailed) {
66
if (!m_downloading.contains(fingerprint, sig)) {
67
m_downloading.insert(fingerprint, sig);
70
const QStringList servers = Settings::signatureKeyServers();
71
if (!servers.count()) {
73
i18n("No server for downloading keys is specified in settings. Downloading aborted."),
74
i18n("No key server"));
80
const QStringList failedMirrors = m_triedMirrors.values(fingerprint);
81
for (int i = 0; i < servers.count(); ++i) {
82
if (!m_triedMirrors.contains(fingerprint, servers.at(i))) {
83
mirror = servers.at(i);
88
mirror = servers.first();
91
if (mirror.isEmpty()) {
93
i18n("No useful key server found, key not downloaded. Add more servers to the settings or restart KGet and retry downloading."),
94
i18n("No key server"));
98
m_triedMirrors.insert(fingerprint, mirror);
100
m_downloading.insert(fingerprint, sig);
104
url.addPath("pks/lookup");
105
url.setQuery("op=get&options=mr&search=" + fingerprint);
108
kDebug(5001) << "Dowloading:" << url;
110
KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::Reload, KIO::HideProgressInfo);
111
m_jobs[job] = fingerprint;
112
connect(job, SIGNAL(finished(KJob*)), this, SLOT(slotDownloaded(KJob*)));
116
void KeyDownloader::slotDownloaded(KJob *job)
119
if (!m_jobs.contains(job)) {
123
const QString fingerprint = m_jobs[job];
124
KIO::StoredTransferJob *transferJob = static_cast<KIO::StoredTransferJob*>(job);
126
if (transferJob->isErrorPage()) {
127
kDebug(5001) << "Mirror did not work, try another one.";
128
downloadKey(fingerprint, 0, true);
133
QByteArray data = transferJob->data();
134
if (data.isEmpty()) {
135
kDebug(5001) << "Downloaded data is empty.";
136
downloadKey(fingerprint, 0, true);
140
const int indexStart = data.indexOf("<pre>");
141
const int indexEnd = data.indexOf("</pre>", indexStart);
142
if ((indexStart == -1) || (indexEnd == -1)) {
143
kDebug(5001) << "Could not find a key.";
144
downloadKey(fingerprint, 0, true);
148
data = data.mid(indexStart + 6, indexEnd - indexStart - 6);
150
GpgME::initializeLibrary();
151
GpgME::Error err = GpgME::checkEngine(GpgME::OpenPGP);
153
kDebug(5001) << "Problem checking the engine.";
157
QScopedPointer<GpgME::Context> context(GpgME::Context::createForProtocol(GpgME::OpenPGP));
158
if (!context.data()) {
159
kDebug(5001) << "Could not create context.";
163
QGpgME::QByteArrayDataProvider keyBA(data);
164
GpgME::Data key(&keyBA);
165
GpgME::ImportResult importResult = context->importKeys(key);
166
err = importResult.error();
168
kDebug(5001) << "Error while importing key.";;
172
kDebug(5001) << "Key downloaded, notifying requesters.";
174
QList<Signature*> sigs = m_downloading.values(fingerprint);
175
foreach (Signature *sig, sigs) {
176
sig->d->signatureDownloaded();
178
m_downloading.remove(fingerprint);
181
kWarning(5001) << "No QGPGME support.";
185
#include "keydownloader.moc"