-Added QTorrentHandle and started torrent adding implementation
authorlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Wed, 21 Oct 2009 13:48:13 +0000 (13:48 +0000)
committerlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Wed, 21 Oct 2009 13:48:13 +0000 (13:48 +0000)
git-svn-id: file:///svnroot/qtrapids/trunk@11 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda

qtrapids.kdevelop
src/engine/AlertWaiterThread.cpp
src/engine/AlertWaiterThread.h
src/engine/QBittorrentSession.cpp
src/engine/QBittorrentSession.h
src/engine/QTorrentHandle.cpp [new file with mode: 0644]
src/engine/QTorrentHandle.h [new file with mode: 0644]
src/engine/engine.pro
src/gui/MainWindow.cpp
src/gui/MainWindow.h
src/gui/gui.pro

index b476a37..7e98567 100644 (file)
       </debugarguments>
     </run>
     <general>
-      <activedir>src/gui</activedir>
+      <activedir>src</activedir>
     </general>
     <make>
       <abortonerror>true</abortonerror>
index 7a2f90f..9b6e741 100644 (file)
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+
 #include <QDebug>
 
 #include "AlertWaiterThread.h"
 
-AlertWaiterThread::AlertWaiterThread(QObject* parent): QThread(parent)
+// Constants:
+// Timeout for waiting alerts
+const libtorrent::time_duration ALERT_WAIT_TIMEOUT 
+               = libtorrent::time_duration(libtorrent::seconds(15));
+
+
+AlertWaiterThread::AlertWaiterThread(TorrentSession *const session, QObject* parent) : 
+               QThread(parent),
+               btSession_(session)
 {
 }
 
@@ -34,18 +43,15 @@ AlertWaiterThread::~AlertWaiterThread()
 
 void AlertWaiterThread::run()
 {
+       TorrentAlert const *alertTemp = NULL;
        while (true)
        {
                qDebug() << "AlertWaiter running";
-               emit alert();
-               sleep(2);
+               // wait_for_alert() returns libtorrent alert. 
+               // Returns NULL, if no alerts in timeout period.
+               alertTemp = btSession_->wait_for_alert(ALERT_WAIT_TIMEOUT);
+               emit alert(alertTemp);
        }
 }
 
 
-// =================== SIGNALS =======================
-// AlertWaiterThread::alert()
-// {
-// }
-
-
index 5ba3bf6..0a2209a 100644 (file)
@@ -21,6 +21,8 @@
 #define ALERTWAITERTHREAD_H
 
 #include <QThread>
+#include "QBittorrentSession.h"
+
 
 /**
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
@@ -30,17 +32,18 @@ class AlertWaiterThread : public QThread
        Q_OBJECT
        
        public:
-               AlertWaiterThread(QObject* parent = 0);
+               AlertWaiterThread(TorrentSession *const session, QObject *parent = 0);
 
-    ~AlertWaiterThread();
+    virtual ~AlertWaiterThread();
 
-               void run(); // Overridden from QThread
+               virtual void run(); // Overridden from QThread
                
        signals:
-               void alert();
+               void alert(TorrentAlert const *alert);
                
        private:
-               
+               TorrentSession *const btSession_;
+
 };
 
 #endif
index 34198c8..4cefcec 100644 (file)
  *   Free Software Foundation, Inc.,                                       *
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
+
+#include <QDebug>
+
+#include "AlertWaiterThread.h"
 #include "QBittorrentSession.h"
 
-QBittorrentSession::QBittorrentSession()
+
+QBittorrentSession::QBittorrentSession(QObject *parent):
+               QObject(parent),
+               btSession_(),
+               alertWaiter_(NULL)
 {
+       alertWaiter_ = new AlertWaiterThread(&btSession_, this);
+       connect(alertWaiter_, SIGNAL(alert(TorrentAlert const*)), this, SLOT(on_alert(TorrentAlert const*)));
+       alertWaiter_->start();
 }
 
 
@@ -29,3 +40,19 @@ QBittorrentSession::~QBittorrentSession()
 }
 
 
+QTorrentHandle QBittorrentSession::addTorrent(AddTorrentParams const& params)
+{
+       // Delegate to Libtorrent and return QTorrentHandle.
+       QTorrentHandle handle(btSession_.add_torrent(params));
+       return handle;
+}
+
+
+// ========================== SLOTS ==============================
+void QBittorrentSession::on_alert(TorrentAlert const *al)
+{
+       qDebug() << "QBittorrentSession:on_alert(" << al << ")";
+       emit alert(al);
+}
+
+
index 19de9d7..82665b4 100644 (file)
 #ifndef QBITTORRENTSESSION_H
 #define QBITTORRENTSESSION_H
 
+#include <QObject>
+
+#include <libtorrent/session.hpp>
+
+#include "QTorrentHandle.h"
+
+
+// Forward declarations and typedefs
+class AlertWaiterThread;
+typedef libtorrent::session TorrentSession;
+typedef libtorrent::add_torrent_params AddTorrentParams;
+typedef libtorrent::alert TorrentAlert;
+
+
 /**
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
 */
-class QBittorrentSession {
-
+class QBittorrentSession : public QObject {
+       Q_OBJECT
+//             class   BitTorrentSession;
+                       
        public:
-    QBittorrentSession();
-
+               QBittorrentSession(QObject *parent = 0);
     ~QBittorrentSession();
-       public slots:
+               
+               /// @brief Add torrent to session.
+               QTorrentHandle addTorrent(AddTorrentParams const& params);
+               
+       signals:
+               void alert(TorrentAlert const *al);
+               
        private slots:
+               void on_alert(TorrentAlert const *al);
+               
        private:
+               TorrentSession btSession_;
+               AlertWaiterThread *alertWaiter_;
                
-
 };
 
 #endif
diff --git a/src/engine/QTorrentHandle.cpp b/src/engine/QTorrentHandle.cpp
new file mode 100644 (file)
index 0000000..2b87b78
--- /dev/null
@@ -0,0 +1,32 @@
+/***************************************************************************
+ *   Copyright (C) 2009 by Lassi Väätämöinen   *
+ *   lassi.vaatamoinen@ixonos.com   *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#include "QTorrentHandle.h"
+
+QTorrentHandle::QTorrentHandle(libtorrent::torrent_handle handle) : 
+               torrentHandle(handle)
+{
+}
+
+
+QTorrentHandle::~QTorrentHandle()
+{
+}
+
+
diff --git a/src/engine/QTorrentHandle.h b/src/engine/QTorrentHandle.h
new file mode 100644 (file)
index 0000000..f4238c8
--- /dev/null
@@ -0,0 +1,44 @@
+/***************************************************************************
+ *   Copyright (C) 2009 by Lassi Väätämöinen   *
+ *   lassi.vaatamoinen@ixonos.com   *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#ifndef QTORRENTHANDLE_H
+#define QTORRENTHANDLE_H
+
+#include <libtorrent/torrent_handle.hpp>
+
+
+
+/**
+       @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
+*/
+class QTorrentHandle 
+{
+       
+       public:
+               QTorrentHandle(libtorrent::torrent_handle handle);
+    ~QTorrentHandle();
+               
+               
+       private:
+               QTorrentHandle(); // Prevent default construct.
+               libtorrent::torrent_handle torrentHandle;
+               
+};
+
+#endif
index 2344e7e..96bd57e 100644 (file)
@@ -6,9 +6,13 @@ CONFIG += dll \
 
 VERSION = 0.1
 
-HEADERS += AlertWaiterThread.h
+HEADERS += AlertWaiterThread.h \
+ QBittorrentSession.h \
+ QTorrentHandle.h
 
-SOURCES += AlertWaiterThread.cpp
+SOURCES += AlertWaiterThread.cpp \
+ QBittorrentSession.cpp \
+ QTorrentHandle.cpp
 
 QT -= gui
 
@@ -18,3 +22,8 @@ TARGET = qtbittorrent
 
 DESTDIR = ../../bin
 
+LIBS += -ltorrent-rasterbar \
+  -lboost_filesystem-mt \
+  -lboost_date_time-mt \
+  -lboost_thread-mt
+
index 9615959..f5e6036 100644 (file)
@@ -29,7 +29,6 @@
 
 #include "DownloadView.h"
 #include "SeedView.h"
-#include "AlertWaiterThread.h"
 
 #include "MainWindow.h"
 
@@ -42,26 +41,29 @@ const QString ABOUT_TEXT
                "\n\nIxonos Plc, Finland\n"));
 
 
-
+// Consturctor
 MainWindow::MainWindow():
        QMainWindow(), // Superclass
        tabWidget_(NULL),
        dlView_(NULL),
-       seedView_(NULL)
+       seedView_(NULL),
+       btSession_()                                    
 {
-       
        // MENUBAR 
        QMenuBar *menuBar = new QMenuBar();
        QMenu *tempMenu = NULL;
        
        tempMenu = menuBar->addMenu(tr("&File"));
-       tempMenu->addAction(tr("&Open"));
+       QAction *openAction = tempMenu->addAction(tr("&Open"));
+       QAction *quitAction = tempMenu->addAction(tr("&Quit"));
        
        tempMenu = menuBar->addMenu(tr("&Help"));
        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(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
        connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
        connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
        
@@ -87,9 +89,6 @@ MainWindow::MainWindow():
        addToolBar(Qt::TopToolBarArea, toolBar);
        connect(toolBar, SIGNAL(actionTriggered(QAction*)), this, SLOT(handleToolBarAction(QAction*)));
        
-       alertWaiter_ = new AlertWaiterThread(this);
-       connect(alertWaiter_, SIGNAL(alert()), this, SLOT(on_alert()));
-       alertWaiter_->start();
 }
 
 
@@ -97,12 +96,29 @@ MainWindow::~MainWindow()
 {
 }
 
+// =========================== SLOTS =================================
+void MainWindow::on_openAction_clicked()
+{
+       QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
+       dialog->setFileMode(QFileDialog::ExistingFile);
+       connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
+       dialog->show();
+
+}
+
+
+void MainWindow::on_quitAction_clicked()
+{
+       close();
+}
+
 
 void MainWindow::on_aboutAction_clicked()
 {
        QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT); 
 }
                
+               
 void MainWindow::on_aboutQtAction_clicked()
 {
        QMessageBox::aboutQt (this, tr("About Qt"));
@@ -112,14 +128,26 @@ void MainWindow::on_aboutQtAction_clicked()
 void MainWindow::handleToolBarAction(QAction* action)
 {
        if (action->text() == "Open") {
-               QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
-               dialog->setFileMode(QFileDialog::ExistingFile);
-               dialog->show();
+               on_openAction_clicked();
        } else {
        }
 }
 
-void MainWindow::on_alert()
+void MainWindow::on_torrentFileSelected(const QString& file)
 {
-       qDebug() << "MainWindow: got alert()";
+       qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
+       // Torrent filename empty, do nothing.
+       if (file == "") {
+               return;
+       }
+       
+       // Otherwise add torrent
+       /*      
+       // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
+       AddTorrentParams addParams;
+       addParams.ti = torrent_info(boost::filesystem::path const& filename);
+       addParams.save_path = "/home/vaatala/Downloads"; // The only mandatory parameter, rest are optional.
+       //addParams.storage_mode = libtorrent::storage_mode_allocate;
+       btSession_.addTorrent();
+       */
 }
index 4bae579..1b1155e 100644 (file)
 
 #include <QMainWindow>
 
+#include "QBittorrentSession.h"
 
 class QTabWidget;
 class DownloadView;
 class SeedView;
 
-class AlertWaiterThread;
 
 /**
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
@@ -42,18 +42,21 @@ class MainWindow : public QMainWindow {
                
        public slots:
        private slots:
+               void on_openAction_clicked();
+               void on_quitAction_clicked();
                void on_aboutAction_clicked();
                void on_aboutQtAction_clicked();
                void handleToolBarAction(QAction* action);
-               
-               void on_alert();
+               void on_torrentFileSelected(const QString& file);
                
        private:
                QTabWidget *tabWidget_;
                DownloadView *dlView_;
                SeedView *seedView_;
                
-               AlertWaiterThread *alertWaiter_;
+               QBittorrentSession btSession_;
+               
+
 };
 
 #endif
index 24598ed..daa701a 100644 (file)
@@ -15,7 +15,6 @@ CONFIG += debug \
  qtestlib
 
 
-TARGETDEPS += ../engine/liblibqtbittorrent.so
 
 DESTDIR = ../../bin
 
@@ -27,3 +26,6 @@ LIBS += -L../../bin \
   -L../engine
 
 QMAKE_LFLAGS_DEBUG += -L.
+TARGETDEPS += ../engine/liblibqtbittorrent.so \
+  ../../bin/libqtbittorrent.so
+