• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.14
  • kdenetwork
  • kget
  • core
keydownloader.cpp
Go to the documentation of this file.
1 /**************************************************************************
2 * Copyright (C) 2009-2011 Matthias Fuchs <mat69@gmx.net> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19 
20 #include "keydownloader.h"
21 #include "settings.h"
22 #include "signature_p.h"
23 
24 #include <KDebug>
25 #include <KIO/Job>
26 #include <KLocale>
27 #include <KMessageBox>
28 
29 #ifdef HAVE_QGPGME
30 #include <gpgme++/context.h>
31 #include <gpgme++/data.h>
32 #include <gpgme++/importresult.h>
33 #include <qgpgme/dataprovider.h>
34 #endif
35 
36 KeyDownloader::KeyDownloader(QObject *parent)
37  : QObject(parent)
38 {
39 }
40 
41 bool KeyDownloader::isValid() const
42 {
43 #ifdef HAVE_QGPGME
44  return true;
45 #else //HAVE_QGPGME
46  return false;
47 #endif //HAVE_QGPGME
48 }
49 
50 void KeyDownloader::downloadKey(QString fingerprint, Signature *sig)
51 {
52  downloadKey(fingerprint, sig, false);
53 }
54 
55 void KeyDownloader::downloadKey(QString fingerprint, Signature *sig, bool mirrorFailed)
56 {
57  if (fingerprint.isEmpty() || (!sig && !mirrorFailed)) {
58  return;
59  }
60 
61  if (!fingerprint.startsWith(QLatin1String("0x"))) {
62  fingerprint = "0x" + fingerprint;
63  }
64 
65  if (m_downloading.contains(fingerprint) && !mirrorFailed) {
66  if (!m_downloading.contains(fingerprint, sig)) {
67  m_downloading.insert(fingerprint, sig);
68  }
69  } else {
70  const QStringList servers = Settings::signatureKeyServers();
71  if (!servers.count()) {
72  KMessageBox::error(0,
73  i18n("No server for downloading keys is specified in settings. Downloading aborted."),
74  i18n("No key server"));
75  return;
76  }
77 
78  QString mirror;
79  if (mirrorFailed) {
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);
84  break;
85  }
86  }
87  } else {
88  mirror = servers.first();
89  }
90 
91  if (mirror.isEmpty()) {
92  KMessageBox::error(0,
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"));
95  return;
96  }
97 
98  m_triedMirrors.insert(fingerprint, mirror);
99  if (!mirrorFailed) {
100  m_downloading.insert(fingerprint, sig);
101  }
102 
103  KUrl url(mirror);
104  url.addPath("pks/lookup");
105  url.setQuery("op=get&options=mr&search=" + fingerprint);
106  url.setPort(11371);
107 
108  kDebug(5001) << "Dowloading:" << url;
109 
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*)));
113  }
114 }
115 
116 void KeyDownloader::slotDownloaded(KJob *job)
117 {
118 #ifdef HAVE_QGPGME
119  if (!m_jobs.contains(job)) {
120  return;
121  }
122 
123  const QString fingerprint = m_jobs[job];
124  KIO::StoredTransferJob *transferJob = static_cast<KIO::StoredTransferJob*>(job);
125 
126  if (transferJob->isErrorPage()) {
127  kDebug(5001) << "Mirror did not work, try another one.";
128  downloadKey(fingerprint, 0, true);
129  return;
130  }
131 
132 
133  QByteArray data = transferJob->data();
134  if (data.isEmpty()) {
135  kDebug(5001) << "Downloaded data is empty.";
136  downloadKey(fingerprint, 0, true);
137  return;
138  }
139 
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);
145  return;
146  }
147 
148  data = data.mid(indexStart + 6, indexEnd - indexStart - 6);
149 
150  GpgME::initializeLibrary();
151  GpgME::Error err = GpgME::checkEngine(GpgME::OpenPGP);
152  if (err) {
153  kDebug(5001) << "Problem checking the engine.";
154  return;
155  }
156 
157  QScopedPointer<GpgME::Context> context(GpgME::Context::createForProtocol(GpgME::OpenPGP));
158  if (!context.data()) {
159  kDebug(5001) << "Could not create context.";
160  return;
161  }
162 
163  QGpgME::QByteArrayDataProvider keyBA(data);
164  GpgME::Data key(&keyBA);
165  GpgME::ImportResult importResult = context->importKeys(key);
166  err = importResult.error();
167  if (err) {
168  kDebug(5001) << "Error while importing key.";;
169  return;
170  }
171 
172  kDebug(5001) << "Key downloaded, notifying requesters.";
173 
174  QList<Signature*> sigs = m_downloading.values(fingerprint);
175  foreach (Signature *sig, sigs) {
176  sig->d->signatureDownloaded();
177  }
178  m_downloading.remove(fingerprint);
179 #else //HAVE_QGPGME
180  Q_UNUSED(job)
181  kWarning(5001) << "No QGPGME support.";
182 #endif //HAVE_QGPGME
183 }
184 
185 #include "keydownloader.moc"
QByteArray
KeyDownloader::KeyDownloader
KeyDownloader(QObject *parent=0)
Definition: keydownloader.cpp:36
QList::at
const T & at(int i) const
QByteArray::isEmpty
bool isEmpty() const
keydownloader.h
mirror
Definition: mirrors.h:17
KeyDownloader::downloadKey
void downloadKey(QString fingerprint, Signature *sig)
Searches for a key with fignerprint.
Definition: keydownloader.cpp:50
QMultiHash::contains
bool contains(const Key &key) const
QByteArray::indexOf
int indexOf(char ch, int from) const
QList::count
int count(const T &value) const
SignaturePrivate::signatureDownloaded
void signatureDownloaded()
Starts verification again if a verification was tried before but aborted because of missing keys...
Definition: signature.cpp:56
QObject
QScopedPointer
QString::isEmpty
bool isEmpty() const
QMultiHash::remove
int remove(const Key &key)
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QList::first
T & first()
QString
Settings::signatureKeyServers
static QStringList signatureKeyServers()
Get SignatureKeyServers.
Definition: settings.h:1095
QList
QMultiHash::insert
QHash< Key, T >::iterator insert(const Key &key, const T &value)
QByteArray::mid
QByteArray mid(int pos, int len) const
QStringList
signature_p.h
KeyDownloader::isValid
bool isValid() const
Definition: keydownloader.cpp:41
settings.h
QLatin1String
QByteArray::data
char * data()
Signature
Class to verify signatures.
Definition: signature.h:38
QHash::contains
bool contains(const Key &key) const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:28:43 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal