1 /***************************************************************************
2 * Copyright 2008 by 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 ***************************************************************************/
20 #include "jobwidget.h"
21 #include "../core/job.h"
23 #include <KGlobalSettings>
27 #include <QGraphicsGridLayout>
29 #include <plasma/widgets/meter.h>
30 #include <Plasma/DataEngine>
31 #include <Plasma/Service>
32 #include <Plasma/ExtenderItem>
33 #include <Plasma/Theme>
34 #include <Plasma/Label>
35 #include <Plasma/Meter>
37 JobWidget::JobWidget(SystemTray::Job
*job
, Plasma::ExtenderItem
*parent
)
38 : QGraphicsWidget(parent
),
39 m_extenderItem(parent
),
42 Q_ASSERT(m_extenderItem
);
44 m_meter
= new Plasma::Meter(this);
45 m_meter
->setSvg("widgets/bar_meter_horizontal");
46 m_meter
->setMeterType(Plasma::Meter::BarMeterHorizontal
);
47 m_meter
->setMaximumHeight(16);
48 m_meter
->setMaximum(100);
51 m_fromNameLabel
= new Plasma::Label(this);
52 m_fromLabel
= new Plasma::Label(this);
53 m_toNameLabel
= new Plasma::Label(this);
54 m_toLabel
= new Plasma::Label(this);
55 m_speedLabel
= new Plasma::Label(this);
56 m_processedLabel
= new Plasma::Label(this);
57 m_totalBytesLabel
= new Plasma::Label(this);
59 m_fromNameLabel
->setAlignment(Qt::AlignRight
);
60 m_fromLabel
->setAlignment(Qt::AlignLeft
);
61 m_toNameLabel
->setAlignment(Qt::AlignRight
);
62 m_toLabel
->setAlignment(Qt::AlignLeft
);
63 m_speedLabel
->setAlignment(Qt::AlignRight
);
64 m_processedLabel
->setAlignment(Qt::AlignLeft
);
65 m_totalBytesLabel
->setAlignment(Qt::AlignRight
);
67 QGraphicsGridLayout
*layout
= new QGraphicsGridLayout(this);
68 layout
->addItem(m_fromNameLabel
, 0, 0);
69 layout
->addItem(m_fromLabel
, 0, 1, 1, 3);
70 layout
->addItem(m_toNameLabel
, 1, 0);
71 layout
->addItem(m_toLabel
, 1, 1, 1, 3);
73 layout
->addItem(m_speedLabel
, 2, 0);
74 layout
->addItem(m_processedLabel
, 2, 1);
75 layout
->addItem(m_totalBytesLabel
, 2, 3);
77 layout
->addItem(m_meter
, 3, 1, 1, 3);
82 connect(m_job
, SIGNAL(changed(SystemTray::Job
*)),
83 this, SLOT(updateJob()));
84 connect(m_job
, SIGNAL(destroyed(SystemTray::Job
*)),
85 this, SLOT(destroy()));
88 QAction
*suspendAction
= new QAction(m_extenderItem
);
89 suspendAction
->setIcon(KIcon("media-playback-pause"));
90 suspendAction
->setEnabled(true);
91 suspendAction
->setVisible(false);
92 suspendAction
->setToolTip(i18n("Pause job"));
93 m_extenderItem
->addAction("suspend", suspendAction
);
94 connect(suspendAction
, SIGNAL(triggered()), m_job
,
98 QAction
*resumeAction
= new QAction(m_extenderItem
);
99 resumeAction
->setIcon(KIcon("media-playback-start"));
100 resumeAction
->setEnabled(true);
101 resumeAction
->setVisible(false);
102 resumeAction
->setToolTip(i18n("Resume job"));
103 m_extenderItem
->addAction("resume", resumeAction
);
104 connect(resumeAction
, SIGNAL(triggered()), m_job
,
107 //the friendly stop action
108 QAction
*stopAction
= new QAction(m_extenderItem
);
109 stopAction
->setIcon(KIcon("media-playback-stop"));
110 stopAction
->setEnabled(true);
111 stopAction
->setVisible(true);
112 stopAction
->setToolTip(i18n("Cancel job"));
113 m_extenderItem
->addAction("stop", stopAction
);
114 connect(stopAction
, SIGNAL(triggered()), m_job
,
119 m_extenderItem
->showCloseButton();
121 labelName0
= m_extenderItem
->config().readEntry("labelName0", "");
122 label0
= m_extenderItem
->config().readEntry("label0", "");
123 labelName1
= m_extenderItem
->config().readEntry("labelName1", "");
124 label1
= m_extenderItem
->config().readEntry("label1", "");
130 JobWidget::~JobWidget()
134 void JobWidget::destroy()
136 if (!m_extenderItem
->isDetached()) {
137 //TODO: make configurable:
138 m_extenderItem
->setAutoExpireDelay(15000);
140 m_meter
->setValue(100);
144 void JobWidget::updateJob()
146 m_meter
->setValue(m_job
->percentage());
148 Plasma::ExtenderItem
*item
= m_extenderItem
;
151 if (m_job
->labels().count() > 0) {
152 labelName0
= m_job
->labels().value(0).first
;
153 label0
= m_job
->labels().value(0).second
;
155 if (m_job
->labels().count() > 1) {
156 labelName1
= m_job
->labels().value(1).first
;
157 label1
= m_job
->labels().value(1).second
;
159 KConfigGroup cg
= m_extenderItem
->config();
160 cg
.writeEntry("labelName0", labelName0
);
161 cg
.writeEntry("label0", label0
);
162 cg
.writeEntry("labelName1", labelName1
);
163 cg
.writeEntry("label1", label1
);
168 //show the current status in the title.
169 if (!m_job
->error().isEmpty()) {
170 item
->setTitle(m_job
->error());
171 } else if (m_job
->state() == SystemTray::Job::Running
) {
172 item
->setTitle(m_job
->message());
173 } else if (m_job
->state() == SystemTray::Job::Suspended
) {
175 i18nc("%1 is the name of the job, can be things like Copying, deleting, moving",
176 "(paused) %1", m_job
->message()));
179 i18nc("%1 is the name of the job, can be things like Copying, deleting, moving",
180 "(finished) %1", m_job
->message()));
181 item
->showCloseButton();
184 //set the correct actions to visible.
185 if (item
->action("suspend")) {
186 item
->action("suspend")->setVisible(m_job
->isSuspendable() &&
187 m_job
->state() == SystemTray::Job::Running
);
189 if (item
->action("resume")) {
190 item
->action("resume")->setVisible(m_job
->isSuspendable() &&
191 m_job
->state() == SystemTray::Job::Suspended
);
193 if (item
->action("stop")) {
194 item
->action("stop")->setVisible(m_job
->isKillable() &&
195 m_job
->state() != SystemTray::Job::Stopped
);
198 m_speedLabel
->setText(m_job
->speed());
199 m_processedLabel
->setText(KGlobal::locale()->formatByteSize(m_job
->processedAmounts()["bytes"]));
200 m_totalBytesLabel
->setText(KGlobal::locale()->formatByteSize(m_job
->totalAmounts()["bytes"]));
202 item
->setIcon(m_job
->applicationIconName());
205 void JobWidget::resizeEvent(QGraphicsSceneResizeEvent
*event
)
211 void JobWidget::updateLabels()
213 Plasma::Theme
*theme
= Plasma::Theme::defaultTheme();
214 QFont font
= theme
->font(Plasma::Theme::DefaultFont
);
215 QFontMetricsF
fm(font
);
216 if (!labelName0
.isEmpty()) {
217 m_fromNameLabel
->setText(QString("%1: ").arg(labelName0
));
219 if (label0
.startsWith("file://")) {
220 label0
= KUrl(label0
).toLocalFile();
223 m_fromLabel
->setText(fm
.elidedText(label0
, Qt::ElideMiddle
, m_fromLabel
->size().width()));
225 if (!labelName1
.isEmpty()) {
226 m_toNameLabel
->setText(QString("%1: ").arg(labelName1
));
228 if (label1
.startsWith("file://")) {
229 label1
= KUrl(label1
).toLocalFile();
231 m_toLabel
->setText(fm
.elidedText(label1
, Qt::ElideMiddle
, m_toLabel
->size().width()));
234 #include "jobwidget.moc"