From b00c42d2b71e46b466d5796900f5bf5cb6017063 Mon Sep 17 00:00:00 2001 From: lvaatamoinen Date: Wed, 21 Oct 2009 13:48:13 +0000 Subject: [PATCH] -Added QTorrentHandle and started torrent adding implementation git-svn-id: file:///svnroot/qtrapids/trunk@11 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda --- qtrapids.kdevelop | 2 +- src/engine/AlertWaiterThread.cpp | 24 ++++++++++------ src/engine/AlertWaiterThread.h | 13 +++++---- src/engine/QBittorrentSession.cpp | 29 ++++++++++++++++++- src/engine/QBittorrentSession.h | 36 ++++++++++++++++++++---- src/engine/QTorrentHandle.cpp | 32 +++++++++++++++++++++ src/engine/QTorrentHandle.h | 44 +++++++++++++++++++++++++++++ src/engine/engine.pro | 13 +++++++-- src/gui/MainWindow.cpp | 56 +++++++++++++++++++++++++++---------- src/gui/MainWindow.h | 11 +++++--- src/gui/gui.pro | 4 ++- 11 files changed, 221 insertions(+), 43 deletions(-) create mode 100644 src/engine/QTorrentHandle.cpp create mode 100644 src/engine/QTorrentHandle.h diff --git a/qtrapids.kdevelop b/qtrapids.kdevelop index b476a37..7e98567 100644 --- a/qtrapids.kdevelop +++ b/qtrapids.kdevelop @@ -170,7 +170,7 @@ - src/gui + src true diff --git a/src/engine/AlertWaiterThread.cpp b/src/engine/AlertWaiterThread.cpp index 7a2f90f..9b6e741 100644 --- a/src/engine/AlertWaiterThread.cpp +++ b/src/engine/AlertWaiterThread.cpp @@ -18,11 +18,20 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #include #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() -// { -// } - - diff --git a/src/engine/AlertWaiterThread.h b/src/engine/AlertWaiterThread.h index 5ba3bf6..0a2209a 100644 --- a/src/engine/AlertWaiterThread.h +++ b/src/engine/AlertWaiterThread.h @@ -21,6 +21,8 @@ #define ALERTWAITERTHREAD_H #include +#include "QBittorrentSession.h" + /** @author Lassi Väätämöinen @@ -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 diff --git a/src/engine/QBittorrentSession.cpp b/src/engine/QBittorrentSession.cpp index 34198c8..4cefcec 100644 --- a/src/engine/QBittorrentSession.cpp +++ b/src/engine/QBittorrentSession.cpp @@ -17,10 +17,21 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + +#include + +#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); +} + + diff --git a/src/engine/QBittorrentSession.h b/src/engine/QBittorrentSession.h index 19de9d7..82665b4 100644 --- a/src/engine/QBittorrentSession.h +++ b/src/engine/QBittorrentSession.h @@ -20,20 +20,44 @@ #ifndef QBITTORRENTSESSION_H #define QBITTORRENTSESSION_H +#include + +#include + +#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 */ -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 index 0000000..2b87b78 --- /dev/null +++ b/src/engine/QTorrentHandle.cpp @@ -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 index 0000000..f4238c8 --- /dev/null +++ b/src/engine/QTorrentHandle.h @@ -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 + + + +/** + @author Lassi Väätämöinen +*/ +class QTorrentHandle +{ + + public: + QTorrentHandle(libtorrent::torrent_handle handle); + ~QTorrentHandle(); + + + private: + QTorrentHandle(); // Prevent default construct. + libtorrent::torrent_handle torrentHandle; + +}; + +#endif diff --git a/src/engine/engine.pro b/src/engine/engine.pro index 2344e7e..96bd57e 100644 --- a/src/engine/engine.pro +++ b/src/engine/engine.pro @@ -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 + diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 9615959..f5e6036 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -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(); + */ } diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 4bae579..1b1155e 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -22,12 +22,12 @@ #include +#include "QBittorrentSession.h" class QTabWidget; class DownloadView; class SeedView; -class AlertWaiterThread; /** @author Lassi Väätämöinen @@ -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 diff --git a/src/gui/gui.pro b/src/gui/gui.pro index 24598ed..daa701a 100644 --- a/src/gui/gui.pro +++ b/src/gui/gui.pro @@ -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 + -- 1.7.9.5