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

« back to all changes in this revision

Viewing changes to plasma/generic/applets/notifications/protocols/jobs/dbusjobprotocol.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 "dbusjob.h"
 
21
#include "dbusjobprotocol.h"
 
22
 
 
23
 
 
24
#include <KConfigGroup>
 
25
 
 
26
#include <Plasma/DataEngineManager>
 
27
#include <Plasma/Service>
 
28
#include <Plasma/ServiceJob>
 
29
 
 
30
static const char engineName[] = "applicationjobs";
 
31
 
 
32
DBusJobProtocol::DBusJobProtocol(Manager *parent)
 
33
    : Protocol(parent),
 
34
      m_manager(parent),
 
35
      m_engine(0)
 
36
{
 
37
}
 
38
 
 
39
 
 
40
DBusJobProtocol::~DBusJobProtocol()
 
41
{
 
42
    if (m_engine) {
 
43
        Plasma::DataEngineManager::self()->unloadEngine(engineName);
 
44
    }
 
45
 
 
46
    foreach (DBusJob *job, m_jobs) {
 
47
        disconnect(job);
 
48
        job->destroy();
 
49
    }
 
50
 
 
51
    m_jobs.clear();
 
52
}
 
53
 
 
54
 
 
55
void DBusJobProtocol::init()
 
56
{
 
57
    m_engine = Plasma::DataEngineManager::self()->loadEngine(engineName);
 
58
 
 
59
    if (!m_engine->isValid()) {
 
60
        Plasma::DataEngineManager::self()->unloadEngine(engineName);
 
61
        m_engine = 0;
 
62
        return;
 
63
    }
 
64
 
 
65
    connect(m_engine, SIGNAL(sourceAdded(const QString&)),
 
66
            this, SLOT(prepareJob(const QString&)));
 
67
    connect(m_engine, SIGNAL(sourceRemoved(const QString&)),
 
68
            this, SLOT(removeJob(const QString&)));
 
69
}
 
70
 
 
71
void DBusJobProtocol::prepareJob(const QString &source)
 
72
{
 
73
    m_engine->connectSource(source, this);
 
74
}
 
75
 
 
76
void DBusJobProtocol::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
 
77
{
 
78
    DBusJob *job = m_jobs.value(source, 0);
 
79
 
 
80
    if (!job) {
 
81
        job = new DBusJob(source, this);
 
82
        m_jobs.insert(source, job);
 
83
        connect(job, SIGNAL(jobDeleted(const QString&)),
 
84
                this, SLOT(removeJob(const QString&)));
 
85
        connect(job, SIGNAL(suspend(const QString&)),
 
86
                this, SLOT(suspend(const QString&)));
 
87
        connect(job, SIGNAL(resume(const QString&)),
 
88
                this, SLOT(resume(const QString&)));
 
89
        connect(job, SIGNAL(stop(const QString&)),
 
90
                this, SLOT(stop(const QString&)));
 
91
        connect(job, SIGNAL(ready(Job*)),
 
92
                this, SIGNAL(jobCreated(Job*)));
 
93
    }
 
94
 
 
95
    job->setApplicationName(data.value("appName").toString());
 
96
    job->setApplicationIconName(data.value("appIconName").toString());
 
97
    job->setPercentage(data["percentage"].toUInt());
 
98
    job->setError(data["error"].toString());
 
99
    job->setMessage(data["infoMessage"].toString());
 
100
    job->setSuspendable(data["suspendable"].toBool());
 
101
    job->setKillable(data["killable"].toBool());
 
102
    job->setSpeed(data["speed"].toString());
 
103
    job->setNumericSpeed(data["numericSpeed"].toLongLong());
 
104
    job->setEta(data["eta"].toULongLong());
 
105
 
 
106
    if (data["state"].toString() == "running") {
 
107
        job->setState(Job::Running);
 
108
    } else if (data["state"].toString() == "suspended") {
 
109
        job->setState(Job::Suspended);
 
110
    } else {
 
111
        job->setState(Job::Stopped);
 
112
    }
 
113
 
 
114
    int i = 0;
 
115
    QList<QPair<QString, QString> > labels;
 
116
    while (data.contains(QString("label%1").arg(i))) {
 
117
        QPair<QString, QString> label;
 
118
        label.first = data[QString("labelName%1").arg(i)].toString();
 
119
        label.second = data[QString("label%1").arg(i)].toString();
 
120
        labels << label;
 
121
        i++;
 
122
    }
 
123
    job->setLabels(labels);
 
124
 
 
125
    i = 0;
 
126
    QMap<QString, qlonglong> totalAmounts;
 
127
    while (data.contains(QString("totalUnit%1").arg(i))) {
 
128
        QString unit = data[QString("totalUnit%1").arg(i)].toString();
 
129
        qlonglong amount = data[QString("totalAmount%1").arg(i)].toLongLong();
 
130
        totalAmounts[unit] = amount;
 
131
        i++;
 
132
    }
 
133
    job->setTotalAmounts(totalAmounts);
 
134
 
 
135
    i = 0;
 
136
    QMap<QString, qlonglong> processedAmounts;
 
137
    while (data.contains(QString("processedUnit%1").arg(i))) {
 
138
        QString unit = data[QString("processedUnit%1").arg(i)].toString();
 
139
        qlonglong amount = data[QString("processedAmount%1").arg(i)].toLongLong();
 
140
        processedAmounts[unit] = amount;
 
141
        i++;
 
142
    }
 
143
 
 
144
    job->setProcessedAmounts(processedAmounts);
 
145
}
 
146
 
 
147
void DBusJobProtocol::removeJob(const QString &source)
 
148
{
 
149
    if (m_jobs.contains(source)) {
 
150
        DBusJob *job = m_jobs.take(source);
 
151
        job->setState(Job::Stopped);
 
152
        job->destroy();
 
153
    }
 
154
}
 
155
 
 
156
void DBusJobProtocol::suspend(const QString &source)
 
157
{
 
158
    Plasma::Service *service = m_engine->serviceForSource(source);
 
159
    KConfigGroup op = service->operationDescription("suspend");
 
160
    KJob *job = service->startOperationCall(op);
 
161
    connect(job, SIGNAL(finished(KJob*)), service, SLOT(deleteLater()));
 
162
}
 
163
 
 
164
void DBusJobProtocol::resume(const QString &source)
 
165
{
 
166
    Plasma::Service *service = m_engine->serviceForSource(source);
 
167
    KConfigGroup op = service->operationDescription("resume");
 
168
    KJob *job = service->startOperationCall(op);
 
169
    connect(job, SIGNAL(finished(KJob*)), service, SLOT(deleteLater()));
 
170
}
 
171
 
 
172
void DBusJobProtocol::stop(const QString &source)
 
173
{
 
174
    Plasma::Service *service = m_engine->serviceForSource(source);
 
175
    KConfigGroup op = service->operationDescription("stop");
 
176
    KJob *job = service->startOperationCall(op);
 
177
    connect(job, SIGNAL(finished(KJob*)), service, SLOT(deleteLater()));
 
178
}
 
179
 
 
180
#include "dbusjobprotocol.moc"