1 #include "TorrentSession.hpp"
2 #include "TorrentHandle.hpp"
3 #include "AlertWaiterThread.hpp"
11 TorrentSession::TorrentSession(QObject *parent, QSettings *settings)
14 , alertWaiter_(new AlertWaiterThread(&btSession_, this))
16 qDBusRegisterMetaType<qtrapids::TorrentState>();
17 qDBusRegisterMetaType<qtrapids::ParamsMap_t>();
18 new QtRapidsServer(this);
20 QDBusConnection dbus = QDBusConnection::sessionBus();
21 dbus.registerObject("/qtrapids", this);
22 dbus.registerService("com.ixonos.qtrapids");
24 alertWaiter_->allAlerts();
25 connect(alertWaiter_, SIGNAL(alert()), this, SLOT(on_alert()));
26 alertWaiter_->start();
31 void TorrentSession::on_alert()
32 //NOTE: al parameter not necessarily needed here, as we pop_alert() now!
35 //qDebug() << "QBittorrentSession:on_alert(" << al << ")";
37 // qDebug() << "on_alert():" << QString::fromStdString(al->message());
39 std::auto_ptr<alert_t> alertPtr = btSession_.pop_alert();
41 if (alertPtr.get() != NULL) {
43 torrent_alert_t *ta = dynamic_cast<torrent_alert_t*> (alertPtr.get());
46 << "QBittorrentSession::on_alert(): "
47 << QString::fromStdString(alertPtr->message());
51 TorrentHandle handle(ta->handle);
54 state.hash = Hash2QStr(handle.hash());
55 state.state = handle.state();
56 state.progress = handle.progress() * torrent_progress_max;
57 state.down_rate = handle.downloadRate();
58 state.up_rate = handle.uploadRate();
59 state.seeds = handle.numSeeds();
60 state.leeches = handle.numLeeches();
63 emit alert(state, params);
69 void TorrentSession::getState()
71 torrents_t::const_iterator p;
72 for (p = torrents_.constBegin(); p != torrents_.constEnd(); ++p) {
73 TorrentHandlePtr handle = *p;
75 QString hash = Hash2QStr(handle->hash());
78 state.name = handle->name();
79 state.state = handle->state();
80 state.progress = handle->progress() * torrent_progress_max;
81 state.down_rate = handle->downloadRate();
82 state.up_rate = handle->uploadRate();
83 state.seeds = handle->numSeeds();
84 state.leeches = handle->numLeeches();
85 state.total_size = handle->getTotalSize();
87 emit alert(state, ParamsMap_t());
91 void TorrentSession::addTorrent(const QString &path, const QString &save_path
92 , qtrapids::ParamsMap_t other_params)
94 add_torrent_params_t addParams;
95 QFile torrent_file(path);
96 if (!torrent_file.exists()) {
97 qWarning() << "Torrent file " << path << "doesn't exist";
101 qDebug() << "addTorrent: " << path << " save to " << save_path;
102 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
103 = new libtorrent::torrent_info(path.toStdString());
104 addParams.ti = tiTmp;
106 // save_path is the only mandatory parameter, rest are optional.
107 addParams.save_path = save_path.toStdString();
108 //addParams.storage_mode = libtorrent::storage_mode_allocate;
110 TorrentHandlePtr handle(new TorrentHandle(btSession_.add_torrent(addParams)));
111 QString hash = Hash2QStr(handle->hash());
116 state.name = handle->name();
117 state.state = handle->state();
118 state.progress = handle->progress() * torrent_progress_max;
119 state.down_rate = handle->downloadRate();
120 state.up_rate = handle->uploadRate();
121 state.seeds = handle->numSeeds();
122 state.leeches = handle->numLeeches();
123 state.total_size = handle->getTotalSize();
125 torrents_[hash] = handle;
127 emit alert(state, ParamsMap_t());
130 void TorrentSession::removeTorrent(const QString &hash)
132 torrents_t::iterator p = torrents_.find(hash);
133 if (p == torrents_.end()) {
134 qDebug() << "Invalid request to remove torrent with hash " << hash;
138 btSession_.remove_torrent(p.value()->getHandle());
139 } catch (torrent_exception_t e) {
140 qDebug() << // e.what()
147 } // namespace qtrapids