2 // This file is part of the aMule Project.
4 // Copyright (c) 2010-2011 Werner Mahr (Vollstrecker) <amule@vollstreckernet.de>
6 // Any parts of this program contributed by third-party developers are copyrighted
7 // by their respective authors.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "plasma-applet-plasmamule.h"
24 #include "plasmamule-dbus.h"
26 #include <kaboutapplicationdialog.h>
27 #include <kaboutdata.h>
30 #include <kplugininfo.h>
34 #include <plasma/svg.h>
35 #include <plasma/theme.h>
36 #include <plasma/version.h>
38 #include <QApplication>
40 #include <QFontMetrics>
44 #include <QStringList>
46 K_EXPORT_PLASMA_APPLET(plasma
-applet
-plasmamule
, PlasmaMuleApplet
)
48 PlasmaMuleApplet::PlasmaMuleApplet(QObject
*parent
, const QVariantList
&args
)
49 : Plasma::Applet(parent
, args
),
52 QString path
= __IMG_PATH__
;
53 path
.append( "application-x-emulecollection.svg");
54 m_svg
.setImagePath(path
);
55 setBackgroundHints(TranslucentBackground
);
56 setMinimumSize(200, 200);
57 setMaximumSize(300, 300);
59 setHasConfigurationInterface(FALSE
);
62 PlasmaMuleApplet::~PlasmaMuleApplet()
66 void PlasmaMuleApplet::init()
68 m_debugChannel
= KDebug::registerArea ("plasmamule-applet",
78 void PlasmaMuleApplet::paintInterface(QPainter
*painter
,
79 const QStyleOptionGraphicsItem
*option
, const QRect
&contentsRect
)
81 painter
->setRenderHint(QPainter::SmoothPixmapTransform
);
82 painter
->setRenderHint(QPainter::Antialiasing
);
84 pixmap
= m_svg
.pixmap();
85 QPixmap
temp(pixmap
.size());
86 temp
.fill(Qt::transparent
);
88 p
.setCompositionMode(QPainter::CompositionMode_Source
);
89 p
.drawPixmap(0, 0, pixmap
);
90 p
.setCompositionMode(QPainter::CompositionMode_DestinationIn
);
91 p
.fillRect(temp
.rect(), QColor(0, 0, 0, 200));
94 painter
->drawPixmap(contentsRect
, pixmap
);
96 painter
->setPen(Qt::red
);
97 QFont font
= painter
->font();
99 painter
->setFont(font
);
102 painter
->drawText(contentsRect
, Qt::AlignCenter
,
103 "aMule not configured or installed");
105 else if (!m_os_active
)
107 painter
->drawText(contentsRect
, Qt::AlignCenter
,
108 "Online Signature disabled");
109 } else if (m_uptime
== 0)
111 painter
->drawText(contentsRect
, Qt::AlignCenter
,
112 "aMule is not running\n");
115 painter
->setPen(Qt::black
);
116 int time
, days
, hours
, minutes
;
118 days
= m_uptime
/86400;
119 time
= m_uptime
%86400;
125 runtime
= QString("%1:%2:%3").arg(hours
, 2, 10, QLatin1Char('0')).arg(minutes
, 2, 10, QLatin1Char('0')).arg(time
, 2, 10, QLatin1Char('0'));
131 runtime
.prepend(QString("%1 day ").arg(days
));
133 runtime
.prepend(QString("%1 days ").arg(days
));
136 message
= QString("%1 has been running for %2 \n\n").arg(m_version
).arg(runtime
);
137 if (m_ed2k_state
== 0 && m_kad_status
== 0)
139 message
.append(QString("%1 is not connected ").arg(m_nickname
));
140 message
.append("but running\n\n");
142 else if (m_ed2k_state
== 0 && m_kad_status
!= 0)
144 message
.append(QString("%1 is connected to ").arg(m_nickname
));
145 if (m_kad_status
== 1)
147 message
.append("Kad: firewalled \n\n");
149 message
.append("Kad: ok \n\n");
152 message
.append(QString("%1 is connected to %2 [%3:%4] with ").arg(m_nickname
).arg(m_ed2k_server_name
).arg(m_ed2k_server_ip
).arg(m_ed2k_server_port
));
153 if (m_kad_status
== 1)
155 if (m_ed2k_id_high_low
== "H")
157 message
.append("HighID | Kad: firewalled \n\n");
159 message
.append("LowID | Kad: firewalled \n\n");
161 } else if (m_kad_status
== 2)
163 if (m_ed2k_id_high_low
== "H")
165 message
.append("HighID | Kad: ok \n\n");
167 message
.append("LowID | Kad: ok \n\n");
170 if (m_ed2k_id_high_low
== "H")
172 message
.append("HighID | Kad: off \n\n");
174 message
.append("LowID | Kad: off \n\n");
178 message
.append(QString("Total Download: %1, Upload: %2\n\n").arg(calcSize(m_total_bytes_downloaded
)).arg(calcSize(m_total_bytes_uploaded
)));
179 message
.append(QString("Session Download: %1, Upload: %2\n\n").arg(calcSize(m_session_bytes_downloaded
)).arg(calcSize(m_session_bytes_uploaded
)));
180 message
.append(QString("Download: %L1 kB/s, Upload: %L2 kB/s\n\n").arg(m_down_speed
, 0 , 'f', 1).arg(m_up_speed
,0 ,'f', 1));
182 if (m_shared_files_count
== 1)
186 files_unit
= "files";
188 message
.append(QString("Sharing: %1 %2, Clients on queue: %3").arg(m_shared_files_count
).arg(files_unit
).arg(m_clients_in_up_queue
));
189 painter
->drawText(contentsRect
, Qt::TextDontClip
| Qt::AlignTop
| Qt::AlignLeft
| Qt::TextWordWrap
, message
);
191 if (painter
->boundingRect(contentsRect
, Qt::TextDontClip
| Qt::AlignTop
| Qt::AlignLeft
| Qt::TextWordWrap
,
192 message
).height() > contentsRect
.height())
194 kDebug(m_debugChannel
) << "Resizing";
195 resize(painter
->boundingRect(contentsRect
, message
).height()+(contentsRect
.topLeft().y()*2),
196 painter
->boundingRect(contentsRect
, message
).height()+(contentsRect
.topLeft().y()*2));
202 QString
PlasmaMuleApplet::calcSize (qlonglong in_size
)
207 units
<< "Bytes" << "kBs" << "MBs" << "GBs" << "TBs" << "PBs" << "BBs" << "ZBs" << "YBs";
209 while (in_size
>1023)
215 size
= (in_size
* 1024) / 1024;
216 return QString("%L1 %2").arg(size
, 0, 'f', 2).arg(units
.at(unit
));
219 void PlasmaMuleApplet::connectToEngine()
221 m_aMuleEngine
= dataEngine("plasmamule");
222 m_aMuleEngine
->connectAllSources(this, 0);
223 connect(m_aMuleEngine
, SIGNAL(sourceAdded(const QString
&)), this, SLOT(onSourceAdded(const QString
&)));
224 connect(m_aMuleEngine
, SIGNAL(sourceRemoved(const QString
&)), this, SLOT(onSourceRemoved(const QString
&)));
227 void PlasmaMuleApplet::onSourceAdded(const QString
& source
)
229 kDebug(m_debugChannel
) << "New Source: " << source
<< " added";
230 m_aMuleEngine
->connectSource(source
, this, 0);
233 void PlasmaMuleApplet::onSourceRemoved(const QString
& source
)
235 kDebug(m_debugChannel
) << "Source: " << source
<< " removed";
239 void PlasmaMuleApplet::dataUpdated(const QString
& source
, const Plasma::DataEngine::Data
&data
)
246 bool needs_update
= FALSE
;
247 kDebug(m_debugChannel
) << "Updating data" << data
;
248 if (data
["cat_dirs"].toStringList() != m_catDirs
&& data
.contains("cat_dirs"))
250 m_catDirs
= data
["cat_dirs"].toStringList();
252 if (data
["cat_names"].toStringList() != m_catNames
&& data
.contains("cat_names"))
254 m_catNames
= data
["cat_names"].toStringList();
256 if (data
["os_active"].toBool() != m_os_active
&& data
.contains("os_active"))
258 m_os_active
= data
["os_active"].toBool();
261 if (data
["config_found"].toBool() != m_config_found
&& data
.contains("config_found"))
263 m_config_found
= data
["config_found"].toBool();
266 if (data
["ed2k_state"].toInt() != m_ed2k_state
&& data
.contains("ed2k_state"))
268 m_ed2k_state
= data
["ed2k_state"].toInt();
271 if (data
["ed2k_server_name"] != m_ed2k_server_name
&& data
.contains("ed2k_server_name"))
273 m_ed2k_server_name
= data
["ed2k_server_name"].toString();
276 if (data
["ed2k_server_ip"] != m_ed2k_server_ip
&& data
.contains("ed2k_server_ip"))
278 m_ed2k_server_ip
= data
["ed2k_server_ip"].toString();
281 if (data
["ed2k_server_port"].toInt() != m_ed2k_server_port
&& data
.contains("ed2k_server_port"))
283 m_ed2k_server_port
= data
["ed2k_server_port"].toInt();
286 if (data
["ed2k_id_high_low"] != m_ed2k_id_high_low
&& data
.contains("ed2k_id_high_low"))
288 m_ed2k_id_high_low
= data
["ed2k_id_high_low"].toString();
291 if (data
["kad_status"].toInt() != m_kad_status
&& data
.contains("kad_status"))
293 m_kad_status
= data
["kad_status"].toInt();
296 if (data
["down_speed"].toInt() != m_down_speed
&& data
.contains("down_speed"))
298 m_down_speed
= data
["down_speed"].toDouble();
301 if (data
["up_speed"].toDouble() != m_up_speed
&& data
.contains("up_speed"))
303 m_up_speed
= data
["up_speed"].toDouble();
306 if (data
["clients_in_up_queue"].toInt() != m_clients_in_up_queue
&& data
.contains("clients_in_up_queue"))
308 m_clients_in_up_queue
= data
["clients_in_up_queue"].toInt();
311 if (data
["shared_files_count"].toInt() != m_shared_files_count
&& data
.contains("shared_files_count"))
313 m_shared_files_count
= data
["shared_files_count"].toInt();
316 if (data
["nickname"] != m_nickname
&& data
.contains("nickname"))
318 m_nickname
= data
["nickname"].toString();
321 if (data
["total_bytes_downloaded"].toLongLong() != m_total_bytes_downloaded
&& data
.contains("total_bytes_downloaded"))
323 m_total_bytes_downloaded
= data
["total_bytes_downloaded"].toLongLong();
326 if (data
["total_bytes_uploaded"].toLongLong() != m_total_bytes_uploaded
&& data
.contains("total_bytes_uploaded"))
328 m_total_bytes_uploaded
= data
["total_bytes_uploaded"].toLongLong();
331 if (data
["version"] != m_version
&& data
.contains("version"))
333 m_version
= data
["version"].toString();
336 if (data
["session_bytes_downloaded"].toLongLong() != m_session_bytes_downloaded
&& data
.contains("session_bytes_downloaded"))
338 m_session_bytes_downloaded
= data
["session_bytes_downloaded"].toLongLong();
341 if (data
["session_bytes_uploaded"].toLongLong() != m_session_bytes_uploaded
&& data
.contains("session_bytes_uploaded"))
343 m_session_bytes_uploaded
= data
["session_bytes_uploaded"].toLongLong();
346 if (data
["uptime"].toInt() != m_uptime
&& data
.contains("uptime"))
348 m_uptime
= data
["uptime"].toInt();
354 kDebug(m_debugChannel
) << "Updating view";
359 void PlasmaMuleApplet::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
)
361 kDebug(m_debugChannel
) << "Dragged Data detected " << event
;
362 if (event
->mimeData()->hasUrls())
364 event
->acceptProposedAction();
368 void PlasmaMuleApplet::dropEvent(QGraphicsSceneDragDropEvent
* event
)
370 QStringList::const_iterator constIterator
;
372 event
->acceptProposedAction();
374 QMenu
*menu
= new QMenu
;
376 if (m_catNames
.count() == 1)
378 sendLinkToEngine (event
->mimeData()->text(), 0, this, m_debugChannel
);
380 for (constIterator
= m_catNames
.constBegin(); constIterator
!= m_catNames
.constEnd(); ++constIterator
)
382 menu
->addAction(*constIterator
);
383 if (constIterator
!= m_catNames
.constEnd())
385 menu
->addSeparator();
389 QAction
*cat_selection
= menu
->exec(QCursor::pos());
392 sendLinkToEngine (event
->mimeData()->text(), m_catNames
.indexOf(cat_selection
->text()), this, m_debugChannel
);
399 void PlasmaMuleApplet::contextMenuEvent(QGraphicsSceneContextMenuEvent
* event
)
401 QStringList::const_iterator constIterator
;
404 if (m_catDirs
.count() == 1)
406 menu
.addAction (KIcon("folder"), QString("Default"));
408 QMenu
*open_sub_menu
= menu
.addMenu("Open Incoming");
409 for (constIterator
= m_catNames
.constBegin(); constIterator
!= m_catNames
.constEnd(); ++constIterator
)
411 open_sub_menu
->addAction(KIcon("folder"), "Folder for " + *constIterator
);
412 if (constIterator
!= m_catNames
.constEnd())
414 open_sub_menu
->addSeparator();
420 QClipboard
* clipboard
= QApplication::clipboard();
422 if (!clipboard
->text(QClipboard::Clipboard
).isEmpty() || !clipboard
->text(QClipboard::Selection
).isEmpty())
424 if (m_catDirs
.count() == 1)
426 if (!clipboard
->text(QClipboard::Clipboard
).isEmpty())
428 menu
.addAction(KIcon("arrow-down-double"), "Download Link from Clipboard");
430 if (!clipboard
->text(QClipboard::Selection
).isEmpty())
436 if (!clipboard
->text(QClipboard::Selection
).isEmpty())
438 menu
.addAction(KIcon("arrow-down-double"), "Download Link from Selection");
441 QMenu
*download_sub_menu
= menu
.addMenu("Download Link");
443 for (constIterator
= m_catNames
.constBegin(); constIterator
!= m_catNames
.constEnd(); ++constIterator
)
445 if (!clipboard
->text(QClipboard::Clipboard
).isEmpty())
447 download_sub_menu
->addAction(KIcon("arrow-down-double"), "Clipboard->" + *constIterator
);
449 if (!clipboard
->text(QClipboard::Selection
).isEmpty())
451 download_sub_menu
->addSeparator();
455 if (!clipboard
->text(QClipboard::Selection
).isEmpty())
457 download_sub_menu
->addAction(KIcon("arrow-down-double"), "Selection->" + *constIterator
);
460 if (constIterator
!= m_catNames
.constEnd())
462 download_sub_menu
->addSeparator();
469 menu
.addAction(KIcon("documentinfo"), "About");
471 QAction
* selectedItem
= menu
.exec(QCursor::pos());
474 if (selectedItem
->text() == "About")
476 KPluginInfo
* service
= new KPluginInfo (KService::serviceByDesktopName ("plasma-applet-plasmamule"));
477 KAboutData
* aboutData
= new KAboutData (service
->name().toUtf8(),
478 service
->name().toUtf8(),
479 ki18n(service
->pluginName().toUtf8()),
480 service
->version().toUtf8(),
481 ki18n(service
->comment().toUtf8()),
482 KAboutData::License_GPL_V3
,
485 service
->website().toLatin1(),
486 service
->email().toLatin1());
487 aboutData
->addAuthor (ki18n(service
->author().toUtf8()),
489 service
->email().toLatin1(),
490 service
->website().toLatin1());
491 aboutData
->setTranslator(ki18nc ("NAME OF TRANSLATORS", "Your names"),
492 ki18nc("EMAIL OF TRANSLATORS", "Your emails"));
493 aboutData
->setProgramIconName (service
->icon());
495 KAboutApplicationDialog
* about
= new KAboutApplicationDialog(aboutData
, KAboutApplicationDialog::HideTranslators
);
497 } else if (selectedItem
->text().startsWith("Folder for"))
499 kDebug(m_debugChannel
) << "Opening Folder " << m_catDirs
.at(m_catNames
.indexOf(selectedItem
->text().remove("Folder for ")));
500 KUrl
url(m_catDirs
.at(m_catNames
.indexOf(selectedItem
->text().remove("Folder for "))) + "/");
501 (void) new KRun( url
, 0, true );
502 } else if (selectedItem
->text().startsWith("Download"))
504 if (selectedItem
->text().remove("Download Link from ") == "Clipboard")
506 sendLinkToEngine (clipboard
->text(QClipboard::Clipboard
), 0, this, m_debugChannel
);
508 sendLinkToEngine (clipboard
->text(QClipboard::Selection
), 0, this, m_debugChannel
);
510 } else if (selectedItem
->text().startsWith("Clipboard->"))
512 sendLinkToEngine (clipboard
->text(QClipboard::Clipboard
), m_catNames
.indexOf(selectedItem
->text().remove("Clipboard->")), this, m_debugChannel
);
513 } else if (selectedItem
->text().startsWith("Selection->"))
515 sendLinkToEngine (clipboard
->text(QClipboard::Selection
), m_catNames
.indexOf(selectedItem
->text().remove("Selection->")), this, m_debugChannel
);
520 #include "plasma-applet-plasmamule.moc"