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.state = handle.state();
62 state.progress = handle.progress() * torrent_progress_max;
63 state.down_rate = handle.downloadRate();
64 state.up_rate = handle.uploadRate();
65 state.seeds = handle.numSeeds();
66 state.leeches = handle.numLeeches();
69 emit alert(state, params);
75 void TorrentSession::getState()
77 torrents_t::const_iterator p;
78 for (p = torrents_.constBegin(); p != torrents_.constEnd(); ++p) {
79 TorrentHandlePtr handle = *p;
81 QString hash = Hash2QStr(handle->hash());
84 state.name = handle->name();
85 state.state = handle->state();
86 state.progress = handle->progress() * torrent_progress_max;
87 state.down_rate = handle->downloadRate();
88 state.up_rate = handle->uploadRate();
89 state.seeds = handle->numSeeds();
90 state.leeches = handle->numLeeches();
91 state.total_size = handle->getTotalSize();
93 emit alert(state, ParamsMap_t());
97 void TorrentSession::addTorrent(const QString &path, const QString &save_path
98 , qtrapids::ParamsMap_t other_params)
100 add_torrent_params_t addParams;
101 QFile torrent_file(path);
102 if (!torrent_file.exists()) {
103 qWarning() << "Torrent file " << path << "doesn't exist";
107 qDebug() << "addTorrent: " << path << " save to " << save_path;
108 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
109 = new libtorrent::torrent_info
110 (boost::filesystem::path(path.toStdString()));
111 addParams.ti = tiTmp;
113 // save_path is the only mandatory parameter, rest are optional.
114 addParams.save_path = boost::filesystem::path(save_path.toStdString());
115 //addParams.storage_mode = libtorrent::storage_mode_allocate;
117 TorrentHandlePtr handle(new TorrentHandle(btSession_.add_torrent(addParams)));
118 QString hash = Hash2QStr(handle->hash());
123 state.name = handle->name();
124 state.state = handle->state();
125 state.progress = handle->progress() * torrent_progress_max;
126 state.down_rate = handle->downloadRate();
127 state.up_rate = handle->uploadRate();
128 state.seeds = handle->numSeeds();
129 state.leeches = handle->numLeeches();
130 state.total_size = handle->getTotalSize();
132 torrents_[hash] = handle;
134 emit alert(state, ParamsMap_t());
137 void TorrentSession::removeTorrent(const QString &hash)
139 torrents_t::iterator p = torrents_.find(hash);
140 if (p == torrents_.end()) {
141 qDebug() << "Invalid request to remove torrent with hash " << hash;
145 btSession_.remove_torrent(p.value()->getHandle());
146 } catch (torrent_exception_t e) {
147 qDebug() << // e.what()
154 } // namespace qtrapids