00001 /*************************************************************************** 00002 * scriptcontainer.h 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 #ifndef KROSS_API_SCRIPTCONTAINER_H 00021 #define KROSS_API_SCRIPTCONTAINER_H 00022 00023 #include "mainmodule.h" 00024 00025 #include <qstring.h> 00026 #include <qvariant.h> 00027 #include <qobject.h> 00028 //#include <kdebug.h> 00029 #include <ksharedptr.h> 00030 00031 namespace Kross { namespace Api { 00032 00033 // Forward declarations. 00034 class Object; 00035 class List; 00036 class ScriptContainerPrivate; 00037 00038 /** 00039 * The ScriptContainer class is something like a single 00040 * standalone scriptfile. 00041 * 00042 * Once you've such a ScriptContainer instance you're 00043 * able to perform actions with it like to execute 00044 * scripting code. The \a Manager takes care of 00045 * handling the ScriptContainer instances application 00046 * width. 00047 * 00048 * The class \a ScriptAction provides a higher level class 00049 * to work with a \a ScriptContainer instances. 00050 */ 00051 class ScriptContainer : public MainModule 00052 { 00053 // We protected the constructor cause ScriptContainer 00054 // instances should be created only within the 00055 // Manager::getScriptContainer() method. 00056 friend class Manager; 00057 00058 protected: 00059 00060 /** 00061 * Constructor. 00062 * 00063 * The constructor is protected cause only with the 00064 * \a ScriptManager it's possible to access 00065 * \a ScriptContainer instances. 00066 * 00067 * \param name The unique name this ScriptContainer 00068 * has. It's used e.g. at the \a Manager to 00069 * identify the ScriptContainer. 00070 */ 00071 explicit ScriptContainer(const QString& name = QString::null); 00072 00073 public: 00074 00075 /// Shared pointer to implement reference-counting. 00076 typedef KSharedPtr<ScriptContainer> Ptr; 00077 00078 /** 00079 * Destructor. 00080 */ 00081 virtual ~ScriptContainer(); 00082 00083 /** 00084 * Return the unique name this ScriptContainer is 00085 * reachable as. 00086 */ 00087 const QString& getName() const; 00088 00089 /** 00090 * Set the name this ScriptContainer is reachable as. 00091 */ 00092 void setName(const QString& name); 00093 00094 /** 00095 * Return the scriptcode this ScriptContainer holds. 00096 */ 00097 const QString& getCode() const; 00098 00099 /** 00100 * Set the scriptcode this ScriptContainer holds. 00101 */ 00102 void setCode(const QString& code); 00103 00104 /** 00105 * \return the name of the interpreter used 00106 * on \a execute. 00107 */ 00108 const QString& getInterpreterName() const; 00109 00110 /** 00111 * Set the name of the interpreter used 00112 * on \a execute. 00113 */ 00114 void setInterpreterName(const QString& interpretername); 00115 00116 /** 00117 * \return the filename which will be executed 00118 * on \a execute. 00119 */ 00120 const QString& getFile() const; 00121 00122 /** 00123 * Set the filename which will be executed 00124 * on \a execute. The \p scriptfile needs to 00125 * be a valid local file or QString::null if 00126 * you don't like to use a file rather then 00127 * the with \a setCode() defined scripting code. 00128 */ 00129 void setFile(const QString& scriptfile); 00130 00131 /** 00132 * \return a map of options this \a ScriptContainer defines. 00133 * The options are returned call-by-ref, so you are able to 00134 * manipulate them. 00135 */ 00136 QMap<QString, QVariant>& getOptions(); 00137 00138 /** 00139 * \return the value of the option defined with \p name . 00140 * If there doesn't exists an option with such a name, 00141 * the \p defaultvalue is returned. If \p recursive is 00142 * true then first the \a ScriptContainer options are 00143 * seeked for the matching \p name and if not found 00144 * the \a Manager options are seeked for the \p name and 00145 * if not found either the \p defaultvalue is returned. 00146 */ 00147 const QVariant& getOption(const QString name, QVariant defaultvalue = QVariant(), bool recursive = false); 00148 00149 /** 00150 * Set the \a Interpreter::Option value. 00151 */ 00152 bool setOption(const QString name, const QVariant& value); 00153 00154 /** 00155 * Execute the script container. 00156 */ 00157 Object::Ptr execute(); 00158 00159 /** 00160 * Return a list of functionnames the with 00161 * \a setCode defined scriptcode spends. 00162 */ 00163 const QStringList getFunctionNames(); 00164 00165 /** 00166 * Call a function in the script container. 00167 * 00168 * \param functionname The name of the function 00169 * to call. 00170 * \param arguments Optional list of arguments 00171 * passed to the function. 00172 * \return \a Object instance representing 00173 * the functioncall returnvalue. 00174 */ 00175 KSharedPtr<Object> callFunction(const QString& functionname, KSharedPtr<List> arguments = 0); 00176 00177 /** 00178 * Return a list of classes. 00179 */ 00180 const QStringList getClassNames(); 00181 00182 /** 00183 * Create and return a new class instance. 00184 */ 00185 KSharedPtr<Object> classInstance(const QString& classname); 00186 00187 /** 00188 * Initialize the \a Script instance. 00189 * 00190 * Normaly it's not needed to call this function direct cause 00191 * if will be internaly called if needed (e.g. on \a execute ). 00192 */ 00193 bool initialize(); 00194 00195 /** 00196 * Finalize the \a Script instance and free's any cached or still 00197 * running executions. Normaly it's not needed to call this 00198 * function direct cause the \a ScriptContainer will take care 00199 * of calling it if needed. 00200 */ 00201 void finalize(); 00202 00203 private: 00204 /// Internaly used private d-pointer. 00205 ScriptContainerPrivate* d; 00206 }; 00207 00208 }} 00209 00210 #endif 00211
1.4.6