- Added daemon start functionality
authorlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Thu, 21 Jan 2010 13:27:05 +0000 (13:27 +0000)
committerlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Thu, 21 Jan 2010 13:27:05 +0000 (13:27 +0000)
- TODO: Consider removing forced torrent recheck when issuing Start Daemon

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

dbus/com.ixonos.qtrapids.xml
src/client/MainWindow.cpp
src/client/MainWindow.h
src/server/AlertWaiterThread.cpp
src/server/AlertWaiterThread.hpp
src/server/TorrentSession.cpp
src/server/TorrentSession.hpp

index ac00a19..d2e4750 100644 (file)
@@ -46,6 +46,9 @@
                <signal name="terminate">
                </signal>
                
+               <signal name="sessionTerminated">
+               </signal>
+               
   </interface>
 
 </node>
index b589dc3..36d3081 100644 (file)
@@ -58,6 +58,8 @@ MainWindow::MainWindow() :
                dlView_(NULL),
                seedView_(NULL),
                searchWidget_(NULL),
+               startDaemonAction_(NULL),
+               stopDaemonAction_(NULL),
                preferencesDialog_(NULL),
                settings_(QCoreApplication::organizationName()
                          , QCoreApplication::applicationName()),
@@ -77,9 +79,12 @@ MainWindow::MainWindow() :
        QAction *openAction = tempMenu->addAction(tr("&Open"));
        QAction *removeAction = tempMenu->addAction(tr("&Remove"));
        removeAction->setEnabled(false);
-       QAction *quitAction = tempMenu->addAction(tr("&Quit"));
-       QAction *stopDaemonAction = tempMenu->addAction(tr("Stop &daemon"));
        
+       startDaemonAction_ = tempMenu->addAction(tr("S&tart daemon"));
+       stopDaemonAction_ = tempMenu->addAction(tr("Sto&p daemon"));
+       startDaemonAction_->setEnabled(false);
+       
+       QAction *quitAction = tempMenu->addAction(tr("&Quit"));
 
        tempMenu = menuBar->addMenu(tr("&View"));
        QAction *columnsAction = tempMenu->addAction(tr("&Columns"));
@@ -96,7 +101,8 @@ MainWindow::MainWindow() :
        connect(removeAction, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
        connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
        connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
-       connect(stopDaemonAction, SIGNAL(triggered()), this, SLOT(on_stopDaemonAction_clicked()));
+       connect(startDaemonAction_, SIGNAL(triggered()), this, SLOT(on_startDaemonAction_clicked()));
+       connect(stopDaemonAction_, SIGNAL(triggered()), this, SLOT(on_stopDaemonAction_clicked()));
        connect(columnsAction, SIGNAL(triggered()), this, SLOT(on_columnsAction_clicked()));
        connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
        connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
@@ -135,7 +141,9 @@ MainWindow::MainWindow() :
 
        connect(&server_, SIGNAL(alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)), 
                                        this, SLOT(on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)));
-                                       
+       
+       connect(&server_, SIGNAL(sessionTerminated()), this, SLOT(on_serverTerminated()));
+       
 //     connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
 //             this, SLOT(on_alert(std::auto_ptr<Alert>)));
 
@@ -346,12 +354,28 @@ void MainWindow::on_quitAction_clicked()
 }
 
 
+void MainWindow::on_startDaemonAction_clicked()
+{
+       server_.getState();
+       /// @todo create signal that signals server startup and 
+       /// enable controls in the handler slot
+       stopDaemonAction_->setEnabled(true);
+       startDaemonAction_->setEnabled(false);
+}
+
+
 void MainWindow::on_stopDaemonAction_clicked()
 {
        server_.terminateSession();
 }
 
 
+void MainWindow::on_serverTerminated()
+{
+       stopDaemonAction_->setEnabled(false);
+       startDaemonAction_->setEnabled(true);
+}
+
 void MainWindow::on_columnsAction_clicked()
 {      
        ColumnSelectorDialog *dialog = new ColumnSelectorDialog(dlView_);
index b84a655..d569caf 100644 (file)
@@ -83,7 +83,9 @@ private slots:
        void on_openAction_clicked();
        void on_removeAction_clicked();
        void on_quitAction_clicked();
+       void on_startDaemonAction_clicked();
        void on_stopDaemonAction_clicked();
+       void on_serverTerminated();
        void on_columnsAction_clicked();
        void on_preferencesAction_clicked();
        void on_aboutAction_clicked();
@@ -95,6 +97,7 @@ private slots:
        void on_torrentFileSelected(const QString& file);
        void on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t);
 
+       
 private:
        void LoadPlugins();
        void StartTorrentFromBufferData(char const* data, int size);
@@ -104,11 +107,13 @@ private:
        DownloadView *dlView_;
        SeedView *seedView_;
        QWidget *searchWidget_;
+       QAction *startDaemonAction_, *stopDaemonAction_;
        PreferencesDialog *preferencesDialog_;
        QSettings settings_;
        QList<QDir> pluginDirs_;
        QStringList pluginFileNames_;
 
+
        //std::vector< std::auto_ptr<QTorrentHandle> const > torrentHandles_;
 
        QtRapidsServer server_;
index 77e7767..6c73330 100644 (file)
@@ -33,7 +33,9 @@ const libtorrent::time_duration ALERT_WAIT_TIMEOUT
 
 AlertWaiterThread::AlertWaiterThread(session_t *session, QObject* parent) :
                QThread(parent),
-               btSession_(session)
+               btSession_(session),
+               running_(false),
+               alertMutex_()
 {
 }
 
@@ -58,7 +60,8 @@ void AlertWaiterThread::allAlerts(bool enable)
 void AlertWaiterThread::run()
 {
        alert_t const *alertTemp = NULL;
-       while (true) {
+       running_ = true;
+       while (running_) {
                // wait_for_alert() call blocks. Returns libtorrent alert.
                // Returns NULL, if no alerts in timeout period.
                alertTemp = btSession_->wait_for_alert(ALERT_WAIT_TIMEOUT);
@@ -68,4 +71,13 @@ void AlertWaiterThread::run()
        }
 }
 
+
+void AlertWaiterThread::stop()
+{
+       alertMutex_.lock();
+       running_ = false;
+       alertMutex_.unlock();
 }
+
+
+} // namespace qtrapids
index 3008638..acf86c9 100644 (file)
@@ -21,6 +21,7 @@
 #define ALERTWAITERTHREAD_H
 
 #include <QThread>
+#include <QMutex>
 #include "TorrentSession.hpp"
 
 namespace qtrapids
@@ -41,13 +42,16 @@ public:
        void allAlerts(bool enable = true);
 
        virtual void run(); // Overridden from QThread
-
+       
+       virtual void stop();
+       
 signals:
        void alert();
 
 private:
        session_t *const btSession_;
-
+       bool running_;
+       QMutex alertMutex_;
 };
 
 } // namespace qtrapids
index b877470..1065a6e 100644 (file)
@@ -38,6 +38,15 @@ TorrentSession::TorrentSession(QObject *parent, QSettings *settings)
        
 }
 
+
+TorrentSession::~TorrentSession()
+{
+       alertWaiter_->stop();
+       alertWaiter_->wait();
+       emit sessionTerminated();
+}
+
+
 void TorrentSession::loadState()
 {
        TorrentDownloadInfo info;
@@ -251,6 +260,7 @@ qtrapids::ParamsMap_t TorrentSession::getOptions()
 void TorrentSession::terminateSession()
 {
        qDebug() << "Terminate called";
+       // Emiting terminate() here causes the server application to quit in main()
        emit terminate();
 }
 
index 66b6b54..254e02e 100644 (file)
@@ -51,7 +51,8 @@ class TorrentSession : public QObject
 
 public:
        TorrentSession(QObject *parent, QSettings *);
-
+       virtual ~TorrentSession();
+       
 public slots:
        void getState();
        void addTorrent(const QString &path, const QString &save_path
@@ -64,6 +65,7 @@ public slots:
 signals:
        void alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info);
        void terminate();
+       void sessionTerminated();
        
 private slots:
        void on_alert();