• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • plasma
  • applet
  • common
kgetapplet.cpp
Go to the documentation of this file.
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) 2008 - 2009 by Lukas Appelhans <l.appelhans@gmx.de>
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 #include "kgetapplet.h"
21 #include "kget_interface.h"
22 #include "kgetappletutils.h"
23 #include "../../../core/transferhandler.h"
24 
25 #include <plasma/dataengine.h>
26 #include <plasma/theme.h>
27 #include <plasma/widgets/iconwidget.h>
28 #include <plasma/widgets/meter.h>
29 
30 #include <QGraphicsSceneDragDropEvent>
31 #include <QGraphicsLinearLayout>
32 #include <QDropEvent>
33 #include <QStyleOptionGraphicsItem>
34 #include <QProgressBar>
35 #include <QGraphicsProxyWidget>
36 #include <QPainter>
37 #include <QtDBus/QDBusConnectionInterface>
38 #include <KUrl>
39 #include <KLocale>
40 #include <KIcon>
41 
42 const int ProxyWidget::MARGIN = 20;
43 const int ProxyWidget::TOP_MARGIN = 55;
44 const int ProxyWidget::LEFT_MARGIN = 15;
45 const int ProxyWidget::SPACING = 4;
46 
47 const QString KGetApplet::KGET_DBUS_SERVICE = "org.kde.kget";
48 const QString KGetApplet::KGET_DBUS_PATH = "/KGet";
49 
50 ProxyWidget::ProxyWidget(QGraphicsWidget * parent)
51  : QGraphicsWidget(parent),
52  m_layout(0),
53  m_dataWidget(0)
54 {
55  m_layout = new QGraphicsLinearLayout(Qt::Vertical, this);
56  m_layout->setSpacing(SPACING);
57  //m_layout->setContentsMargins(MARGIN, TOP_MARGIN, MARGIN, MARGIN);
58 
59  themeChanged();
60 
61  connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(themeChanged()));
62 }
63 
64 ProxyWidget::~ProxyWidget()
65 {
66 }
67 
68 void ProxyWidget::paint(QPainter * p, const QStyleOptionGraphicsItem * option, QWidget * widget)
69 {
70  const QRect rect = option->rect;
71 
72  p->setRenderHint(QPainter::SmoothPixmapTransform);
73 
74  QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
75  font.setBold(true);
76  font.setPointSize(15);
77 
78  p->setFont(font);
79  p->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
80 
81  QRect iconRect(QPoint(rect.x() + SPACING + 10, rect.y() + SPACING + 10), QSize(m_textHeight, m_textHeight));
82 
83  KIcon("kget").paint(p, iconRect);
84  p->drawText(QRectF(rect.x() + SPACING * 2 + 10 + iconRect.width(), rect.y() + SPACING + 10,
85  m_textWidth, m_textHeight), i18n("KGet"));
86  p->drawLine(QPointF(rect.x() + SPACING + 10, rect.y() + SPACING * 2 + 10 + m_textHeight),
87  QPointF(rect.width() - SPACING - 10, rect.y() + SPACING * 2 + 10 + m_textHeight));
88 
89  QGraphicsWidget::paint(p, option, widget);
90 }
91 
92 void ProxyWidget::themeChanged()
93 {
94  QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
95  font.setBold(true);
96  font.setPointSize(15);
97 
98  QFontMetrics metrics(font);
99  m_textWidth = metrics.width(i18n("KGet"));
100  m_textHeight = metrics.height();
101 
102  m_layout->setContentsMargins(MARGIN, MARGIN + m_textHeight + SPACING + 10, MARGIN, MARGIN);
103 }
104 
105 void ProxyWidget::setDataWidget(QGraphicsWidget *widget)
106 {
107  if (m_layout->count())
108  m_layout->removeAt(0);
109  m_layout->addItem(widget);
110  m_dataWidget = widget;
111 }
112 
113 QGraphicsWidget *ProxyWidget::dataWidget()
114 {
115  return m_dataWidget;
116 }
117 
118 KGetApplet::KGetApplet(QObject *parent, const QVariantList &args)
119  : Plasma::PopupApplet(parent, args),
120  m_proxyWidget(0),
121  m_errorWidget(0),
122  m_dataWidget(0),
123  m_globalProgress(0),
124  m_icon(0),
125  m_engine(0),
126  m_totalSize(0),
127  m_downloadedSize(0)
128 {
129  setAspectRatioMode(Plasma::IgnoreAspectRatio);
130  setBackgroundHints(Applet::DefaultBackground);
131  setAcceptDrops(true);
132  m_proxyWidget = new ProxyWidget(this);
133 }
134 
135 KGetApplet::~KGetApplet()
136 {
137 }
138 
139 void KGetApplet::init()
140 {
141  KGlobal::locale()->insertCatalog("plasma_applet_kget");
142 
143  setPopupIcon("kget");
144  m_engine = dataEngine("kget");
145  if (m_engine) {
146  m_engine->connectSource("KGet", this);
147  } else {
148  kDebug(5001) << "KGet Engine could not be loaded";
149  }
150  m_globalProgress = new Plasma::Meter(this);
151  m_globalProgress->setMeterType(Plasma::Meter::BarMeterHorizontal);
152  m_globalProgress->setMinimumSize(QSize(0, 0));
153  setGraphicsWidget(m_proxyWidget);
154 }
155 
156 void KGetApplet::slotKgetStarted()
157 {
158  m_engine->query("KGet");
159 }
160 
161 void KGetApplet::dataUpdated(const QString &name, const Plasma::DataEngine::Data &data)
162 {
163  Q_UNUSED(name)
164  kDebug() << layout()->count();
165  if (data["error"].toBool()) {
166  if (!m_errorWidget) {
167  m_errorWidget = new ErrorWidget(data["errorMessage"].toString(), this);
168  connect(m_errorWidget, SIGNAL(kgetStarted()), this, SLOT(slotKgetStarted()));
169  }
170  if (m_proxyWidget->dataWidget() != m_errorWidget) {
171  m_proxyWidget->setDataWidget(m_errorWidget);
172  m_errorWidget->show();
173  m_dataWidget->hide();
174  }
175  } else if (!data["error"].toBool()) {
176  if (m_errorWidget && m_errorWidget->isVisible())
177  m_errorWidget->hide();
178  if (m_proxyWidget->dataWidget() != m_dataWidget) {
179  m_proxyWidget->setDataWidget(m_dataWidget);
180  m_dataWidget->show();
181  }
182  if (m_transfers.isEmpty()) {
183  transferAdded(data["transfers"].toMap());
184  } else {
185  if (data.contains("transferAdded")) {
186  transferAdded(data["transferAdded"].toMap());
187  }
188  if (data.contains("transferRemoved")) {
189  transferRemoved(data["transferRemoved"].toMap());
190  }
191  }
192  }
193 }
194 
195 void KGetApplet::transferAdded(const QVariantMap &transfer)
196 {
197  QList<OrgKdeKgetTransferInterface*> added;
198  QVariantMap::const_iterator it;
199  QVariantMap::const_iterator itEnd = transfer.constEnd();
200  for (it = transfer.constBegin(); it != itEnd; ++it) {
201  OrgKdeKgetTransferInterface *newTransfer = new OrgKdeKgetTransferInterface("org.kde.kget", it.value().toString(), QDBusConnection::sessionBus(), this);
202  connect(newTransfer, SIGNAL(transferChangedEvent(int)), this, SLOT(slotUpdateTransfer(int)));
203 
204  added.append(newTransfer);
205 
206  m_transfers[newTransfer].downloadedSize = newTransfer->downloadedSize();
207  m_transfers[newTransfer].size = newTransfer->totalSize();
208  m_downloadedSize += m_transfers[newTransfer].downloadedSize;
209  m_totalSize += m_transfers[newTransfer].size;
210  }
211 
212  if (!added.isEmpty()) {
213  emit transfersAdded(added);
214  emit update();
215  updateGlobalProgress();
216  }
217 }
218 
219 void KGetApplet::transferRemoved(const QVariantMap &transfer)
220 {
221  Q_UNUSED(transfer)
222 
223  QList<OrgKdeKgetTransferInterface*> removed;
224 
225  QHash<OrgKdeKgetTransferInterface*, Data>::iterator it;
226  QHash<OrgKdeKgetTransferInterface*, Data>::iterator itEnd = m_transfers.end();
227  for (it = m_transfers.begin(); it != itEnd; ) {
228  const KUrl url = KUrl(it.key()->source().value());
229  //if the protocol is empty that means, that the transer does not exist anymore
230  if (url.protocol().isEmpty()) {
231  removed.append(it.key());
232 
233  m_downloadedSize -= m_transfers[it.key()].downloadedSize;
234  m_totalSize -= m_transfers[it.key()].size;
235 
236  it = m_transfers.erase(it);
237  } else {
238  ++it;
239  }
240  }
241 
242  if (!removed.isEmpty()) {
243  emit transfersRemoved(removed);
244  emit update();
245  updateGlobalProgress();
246  }
247 }
248 
249 void KGetApplet::slotUpdateTransfer(int transferChange)
250 {
251  OrgKdeKgetTransferInterface *transfer = qobject_cast<OrgKdeKgetTransferInterface*>(QObject::sender());
252 
253  if (transfer && m_transfers.contains(transfer)) {
254  if (transferChange & Transfer::Tc_TotalSize) {
255  m_totalSize -= m_transfers[transfer].size;
256  m_downloadedSize -= m_transfers[transfer].downloadedSize;
257 
258  m_transfers[transfer].size = transfer->totalSize();
259  m_transfers[transfer].downloadedSize = transfer->downloadedSize();
260  m_totalSize += m_transfers[transfer].size;
261  m_downloadedSize += m_transfers[transfer].downloadedSize;
262 
263  updateGlobalProgress();
264  return;
265  }
266  if (transferChange & Transfer::Tc_DownloadedSize) {
267  m_downloadedSize -= m_transfers[transfer].downloadedSize;
268 
269  m_transfers[transfer].downloadedSize = transfer->downloadedSize();
270  m_downloadedSize += m_transfers[transfer].downloadedSize;
271 
272  updateGlobalProgress();
273  return;
274  }
275  }
276 }
277 
278 void KGetApplet::updateGlobalProgress()
279 {
280  if (m_globalProgress && m_totalSize) {
281  m_globalProgress->setValue((m_downloadedSize * 100) / m_totalSize);
282  }
283 }
284 
285 void KGetApplet::setDataWidget(QGraphicsWidget * widget)
286 {
287  m_dataWidget = widget;
288  if (m_proxyWidget->dataWidget() != m_errorWidget)
289  m_proxyWidget->setDataWidget(widget);
290 }
291 
292 void KGetApplet::constraintsEvent(Plasma::Constraints constraints)
293 {
294  if (constraints & Plasma::SizeConstraint) {
295  QGraphicsLayoutItem *widget = layout()->itemAt(0);
296  Plasma::IconWidget *icon = 0;
297  if (!m_icon && (icon = dynamic_cast<Plasma::IconWidget*>(widget))) {
298  m_icon = icon;
299  }
300  if (widget == m_proxyWidget && m_globalProgress->isVisible()) {
301  kDebug() << "remove progressbar";
302  m_globalProgress->hide();
303  dynamic_cast<QGraphicsLinearLayout*>(layout())->removeItem(m_globalProgress);
304  } else if (m_icon && m_icon->isVisible()) {
305  QGraphicsLinearLayout *lay = dynamic_cast<QGraphicsLinearLayout*>(layout());
306  kDebug() << "switch to progressbar";
307  m_globalProgress->show();
308  m_icon->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
309  m_icon->setPreferredSize(size().height(), size().height());
310  m_globalProgress->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
311  for (int i = 0; i != 2; i++) {
312  if (lay->count())
313  lay->removeAt(0);
314  }
315  lay->addItem(m_icon);
316  lay->addItem(m_globalProgress);
317  }
318  /*if (layout()->count() && dynamic_cast<Plasma::IconWidget*>(layout()->itemAt(0)) && !m_progressProxy.isVisible()) {
319  qobject_cast<QGraphicsLinearLayout*>(layout())->addItem(m_progressProxy);
320  m_progressProxy->show();
321  } else if (m_progressProxy.isVisible() && layout->count() == 1) {
322  kDebug();
323  layout()->removeAt(0);
324  m_progressProxy->hide();
325  }*/
326  }
327 }
328 
329 bool KGetApplet::sceneEventFilter(QGraphicsItem * watched, QEvent * event)
330 {
331  Q_UNUSED(watched)
332  switch (event->type())
333  {
334  case QEvent::GraphicsSceneDrop:
335  dropEvent(static_cast<QGraphicsSceneDragDropEvent*>(event));
336  break;
337  case QEvent::Drop:
338  dropEvent(static_cast<QDropEvent*>(event));
339  break;
340  default:
341  break;
342  }
343  return Plasma::Applet::sceneEventFilter(watched, event);
344  //This is not 100% kosher, I think we should just return false, but otherwise the plasma resize buttons etc don't show u
345 }
346 
347 void KGetApplet::dropEvent(QGraphicsSceneDragDropEvent * event)
348 {
349  kDebug(5001);
350 
351  QStringList urls;
352  if (event->mimeData()->hasUrls())
353  {
354  foreach (const KUrl &url, event->mimeData()->urls())
355  urls.append(url.url());
356  }
357  else
358  {
359  event->ignore();
360  return;
361  }
362 
363  if (QDBusConnection::sessionBus().interface()->isServiceRegistered(KGET_DBUS_SERVICE))
364  {
365  OrgKdeKgetMainInterface kget_interface(KGET_DBUS_SERVICE, KGET_DBUS_PATH,
366  QDBusConnection::sessionBus());
367 
368  kget_interface.showNewTransferDialog(urls);
369  }
370  else
371  {
372  QProcess::startDetached("kget", urls);
373  }
374  event->accept();
375 }
376 
377 void KGetApplet::dropEvent(QDropEvent * event)
378 {
379  kDebug(5001);
380 
381  QStringList urls;
382  if (event->mimeData()->hasUrls())
383  {
384  foreach (const KUrl &url, event->mimeData()->urls())
385  urls.append(url.url());
386  }
387  else
388  {
389  event->ignore();
390  return;
391  }
392 
393  if (QDBusConnection::sessionBus().interface()->isServiceRegistered(KGET_DBUS_SERVICE))
394  {
395  OrgKdeKgetMainInterface kget_interface(KGET_DBUS_SERVICE, KGET_DBUS_PATH,
396  QDBusConnection::sessionBus());
397 
398  kget_interface.showNewTransferDialog(urls);
399  event->accept();
400  }
401  else
402  {
403  QProcess::startDetached("kget", urls);
404  }
405  event->accept();
406 }
407 
408 #include "kgetapplet.moc"
KGetApplet::transfersAdded
void transfersAdded(const QList< OrgKdeKgetTransferInterface * > &transfers)
KGetApplet::m_dataWidget
QGraphicsWidget * m_dataWidget
Definition: kgetapplet.h:109
KGetApplet::m_icon
Plasma::IconWidget * m_icon
Definition: kgetapplet.h:111
ProxyWidget::paint
void paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: kgetapplet.cpp:68
ProxyWidget::ProxyWidget
ProxyWidget(QGraphicsWidget *parent)
Definition: kgetapplet.cpp:50
QWidget
kgetappletutils.h
QObject
ProxyWidget::dataWidget
QGraphicsWidget * dataWidget()
Definition: kgetapplet.cpp:113
KGetApplet::m_globalProgress
Plasma::Meter * m_globalProgress
Definition: kgetapplet.h:110
KGetApplet::dropEvent
virtual void dropEvent(QGraphicsSceneDragDropEvent *event)
Definition: kgetapplet.cpp:347
kgetapplet.h
KGetApplet::KGET_DBUS_PATH
static const QString KGET_DBUS_PATH
Definition: kgetapplet.h:117
ErrorWidget
Definition: kgetappletutils.h:50
ProxyWidget::~ProxyWidget
~ProxyWidget()
Definition: kgetapplet.cpp:64
KGetApplet::m_engine
Plasma::DataEngine * m_engine
Definition: kgetapplet.h:112
ProxyWidget
Definition: kgetapplet.h:41
KGetApplet::init
void init()
Definition: kgetapplet.cpp:139
KGetApplet::transfersRemoved
void transfersRemoved(const QList< OrgKdeKgetTransferInterface * > &transfers)
KGetApplet::m_transfers
QHash< OrgKdeKgetTransferInterface *, Data > m_transfers
Definition: kgetapplet.h:115
KGetApplet::KGET_DBUS_SERVICE
static const QString KGET_DBUS_SERVICE
Definition: kgetapplet.h:116
KGetApplet::constraintsEvent
virtual void constraintsEvent(Plasma::Constraints constraints)
Definition: kgetapplet.cpp:292
KGetApplet::update
void update()
KGetApplet::sceneEventFilter
virtual bool sceneEventFilter(QGraphicsItem *watched, QEvent *event)
Definition: kgetapplet.cpp:329
KGetApplet::KGetApplet
KGetApplet(QObject *parent, const QVariantList &args)
Definition: kgetapplet.cpp:118
Transfer::Tc_TotalSize
Definition: transfer.h:54
ProxyWidget::setDataWidget
void setDataWidget(QGraphicsWidget *widget)
Definition: kgetapplet.cpp:105
KGetApplet::dataUpdated
void dataUpdated(const QString &name, const Plasma::DataEngine::Data &data)
Definition: kgetapplet.cpp:161
KGetApplet::~KGetApplet
~KGetApplet()
Definition: kgetapplet.cpp:135
KGetApplet::setDataWidget
void setDataWidget(QGraphicsWidget *widget)
Definition: kgetapplet.cpp:285
Transfer::Tc_DownloadedSize
Definition: transfer.h:63
KGetApplet::m_proxyWidget
ProxyWidget * m_proxyWidget
Definition: kgetapplet.h:107
KGetApplet::m_errorWidget
ErrorWidget * m_errorWidget
Definition: kgetapplet.h:108
QGraphicsWidget
KGetApplet::m_totalSize
KIO::filesize_t m_totalSize
Definition: kgetapplet.h:113
KGetApplet::m_downloadedSize
KIO::filesize_t m_downloadedSize
Definition: kgetapplet.h:114
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal