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)
44 torrent_alert_t *ta = dynamic_cast<torrent_alert_t*> (alertPtr.get());
47 << "QBittorrentSession::on_alert(): "
48 << QString::fromStdString(alertPtr->message());
54 if (!ta->handle.is_valid())
56 qDebug() << "handle is invalid";
60 TorrentHandle handle(ta->handle);
63 state.hash = Hash2QStr(handle.hash());
64 state.action = TorrentState::action_update;
65 state.state = handle.state();
66 state.progress = handle.progress() * torrent_progress_max;
67 state.down_rate = handle.downloadRate();
68 state.up_rate = handle.uploadRate();
69 state.seeds = handle.numSeeds();
70 state.leeches = handle.numLeeches();
73 emit alert(state, params);
79 void TorrentSession::getState()
81 torrents_t::const_iterator p;
82 for (p = torrents_.constBegin(); p != torrents_.constEnd(); ++p)
84 TorrentHandlePtr handle = *p;
86 QString hash = Hash2QStr(handle->hash());
89 state.name = handle->name();
90 state.action = TorrentState::action_add;
91 state.state = handle->state();
92 state.progress = handle->progress() * torrent_progress_max;
93 state.down_rate = handle->downloadRate();
94 state.up_rate = handle->uploadRate();
95 state.seeds = handle->numSeeds();
96 state.leeches = handle->numLeeches();
97 state.total_size = handle->getTotalSize();
99 emit alert(state, ParamsMap_t());
103 void TorrentSession::addTorrent(const QString &path, const QString &save_path
104 , qtrapids::ParamsMap_t other_params)
106 add_torrent_params_t addParams;
107 QFile torrent_file(path);
108 if (!torrent_file.exists())
110 qWarning() << "Torrent file " << path << "doesn't exist";
114 qDebug() << "addTorrent: " << path << " save to " << save_path;
115 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
116 = new libtorrent::torrent_info
117 (boost::filesystem::path(path.toStdString()));
118 addParams.ti = tiTmp;
120 // save_path is the only mandatory parameter, rest are optional.
121 addParams.save_path = boost::filesystem::path(save_path.toStdString());
122 //addParams.storage_mode = libtorrent::storage_mode_allocate;
124 TorrentHandlePtr handle(new TorrentHandle(btSession_.add_torrent(addParams)));
125 QString hash = Hash2QStr(handle->hash());
130 state.name = handle->name();
131 state.action = TorrentState::action_add;
132 state.state = handle->state();
133 state.progress = handle->progress() * torrent_progress_max;
134 state.down_rate = handle->downloadRate();
135 state.up_rate = handle->uploadRate();
136 state.seeds = handle->numSeeds();
137 state.leeches = handle->numLeeches();
138 state.total_size = handle->getTotalSize();
140 torrents_[hash] = handle;
142 emit alert(state, ParamsMap_t());
145 void TorrentSession::removeTorrent(const QString &hash)
147 torrents_t::iterator p = torrents_.find(hash);
149 if (p == torrents_.end())
151 qDebug() << "Invalid request to remove torrent with hash " << hash;
156 btSession_.remove_torrent(p.value()->getHandle());
158 catch (torrent_exception_t e)
160 qDebug() << // e.what()
167 state.action = TorrentState::action_remove;
168 emit alert(state, ParamsMap_t());
173 } // namespace qtrapids