X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Fserver%2FTorrentSession.cpp;fp=src%2Fserver%2FTorrentSession.cpp;h=514b2c9fb8d010cf5c6b35ef28cff7c05f00bc42;hb=06410091b1e07c443849f0fe71050654c6fb9710;hp=0000000000000000000000000000000000000000;hpb=d3d85653bf84dadcf6c2890cc2ddf9f629ee7619;p=qtrapids diff --git a/src/server/TorrentSession.cpp b/src/server/TorrentSession.cpp new file mode 100644 index 0000000..514b2c9 --- /dev/null +++ b/src/server/TorrentSession.cpp @@ -0,0 +1,147 @@ +#include "TorrentSession.hpp" +#include "TorrentHandle.hpp" +#include "AlertWaiterThread.hpp" + + +namespace qtrapids +{ + + + + TorrentSession::TorrentSession(QObject *parent, QSettings *settings) + : QObject(parent) + , btSession_() + , alertWaiter_(new AlertWaiterThread(&btSession_, this)) + { + qDBusRegisterMetaType(); + qDBusRegisterMetaType(); + new QtRapidsServer(this); + + QDBusConnection dbus = QDBusConnection::sessionBus(); + dbus.registerObject("/qtrapids", this); + dbus.registerService("com.ixonos.qtrapids"); + + alertWaiter_->allAlerts(); + connect(alertWaiter_, SIGNAL(alert()), this, SLOT(on_alert())); + alertWaiter_->start(); + + + } + + void TorrentSession::on_alert() + //NOTE: al parameter not necessarily needed here, as we pop_alert() now! + { + + //qDebug() << "QBittorrentSession:on_alert(" << al << ")"; + // if (al) + // qDebug() << "on_alert():" << QString::fromStdString(al->message()); + + std::auto_ptr alertPtr = btSession_.pop_alert(); + + if (alertPtr.get() != NULL) { + + torrent_alert_t *ta = dynamic_cast (alertPtr.get()); + + qDebug() + << "QBittorrentSession::on_alert(): " + << QString::fromStdString(alertPtr->message()); + + + if (ta) { + TorrentHandle handle(ta->handle); + TorrentState state; + + state.hash = Hash2QStr(handle.hash()); + state.state = handle.state(); + state.progress = handle.progress() * torrent_progress_max; + state.down_rate = handle.downloadRate(); + state.up_rate = handle.uploadRate(); + state.seeds = handle.numSeeds(); + state.leeches = handle.numLeeches(); + + ParamsMap_t params; + emit alert(state, params); + } + + } + } + + void TorrentSession::getState() + { + torrents_t::const_iterator p; + for (p = torrents_.constBegin(); p != torrents_.constEnd(); ++p) { + TorrentHandlePtr handle = *p; + TorrentState state; + QString hash = Hash2QStr(handle->hash()); + + state.hash = hash; + state.name = handle->name(); + state.state = handle->state(); + state.progress = handle->progress() * torrent_progress_max; + state.down_rate = handle->downloadRate(); + state.up_rate = handle->uploadRate(); + state.seeds = handle->numSeeds(); + state.leeches = handle->numLeeches(); + state.total_size = handle->getTotalSize(); + + emit alert(state, ParamsMap_t()); + } + } + + void TorrentSession::addTorrent(const QString &path, const QString &save_path + , qtrapids::ParamsMap_t other_params) + { + add_torrent_params_t addParams; + QFile torrent_file(path); + if (!torrent_file.exists()) { + qWarning() << "Torrent file " << path << "doesn't exist"; + return; + } + + qDebug() << "addTorrent: " << path << " save to " << save_path; + boost::intrusive_ptr tiTmp + = new libtorrent::torrent_info(path.toStdString()); + addParams.ti = tiTmp; + + // save_path is the only mandatory parameter, rest are optional. + addParams.save_path = save_path.toStdString(); + //addParams.storage_mode = libtorrent::storage_mode_allocate; + + TorrentHandlePtr handle(new TorrentHandle(btSession_.add_torrent(addParams))); + QString hash = Hash2QStr(handle->hash()); + + TorrentState state; + + state.hash = hash; + state.name = handle->name(); + state.state = handle->state(); + state.progress = handle->progress() * torrent_progress_max; + state.down_rate = handle->downloadRate(); + state.up_rate = handle->uploadRate(); + state.seeds = handle->numSeeds(); + state.leeches = handle->numLeeches(); + state.total_size = handle->getTotalSize(); + + torrents_[hash] = handle; + + emit alert(state, ParamsMap_t()); + } + + void TorrentSession::removeTorrent(const QString &hash) + { + torrents_t::iterator p = torrents_.find(hash); + if (p == torrents_.end()) { + qDebug() << "Invalid request to remove torrent with hash " << hash; + return; + } + try { + btSession_.remove_torrent(p.value()->getHandle()); + } catch (torrent_exception_t e) { + qDebug() << // e.what() + "exception catched" + ; + } + } + + +} // namespace qtrapids