1 /***************************************************************************
2 * *
3 * This program is free software; you can redistribute it and/or modify *
4 * it under the terms of the GNU General Public License as published by *
5 * the Free Software Foundation; either version 2 of the License, or *
6 * (at your option) any later version. *
7 *
8 * Copyright (C) 2007 by Javier Goday <jgoday@gmail.com>
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
19 ***************************************************************************/
20
21 #include "panelbar/kgetpanelbar.h"
22 #include "panelbar/kgetpanelbar_p.h"
23 #include "common/kgetappletutils.h"
24
25 #include <QGridLayout>
26 #include <QGraphicsProxyWidget>
27 #include <QGraphicsLinearLayout>
28 #include <QLabel>
29 #include <QPainter>
30 #include <QProgressBar>
31
32 #include <QDebug>
33 #include <QIcon>
34 #include <KIconLoader>
35 #include <KLocale>
36 #include <KTitleWidget>
37 #include <KWindowSystem>
38 #include <kio/global.h>
39
40 #include <plasma/applet.h>
41 #include <plasma/dataengine.h>
42 #include <plasma/dialog.h>
43 #include <plasma/svg.h>
44 #include <plasma/theme.h>
45 #include <plasma/widgets/iconwidget.h>
46
47 const static int TOP_MARGIN = 60;
48 const static int SPACING = 4;
49 const static int MARGIN = 20;
50 const static int MAX_DOWNLOADS_PER_PAGE = 5;
51
Private(QWidget * parent)52 KGetPanelBar::Private::Private(QWidget *parent) : Plasma::Dialog(parent),
53 m_transfers()
54 {
55 m_dialogLayout = new QGridLayout(this);
56 m_dialogLayout->setColumnStretch(0, 0);
57 m_dialogLayout->setColumnStretch(1, 0);
58 m_dialogLayout->setColumnStretch(2, 1);
59 setLayout(m_dialogLayout);
60
61 KTitleWidget *title = new KTitleWidget(this);
62 title->setText(i18n("KGet transfers"));
63 title->setPixmap(QIcon::fromTheme("kget").pixmap(22, 22), KTitleWidget::ImageRight);
64 m_dialogLayout->addWidget(title, 0, 0, 1, 3);
65 }
66
~Private()67 KGetPanelBar::Private::~Private()
68 {
69 }
70
transfersAdded(const QList<OrgKdeKgetTransferInterface * > & transfers)71 void KGetPanelBar::Private::transfersAdded(const QList<OrgKdeKgetTransferInterface*> &transfers)
72 {
73 foreach (OrgKdeKgetTransferInterface* transfer, transfers) {
74 QLabel *icon = new QLabel(this);
75 QLabel *name = new QLabel(this);
76 QProgressBar *progressBar = new QProgressBar(this);
77
78 QString fileName = KUrl(transfer->source().value()).fileName();
79 icon->setPixmap(KIO::pixmapForUrl(fileName, 0, KIconLoader::Desktop, 16));
80 name->setText(fileName);
81 name->setStyleSheet(QString("color: %1;").
82 arg(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor).name()));
83 progressBar->setValue(transfer->percent().value());
84
85 int row = m_dialogLayout->rowCount();
86
87 m_dialogLayout->addWidget(icon, row, 0);
88 m_dialogLayout->addWidget(name, row, 1);
89 m_dialogLayout->addWidget(progressBar, row, 2);
90
91 m_transfers.insert(transfer, progressBar);
92 }
93 }
94
transfersRemoved(const QList<OrgKdeKgetTransferInterface * > & transfers)95 void KGetPanelBar::Private::transfersRemoved(const QList<OrgKdeKgetTransferInterface*> &transfers)
96 {
97 foreach (OrgKdeKgetTransferInterface* transfer, transfers) {
98 int index = m_dialogLayout->indexOf(m_transfers[transfer]);
99 int row, column, rowSpan, columnSpan;
100 m_dialogLayout->getItemPosition(index, &row, &column, &rowSpan, &columnSpan);
101 qDebug() << row;
102
103 QList<QLayoutItem*> items;
104 for (int i = 0; i != 3; i++) {
105 QLayoutItem * item = m_dialogLayout->itemAtPosition(row, i);
106 items << item;
107 m_dialogLayout->removeItem(item);
108 delete item->widget();
109 delete item;
110 }
111
112 m_transfers.remove(transfer);
113 }
114 }
115
update()116 void KGetPanelBar::Private::update()
117 {
118 int totalSize = 0;
119 int completedSize = 0;
120 QMap<OrgKdeKgetTransferInterface*, QProgressBar*>::const_iterator it;
121 QMap<OrgKdeKgetTransferInterface*, QProgressBar*>::const_iterator itEnd = m_transfers.constEnd();
122 for (it = m_transfers.constBegin(); it != itEnd; ++it) {
123 OrgKdeKgetTransferInterface *transfer = it.key();
124 (*it)->setValue(transfer->percent().value());
125 totalSize += transfer->totalSize().value();
126 completedSize += transfer->downloadedSize().value();
127 }
128 if (totalSize > 0) {
129 emit progressBarChanged((int) ((completedSize * 100) / totalSize));
130 }
131 else {
132 emit progressBarChanged(0);
133 }
134 }
135
clear()136 void KGetPanelBar::Private::clear()
137 {
138 // TODO : Find another way to remove all layout widgets except the title one
139 for (int row = 1; row < m_dialogLayout->rowCount(); ++row) {
140 delete m_dialogLayout->itemAtPosition(row, 0);
141 delete m_dialogLayout->itemAtPosition(row, 1);
142 delete m_dialogLayout->itemAtPosition(row, 2);
143
144 m_dialogLayout->removeItem(m_dialogLayout->itemAtPosition(row, 0));
145 m_dialogLayout->removeItem(m_dialogLayout->itemAtPosition(row, 1));
146 m_dialogLayout->removeItem(m_dialogLayout->itemAtPosition(row, 2));
147 }
148 m_transfers.clear();
149 }
150
151
KGetPanelBar(QObject * parent,const QVariantList & args)152 KGetPanelBar::KGetPanelBar(QObject *parent, const QVariantList &args)
153 : KGetApplet(parent, args),
154 m_icon(0),
155 d(new KGetPanelBar::Private())
156 {
157 d->setFocusPolicy(Qt::NoFocus);
158 connect(this, SIGNAL(transfersAdded(QList<OrgKdeKgetTransferInterface*>)), d, SLOT(transfersAdded(QList<OrgKdeKgetTransferInterface*>)));
159 connect(this, SIGNAL(transfersRemoved(QList<OrgKdeKgetTransferInterface*>)), d, SLOT(transfersRemoved(QList<OrgKdeKgetTransferInterface*>)));
160 setBackgroundHints(Applet::NoBackground);
161 }
162
~KGetPanelBar()163 KGetPanelBar::~KGetPanelBar()
164 {
165 delete d;
166 delete m_bar;
167 }
168
init()169 void KGetPanelBar::init()
170 {
171 //KF5 port: remove this line and define TRANSLATION_DOMAIN in CMakeLists.txt instead
172 //KLocale::global()->insertCatalog("plasma_applet_kget");
173
174 m_icon = new Plasma::IconWidget(QIcon::fromTheme("go-down"), QString(), this);
175
176 m_layout = new QGraphicsLinearLayout(Qt::Horizontal, this);
177 m_layout->addItem(m_icon);
178
179 QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(this);
180
181 m_bar = new QProgressBar();
182
183 connect(d, SIGNAL(progressBarChanged(int)), m_bar, SLOT(setValue(int)));
184
185 proxy->setWidget(m_bar);
186
187 m_bar->setValue(0);
188 m_bar->setStyleSheet("background-color: transparent");
189
190 m_layout->addItem(proxy);
191
192 setLayout(m_layout);
193
194 connect(m_icon, SIGNAL(clicked()), SLOT(showDialog()));
195
196 KGetApplet::init();
197 }
198
paintInterface(QPainter * p,const QStyleOptionGraphicsItem * option,const QRect & contentsRect)199 void KGetPanelBar::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
200 {
201 Q_UNUSED(p)
202 Q_UNUSED(option)
203 Q_UNUSED(contentsRect)
204 }
205
dataUpdated(const QString & source,const Plasma::DataEngine::Data & data)206 void KGetPanelBar::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
207 {
208 Q_UNUSED(source)
209
210 if(data["error"].toBool()) {
211 qDebug() << "Error : " << data["errorMessage"].toString();
212 d->clear();
213 }
214 else if(!data["error"].toBool()) {
215 setTransfers(data["transfers"].toMap());
216 d->update();
217 }
218 }
219
showDialog()220 void KGetPanelBar::showDialog()
221 {
222 if (d->isVisible()) {
223 d->hide();
224 }
225 else {
226 d->show();
227 KWindowSystem::setState(d->winId(), NET::SkipTaskbar);
228 d->move(popupPosition(d->sizeHint()));
229 }
230 }
231
232
233
234