F:/KPlato/koffice/libs/kross/core/manager.cpp

Aller à la documentation de ce fichier.
00001 /***************************************************************************
00002  * manager.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2006 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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()) { // If the Kross Python plugin exists we offer is as supported scripting language.
00093         InterpreterInfo::Option::Map pythonoptions;
00094         d->interpreterinfos.insert("python",
00095             new InterpreterInfo("python",
00096                 pythonlib, // library
00097                 "*.py", // file filter-wildcard
00098                 QStringList() << /* "text/x-python" << */ "application/x-python", // mimetypes
00099                 pythonoptions // options
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()) { // If the Kross Ruby plugin exists we offer is as supported scripting language.
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) // 0 -> unsafe, 4 -> very safe
00117             )
00118         );
00119         d->interpreterinfos.insert("ruby",
00120             new InterpreterInfo("ruby",
00121                 rubylib, // library
00122                 "*.rb", // file filter-wildcard
00123                 QStringList() << /* "text/x-ruby" << */ "application/x-ruby", // mimetypes
00124                 rubyoptions // options
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()) { // If the Kjs plugin exists we offer is as supported scripting language.
00137         InterpreterInfo::Option::Map kjsoptions;
00138         kjsoptions.insert("restricted",
00139             new InterpreterInfo::Option(
00140                 i18n("Restricted mode for untrusted scripts"),
00141                 QVariant(true) // per default enabled
00142             )
00143         );
00144         d->interpreterinfos.insert("javascript",
00145             new InterpreterInfo("javascript",
00146                 kjslib, // library
00147                 "*.js", // file filter-wildcard
00148                 QStringList() << "application/x-javascript", // mimetypes
00149                 kjsoptions // options
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     // fill the list of supported interpreternames.
00160     QHash<QString, InterpreterInfo*>::Iterator it( d->interpreterinfos.begin() );
00161     for(; it != d->interpreterinfos.end(); ++it)
00162         d->interpreters << it.key();
00163     //d->interpreters.sort();
00164 
00165     // publish ourself.
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); //FIXME should we really remember the 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)(); // call the function
00273     lib->unload(); // unload the library
00274 
00275     if( ! module ) {
00276         krosswarning( QString("Failed to load module object '%1'").arg(modulename) );
00277         return 0;
00278     }
00279 
00280     //krossdebug( QString("Manager::module Module successfully loaded: modulename=%1 module.objectName=%2 module.className=%3").arg(modulename).arg(module->objectName()).arg(module->metaObject()->className()) );
00281     d->modules.insert(modulename, module);
00282     return module;
00283 }
00284 
00285 #include "manager.moc"

Généré le Wed Nov 22 23:41:11 2006 pour KPlato par  doxygen 1.5.1-p1