~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/applets/notifications/core/job.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 Rob Scheepmaker <r.scheepmaker@student.utwente.nl> *
 
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 "job.h"
 
21
 
 
22
#include <QtCore/QTimer>
 
23
#include <QtCore/QTime>
 
24
#include <QTextDocument>
 
25
#include <QFontMetrics>
 
26
#include <QApplication>
 
27
 
 
28
#include <KDebug>
 
29
#include <KUrl>
 
30
#include <KLocalizedString>
 
31
 
 
32
 
 
33
class Job::Private
 
34
{
 
35
public:
 
36
    Private() :
 
37
        numericSpeed(0),
 
38
        finalElapsed(0),
 
39
        state(Running),
 
40
        percentage(0),
 
41
        eta(0),
 
42
        timerId(0),
 
43
        killable(false),
 
44
        suspendable(false),
 
45
        shown(false)
 
46
    {
 
47
    }
 
48
 
 
49
    QString applicationName;
 
50
    QString applicationIconName;
 
51
    QString message;
 
52
    QString error;
 
53
    QString speed;
 
54
    QString destination;
 
55
    qlonglong numericSpeed;
 
56
 
 
57
 
 
58
    QMap<QString, qlonglong> totalAmounts;
 
59
    QMap<QString, qlonglong> processedAmounts;
 
60
 
 
61
    QList<QPair<QString, QString> > labels;
 
62
 
 
63
    QTime elapsed;
 
64
    uint finalElapsed;
 
65
 
 
66
    State state;
 
67
    uint percentage;
 
68
    uint eta;
 
69
    int timerId;
 
70
 
 
71
    bool killable : 1;
 
72
    bool suspendable : 1;
 
73
    bool shown : 1;
 
74
};
 
75
 
 
76
Job::Job(QObject *parent)
 
77
    : QObject(parent),
 
78
      d(new Private)
 
79
{
 
80
    //delay a little the job to avoid the user to be distracted with short ones
 
81
    QTimer::singleShot(1500, this, SLOT(show()));
 
82
    d->elapsed.restart();
 
83
}
 
84
 
 
85
Job::~Job()
 
86
{
 
87
    delete d;
 
88
}
 
89
 
 
90
void Job::destroy()
 
91
{
 
92
    emit destroyed(this);
 
93
    deleteLater();
 
94
}
 
95
 
 
96
QString Job::applicationName() const
 
97
{
 
98
    return d->applicationName;
 
99
}
 
100
 
 
101
void Job::setApplicationName(const QString &applicationName)
 
102
{
 
103
    if (d->applicationName != applicationName) {
 
104
        d->applicationName = applicationName;
 
105
        scheduleChangedSignal();
 
106
    }
 
107
}
 
108
 
 
109
QString Job::applicationIconName() const
 
110
{
 
111
    return d->applicationIconName;
 
112
}
 
113
 
 
114
void Job::setApplicationIconName(const QString &applicationIcon)
 
115
{
 
116
    if (d->applicationIconName != applicationIcon) {
 
117
        d->applicationIconName = applicationIcon;
 
118
        scheduleChangedSignal();
 
119
    }
 
120
}
 
121
 
 
122
QString Job::message() const
 
123
{
 
124
    return d->message;
 
125
}
 
126
 
 
127
void Job::setMessage(const QString &message)
 
128
{
 
129
    if (d->message != message) {
 
130
        d->message = message;
 
131
        scheduleChangedSignal();
 
132
    }
 
133
}
 
134
 
 
135
QString Job::error() const
 
136
{
 
137
    return d->error;
 
138
}
 
139
 
 
140
void Job::setError(const QString &error)
 
141
{
 
142
    if (d->error != error) {
 
143
        d->error = error;
 
144
        scheduleChangedSignal();
 
145
    }
 
146
}
 
147
 
 
148
QString Job::speed() const
 
149
{
 
150
    return d->speed;
 
151
}
 
152
 
 
153
void Job::setSpeed(const QString &speed)
 
154
{
 
155
    if (d->speed != speed) {
 
156
        d->speed = speed;
 
157
        scheduleChangedSignal();
 
158
    }
 
159
}
 
160
 
 
161
qlonglong Job::numericSpeed() const
 
162
{
 
163
    return d->numericSpeed;
 
164
}
 
165
 
 
166
void Job::setNumericSpeed(const qlonglong speed)
 
167
{
 
168
    if (d->numericSpeed != speed) {
 
169
        d->numericSpeed = speed;
 
170
        scheduleChangedSignal();
 
171
    }
 
172
}
 
173
 
 
174
QString Job::completedMessage() const
 
175
{
 
176
    KUrl location(d->destination);
 
177
    if (location.isValid()) {
 
178
        if (totalAmounts().value("files") > 1) {
 
179
            location.setFileName(QString());
 
180
        }
 
181
 
 
182
        QString destinationString;
 
183
        if (location.isLocalFile()) {
 
184
            destinationString = location.toLocalFile();
 
185
        } else {
 
186
            destinationString = location.prettyUrl();
 
187
        }
 
188
 
 
189
        //FIXME: this is visualization stuff, but putting html here is not so model as well
 
190
 
 
191
        kDebug() << "href = " << location.url();
 
192
        QString destinationLink = QString("<a href=\"%1\">%2</a>").arg(location.url())
 
193
                                  .arg(Qt::escape(destinationString));
 
194
 
 
195
        if (totalAmounts().value("files") > 1) {
 
196
            return i18np("%1 file, to: %2", "%1 files, to: %2", totalAmounts().value("files"),
 
197
                         destinationLink);
 
198
        } else {
 
199
            return destinationLink;
 
200
        }
 
201
    } else {
 
202
        return QString("%1: %2").arg(labels().value(0).first).arg(labels().value(0).second);
 
203
    }
 
204
}
 
205
 
 
206
KUrl Job::destination() const
 
207
{
 
208
    return d->destination;
 
209
}
 
210
 
 
211
ulong Job::eta() const
 
212
{
 
213
    return d->eta;
 
214
}
 
215
 
 
216
void Job::setEta(ulong eta)
 
217
{
 
218
    d->eta = eta;
 
219
}
 
220
 
 
221
QMap<QString, qlonglong> Job::totalAmounts() const
 
222
{
 
223
    return d->totalAmounts;
 
224
}
 
225
 
 
226
void Job::setTotalAmounts(QMap<QString, qlonglong> amounts)
 
227
{
 
228
    d->totalAmounts = amounts;
 
229
    scheduleChangedSignal();
 
230
}
 
231
 
 
232
QMap<QString, qlonglong> Job::processedAmounts() const
 
233
{
 
234
    return d->processedAmounts;
 
235
}
 
236
 
 
237
void Job::setProcessedAmounts(QMap<QString, qlonglong> amounts)
 
238
{
 
239
    d->processedAmounts = amounts;
 
240
    scheduleChangedSignal();
 
241
}
 
242
 
 
243
Job::State Job::state() const
 
244
{
 
245
    return d->state;
 
246
}
 
247
 
 
248
void Job::setState(State state)
 
249
{
 
250
    if (d->state != state) {
 
251
        d->state = state;
 
252
        show();
 
253
        if (state == Stopped) {
 
254
            d->finalElapsed = d->elapsed.elapsed();
 
255
        }
 
256
        emit stateChanged(this);
 
257
    }
 
258
}
 
259
 
 
260
QList<QPair<QString, QString> > Job::labels() const
 
261
{
 
262
    return d->labels;
 
263
}
 
264
 
 
265
void Job::setLabels(QList<QPair<QString, QString> > labels)
 
266
{
 
267
    d->labels = labels;
 
268
    if (d->labels.count() > 1 && d->destination.isEmpty()) {
 
269
        d->destination = d->labels.value(1).second;
 
270
    }
 
271
    scheduleChangedSignal();
 
272
}
 
273
 
 
274
uint Job::percentage() const
 
275
{
 
276
    return d->percentage;
 
277
}
 
278
 
 
279
void Job::setPercentage(uint percentage)
 
280
{
 
281
    if (d->percentage != percentage) {
 
282
        d->percentage = percentage;
 
283
        scheduleChangedSignal();
 
284
    }
 
285
}
 
286
 
 
287
uint Job::elapsed() const
 
288
{
 
289
    if (d->finalElapsed) {
 
290
        return d->finalElapsed;
 
291
    } else {
 
292
        return d->elapsed.elapsed();
 
293
    }
 
294
}
 
295
 
 
296
bool Job::isSuspendable() const
 
297
{
 
298
    return d->suspendable;
 
299
}
 
300
 
 
301
void Job::setSuspendable(bool suspendable)
 
302
{
 
303
    if (d->suspendable != suspendable) {
 
304
        d->suspendable = suspendable;
 
305
        scheduleChangedSignal();
 
306
    }
 
307
}
 
308
 
 
309
bool Job::isKillable() const
 
310
{
 
311
    return d->killable;
 
312
}
 
313
 
 
314
void Job::setKillable(bool killable)
 
315
{
 
316
    if (d->killable != killable) {
 
317
        d->killable = killable;
 
318
        scheduleChangedSignal();
 
319
    }
 
320
}
 
321
 
 
322
void Job::suspend()
 
323
{
 
324
    kWarning() << "Suspend is not implemented in this job provider.";
 
325
}
 
326
 
 
327
void Job::resume()
 
328
{
 
329
    kWarning() << "Resume is not implemented in this job provider.";
 
330
}
 
331
 
 
332
void Job::stop()
 
333
{
 
334
    kWarning() << "Stop is not implemented in this job provider.";
 
335
}
 
336
 
 
337
void Job::show()
 
338
{
 
339
    if (state() == Job::Running && !d->shown) {
 
340
        d->shown = true;
 
341
        emit ready(this);
 
342
    }
 
343
}
 
344
 
 
345
void Job::scheduleChangedSignal()
 
346
{
 
347
    if (d->shown && !d->timerId) {
 
348
        d->timerId = startTimer(0);
 
349
    }
 
350
}
 
351
 
 
352
void Job::timerEvent(QTimerEvent *)
 
353
{
 
354
    killTimer(d->timerId);
 
355
    d->timerId = 0;
 
356
    emit changed(this);
 
357
}
 
358
 
 
359
 
 
360
#include "job.moc"