From 45e692ff47151854cc149c514d3a53285f0f195a Mon Sep 17 00:00:00 2001 From: lvaatamoinen Date: Fri, 23 Oct 2009 14:28:23 +0000 Subject: [PATCH] - Torrent can be now opened and download starts - New opened torrent appends to download list view git-svn-id: file:///svnroot/qtrapids/trunk@13 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda --- qtrapids.kdevelop | 26 ++++----- src/engine/QBittorrentSession.cpp | 11 ++-- src/engine/QBittorrentSession.h | 3 +- src/engine/QTorrentHandle.cpp | 111 +++++++++++++++++++++++++++++++++++++ src/engine/QTorrentHandle.h | 25 +++++++++ src/gui/DownloadView.cpp | 26 ++++++++- src/gui/DownloadView.h | 16 +++++- src/gui/MainWindow.cpp | 18 ++++-- src/gui/MainWindow.h | 1 + src/gui/SeedView.cpp | 3 +- src/gui/SeedView.h | 11 ++++ 11 files changed, 224 insertions(+), 27 deletions(-) diff --git a/qtrapids.kdevelop b/qtrapids.kdevelop index cc9549c..da398ee 100644 --- a/qtrapids.kdevelop +++ b/qtrapids.kdevelop @@ -14,8 +14,8 @@ qtrapids . false - - + + @@ -67,7 +67,7 @@ .; - + set m_,_ theValue @@ -125,16 +125,16 @@ - + /usr/bin/gdb true false false - - - + + + false @@ -147,9 +147,9 @@ /home/vaatala/Projects/qtrapids/trunk/bin/qtrapids - + executable - + /home/vaatala/Projects/qtrapids/trunk/bin true false @@ -160,24 +160,24 @@ - + /home/vaatala/Projects/qtrapids/trunk - + - src + src/gui true false 1 false - + 0 diff --git a/src/engine/QBittorrentSession.cpp b/src/engine/QBittorrentSession.cpp index e0d6c5c..dd94f95 100644 --- a/src/engine/QBittorrentSession.cpp +++ b/src/engine/QBittorrentSession.cpp @@ -41,12 +41,12 @@ QBittorrentSession::~QBittorrentSession() } -QTorrentHandle QBittorrentSession::addTorrent(AddTorrentParams const& params) +std::auto_ptr +QBittorrentSession::addTorrent(AddTorrentParams const& params) { // Delegate to Libtorrent and return QTorrentHandle. - QTorrentHandle handle(btSession_.add_torrent(params)); - qDebug() << "Is valid: " << handle.isValid(); - return handle; + std::auto_ptr handlePtr(new QTorrentHandle(btSession_.add_torrent(params))); + return handlePtr; } @@ -55,11 +55,10 @@ void QBittorrentSession::on_alert(TorrentAlert const *al) //NOTE: al parameter not necessarily needed here, as we pop_alert() now! { - qDebug() << "QBittorrentSession:on_alert(" << al << ")"; + //qDebug() << "QBittorrentSession:on_alert(" << al << ")"; // if (al) // qDebug() << "on_alert():" << QString::fromStdString(al->message()); - std::auto_ptr alertPtr = btSession_.pop_alert(); emit alert(alertPtr); } diff --git a/src/engine/QBittorrentSession.h b/src/engine/QBittorrentSession.h index a6b6ac2..d941a92 100644 --- a/src/engine/QBittorrentSession.h +++ b/src/engine/QBittorrentSession.h @@ -35,6 +35,7 @@ class AlertWaiterThread; typedef libtorrent::session TorrentSession; typedef libtorrent::add_torrent_params AddTorrentParams; typedef libtorrent::alert TorrentAlert; +typedef libtorrent::sha1_hash Sha1Hash; /** @@ -49,7 +50,7 @@ class QBittorrentSession : public QObject { ~QBittorrentSession(); /// @brief Add torrent to session. - QTorrentHandle addTorrent(AddTorrentParams const& params); + std::auto_ptr addTorrent(AddTorrentParams const& params); signals: void alert(std::auto_ptr al); diff --git a/src/engine/QTorrentHandle.cpp b/src/engine/QTorrentHandle.cpp index 1be0bf1..c69162c 100644 --- a/src/engine/QTorrentHandle.cpp +++ b/src/engine/QTorrentHandle.cpp @@ -31,9 +31,120 @@ QTorrentHandle::~QTorrentHandle() { } + +TorrentStatus QTorrentHandle::status() const +{ + return torrentHandle_.status(); +} + + +TorrentInfo const& QTorrentHandle::getTorrentInfo() const +{ + return torrentHandle_.get_torrent_info(); +} + + bool QTorrentHandle::isValid() const { return torrentHandle_.is_valid(); } +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(); + return static_cast (info.total_size()); +} + +QString QTorrentHandle::state() const +{ + return GetStatusString(status()); +} + +float QTorrentHandle::progress() const +{ + TorrentStatus statusTmp = status(); + return statusTmp.progress; +} + +float QTorrentHandle::uploadRate() const +{ + TorrentStatus statusTmp = status(); + return statusTmp.upload_rate; +} + + +float QTorrentHandle::downloadRate() const +{ + TorrentStatus statusTmp = status(); + 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(); + size_t ratio; + if (statusTmp.total_payload_download == 0) { + ratio = 0; + } else { + ratio = static_cast (statusTmp.total_payload_upload / statusTmp.total_payload_download); + } + + return ratio; +} + + +QString QTorrentHandle::GetStatusString(TorrentStatus const& status) const +{ + switch (status.state) { + case TorrentStatus::queued_for_checking : + return "Queued"; + case TorrentStatus::checking_files : + return "Checking"; + case TorrentStatus::downloading_metadata : + return "DL meta"; + case TorrentStatus::downloading : + return "DL"; + case TorrentStatus::finished : + return "Finished"; + case TorrentStatus::seeding : + return "Seeding"; + case TorrentStatus::allocating : + return "Allocating"; + default: + return "N/A"; + } +} + + \ No newline at end of file diff --git a/src/engine/QTorrentHandle.h b/src/engine/QTorrentHandle.h index 14d969b..c102086 100644 --- a/src/engine/QTorrentHandle.h +++ b/src/engine/QTorrentHandle.h @@ -20,9 +20,13 @@ #ifndef QTORRENTHANDLE_H #define QTORRENTHANDLE_H +#include + #include +typedef libtorrent::torrent_status TorrentStatus; +typedef libtorrent::torrent_info TorrentInfo; /** @author Lassi Väätämöinen @@ -34,12 +38,33 @@ class QTorrentHandle QTorrentHandle(libtorrent::torrent_handle handle); ~QTorrentHandle(); + + TorrentStatus status() const; + + TorrentInfo const& getTorrentInfo() const; + bool isValid() const; + + QString name() const; + size_t getTotalSize() const; + QString state() const; + float progress() const; + float uploadRate() const; + float downloadRate() const; + qint32 numSeeds() const; + qint32 numLeeches() const; + qint32 ratio() const; + + + bool operator==(QTorrentHandle const& h); + bool operator<(QTorrentHandle const& h); private: QTorrentHandle(); // Prevent default construct. libtorrent::torrent_handle torrentHandle_; + // Private functions. + QString GetStatusString(TorrentStatus const& status) const; }; #endif diff --git a/src/gui/DownloadView.cpp b/src/gui/DownloadView.cpp index a589e39..6912b5e 100644 --- a/src/gui/DownloadView.cpp +++ b/src/gui/DownloadView.cpp @@ -19,7 +19,9 @@ ***************************************************************************/ #include "DownloadView.h" -DownloadView::DownloadView(QWidget* parent): QTreeWidget(parent) +DownloadView::DownloadView(QWidget* parent) : + QTreeWidget(parent), + items_() { setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list) setHeaderItem(DownloadViewItem::getHeaderItem()); @@ -30,4 +32,26 @@ DownloadView::~DownloadView() { } +void DownloadView::newItem(QTorrentHandle const* 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()) + << "ETA" ); + + addTopLevelItem(item); + +} + +void DownloadView::updateItem(QTorrentHandle const* handle) +{ +} + diff --git a/src/gui/DownloadView.h b/src/gui/DownloadView.h index f3d43c5..80b5228 100644 --- a/src/gui/DownloadView.h +++ b/src/gui/DownloadView.h @@ -20,9 +20,17 @@ #ifndef DOWNLOADVIEW_H #define DOWNLOADVIEW_H +#include + #include +#include "QBittorrentSession.h" + +class DownloadViewItem; + /** + @class DownloadView + @brief A view showing all downloaded torrents @author Lassi Väätämöinen */ class DownloadView : public QTreeWidget @@ -34,8 +42,14 @@ Q_OBJECT ~DownloadView(); + void newItem(QTorrentHandle const* handle); + void updateItem(QTorrentHandle const* handle); + private: - + // Maps torrent to downloadview item. + // Key: SHA1 info hash of torrent. Data: View item corresponding to torrent. + std::map items_; + }; diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 972e89a..1f1db3a 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -33,7 +33,6 @@ #include "MainWindow.h" - const QString ABOUT_TEXT = QString(QObject::trUtf8("QtRapids, a simple BitTorrent client based on" "\nQt and Libtorrent." @@ -87,7 +86,6 @@ MainWindow::MainWindow(): // Tab widget as central widget. setCentralWidget(tabWidget_); - // TOOLBAR QToolBar *toolBar = new QToolBar(); toolBar->addAction(tr("Open")); @@ -95,6 +93,9 @@ MainWindow::MainWindow(): addToolBar(Qt::TopToolBarArea, toolBar); connect(toolBar, SIGNAL(actionTriggered(QAction*)), this, SLOT(handleToolBarAction(QAction*))); + + connect(&btSession_, SIGNAL(alert(std::auto_ptr)), + this, SLOT(on_torrentAlert(std::auto_ptr))); } @@ -162,7 +163,16 @@ void MainWindow::on_torrentFileSelected(const QString& file) boost::intrusive_ptr tiTmp = new libtorrent::torrent_info(boost::filesystem::path(file.toStdString())); addParams.ti = tiTmp; - addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString()); // The only mandatory parameter, rest are optional. + // 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; - btSession_.addTorrent(addParams); + std::auto_ptr handlePtr = btSession_.addTorrent(addParams); + dlView_->newItem(handlePtr.get()); + qDebug() << "Is valid: " << handlePtr->isValid(); } + +void MainWindow::on_torrentAlert(std::auto_ptr al) +{ + if (al.get() != NULL) + qDebug() << "MainWindow::on_torrentAlert(): " << QString::fromStdString(al->message()); +} \ No newline at end of file diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index e64dfc1..03fe781 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -50,6 +50,7 @@ class MainWindow : public QMainWindow { void on_aboutQtAction_clicked(); void handleToolBarAction(QAction* action); void on_torrentFileSelected(const QString& file); + void on_torrentAlert(std::auto_ptr al); private: QTabWidget *tabWidget_; diff --git a/src/gui/SeedView.cpp b/src/gui/SeedView.cpp index e15621e..6c2513d 100644 --- a/src/gui/SeedView.cpp +++ b/src/gui/SeedView.cpp @@ -20,7 +20,8 @@ #include "SeedView.h" SeedView::SeedView(QWidget* parent): - QTreeWidget(parent) + QTreeWidget(parent), + items_() { setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list) setHeaderItem(SeedViewItem::getHeaderItem()); diff --git a/src/gui/SeedView.h b/src/gui/SeedView.h index 37d61e0..1410aa8 100644 --- a/src/gui/SeedView.h +++ b/src/gui/SeedView.h @@ -22,6 +22,10 @@ #include +#include "QBittorrentSession.h" + +class SeedViewItem; + /** @author Lassi Väätämöinen */ @@ -32,8 +36,15 @@ Q_OBJECT SeedView(QWidget* parent); ~SeedView(); + + void newItem(QTorrentHandle const* handle); + void updateItem(QTorrentHandle const* handle); private: + // Maps torrent to SeedView item. + // Key: SHA1 info hash of torrent. Data: View item corresponding to torrent. + std::map items_; + // Name // Size // Status -- 1.7.9.5