Vidalia  0.3.1
PluginEngine.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file PluginEngine.cpp
13 ** \brief Engine that handles all plugin related features
14 */
15 
16 #include "PluginEngine.h"
17 #include "VidaliaSettings.h"
18 #include "PluginWrapper.h"
19 #include "DebugDialog.h"
20 
21 #include "Vidalia.h"
22 
23 #include <unistd.h>
24 
26  : QScriptEngine(parent)
27 {
33 
34  globalObject().setProperty("torControl", newQObject(Vidalia::torControl()));
35  globalObject().setProperty("vidaliaApp", newQObject(vApp));
36 
37 // globalObject().setProperty("include", newFunction(includeScript));
38  globalObject().setProperty("importExtension", newFunction(importExtension));
39  globalObject().setProperty("vdebug", newFunction(vdebug));
40  globalObject().setProperty("findWidget", newFunction(findWidget));
41  globalObject().setProperty("sleep", newFunction(sleep));
42 
43  VidaliaSettings settings;
44  globalObject().setProperty("pluginPath", QScriptValue(settings.pluginPath()));
45 
46  DebugDialog::outputDebug("Available extensions:");
47  foreach(QString ext, availableExtensions())
48  DebugDialog::outputDebug(QString(" %1").arg(ext));
49 
51 }
52 
54 {
55  foreach(PluginWrapper *wrapper, wrappers)
56  wrapper->stop();
57 }
58 
59 void
61 {
62  DebugDialog::outputDebug("Loading all plugins...");
63 
64  VidaliaSettings settings;
65  QDir path = QDir(settings.pluginPath());
66 
67  DebugDialog::outputDebug(QString("PluginPath=%1").arg(path.absolutePath()));
68 
69  foreach(QString pdir, path.entryList(QDir::NoDotAndDotDot|QDir::AllDirs)) {
70  QFileInfo finfo(QString("%1%2%3")
71  .arg(path.absolutePath())
72  .arg(QDir::separator())
73  .arg(pdir));
74 
75  if(finfo.isDir()) {
76  tryLoadPlugin(finfo.filePath());
77  }
78  }
79 }
80 
81 void
83 {
84  QStringList files = path.entryList();
85 
86  if(!files.contains("info.xml")) {
87  DebugDialog::outputDebug(tr("WARNING: %1 doesn't have an info file.")
88  .arg(path.absolutePath()));
89  return;
90  }
91 
92  PluginWrapper *wrapper = new PluginWrapper(QString("%1%2info.xml")
93  .arg(path.absolutePath())
94  .arg(QDir::separator()), this);
95 
96  // if it's persistent, start it right away
97  if(wrapper->isPersistent()) {
98  wrapper->start();
99  }
100 
101  wrappers << wrapper;
102 
103  connect(wrapper, SIGNAL(pluginTab(VidaliaTab *)), this, SIGNAL(pluginTab(VidaliaTab *)));
104 }
105 
106 QList<QAction *>
108 {
109  QList<QAction *> actions;
110  foreach(PluginWrapper *wrapper, wrappers)
111  actions << wrapper->menuAction();
112 
113  return actions;
114 }
115 
116 QScriptValue
117 PluginEngine::importExtension(QScriptContext *context, QScriptEngine *engine)
118 {
119  return engine->importExtension(context->argument(0).toString());
120 }
121 
122 //QScriptValue
123 //PluginEngine::includeScript(QScriptContext *context, QScriptEngine *engine)
124 //{
125 // VidaliaSettings settings;
126 // QString path = settings.pluginPath();
127 // QString importFile = context->argument(0).toString();
128 // QFileInfo importInfo(importFile);
129 // if (importInfo.isRelative()) {
130 // importFile = path + "/" + importInfo.filePath();
131 // }
132 
133 // if (!loadFile(importFile, engine)) {
134 // return context->throwError(QString("Failed to resolve include: %1").arg(importFile));
135 // }
136 // return engine->toScriptValue(true);
137 //}
138 
139 //bool
140 //PluginEngine::loadFile(QString fileName, QScriptEngine *engine)
141 //{
142 // static QSet<QString> loadedFiles;
143 // QFileInfo fileInfo(fileName);
144 // QString absoluteFileName = fileInfo.absoluteFilePath();
145 // QString absolutePath = fileInfo.absolutePath();
146 // QString canonicalFileName = fileInfo.canonicalFilePath();
147 // if (loadedFiles.contains(canonicalFileName)) {
148 // return true;
149 // }
150 // loadedFiles.insert(canonicalFileName);
151 // QString path = fileInfo.path();
152 
153 // QFile file(fileName);
154 // if (file.open(QFile::ReadOnly)) {
155 // QTextStream stream(&file);
156 // QString contents = stream.readAll();
157 // file.close();
158 
159 // QScriptValue r = engine->evaluate(contents);
160 // if (engine->hasUncaughtException()) {
161 // QStringList backtrace = engine->uncaughtExceptionBacktrace();
162 // qDebug() << QString(" %1\n%2\n\n").arg(r.toString()).arg(backtrace.join("\n"));
163 // return true;
164 // }
165 // } else {
166 // return false;
167 // }
168 // return true;
169 //}
170 
171 QScriptValue
172 PluginEngine::vdebug(QScriptContext *context, QScriptEngine *engine)
173 {
174  QString result;
175  for(int i = 0; i<context->argumentCount(); i++) {
176  if(i>0)
177  result.append(" ");
178  result.append(context->argument(i).toString());
179  }
180 
181  vInfo(result);
182 
183  return engine->undefinedValue();
184 }
185 
186 QScriptValue
187 PluginEngine::findWidget(QScriptContext *context, QScriptEngine *engine)
188 {
189  if(context->argumentCount() != 2)
190  return context->throwError(QString("findWidget called with the wrong argument count. Expected 2."));
191 
192  QWidget *widget = qscriptvalue_cast<QWidget *>(context->argument(0));
193  QString name = context->argument(1).toString();
194 
195  QObjectList list = widget->children();
196  QScriptValue ret = engine->nullValue();
197 
198  for(int i = 0; i < list.length(); i++) {
199  if(list[i]->objectName() == name) {
200  ret = QScriptValue(i);
201  break;
202  }
203  }
204 
205  return ret;
206 }
207 
208 QScriptValue
209 PluginEngine::sleep(QScriptContext *context, QScriptEngine *engine)
210 {
211  if(context->argumentCount() != 1)
212  return context->throwError(QString("sleep must be called with 1 parameter."));
213 
214  int s = context->argument(1).toInt32();
215 
216 #if defined(Q_WS_WIN)
217  Sleep(s*1000);
218 #else
219  !::sleep(s);
220 #endif
221 
222  return engine->nullValue();
223 }
QString pluginPath() const
static QScriptValue sleep(QScriptContext *context, QScriptEngine *engine)
void tryLoadPlugin(QDir path)
#define vInfo(fmt)
Definition: Vidalia.h:40
#define ADD_PROTOTYPE(protoType)
Definition: PluginEngine.h:28
static QScriptValue importExtension(QScriptContext *context, QScriptEngine *engine)
static QScriptValue findWidget(QScriptContext *context, QScriptEngine *engine)
#define MAKE_CREATABLE(protoType)
Definition: PluginEngine.h:32
QList< QAction * > getAllActions()
static TorControl * torControl()
Definition: Vidalia.h:76
QString i(QString str)
Definition: html.cpp:32
stop errmsg connect(const QHostAddress &address, quint16 port)
void pluginTab(VidaliaTab *)
QAction * menuAction()
void loadAllPlugins()
static void outputDebug(const QString &msg)
Definition: DebugDialog.cpp:46
QList< PluginWrapper * > wrappers
Definition: PluginEngine.h:58
PluginEngine(QObject *parent=0)
#define vApp
Definition: Vidalia.h:37
static QScriptValue vdebug(QScriptContext *context, QScriptEngine *engine)