~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to plasma/applet/applet.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-02-16 18:37:14 UTC
  • mfrom: (1.1.25 upstream) (0.4.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090216183714-52tf47jrnmk4xkmp
Tags: 3.2+dfsg.1-2ubuntu1
* Merge with Debian, remaining changes: (LP: #296433)
  - Use Kubuntu's kde4.mk
  - Build-depend on libboost-serialization1.35-dev since unversioned -dev is
    in universe
  - Change plasma-applet-ktorrent to plasma-widget-ktorrent since we're
    supposed to call them widgets for the users

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by Joris Guisson and Ivan Vasic                    *
 
3
 *   joris.guisson@gmail.com                                               *
 
4
 *   ivasic@gmail.com                                                      *
 
5
 *                                                                         *
 
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.                                   *
 
10
 *                                                                         *
 
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.                          *
 
15
 *                                                                         *
 
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
 ***************************************************************************/
 
21
#include "applet.h"
 
22
#include <math.h>
 
23
#include <QFile>
 
24
#include <QGraphicsLinearLayout>
 
25
#include <QGraphicsGridLayout>
 
26
#include <QGraphicsProxyWidget>
 
27
#include <KIcon>
 
28
#include <KIconLoader>
 
29
#include <KConfigDialog>
 
30
#include <KLocale>
 
31
#include <KRun>
 
32
#include <KWindowSystem>
 
33
#if (PLASMA_VERSION_MAJOR < 3)
 
34
#include <Plasma/Icon>
 
35
#else
 
36
#include <Plasma/IconWidget>
 
37
#endif
 
38
#include <Plasma/Meter>
 
39
#include <Plasma/Label>
 
40
#include <taskmanager/taskmanager.h>
 
41
#include <taskmanager/task.h>
 
42
#include <util/functions.h>
 
43
#include "chunkbar.h"
 
44
 
 
45
 
 
46
using namespace bt;
 
47
 
 
48
namespace ktplasma
 
49
{
 
50
 
 
51
        
 
52
 
 
53
        Applet::Applet(QObject *parent, const QVariantList &args) : Plasma::Applet(parent, args),icon(0)
 
54
        {
 
55
                KLocale::setMainCatalog("ktorrent");
 
56
                setAspectRatioMode(Plasma::ConstrainedSquare);
 
57
                int iconSize = IconSize(KIconLoader::Desktop);
 
58
                resize(iconSize * 6, iconSize * 3);
 
59
                engine = 0;
 
60
                root_layout = 0;
 
61
                connected_to_app = false;
 
62
 
 
63
                // drop data!
 
64
                if (!args.isEmpty()) 
 
65
                {
 
66
                        QFile f(args[0].toString());
 
67
                        if (f.open(QIODevice::ReadOnly)) 
 
68
                        {
 
69
                                QDataStream s(&f);
 
70
                                s >> current_source;
 
71
                        }
 
72
                }
 
73
        }
 
74
 
 
75
 
 
76
        Applet::~Applet()
 
77
        {
 
78
        }
 
79
        
 
80
        void Applet::init()
 
81
        {
 
82
                engine = dataEngine("ktorrent");
 
83
                
 
84
                connect(engine,SIGNAL(sourceAdded(const QString &)),this,SLOT(sourceAdded(const QString&)));
 
85
                connect(engine,SIGNAL(sourceRemoved(const QString &)),this,SLOT(sourceRemoved(const QString&)));
 
86
 
 
87
                setHasConfigurationInterface(true);
 
88
                
 
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);
 
93
                
 
94
                QGraphicsLinearLayout* line = new QGraphicsLinearLayout(0);
 
95
                
 
96
#if (PLASMA_VERSION_MAJOR < 3)
 
97
                icon = new Plasma::Icon(KIcon("ktorrent"),QString(),this);
 
98
#else
 
99
                icon = new Plasma::IconWidget(KIcon("ktorrent"),QString(),this);
 
100
#endif
 
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()));
 
106
                
 
107
                title = new Plasma::Label(this);
 
108
                title->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
 
109
                line->addItem(icon);
 
110
                line->addItem(title);
 
111
                root_layout->addItem(line);
 
112
                
 
113
                chunk_bar = new ChunkBar(this);
 
114
                root_layout->addItem(chunk_bar);
 
115
 
 
116
                misc = new Plasma::Label(this);
 
117
                root_layout->addItem(misc);
 
118
                
 
119
                clearData();
 
120
                
 
121
                current_source = config().readEntry("current_source",QString());
 
122
                if (current_source.isNull()) 
 
123
                {
 
124
                        current_source = selectTorrent();
 
125
                } 
 
126
                else 
 
127
                {
 
128
                        QStringList sources = engine->sources();
 
129
                        bool found = false;
 
130
                        foreach (const QString & s,sources)
 
131
                        {
 
132
                                QString name = engine->query(s).value("name").toString();
 
133
                                if (name == current_source) {
 
134
                                        current_source = s;
 
135
                                        found = true;
 
136
                                        break;
 
137
                                }
 
138
                        }
 
139
 
 
140
                        if (!found)
 
141
                            current_source = selectTorrent();
 
142
                }
 
143
                
 
144
                if (!current_source.isNull())
 
145
                {
 
146
                        connected_to_app = true;
 
147
                        engine->connectSource(current_source,this,1000);
 
148
                }
 
149
                else
 
150
                {
 
151
                        connected_to_app = engine->query("core").value("connected").toBool();
 
152
                        if (!connected_to_app)
 
153
                                title->setText(i18n("KTorrent is not running !"));
 
154
                        else
 
155
                                title->setText(i18n("No torrents loaded !"));
 
156
                }
 
157
 
 
158
                engine->connectSource("core",this);
 
159
        }
 
160
        
 
161
        QString Applet::selectTorrent()
 
162
        {
 
163
                QStringList sources = engine->sources();
 
164
                foreach (const QString & s,sources)
 
165
                        if (s != "core")
 
166
                                return s;
 
167
                
 
168
                return QString();
 
169
        }
 
170
        
 
171
        void Applet::saveState(KConfigGroup & config) const
 
172
        {
 
173
                Q_UNUSED(config);
 
174
        }
 
175
        
 
176
        void Applet::constraintsEvent(Plasma::Constraints constraints)
 
177
        {
 
178
                if (constraints & Plasma::FormFactorConstraint) 
 
179
                {
 
180
                        if (formFactor() == Plasma::Vertical) 
 
181
                        {
 
182
                                
 
183
                        } 
 
184
                        else if (formFactor() == Plasma::Horizontal) 
 
185
                        {
 
186
 
 
187
                        }
 
188
                }
 
189
 
 
190
                if (constraints & (Plasma::SizeConstraint | Plasma::FormFactorConstraint)) 
 
191
                {
 
192
                }
 
193
        }
 
194
        
 
195
        void Applet::createConfigurationInterface(KConfigDialog *parent)
 
196
        {
 
197
                QWidget *widget = new QWidget();
 
198
                ui.setupUi(widget);
 
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();
 
204
        }
 
205
        
 
206
        void Applet::updateTorrentCombo()
 
207
        {       
 
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)
 
212
                        return;
 
213
                
 
214
                QStringList names;
 
215
                foreach (const QString & s,sources)
 
216
                {
 
217
                        if (s != "core")
 
218
                                names << engine->query(s).value("name").toString();
 
219
                }
 
220
                ui.torrent_to_display->addItems(names);
 
221
                
 
222
                if (current_source.isNull())
 
223
                {
 
224
                        current_source = selectTorrent();
 
225
                        if (!current_source.isNull())
 
226
                                engine->connectSource(current_source,this,1000);
 
227
                        else
 
228
                                clearData();
 
229
                }
 
230
        }
 
231
        
 
232
        void Applet::configUpdated()
 
233
        {
 
234
                QString name = ui.torrent_to_display->currentText();
 
235
                if (!current_source.isNull())
 
236
                {
 
237
                        engine->disconnectSource(current_source,this);
 
238
                        current_source = QString();
 
239
                        clearData();
 
240
                }
 
241
                
 
242
                QStringList sources = engine->sources();
 
243
                foreach (const QString & s,sources)
 
244
                {
 
245
                        if (s != "core" && engine->query(s).value("name").toString() == name)
 
246
                        {
 
247
                                current_source = s;
 
248
                                engine->connectSource(current_source,this,1000);
 
249
                                config().writeEntry("current_source",current_source);
 
250
                                config().sync();
 
251
                                break;
 
252
                        }
 
253
                }
 
254
        }
 
255
 
 
256
        void Applet::dataUpdated(const QString &name,const Plasma::DataEngine::Data &data)
 
257
        {
 
258
                if (name == "core")
 
259
                {
 
260
                        if (!connected_to_app && data.value("connected").toBool())
 
261
                        {
 
262
                                connected_to_app = true;
 
263
                                current_source = config().readEntry("current_source",QString());
 
264
                                if (current_source.isEmpty())
 
265
                                        current_source = selectTorrent();
 
266
                                
 
267
                                if (!current_source.isEmpty())
 
268
                                {
 
269
                                        engine->connectSource(current_source,this,1000);
 
270
                                }
 
271
                                else
 
272
                                {
 
273
                                        title->setText(i18n("No torrents loaded !"));
 
274
                                        clearData();
 
275
                                }
 
276
                        }
 
277
                        else if (connected_to_app && !data.value("connected").toBool())
 
278
                        {
 
279
                                connected_to_app = false;
 
280
                                current_source = QString();
 
281
                                title->setText(i18n("KTorrent is not running !"));
 
282
                                clearData();
 
283
                        }
 
284
                }
 
285
                else if (name == current_source)
 
286
                {
 
287
                        updateCurrent(data);
 
288
                }
 
289
        }
 
290
        
 
291
        void Applet::updateCurrent(const Plasma::DataEngine::Data &data)
 
292
        {
 
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;
 
304
                misc->setText(
 
305
                        i18n(
 
306
                                 "<table>\
 
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>\
 
310
                                </table>",
 
311
                                sc,st,cc,ct,BytesPerSecToString(ds),BytesPerSecToString(us),
 
312
                                BytesToString(downloaded),BytesToString(size),BytesToString(uploaded)));
 
313
                
 
314
                
 
315
                
 
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");
 
321
                
 
322
                title->setText(t);
 
323
                
 
324
                chunk_bar->updateBitSets(
 
325
                        data.value("total_chunks").toInt(),
 
326
                        data.value("downloaded_chunks").toByteArray(),
 
327
                        data.value("excluded_chunks").toByteArray());
 
328
        }
 
329
        
 
330
        void Applet::sourceAdded(const QString & s)
 
331
        {
 
332
                Q_UNUSED(s);
 
333
                if (current_source.isNull())
 
334
                {
 
335
                        current_source = selectTorrent();
 
336
                        if (!current_source.isNull())
 
337
                                engine->connectSource(current_source,this,1000);
 
338
                        else
 
339
                                clearData();
 
340
                }
 
341
        }
 
342
        
 
343
        void Applet::sourceRemoved(const QString & s)
 
344
        {
 
345
                if (current_source == s)
 
346
                {
 
347
                        current_source = selectTorrent();
 
348
                        if (!current_source.isNull())
 
349
                                engine->connectSource(current_source,this,1000);
 
350
                        else
 
351
                                clearData();
 
352
                }
 
353
        }
 
354
 
 
355
        void Applet::iconClicked()
 
356
        {
 
357
                TaskManager::TaskDict tasks = TaskManager::TaskManager::self()->tasks();
 
358
                for (TaskManager::TaskDict::iterator i = tasks.begin();i != tasks.end();i ++)
 
359
                {
 
360
                        if (i.value()->className() == "ktorrent")
 
361
                        {
 
362
                                KWindowSystem::activateWindow(i.key());
 
363
                                return;
 
364
                        }
 
365
                }
 
366
 
 
367
                // can't find the window, try launching it
 
368
                KUrl::List empty;
 
369
                KRun::run("ktorrent", empty, 0);
 
370
        }
 
371
 
 
372
        void Applet::clearData()
 
373
        {               
 
374
                misc->setText(
 
375
                        i18n(
 
376
                                "<table>\
 
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>\
 
380
                                </table>",
 
381
                                0,0,0,0,BytesPerSecToString(0),BytesPerSecToString(0),
 
382
                                BytesToString(0),BytesToString(0),BytesToString(0)));
 
383
        }
 
384
}
 
385
 
 
386
K_EXPORT_PLASMA_APPLET(ktorrent, ktplasma::Applet)