[tor-commits] [vidalia/alpha] Improve communication between modules

chiiph at torproject.org chiiph at torproject.org
Sat Jul 2 21:53:47 UTC 2011


commit 38044c9518ef6335b9499043c3dca965d000e9e6
Author: Tomas Touceda <chiiph at gentoo.org>
Date:   Wed Jun 1 23:44:10 2011 -0300

    Improve communication between modules
    
    Takes out the hacky casts and parent handling for a more Qt-ish way of
    doing this.
---
 src/vidalia/MainWindow.cpp           |   12 +++---------
 src/vidalia/MainWindow.h             |    4 ++--
 src/vidalia/plugin/PluginEngine.cpp  |   23 ++++++++++++++---------
 src/vidalia/plugin/PluginEngine.h    |   10 ++++++++++
 src/vidalia/plugin/PluginWrapper.cpp |   23 ++++++++++++++++++++++-
 src/vidalia/plugin/PluginWrapper.h   |    6 ++++++
 6 files changed, 57 insertions(+), 21 deletions(-)

diff --git a/src/vidalia/MainWindow.cpp b/src/vidalia/MainWindow.cpp
index 931df1c..bd72346 100644
--- a/src/vidalia/MainWindow.cpp
+++ b/src/vidalia/MainWindow.cpp
@@ -183,7 +183,6 @@ MainWindow::createMenuBar()
   QMenu *pluginsMenu = menu->addMenu(tr("Plugins"));
   foreach(QAction *action, _engine->getAllActions()) {
     pluginsMenu->addAction(action);
-    connect(action, SIGNAL(triggered()), this, SLOT(showPluginTab()));
   }
 
   QMenu *helpMenu = menu->addMenu(tr("Help"));
@@ -414,6 +413,9 @@ MainWindow::createConnections()
   _proxyProcess = new HelperProcess(this);
   connect(_proxyProcess, SIGNAL(startFailed(QString)),
            this, SLOT(onProxyFailed(QString)));
+
+  connect(_engine, SIGNAL(pluginTab(VidaliaTab *)), 
+          this, SLOT(addTab(VidaliaTab *)));
 }
 
 /** Called when the application is closing, by selecting "Exit" from the tray
@@ -1593,14 +1595,6 @@ MainWindow::delTab(int index)
 }
 
 void
-MainWindow::showPluginTab()
-{
-  QAction *act = qobject_cast<QAction *>(sender());
-  PluginWrapper *wrapper = qobject_cast<PluginWrapper *>(act->parent());
-  addTab(wrapper->buildGUI());
-}
-
-void
 MainWindow::showStatusTab()
 {
   addTab(&_statusTab);
diff --git a/src/vidalia/MainWindow.h b/src/vidalia/MainWindow.h
index e5366b8..8172f71 100644
--- a/src/vidalia/MainWindow.h
+++ b/src/vidalia/MainWindow.h
@@ -144,8 +144,8 @@ private slots:
   /** Deletes the tab at index if it exists and it isn't the Status tab */
   void delTab(int index = -1);
 
-  /** Handles adding a new tab corresponding to a plugin */
-  void showPluginTab();
+//  /** Handles adding a new tab corresponding to a plugin */
+//  void showPluginTab();
 
   /** Called when the web browser or IM client have stopped */
   void onSubprocessFinished(int exitCode, QProcess::ExitStatus exitStatus);
diff --git a/src/vidalia/plugin/PluginEngine.cpp b/src/vidalia/plugin/PluginEngine.cpp
index fe85883..c7b74ea 100644
--- a/src/vidalia/plugin/PluginEngine.cpp
+++ b/src/vidalia/plugin/PluginEngine.cpp
@@ -5,19 +5,17 @@
 PluginEngine::PluginEngine(QObject *parent)
   : QScriptEngine(parent)
 {
-  // load prototypes
-  VidaliaTabPrototype vtabproto;
-  QScriptValue vtabscript = newQObject(&vtabproto, QScriptEngine::ScriptOwnership);
-  setDefaultPrototype(qMetaTypeId<VidaliaTab *>(), vtabscript);
-
-  // load constructors
-  QScriptValue vtabctor = newFunction(VidaliaTabPrototype::constructor, vtabscript);
-  globalObject().setProperty("VidaliaTab", vtabctor);
+  ADD_CLASS("VidaliaTab", VidaliaTabPrototype, VidaliaTab *, 
+      VidaliaTabPrototype::constructor)
 
   loadAllPlugins();
 }
 
-PluginEngine::~PluginEngine() {}
+PluginEngine::~PluginEngine() 
+{
+  foreach(PluginWrapper *wrapper, wrappers)
+    wrapper->stop();
+}
 
 void
 PluginEngine::loadAllPlugins()
@@ -50,7 +48,14 @@ PluginEngine::tryLoadPlugin(QDir path)
     return;
   
   PluginWrapper *wrapper = new PluginWrapper(QString("%1%2info.xml").arg(path.absolutePath()).arg(QDir::separator()), this);
+
+  // if it's persistent, start it right away
+  if(wrapper->isPersistent())
+    wrapper->start();
+
   wrappers << wrapper;
+
+  connect(wrapper, SIGNAL(pluginTab(VidaliaTab *)), this, SIGNAL(pluginTab(VidaliaTab *)));
 }
 
 QList<QAction *>
diff --git a/src/vidalia/plugin/PluginEngine.h b/src/vidalia/plugin/PluginEngine.h
index 09bff23..7876803 100644
--- a/src/vidalia/plugin/PluginEngine.h
+++ b/src/vidalia/plugin/PluginEngine.h
@@ -8,6 +8,13 @@
 
 class PluginWrapper;
 
+#define ADD_CLASS(name, protoType, absType, ctor) \
+  protoType __proto; \
+  QScriptValue __script = newQObject(&__proto, QScriptEngine::ScriptOwnership); \
+  setDefaultPrototype(qMetaTypeId<absType>(), __script); \
+  QScriptValue __ctor = newFunction(ctor, __script); \
+  globalObject().setProperty(name, __ctor);
+
 class PluginEngine : public QScriptEngine {
   Q_OBJECT
 
@@ -17,6 +24,9 @@ class PluginEngine : public QScriptEngine {
 
     QList<QAction *> getAllActions();
 
+  signals:
+    void pluginTab(VidaliaTab *);
+
   protected:
     void loadAllPlugins();
     void tryLoadPlugin(QDir path);
diff --git a/src/vidalia/plugin/PluginWrapper.cpp b/src/vidalia/plugin/PluginWrapper.cpp
index b06b863..8e839fa 100644
--- a/src/vidalia/plugin/PluginWrapper.cpp
+++ b/src/vidalia/plugin/PluginWrapper.cpp
@@ -100,6 +100,8 @@ PluginWrapper::buildGUI()
   if(_engine->hasUncaughtException()) {
     qWarning() << "Exception:";
     qWarning() << _engine->uncaughtExceptionLineNumber();
+
+    return NULL;
   }
   qWarning() << "Casted tab:" << tab << nspace();
   return tab;
@@ -150,8 +152,27 @@ PluginWrapper::files() const
 QAction *
 PluginWrapper::menuAction()
 {
+  _action = new QAction(_name, this);
+
   if(hasGUI()) {
-    _action = new QAction(_name, this);
+    connect(_action, SIGNAL(triggered()), this, SLOT(emitPluginTab()));
+  }
+
+  // if it hasn't been started yet
+  if(!isPersistent()) {
+    connect(_action, SIGNAL(triggered()), this, SLOT(start()));
   }
+
   return _action;
 }
+
+void
+PluginWrapper::emitPluginTab()
+{
+  VidaliaTab *tab = buildGUI();
+  if(tab)
+    emit pluginTab(tab);
+  else
+    // TODO: make this more than a console print
+    qWarning() << "Error: buildGUI() failed for plugin" << name();
+}
diff --git a/src/vidalia/plugin/PluginWrapper.h b/src/vidalia/plugin/PluginWrapper.h
index 1c7ad8f..d99538c 100644
--- a/src/vidalia/plugin/PluginWrapper.h
+++ b/src/vidalia/plugin/PluginWrapper.h
@@ -25,11 +25,17 @@ class PluginWrapper : public QObject {
 
     QAction *menuAction();
 
+  signals:
+    void pluginTab(VidaliaTab *);
+
   public slots:
     void start();
     void stop();
     VidaliaTab *buildGUI();
 
+  private slots:
+    void emitPluginTab();
+
   protected:
     void processInfo(const QString &path);
 





More information about the tor-commits mailing list