From cced647c88d92e05fde31cce0cc6a53a268c1954 Mon Sep 17 00:00:00 2001 From: lvaatamoinen Date: Wed, 18 Nov 2009 10:06:02 +0000 Subject: [PATCH] - Plugin interface added to MainWindow - Search plugin dummy example git-svn-id: file:///svnroot/qtrapids/trunk@32 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda --- src/gui/MainWindow.cpp | 225 ++++++++++++++++++++--------- src/gui/MainWindow.h | 86 ++++++----- src/gui/gui.pro | 3 +- src/plugins/PluginInterface.h | 35 +++++ src/plugins/searchplugin/SearchPlugin.cpp | 81 +++++++++++ src/plugins/searchplugin/SearchPlugin.h | 19 +++ src/plugins/searchplugin/searchplugin.pro | 2 +- 7 files changed, 347 insertions(+), 104 deletions(-) diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 4d47618..4c8cc19 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -26,6 +26,8 @@ #include #include #include +#include + #include "DownloadView.h" #include "SeedView.h" @@ -33,14 +35,13 @@ #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\nAuthors:\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com" - "\nDenis Zalievsky, denis.zalewsky@ixonos.com" - "\n\nIxonos Plc, Finland\n")); - +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\nAuthors:\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com" + "\nDenis Zalevskiy, denis.zalewsky@ixonos.com" + "\n\nIxonos Plc, Finland\n")); // Consturctor MainWindow::MainWindow(): @@ -53,63 +54,66 @@ MainWindow::MainWindow(): // torrentHandles_(), 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(); - - /// @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)), - this, SLOT(on_alert(std::auto_ptr))); + // 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)), + this, SLOT(on_alert(std::auto_ptr))); + + LoadPlugins(); } @@ -117,6 +121,97 @@ MainWindow::~MainWindow() { } +// ===================== Implements PluginInterface ========================= +bool MainWindow::setGui(qtrapids::PluginInterface* from, QWidget* widget) +{ +#ifdef QTRAPIDS_DEBUG + qDebug() << "MainWindow::setGui():" << dlView_->currentItem(); +#endif + 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) +{ +#ifdef QTRAPIDS_DEBUG + qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem(); +#endif + + int index = tabWidget_->addTab(widget, tr("Test")); + tabWidget_->setCurrentIndex(index); + //layout_->addWidget(widget); +} + +void MainWindow::addToolbar(qtrapids::PluginInterface* from, QWidget* widget) +{ +} + +void MainWindow::addToolItem(qtrapids::PluginInterface* from, QWidget* widget) +{ +} + +void MainWindow::addMenu(qtrapids::PluginInterface* from, QWidget* widget) +{ +} + +void MainWindow::addMenuItem(qtrapids::PluginInterface* from, QWidget* widget) +{ +} + +//=========================== PRIVATE ================================ + +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(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"); + + + + +} + // =========================== SLOTS ================================= void MainWindow::on_openAction_clicked() { diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index f4d43b1..9ac916d 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -22,7 +22,10 @@ #include #include +#include +#include +#include "PluginInterface.h" #include "QBittorrentSession.h" class QTabWidget; @@ -33,45 +36,54 @@ class PreferencesDialog; /** @author Lassi Väätämöinen */ -class MainWindow : public QMainWindow +class MainWindow : public QMainWindow, public qtrapids::PluginHostInterface { - Q_OBJECT + Q_OBJECT + + public: + MainWindow(); -public: - MainWindow(); - - ~MainWindow(); - -signals: - void itemSelected(bool enabled); - -public slots: -private slots: - void on_openAction_clicked(); - void on_removeAction_clicked(); - void on_quitAction_clicked(); - void on_preferencesAction_clicked(); - void on_aboutAction_clicked(); - void on_aboutQtAction_clicked(); - void on_downloadItemSelectionChanged(); - void on_seedItemSelectionChanged(); - void handleToolBarAction(QAction* action); - void on_torrentFileSelected(const QString& file); - void on_alert(std::auto_ptr al); - -private: - QTabWidget *tabWidget_; - DownloadView *dlView_; - SeedView *seedView_; - PreferencesDialog *preferencesDialog_; - QSettings settings_; - - //std::vector< std::auto_ptr const > torrentHandles_; - - qtrapids::QBittorrentSession btSession_; - - - //bool IsNewTorrent(std::auto_ptr handlePtr); + virtual ~MainWindow(); + + // Implemented from PluginHostInterface + virtual bool setGui(qtrapids::PluginInterface* from, QWidget* widget); + virtual void addPluginWidget(qtrapids::PluginInterface* from, QWidget* widget); + virtual void addToolbar(qtrapids::PluginInterface* from, QWidget* widget); + virtual void addToolItem(qtrapids::PluginInterface* from, QWidget* widget); + virtual void addMenu(qtrapids::PluginInterface* from, QWidget* widget); + virtual void addMenuItem(qtrapids::PluginInterface* from, QWidget* widget); + + signals: + void itemSelected(bool enabled); + + public slots: + private slots: + void on_openAction_clicked(); + void on_removeAction_clicked(); + void on_quitAction_clicked(); + void on_preferencesAction_clicked(); + void on_aboutAction_clicked(); + void on_aboutQtAction_clicked(); + void on_downloadItemSelectionChanged(); + void on_seedItemSelectionChanged(); + void handleToolBarAction(QAction* action); + void on_torrentFileSelected(const QString& file); + void on_alert(std::auto_ptr al); + + private: + void LoadPlugins(); + + private: + QTabWidget *tabWidget_; + DownloadView *dlView_; + SeedView *seedView_; + PreferencesDialog *preferencesDialog_; + QSettings settings_; + QDir pluginsDir_; + QStringList pluginFileNames_; + + qtrapids::QBittorrentSession btSession_; + }; #endif diff --git a/src/gui/gui.pro b/src/gui/gui.pro index 668b387..3945159 100644 --- a/src/gui/gui.pro +++ b/src/gui/gui.pro @@ -19,7 +19,8 @@ CONFIG += qtestlib DESTDIR = ../../bin -INCLUDEPATH += ../engine +INCLUDEPATH += ../engine \ + ../plugins LIBS += -L../../bin \ -lqtbittorrent \ diff --git a/src/plugins/PluginInterface.h b/src/plugins/PluginInterface.h index dc7a0f7..0e56353 100644 --- a/src/plugins/PluginInterface.h +++ b/src/plugins/PluginInterface.h @@ -25,6 +25,40 @@ namespace qtrapids { +<<<<<<< .mine + + // Forward declaration because of co-dependency of classes. + class PluginInterface; + + /** @class PluginHostInterface + * @brief Defines interface for plugins to access the host application. + * A Host is an application that is extended by implementing Plugins, + * that implement the additional functionality + * @note Implementing plugin host should inherit QObject. + */ + class PluginHostInterface { + public: + + /// @brief Sets the plugin GUI element to host application + /// @note It is up to the host application to decide how to manage + /// and show the actual widget. + virtual bool setGui(PluginInterface* from, QWidget* widget) = 0; + + /// @brief Adds additional plugin wigdets to the host application. + /// This functio can be called by the plugin recursively, i.e. when GUI events occur + /// The host application must handle placing the additional widgets. + /// @todo Could we implement this using in a more manageable way, e.g. signal-slot? + virtual void addPluginWidget(PluginInterface* from, QWidget* widget) = 0; + virtual void addToolbar(PluginInterface* from, QWidget* widget) = 0; + virtual void addToolItem(PluginInterface* from, QWidget* widget) = 0; + virtual void addMenu(PluginInterface* from, QWidget* widget) = 0; + virtual void addMenuItem(PluginInterface* from, QWidget* widget) = 0; + }; +======= +>>>>>>> .r31 +<<<<<<< .mine + +======= /** @class PluginHostInterface * @brief Defines interface for plugins to access the host application. @@ -35,6 +69,7 @@ class PluginHostInterface : public QObject { public: +>>>>>>> .r31 /// @brief Sets the plugin GUI element to host application /// @note It is up to the host application to decide how to manage /// and show the actual widget. diff --git a/src/plugins/searchplugin/SearchPlugin.cpp b/src/plugins/searchplugin/SearchPlugin.cpp index f4ded6d..10165f0 100644 --- a/src/plugins/searchplugin/SearchPlugin.cpp +++ b/src/plugins/searchplugin/SearchPlugin.cpp @@ -1,3 +1,23 @@ +/*************************************************************************** + * 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 @@ -13,12 +33,73 @@ namespace qtrapids { +<<<<<<< .mine + SearchPlugin::SearchPlugin() : + comboBox_(NULL), searchLine_(NULL), searchButton_(NULL), host_(NULL) + { + // TODO: Parse engine descriptions. + // -Add engines to model + // -Show model in comboBox + + } + + void SearchPlugin::initialize(PluginHostInterface* host) + { + host_ = host; + + if (host_ != NULL) { + + QWidget *pluginWidget = new QWidget; + QVBoxLayout *vbox = new QVBoxLayout; + QHBoxLayout *hbox = new QHBoxLayout; + comboBox_ = new QComboBox; + searchLine_ = new QLineEdit; + searchButton_ = new QPushButton("Search"); + + hbox->addWidget(searchLine_); + hbox->addWidget(searchButton_); + vbox->addWidget(comboBox_); + vbox->addLayout(hbox); + pluginWidget->setLayout(vbox); + + connect(searchButton_, SIGNAL(clicked()), this, SLOT(on_searchButton_clicked())); + //connect(this, SIGNAL(searchResult(QWidget*)), this, SLOT(on_searchResult(QWidget*))); + + host_->setGui(this, pluginWidget); + } + } + + QWidget* SearchPlugin::getGui() + { + return NULL; + } + + void SearchPlugin::on_searchButton_clicked() + { + QUrl searchUrl(QString("http://www.google.fi/search?q=" + + searchLine_->text())); + qDebug() << searchUrl; + QWebView *result = new QWebView; + result->load(searchUrl); + + on_searchResult((QWidget*)result); + } + + void SearchPlugin::on_searchResult(QWidget* resultWidget) + { + qDebug() << "on_searchResult()"; + if (host_) { + host_->addPluginWidget(this, resultWidget); + } + } +======= SearchPlugin::SearchPlugin() : comboBox_(NULL), searchLine_(NULL), searchButton_(NULL), host_(NULL) { // TODO: Parse engine descriptions. // -Add engines to model // -Show model in comboBox +>>>>>>> .r31 } diff --git a/src/plugins/searchplugin/SearchPlugin.h b/src/plugins/searchplugin/SearchPlugin.h index 9024dee..f8320fc 100644 --- a/src/plugins/searchplugin/SearchPlugin.h +++ b/src/plugins/searchplugin/SearchPlugin.h @@ -1,3 +1,22 @@ +/*************************************************************************** + * 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 SEARCHPLUGIN_H #define SEARCHPLUGIN_H diff --git a/src/plugins/searchplugin/searchplugin.pro b/src/plugins/searchplugin/searchplugin.pro index 327648d..374ff9f 100644 --- a/src/plugins/searchplugin/searchplugin.pro +++ b/src/plugins/searchplugin/searchplugin.pro @@ -9,7 +9,7 @@ CONFIG += dll TARGET = -DESTDIR = ../../../bin/ +DESTDIR = ../../../bin/plugins TARGET = searchplugin DEPENDPATH += . .. -- 1.7.9.5