From a287bad35d31bc51adaad80dea33f7573b688eaf Mon Sep 17 00:00:00 2001 From: lvaatamoinen Date: Thu, 22 Oct 2009 13:36:53 +0000 Subject: [PATCH] - Added PreferencesDialog with settings saving and reading. - Torrent can now be added to torrent session git-svn-id: file:///svnroot/qtrapids/trunk@12 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda --- qtrapids.kdevelop | 20 +++--- src/engine/AlertWaiterThread.cpp | 18 ++++- src/engine/AlertWaiterThread.h | 2 + src/engine/QBittorrentSession.cpp | 13 +++- src/engine/QBittorrentSession.h | 5 +- src/engine/QTorrentHandle.cpp | 9 ++- src/engine/QTorrentHandle.h | 3 +- src/gui/MainWindow.cpp | 27 +++++-- src/gui/MainWindow.h | 6 +- src/gui/PreferencesDialog.cpp | 143 +++++++++++++++++++++++++++++++++++++ src/gui/PreferencesDialog.h | 59 +++++++++++++++ src/gui/gui.pro | 6 +- src/gui/main.cpp | 5 +- 13 files changed, 288 insertions(+), 28 deletions(-) create mode 100644 src/gui/PreferencesDialog.cpp create mode 100644 src/gui/PreferencesDialog.h diff --git a/qtrapids.kdevelop b/qtrapids.kdevelop index 7e98567..cc9549c 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 @@ -177,7 +177,7 @@ false 1 false - + 0 diff --git a/src/engine/AlertWaiterThread.cpp b/src/engine/AlertWaiterThread.cpp index 9b6e741..b5d6e95 100644 --- a/src/engine/AlertWaiterThread.cpp +++ b/src/engine/AlertWaiterThread.cpp @@ -41,17 +41,29 @@ AlertWaiterThread::~AlertWaiterThread() } +void AlertWaiterThread::allAlerts(bool enable) +{ + // If all enabled, set all alert cateogries: + if (enable) { + btSession_->set_alert_mask(libtorrent::alert::all_categories); + } else { + // Otherwise set to default, which is only error notifications. + btSession_->set_alert_mask(libtorrent::alert::error_notification); + } +} + + void AlertWaiterThread::run() { TorrentAlert const *alertTemp = NULL; while (true) { qDebug() << "AlertWaiter running"; - // wait_for_alert() returns libtorrent alert. + // wait_for_alert() call blocks. Returns libtorrent alert. // Returns NULL, if no alerts in timeout period. alertTemp = btSession_->wait_for_alert(ALERT_WAIT_TIMEOUT); emit alert(alertTemp); + // 2000 us = 2ms. Gives main thread time to handle alert signal. + usleep(2000); } } - - diff --git a/src/engine/AlertWaiterThread.h b/src/engine/AlertWaiterThread.h index 0a2209a..6ab719e 100644 --- a/src/engine/AlertWaiterThread.h +++ b/src/engine/AlertWaiterThread.h @@ -36,6 +36,8 @@ class AlertWaiterThread : public QThread virtual ~AlertWaiterThread(); + void allAlerts(bool enable = true); + virtual void run(); // Overridden from QThread signals: diff --git a/src/engine/QBittorrentSession.cpp b/src/engine/QBittorrentSession.cpp index 4cefcec..e0d6c5c 100644 --- a/src/engine/QBittorrentSession.cpp +++ b/src/engine/QBittorrentSession.cpp @@ -30,6 +30,7 @@ QBittorrentSession::QBittorrentSession(QObject *parent): alertWaiter_(NULL) { alertWaiter_ = new AlertWaiterThread(&btSession_, this); + alertWaiter_->allAlerts(); connect(alertWaiter_, SIGNAL(alert(TorrentAlert const*)), this, SLOT(on_alert(TorrentAlert const*))); alertWaiter_->start(); } @@ -44,15 +45,23 @@ 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; } // ========================== SLOTS ============================== -void QBittorrentSession::on_alert(TorrentAlert const *al) +void QBittorrentSession::on_alert(TorrentAlert const *al) + //NOTE: al parameter not necessarily needed here, as we pop_alert() now! { + qDebug() << "QBittorrentSession:on_alert(" << al << ")"; - emit 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 82665b4..a6b6ac2 100644 --- a/src/engine/QBittorrentSession.h +++ b/src/engine/QBittorrentSession.h @@ -20,9 +20,12 @@ #ifndef QBITTORRENTSESSION_H #define QBITTORRENTSESSION_H +#include + #include #include +#include #include "QTorrentHandle.h" @@ -49,7 +52,7 @@ class QBittorrentSession : public QObject { QTorrentHandle addTorrent(AddTorrentParams const& params); signals: - void alert(TorrentAlert const *al); + void alert(std::auto_ptr al); private slots: void on_alert(TorrentAlert const *al); diff --git a/src/engine/QTorrentHandle.cpp b/src/engine/QTorrentHandle.cpp index 2b87b78..1be0bf1 100644 --- a/src/engine/QTorrentHandle.cpp +++ b/src/engine/QTorrentHandle.cpp @@ -17,10 +17,12 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include + #include "QTorrentHandle.h" QTorrentHandle::QTorrentHandle(libtorrent::torrent_handle handle) : - torrentHandle(handle) + torrentHandle_(handle) { } @@ -29,4 +31,9 @@ QTorrentHandle::~QTorrentHandle() { } +bool QTorrentHandle::isValid() const +{ + return torrentHandle_.is_valid(); +} + diff --git a/src/engine/QTorrentHandle.h b/src/engine/QTorrentHandle.h index f4238c8..14d969b 100644 --- a/src/engine/QTorrentHandle.h +++ b/src/engine/QTorrentHandle.h @@ -34,10 +34,11 @@ class QTorrentHandle QTorrentHandle(libtorrent::torrent_handle handle); ~QTorrentHandle(); + bool isValid() const; private: QTorrentHandle(); // Prevent default construct. - libtorrent::torrent_handle torrentHandle; + libtorrent::torrent_handle torrentHandle_; }; diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index f5e6036..972e89a 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -29,6 +29,7 @@ #include "DownloadView.h" #include "SeedView.h" +#include "PreferencesDialog.h" #include "MainWindow.h" @@ -47,6 +48,8 @@ MainWindow::MainWindow(): tabWidget_(NULL), dlView_(NULL), seedView_(NULL), + preferencesDialog_(NULL), + settings_(), btSession_() { // MENUBAR @@ -57,6 +60,9 @@ MainWindow::MainWindow(): QAction *openAction = tempMenu->addAction(tr("&Open")); QAction *quitAction = tempMenu->addAction(tr("&Quit")); + tempMenu = menuBar->addMenu(tr("&Settings")); + QAction *preferencesAction = tempMenu->addAction(tr("&Preferences")); + tempMenu = menuBar->addMenu(tr("&Help")); QAction *aboutAction = tempMenu->addAction(tr("&About")); QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt")); @@ -64,10 +70,10 @@ MainWindow::MainWindow(): setMenuBar(menuBar); connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked())); 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())); connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked())); - // TABWIDGET (central widget) tabWidget_ = new QTabWidget(); @@ -112,6 +118,15 @@ void MainWindow::on_quitAction_clicked() close(); } +void MainWindow::on_preferencesAction_clicked() +{ + if (!preferencesDialog_) { + preferencesDialog_ = new PreferencesDialog(this); + } + preferencesDialog_->show(); + preferencesDialog_->raise(); + preferencesDialog_->activateWindow(); +} void MainWindow::on_aboutAction_clicked() { @@ -142,12 +157,12 @@ void MainWindow::on_torrentFileSelected(const QString& file) } // 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. + 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. //addParams.storage_mode = libtorrent::storage_mode_allocate; - btSession_.addTorrent(); - */ + btSession_.addTorrent(addParams); } diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 1b1155e..e64dfc1 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -21,13 +21,14 @@ #define MAINWINDOW_H #include +#include #include "QBittorrentSession.h" class QTabWidget; class DownloadView; class SeedView; - +class PreferencesDialog; /** @author Lassi Väätämöinen @@ -44,6 +45,7 @@ class MainWindow : public QMainWindow { private slots: void on_openAction_clicked(); void on_quitAction_clicked(); + void on_preferencesAction_clicked(); void on_aboutAction_clicked(); void on_aboutQtAction_clicked(); void handleToolBarAction(QAction* action); @@ -53,6 +55,8 @@ class MainWindow : public QMainWindow { QTabWidget *tabWidget_; DownloadView *dlView_; SeedView *seedView_; + PreferencesDialog *preferencesDialog_; + QSettings settings_; QBittorrentSession btSession_; diff --git a/src/gui/PreferencesDialog.cpp b/src/gui/PreferencesDialog.cpp new file mode 100644 index 0000000..9ee381b --- /dev/null +++ b/src/gui/PreferencesDialog.cpp @@ -0,0 +1,143 @@ +/*************************************************************************** + * 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 +#include +#include +#include +#include +#include +#include +#include + +#include "PreferencesDialog.h" + +PreferencesDialog::PreferencesDialog(QWidget* parent, Qt::WindowFlags f) : + QDialog(parent, f), // Superclass + dirLineEdit_(NULL), + dialogButtons_(NULL), + settings_() +{ + setWindowTitle("Preferences"); + + QBoxLayout *verticalBox = new QBoxLayout(QBoxLayout::TopToBottom); + QBoxLayout *horizontalBox1 = new QBoxLayout(QBoxLayout::LeftToRight); + setLayout(verticalBox); + verticalBox->addLayout(horizontalBox1); + + QLabel *dirLabel = new QLabel(tr("Download directory: ")); + dirLineEdit_ = new QLineEdit(this); + QPushButton *browseDirButton = new QPushButton(tr("Browse..")); + + horizontalBox1->addWidget(dirLabel); + horizontalBox1->addWidget(dirLineEdit_); + horizontalBox1->addWidget(browseDirButton); + + connect(browseDirButton, SIGNAL(clicked()), + this, SLOT(on_browseDirButtonClicked())); + + + dialogButtons_ = new QDialogButtonBox(this); + dialogButtons_->setStandardButtons(QDialogButtonBox::Ok + | QDialogButtonBox::Apply + | QDialogButtonBox::Cancel); + + verticalBox->addWidget(dialogButtons_); + + connect(dialogButtons_, SIGNAL(clicked(QAbstractButton*)), + this, SLOT(on_buttonClicked(QAbstractButton*))); + + // Set saved preference values to fields. + ReadSettings(); + +} + + +PreferencesDialog::~PreferencesDialog() +{ +} + +// ======================== SLOTS ======================== +void PreferencesDialog::on_browseDirButtonClicked() +{ + QFileDialog *dialog + = new QFileDialog(this, "Download directory", + QString(), tr("Torrent files (*.torrent)")); + + dialog->setFileMode(QFileDialog::Directory); + dialog->setOption(QFileDialog::ShowDirsOnly, true); + + connect(dialog, SIGNAL(fileSelected(const QString&)), + this, SLOT(on_downloadDirectorySelected(const QString&))); + + dialog->show(); +} + +void PreferencesDialog::on_buttonClicked(QAbstractButton* button) +{ + switch (dialogButtons_->buttonRole ( button ) ) + { + case QDialogButtonBox::AcceptRole : + qDebug() << "PreferencesDialog: OK"; + WriteSettings(); + close(); + break; + case QDialogButtonBox::ApplyRole : + qDebug() << "PreferencesDialog: APPLY"; + WriteSettings(); + break; + case QDialogButtonBox::RejectRole : + qDebug() << "PreferencesDialog: CANCEL"; + close(); + break; + default: + return; + } +} + +void PreferencesDialog::on_downloadDirectorySelected(const QString& directory) +{ + qDebug() << "PreferencesDialog::on_downloadDirectorySelected(): " << directory; + // Torrent filename empty, do nothing. + if (directory == "") + return; + + dirLineEdit_->insert(directory); + + /// @todo check that user has privileges to write to this directory. +} + + +// ========================= Private functions ========================== +void PreferencesDialog::WriteSettings() +{ + settings_.setValue("download/directory", dirLineEdit_->text()); + + // NOTE: We might need to call QSettigns::sync() here to instantly write settings. + // settings are written also by QSettings() destructor and by event loop at regular interval. +} + +void PreferencesDialog::ReadSettings() +{ + dirLineEdit_->insert(settings_.value("download/directory").toString()); + + // NOTE: We might need to call QSettigns::sync() here to instantly write settings. + // settings are written also by QSettings() destructor and by event loop at regular interval. +} + diff --git a/src/gui/PreferencesDialog.h b/src/gui/PreferencesDialog.h new file mode 100644 index 0000000..c368ea0 --- /dev/null +++ b/src/gui/PreferencesDialog.h @@ -0,0 +1,59 @@ +/*************************************************************************** + * 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 PREFERENCESDIALOG_H +#define PREFERENCESDIALOG_H + +#include +#include + + +class QAbstractButton; +class QLineEdit; +class QDialogButtonBox; + +/** + @author Lassi Väätämöinen +*/ +class PreferencesDialog : public QDialog +{ + + Q_OBJECT + + public: + PreferencesDialog(QWidget* parent = 0, Qt::WindowFlags f = 0); + + ~PreferencesDialog(); + + private slots: + void on_browseDirButtonClicked(); + void on_buttonClicked(QAbstractButton* button); + void on_downloadDirectorySelected(const QString& directory); + + private: + QLineEdit *dirLineEdit_; + QDialogButtonBox *dialogButtons_; + QSettings settings_; + + // Private functions: + void WriteSettings(); + void ReadSettings(); +}; + +#endif diff --git a/src/gui/gui.pro b/src/gui/gui.pro index daa701a..9c23ca1 100644 --- a/src/gui/gui.pro +++ b/src/gui/gui.pro @@ -1,10 +1,12 @@ SOURCES += DownloadView.cpp \ MainWindow.cpp \ SeedView.cpp \ - main.cpp + main.cpp \ + PreferencesDialog.cpp HEADERS += DownloadView.h \ MainWindow.h \ -SeedView.h +SeedView.h \ + PreferencesDialog.h TEMPLATE = app TARGET = qtrapids diff --git a/src/gui/main.cpp b/src/gui/main.cpp index 25426e8..2538271 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -27,8 +27,11 @@ int main(int argc, char *argv[]) { + QCoreApplication::setOrganizationName("Ixonos"); + QCoreApplication::setOrganizationDomain("ixonos.com"); + QCoreApplication::setApplicationName("QtRapids"); - // Q_INIT_RESOURCE(application); + // Q_INIT_RESOURCE(application); QApplication app(argc, argv); MainWindow *mainWindow = new MainWindow(); mainWindow->show(); -- 1.7.9.5