1 /***************************************************************************
2 * Copyright (C) 2008 Rob Scheepmaker <r.scheepmaker@student.utwente.nl> *
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. *
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. *
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 ***************************************************************************/
21 #include "dbusjobprotocol.h"
24 #include <KConfigGroup>
27 #include <plasma/dataenginemanager.h>
28 #include <plasma/service.h>
34 static const char *engineName
= "applicationjobs";
36 DBusJobProtocol::DBusJobProtocol(QObject
*parent
)
43 DBusJobProtocol::~DBusJobProtocol()
46 Plasma::DataEngineManager::self()->unloadEngine(engineName
);
51 void DBusJobProtocol::init()
53 m_engine
= Plasma::DataEngineManager::self()->loadEngine(engineName
);
55 if (!m_engine
->isValid()) {
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);
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
);
105 job
->setState(Job::Stopped
);
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();
117 job
->setLabels(labels
);
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
;
127 job
->setTotalAmounts(totalAmounts
);
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
;
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"