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

« back to all changes in this revision

Viewing changes to libktcore/dbus/dbus.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) 2007 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 <QDBusConnection>
 
22
#include <kconfig.h>
 
23
#include <interfaces/torrentinterface.h>
 
24
#include <torrent/queuemanager.h>
 
25
#include <util/log.h>
 
26
#include <util/sha1hash.h>
 
27
#include <groups/groupmanager.h>
 
28
#include "dbus.h"
 
29
#include <interfaces/coreinterface.h>
 
30
#include <interfaces/guiinterface.h>
 
31
#include "dbustorrent.h"
 
32
#include "dbusgroup.h"
 
33
 
 
34
using namespace bt;
 
35
 
 
36
namespace kt
 
37
{
 
38
        QString DataDir();
 
39
        
 
40
        DBus::DBus(GUIInterface* gui,CoreInterface* core,QObject* parent) : QObject(parent),gui(gui),core(core)
 
41
        {
 
42
                torrent_map.setAutoDelete(true);
 
43
                group_map.setAutoDelete(true);
 
44
                
 
45
                QDBusConnection::sessionBus().registerObject("/core", this,
 
46
                                QDBusConnection::ExportScriptableSlots|QDBusConnection::ExportScriptableSignals);
 
47
 
 
48
                connect(core,SIGNAL(torrentAdded(bt::TorrentInterface*)),this,SLOT(torrentAdded(bt::TorrentInterface*)));
 
49
                connect(core,SIGNAL(torrentRemoved(bt::TorrentInterface*)),this,SLOT(torrentRemoved(bt::TorrentInterface*)));
 
50
                connect(core,SIGNAL(torrentStoppedByError(bt::TorrentInterface*, QString)),this,SLOT(torrentStoppedByError(bt::TorrentInterface*, QString)));
 
51
                connect(core,SIGNAL(finished(bt::TorrentInterface*)),this,SLOT(finished(bt::TorrentInterface*)));
 
52
                connect(core,SIGNAL(settingsChanged()),this,SIGNAL(settingsChanged()));
 
53
                
 
54
                // fill the map with torrents
 
55
                kt::QueueManager* qm = core->getQueueManager();
 
56
                for (QList<bt::TorrentInterface *>::iterator i = qm->begin();i != qm->end();i++)
 
57
                {
 
58
                        torrentAdded(*i);
 
59
                }
 
60
                
 
61
                connect(qm,SIGNAL(pauseStateChanged(bool)),this,SIGNAL(pauseStateChanged(bool)));
 
62
                
 
63
                kt::GroupManager* gman = core->getGroupManager();
 
64
                connect(gman,SIGNAL(customGroupAdded(Group*)),this,SLOT(groupAdded(Group*)));
 
65
                connect(gman,SIGNAL(customGroupRemoved(Group*)),this,SLOT(groupRemoved(Group*)));
 
66
                kt::GroupManager::const_iterator i = gman->begin();
 
67
                while (i != gman->end())
 
68
                {
 
69
                        groupAdded(i->second);
 
70
                        i++;
 
71
                }
 
72
        }
 
73
 
 
74
        DBus::~DBus()
 
75
        {
 
76
        }
 
77
 
 
78
        QStringList DBus::torrents()
 
79
        {
 
80
                QStringList tors;
 
81
                DBusTorrentItr i = torrent_map.begin();
 
82
                while (i != torrent_map.end())
 
83
                {
 
84
                        tors.append(i->first);
 
85
                        i++;
 
86
                }
 
87
 
 
88
                return tors;
 
89
        }
 
90
        
 
91
        void DBus::start(const QString & info_hash)
 
92
        {
 
93
                DBusTorrent* tc = torrent_map.find(info_hash);
 
94
                if (!tc)
 
95
                        return;
 
96
 
 
97
                core->getQueueManager()->start(tc->torrent(),true);
 
98
        }
 
99
 
 
100
        void DBus::stop(const QString & info_hash)
 
101
        {
 
102
                DBusTorrent* tc = torrent_map.find(info_hash);
 
103
                if (!tc)
 
104
                        return;
 
105
 
 
106
                core->getQueueManager()->stop(tc->torrent(),true);
 
107
        }
 
108
 
 
109
        void DBus::startAll()
 
110
        {
 
111
                core->startAll(3);
 
112
        }
 
113
 
 
114
        void DBus::stopAll()
 
115
        {
 
116
                core->stopAll(3);
 
117
        }
 
118
        
 
119
        void DBus::torrentAdded(bt::TorrentInterface* tc)
 
120
        {
 
121
                DBusTorrent* db = new DBusTorrent(tc,this);
 
122
                torrent_map.insert(db->infoHash(),db);
 
123
                torrentAdded(db->infoHash());
 
124
        }
 
125
 
 
126
        void DBus::torrentRemoved(bt::TorrentInterface* tc)
 
127
        {
 
128
                DBusTorrent* db = torrent_map.find(tc->getInfoHash().toString());
 
129
                if (db)
 
130
                {
 
131
                        QString ih = db->infoHash();
 
132
                        torrentRemoved(ih);
 
133
                        torrent_map.erase(ih);
 
134
                }
 
135
        }
 
136
        
 
137
        void DBus::finished(bt::TorrentInterface* tc)
 
138
        {
 
139
                DBusTorrent* db = torrent_map.find(tc->getInfoHash().toString());
 
140
                if (db)
 
141
                {
 
142
                        QString ih = db->infoHash();
 
143
                        finished(ih);
 
144
                }
 
145
        }
 
146
        
 
147
        void DBus::torrentStoppedByError(bt::TorrentInterface* tc, QString msg)
 
148
        {
 
149
                DBusTorrent* db = torrent_map.find(tc->getInfoHash().toString());
 
150
                if (db)
 
151
                {
 
152
                        QString ih = db->infoHash();
 
153
                        torrentStoppedByError(ih,msg);
 
154
                }
 
155
        }
 
156
 
 
157
        void DBus::load(const QString & url,const QString & group)
 
158
        {
 
159
                core->load(KUrl(url),group);
 
160
        }
 
161
                
 
162
        void DBus::loadSilently(const QString & url,const QString & group)
 
163
        {
 
164
                core->loadSilently(KUrl(url),group);
 
165
        }
 
166
        
 
167
        QStringList DBus::groups() const
 
168
        {
 
169
                QStringList ret;
 
170
                kt::GroupManager* gman = core->getGroupManager();
 
171
                kt::GroupManager::const_iterator i = gman->begin();
 
172
                while (i != gman->end())
 
173
                {
 
174
                        ret << i->first;
 
175
                        i++;
 
176
                }
 
177
                return ret;
 
178
        }
 
179
                
 
180
        bool DBus::addGroup(const QString & group)
 
181
        {
 
182
                kt::GroupManager* gman = core->getGroupManager();
 
183
                return gman->newGroup(group) != 0;
 
184
        }
 
185
 
 
186
        bool DBus::removeGroup(const QString & group)
 
187
        {
 
188
                kt::GroupManager* gman = core->getGroupManager();
 
189
                Group* g = gman->find(group);
 
190
                if (!g)
 
191
                        return false;
 
192
                
 
193
                gman->removeGroup(g);
 
194
                return true;
 
195
        }
 
196
        
 
197
        void DBus::groupAdded(kt::Group* g)
 
198
        {
 
199
                group_map.insert(g,new DBusGroup(g,core->getGroupManager(),this));
 
200
        }
 
201
        
 
202
        void DBus::groupRemoved(kt::Group* g)
 
203
        {
 
204
                group_map.erase(g);
 
205
        }
 
206
        
 
207
        QObject* DBus::torrent(const QString & info_hash)
 
208
        {
 
209
                return torrent_map.find(info_hash);
 
210
        }
 
211
                
 
212
        QObject* DBus::group(const QString & name)
 
213
        {
 
214
                kt::GroupManager* gman = core->getGroupManager();
 
215
                kt::GroupManager::const_iterator i = gman->begin();
 
216
                while (i != gman->end())
 
217
                {
 
218
                        if (i->first == name)
 
219
                                return group_map.find(i->second);
 
220
                        i++;
 
221
                }
 
222
                return 0;
 
223
        }
 
224
        
 
225
        void DBus::log(const QString & line)
 
226
        {
 
227
                Out(SYS_GEN|LOG_NOTICE) << line << endl;
 
228
        }
 
229
        
 
230
        void DBus::queue(const QString & info_hash)
 
231
        {
 
232
                DBusTorrent* tc = torrent_map.find(info_hash);
 
233
                if (!tc)
 
234
                        return;
 
235
 
 
236
                core->queue(tc->torrent());
 
237
        }
 
238
                
 
239
        void DBus::remove(const QString & info_hash,bool data_to)
 
240
        {
 
241
                DBusTorrent* tc = torrent_map.find(info_hash);
 
242
                if (!tc)
 
243
                        return;
 
244
 
 
245
                core->remove(tc->torrent(),data_to);
 
246
        }
 
247
        
 
248
        void DBus::setPaused(bool pause)
 
249
        {
 
250
                core->setPausedState(pause);
 
251
        }
 
252
                
 
253
        bool DBus::paused()
 
254
        {
 
255
                return core->getPausedState();
 
256
        }
 
257
        
 
258
        uint DBus::numTorrentsRunning() const
 
259
        {
 
260
                return core->getNumTorrentsRunning();
 
261
        }
 
262
 
 
263
        uint DBus::numTorrentsNotRunning() const
 
264
        {
 
265
                return core->getNumTorrentsNotRunning();
 
266
        }
 
267
        
 
268
        QString DBus::dataDir() const
 
269
        {
 
270
                return kt::DataDir();
 
271
        }
 
272
        
 
273
        void DBus::orderQueue()
 
274
        {
 
275
                core->getQueueManager()->orderQueue();
 
276
        }
 
277
        
 
278
}
 
279