2 * Copyright © 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 Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "jobviewadaptor.h"
20 #include "jobviewserveradaptor.h"
21 #include "kuiserverengine.h"
22 #include "jobcontrol.h"
24 #include <QDBusConnection>
28 #include <Plasma/DataEngine>
31 uint
KuiserverEngine::s_jobId
= 0;
33 JobView::JobView(QObject
* parent
)
35 m_percent(0), m_speed(0), m_state(Running
)
37 m_objectPath
.setPath(QString("/JobViewServer/JobView_%1").arg(++KuiserverEngine::s_jobId
));
39 new JobViewAdaptor(this);
40 QDBusConnection::sessionBus().registerObject(m_objectPath
.path(), this);
41 m_jobId
= KuiserverEngine::s_jobId
;
44 void JobView::terminate(const QString
&errorMessage
)
46 m_error
= errorMessage
;
48 emit
viewUpdated(this);
51 void JobView::setSuspended(bool suspended
)
59 emit
viewUpdated(this);
62 void JobView::setTotalAmount(qlonglong amount
, const QString
&unit
)
64 m_totalMap
[unit
] = amount
;
65 emit
viewUpdated(this);
68 void JobView::setProcessedAmount(qlonglong amount
, const QString
&unit
)
70 m_processedMap
[unit
] = amount
;
71 emit
viewUpdated(this);
74 void JobView::setPercent(uint percent
)
77 emit
viewUpdated(this);
80 void JobView::setSpeed(qlonglong bytesPerSecond
)
82 m_speed
= bytesPerSecond
;
83 emit
viewUpdated(this);
86 QString
JobView::speedString() const
88 //FIXME: how to i18n this?
89 return QString("%1/s").arg(KGlobal::locale()->formatByteSize(m_speed
));
92 void JobView::setInfoMessage(const QString
&infoMessage
)
94 m_infoMessage
= infoMessage
;
95 emit
viewUpdated(this);
98 bool JobView::setDescriptionField(uint number
, const QString
&name
, const QString
&value
)
100 m_labels
[number
] = value
;
101 m_labelNames
[number
] = name
;
102 emit
viewUpdated(this);
106 void JobView::clearDescriptionField(uint number
)
108 m_labels
.remove(number
);
109 m_labelNames
.remove(number
);
112 void JobView::setAppName(const QString
&appName
)
117 void JobView::setAppIconName(const QString
&appIconName
)
119 m_appIconName
= appIconName
;
122 void JobView::setCapabilities(int capabilities
)
124 m_capabilities
= capabilities
;
127 QString
JobView::sourceName() const
129 return QString("Job %1").arg(m_jobId
);
132 QDBusObjectPath
JobView::objectPath() const
137 KuiserverEngine::KuiserverEngine(QObject
* parent
, const QVariantList
& args
)
138 : Plasma::DataEngine(parent
, args
)
140 new JobViewServerAdaptor(this);
142 QDBusConnection::sessionBus().registerService(QLatin1String("org.kde.JobViewServer"));
143 QDBusConnection::sessionBus().registerObject(QLatin1String("/JobViewServer"), this);
145 setMinimumPollingInterval(500);
148 KuiserverEngine::~KuiserverEngine()
150 QDBusConnection::sessionBus().unregisterService("org.kde.JobViewServer");
151 qDeleteAll(m_jobViews
);
154 QDBusObjectPath
KuiserverEngine::requestView(const QString
&appName
,
155 const QString
&appIconName
, int capabilities
)
157 JobView
*jobView
= new JobView();
158 connect(jobView
, SIGNAL(viewUpdated(JobView
*)),
159 this, SLOT(sourceUpdated(JobView
*)));
161 jobView
->setAppName(appName
);
162 jobView
->setAppIconName(appIconName
);
163 jobView
->m_appName
= appName
;
164 jobView
->m_appIconName
= appIconName
;
165 jobView
->m_capabilities
= capabilities
;
167 m_jobViews
[jobView
->sourceName()] = jobView
;
168 return jobView
->objectPath();
171 Plasma::Service
* KuiserverEngine::serviceForSource(const QString
& source
)
173 if (m_jobViews
.contains(source
)) {
174 return new JobControl(this, m_jobViews
[source
]);
176 return DataEngine::serviceForSource(source
);
180 void KuiserverEngine::init()
184 void KuiserverEngine::sourceUpdated(JobView
*jobView
)
186 QString sourceName
= jobView
->sourceName();
188 Plasma::DataEngine::Data data
;
189 data
["appName"] = jobView
->m_appName
;
190 data
["appIconName"] = jobView
->m_appIconName
;
191 data
["percentage"] = jobView
->m_percent
;
192 data
["suspendable"] = (jobView
->m_capabilities
& KJob::Suspendable
);
193 data
["killable"] = (jobView
->m_capabilities
& KJob::Killable
);
194 data
["infoMessage"] = jobView
->m_infoMessage
;
196 if (!jobView
->m_error
.isEmpty()) {
197 data
["error"] = jobView
->m_error
;
200 if (jobView
->m_state
== JobView::Running
) {
201 data
["speed"] = jobView
->speedString();
204 for (int i
= 0; i
< jobView
->m_labels
.count(); i
++) {
205 data
[QString("label%1").arg(i
)] = jobView
->m_labels
[i
];
206 data
[QString("labelName%1").arg(i
)] = jobView
->m_labelNames
[i
];
210 foreach (const QString
&unit
, jobView
->m_totalMap
.keys()) {
211 data
[QString("totalUnit%1").arg(i
)] = unit
;
212 data
[QString("totalAmount%1").arg(i
++)] = jobView
->m_totalMap
[unit
];
216 foreach (const QString
&unit
, jobView
->m_processedMap
.keys()) {
217 data
[QString("processedUnit%1").arg(i
)] = unit
;
218 data
[QString("processedAmount%1").arg(i
++)] = jobView
->m_processedMap
[unit
];
221 switch (jobView
->m_state
) {
222 case JobView::Running
:
223 data
["state"] = "running";
224 setData(sourceName
, data
);
226 case JobView::Suspended
:
227 data
["state"] = "suspended";
228 setData(sourceName
, data
);
230 case JobView::Stopped
:
231 data
["state"] = "stopped";
232 setData(sourceName
, data
);
233 removeSource(sourceName
);
239 K_EXPORT_PLASMA_DATAENGINE(kuiserver
, KuiserverEngine
)
241 #include "kuiserverengine.moc"