taskcollection.cpp 6.22 KB
Newer Older
1
2
3
4
/* -*- mode: c++; c-basic-offset:4 -*-
    crypto/taskcollection.cpp

    This file is part of Kleopatra, the KDE keymanager
5
    SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
6

7
8
    SPDX-FileCopyrightText: 2016 Bundesamt für Sicherheit in der Informationstechnik
    SPDX-FileContributor: Intevation GmbH
9

10
    SPDX-License-Identifier: GPL-2.0-or-later
11
12
13
14
15
*/

#include <config-kleopatra.h>

#include "taskcollection.h"
Allen Winter's avatar
Allen Winter committed
16
17
#include "task.h"
#include "kleopatra_debug.h"
18

19
#include <Libkleo/GnuPG>
20
21
22
23
24
25

#include <algorithm>
#include <map>

#include <cmath>

26
27
#include <KLocalizedString>

28
29
30
using namespace Kleo;
using namespace Kleo::Crypto;

Laurent Montel's avatar
Laurent Montel committed
31
32
33
class TaskCollection::Private
{
    TaskCollection *const q;
34
public:
Laurent Montel's avatar
Laurent Montel committed
35
    explicit Private(TaskCollection *qq);
36

Laurent Montel's avatar
Laurent Montel committed
37
    void taskProgress(const QString &, int, int);
38
    void taskResult(const std::shared_ptr<const Task::Result> &);
39
40
41
    void taskStarted();
    void calculateAndEmitProgress();

42
    std::map<int, std::shared_ptr<Task> > m_tasks;
43
44
    mutable quint64 m_totalProgress;
    mutable quint64 m_progress;
Marc Mutz's avatar
Marc Mutz committed
45
    unsigned int m_nCompleted;
46
    unsigned int m_nErrors;
47
48
    QString m_lastProgressMessage;
    bool m_errorOccurred;
49
    bool m_doneEmitted;
50
51
};

52
53
54
55
56
57
58
59
TaskCollection::Private::Private(TaskCollection *qq):
    q(qq),
    m_totalProgress(0),
    m_progress(0),
    m_nCompleted(0),
    m_nErrors(0),
    m_errorOccurred(false),
    m_doneEmitted(false)
60
61
62
63
64
65
66
67
{
}

int TaskCollection::numberOfCompletedTasks() const
{
    return d->m_nCompleted;
}

Laurent Montel's avatar
Laurent Montel committed
68
69
size_t TaskCollection::size() const
{
70
71
72
    return d->m_tasks.size();
}

73
74
bool TaskCollection::allTasksCompleted() const
{
75
    Q_ASSERT(d->m_nCompleted <= d->m_tasks.size());
76
77
78
    return d->m_nCompleted == d->m_tasks.size();
}

Laurent Montel's avatar
Laurent Montel committed
79
void TaskCollection::Private::taskProgress(const QString &msg, int, int)
80
81
82
83
84
{
    m_lastProgressMessage = msg;
    calculateAndEmitProgress();
}

85
void TaskCollection::Private::taskResult(const std::shared_ptr<const Task::Result> &result)
86
{
87
    Q_ASSERT(result);
88
    ++m_nCompleted;
89
90
91
92
93

    if (result->hasError()) {
        m_errorOccurred = true;
        ++m_nErrors;
    }
94
95
    m_lastProgressMessage.clear();
    calculateAndEmitProgress();
Laurent Montel's avatar
Laurent Montel committed
96
    Q_EMIT q->result(result);
97
    if (!m_doneEmitted && q->allTasksCompleted()) {
Laurent Montel's avatar
Laurent Montel committed
98
        Q_EMIT q->done();
99
        m_doneEmitted = true;
Laurent Montel's avatar
Laurent Montel committed
100
    }
101
102
103
104
}

void TaskCollection::Private::taskStarted()
{
Laurent Montel's avatar
Laurent Montel committed
105
    const Task *const task = qobject_cast<Task *>(q->sender());
106
107
    Q_ASSERT(task);
    Q_ASSERT(m_tasks.find(task->id()) != m_tasks.end());
Laurent Montel's avatar
Laurent Montel committed
108
    Q_EMIT q->started(m_tasks[task->id()]);
Thomas McGuire's avatar
Thomas McGuire committed
109
    calculateAndEmitProgress(); // start Knight-Rider-Mode right away (gpgsm doesn't report _any_ progress).
Andre Heinecke's avatar
Andre Heinecke committed
110
111
112
    if (m_doneEmitted) {
        // We are not done anymore, one task restarted.
        m_nCompleted--;
113
        m_nErrors--;
Andre Heinecke's avatar
Andre Heinecke committed
114
115
        m_doneEmitted = false;
    }
116
117
118
119
}

void TaskCollection::Private::calculateAndEmitProgress()
{
120
121
122
    quint64 total = 0;
    quint64 processed = 0;

123
    static bool haveWorkingProgress = engineIsVersion(2, 1, 15);
124
125
126
127
128
129
130
131
132
133
134
135
    if (!haveWorkingProgress) {
        // GnuPG before 2.1.15 would overflow on progress values > max int.
        // and did not emit a total for our Qt data types.
        // As we can't know if it overflowed or what the total is we just knight
        // rider in that case
        if (m_doneEmitted) {
            Q_EMIT q->progress(m_lastProgressMessage, 1, 1);
        } else {
            Q_EMIT q->progress(m_lastProgressMessage, 0, 0);
        }
        return;
    }
136

137
    bool unknowable = false;
Laurent Montel's avatar
Laurent Montel committed
138
    for (auto it = m_tasks.begin(), end = m_tasks.end(); it != end; ++it) {
139
        // Sum up progress and totals
140
        const std::shared_ptr<Task> &i = it->second;
141
        Q_ASSERT(i);
142
143
144
145
146
        if (!i->totalProgress()) {
            // There still might be jobs for which we don't know the progress.
            qCDebug(KLEOPATRA_LOG) << "Task: " << i->label() << " has no total progress set. ";
            unknowable = true;
            break;
Laurent Montel's avatar
Laurent Montel committed
147
        }
148
149
        processed += i->currentProgress();
        total += i->totalProgress();
Laurent Montel's avatar
Laurent Montel committed
150
    }
151
152
153
154

    m_totalProgress = total;
    m_progress = processed;
    if (!unknowable && processed && total >= processed) {
Allen Winter's avatar
Allen Winter committed
155
        // Scale down to avoid range issues.
156
157
        int scaled = 1000 * (m_progress / static_cast<double>(m_totalProgress));
        qCDebug(KLEOPATRA_LOG) << "Collection Progress: " << scaled << " total: " << 1000;
158
159
160
161
162
163
164
        if (total == processed) {
            // This can happen when an output is finalizing, e.g. extracting an
            // archive.
            Q_EMIT q->progress(i18n("Finalizing output..."), 0, 0);
        } else {
            Q_EMIT q->progress(m_lastProgressMessage, scaled, 1000);
        }
165
166
167
168
169
170
    } else {
        if (total < processed) {
            qCDebug(KLEOPATRA_LOG) << "Total progress is smaller then current progress.";
        }
        // Knight rider.
        Q_EMIT q->progress(m_lastProgressMessage, 0, 0);
Laurent Montel's avatar
Laurent Montel committed
171
    }
172
173
}

Laurent Montel's avatar
Laurent Montel committed
174
TaskCollection::TaskCollection(QObject *parent) : QObject(parent), d(new Private(this))
175
176
177
178
179
180
181
182
183
184
185
186
{
}

TaskCollection::~TaskCollection()
{
}

bool TaskCollection::isEmpty() const
{
    return d->m_tasks.empty();
}

187
188
189
190
191
bool TaskCollection::errorOccurred() const
{
    return d->m_errorOccurred;
}

192
193
194
195
196
bool TaskCollection::allTasksHaveErrors() const
{
    return d->m_nErrors == d->m_nCompleted;
}

197
std::shared_ptr<Task> TaskCollection::taskById(int id) const
198
{
Laurent Montel's avatar
Laurent Montel committed
199
    const auto it = d->m_tasks.find(id);
200
    return it != d->m_tasks.end() ? it->second : std::shared_ptr<Task>();
201
202
}

203
std::vector<std::shared_ptr<Task> > TaskCollection::tasks() const
204
{
205
    std::vector<std::shared_ptr<Task> > res;
Laurent Montel's avatar
Laurent Montel committed
206
    res.reserve(d->m_tasks.size());
Laurent Montel's avatar
Laurent Montel committed
207
    for (auto it = d->m_tasks.begin(), end = d->m_tasks.end(); it != end; ++it) {
Laurent Montel's avatar
Laurent Montel committed
208
209
        res.push_back(it->second);
    }
210
211
212
    return res;
}

213
void TaskCollection::setTasks(const std::vector<std::shared_ptr<Task> > &tasks)
214
{
Laurent Montel's avatar
Laurent Montel committed
215
    for (const std::shared_ptr<Task> &i : tasks) {
216
        Q_ASSERT(i);
217
        d->m_tasks[i->id()] = i;
Laurent Montel's avatar
Laurent Montel committed
218
219
        connect(i.get(), SIGNAL(progress(QString,int,int)),
                this, SLOT(taskProgress(QString,int,int)));
220
221
        connect(i.get(), SIGNAL(result(std::shared_ptr<const Kleo::Crypto::Task::Result>)),
                this, SLOT(taskResult(std::shared_ptr<const Kleo::Crypto::Task::Result>)));
Laurent Montel's avatar
Laurent Montel committed
222
223
        connect(i.get(), SIGNAL(started()),
                this, SLOT(taskStarted()));
224
225
226
    }
}

Stephen Kelly's avatar
Stephen Kelly committed
227
#include "moc_taskcollection.cpp"