1
/***************************************************************************
2
* Copyright (C) 2007 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
***************************************************************************/
21
#include <QDBusConnection>
23
#include <interfaces/torrentinterface.h>
24
#include <torrent/queuemanager.h>
26
#include <util/sha1hash.h>
27
#include <groups/groupmanager.h>
29
#include <interfaces/coreinterface.h>
30
#include <interfaces/guiinterface.h>
31
#include "dbustorrent.h"
32
#include "dbusgroup.h"
40
DBus::DBus(GUIInterface* gui,CoreInterface* core,QObject* parent) : QObject(parent),gui(gui),core(core)
42
torrent_map.setAutoDelete(true);
43
group_map.setAutoDelete(true);
45
QDBusConnection::sessionBus().registerObject("/core", this,
46
QDBusConnection::ExportScriptableSlots|QDBusConnection::ExportScriptableSignals);
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()));
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++)
61
connect(qm,SIGNAL(pauseStateChanged(bool)),this,SIGNAL(pauseStateChanged(bool)));
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())
69
groupAdded(i->second);
78
QStringList DBus::torrents()
81
DBusTorrentItr i = torrent_map.begin();
82
while (i != torrent_map.end())
84
tors.append(i->first);
91
void DBus::start(const QString & info_hash)
93
DBusTorrent* tc = torrent_map.find(info_hash);
97
core->getQueueManager()->start(tc->torrent(),true);
100
void DBus::stop(const QString & info_hash)
102
DBusTorrent* tc = torrent_map.find(info_hash);
106
core->getQueueManager()->stop(tc->torrent(),true);
109
void DBus::startAll()
119
void DBus::torrentAdded(bt::TorrentInterface* tc)
121
DBusTorrent* db = new DBusTorrent(tc,this);
122
torrent_map.insert(db->infoHash(),db);
123
torrentAdded(db->infoHash());
126
void DBus::torrentRemoved(bt::TorrentInterface* tc)
128
DBusTorrent* db = torrent_map.find(tc->getInfoHash().toString());
131
QString ih = db->infoHash();
133
torrent_map.erase(ih);
137
void DBus::finished(bt::TorrentInterface* tc)
139
DBusTorrent* db = torrent_map.find(tc->getInfoHash().toString());
142
QString ih = db->infoHash();
147
void DBus::torrentStoppedByError(bt::TorrentInterface* tc, QString msg)
149
DBusTorrent* db = torrent_map.find(tc->getInfoHash().toString());
152
QString ih = db->infoHash();
153
torrentStoppedByError(ih,msg);
157
void DBus::load(const QString & url,const QString & group)
159
core->load(KUrl(url),group);
162
void DBus::loadSilently(const QString & url,const QString & group)
164
core->loadSilently(KUrl(url),group);
167
QStringList DBus::groups() const
170
kt::GroupManager* gman = core->getGroupManager();
171
kt::GroupManager::const_iterator i = gman->begin();
172
while (i != gman->end())
180
bool DBus::addGroup(const QString & group)
182
kt::GroupManager* gman = core->getGroupManager();
183
return gman->newGroup(group) != 0;
186
bool DBus::removeGroup(const QString & group)
188
kt::GroupManager* gman = core->getGroupManager();
189
Group* g = gman->find(group);
193
gman->removeGroup(g);
197
void DBus::groupAdded(kt::Group* g)
199
group_map.insert(g,new DBusGroup(g,core->getGroupManager(),this));
202
void DBus::groupRemoved(kt::Group* g)
207
QObject* DBus::torrent(const QString & info_hash)
209
return torrent_map.find(info_hash);
212
QObject* DBus::group(const QString & name)
214
kt::GroupManager* gman = core->getGroupManager();
215
kt::GroupManager::const_iterator i = gman->begin();
216
while (i != gman->end())
218
if (i->first == name)
219
return group_map.find(i->second);
225
void DBus::log(const QString & line)
227
Out(SYS_GEN|LOG_NOTICE) << line << endl;
230
void DBus::queue(const QString & info_hash)
232
DBusTorrent* tc = torrent_map.find(info_hash);
236
core->queue(tc->torrent());
239
void DBus::remove(const QString & info_hash,bool data_to)
241
DBusTorrent* tc = torrent_map.find(info_hash);
245
core->remove(tc->torrent(),data_to);
248
void DBus::setPaused(bool pause)
250
core->setPausedState(pause);
255
return core->getPausedState();
258
uint DBus::numTorrentsRunning() const
260
return core->getNumTorrentsRunning();
263
uint DBus::numTorrentsNotRunning() const
265
return core->getNumTorrentsNotRunning();
268
QString DBus::dataDir() const
270
return kt::DataDir();
273
void DBus::orderQueue()
275
core->getQueueManager()->orderQueue();