not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / systemtray / protocols / jobs / dbusjobprotocol.cpp
blobe0facaded3caff81a6341cd1dff63af0b959486d
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 ***************************************************************************/
20 #include "dbusjob.h"
21 #include "dbusjobprotocol.h"
24 #include <KConfigGroup>
25 #include <KIcon>
27 #include <plasma/dataenginemanager.h>
28 #include <plasma/service.h>
31 namespace SystemTray
34 static const char *engineName = "applicationjobs";
36 DBusJobProtocol::DBusJobProtocol(QObject *parent)
37 : Protocol(parent),
38 m_engine(0)
43 DBusJobProtocol::~DBusJobProtocol()
45 if (m_engine) {
46 Plasma::DataEngineManager::self()->unloadEngine(engineName);
51 void DBusJobProtocol::init()
53 m_engine = Plasma::DataEngineManager::self()->loadEngine(engineName);
55 if (!m_engine->isValid()) {
56 m_engine = 0;
57 return;
60 connect(m_engine, SIGNAL(sourceAdded(const QString&)),
61 this, SLOT(prepareJob(const QString&)));
62 connect(m_engine, SIGNAL(sourceRemoved(const QString&)),
63 this, SLOT(removeJob(const QString&)));
66 void DBusJobProtocol::prepareJob(const QString &source)
68 m_engine->connectSource(source, this);
72 void DBusJobProtocol::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
74 DBusJob *job = m_jobs.value(source, 0);
76 if (!job) {
77 job = new DBusJob(source, this);
78 m_jobs.insert(source, job);
79 connect(job, SIGNAL(jobDeleted(const QString&)),
80 this, SLOT(removeJob(const QString&)));
81 connect(job, SIGNAL(suspend(const QString&)),
82 this, SLOT(suspend(const QString&)));
83 connect(job, SIGNAL(resume(const QString&)),
84 this, SLOT(resume(const QString&)));
85 connect(job, SIGNAL(stop(const QString&)),
86 this, SLOT(stop(const QString&)));
87 connect(job, SIGNAL(ready(SystemTray::Job*)),
88 this, SIGNAL(jobCreated(SystemTray::Job*)));
91 job->setApplicationName(data.value("appName").toString());
92 job->setApplicationIconName(data.value("appIconName").toString());
93 job->setPercentage(data["percentage"].toUInt());
94 job->setError(data["error"].toString());
95 job->setMessage(data["infoMessage"].toString());
96 job->setSuspendable(data["suspendable"].toBool());
97 job->setKillable(data["killable"].toBool());
98 job->setSpeed(data["speed"].toString());
100 if (data["state"].toString() == "running") {
101 job->setState(Job::Running);
102 } else if (data["state"].toString() == "suspended") {
103 job->setState(Job::Suspended);
104 } else {
105 job->setState(Job::Stopped);
108 int i = 0;
109 QList<QPair<QString, QString> > labels;
110 while (data.contains(QString("label%1").arg(i))) {
111 QPair<QString, QString> label;
112 label.first = data[QString("labelName%1").arg(i)].toString();
113 label.second = data[QString("label%1").arg(i)].toString();
114 labels << label;
115 i++;
117 job->setLabels(labels);
119 i = 0;
120 QMap<QString, qlonglong> totalAmounts;
121 while (data.contains(QString("totalUnit%1").arg(i))) {
122 QString unit = data[QString("totalUnit%1").arg(i)].toString();
123 qlonglong amount = data[QString("totalAmount%1").arg(i)].toLongLong();
124 totalAmounts[unit] = amount;
125 i++;
127 job->setTotalAmounts(totalAmounts);
129 i = 0;
130 QMap<QString, qlonglong> processedAmounts;
131 while (data.contains(QString("processedUnit%1").arg(i))) {
132 QString unit = data[QString("processedUnit%1").arg(i)].toString();
133 qlonglong amount = data[QString("processedAmount%1").arg(i)].toLongLong();
134 processedAmounts[unit] = amount;
135 i++;
138 job->setProcessedAmounts(processedAmounts);
141 void DBusJobProtocol::removeJob(const QString &source)
143 if (m_jobs.contains(source)) {
144 m_jobs[source]->setState(Job::Stopped);
145 m_jobs.take(source)->destroy();
149 void DBusJobProtocol::suspend(const QString &source)
151 Plasma::Service *service = m_engine->serviceForSource(source);
152 KConfigGroup op = service->operationDescription("suspend");
153 service->startOperationCall(op);
156 void DBusJobProtocol::resume(const QString &source)
158 Plasma::Service *service = m_engine->serviceForSource(source);
159 KConfigGroup op = service->operationDescription("resume");
160 service->startOperationCall(op);
163 void DBusJobProtocol::stop(const QString &source)
165 Plasma::Service *service = m_engine->serviceForSource(source);
166 KConfigGroup op = service->operationDescription("stop");
167 service->startOperationCall(op);
172 #include "dbusjobprotocol.moc"