From 08828f51a534f17235944d186b8f1b0c69696e1b Mon Sep 17 00:00:00 2001 From: lvaatamoinen Date: Tue, 27 Oct 2009 11:22:46 +0000 Subject: [PATCH] - Torrent addign and removal functional - Additions to QTorrentHandle - TODO: Test torrent downloading. git-svn-id: file:///svnroot/qtrapids/trunk@14 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda --- src/engine/AlertWaiterThread.cpp | 2 +- src/engine/AlertWaiterThread.h | 2 +- src/engine/QBittorrentSession.cpp | 20 +++++--- src/engine/QBittorrentSession.h | 11 +++-- src/engine/QTorrentHandle.cpp | 37 ++++++++------ src/engine/QTorrentHandle.h | 10 ++-- src/gui/DownloadView.cpp | 93 +++++++++++++++++++++++++++++++---- src/gui/DownloadView.h | 10 ++-- src/gui/MainWindow.cpp | 97 +++++++++++++++++++++++++++++++------ src/gui/MainWindow.h | 11 ++++- src/gui/SeedView.cpp | 8 +++ src/gui/SeedView.h | 3 ++ 12 files changed, 245 insertions(+), 59 deletions(-) diff --git a/src/engine/AlertWaiterThread.cpp b/src/engine/AlertWaiterThread.cpp index b5d6e95..971967c 100644 --- a/src/engine/AlertWaiterThread.cpp +++ b/src/engine/AlertWaiterThread.cpp @@ -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"; diff --git a/src/engine/AlertWaiterThread.h b/src/engine/AlertWaiterThread.h index 6ab719e..ec46d7f 100644 --- a/src/engine/AlertWaiterThread.h +++ b/src/engine/AlertWaiterThread.h @@ -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_; diff --git a/src/engine/QBittorrentSession.cpp b/src/engine/QBittorrentSession.cpp index dd94f95..57054c3 100644 --- a/src/engine/QBittorrentSession.cpp +++ b/src/engine/QBittorrentSession.cpp @@ -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 QBittorrentSession::addTorrent(AddTorrentParams const& params) { // Delegate to Libtorrent and return QTorrentHandle. - std::auto_ptr handlePtr(new QTorrentHandle(btSession_.add_torrent(params))); - return handlePtr; + //std::auto_ptr 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 alertPtr = btSession_.pop_alert(); + std::auto_ptr alertPtr = btSession_.pop_alert(); emit alert(alertPtr); } + diff --git a/src/engine/QBittorrentSession.h b/src/engine/QBittorrentSession.h index d941a92..9cf5381 100644 --- a/src/engine/QBittorrentSession.h +++ b/src/engine/QBittorrentSession.h @@ -26,6 +26,7 @@ #include #include +#include #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 addTorrent(AddTorrentParams const& params); + QTorrentHandle addTorrent(AddTorrentParams const& params); + void removeTorrent(QTorrentHandle const& handle); signals: - void alert(std::auto_ptr al); + void alert(std::auto_ptr al); private slots: - void on_alert(TorrentAlert const *al); + void on_alert(Alert const *al); private: TorrentSession btSession_; diff --git a/src/engine/QTorrentHandle.cpp b/src/engine/QTorrentHandle.cpp index c69162c..3fb6ec0 100644 --- a/src/engine/QTorrentHandle.cpp +++ b/src/engine/QTorrentHandle.cpp @@ -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 diff --git a/src/engine/QTorrentHandle.h b/src/engine/QTorrentHandle.h index c102086..f03194d 100644 --- a/src/engine/QTorrentHandle.h +++ b/src/engine/QTorrentHandle.h @@ -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 @@ -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; diff --git a/src/gui/DownloadView.cpp b/src/gui/DownloadView.cpp index 6912b5e..2f91ff4 100644 --- a/src/gui/DownloadView.cpp +++ b/src/gui/DownloadView.cpp @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include #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 (currentItem()); + + std::map::iterator listIter + = items_.begin(); + std::map::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); + } + */ +} + diff --git a/src/gui/DownloadView.h b/src/gui/DownloadView.h index 80b5228..408a232 100644 --- a/src/gui/DownloadView.h +++ b/src/gui/DownloadView.h @@ -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 items_; + std::map items_; }; diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 1f1db3a..d45cc96 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -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)), - this, SLOT(on_torrentAlert(std::auto_ptr))); + connect(&btSession_, SIGNAL(alert(std::auto_ptr)), + this, SLOT(on_alert(std::auto_ptr))); } @@ -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 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 al) +{ + if (al.get() != NULL) { + qDebug() + << "MainWindow::on_torrentAlert(): " + << QString::fromStdString(al->message()); + + TorrentAlert *torrentAlert + = dynamic_cast (al.get()); + if (torrentAlert) { + dlView_->updateItem(QTorrentHandle(torrentAlert->handle)); + } + + } + + + } -void MainWindow::on_torrentAlert(std::auto_ptr al) +/* +bool MainWindow::IsNewTorrent(std::auto_ptr 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; + } + } +} +*/ diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 03fe781..275e1dd 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -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 al); + void on_alert(std::auto_ptr al); private: QTabWidget *tabWidget_; @@ -59,9 +65,12 @@ class MainWindow : public QMainWindow { PreferencesDialog *preferencesDialog_; QSettings settings_; + //std::vector< std::auto_ptr const > torrentHandles_; + QBittorrentSession btSession_; + //bool IsNewTorrent(std::auto_ptr handlePtr); }; #endif diff --git a/src/gui/SeedView.cpp b/src/gui/SeedView.cpp index 6c2513d..2765e14 100644 --- a/src/gui/SeedView.cpp +++ b/src/gui/SeedView.cpp @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include #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; +} diff --git a/src/gui/SeedView.h b/src/gui/SeedView.h index 1410aa8..9b05e99 100644 --- a/src/gui/SeedView.h +++ b/src/gui/SeedView.h @@ -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. -- 1.7.9.5