Code formatting/indentation unified in trunk
[qtrapids] / src / server / TorrentSession.cpp
index 531cd83..c2fb92b 100644 (file)
@@ -8,118 +8,82 @@ namespace qtrapids
 
 
 
-    TorrentSession::TorrentSession(QObject *parent, QSettings *settings)
+TorrentSession::TorrentSession(QObject *parent, QSettings *settings)
         : QObject(parent)
         , btSession_()
-               , alertWaiter_(new AlertWaiterThread(&btSession_, this))
-    {
-        qDBusRegisterMetaType<qtrapids::TorrentState>();
-        qDBusRegisterMetaType<qtrapids::ParamsMap_t>();
-        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();
+        , alertWaiter_(new AlertWaiterThread(&btSession_, this))
+{
+    qDBusRegisterMetaType<qtrapids::TorrentState>();
+    qDBusRegisterMetaType<qtrapids::ParamsMap_t>();
+    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<alert_t> alertPtr = btSession_.pop_alert();
+void TorrentSession::on_alert()
+//NOTE: al parameter not necessarily needed here, as we pop_alert() now!
+{
 
-        if (alertPtr.get() != NULL) {
+    //qDebug() << "QBittorrentSession:on_alert(" << al << ")";
+    // if (al)
+    //         qDebug() << "on_alert():" << QString::fromStdString(al->message());
 
-            torrent_alert_t *ta = dynamic_cast<torrent_alert_t*> (alertPtr.get());
+    std::auto_ptr<alert_t> alertPtr = btSession_.pop_alert();
 
-            qDebug()
-                               << "QBittorrentSession::on_alert(): "
-                               << QString::fromStdString(alertPtr->message());
+    if (alertPtr.get() != NULL)
+    {
 
+        torrent_alert_t *ta = dynamic_cast<torrent_alert_t*> (alertPtr.get());
 
-            if (ta) {
+        qDebug()
+        << "QBittorrentSession::on_alert(): "
+        << QString::fromStdString(alertPtr->message());
 
-                if (!ta->handle.is_valid()) {
-                    qDebug() << "handle is invalid";
-                    return;
-                }
 
-                TorrentHandle handle(ta->handle);
-                TorrentState state;
+        if (ta)
+        {
 
-                state.hash = Hash2QStr(handle.hash());
-                state.action = TorrentState::action_update;
-                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);
+            if (!ta->handle.is_valid())
+            {
+                qDebug() << "handle is invalid";
+                return;
             }
 
-        }
-    }
-
-    void TorrentSession::getState()
-    {
-        torrents_t::const_iterator p;
-        for (p = torrents_.constBegin(); p != torrents_.constEnd(); ++p) {
-            TorrentHandlePtr handle = *p;
+            TorrentHandle handle(ta->handle);
             TorrentState state;
-            QString hash = Hash2QStr(handle->hash());
-            
-            state.hash = hash;
-            state.name = handle->name();
-            state.action = TorrentState::action_add;
-            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;
+            state.hash = Hash2QStr(handle.hash());
+            state.action = TorrentState::action_update;
+            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);
         }
 
-        qDebug() << "addTorrent: " << path << " save to " << save_path;
-        boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
-            = new libtorrent::torrent_info
-            (boost::filesystem::path(path.toStdString()));
-        addParams.ti = tiTmp;
-
-        // save_path is the only mandatory parameter, rest are optional.
-        addParams.save_path = boost::filesystem::path(save_path.toStdString());
-        //addParams.storage_mode = libtorrent::storage_mode_allocate;
-
-        TorrentHandlePtr handle(new TorrentHandle(btSession_.add_torrent(addParams)));
-        QString hash = Hash2QStr(handle->hash());
+    }
+}
 
+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();
@@ -132,33 +96,78 @@ namespace qtrapids
         state.leeches = handle->numLeeches();
         state.total_size = handle->getTotalSize();
 
-        torrents_[hash] = handle;
-
         emit alert(state, ParamsMap_t());
     }
+}
 
-    void TorrentSession::removeTorrent(const QString &hash)
+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())
     {
-        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"
-                ;
-        }
+        qWarning() << "Torrent file " << path << "doesn't exist";
+        return;
+    }
 
-        TorrentState state;
-        state.hash = hash;
-        state.action = TorrentState::action_remove;
-        emit alert(state, ParamsMap_t());
-        torrents_.erase(p);
+    qDebug() << "addTorrent: " << path << " save to " << save_path;
+    boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
+    = new libtorrent::torrent_info
+    (boost::filesystem::path(path.toStdString()));
+    addParams.ti = tiTmp;
+
+    // save_path is the only mandatory parameter, rest are optional.
+    addParams.save_path = boost::filesystem::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.action = TorrentState::action_add;
+    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"
+        ;
+    }
+
+    TorrentState state;
+    state.hash = hash;
+    state.action = TorrentState::action_remove;
+    emit alert(state, ParamsMap_t());
+    torrents_.erase(p);
+}
 
 
 } // namespace qtrapids