lib

scriptcontainer.cpp

00001 /***************************************************************************
00002  * scriptcontainer.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 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 "scriptcontainer.h"
00021 #include "../api/object.h"
00022 #include "../api/list.h"
00023 #include "../api/interpreter.h"
00024 #include "../api/script.h"
00025 #include "../main/manager.h"
00026 #include "mainmodule.h"
00027 
00028 #include <qfile.h>
00029 
00030 #include <klocale.h>
00031 
00032 using namespace Kross::Api;
00033 
00034 namespace Kross { namespace Api {
00035 
00037     class ScriptContainerPrivate
00038     {
00039         public:
00040 
00047             Script* script;
00048 
00053             QString name;
00054 
00058             QString code;
00059 
00065             QString interpretername;
00066 
00074             QString scriptfile;
00075 
00080             QMap<QString, QVariant> options;
00081 
00082     };
00083 
00084 }}
00085 
00086 ScriptContainer::ScriptContainer(const QString& name)
00087     : MainModule(name)
00088     , d( new ScriptContainerPrivate() ) // initialize d-pointer class
00089 {
00090     kdDebug() << QString("ScriptContainer::ScriptContainer() Ctor name='%1'").arg(name) << endl;
00091 
00092     d->script = 0;
00093     d->name = name;
00094 }
00095 
00096 ScriptContainer::~ScriptContainer()
00097 {
00098     kdDebug() << QString("ScriptContainer::~ScriptContainer() Dtor name='%1'").arg(d->name) << endl;
00099 
00100     finalize();
00101     delete d;
00102 }
00103 
00104 const QString& ScriptContainer::getName() const
00105 {
00106     return d->name;
00107 }
00108 
00109 void ScriptContainer::setName(const QString& name)
00110 {
00111     d->name = name;
00112 }
00113 
00114 const QString& ScriptContainer::getCode() const
00115 {
00116     return d->code;
00117 }
00118 
00119 void ScriptContainer::setCode(const QString& code)
00120 {
00121     finalize();
00122     d->code = code;
00123 }
00124 
00125 const QString& ScriptContainer::getInterpreterName() const
00126 {
00127     return d->interpretername;
00128 }
00129 
00130 void ScriptContainer::setInterpreterName(const QString& interpretername)
00131 {
00132     finalize();
00133     d->interpretername = interpretername;
00134 }
00135 
00136 const QString& ScriptContainer::getFile() const
00137 {
00138     return d->scriptfile;
00139 }
00140 
00141 void ScriptContainer::setFile(const QString& scriptfile)
00142 {
00143     finalize();
00144     d->scriptfile = scriptfile;
00145 }
00146 
00147 QMap<QString, QVariant>& ScriptContainer::getOptions()
00148 {
00149     return d->options;
00150 }
00151 
00152 const QVariant& ScriptContainer::getOption(const QString name, QVariant defaultvalue, bool /*recursive*/)
00153 {
00154     if(d->options.contains(name))
00155         return d->options[name];
00156     Kross::Api::InterpreterInfo* info = Kross::Api::Manager::scriptManager()->getInterpreterInfo( d->interpretername );
00157     return info ? info->getOptionValue(name, defaultvalue) : defaultvalue;
00158 }
00159 
00160 bool ScriptContainer::setOption(const QString name, const QVariant& value)
00161 {
00162     Kross::Api::InterpreterInfo* info = Kross::Api::Manager::scriptManager()->getInterpreterInfo( d->interpretername );
00163     if(info) {
00164         if(info->hasOption(name)) {
00165             d->options.replace(name, value);
00166             return true;
00167         } else kdWarning() << QString("Kross::Api::ScriptContainer::setOption(%1, %2): No such option").arg(name).arg(value.toString()) << endl;
00168     } else kdWarning() << QString("Kross::Api::ScriptContainer::setOption(%1, %2): No such interpreterinfo").arg(name).arg(value.toString()) << endl;
00169     return false;
00170 }
00171 
00172 Object::Ptr ScriptContainer::execute()
00173 {
00174     if(! d->script)
00175         if(! initialize())
00176             return 0;
00177 
00178     if(hadException())
00179         return 0;
00180 
00181     Object::Ptr r = d->script->execute();
00182     if(d->script->hadException()) {
00183         setException( d->script->getException() );
00184         finalize();
00185         return 0;
00186     }
00187     return r;
00188 }
00189 
00190 const QStringList ScriptContainer::getFunctionNames()
00191 {
00192     return d->script ? d->script->getFunctionNames() : QStringList(); //FIXME init before if needed?
00193 }
00194 
00195 Object::Ptr ScriptContainer::callFunction(const QString& functionname, List::Ptr arguments)
00196 {
00197     if(! d->script)
00198         if(! initialize())
00199             return 0;
00200 
00201     if(hadException())
00202         return 0;
00203 
00204     if(functionname.isEmpty()) {
00205         setException( new Exception(QString(i18n("No functionname defined for ScriptContainer::callFunction()."))) );
00206         finalize();
00207         return 0;
00208     }
00209 
00210     Object::Ptr r = d->script->callFunction(functionname, arguments);
00211     if(d->script->hadException()) {
00212         setException( d->script->getException() );
00213         finalize();
00214         return 0;
00215     }
00216     return r;
00217 }
00218 
00219 const QStringList ScriptContainer::getClassNames()
00220 {
00221     return d->script ? d->script->getClassNames() : QStringList(); //FIXME init before if needed?
00222 }
00223 
00224 Object::Ptr ScriptContainer::classInstance(const QString& classname)
00225 {
00226     if(! d->script)
00227         if(! initialize())
00228             return 0;
00229 
00230     if(hadException())
00231         return 0;
00232 
00233     Object::Ptr r = d->script->classInstance(classname);
00234     if(d->script->hadException()) {
00235         setException( d->script->getException() );
00236         finalize();
00237         return 0;
00238     }
00239     return r;
00240 }
00241 
00242 bool ScriptContainer::initialize()
00243 {
00244     finalize();
00245 
00246     if(! d->scriptfile.isNull()) {
00247         kdDebug() << QString("Kross::Api::ScriptContainer::initialize() file=%1").arg(d->scriptfile) << endl;
00248 
00249         if(d->interpretername.isNull()) {
00250             d->interpretername = Manager::scriptManager()->getInterpreternameForFile( d->scriptfile );
00251             if(d->interpretername.isNull()) {
00252                 setException( new Exception(QString(i18n("Failed to determinate interpreter for scriptfile '%1'")).arg(d->scriptfile)) );
00253                 return false;
00254             }
00255         }
00256 
00257         QFile f( d->scriptfile );
00258         if(! f.open(IO_ReadOnly)) {
00259             setException( new Exception(QString(i18n("Failed to open scriptfile '%1'")).arg(d->scriptfile)) );
00260             return false;
00261         }
00262         d->code = QString( f.readAll() );
00263         f.close();
00264     }
00265 
00266     Interpreter* interpreter = Manager::scriptManager()->getInterpreter(d->interpretername);
00267     if(! interpreter) {
00268         setException( new Exception(QString(i18n("Unknown interpreter '%1'")).arg(d->interpretername)) );
00269         return false;
00270     }
00271 
00272     d->script = interpreter->createScript(this);
00273     if(! d->script) {
00274         setException( new Exception(QString(i18n("Failed to create script for interpreter '%1'")).arg(d->interpretername)) );
00275         return false;
00276     }
00277     if(d->script->hadException()) {
00278         setException( d->script->getException() );
00279         finalize();
00280         return false;
00281     }
00282     setException( 0 ); // clear old exception
00283 
00284     return true;
00285 }
00286 
00287 void ScriptContainer::finalize()
00288 {
00289     delete d->script;
00290     d->script = 0;
00291 }
00292 
00293 
KDE Home | KDE Accessibility Home | Description of Access Keys