- Added session-level upload/download-rate limit preferences
[qtrapids] / src / gui / MainWindow.cpp
index e233aee..6ab93c6 100644 (file)
@@ -51,6 +51,7 @@ MainWindow::MainWindow():
                tabWidget_(NULL),
                dlView_(NULL),
                seedView_(NULL),
+               searchWidget_(NULL),
                preferencesDialog_(NULL),
                settings_(),
                pluginDirs_(),
@@ -112,11 +113,13 @@ MainWindow::MainWindow():
                SLOT(setEnabled(bool)));
        connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
                SLOT(handleToolBarAction(QAction*)));
-
+       connect (tabWidget_, SIGNAL(tabCloseRequested(int)), this, SLOT(on_tabWidget_tabCloseRequested(int)));
+       
        connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
                this, SLOT(on_alert(std::auto_ptr<Alert>)));
 
        LoadPlugins();
+       RestoreSettings();
 }
 
 
@@ -125,16 +128,24 @@ MainWindow::~MainWindow()
 }
 
 // ===================== Implements PluginInterface =========================
-bool MainWindow::setGui(QWidget* widget, PluginWidgetType type)
+/// @todo add PluginInterface parameter to request plugin name 
+bool MainWindow::setGui(QWidget* widget, PluginWidgetType type, qtrapids::PluginInterface* plugin)
 {
 #ifdef QTRAPIDS_DEBUG
        qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
 #endif
+
+       if (plugin && plugin->identifier() == "SearchPlugin") {
+               searchWidget_ = widget;
+       } else {
+               return false;
+       }
+       
        tabWidget_->addTab(widget, tr("Search"));
        return true;
 }
 
-/// @todo Add PluginInterface parameter check which plugin gives the widget to handle appropriately
+/// @todo Add PluginInterface parameter to check which plugin gives the widget, to handle appropriately
 void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
 {
 #ifdef QTRAPIDS_DEBUG
@@ -142,7 +153,7 @@ void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
 #endif
 
        if (type == qtrapids::PluginHostInterface::TAB_PAGE) {
-               int index = tabWidget_->addTab(widget, tr("Test"));
+               int index = tabWidget_->addTab(widget, tr("Results"));
                tabWidget_->setCurrentIndex(index);
                //layout_->addWidget(widget);
        }
@@ -258,6 +269,12 @@ void MainWindow::LoadPlugins()
 }
 
 
+void MainWindow::RestoreSettings()
+{
+       btSession_.setUploadRateLimit(settings_.value("network/uploadRate").toInt());
+       btSession_.setUploadRateLimit(settings_.value("network/downloadRate").toInt());
+}
+
 
 // Opens torrent information from buffer data and adds torrent to session 
 void MainWindow::StartTorrentFromBufferData(char const* data, int size)
@@ -279,7 +296,7 @@ void MainWindow::StartTorrentFromBufferData(char const* data, int size)
 #endif
 }
 
-// =========================== SLOTS =================================
+// =========================== PRIVATE SLOTS =================================
 void MainWindow::on_openAction_clicked()
 {
        QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
@@ -294,21 +311,25 @@ void MainWindow::on_removeAction_clicked()
        btSession_.removeTorrent(handle);
 }
 
+
 void MainWindow::on_quitAction_clicked()
 {
        close();
 }
 
+
 void MainWindow::on_preferencesAction_clicked()
 {
        if (!preferencesDialog_) {
-               preferencesDialog_ = new PreferencesDialog(this);
+               preferencesDialog_ = new PreferencesDialog(this, NULL, &btSession_);
        }
+
        preferencesDialog_->show();
        preferencesDialog_->raise();
        preferencesDialog_->activateWindow();
 }
 
+
 void MainWindow::on_aboutAction_clicked()
 {
        QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
@@ -321,6 +342,22 @@ void MainWindow::on_aboutQtAction_clicked()
 }
 
 
+void MainWindow::on_tabWidget_tabCloseRequested(int index)
+{
+       
+       int searchWidgetIndex = tabWidget_->indexOf(searchWidget_);
+       
+       // Allow closing other tabs than the first two
+       // TODO The first two may well be closable, just add "show tabs" action for these in the menu
+       if (index != 0 && index != 1 && index != searchWidgetIndex) {
+               QWidget *remove = tabWidget_->widget(index);
+               tabWidget_->removeTab(index);
+               delete remove;
+               remove = NULL;
+       }
+}
+
+
 void MainWindow::on_downloadItemSelectionChanged()
 {
 #ifdef QTRAPIDS_DEBUG
@@ -333,6 +370,7 @@ void MainWindow::on_downloadItemSelectionChanged()
        }
 }
 
+
 void MainWindow::on_seedItemSelectionChanged()
 {
 #ifdef QTRAPIDS_DEBUG
@@ -345,6 +383,7 @@ void MainWindow::on_seedItemSelectionChanged()
        }
 }
 
+
 void MainWindow::handleToolBarAction(QAction* action)
 {
        if (action->text() == "Open") {
@@ -354,6 +393,7 @@ void MainWindow::handleToolBarAction(QAction* action)
        }
 }
 
+
 void MainWindow::on_torrentFileSelected(const QString& file)
 {
 #ifdef QTRAPIDS_DEBUG