1
/***************************************************************************
2
* Copyright (C) 2008 by Joris Guisson and Ivan Vasic *
3
* joris.guisson@gmail.com *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20
***************************************************************************/
24
#include <QGraphicsLinearLayout>
25
#include <QGraphicsGridLayout>
26
#include <QGraphicsProxyWidget>
28
#include <KIconLoader>
29
#include <KConfigDialog>
32
#include <KWindowSystem>
33
#if (PLASMA_VERSION_MAJOR < 3)
34
#include <Plasma/Icon>
36
#include <Plasma/IconWidget>
38
#include <Plasma/Meter>
39
#include <Plasma/Label>
40
#include <taskmanager/taskmanager.h>
41
#include <taskmanager/task.h>
42
#include <util/functions.h>
53
Applet::Applet(QObject *parent, const QVariantList &args) : Plasma::Applet(parent, args),icon(0)
55
KLocale::setMainCatalog("ktorrent");
56
setAspectRatioMode(Plasma::ConstrainedSquare);
57
int iconSize = IconSize(KIconLoader::Desktop);
58
resize(iconSize * 6, iconSize * 3);
61
connected_to_app = false;
66
QFile f(args[0].toString());
67
if (f.open(QIODevice::ReadOnly))
82
engine = dataEngine("ktorrent");
84
connect(engine,SIGNAL(sourceAdded(const QString &)),this,SLOT(sourceAdded(const QString&)));
85
connect(engine,SIGNAL(sourceRemoved(const QString &)),this,SLOT(sourceRemoved(const QString&)));
87
setHasConfigurationInterface(true);
89
root_layout = new QGraphicsLinearLayout(this);
90
root_layout->setContentsMargins(0, 0, 0, 0);
91
root_layout->setSpacing(0);
92
root_layout->setOrientation(Qt::Vertical);
94
QGraphicsLinearLayout* line = new QGraphicsLinearLayout(0);
96
#if (PLASMA_VERSION_MAJOR < 3)
97
icon = new Plasma::Icon(KIcon("ktorrent"),QString(),this);
99
icon = new Plasma::IconWidget(KIcon("ktorrent"),QString(),this);
101
int icon_size = IconSize(KIconLoader::Desktop);
102
icon->setMaximumSize(icon_size,icon_size);
103
icon->setMinimumSize(icon_size,icon_size);
104
icon->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
105
connect(icon, SIGNAL(clicked()), this, SLOT(iconClicked()));
107
title = new Plasma::Label(this);
108
title->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
110
line->addItem(title);
111
root_layout->addItem(line);
113
chunk_bar = new ChunkBar(this);
114
root_layout->addItem(chunk_bar);
116
misc = new Plasma::Label(this);
117
root_layout->addItem(misc);
121
current_source = config().readEntry("current_source",QString());
122
if (current_source.isNull())
124
current_source = selectTorrent();
128
QStringList sources = engine->sources();
130
foreach (const QString & s,sources)
132
QString name = engine->query(s).value("name").toString();
133
if (name == current_source) {
141
current_source = selectTorrent();
144
if (!current_source.isNull())
146
connected_to_app = true;
147
engine->connectSource(current_source,this,1000);
151
connected_to_app = engine->query("core").value("connected").toBool();
152
if (!connected_to_app)
153
title->setText(i18n("KTorrent is not running !"));
155
title->setText(i18n("No torrents loaded !"));
158
engine->connectSource("core",this);
161
QString Applet::selectTorrent()
163
QStringList sources = engine->sources();
164
foreach (const QString & s,sources)
171
void Applet::saveState(KConfigGroup & config) const
176
void Applet::constraintsEvent(Plasma::Constraints constraints)
178
if (constraints & Plasma::FormFactorConstraint)
180
if (formFactor() == Plasma::Vertical)
184
else if (formFactor() == Plasma::Horizontal)
190
if (constraints & (Plasma::SizeConstraint | Plasma::FormFactorConstraint))
195
void Applet::createConfigurationInterface(KConfigDialog *parent)
197
QWidget *widget = new QWidget();
199
parent->setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
200
parent->addPage(widget, parent->windowTitle(), "ktorrent");
201
connect(parent, SIGNAL(applyClicked()), this, SLOT(configUpdated()));
202
connect(parent, SIGNAL(okClicked()), this, SLOT(configUpdated()));
203
updateTorrentCombo();
206
void Applet::updateTorrentCombo()
208
QStringList sources = engine->sources();
209
ui.torrent_to_display->clear();
210
ui.torrent_to_display->setEnabled(sources.count() > 0);
211
if (sources.count() == 0)
215
foreach (const QString & s,sources)
218
names << engine->query(s).value("name").toString();
220
ui.torrent_to_display->addItems(names);
222
if (current_source.isNull())
224
current_source = selectTorrent();
225
if (!current_source.isNull())
226
engine->connectSource(current_source,this,1000);
232
void Applet::configUpdated()
234
QString name = ui.torrent_to_display->currentText();
235
if (!current_source.isNull())
237
engine->disconnectSource(current_source,this);
238
current_source = QString();
242
QStringList sources = engine->sources();
243
foreach (const QString & s,sources)
245
if (s != "core" && engine->query(s).value("name").toString() == name)
248
engine->connectSource(current_source,this,1000);
249
config().writeEntry("current_source",current_source);
256
void Applet::dataUpdated(const QString &name,const Plasma::DataEngine::Data &data)
260
if (!connected_to_app && data.value("connected").toBool())
262
connected_to_app = true;
263
current_source = config().readEntry("current_source",QString());
264
if (current_source.isEmpty())
265
current_source = selectTorrent();
267
if (!current_source.isEmpty())
269
engine->connectSource(current_source,this,1000);
273
title->setText(i18n("No torrents loaded !"));
277
else if (connected_to_app && !data.value("connected").toBool())
279
connected_to_app = false;
280
current_source = QString();
281
title->setText(i18n("KTorrent is not running !"));
285
else if (name == current_source)
291
void Applet::updateCurrent(const Plasma::DataEngine::Data &data)
293
double ds = data.value("download_rate").toDouble();
294
double us = data.value("upload_rate").toDouble();
295
int uploaded = data.value("bytes_uploaded").toInt();
296
int downloaded = data.value("bytes_downloaded").toInt();
297
int size = data.value("total_bytes_to_download").toInt();
298
int st = data.value("seeders_total").toInt();
299
int sc = data.value("seeders_connected_to").toInt();
300
int ct = data.value("leechers_total").toInt();
301
int cc = data.value("leechers_connected_to").toInt();
302
KLocale* loc = KGlobal::locale();
303
float share_ratio = (downloaded == 0) ? 0 : (float)uploaded/downloaded;
307
<tr><td>Download Speed:</td><td>%5 </td><td>Seeders: </td><td>%1 (%2)</td></tr>\
308
<tr><td>Upload Speed:</td><td>%6 </td><td>Leechers: </td><td>%3 (%4)</td></tr>\
309
<tr><td>Downloaded:</td><td>%7 / %8 </td><td>Uploaded: </td><td>%9</td></tr>\
311
sc,st,cc,ct,BytesPerSecToString(ds),BytesPerSecToString(us),
312
BytesToString(downloaded),BytesToString(size),BytesToString(uploaded)));
316
QString t = i18n("<b>%1</b><br/>%2 (Share Ratio: <font color=\"%4\">%3</font>)",
317
data.value("name").toString(),
318
data.value("status").toString(),
319
loc->formatNumber(share_ratio,2),
320
share_ratio <= 0.8 ? "#ff0000" : "#1c9a1c");
324
chunk_bar->updateBitSets(
325
data.value("total_chunks").toInt(),
326
data.value("downloaded_chunks").toByteArray(),
327
data.value("excluded_chunks").toByteArray());
330
void Applet::sourceAdded(const QString & s)
333
if (current_source.isNull())
335
current_source = selectTorrent();
336
if (!current_source.isNull())
337
engine->connectSource(current_source,this,1000);
343
void Applet::sourceRemoved(const QString & s)
345
if (current_source == s)
347
current_source = selectTorrent();
348
if (!current_source.isNull())
349
engine->connectSource(current_source,this,1000);
355
void Applet::iconClicked()
357
TaskManager::TaskDict tasks = TaskManager::TaskManager::self()->tasks();
358
for (TaskManager::TaskDict::iterator i = tasks.begin();i != tasks.end();i ++)
360
if (i.value()->className() == "ktorrent")
362
KWindowSystem::activateWindow(i.key());
367
// can't find the window, try launching it
369
KRun::run("ktorrent", empty, 0);
372
void Applet::clearData()
377
<tr><td>Download Speed:</td><td>%5 </td><td>Seeders: </td><td>%1 (%2)</td></tr>\
378
<tr><td>Upload Speed:</td><td>%6 </td><td>Leechers: </td><td>%3 (%4)</td></tr>\
379
<tr><td>Downloaded:</td><td>%7 / %8 </td><td>Uploaded: </td><td>%9</td></tr>\
381
0,0,0,0,BytesPerSecToString(0),BytesPerSecToString(0),
382
BytesToString(0),BytesToString(0),BytesToString(0)));
386
K_EXPORT_PLASMA_APPLET(ktorrent, ktplasma::Applet)