00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "manager.h"
00021 #include "interpreter.h"
00022 #include "action.h"
00023 #include "actioncollection.h"
00024
00025 #include <QObject>
00026 #include <QMetaObject>
00027 #include <QFile>
00028 #include <QRegExp>
00029 #include <QAbstractItemModel>
00030 #include <QFileInfo>
00031
00032 #include <kapplication.h>
00033 #include <kactioncollection.h>
00034 #include <klibloader.h>
00035 #include <klocale.h>
00036 #include <kstaticdeleter.h>
00037 #include <kdialog.h>
00038 #include <kicon.h>
00039 #include <kconfig.h>
00040 #include <kmenu.h>
00041 #include <kstandarddirs.h>
00042 #include <kmimetype.h>
00043 #include <kmenu.h>
00044
00045 extern "C"
00046 {
00047 typedef QObject* (*def_module_func)();
00048 }
00049
00050 using namespace Kross;
00051
00052 namespace Kross {
00053
00055 class Manager::Private
00056 {
00057 public:
00059 QHash< QString, InterpreterInfo* > interpreterinfos;
00060
00062 QStringList interpreters;
00063
00065 QHash< QString, QPointer<QObject> > modules;
00066
00068 ActionCollection* collection;
00069 };
00070
00071 }
00072
00073 static KStaticDeleter<Manager> m_manager;
00074 static Manager* _self = 0;
00075
00076 Manager& Manager::self()
00077 {
00078 if(! _self)
00079 m_manager.setObject(_self, new Manager());
00080 return *_self;
00081 }
00082
00083 Manager::Manager()
00084 : QObject()
00085 , ChildrenInterface()
00086 , d( new Private() )
00087 {
00088 d->collection = new ActionCollection("main");
00089
00090 #ifdef KROSS_PYTHON_LIBRARY
00091 QString pythonlib = QFile::encodeName( KLibLoader::self()->findLibrary(KROSS_PYTHON_LIBRARY) );
00092 if(! pythonlib.isEmpty()) {
00093 InterpreterInfo::Option::Map pythonoptions;
00094 d->interpreterinfos.insert("python",
00095 new InterpreterInfo("python",
00096 pythonlib,
00097 "*.py",
00098 QStringList() << "application/x-python",
00099 pythonoptions
00100 )
00101 );
00102 } else {
00103 #ifdef KROSS_INTERPRETER_DEBUG
00104 krossdebug("Python interpreter for kross is unavailable");
00105 #endif
00106 }
00107 #endif
00108
00109 #ifdef KROSS_RUBY_LIBRARY
00110 QString rubylib = QFile::encodeName( KLibLoader::self()->findLibrary(KROSS_RUBY_LIBRARY) );
00111 if(! rubylib.isEmpty()) {
00112 InterpreterInfo::Option::Map rubyoptions;
00113 rubyoptions.insert("safelevel",
00114 new InterpreterInfo::Option(
00115 i18n("Level of safety of the Ruby interpreter"),
00116 QVariant(0)
00117 )
00118 );
00119 d->interpreterinfos.insert("ruby",
00120 new InterpreterInfo("ruby",
00121 rubylib,
00122 "*.rb",
00123 QStringList() << "application/x-ruby",
00124 rubyoptions
00125 )
00126 );
00127 } else {
00128 #ifdef KROSS_INTERPRETER_DEBUG
00129 krossdebug("Ruby interpreter for kross is unavailable");
00130 #endif
00131 }
00132 #endif
00133
00134 #ifdef KROSS_KJS_LIBRARY
00135 QString kjslib = QFile::encodeName( KLibLoader::self()->findLibrary(KROSS_KJS_LIBRARY) );
00136 if(! kjslib.isEmpty()) {
00137 InterpreterInfo::Option::Map kjsoptions;
00138 kjsoptions.insert("restricted",
00139 new InterpreterInfo::Option(
00140 i18n("Restricted mode for untrusted scripts"),
00141 QVariant(true)
00142 )
00143 );
00144 d->interpreterinfos.insert("javascript",
00145 new InterpreterInfo("javascript",
00146 kjslib,
00147 "*.js",
00148 QStringList() << "application/x-javascript",
00149 kjsoptions
00150 )
00151 );
00152 } else {
00153 #ifdef KROSS_INTERPRETER_DEBUG
00154 krossdebug("KDE JavaScript interpreter for kross is unavailable");
00155 #endif
00156 }
00157 #endif
00158
00159
00160 QHash<QString, InterpreterInfo*>::Iterator it( d->interpreterinfos.begin() );
00161 for(; it != d->interpreterinfos.end(); ++it)
00162 d->interpreters << it.key();
00163
00164
00165
00166 ChildrenInterface::addObject(this, "Kross");
00167 }
00168
00169 Manager::~Manager()
00170 {
00171 for(QHash<QString, InterpreterInfo* >::Iterator it = d->interpreterinfos.begin(); it != d->interpreterinfos.end(); ++it)
00172 delete it.value();
00173 for(QHash<QString, QPointer<QObject> >::Iterator it = d->modules.begin(); it != d->modules.end(); ++it)
00174 delete it.value();
00175 delete d->collection;
00176 delete d;
00177 }
00178
00179 QHash< QString, InterpreterInfo* > Manager::interpreterInfos() const
00180 {
00181 return d->interpreterinfos;
00182 }
00183
00184 bool Manager::hasInterpreterInfo(const QString& interpretername) const
00185 {
00186 return d->interpreterinfos.contains(interpretername);
00187 }
00188
00189 InterpreterInfo* Manager::interpreterInfo(const QString& interpretername) const
00190 {
00191 return d->interpreterinfos[interpretername];
00192 }
00193
00194 const QString Manager::interpreternameForFile(const QString& file)
00195 {
00196 QRegExp rx;
00197 rx.setPatternSyntax(QRegExp::Wildcard);
00198 for(QHash<QString, InterpreterInfo*>::Iterator it = d->interpreterinfos.begin(); it != d->interpreterinfos.end(); ++it) {
00199 rx.setPattern((*it)->wildcard());
00200 if( file.contains(rx) )
00201 return (*it)->interpreterName();
00202 }
00203 return QString::null;
00204 }
00205
00206 Interpreter* Manager::interpreter(const QString& interpretername) const
00207 {
00208 if(! d->interpreterinfos.contains(interpretername)) {
00209 krosswarning( QString("No such interpreter '%1'").arg(interpretername) );
00210 return 0;
00211 }
00212 return d->interpreterinfos[interpretername]->interpreter();
00213 }
00214
00215 QStringList Manager::interpreters() const
00216 {
00217 return d->interpreters;
00218 }
00219
00220 ActionCollection* Manager::actionCollection() const
00221 {
00222 return d->collection;
00223 }
00224
00225 bool Manager::hasAction(const QString& name)
00226 {
00227 return findChild< Action* >(name) != 0L;
00228 }
00229
00230 QObject* Manager::action(const QString& name)
00231 {
00232 Action* action = findChild< Action* >(name);
00233 if(! action) {
00234 action = new Action(name);
00235 action->setParent(this);
00236 #if 0
00237 d->actioncollection->insert(action);
00238 #endif
00239 }
00240 return action;
00241 }
00242
00243 QObject* Manager::module(const QString& modulename)
00244 {
00245 if( d->modules.contains(modulename) ) {
00246 QObject* obj = d->modules[modulename];
00247 if( obj )
00248 return obj;
00249 }
00250
00251 if( modulename.isEmpty() || modulename.contains( QRegExp("[^a-zA-Z0-9]") ) ) {
00252 krosswarning( QString("Invalid module name '%1'").arg(modulename) );
00253 return 0;
00254 }
00255
00256 QByteArray libraryname = QString("krossmodule%1").arg(modulename).toLower().toLatin1();
00257
00258 KLibLoader* loader = KLibLoader::self();
00259 KLibrary* lib = loader->globalLibrary( libraryname );
00260 if( ! lib ) {
00261 krosswarning( QString("Failed to load module '%1': %2").arg(modulename).arg(loader->lastErrorMessage()) );
00262 return 0;
00263 }
00264
00265 def_module_func func;
00266 func = (def_module_func) lib->symbol("krossmodule");
00267 if( ! func ) {
00268 krosswarning( QString("Failed to determinate init function in module '%1'").arg(modulename) );
00269 return 0;
00270 }
00271
00272 QObject* module = (QObject*) (func)();
00273 lib->unload();
00274
00275 if( ! module ) {
00276 krosswarning( QString("Failed to load module object '%1'").arg(modulename) );
00277 return 0;
00278 }
00279
00280
00281 d->modules.insert(modulename, module);
00282 return module;
00283 }
00284
00285 #include "manager.moc"