1 #include "TorrentSession.hpp"
2 #include "TorrentHandle.hpp"
3 #include "AlertWaiterThread.hpp"
4 #include "ServerDb.hpp"
5 #include <qtrapids/error.hpp>
12 TorrentSession::TorrentSession(QObject *parent, QSettings *settings)
15 , alertWaiter_(new AlertWaiterThread(&btSession_, this))
16 , settings_(new ServerSettings(*settings))
17 , db_(new ServerDb(settings_.get()))
19 qDBusRegisterMetaType<qtrapids::TorrentState>();
20 qDBusRegisterMetaType<qtrapids::ParamsMap_t>();
21 new QtRapidsServer(this);
23 QDBusConnection dbus = QDBusConnection::sessionBus();
24 dbus.registerObject("/qtrapids", this);
25 dbus.registerService("com.ixonos.qtrapids");
27 alertWaiter_->allAlerts();
28 connect(alertWaiter_, SIGNAL(alert()), this, SLOT(on_alert()));
29 alertWaiter_->start();
34 void TorrentSession::loadState()
36 TorrentDownloadInfo info;
37 TorrentsStorage storage(*db_);
38 while (storage.nextTorrent(info)) {
39 qDebug() << "adding " << info.path;
40 addTorrent_(info.path, info.download_path, ParamsMap_t(), true);
44 void TorrentSession::on_alert()
46 std::auto_ptr<alert_t> alertPtr = btSession_.pop_alert();
48 if (alertPtr.get() != NULL) {
50 torrent_alert_t *ta = dynamic_cast<torrent_alert_t*> (alertPtr.get());
53 << "QBittorrentSession::on_alert(): "
54 << QString::fromStdString(alertPtr->message());
57 if (!ta->handle.is_valid()) {
58 qDebug() << "handle is invalid";
62 TorrentHandle handle(ta->handle);
65 state.hash = Hash2QStr(handle.hash());
66 state.action = TorrentState::action_update;
67 state.state = handle.state();
68 state.progress = handle.progress() * torrent_progress_max;
69 state.down_rate = handle.downloadRate();
70 state.up_rate = handle.uploadRate();
71 state.seeds = handle.numSeeds();
72 state.leeches = handle.numLeeches();
75 emit alert(state, params);
81 void TorrentSession::getState()
83 torrents_t::const_iterator p;
84 for (p = torrents_.constBegin(); p != torrents_.constEnd(); ++p) {
85 TorrentHandlePtr handle = *p;
87 QString hash = Hash2QStr(handle->hash());
90 state.name = handle->name();
91 state.action = TorrentState::action_add;
92 state.state = handle->state();
93 state.progress = handle->progress() * torrent_progress_max;
94 state.down_rate = handle->downloadRate();
95 state.up_rate = handle->uploadRate();
96 state.seeds = handle->numSeeds();
97 state.leeches = handle->numLeeches();
98 state.total_size = handle->getTotalSize();
100 emit alert(state, ParamsMap_t());
104 void TorrentSession::addTorrent(const QString &path, const QString &save_path
105 , qtrapids::ParamsMap_t other_params)
107 return addTorrent_(path, save_path, other_params, false);
110 void TorrentSession::addTorrent_(const QString &path, const QString &save_path
111 , const qtrapids::ParamsMap_t &other_params
112 , bool is_restore_session)
114 add_torrent_params_t addParams;
115 QFile torrent_file(path);
116 QDir::home().mkdir(settings_->getTorrentsSubDir());
118 if (!torrent_file.exists()) {
119 qWarning() << "Torrent file " << path << "doesn't exist";
123 QString new_torrent_fname(QDir(settings_->getTorrentsDir())
124 .filePath(QFileInfo(path).fileName()));
125 qDebug() << "copy to " << new_torrent_fname;
126 torrent_file.copy(new_torrent_fname);
128 qDebug() << "addTorrent: " << path << " save to " << save_path;
129 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
130 = new libtorrent::torrent_info
131 (boost::filesystem::path(new_torrent_fname.toStdString()));
132 addParams.ti = tiTmp;
134 QString download_dir;
135 if (!save_path.isEmpty()) {
136 download_dir = save_path;
138 download_dir = settings_->getDownloadDir();
140 // save_path is the only mandatory parameter, rest are optional.
141 addParams.save_path = boost::filesystem::path(download_dir.toStdString());
142 //addParams.storage_mode = libtorrent::storage_mode_allocate;
144 TorrentHandlePtr handle(new TorrentHandle(btSession_.add_torrent(addParams)));
145 QString hash = Hash2QStr(handle->hash());
147 if (!is_restore_session) {
148 db_->addTorrent(hash, path, save_path);
154 state.name = handle->name();
155 state.action = TorrentState::action_add;
156 state.state = handle->state();
157 state.progress = handle->progress() * torrent_progress_max;
158 state.down_rate = handle->downloadRate();
159 state.up_rate = handle->uploadRate();
160 state.seeds = handle->numSeeds();
161 state.leeches = handle->numLeeches();
162 state.total_size = handle->getTotalSize();
164 torrents_[hash] = handle;
166 emit alert(state, ParamsMap_t());
169 void TorrentSession::removeTorrent(const QString &hash)
171 torrents_t::iterator p = torrents_.find(hash);
173 if (p == torrents_.end()) {
174 qDebug() << "Invalid request to remove torrent with hash " << hash;
178 btSession_.remove_torrent(p.value()->getHandle());
179 } catch (torrent_exception_t e) {
180 qDebug() << // e.what()
187 state.action = TorrentState::action_remove;
188 emit alert(state, ParamsMap_t());
190 db_->removeTorrent(hash);
194 } // namespace qtrapids