- Added buildconf.pri to hold the master-level build options.
[qtrapids] / src / gui / MainWindow.cpp
index f5e6036..a3262e1 100644 (file)
 
 #include "DownloadView.h"
 #include "SeedView.h"
+#include "PreferencesDialog.h"
 
 #include "MainWindow.h"
 
-
 const QString ABOUT_TEXT 
        = QString(QObject::trUtf8("QtRapids, a simple BitTorrent client based on"
                "\nQt and Libtorrent."
                "\n\nURL: http://qtrapids.garage.maemo.org/"
-               "\n\nAuthor(s):\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com"
+               "\n\nAuthors:\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com"
+               "\nDenis Zalievsky, denis.zalewsky@ixonos.com"
                "\n\nIxonos Plc, Finland\n"));
 
 
@@ -47,6 +48,9 @@ MainWindow::MainWindow():
        tabWidget_(NULL),
        dlView_(NULL),
        seedView_(NULL),
+       preferencesDialog_(NULL),
+       settings_(),
+//     torrentHandles_(),
        btSession_()                                    
 {
        // MENUBAR 
@@ -55,40 +59,57 @@ MainWindow::MainWindow():
        
        tempMenu = menuBar->addMenu(tr("&File"));
        QAction *openAction = tempMenu->addAction(tr("&Open"));
+       QAction *removeAction = tempMenu->addAction(tr("&Remove"));
+       removeAction->setEnabled(false);
        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"));
        
-               setMenuBar(menuBar);
+       setMenuBar(menuBar);
        connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
+       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(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();
        
        /// @todo Exception handling
        dlView_ = new DownloadView(this);
        seedView_ = new SeedView(this);
-       
        tabWidget_->addTab(dlView_, tr("Downloads"));
        tabWidget_->addTab(seedView_, tr("Seeds"));
+       connect(dlView_, SIGNAL(itemSelectionChanged()), this,
+                                       SLOT(on_downloadItemSelectionChanged()));
+       connect(seedView_, SIGNAL(itemSelectionChanged()), this,
+                                       SLOT(on_seedItemSelectionChanged()));
+
        
        // Tab widget as central widget.
        setCentralWidget(tabWidget_);
        
-       
        // TOOLBAR
        QToolBar *toolBar = new QToolBar();
        toolBar->addAction(tr("Open"));
-       
+       removeAction = toolBar->addAction(tr("Remove"));
+       removeAction->setEnabled(false);
        addToolBar(Qt::TopToolBarArea, toolBar);
-       connect(toolBar, SIGNAL(actionTriggered(QAction*)), this, SLOT(handleToolBarAction(QAction*)));
        
+       connect(this, SIGNAL(itemSelected(bool)), removeAction,
+                                       SLOT(setEnabled(bool)));
+       connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
+                                       SLOT(handleToolBarAction(QAction*)));
+       
+       connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
+                                        this, SLOT(on_alert(std::auto_ptr<Alert>)));
 }
 
 
@@ -106,12 +127,26 @@ void MainWindow::on_openAction_clicked()
 
 }
 
+void MainWindow::on_removeAction_clicked()
+{
+       QTorrentHandle handle = dlView_->removeSelected();
+       btSession_.removeTorrent(handle);
+}
 
 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()
 {
@@ -125,29 +160,99 @@ void MainWindow::on_aboutQtAction_clicked()
 }
 
 
+void MainWindow::on_downloadItemSelectionChanged()
+{
+#ifdef QTRAPIDS_DEBUG
+       qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
+#endif
+       if (dlView_->currentItem() != NULL) {
+               emit(itemSelected(true));
+       } else {
+               emit(itemSelected(false));
+       }
+}
+
+void MainWindow::on_seedItemSelectionChanged()
+{
+#ifdef QTRAPIDS_DEBUG
+       qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
+#endif
+       if (seedView_->currentItem() != NULL) {
+               emit(itemSelected(true));
+       } else {
+               emit(itemSelected(false));
+       }
+}
+               
 void MainWindow::handleToolBarAction(QAction* action)
 {
        if (action->text() == "Open") {
                on_openAction_clicked();
-       } else {
+       } else if (action->text() == "Remove") {
+               on_removeAction_clicked();
        }
 }
 
 void MainWindow::on_torrentFileSelected(const QString& file)
 {
+#ifdef QTRAPIDS_DEBUG
        qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
+#endif
        // 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.
+       boost::intrusive_ptr<libtorrent::torrent_info> tiTmp = 
+                       new libtorrent::torrent_info(boost::filesystem::path(file.toStdString()));
+       addParams.ti = tiTmp;
+       // 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();
-       */
+       QTorrentHandle handle = btSession_.addTorrent(addParams);
+       dlView_->newItem(handle);
+//     torrentHandles_.push_back(handlePtr);
+#ifdef QTRAPIDS_DEBUG
+       qDebug() << "Is valid: " << handle.isValid();
+#endif
+}
+
+
+void MainWindow::on_alert(std::auto_ptr<Alert> al)
+{
+
+       
+       if (al.get() != NULL) {
+//             qDebug() 
+//                             << "MainWindow::on_torrentAlert(): " 
+//                             << QString::fromStdString(al->message());
+
+               TorrentAlert *torrentAlert 
+                               = dynamic_cast<TorrentAlert*> (al.get());
+
+               if (torrentAlert) {
+                       QTorrentHandle torrentHandle = QTorrentHandle(torrentAlert->handle);
+                       dlView_->updateItem(QTorrentHandle(torrentAlert->handle));
+               }
+       
+       }
+       
+       
+       
+}
+
+/*
+bool MainWindow::IsNewTorrent(std::auto_ptr<QTorrentHandle> handlePtr)
+{
+       for (unsigned i = 0; i < torrentHandles_.size(); ++i) {
+               if (torrentHandles_.at(i).get() == handlePtr.get()) {
+                       return false;
+               } else {
+                       return true;
+               }
+       }
 }
+*/