- Torrent can be now opened and download starts
authorlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Fri, 23 Oct 2009 14:28:23 +0000 (14:28 +0000)
committerlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Fri, 23 Oct 2009 14:28:23 +0000 (14:28 +0000)
- New opened torrent appends to download list view

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

qtrapids.kdevelop
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 cc9549c..da398ee 100644 (file)
@@ -14,8 +14,8 @@
     <projectname>qtrapids</projectname>
     <projectdirectory>.</projectdirectory>
     <absoluteprojectpath>false</absoluteprojectpath>
-    <description/>
-    <defaultencoding/>
+    <description></description>
+    <defaultencoding></defaultencoding>
   </general>
   <kdevfileview>
     <groups>
@@ -67,7 +67,7 @@
       <includePaths>.;</includePaths>
     </codecompletion>
     <creategettersetter>
-      <prefixGet/>
+      <prefixGet></prefixGet>
       <prefixSet>set</prefixSet>
       <prefixVariable>m_,_</prefixVariable>
       <parameterName>theValue</parameterName>
   </kdevdoctreeview>
   <kdevdebugger>
     <general>
-      <dbgshell/>
+      <dbgshell></dbgshell>
       <breakpoints/>
       <programargs/>
       <gdbpath>/usr/bin/gdb</gdbpath>
       <breakonloadinglibs>true</breakonloadinglibs>
       <separatetty>false</separatetty>
       <floatingtoolbar>false</floatingtoolbar>
-      <configGdbScript/>
-      <runShellScript/>
-      <runGdbScript/>
+      <configGdbScript></configGdbScript>
+      <runShellScript></runShellScript>
+      <runGdbScript></runGdbScript>
       <raiseGDBOnStart>false</raiseGDBOnStart>
     </general>
     <display>
   <kdevtrollproject>
     <run>
       <mainprogram>/home/vaatala/Projects/qtrapids/trunk/bin/qtrapids</mainprogram>
-      <programargs/>
+      <programargs></programargs>
       <directoryradio>executable</directoryradio>
-      <globaldebugarguments/>
+      <globaldebugarguments></globaldebugarguments>
       <globalcwd>/home/vaatala/Projects/qtrapids/trunk/bin</globalcwd>
       <useglobalprogram>true</useglobalprogram>
       <terminal>false</terminal>
         <envvar value="." name="LD_LIBRARY_PATH" />
       </envvars>
       <runarguments>
-        <qtrapids/>
+        <qtrapids></qtrapids>
       </runarguments>
       <cwd>
         <qtrapids>/home/vaatala/Projects/qtrapids/trunk</qtrapids>
       </cwd>
       <debugarguments>
-        <qtrapids/>
+        <qtrapids></qtrapids>
       </debugarguments>
     </run>
     <general>
-      <activedir>src</activedir>
+      <activedir>src/gui</activedir>
     </general>
     <make>
       <abortonerror>true</abortonerror>
       <runmultiplejobs>false</runmultiplejobs>
       <numberofjobs>1</numberofjobs>
       <dontact>false</dontact>
-      <makebin/>
+      <makebin></makebin>
       <prio>0</prio>
       <envvars/>
     </make>
index e0d6c5c..dd94f95 100644 (file)
@@ -41,12 +41,12 @@ QBittorrentSession::~QBittorrentSession()
 }
 
 
-QTorrentHandle QBittorrentSession::addTorrent(AddTorrentParams const& params)
+std::auto_ptr<QTorrentHandle> 
+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<QTorrentHandle> 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<TorrentAlert> alertPtr = btSession_.pop_alert();
        emit alert(alertPtr);
 }
index a6b6ac2..d941a92 100644 (file)
@@ -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<QTorrentHandle> addTorrent(AddTorrentParams const& params);
                
        signals:
                void alert(std::auto_ptr<TorrentAlert> al);
index 1be0bf1..c69162c 100644 (file)
@@ -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<size_t> (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<size_t> (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
index 14d969b..c102086 100644 (file)
 #ifndef QTORRENTHANDLE_H
 #define QTORRENTHANDLE_H
 
+#include <QtGlobal>
+
 #include <libtorrent/torrent_handle.hpp>
 
 
+typedef libtorrent::torrent_status TorrentStatus;
+typedef libtorrent::torrent_info TorrentInfo;
 
 /**
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
@@ -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
index a589e39..6912b5e 100644 (file)
@@ -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)
+{
+}
+
 
index f3d43c5..80b5228 100644 (file)
 #ifndef DOWNLOADVIEW_H
 #define DOWNLOADVIEW_H
 
+#include <map>
+
 #include <QTreeWidget>
 
+#include "QBittorrentSession.h"
+
+class DownloadViewItem;
+
 /**
+       @class DownloadView
+       @brief A view showing all downloaded torrents
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
 */
 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<Sha1Hash, DownloadViewItem*> items_;
+               
 };
 
 
index 972e89a..1f1db3a 100644 (file)
@@ -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<TorrentAlert>)),
+                                        this, SLOT(on_torrentAlert(std::auto_ptr<TorrentAlert>)));
 }
 
 
@@ -162,7 +163,16 @@ void MainWindow::on_torrentFileSelected(const QString& file)
        boost::intrusive_ptr<libtorrent::torrent_info> 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<QTorrentHandle> handlePtr = btSession_.addTorrent(addParams);
+       dlView_->newItem(handlePtr.get());
+       qDebug() << "Is valid: " << handlePtr->isValid();
 }
+
+void MainWindow::on_torrentAlert(std::auto_ptr<TorrentAlert> al)
+{
+       if (al.get() != NULL)
+               qDebug() << "MainWindow::on_torrentAlert(): " << QString::fromStdString(al->message());
+}
\ No newline at end of file
index e64dfc1..03fe781 100644 (file)
@@ -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<TorrentAlert> al);
                
        private:
                QTabWidget *tabWidget_;
index e15621e..6c2513d 100644 (file)
@@ -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());
index 37d61e0..1410aa8 100644 (file)
 
 #include <QTreeWidget>
 
+#include "QBittorrentSession.h"
+
+class SeedViewItem;
+
 /**
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
 */
@@ -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<Sha1Hash, SeedViewItem*> items_;
+
                        // Name
                // Size
                // Status