• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Kross

  • sources
  • kde-4.12
  • kdelibs
  • kross
  • core
manager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * manager.cpp
3  * This file is part of the KDE project
4  * copyright (C)2004-2007 by Sebastian Sauer (mail@dipe.org)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  ***************************************************************************/
19 
20 #include "manager.h"
21 #include "interpreter.h"
22 #include "action.h"
23 #include "actioncollection.h"
24 
25 #include <QtCore/QObject>
26 #include <QtCore/QArgument>
27 #include <QtCore/QFile>
28 #include <QtCore/QRegExp>
29 #include <QtCore/QFileInfo>
30 #include <QtCore/QPointer>
31 #include <QtCore/QLibrary>
32 #include <QtCore/QCoreApplication>
33 
34 #include <kstandarddirs.h>
35 #include <klocale.h>
36 
37 extern "C"
38 {
39  typedef QObject* (*def_module_func)();
40 }
41 
42 using namespace Kross;
43 
44 namespace Kross {
45 
47  class Manager::Private
48  {
49  public:
51  QHash< QString, InterpreterInfo* > interpreterinfos;
52 
54  QStringList interpreters;
55 
57  QHash< QString, QPointer<QObject> > modules;
58 
60  ActionCollection* collection;
61 
63  QHash<QByteArray, MetaTypeHandler*> wrappers;
64 
66  bool strictTypesEnabled;
67  };
68 
69 }
70 
71 Q_GLOBAL_STATIC(Manager, _self)
72 
73 Manager& Manager::self()
74 {
75  return *_self();
76 }
77 
78 void* loadLibrary(const char* libname, const char* functionname)
79 {
80  QLibrary lib(libname);
81  lib.setLoadHints( QLibrary::ExportExternalSymbolsHint );
82  if( ! lib.load() ) {
83  const QString err = QString("Error: %1").arg(lib.errorString());
84 
85  //TODO move that functionality out of Kross since we like to be Qt-only
86  foreach(const QString &dir, KStandardDirs().resourceDirs("module")) {
87  lib.setFileName( QFileInfo(dir, libname).filePath() );
88  lib.setLoadHints( QLibrary::ExportExternalSymbolsHint );
89  if( lib.load() )
90  break;
91  }
92 
93  /*
94  if( ! lib.isLoaded() ) {
95  foreach(const QString& path, QCoreApplication::instance()->libraryPaths()) {
96  lib.setFileName( QFileInfo(path, libname).filePath() );
97  lib.setLoadHints( QLibrary::ExportExternalSymbolsHint );
98  if( lib.load() )
99  break;
100  }
101  }
102  */
103 
104  if( ! lib.isLoaded() ) {
105  #ifdef KROSS_INTERPRETER_DEBUG
106  if( strcmp(functionname, "krossinterpreter") == 0 )
107  krossdebug( QString("Kross Interpreter '%1' not available: %2").arg(libname).arg(err) );
108  else if( strcmp(functionname, "krossmodule") == 0 )
109  krossdebug( QString("Kross Module '%1' not available: %2").arg(libname).arg(err) );
110  else
111  krosswarning( QString("Failed to load unknown type of '%1' library: %2").arg(libname).arg(err) );
112  #endif
113  return 0;
114  }
115  }
116  void* funcPtr = lib.resolve(functionname);
117  Q_ASSERT(funcPtr);
118  return funcPtr;
119 }
120 
121 Manager::Manager()
122  : QObject()
123  , QScriptable()
124  , ChildrenInterface()
125  , d( new Private() )
126 {
127  d->strictTypesEnabled = true;
128  setObjectName("Kross");
129  d->collection = new ActionCollection("main");
130 
131 #ifdef KROSS_PYTHON_LIBRARY
132  if( void* funcPtr = loadLibrary(KROSS_PYTHON_LIBRARY, "krossinterpreter") ) {
133  d->interpreterinfos.insert("python",
134  new InterpreterInfo("python",
135  funcPtr, // library
136  "*.py", // file filter-wildcard
137  QStringList() << "text/x-python" // mimetypes
138  )
139  );
140  }
141 #endif
142 
143 #ifdef KROSS_RUBY_LIBRARY
144  if( void* funcPtr = loadLibrary(KROSS_RUBY_LIBRARY, "krossinterpreter") ) {
145  InterpreterInfo::Option::Map options;
146  options.insert("safelevel", new InterpreterInfo::Option(
147  i18n("Level of safety of the Ruby interpreter"),
148  QVariant(0) )); // 0 -> unsafe, 4 -> very safe
149  d->interpreterinfos.insert("ruby",
150  new InterpreterInfo("ruby",
151  funcPtr, // library
152  "*.rb", // file filter-wildcard
153  QStringList() << /* "text/x-ruby" << */ "application/x-ruby", // mimetypes
154  options // options
155  )
156  );
157  }
158 #endif
159 
160 #ifdef KROSS_JAVA_LIBRARY
161  if( void* funcPtr = loadLibrary(KROSS_JAVA_LIBRARY, "krossinterpreter") ) {
162  d->interpreterinfos.insert("java",
163  new InterpreterInfo("java",
164  funcPtr, // library
165  "*.java *.class *.jar", // file filter-wildcard
166  QStringList() << "application/java" // mimetypes
167  )
168  );
169  }
170 #endif
171 
172 #ifdef KROSS_KJS_LIBRARY
173  if( void* funcPtr = loadLibrary(KROSS_KJS_LIBRARY, "krossinterpreter") ) {
174  d->interpreterinfos.insert("javascript",
175  new InterpreterInfo("javascript",
176  funcPtr, // library
177  "*.js", // file filter-wildcard
178  QStringList() << "application/javascript" // mimetypes
179  )
180  );
181  }
182 #endif
183 
184 #ifdef KROSS_FALCON_LIBRARY
185  if( void* funcPtr = loadLibrary(KROSS_FALCON_LIBRARY, "krossinterpreter") ) {
186  d->interpreterinfos.insert("falcon",
187  new InterpreterInfo("falcon",
188  funcPtr, // library
189  "*.fal", // file filter-wildcard
190  QStringList() << "application/x-falcon" // mimetypes
191  )
192  );
193  }
194 #endif
195 
196 #ifdef KROSS_QTSCRIPT_LIBRARY
197  if( void* funcPtr = loadLibrary(KROSS_QTSCRIPT_LIBRARY, "krossinterpreter") ) {
198  d->interpreterinfos.insert("qtscript",
199  new InterpreterInfo("qtscript",
200  funcPtr, // library
201  "*.es", // file filter-wildcard
202  QStringList() << "application/ecmascript" // mimetypes
203  )
204  );
205  }
206 #endif
207 
208 #ifdef KROSS_LUA_LIBRARY
209  if( void* funcPtr = loadLibrary(KROSS_LUA_LIBRARY, "krossinterpreter") ) {
210  d->interpreterinfos.insert("lua",
211  new InterpreterInfo("lua",
212  funcPtr, // library
213  "*.lua *.luac", // file filter-wildcard
214  QStringList() << "application/x-lua" // mimetypes
215  )
216  );
217  }
218 #endif
219 
220  // fill the list of supported interpreternames.
221  QHash<QString, InterpreterInfo*>::Iterator it( d->interpreterinfos.begin() );
222  for(; it != d->interpreterinfos.end(); ++it)
223  if( it.value() )
224  d->interpreters << it.key();
225  d->interpreters.sort();
226 
227  // publish ourself.
228  ChildrenInterface::addObject(this, "Kross");
229 }
230 
231 Manager::~Manager()
232 {
233  qDeleteAll(d->wrappers);
234  qDeleteAll(d->interpreterinfos);
235  qDeleteAll(d->modules);
236  delete d->collection;
237  delete d;
238 }
239 
240 QHash< QString, InterpreterInfo* > Manager::interpreterInfos() const
241 {
242  return d->interpreterinfos;
243 }
244 
245 bool Manager::hasInterpreterInfo(const QString& interpretername) const
246 {
247  return d->interpreterinfos.contains(interpretername) && d->interpreterinfos[interpretername];
248 }
249 
250 InterpreterInfo* Manager::interpreterInfo(const QString& interpretername) const
251 {
252  return hasInterpreterInfo(interpretername) ? d->interpreterinfos[interpretername] : 0;
253 }
254 
255 const QString Manager::interpreternameForFile(const QString& file)
256 {
257  QRegExp rx;
258  rx.setPatternSyntax(QRegExp::Wildcard);
259  for(QHash<QString, InterpreterInfo*>::Iterator it = d->interpreterinfos.begin(); it != d->interpreterinfos.end(); ++it) {
260  if( ! it.value() )
261  continue;
262  foreach(const QString &wildcard, it.value()->wildcard().split(' ', QString::SkipEmptyParts)) {
263  rx.setPattern( wildcard );
264  if( rx.exactMatch(file) )
265  return it.value()->interpreterName();
266  }
267  }
268  return QString();
269 }
270 
271 Interpreter* Manager::interpreter(const QString& interpretername) const
272 {
273  if( ! hasInterpreterInfo(interpretername) ) {
274  krosswarning( QString("No such interpreter '%1'").arg(interpretername) );
275  return 0;
276  }
277  return d->interpreterinfos[interpretername]->interpreter();
278 }
279 
280 QStringList Manager::interpreters() const
281 {
282  return d->interpreters;
283 }
284 
285 ActionCollection* Manager::actionCollection() const
286 {
287  return d->collection;
288 }
289 
290 bool Manager::hasAction(const QString& name)
291 {
292  return findChild< Action* >(name) != 0L;
293 }
294 
295 QObject* Manager::action(const QString& name)
296 {
297  Action* action = findChild< Action* >(name);
298  if(! action) {
299  action = new Action(this, name);
300 #if 0
301  d->actioncollection->insert(action); //FIXME should we really remember the action?
302 #endif
303  }
304  return action;
305 }
306 
307 QObject* Manager::module(const QString& modulename)
308 {
309  if( d->modules.contains(modulename) ) {
310  QObject* obj = d->modules[modulename];
311  if( obj )
312  return obj;
313  }
314 
315  if( modulename.isEmpty() || modulename.contains( QRegExp("[^a-zA-Z0-9]") ) ) {
316  krosswarning( QString("Invalid module name '%1'").arg(modulename) );
317  return 0;
318  }
319 
320  QByteArray libraryname = QString("krossmodule%1").arg(modulename).toLower().toLatin1();
321 
322 #if 0
323  KLibLoader* loader = KLibLoader::self();
324  KLibrary* lib = loader->library( libraryname, QLibrary::ExportExternalSymbolsHint );
325  if( ! lib ) { //FIXME this fallback-code should be in KLibLoader imho.
326  lib = loader->library( QString("lib%1").arg(libraryname), QLibrary::ExportExternalSymbolsHint );
327  if( ! lib ) {
328  krosswarning( QString("Failed to load module '%1': %2").arg(modulename).arg(loader->lastErrorMessage()) );
329  return 0;
330  }
331  }
332 
333  def_module_func func;
334  func = (def_module_func) lib->resolveFunction("krossmodule");
335  if( ! func ) {
336  krosswarning( QString("Failed to determinate init function in module '%1'").arg(modulename) );
337  return 0;
338  }
339 
340  QObject* module = (QObject*) (func)(); // call the function
341  lib->unload(); // unload the library
342 
343  if( ! module ) {
344  krosswarning( QString("Failed to load module object '%1'").arg(modulename) );
345  return 0;
346  }
347 #else
348  if( void* funcPtr = loadLibrary(libraryname, "krossmodule") ) {
349  def_module_func func = (def_module_func) funcPtr;
350  Q_ASSERT( func );
351  QObject* module = (QObject*) (func)(); // call the function
352  Q_ASSERT( module );
353  //krossdebug( QString("Manager::module Module successfully loaded: modulename=%1 module.objectName=%2 module.className=%3").arg(modulename).arg(module->objectName()).arg(module->metaObject()->className()) );
354  d->modules.insert(modulename, module);
355  return module;
356  }
357  else {
358  krosswarning( QString("Failed to load module '%1'").arg(modulename) );
359  }
360 #endif
361  return 0;
362 }
363 
364 void Manager::deleteModules()
365 {
366  qDeleteAll(d->modules);
367  d->modules.clear();
368 }
369 
370 bool Manager::executeScriptFile(const QUrl& file)
371 {
372  krossdebug( QString("Manager::executeScriptFile() file='%1'").arg(file.toString()) );
373  Action* action = new Action(0 /*no parent*/, file);
374  action->trigger();
375  bool ok = ! action->hadError();
376  delete action; //action->delayedDestruct();
377  return ok;
378 }
379 
380 void Manager::addQObject(QObject* obj, const QString &name)
381 {
382  this->addObject(obj, name);
383 }
384 
385 QObject* Manager::qobject(const QString &name) const
386 {
387  return this->object(name);
388 }
389 
390 QStringList Manager::qobjectNames() const
391 {
392  return this->objects().keys();
393 }
394 
395 MetaTypeHandler* Manager::metaTypeHandler(const QByteArray& typeName) const
396 {
397  return d->wrappers.contains(typeName) ? d->wrappers[typeName] : 0;
398 }
399 
400 void Manager::registerMetaTypeHandler(const QByteArray& typeName, MetaTypeHandler::FunctionPtr* handler)
401 {
402  d->wrappers.insert(typeName, new MetaTypeHandler(handler));
403 }
404 
405 void Manager::registerMetaTypeHandler(const QByteArray& typeName, MetaTypeHandler::FunctionPtr2* handler)
406 {
407  d->wrappers.insert(typeName, new MetaTypeHandler(handler));
408 }
409 
410 void Manager::registerMetaTypeHandler(const QByteArray& typeName, MetaTypeHandler* handler)
411 {
412  d->wrappers.insert(typeName, handler);
413 }
414 
415 bool Manager::strictTypesEnabled() const
416 {
417  return d->strictTypesEnabled;
418 }
419 
420 void Manager::setStrictTypesEnabled(bool enabled)
421 {
422  d->strictTypesEnabled = enabled;
423 }
424 
425 bool Manager::hasHandlerAssigned(const QByteArray& typeName) const
426 {
427  return d->wrappers.contains(typeName);
428 }
429 
430 #include "manager.moc"
QVariant
i18n
QString i18n(const char *text)
KLibLoader::library
KLibrary * library(const QString &libname, QLibrary::LoadHints loadHint=0)
Kross::Manager::strictTypesEnabled
bool strictTypesEnabled() const
Returns true if strict type handling is enabled.
Definition: manager.cpp:415
Kross::ErrorInterface::hadError
bool hadError() const
Definition: errorinterface.h:48
Kross::ChildrenInterface::addObject
void addObject(QObject *object, const QString &name=QString(), Options options=NoOption)
Add a QObject to the list of children.
Definition: childreninterface.h:80
Kross::MetaTypeHandler::FunctionPtr
QVariant( FunctionPtr)(void *)
Definition: metatype.h:141
Kross::Manager::qobjectNames
QStringList qobjectNames() const
Definition: manager.cpp:390
def_module_func
QObject *(* def_module_func)()
Definition: manager.cpp:39
KROSS_QTSCRIPT_LIBRARY
#define KROSS_QTSCRIPT_LIBRARY
Definition: krossconfig.h:85
Kross::InterpreterInfo::Option
Each interpreter is able to define options we could use to manipulate the interpreter behaviour...
Definition: core/interpreter.h:51
Kross::Manager::module
QObject * module(const QString &modulename)
Load and return an external module.
Definition: manager.cpp:307
Kross::Manager::~Manager
virtual ~Manager()
Destructor.
Definition: manager.cpp:231
KLibLoader::lastErrorMessage
QString lastErrorMessage() const
Kross::MetaTypeHandler::FunctionPtr2
QVariant( FunctionPtr2)(MetaTypeHandler *handler, void *)
Definition: metatype.h:142
QUrl
Kross::Manager::interpreterInfo
InterpreterInfo * interpreterInfo(const QString &interpretername) const
Definition: manager.cpp:250
QString
QHash
Kross::Manager::addQObject
void addQObject(QObject *obj, const QString &name=QString())
Definition: manager.cpp:380
QObject
klocale.h
Kross::Manager::setStrictTypesEnabled
void setStrictTypesEnabled(bool enabled)
Enable more strict type handling.
Definition: manager.cpp:420
Kross::InterpreterInfo
The InterpreterInfo class provides abstract information about a Interpreter before the interpreter-ba...
Definition: core/interpreter.h:43
Kross::Manager::qobject
QObject * qobject(const QString &name) const
Definition: manager.cpp:385
KROSS_KJS_LIBRARY
#define KROSS_KJS_LIBRARY
Definition: krossconfig.h:82
interpreter.h
QScriptable
Kross::Manager::executeScriptFile
bool executeScriptFile(const QUrl &file=QUrl())
Execute a script file.
Definition: manager.cpp:370
Kross::ChildrenInterface::objects
QHash< QString, QObject * > objects() const
Definition: childreninterface.h:104
action.h
KStandardDirs
QStringList
Kross::ChildrenInterface
Interface for managing Object collections.
Definition: childreninterface.h:38
KROSS_RUBY_LIBRARY
#define KROSS_RUBY_LIBRARY
Definition: krossconfig.h:81
KLibrary
Kross::Manager::interpreternameForFile
const QString interpreternameForFile(const QString &file)
Return the name of the Interpreter that feels responsible for the defined file .
Definition: manager.cpp:255
Kross::Manager::hasAction
bool hasAction(const QString &name)
Definition: manager.cpp:290
KROSS_LUA_LIBRARY
#define KROSS_LUA_LIBRARY
Definition: krossconfig.h:86
KROSS_FALCON_LIBRARY
#define KROSS_FALCON_LIBRARY
Definition: krossconfig.h:84
Kross::Manager::registerMetaTypeHandler
void registerMetaTypeHandler(const QByteArray &typeName, MetaTypeHandler::FunctionPtr *handler)
Register a handler for custom types.
Definition: manager.cpp:400
manager.h
Kross::Manager::actionCollection
ActionCollection * actionCollection() const
Definition: manager.cpp:285
Kross::Manager::action
QObject * action(const QString &name)
Definition: manager.cpp:295
Kross::Interpreter
Base class for interpreter implementations.
Definition: core/interpreter.h:177
Kross::krosswarning
void krosswarning(const QString &s)
Warning function.
Definition: krossconfig.cpp:34
loadLibrary
void * loadLibrary(const char *libname, const char *functionname)
Definition: manager.cpp:78
Kross::Manager::interpreter
Interpreter * interpreter(const QString &interpretername) const
Return the Interpreter instance defined by the interpretername.
Definition: manager.cpp:271
KStandardDirs::resourceDirs
QStringList resourceDirs(const char *type) const
ok
KGuiItem ok()
Kross::ChildrenInterface::object
QObject * object(const QString &name) const
Definition: childreninterface.h:97
KLibLoader::self
static KLibLoader * self()
KLibrary::resolveFunction
void_function_ptr resolveFunction(const char *name)
KLibLoader
Kross::MetaTypeHandler
Base class for metatype-handlers as used returned by the Kross::Manager::metaTypeHandler() method...
Definition: metatype.h:138
Kross::Manager::interpreters
QStringList interpreters() const
Definition: manager.cpp:280
kstandarddirs.h
KROSS_JAVA_LIBRARY
#define KROSS_JAVA_LIBRARY
Definition: krossconfig.h:83
Kross::Manager::metaTypeHandler
MetaTypeHandler * metaTypeHandler(const QByteArray &typeName) const
Definition: manager.cpp:395
Kross::Manager::hasInterpreterInfo
bool hasInterpreterInfo(const QString &interpretername) const
Definition: manager.cpp:245
Kross::Manager::interpreterInfos
QHash< QString, InterpreterInfo * > interpreterInfos() const
Definition: manager.cpp:240
Kross::Action
The Action class is an abstract container to deal with scripts like a single standalone script file...
Definition: action.h:94
KLibrary::unload
bool unload()
Kross::ActionCollection
The ActionCollection class manages collections of Action instances.
Definition: actioncollection.h:45
Kross::Manager::hasHandlerAssigned
bool hasHandlerAssigned(const QByteArray &typeName) const
Definition: manager.cpp:425
actioncollection.h
Kross::krossdebug
void krossdebug(const QString &s)
Debugging function.
Definition: krossconfig.cpp:28
KROSS_PYTHON_LIBRARY
#define KROSS_PYTHON_LIBRARY
Definition: krossconfig.h:80
Kross::Manager::deleteModules
void deleteModules()
External modules are dynamically loadable and are normally deleted when the kross library is unloaded...
Definition: manager.cpp:364
QMap< QString, Option * >
Kross::Manager::Manager
Manager()
The constructor.
Definition: manager.cpp:121
QLibrary
Kross::Manager
The Manager class is a singleton that provides the main entry point to deal with the Kross Scripting ...
Definition: manager.h:49
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:54 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kross

Skip menu "Kross"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal