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());
52 if (!ta->handle.is_valid()) {
53 qDebug() << "handle is invalid";
57 TorrentHandle handle(ta->handle);
60 state.hash = Hash2QStr(handle.hash());
61 state.action = TorrentState::action_update;
62 state.state = handle.state();
63 state.progress = handle.progress() * torrent_progress_max;
64 state.down_rate = handle.downloadRate();
65 state.up_rate = handle.uploadRate();
66 state.seeds = handle.numSeeds();
67 state.leeches = handle.numLeeches();
70 emit alert(state, params);
76 void TorrentSession::getState()
78 torrents_t::const_iterator p;
79 for (p = torrents_.constBegin(); p != torrents_.constEnd(); ++p) {
80 TorrentHandlePtr handle = *p;
82 QString hash = Hash2QStr(handle->hash());
85 state.name = handle->name();
86 state.action = TorrentState::action_add;
87 state.state = handle->state();
88 state.progress = handle->progress() * torrent_progress_max;
89 state.down_rate = handle->downloadRate();
90 state.up_rate = handle->uploadRate();
91 state.seeds = handle->numSeeds();
92 state.leeches = handle->numLeeches();
93 state.total_size = handle->getTotalSize();
95 emit alert(state, ParamsMap_t());
99 void TorrentSession::addTorrent(const QString &path, const QString &save_path
100 , qtrapids::ParamsMap_t other_params)
102 add_torrent_params_t addParams;
103 QFile torrent_file(path);
104 if (!torrent_file.exists()) {
105 qWarning() << "Torrent file " << path << "doesn't exist";
109 qDebug() << "addTorrent: " << path << " save to " << save_path;
110 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
111 = new libtorrent::torrent_info
112 (boost::filesystem::path(path.toStdString()));
113 addParams.ti = tiTmp;
115 // save_path is the only mandatory parameter, rest are optional.
116 addParams.save_path = boost::filesystem::path(save_path.toStdString());
117 //addParams.storage_mode = libtorrent::storage_mode_allocate;
119 TorrentHandlePtr handle(new TorrentHandle(btSession_.add_torrent(addParams)));
120 QString hash = Hash2QStr(handle->hash());
125 state.name = handle->name();
126 state.action = TorrentState::action_add;
127 state.state = handle->state();
128 state.progress = handle->progress() * torrent_progress_max;
129 state.down_rate = handle->downloadRate();
130 state.up_rate = handle->uploadRate();
131 state.seeds = handle->numSeeds();
132 state.leeches = handle->numLeeches();
133 state.total_size = handle->getTotalSize();
135 torrents_[hash] = handle;
137 emit alert(state, ParamsMap_t());
140 void TorrentSession::removeTorrent(const QString &hash)
142 torrents_t::iterator p = torrents_.find(hash);
144 if (p == torrents_.end()) {
145 qDebug() << "Invalid request to remove torrent with hash " << hash;
149 btSession_.remove_torrent(p.value()->getHandle());
150 } catch (torrent_exception_t e) {
151 qDebug() << // e.what()
158 state.action = TorrentState::action_remove;
159 emit alert(state, ParamsMap_t());
164 } // namespace qtrapids