- Plugin interface added to MainWindow
authorlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Wed, 18 Nov 2009 10:06:02 +0000 (10:06 +0000)
committerlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Wed, 18 Nov 2009 10:06:02 +0000 (10:06 +0000)
- Search plugin dummy example

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

src/gui/MainWindow.cpp
src/gui/MainWindow.h
src/gui/gui.pro
src/plugins/PluginInterface.h
src/plugins/searchplugin/SearchPlugin.cpp
src/plugins/searchplugin/SearchPlugin.h
src/plugins/searchplugin/searchplugin.pro

index 4d47618..4c8cc19 100644 (file)
@@ -26,6 +26,8 @@
 #include <QAction>
 #include <QFileDialog>
 #include <QMessageBox>
+#include <QApplication>
+
 
 #include "DownloadView.h"
 #include "SeedView.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\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<Alert>)),
-            this, SLOT(on_alert(std::auto_ptr<Alert>)));
+       // 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();
 }
 
 
@@ -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<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");
+       
+
+       
+
+}
+
 // =========================== SLOTS =================================
 void MainWindow::on_openAction_clicked()
 {
index f4d43b1..9ac916d 100644 (file)
 
 #include <QMainWindow>
 #include <QSettings>
+#include <QDir>
+#include <QStringList>
 
+#include "PluginInterface.h"
 #include "QBittorrentSession.h"
 
 class QTabWidget;
@@ -33,45 +36,54 @@ class PreferencesDialog;
 /**
        @author Lassi Väätämöinen <lassi.vaatamoinen@ixonos.com>
 */
-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<Alert> al);
-
-private:
-    QTabWidget *tabWidget_;
-    DownloadView *dlView_;
-    SeedView *seedView_;
-    PreferencesDialog *preferencesDialog_;
-    QSettings settings_;
-
-    //std::vector< std::auto_ptr<QTorrentHandle> const > torrentHandles_;
-
-    qtrapids::QBittorrentSession btSession_;
-
-
-    //bool IsNewTorrent(std::auto_ptr<QTorrentHandle> 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<Alert> al);
+       
+       private:
+               void LoadPlugins();
+               
+       private:
+               QTabWidget *tabWidget_;
+               DownloadView *dlView_;
+               SeedView *seedView_;
+               PreferencesDialog *preferencesDialog_;
+               QSettings settings_;
+               QDir pluginsDir_;
+               QStringList pluginFileNames_;
+               
+               qtrapids::QBittorrentSession btSession_;
+               
 };
 
 #endif
index 668b387..3945159 100644 (file)
@@ -19,7 +19,8 @@ CONFIG += qtestlib
 
 DESTDIR = ../../bin
 
-INCLUDEPATH += ../engine
+INCLUDEPATH += ../engine \
+       ../plugins
 
 LIBS += -L../../bin \
   -lqtbittorrent \
index dc7a0f7..0e56353 100644 (file)
 
 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.
index f4ded6d..10165f0 100644 (file)
@@ -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 <QDebug>
 #include <QtCore/qplugin.h>
 #include <QVBoxLayout>
 
 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
 
 }
 
index 9024dee..f8320fc 100644 (file)
@@ -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
 
index 327648d..374ff9f 100644 (file)
@@ -9,7 +9,7 @@ CONFIG += dll
 
 TARGET = 
 
-DESTDIR = ../../../bin/
+DESTDIR = ../../../bin/plugins
 
 TARGET = searchplugin 
 DEPENDPATH += . ..