- Search plugin interface changed: enum constants to define widget types to allow...
[qtrapids] / src / gui / MainWindow.cpp
index 549fb8d..7eca355 100644 (file)
@@ -45,75 +45,75 @@ const QString ABOUT_TEXT
 
 // Consturctor
 MainWindow::MainWindow():
-        QMainWindow(), // Superclass
-        tabWidget_(NULL),
-        dlView_(NULL),
-        seedView_(NULL),
-        preferencesDialog_(NULL),
-        settings_(),
+               QMainWindow(), // Superclass
+               tabWidget_(NULL),
+               dlView_(NULL),
+               seedView_(NULL),
+               preferencesDialog_(NULL),
+               settings_(),
 //     torrentHandles_(),
-        btSession_()
+               btSession_()
 {
-    // MENUBAR
-    QMenuBar *menuBar = new QMenuBar();
-    QMenu *tempMenu = NULL;
-
-    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);
-    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();
-    tabWidget_->setTabsClosable(true);
-
-    /// @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(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>)));
-
-    LoadPlugins();
+       // MENUBAR
+       QMenuBar *menuBar = new QMenuBar();
+       QMenu *tempMenu = NULL;
+
+       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);
+       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();
+       tabWidget_->setTabsClosable(true);
+
+       /// @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(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>)));
+
+       LoadPlugins();
 }
 
 
@@ -122,40 +122,41 @@ MainWindow::~MainWindow()
 }
 
 // ===================== Implements PluginInterface =========================
-bool MainWindow::setGui(qtrapids::PluginInterface* from, QWidget* widget)
+bool MainWindow::setGui(QWidget* widget, PluginWidgetType type)
 {
 #ifdef QTRAPIDS_DEBUG
-    qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
+       qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
 #endif
-    tabWidget_->addTab(widget, tr("Search"));
-    return true;
+       tabWidget_->addTab(widget, tr("Search"));
+       return true;
 }
 
 /// @todo Add PluginInterface parameter check which plugin gives the widget to handle appropriately
-void MainWindow::addPluginWidget(qtrapids::PluginInterface* from, QWidget* widget)
+void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
 {
 #ifdef QTRAPIDS_DEBUG
-    qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
+       qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
 #endif
 
-    int index = tabWidget_->addTab(widget, tr("Test"));
-    tabWidget_->setCurrentIndex(index);
-    //layout_->addWidget(widget);
+       if (type == qtrapids::PluginHostInterface::TAB_PAGE) {
+               int index = tabWidget_->addTab(widget, tr("Test"));
+               tabWidget_->setCurrentIndex(index);
+               //layout_->addWidget(widget);
+       }
 }
-
-void MainWindow::addToolbar(qtrapids::PluginInterface* from, QWidget* widget)
+void MainWindow::addToolbar(QWidget* widget, PluginWidgetType type)
 {
 }
 
-void MainWindow::addToolItem(qtrapids::PluginInterface* from, QWidget* widget)
+void MainWindow::addToolItem(QWidget* widget, PluginWidgetType type)
 {
 }
 
-void MainWindow::addMenu(qtrapids::PluginInterface* from, QWidget* widget)
+void MainWindow::addMenu(QWidget* widget, PluginWidgetType type)
 {
 }
 
-void MainWindow::addMenuItem(qtrapids::PluginInterface* from, QWidget* widget)
+void MainWindow::addMenuItem(QWidget* widget, PluginWidgetType type)
 {
 }
 
@@ -163,209 +164,169 @@ void MainWindow::addMenuItem(qtrapids::PluginInterface* from, QWidget* widget)
 
 void MainWindow::LoadPlugins()
 {
-    /// @todo get plugin directory from settings or go through multiple diectories
-    /// Now we only check the application directory
-    pluginsDir_ = QDir(qApp->applicationDirPath());
-    pluginsDir_.cd("plugins");
-    QStringList nameFilters;
-    nameFilters << "*.so";
-
-    foreach (QString fileName, pluginsDir_.entryList(nameFilters, QDir::Files))
-    {
-        QPluginLoader pluginLoader(pluginsDir_.absoluteFilePath(fileName));
-
-        if (!QLibrary::isLibrary(fileName))
-        {
-            qDebug() << fileName << " not a library";
-        }
-
-        if (pluginLoader.load())
-        {
-            qDebug() << "Plugin loaded: "  << fileName;
-        }
-        else
-        {
-            qDebug() << "Plugin load failed: " << pluginLoader.errorString();
-        }
-
-        QObject *baseInstance = pluginLoader.instance();
-        if (!baseInstance)
-        {
-            qDebug() << "Base instance = NULL.";
-        }
-
-        qtrapids::PluginInterface *plugin = qobject_cast<qtrapids::PluginInterface*>(baseInstance);
-
-        if (!plugin)
-        {
-            qDebug() << "Cast failed.";
-        }
-        else
-        {
-            plugin->initialize(this);
-            pluginFileNames_ += fileName;
-        }
-
-//             QObject *plugin = pluginLoader.instance();
-//             if (plugin) {
-//                             populateMenus(plugin);
-//                             pluginFileNames += fileName;
-//             }
-
-    }
-
-    //pluginLoader_.setFileName("../libsearchplugin.so");
+       /// @todo get plugin directory from settings or go through multiple diectories
+       /// Now we only check the application directory
+       pluginsDir_ = QDir(qApp->applicationDirPath());
+       pluginsDir_.cd("plugins");
+       QStringList nameFilters;
+       nameFilters << "*.so";
+
+       foreach (QString fileName, pluginsDir_.entryList(nameFilters, QDir::Files)) {
+               QPluginLoader pluginLoader(pluginsDir_.absoluteFilePath(fileName));
+
+               if (!QLibrary::isLibrary(fileName)) {
+                       qDebug() << fileName << " not a library";
+               }
 
+               if (pluginLoader.load()) {
+                       qDebug() << "Plugin loaded: "  << fileName;
+               } else {
+                       qDebug() << "Plugin load failed: " << pluginLoader.errorString();
+               }
 
+               QObject *baseInstance = pluginLoader.instance();
+               if (!baseInstance) {
+                       qDebug() << "Base instance = NULL.";
+               }
 
+               qtrapids::PluginInterface *plugin = qobject_cast<qtrapids::PluginInterface*>(baseInstance);
 
+               if (!plugin) {
+                       qDebug() << "Cast failed.";
+               } else {
+                       plugin->initialize(this);
+                       pluginFileNames_ += fileName;
+               }
+       }
 }
 
 // =========================== 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();
+       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_removeAction_clicked()
 {
-    qtrapids::QTorrentHandle handle = dlView_->removeSelected();
-    btSession_.removeTorrent(handle);
+       qtrapids::QTorrentHandle handle = dlView_->removeSelected();
+       btSession_.removeTorrent(handle);
 }
 
 void MainWindow::on_quitAction_clicked()
 {
-    close();
+       close();
 }
 
 void MainWindow::on_preferencesAction_clicked()
 {
-    if (!preferencesDialog_)
-    {
-        preferencesDialog_ = new PreferencesDialog(this);
-    }
-    preferencesDialog_->show();
-    preferencesDialog_->raise();
-    preferencesDialog_->activateWindow();
+       if (!preferencesDialog_) {
+               preferencesDialog_ = new PreferencesDialog(this);
+       }
+       preferencesDialog_->show();
+       preferencesDialog_->raise();
+       preferencesDialog_->activateWindow();
 }
 
 void MainWindow::on_aboutAction_clicked()
 {
-    QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
+       QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
 }
 
 
 void MainWindow::on_aboutQtAction_clicked()
 {
-    QMessageBox::aboutQt (this, tr("About Qt"));
+       QMessageBox::aboutQt (this, tr("About Qt"));
 }
 
 
 void MainWindow::on_downloadItemSelectionChanged()
 {
 #ifdef QTRAPIDS_DEBUG
-    qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
+       qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
 #endif
-    if (dlView_->currentItem() != NULL)
-    {
-        emit(itemSelected(true));
-    }
-    else
-    {
-        emit(itemSelected(false));
-    }
+       if (dlView_->currentItem() != NULL) {
+               emit(itemSelected(true));
+       } else {
+               emit(itemSelected(false));
+       }
 }
 
 void MainWindow::on_seedItemSelectionChanged()
 {
 #ifdef QTRAPIDS_DEBUG
-    qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
+       qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
 #endif
-    if (seedView_->currentItem() != NULL)
-    {
-        emit(itemSelected(true));
-    }
-    else
-    {
-        emit(itemSelected(false));
-    }
+       if (seedView_->currentItem() != NULL) {
+               emit(itemSelected(true));
+       } else {
+               emit(itemSelected(false));
+       }
 }
 
 void MainWindow::handleToolBarAction(QAction* action)
 {
-    if (action->text() == "Open")
-    {
-        on_openAction_clicked();
-    }
-    else if (action->text() == "Remove")
-    {
-        on_removeAction_clicked();
-    }
+       if (action->text() == "Open") {
+               on_openAction_clicked();
+       } else if (action->text() == "Remove") {
+               on_removeAction_clicked();
+       }
 }
 
 void MainWindow::on_torrentFileSelected(const QString& file)
 {
 #ifdef QTRAPIDS_DEBUG
-    qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
+       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;
-    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;
-    qtrapids::QTorrentHandle handle = btSession_.addTorrent(addParams);
-    dlView_->newItem(handle);
+       // 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;
+       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;
+       qtrapids::QTorrentHandle handle = btSession_.addTorrent(addParams);
+       dlView_->newItem(handle);
 //     torrentHandles_.push_back(handlePtr);
 #ifdef QTRAPIDS_DEBUG
-    qDebug() << "Is valid: " << handle.isValid();
+       qDebug() << "Is valid: " << handle.isValid();
 #endif
 }
 
 
 void MainWindow::on_alert(std::auto_ptr<Alert> al)
 {
-
-
-    if (al.get() != NULL)
-    {
+       if (al.get() != NULL) {
 //             qDebug()
 //                             << "MainWindow::on_torrentAlert(): "
 //                             << QString::fromStdString(al->message());
 
-        TorrentAlert *torrentAlert
-        = dynamic_cast<TorrentAlert*> (al.get());
-
-        if (torrentAlert)
-        {
-            qtrapids::QTorrentHandle torrentHandle = qtrapids::QTorrentHandle(torrentAlert->handle);
-            dlView_->updateItem(qtrapids::QTorrentHandle(torrentAlert->handle));
-        }
-
-    }
-
+               TorrentAlert *torrentAlert
+               = dynamic_cast<TorrentAlert*> (al.get());
 
+               if (torrentAlert) {
+                       qtrapids::QTorrentHandle torrentHandle = qtrapids::QTorrentHandle(torrentAlert->handle);
+                       dlView_->updateItem(qtrapids::QTorrentHandle(torrentAlert->handle));
+               }
 
+       }
 }
 
 /*
 bool MainWindow::IsNewTorrent(std::auto_ptr<qtrapids::QTorrentHandle> handlePtr)
 {
        for (unsigned i = 0; i < torrentHandles_.size(); ++i) {
-               if (torrentHandles_.at(i).get() == handlePtr.get()) {
+    if (torrentHandles_.at(i).get() == handlePtr.get()) {
                        return false;
                } else {
                        return true;