~ubuntu-branches/ubuntu/oneiric/kdenetwork/oneiric-updates

« back to all changes in this revision

Viewing changes to kget/core/keydownloader.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac, Philip Muškovac
  • Date: 2011-07-10 12:36:35 UTC
  • mfrom: (1.1.59 upstream)
  • Revision ID: package-import@ubuntu.com-20110710123635-3db9oyxdswp4sp1e
Tags: 4:4.6.90-0ubuntu1
* New upstream release
 - Bump on kde-sc-dev-latest
 - s/kdebase-runtime-dev/kde-runtime-dev
* Dropped kubuntu_05_samba_sharing.diff <- went upstream
* Added kubuntu_05_make_old_symbols_reappear_and_fix_building.diff
* Refreshed *.install
* Refreshed symbols

[ Philip Muškovac ]
* fix debug package depends on kdebase-runtime-dbg -> kde-runtime-dbg 
  and recommends on kde-workspace-dbg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"