Qt 4.8
qaxscript.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the ActiveQt framework of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include "qaxscript.h"
42 
43 #ifndef QT_NO_WIN_ACTIVEQT
44 
45 #if defined(Q_CC_GNU)
46 # define QT_NO_QAXSCRIPT
47 #elif defined(Q_CC_BOR) && __BORLANDC__ < 0x560
48 # define QT_NO_QAXSCRIPT
49 #endif
50 
51 #include <qapplication.h>
52 #include <qfile.h>
53 #include <qhash.h>
54 #include <qmetaobject.h>
55 #include <quuid.h>
56 #include <qwidget.h>
57 
58 #include <qt_windows.h>
59 #ifndef QT_NO_QAXSCRIPT
60 #include <initguid.h>
61 #include <activscp.h>
62 #endif
63 
64 #include "../shared/qaxtypes.h"
65 
67 
70 
72 {
73 public:
76 };
77 
78 /*
79  \class QAxScriptSite
80  \brief The QAxScriptSite class implements a Windows Scripting Host
81  \internal
82 
83  The QAxScriptSite is used internally to communicate callbacks from the script
84  engine to the script manager.
85 */
86 
87 #ifndef QT_NO_QAXSCRIPT
88 
89 class QAxScriptSite : public IActiveScriptSite, public IActiveScriptSiteWindow
90 {
91 public:
93 
94  ULONG WINAPI AddRef();
95  ULONG WINAPI Release();
96  HRESULT WINAPI QueryInterface(REFIID iid, void **ppvObject);
97 
98  HRESULT WINAPI GetLCID(LCID *plcid);
99  HRESULT WINAPI GetItemInfo(LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti);
100  HRESULT WINAPI GetDocVersionString(BSTR *pbstrVersion);
101 
102  HRESULT WINAPI OnScriptTerminate(const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo);
103  HRESULT WINAPI OnStateChange(SCRIPTSTATE ssScriptState);
104  HRESULT WINAPI OnScriptError(IActiveScriptError *pscripterror);
105  HRESULT WINAPI OnEnterScript();
106  HRESULT WINAPI OnLeaveScript();
107 
108  HRESULT WINAPI GetWindow(HWND *phwnd);
109  HRESULT WINAPI EnableModeless(BOOL fEnable);
110 
111 protected:
112  QWidget *window() const;
113 
114 private:
116  LONG ref;
117 };
118 
119 /*
120  Constructs the site for the \a s.
121 */
123 : script(s), ref(1)
124 {
125 }
126 
127 /*
128  Implements IUnknown::AddRef
129 */
130 ULONG WINAPI QAxScriptSite::AddRef()
131 {
132  return InterlockedIncrement(&ref);
133 }
134 
135 /*
136  Implements IUnknown::Release
137 */
139 {
140  LONG refCount = InterlockedDecrement(&ref);
141  if (!refCount)
142  delete this;
143 
144  return refCount;
145 }
146 
147 /*
148  Implements IUnknown::QueryInterface
149 */
150 HRESULT WINAPI QAxScriptSite::QueryInterface(REFIID iid, void **ppvObject)
151 {
152  *ppvObject = 0;
153  if (iid == IID_IUnknown)
154  *ppvObject = (IUnknown*)(IActiveScriptSite*)this;
155  else if (iid == IID_IActiveScriptSite)
156  *ppvObject = (IActiveScriptSite*)this;
157  else if (iid == IID_IActiveScriptSiteWindow)
158  *ppvObject = (IActiveScriptSiteWindow*)this;
159  else
160  return E_NOINTERFACE;
161 
162  AddRef();
163  return S_OK;
164 }
165 
166 /*
167  Implements IActiveScriptSite::GetLCID
168 
169  This method is not implemented. Use the system-defined locale.
170 */
171 HRESULT WINAPI QAxScriptSite::GetLCID(LCID * /*plcid*/)
172 {
173  return E_NOTIMPL;
174 }
175 
176 /*
177  Implements IActiveScriptSite::GetItemInfo
178 
179  Tries to find the QAxBase for \a pstrName and returns the
180  relevant interfaces in \a item and \a type as requested through \a mask.
181 */
182 HRESULT WINAPI QAxScriptSite::GetItemInfo(LPCOLESTR pstrName, DWORD mask, IUnknown **item, ITypeInfo **type)
183 {
184  if (item)
185  *item = 0;
186  else if (mask & SCRIPTINFO_IUNKNOWN)
187  return E_POINTER;
188 
189  if (type)
190  *type = 0;
191  else if (mask & SCRIPTINFO_ITYPEINFO)
192  return E_POINTER;
193 
194  QAxBase *object = script->findObject(QString::fromWCharArray(pstrName));
195  if (!object)
196  return TYPE_E_ELEMENTNOTFOUND;
197 
198  if (mask & SCRIPTINFO_IUNKNOWN)
199  object->queryInterface(IID_IUnknown, (void**)item);
200  if (mask & SCRIPTINFO_ITYPEINFO) {
201  IProvideClassInfo *classInfo = 0;
202  object->queryInterface(IID_IProvideClassInfo, (void**)&classInfo);
203  if (classInfo) {
204  classInfo->GetClassInfo(type);
205  classInfo->Release();
206  }
207  }
208  return S_OK;
209 }
210 
211 /*
212  Implements IActiveScriptSite::GetDocVersionString
213 
214  This method is not implemented. The scripting engine should assume
215  that the script is in sync with the document.
216 */
217 HRESULT WINAPI QAxScriptSite::GetDocVersionString(BSTR * /*version*/)
218 {
219  return E_NOTIMPL;
220 }
221 
222 /*
223  Implements IActiveScriptSite::OnScriptTerminate
224 
225  This method is usually not called, but if it is it fires
226  QAxScript::finished().
227 */
228 HRESULT WINAPI QAxScriptSite::OnScriptTerminate(const VARIANT *result, const EXCEPINFO *exception)
229 {
230  emit script->finished();
231 
232  if (result && result->vt != VT_EMPTY)
233  emit script->finished(VARIANTToQVariant(*result, 0));
234  if (exception)
235  emit script->finished(exception->wCode,
236  QString::fromWCharArray(exception->bstrSource),
237  QString::fromWCharArray(exception->bstrDescription),
238  QString::fromWCharArray(exception->bstrHelpFile)
239  );
240  return S_OK;
241 }
242 
243 /*
244  Implements IActiveScriptSite::OnEnterScript
245 
246  Fires QAxScript::entered() to inform the host that the
247  scripting engine has begun executing the script code.
248 */
250 {
251  emit script->entered();
252  return S_OK;
253 }
254 
255 /*
256  Implements IActiveScriptSite::OnLeaveScript
257 
258  Fires QAxScript::finished() to inform the host that the
259  scripting engine has returned from executing the script code.
260 */
262 {
263  emit script->finished();
264  return S_OK;
265 }
266 
267 /*
268  Implements IActiveScriptSite::OnScriptError
269 
270  Fires QAxScript::error() to inform the host that an
271  that an execution error occurred while the engine was running the script.
272 */
273 HRESULT WINAPI QAxScriptSite::OnScriptError(IActiveScriptError *error)
274 {
275  EXCEPINFO exception;
276  memset(&exception, 0, sizeof(exception));
277  DWORD context;
278  ULONG lineNumber;
279  LONG charPos;
280  BSTR bstrLineText;
281  QString lineText;
282 
283  error->GetExceptionInfo(&exception);
284  error->GetSourcePosition(&context, &lineNumber, &charPos);
285  HRESULT hres = error->GetSourceLineText(&bstrLineText);
286  if (hres == S_OK) {
287  lineText = QString::fromWCharArray(bstrLineText);
288  SysFreeString(bstrLineText);
289  }
290  SysFreeString(exception.bstrSource);
291  SysFreeString(exception.bstrDescription);
292  SysFreeString(exception.bstrHelpFile);
293 
294  emit script->error(exception.wCode, QString::fromWCharArray(exception.bstrDescription), lineNumber, lineText);
295 
296  return S_OK;
297 }
298 
299 /*
300  Implements IActiveScriptSite::OnStateChange
301 
302  Fires QAxScript::stateChanged() to inform the
303  the host that the scripting engine has changed states.
304 */
305 HRESULT WINAPI QAxScriptSite::OnStateChange(SCRIPTSTATE ssScriptState)
306 {
307  emit script->stateChanged(ssScriptState);
308  return S_OK;
309 }
310 
311 /*
312  \internal
313  Returns the toplevel widget parent of this script, or
314  the application' active window if there is no widget parent.
315 */
317 {
318  QWidget *w = 0;
319  QObject *p = script->parent();
320  while (!w && p) {
321  w = qobject_cast<QWidget*>(p);
322  p = p->parent();
323  }
324 
325  if (w)
326  w = w->window();
327  if (!w && qApp)
328  w = qApp->activeWindow();
329 
330  return w;
331 }
332 
333 /*
334  Implements IActiveScriptSiteWindow::GetWindow
335 
336  Retrieves the handle to a window that can act as the owner of a
337  pop-up window that the scripting engine must display.
338 */
340 {
341  if (!phwnd)
342  return E_POINTER;
343 
344  *phwnd = 0;
345  QWidget *w = window();
346  if (!w)
347  return E_FAIL;
348 
349  *phwnd = w->winId();
350  return S_OK;
351 }
352 
353 /*
354  Implements IActiveScriptSiteWindow::EnableModeless
355 
356  Causes the host to enable or disable its main window
357  as well as any modeless dialog boxes.
358 */
360 {
361  QWidget *w = window();
362  if (!w)
363  return E_FAIL;
364 
365  EnableWindow(w->winId(), fEnable);
366  return S_OK;
367 }
368 
369 #endif //QT_NO_QAXSCRIPT
370 
371 
422 : QAxObject(script), script_code(script), engine(0), script_language(language)
423 {
424 #ifdef QT_CHECK_STATE
425  if (language.isEmpty())
426  qWarning("QAxScriptEngine: created without language");
427 
428  if (!script_code)
429  qWarning("QAxScriptEngine: created without script");
430 #endif
431  setObjectName(QLatin1String("QAxScriptEngine_") + language);
434 }
435 
441 {
442 #ifndef QT_NO_QAXSCRIPT
443  if (engine) {
444  engine->SetScriptState(SCRIPTSTATE_DISCONNECTED);
445  engine->Close();
446  engine->Release();
447  }
448 #endif
449 }
450 
464 {
465  *ptr = 0;
466 
467 #ifndef QT_NO_QAXSCRIPT
469  return false;
470 
471  CLSID clsid;
472  HRESULT hres = CLSIDFromProgID((wchar_t*)script_language.utf16(), &clsid);
473  if(FAILED(hres))
474  return false;
475 
476  CoCreateInstance(clsid, 0, CLSCTX_INPROC_SERVER, IID_IActiveScript, (void**)&engine);
477  if (!engine)
478  return false;
479 
480  IActiveScriptParse *parser = 0;
481  engine->QueryInterface(IID_IActiveScriptParse, (void**)&parser);
482  if (!parser) {
483  engine->Release();
484  engine = 0;
485  return false;
486  }
487 
488  if (engine->SetScriptSite(script_code->script_site) != S_OK) {
489  engine->Release();
490  engine = 0;
491  return false;
492  }
493  if (parser->InitNew() != S_OK) {
494  parser->Release();
495  engine->Release();
496  engine = 0;
497  return false;
498  }
499 
500  BSTR bstrCode = QStringToBSTR(script_code->scriptCode());
501 #ifdef Q_OS_WIN64
502  hres = parser->ParseScriptText(bstrCode, 0, 0, 0, DWORDLONG(this), 0, SCRIPTTEXT_ISVISIBLE, 0, 0);
503 #else
504  hres = parser->ParseScriptText(bstrCode, 0, 0, 0, DWORD(this), 0, SCRIPTTEXT_ISVISIBLE, 0, 0);
505 #endif
506  SysFreeString(bstrCode);
507 
508  parser->Release();
509  parser = 0;
510 
512 
513  if (engine->SetScriptState(SCRIPTSTATE_CONNECTED) != S_OK) {
514  engine = 0;
515  return false;
516  }
517 
518  IDispatch *scriptDispatch = 0;
519  engine->GetScriptDispatch(0, &scriptDispatch);
520  if (scriptDispatch) {
521  scriptDispatch->QueryInterface(IID_IUnknown, (void**)ptr);
522  scriptDispatch->Release();
523  }
524 #endif
525 
526  return *ptr != 0;
527 }
528 
544 {
545  if (!isValid())
546  return false;
547 
548  IDispatch *scriptDispatch = 0;
549  QAxBase::queryInterface(IID_IDispatch, (void**)&scriptDispatch);
550  if (!scriptDispatch)
551  return false;
552 
553  UINT tic = 0;
554  HRESULT hres = scriptDispatch->GetTypeInfoCount(&tic);
555  scriptDispatch->Release();
556  return hres == S_OK && tic > 0;
557 }
558 
567 long QAxScriptEngine::queryInterface(const QUuid &uuid, void **iface) const
568 {
569  *iface = 0;
570  if (!engine)
571  return E_NOTIMPL;
572 
573 #ifndef QT_NO_QAXSCRIPT
574  return engine->QueryInterface(uuid, iface);
575 #else
576  Q_UNUSED(uuid)
577  return E_NOTIMPL;
578 #endif
579 }
580 
585 {
586  if (!engine)
587  return Uninitialized;
588 
589 #ifndef QT_NO_QAXSCRIPT
590  SCRIPTSTATE state;
591  engine->GetScriptState(&state);
592  return (State)state;
593 #else
594  return Uninitialized;
595 #endif
596 }
597 
603 {
604 #ifndef QT_NO_QAXSCRIPT
605  if (!engine)
606  return;
607 
608  engine->SetScriptState((SCRIPTSTATE)st);
609 #else
610  Q_UNUSED(st)
611 #endif
612 }
613 
619 {
620 #ifndef QT_NO_QAXSCRIPT
621  if (!engine)
622  return;
623 
624  engine->AddNamedItem((wchar_t*)name.utf16(), SCRIPTITEM_ISSOURCE|SCRIPTITEM_ISVISIBLE);
625 #else
626  Q_UNUSED(name)
627 #endif
628 }
629 
677 : QObject(manager), script_name(name), script_manager(manager),
678 script_engine(0)
679 {
680  if (manager) {
681  manager->d->scriptDict.insert(name, this);
682  connect(this, SIGNAL(error(int,QString,int,QString)),
683  manager, SLOT(scriptError(int,QString,int,QString)));
684  }
685 
686 #ifndef QT_NO_QAXSCRIPT
687  script_site = new QAxScriptSite(this);
688 #else
689  script_site = 0;
690 #endif
691 }
692 
697 {
698  delete script_engine;
699  script_engine = 0;
700 
701 #ifndef QT_NO_QAXSCRIPT
702  script_site->Release();
703 #endif
704 }
705 
720 bool QAxScript::load(const QString &code, const QString &language)
721 {
722  if (script_engine || code.isEmpty())
723  return false;
724 
725  script_code = code;
726  QString lang = language;
727  if (lang.isEmpty()) {
728  if (code.contains(QLatin1String("End Sub"), Qt::CaseInsensitive))
729  lang = QLatin1String("VBScript");
730 
732  for (it = engines.begin(); it != engines.end(); ++it) {
733  QAxEngineDescriptor engine = *it;
734  if (engine.code.isEmpty())
735  continue;
736 
737  if (code.contains(engine.code)) {
738  lang = engine.name;
739  break;
740  }
741  }
742  }
743  if (lang.isEmpty())
744  lang = QLatin1String("JScript");
745 
746  script_engine = new QAxScriptEngine(lang, this);
747  // trigger call to initialize
749 
750  return script_engine->isValid();
751 }
752 
762 {
764 
765  const QMetaObject *mo = script_engine->metaObject();
766  for (int i = mo->methodOffset(); i < mo->methodCount(); ++i) {
767  const QMetaMethod slot(mo->method(i));
768  if (slot.methodType() != QMetaMethod::Slot || slot.access() != QMetaMethod::Public)
769  continue;
770  QString slotname = QString::fromLatin1(slot.signature());
771  if (slotname.contains(QLatin1Char('_')))
772  continue;
773 
774  if (flags == FunctionSignatures)
775  functions << slotname;
776  else
777  functions << slotname.left(slotname.indexOf(QLatin1Char('(')));
778  }
779 
780  return functions;
781 }
782 
793 QVariant QAxScript::call(const QString &function, const QVariant &var1,
794  const QVariant &var2,
795  const QVariant &var3,
796  const QVariant &var4,
797  const QVariant &var5,
798  const QVariant &var6,
799  const QVariant &var7,
800  const QVariant &var8)
801 {
802  if (!script_engine)
803  return QVariant();
804 
805  return script_engine->dynamicCall(function.toLatin1(), var1, var2, var3, var4, var5, var6, var7, var8);
806 }
807 
820 QVariant QAxScript::call(const QString &function, QList<QVariant> &arguments)
821 {
822  if (!script_engine)
823  return QVariant();
824 
825  return script_engine->dynamicCall(function.toLatin1(), arguments);
826 }
827 
835 {
836  if (!script_manager)
837  return;
838 
840 }
841 
849 {
850  if (!script_manager)
851  return 0;
852 
853  return script_manager->d->objectDict.value(name);
854 }
855 
973 : QObject(parent)
974 {
976 }
977 
982 {
983  delete d;
984 }
985 
994 {
996 
998  for (scriptIt = d->scriptDict.begin(); scriptIt != d->scriptDict.end(); ++scriptIt) {
999  QAxScript *script = scriptIt.value();
1000  functions += script->functions(flags);
1001  }
1002 
1003  return functions;
1004 }
1005 
1010 {
1011  QStringList scripts;
1012 
1014  for (scriptIt = d->scriptDict.begin(); scriptIt != d->scriptDict.end(); ++scriptIt) {
1015  scripts << scriptIt.key();
1016  }
1017 
1018  return scripts;
1019 }
1020 
1029 {
1030  return d->scriptDict.value(name);
1031 }
1032 
1041 {
1042  QObject *obj = object->qObject();
1043  QString name = obj->objectName();
1044  if (d->objectDict.contains(name))
1045  return;
1046 
1047  d->objectDict.insert(name, object);
1049 }
1050 
1084 {
1085  QAxScript *script = new QAxScript(name, this);
1086  if (script->load(code, language))
1087  return script;
1088 
1089  delete script;
1090  return 0;
1091 }
1092 
1112 {
1113  QFile f(file);
1114  if (!f.open(QIODevice::ReadOnly))
1115  return 0;
1116  QByteArray data = f.readAll();
1117  QString contents = QString::fromLocal8Bit(data, data.size());
1118  f.close();
1119 
1120  if (contents.isEmpty())
1121  return 0;
1122 
1123  QString language;
1124  if (file.endsWith(QLatin1String(".js"))) {
1125  language = QLatin1String("JScript");
1126  } else {
1128  for (it = engines.begin(); it != engines.end(); ++it) {
1129  QAxEngineDescriptor engine = *it;
1130  if (engine.extension.isEmpty())
1131  continue;
1132 
1133  if (file.endsWith(engine.extension)) {
1134  language = engine.name;
1135  break;
1136  }
1137  }
1138  }
1139 
1140  if (language.isEmpty())
1141  language = QLatin1String("VBScript");
1142 
1143  QAxScript *script = new QAxScript(name, this);
1144  if (script->load(contents, language))
1145  return script;
1146 
1147  delete script;
1148  return 0;
1149 }
1150 
1177 QVariant QAxScriptManager::call(const QString &function, const QVariant &var1,
1178  const QVariant &var2,
1179  const QVariant &var3,
1180  const QVariant &var4,
1181  const QVariant &var5,
1182  const QVariant &var6,
1183  const QVariant &var7,
1184  const QVariant &var8)
1185 {
1186  QAxScript *s = script(function);
1187  if (!s) {
1188 #ifdef QT_CHECK_STATE
1189  qWarning("QAxScriptManager::call: No script provides function %s, or this function\n"
1190  "\tis provided through an engine that does not support introspection", function.latin1());
1191 #endif
1192  return QVariant();
1193  }
1194 
1195  return s->call(function, var1, var2, var3, var4, var5, var6, var7, var8);
1196 }
1197 
1207 {
1208  QAxScript *s = script(function);
1209  if (!s) {
1210 #ifdef QT_CHECK_STATE
1211  qWarning("QAxScriptManager::call: No script provides function %s, or this function\n"
1212  "\tis provided through an engine that does not support introspection", function.latin1());
1213 #endif
1214  return QVariant();
1215  }
1216 
1217  QList<QVariant> args(arguments);
1218  return s->call(function, args);
1219 }
1220 
1229 bool QAxScriptManager::registerEngine(const QString &name, const QString &extension, const QString &code)
1230 {
1231  if (name.isEmpty())
1232  return false;
1233 
1234  CLSID clsid;
1235  HRESULT hres = CLSIDFromProgID((wchar_t*)name.utf16(), &clsid);
1236  if (hres != S_OK)
1237  return false;
1238 
1239  QAxEngineDescriptor engine;
1240  engine.name = name;
1241  engine.extension = extension;
1242  engine.code = code;
1243 
1244  engines.prepend(engine);
1245  return true;
1246 }
1247 
1253 {
1254  QString allFiles = QLatin1String("Script Files (*.js *.vbs *.dsm");
1255  QString specialFiles = QLatin1String(";;VBScript Files (*.vbs *.dsm)"
1256  ";;JavaScript Files (*.js)");
1257 
1259  for (it = engines.begin(); it != engines.end(); ++it) {
1260  QAxEngineDescriptor engine = *it;
1261  if (engine.extension.isEmpty())
1262  continue;
1263 
1264  allFiles += QLatin1String(" *") + engine.extension;
1265  specialFiles += QLatin1String(";;") + engine.name + QLatin1String(" Files (*") + engine.extension + QLatin1Char(')');
1266  }
1267  allFiles += QLatin1Char(')');
1268 
1269  return allFiles + specialFiles + QLatin1String(";;All Files (*.*)");
1270 }
1271 
1296 {
1297  // check full prototypes if included
1298  if (function.contains(QLatin1Char('('))) {
1300  for (scriptIt = d->scriptDict.begin(); scriptIt != d->scriptDict.end(); ++scriptIt) {
1301  QAxScript *script = scriptIt.value();
1302 
1303  if (script->functions(QAxScript::FunctionSignatures).contains(function))
1304  return script;
1305  }
1306  }
1307 
1308  QString funcName = function;
1309  funcName = funcName.left(funcName.indexOf(QLatin1Char('(')));
1310  // second try, checking only names, not prototypes
1312  for (scriptIt = d->scriptDict.begin(); scriptIt != d->scriptDict.end(); ++scriptIt) {
1313  QAxScript *script = scriptIt.value();
1314 
1315  if (script->functions(QAxScript::FunctionNames).contains(funcName))
1316  return script;
1317  }
1318 
1319  return 0;
1320 }
1321 
1326 {
1328  for (objectIt = d->objectDict.constBegin(); objectIt != d->objectDict.constEnd(); ++objectIt) {
1329  QString name = objectIt.key();
1330 
1331  QAxScriptEngine *engine = script->scriptEngine();
1332  if (engine)
1333  engine->addItem(name);
1334  }
1335 }
1336 
1341 {
1342  d->objectDict.take(o->objectName());
1343 }
1344 
1348 void QAxScriptManager::scriptError(int code, const QString &desc, int spos, const QString &stext)
1349 {
1350  QAxScript *source = qobject_cast<QAxScript*>(sender());
1351  emit error(source, code, desc, spos, stext);
1352 }
1353 
1355 #endif // QT_NO_WIN_ACTIVEQT
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
HRESULT WINAPI EnableModeless(BOOL fEnable)
Definition: qaxscript.cpp:359
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
T qobject_cast(QObject *object)
Definition: qobject.h:375
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
void finished()
This signal is emitted when a script engine has finished executing code.
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
int type
Definition: qmetatype.cpp:239
QStringList functions(QAxScript::FunctionFlags=QAxScript::FunctionNames) const
Returns a list with all the functions that are available.
Definition: qaxscript.cpp:993
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QVariant dynamicCall(const char *name, const QVariant &v1=QVariant(), const QVariant &v2=QVariant(), const QVariant &v3=QVariant(), const QVariant &v4=QVariant(), const QVariant &v5=QVariant(), const QVariant &v6=QVariant(), const QVariant &v7=QVariant(), const QVariant &v8=QVariant())
Calls the COM object&#39;s method function, passing the parameters var1, var1, var2, var3, var4, var5, var6, var7 and var8, and returns the value returned by the method, or an invalid QVariant if the method does not return a value or when the function call failed.
Definition: qaxbase.cpp:4046
QString script_code
Definition: qaxscript.h:147
#define it(className, varName)
QString script_language
Definition: qaxscript.h:97
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
HRESULT WINAPI OnEnterScript()
Definition: qaxscript.cpp:249
#define error(msg)
QString objectName
the name of this object
Definition: qobject.h:114
FunctionFlags
This FunctionFlags enum describes formatting for function introspection.
Definition: qaxscript.h:105
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void disableClassInfo()
Disables the class info generation for this ActiveX container.
Definition: qaxbase.cpp:1083
HRESULT WINAPI GetItemInfo(LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
Definition: qaxscript.cpp:182
QAxScriptSite(QAxScript *script)
Definition: qaxscript.cpp:122
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
The QList::const_iterator class provides an STL-style const iterator for QList and QQueue...
Definition: qlist.h:228
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void addItem(const QString &name)
Registers an item with the script engine.
Definition: qaxscript.cpp:618
friend class QAxScript
Definition: qaxscript.h:192
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
T take(const Key &key)
Removes the item with the key from the hash and returns the value associated with it...
Definition: qhash.h:807
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
The QAxObject class provides a QObject that wraps a COM object.
Definition: qaxobject.h:54
void updateScript(QAxScript *)
Definition: qaxscript.cpp:1325
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QStringList scriptNames() const
Returns a list with the names of all the scripts.
Definition: qaxscript.cpp:1009
~QAxScriptEngine()
Destroys the QAxScriptEngine object, releasing all allocated resources.
Definition: qaxscript.cpp:440
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
QAxScript * script
Definition: qaxscript.cpp:115
HRESULT WINAPI OnLeaveScript()
Definition: qaxscript.cpp:261
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const QMetaObject * metaObject() const
Definition: qaxobject.cpp:131
QAxScript * script_code
Definition: qaxscript.h:94
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition: qobject.cpp:2327
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
const Key & key() const
Returns the current item&#39;s key.
Definition: qhash.h:419
bool isValid() const
Returns true if the script engine has been initialized correctly; otherwise returns false...
Definition: qaxscript.h:219
QAxScriptManager * script_manager
Definition: qaxscript.h:148
#define SIGNAL(a)
Definition: qobjectdefs.h:227
NSWindow * window
void error(int code, const QString &description, int sourcePosition, const QString &sourceText)
This signal is emitted when an execution error occurred while running a script.
void addObject(QAxBase *object)
Adds object to the manager.
Definition: qaxscript.cpp:1040
QLocale::Language language
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool load(const QString &code, const QString &language=QString())
Loads the script source code written in language language into the script engine. ...
Definition: qaxscript.cpp:720
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
int methodOffset() const
Returns the method offset for this class; i.e.
bool hasIntrospection() const
Returns true if the script engine supports introspection; otherwise returns false.
Definition: qaxscript.cpp:543
QAxScriptManagerPrivate * d
Definition: qaxscript.h:193
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
static QList< QAxEngineDescriptor > engines
Definition: qaxscript.cpp:69
QAxScriptEngine * scriptEngine() const
Returns a pointer to the script engine.
Definition: qaxscript.h:212
void scriptError(int code, const QString &description, int sourcePosition, const QString &sourceText)
Definition: qaxscript.cpp:1348
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
#define qApp
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
QVariant call(const QString &function, const QVariant &v1=QVariant(), const QVariant &v2=QVariant(), const QVariant &v3=QVariant(), const QVariant &v4=QVariant(), const QVariant &v5=QVariant(), const QVariant &v6=QVariant(), const QVariant &v7=QVariant(), const QVariant &v8=QVariant())
Calls function, passing the parameters var1, var1, var2, var3, var4, var5, var6, var7 and var8 as arg...
Definition: qaxscript.cpp:1177
const char * name
IActiveScript * engine
Definition: qaxscript.h:95
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
#define emit
Definition: qobjectdefs.h:76
void entered()
This signal is emitted when a script engine has started executing code.
QWidget * window() const
Definition: qaxscript.cpp:316
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
HRESULT WINAPI OnScriptError(IActiveScriptError *pscripterror)
Definition: qaxscript.cpp:273
const T & value() const
Returns the current item&#39;s value.
Definition: qhash.h:420
Q_CORE_EXPORT void qWarning(const char *,...)
HRESULT WINAPI OnStateChange(SCRIPTSTATE ssScriptState)
Definition: qaxscript.cpp:305
QAxBase * findObject(const QString &name)
Returns the object name registered with the manager.
Definition: qaxscript.cpp:848
static const char * data(const QByteArray &arr)
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QAxScript * script(const QString &name) const
Returns the script called name.
Definition: qaxscript.cpp:1028
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
State
The State enumeration defines the different states a script engine can be in.
Definition: qaxscript.h:66
const T * ptr(const T &t)
The QAxScriptManager class provides a bridge between application objects and script code...
Definition: qaxscript.h:153
The State element defines configurations of objects and properties.
QAxScriptSite * script_site
Definition: qaxscript.h:150
void setState(State st)
Sets the state of the script engine to st.
Definition: qaxscript.cpp:602
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QAxScript * load(const QString &code, const QString &name, const QString &language)
Loads the script source code using the script engine for language.
Definition: qaxscript.cpp:1083
HRESULT WINAPI GetDocVersionString(BSTR *pbstrVersion)
Definition: qaxscript.cpp:217
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:466
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
long HRESULT
QAxScriptEngine(const QString &language, QAxScript *script)
Constructs a QAxScriptEngine object interpreting script code in language provided by the code in scri...
Definition: qaxscript.cpp:421
State state() const
Returns the state of the script engine.
Definition: qaxscript.cpp:584
void objectDestroyed(QObject *o)
Definition: qaxscript.cpp:1340
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
friend class QAxScriptEngine
Definition: qaxscript.h:141
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
HRESULT WINAPI OnScriptTerminate(const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
Definition: qaxscript.cpp:228
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
void error(QAxScript *script, int code, const QString &description, int sourcePosition, const QString &sourceText)
This signal is emitted when an execution error occurred while running script.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
static bool registerEngine(const QString &name, const QString &extension, const QString &code=QString())
Registers the script engine called name and returns true if the engine was found; otherwise does noth...
Definition: qaxscript.cpp:1229
~QAxScriptManager()
Destroys the objects, releasing all allocated resources.
Definition: qaxscript.cpp:981
#define st(var, type, card)
static BSTR QStringToBSTR(const QString &str)
Definition: qaxtypes.h:76
QAxScriptEngine * script_engine
Definition: qaxscript.h:149
QVariant VARIANTToQVariant(const VARIANT &arg, const QByteArray &typeName, uint type)
Returns arg as a QVariant of type type.
Definition: qaxtypes.cpp:919
iterator begin()
Returns an STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:464
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
void disableEventSink()
Disables the event sink implementation for this ActiveX container.
Definition: qaxbase.cpp:1047
QAxScript * scriptForFunction(const QString &function) const
Returns a pointer to the first QAxScript that knows about function, or 0 if this function is unknown...
Definition: qaxscript.cpp:1295
QAxScriptManager(QObject *parent=0)
Creates a QAxScriptManager object.
Definition: qaxscript.cpp:972
QVariant call(const QString &function, const QVariant &v1=QVariant(), const QVariant &v2=QVariant(), const QVariant &v3=QVariant(), const QVariant &v4=QVariant(), const QVariant &v5=QVariant(), const QVariant &v6=QVariant(), const QVariant &v7=QVariant(), const QVariant &v8=QVariant())
Calls function, passing the parameters var1, var1, var2, var3, var4, var5, var6, var7 and var8 as arg...
Definition: qaxscript.cpp:793
bool initialize(IUnknown **ptr)
Reimplemented Function
Definition: qaxscript.cpp:463
static QString scriptFileFilter()
Returns a file filter listing all the supported script languages.
Definition: qaxscript.cpp:1252
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
WId winId() const
Returns the window system identifier of the widget.
Definition: qwidget.cpp:2557
friend class QAxScriptSite
Definition: qaxscript.h:140
HRESULT WINAPI QueryInterface(REFIID iid, void **ppvObject)
Definition: qaxscript.cpp:150
HRESULT WINAPI GetWindow(HWND *phwnd)
Definition: qaxscript.cpp:339
QHash< QString, QAxBase * > objectDict
Definition: qaxscript.cpp:75
QHash< QString, QAxScript * > scriptDict
Definition: qaxscript.cpp:74
The QAxScript class provides a wrapper around script code.
Definition: qaxscript.h:100
ULONG WINAPI AddRef()
Definition: qaxscript.cpp:130
void stateChanged(int state)
This signal is emitted when a script engine changes state.
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
ULONG WINAPI Release()
Definition: qaxscript.cpp:138
long queryInterface(const QUuid &, void **) const
Requests the interface uuid from the script engine object and sets the value of iface to the provided...
Definition: qaxscript.cpp:567
long queryInterface(const QUuid &, void **) const
Requests the interface uuid from the COM object and sets the value of iface to the provided interface...
Definition: qaxbase.cpp:1479
void updateObjects()
Registers all objects in the manager with the script engine.
Definition: qaxscript.cpp:834
virtual void close()
Calls QFile::flush() and closes the file.
Definition: qfile.cpp:1680
QAxScript(const QString &name, QAxScriptManager *manager)
Constructs a QAxScript object called name and registers it with the QAxScriptManager manager...
Definition: qaxscript.cpp:676
The QAxScriptEngine class provides a wrapper around a script engine.
Definition: qaxscript.h:63
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
~QAxScript()
Destroys the object, releasing all allocated resources.
Definition: qaxscript.cpp:696
The QAxBase class is an abstract class that provides an API to initialize and access a COM object...
Definition: qaxbase.h:66
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
QStringList functions(FunctionFlags=FunctionNames) const
Returns a list of all the functions in this script if the respective script engine supports introspec...
Definition: qaxscript.cpp:761
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
HRESULT WINAPI GetLCID(LCID *plcid)
Definition: qaxscript.cpp:171
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
The QUuid class stores a Universally Unique Identifier (UUID).
Definition: quuid.h:67
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290
QString scriptCode() const
Returns the script&#39;s code, or the null-string if no code has been loaded yet.
Definition: qaxscript.h:202