- Torrent addign and removal functional
authorlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Tue, 27 Oct 2009 11:22:46 +0000 (11:22 +0000)
committerlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Tue, 27 Oct 2009 11:22:46 +0000 (11:22 +0000)
- Additions to QTorrentHandle
- TODO: Test torrent downloading.

git-svn-id: file:///svnroot/qtrapids/trunk@14 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda

12 files changed:
src/engine/AlertWaiterThread.cpp
src/engine/AlertWaiterThread.h
src/engine/QBittorrentSession.cpp
src/engine/QBittorrentSession.h
src/engine/QTorrentHandle.cpp
src/engine/QTorrentHandle.h
src/gui/DownloadView.cpp
src/gui/DownloadView.h
src/gui/MainWindow.cpp
src/gui/MainWindow.h
src/gui/SeedView.cpp
src/gui/SeedView.h

index b5d6e95..971967c 100644 (file)
@@ -55,7 +55,7 @@ void AlertWaiterThread::allAlerts(bool enable)
 
 void AlertWaiterThread::run()
 {
-       TorrentAlert const *alertTemp = NULL;
+       Alert const *alertTemp = NULL;
        while (true)
        {
                qDebug() << "AlertWaiter running";
index 6ab719e..ec46d7f 100644 (file)
@@ -41,7 +41,7 @@ class AlertWaiterThread : public QThread
                virtual void run(); // Overridden from QThread
                
        signals:
-               void alert(TorrentAlert const *alert);
+               void alert(Alert const *alert);
                
        private:
                TorrentSession *const btSession_;
index dd94f95..57054c3 100644 (file)
@@ -31,7 +31,7 @@ QBittorrentSession::QBittorrentSession(QObject *parent):
 {
        alertWaiter_ = new AlertWaiterThread(&btSession_, this);
        alertWaiter_->allAlerts();
-       connect(alertWaiter_, SIGNAL(alert(TorrentAlert const*)), this, SLOT(on_alert(TorrentAlert const*)));
+       connect(alertWaiter_, SIGNAL(alert(Alert const*)), this, SLOT(on_alert(Alert const*)));
        alertWaiter_->start();
 }
 
@@ -41,17 +41,24 @@ QBittorrentSession::~QBittorrentSession()
 }
 
 
-std::auto_ptr<QTorrentHandle> 
+QTorrentHandle 
 QBittorrentSession::addTorrent(AddTorrentParams const& params)
 {
        // Delegate to Libtorrent and return QTorrentHandle.
-       std::auto_ptr<QTorrentHandle> handlePtr(new QTorrentHandle(btSession_.add_torrent(params)));
-       return handlePtr;
+       //std::auto_ptr<QTorrentHandle> handlePtr(new QTorrentHandle(btSession_.add_torrent(params)));
+       QTorrentHandle  handle = QTorrentHandle(btSession_.add_torrent(params));
+       return handle;
+}
+
+
+void QBittorrentSession::removeTorrent(QTorrentHandle const& handle) 
+{
+       btSession_.remove_torrent(handle.getHandle());
 }
 
 
 // ========================== SLOTS ==============================
-void QBittorrentSession::on_alert(TorrentAlert const *al) 
+void QBittorrentSession::on_alert(Alert const *al) 
                //NOTE: al parameter not necessarily needed here, as we pop_alert() now!
 {
        
@@ -59,8 +66,9 @@ void QBittorrentSession::on_alert(TorrentAlert const *al)
 //     if (al)
 //             qDebug() << "on_alert():" << QString::fromStdString(al->message());
        
-       std::auto_ptr<TorrentAlert> alertPtr = btSession_.pop_alert();
+       std::auto_ptr<Alert> alertPtr = btSession_.pop_alert();
        emit alert(alertPtr);
 }
 
 
+
index d941a92..9cf5381 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <libtorrent/session.hpp>
 #include <libtorrent/torrent_info.hpp>
+#include <libtorrent/alert_types.hpp>
 
 #include "QTorrentHandle.h"
 
@@ -34,7 +35,8 @@
 class AlertWaiterThread;
 typedef libtorrent::session TorrentSession;
 typedef libtorrent::add_torrent_params AddTorrentParams;
-typedef libtorrent::alert TorrentAlert;
+typedef libtorrent::alert Alert;
+typedef        libtorrent::torrent_alert TorrentAlert;
 typedef libtorrent::sha1_hash Sha1Hash;
 
 
@@ -50,13 +52,14 @@ class QBittorrentSession : public QObject {
     ~QBittorrentSession();
                
                /// @brief Add torrent to session.
-               std::auto_ptr<QTorrentHandle> addTorrent(AddTorrentParams const& params);
+               QTorrentHandle addTorrent(AddTorrentParams const& params);
+               void removeTorrent(QTorrentHandle const& handle);
                
        signals:
-               void alert(std::auto_ptr<TorrentAlert> al);
+               void alert(std::auto_ptr<Alert> al);
                
        private slots:
-               void on_alert(TorrentAlert const *al);
+               void on_alert(Alert const *al);
                
        private:
                TorrentSession btSession_;
index c69162c..3fb6ec0 100644 (file)
@@ -55,20 +55,6 @@ QString QTorrentHandle::name() const
        return QString::fromStdString(torrentHandle_.name());
 }
 
-
-
-
-bool QTorrentHandle::operator==(QTorrentHandle const& h)
-{
-       return torrentHandle_ == h.torrentHandle_;
-}
-
-
-bool QTorrentHandle::operator<(QTorrentHandle const& h)
-{
-       return torrentHandle_ < h.torrentHandle_;
-}
-
 size_t QTorrentHandle::getTotalSize() const
 {
        TorrentInfo info = getTorrentInfo();
@@ -99,18 +85,21 @@ float QTorrentHandle::downloadRate() const
        return statusTmp.download_rate;
 }
 
+
 qint32 QTorrentHandle::numSeeds() const
 {
        TorrentStatus statusTmp = status();
        return statusTmp.list_seeds;
 }
 
+
 qint32 QTorrentHandle::numLeeches() const
 {
        TorrentStatus statusTmp = status();
        return (statusTmp.list_peers - statusTmp.list_seeds);
 }
 
+
 qint32 QTorrentHandle::ratio() const
 {
        TorrentStatus statusTmp = status();
@@ -125,6 +114,25 @@ qint32 QTorrentHandle::ratio() const
 }
 
 
+TorrentHandle QTorrentHandle::getHandle() const
+{
+       return torrentHandle_;
+}
+
+
+bool QTorrentHandle::operator==(QTorrentHandle const& h) const
+{
+       return torrentHandle_ == h.torrentHandle_;
+}
+
+
+bool QTorrentHandle::operator<(QTorrentHandle const& h) const
+{
+       return torrentHandle_ < h.torrentHandle_;
+}
+
+
+
 QString QTorrentHandle::GetStatusString(TorrentStatus const& status) const
 {
        switch (status.state) {
@@ -147,4 +155,5 @@ QString QTorrentHandle::GetStatusString(TorrentStatus const& status) const
        }
 }
 
+
                                
\ No newline at end of file
index c102086..f03194d 100644 (file)
@@ -27,6 +27,7 @@
 
 typedef libtorrent::torrent_status TorrentStatus;
 typedef libtorrent::torrent_info TorrentInfo;
+typedef libtorrent::torrent_handle TorrentHandle;
 
 /**
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
@@ -55,13 +56,14 @@ class QTorrentHandle
                qint32 numLeeches() const;
                qint32 ratio() const;
                
-
-               bool operator==(QTorrentHandle const& h); 
-               bool operator<(QTorrentHandle const& h);
+               TorrentHandle getHandle() const;
+               
+               bool operator==(QTorrentHandle const& h) const; 
+               bool operator<(QTorrentHandle const& h) const;
                
        private:
                QTorrentHandle(); // Prevent default construct.
-               libtorrent::torrent_handle torrentHandle_;
+               TorrentHandle torrentHandle_;
                
                // Private functions.
                QString GetStatusString(TorrentStatus const& status) const;
index 6912b5e..2f91ff4 100644 (file)
@@ -17,6 +17,7 @@
  *   Free Software Foundation, Inc.,                                       *
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
+#include <QDebug>
 #include "DownloadView.h"
 
 DownloadView::DownloadView(QWidget* parent) : 
@@ -25,6 +26,10 @@ DownloadView::DownloadView(QWidget* parent) :
 {
        setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
        setHeaderItem(DownloadViewItem::getHeaderItem());
+       
+       connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
+                                       this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
+
 }
 
 
@@ -32,26 +37,94 @@ DownloadView::~DownloadView()
 {
 }
 
-void DownloadView::newItem(QTorrentHandle const* handle)
+
+void DownloadView::newItem(QTorrentHandle handle)
 {
+       qDebug() << "DownloadView::newItem() " << items_.count(handle);
 
        DownloadViewItem *item = new DownloadViewItem(QStringList()
-                       << handle->name()
-                       << QString::number(handle->getTotalSize())
-                       << handle->state()
-                       << QString::number(handle->progress())
-                       << QString::number(handle->downloadRate()) 
-                       << QString::number(handle->uploadRate()) 
-                       << QString::number(handle->numSeeds()) + "/" + QString::number(handle->numLeeches())
-                       << QString::number(handle->ratio())
+                       << handle.name()
+                       << QString::number(handle.getTotalSize())
+                       << handle.state()
+                       << QString::number(handle.progress())
+                       << QString::number(handle.downloadRate()) 
+                       << QString::number(handle.uploadRate()) 
+                       << QString::number(handle.numSeeds()) + "/"
+                                       + QString::number(handle.numLeeches())
+                       << QString::number(handle.ratio())
                        << "ETA" );
        
        addTopLevelItem(item);
+       items_[handle] = item;
+}
+
+
+void DownloadView::updateItem(QTorrentHandle handle)
+{
+       qDebug() << "DownloadView::updateItem() "  << items_.count(handle);
        
+       if (items_.count(handle) > 0) {
+               DownloadViewItem *item = items_[handle];
+               item->setData(2, Qt::DisplayRole, QVariant(handle.state()));
+               item->setData(3, Qt::DisplayRole,
+                                                                       QVariant(QString::number(handle.progress())));
+               item->setData(4, Qt::DisplayRole,
+                                                                       QVariant(QString::number(handle.downloadRate())));
+               item->setData(5, Qt::DisplayRole,
+                                                                       QVariant(QString::number(handle.uploadRate())));
+               item->setData(6, Qt::DisplayRole, 
+                                                                       QString::number(handle.numSeeds()) + "/" 
+                                                                               + QString::number(handle.numLeeches()));
+       }
 }
 
-void DownloadView::updateItem(QTorrentHandle const* handle)
+
+QTorrentHandle DownloadView::removeSelected()
 {
+       qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
+
+       DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
+       
+       std::map<QTorrentHandle, DownloadViewItem*>::iterator listIter
+                       = items_.begin();
+       std::map<QTorrentHandle, DownloadViewItem*>::iterator listEnd
+                       = items_.end();
+       
+       while (listIter != listEnd) {
+               if (listIter->second == item) {
+                       break;
+               }
+               ++listIter;
+       }
+       
+       QTorrentHandle handle = listIter->first;
+       items_.erase(listIter);
+
+       
+       int index = indexOfTopLevelItem(currentItem());
+       if (index >= 0) {
+               takeTopLevelItem(index);
+       }
+       
+       qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
+       
+       return handle;
 }
 
 
+void DownloadView::removeItem(QTorrentHandle handle)
+{
+}
+
+
+void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
+{/*
+       qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
+       qDebug() << "current item" << currentItem();
+       
+       if (item == currentItem() && item->isSelected()) {
+               item->setSelected(false);
+       }
+       */
+}
+
index 80b5228..408a232 100644 (file)
@@ -42,13 +42,17 @@ Q_OBJECT
 
     ~DownloadView();
 
-               void newItem(QTorrentHandle const* handle);
-               void updateItem(QTorrentHandle const* handle);
+               void newItem(QTorrentHandle handle);
+               void updateItem(QTorrentHandle handle);
+               QTorrentHandle removeSelected();
+               void removeItem(QTorrentHandle handle);
                
+       private slots:
+               void on_itemClicked(QTreeWidgetItem * item, int column);
        private:
                // Maps torrent to downloadview item.
                // Key: SHA1 info hash of torrent. Data: View item corresponding to torrent.
-               std::map<Sha1Hash, DownloadViewItem*> items_;
+               std::map<QTorrentHandle, DownloadViewItem*> items_;
                
 };
 
index 1f1db3a..d45cc96 100644 (file)
@@ -48,7 +48,8 @@ MainWindow::MainWindow():
        dlView_(NULL),
        seedView_(NULL),
        preferencesDialog_(NULL),
-       settings_(),                                                                     
+       settings_(),
+//     torrentHandles_(),
        btSession_()                                    
 {
        // MENUBAR 
@@ -57,6 +58,8 @@ MainWindow::MainWindow():
        
        tempMenu = menuBar->addMenu(tr("&File"));
        QAction *openAction = tempMenu->addAction(tr("&Open"));
+       QAction *removeAction = tempMenu->addAction(tr("&Remove"));
+       removeAction->setEnabled(false);
        QAction *quitAction = tempMenu->addAction(tr("&Quit"));
        
        tempMenu = menuBar->addMenu(tr("&Settings"));
@@ -66,8 +69,10 @@ MainWindow::MainWindow():
        QAction *aboutAction = tempMenu->addAction(tr("&About"));
        QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
        
-               setMenuBar(menuBar);
+       setMenuBar(menuBar);
        connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
+       connect(removeAction, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
+       connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
        connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
        connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
        connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
@@ -79,9 +84,13 @@ MainWindow::MainWindow():
        /// @todo Exception handling
        dlView_ = new DownloadView(this);
        seedView_ = new SeedView(this);
-       
        tabWidget_->addTab(dlView_, tr("Downloads"));
        tabWidget_->addTab(seedView_, tr("Seeds"));
+       connect(dlView_, SIGNAL(itemSelectionChanged()), this,
+                                       SLOT(on_downloadItemSelectionChanged()));
+       connect(seedView_, SIGNAL(itemSelectionChanged()), this,
+                                       SLOT(on_seedItemSelectionChanged()));
+
        
        // Tab widget as central widget.
        setCentralWidget(tabWidget_);
@@ -89,13 +98,17 @@ MainWindow::MainWindow():
        // TOOLBAR
        QToolBar *toolBar = new QToolBar();
        toolBar->addAction(tr("Open"));
-       
+       removeAction = toolBar->addAction(tr("Remove"));
+       removeAction->setEnabled(false);
        addToolBar(Qt::TopToolBarArea, toolBar);
-       connect(toolBar, SIGNAL(actionTriggered(QAction*)), this, SLOT(handleToolBarAction(QAction*)));
        
+       connect(this, SIGNAL(itemSelected(bool)), removeAction,
+                                       SLOT(setEnabled(bool)));
+       connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
+                                       SLOT(handleToolBarAction(QAction*)));
        
-       connect(&btSession_, SIGNAL(alert(std::auto_ptr<TorrentAlert>)),
-                                        this, SLOT(on_torrentAlert(std::auto_ptr<TorrentAlert>)));
+       connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
+                                        this, SLOT(on_alert(std::auto_ptr<Alert>)));
 }
 
 
@@ -113,6 +126,11 @@ void MainWindow::on_openAction_clicked()
 
 }
 
+void MainWindow::on_removeAction_clicked()
+{
+       QTorrentHandle handle = dlView_->removeSelected();
+       btSession_.removeTorrent(handle);
+}
 
 void MainWindow::on_quitAction_clicked()
 {
@@ -141,11 +159,32 @@ void MainWindow::on_aboutQtAction_clicked()
 }
 
 
+void MainWindow::on_downloadItemSelectionChanged()
+{
+       qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
+       if (dlView_->currentItem() != NULL) {
+               emit(itemSelected(true));
+       } else {
+               emit(itemSelected(false));
+       }
+}
+
+void MainWindow::on_seedItemSelectionChanged()
+{
+       qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
+       if (seedView_->currentItem() != NULL) {
+               emit(itemSelected(true));
+       } else {
+               emit(itemSelected(false));
+       }
+}
+               
 void MainWindow::handleToolBarAction(QAction* action)
 {
        if (action->text() == "Open") {
                on_openAction_clicked();
-       } else {
+       } else if (action->text() == "Remove") {
+               on_removeAction_clicked();
        }
 }
 
@@ -166,13 +205,41 @@ void MainWindow::on_torrentFileSelected(const QString& file)
        // save_path is the only mandatory parameter, rest are optional.
        addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString()); 
        //addParams.storage_mode = libtorrent::storage_mode_allocate;
-       std::auto_ptr<QTorrentHandle> handlePtr = btSession_.addTorrent(addParams);
-       dlView_->newItem(handlePtr.get());
-       qDebug() << "Is valid: " << handlePtr->isValid();
+       QTorrentHandle handle = btSession_.addTorrent(addParams);
+       dlView_->newItem(handle);
+//     torrentHandles_.push_back(handlePtr);
+       qDebug() << "Is valid: " << handle.isValid();
+}
+
+
+void MainWindow::on_alert(std::auto_ptr<Alert> al)
+{
+       if (al.get() != NULL) {
+               qDebug() 
+                               << "MainWindow::on_torrentAlert(): " 
+                               << QString::fromStdString(al->message());
+               
+               TorrentAlert *torrentAlert 
+                               = dynamic_cast<TorrentAlert*> (al.get());
+               if (torrentAlert) {
+                       dlView_->updateItem(QTorrentHandle(torrentAlert->handle));
+               }
+       
+       }
+       
+       
+       
 }
 
-void MainWindow::on_torrentAlert(std::auto_ptr<TorrentAlert> al)
+/*
+bool MainWindow::IsNewTorrent(std::auto_ptr<QTorrentHandle> handlePtr)
 {
-       if (al.get() != NULL)
-               qDebug() << "MainWindow::on_torrentAlert(): " << QString::fromStdString(al->message());
-}
\ No newline at end of file
+       for (unsigned i = 0; i < torrentHandles_.size(); ++i) {
+               if (torrentHandles_.at(i).get() == handlePtr.get()) {
+                       return false;
+               } else {
+                       return true;
+               }
+       }
+}
+*/
index 03fe781..275e1dd 100644 (file)
@@ -41,16 +41,22 @@ class MainWindow : public QMainWindow {
 
     ~MainWindow();
                
+       signals:
+               void itemSelected(bool enabled);
+               
        public slots:
        private slots:
                void on_openAction_clicked();
+               void on_removeAction_clicked();
                void on_quitAction_clicked();
                void on_preferencesAction_clicked();
                void on_aboutAction_clicked();
                void on_aboutQtAction_clicked();
+               void on_downloadItemSelectionChanged();
+               void on_seedItemSelectionChanged();
                void handleToolBarAction(QAction* action);
                void on_torrentFileSelected(const QString& file);
-               void on_torrentAlert(std::auto_ptr<TorrentAlert> al);
+               void on_alert(std::auto_ptr<Alert> al);
                
        private:
                QTabWidget *tabWidget_;
@@ -59,9 +65,12 @@ class MainWindow : public QMainWindow {
                PreferencesDialog *preferencesDialog_;
                QSettings settings_;
                
+               //std::vector< std::auto_ptr<QTorrentHandle> const > torrentHandles_;
+               
                QBittorrentSession btSession_;
                
 
+               //bool IsNewTorrent(std::auto_ptr<QTorrentHandle> handlePtr);
 };
 
 #endif
index 6c2513d..2765e14 100644 (file)
@@ -17,6 +17,7 @@
  *   Free Software Foundation, Inc.,                                       *
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
+#include <QDebug>
 #include "SeedView.h"
 
 SeedView::SeedView(QWidget* parent):
@@ -25,6 +26,9 @@ SeedView::SeedView(QWidget* parent):
 {
        setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
        setHeaderItem(SeedViewItem::getHeaderItem());
+       
+       connect(this, SIGNAL(itemPressed( QTreeWidgetItem*, int)),
+                                       this, SLOT(on_itemPressed(QTreeWidgetItem*, int)));
 }
 
 
@@ -32,4 +36,8 @@ SeedView::~SeedView()
 {
 }
 
+void SeedView::on_itemPressed(QTreeWidgetItem * item, int column)
+{
+       qDebug() << "SeedView::on_itemPressed() " << item << "," << column;
+}
 
index 1410aa8..9b05e99 100644 (file)
@@ -40,6 +40,9 @@ Q_OBJECT
                void newItem(QTorrentHandle const* handle);
                void updateItem(QTorrentHandle const* handle);
 
+       private slots:
+               void on_itemPressed(QTreeWidgetItem *item, int column);
+               
        private:
                // Maps torrent to SeedView item.
                // Key: SHA1 info hash of torrent. Data: View item corresponding to torrent.