lib

manager.cpp
1/***************************************************************************
2 * manager.cpp
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 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
22#include "../api/interpreter.h"
23//#include "../api/tqtobject.h"
24#include "../api/eventslot.h"
25#include "../api/eventsignal.h"
26//#include "../api/script.h"
27
28#include "krossconfig.h"
29#include "scriptcontainer.h"
30
31#include <tqobject.h>
32#include <tqfile.h>
33#include <tqregexp.h>
34
35#include <klibloader.h>
36#include <tdelocale.h>
37#include <kstaticdeleter.h>
38
39extern "C"
40{
41 typedef Kross::Api::Object* (*def_module_func)(Kross::Api::Manager*);
42}
43
44using namespace Kross::Api;
45
46namespace Kross { namespace Api {
47
49 class ManagerPrivate
50 {
51 public:
53 TQMap<TQString, InterpreterInfo*> interpreterinfos;
54
56 TQMap<TQString, Module::Ptr> modules;
57 };
58
63 static TDESharedPtr<Manager> m_manager = TDESharedPtr<Manager>(0);
64
65}}
66
68{
69 if(! m_manager.data()) {
70 // Create the Manager-singleton on demand.
71 m_manager = TDESharedPtr<Manager>( new Manager() );
72 }
73
74 // and finally return the singleton.
75 return m_manager.data();
76}
77
79 : MainModule("Kross") // the manager has the name "Kross"
80 , d( new ManagerPrivate() )
81{
82#ifdef KROSS_PYTHON_LIBRARY
83 TQString pythonlib = TQFile::encodeName( KLibLoader::self()->findLibrary(KROSS_PYTHON_LIBRARY) );
84 if(! pythonlib.isEmpty()) { // If the Kross Python plugin exists we offer it as supported scripting language.
85 InterpreterInfo::Option::Map pythonoptions;
86 pythonoptions.replace("restricted",
87 new InterpreterInfo::Option("Restricted", "Restricted Python interpreter", TQVariant(false))
88 );
89 d->interpreterinfos.replace("python",
90 new InterpreterInfo("python",
91 pythonlib, // library
92 "*.py", // file filter-wildcard
93 TQStringList() << /* "text/x-python" << */ "application/x-python", // mimetypes
94 pythonoptions // options
95 )
96 );
97 }
98#endif
99#ifdef KROSS_RUBY_LIBRARY
100 TQString rubylib = TQFile::encodeName( KLibLoader::self()->findLibrary(KROSS_RUBY_LIBRARY) );
101 if(! rubylib.isEmpty()) { // If the Kross Ruby plugin exists we offer it as supported scripting language.
103 rubyoptions.replace("safelevel",
104 new InterpreterInfo::Option("safelevel", "Level of safety of the Ruby interpreter", TQVariant(0)) // 0 -> unsafe, 4 -> very safe
105 );
106 d->interpreterinfos.replace("ruby",
107 new InterpreterInfo("ruby",
108 rubylib, // library
109 "*.rb", // file filter-wildcard
110 TQStringList() << /* "text/x-ruby" << */ "application/x-ruby", // mimetypes
111 rubyoptions // options
112 )
113 );
114 } else {
115 krossdebug("Ruby interpreter for kross in unavailable");
116 }
117#endif
118}
119
121{
122 for(TQMap<TQString, InterpreterInfo*>::Iterator it = d->interpreterinfos.begin(); it != d->interpreterinfos.end(); ++it)
123 delete it.data();
124 delete d;
125}
126
127TQMap<TQString, InterpreterInfo*> Manager::getInterpreterInfos()
128{
129 return d->interpreterinfos;
130}
131
132bool Manager::hasInterpreterInfo(const TQString& interpretername) const
133{
134 return d->interpreterinfos.contains(interpretername);
135}
136
137InterpreterInfo* Manager::getInterpreterInfo(const TQString& interpretername)
138{
139 return d->interpreterinfos[interpretername];
140}
141
142const TQString Manager::getInterpreternameForFile(const TQString& file)
143{
144 TQRegExp rx;
145 rx.setWildcard(true);
146 for(TQMap<TQString, InterpreterInfo*>::Iterator it = d->interpreterinfos.begin(); it != d->interpreterinfos.end(); ++it) {
147 rx.setPattern((*it)->getWildcard());
148 if( file.find(rx) >= 0 )
149 return (*it)->getInterpretername();
150 }
151 return TQString();
152}
153
155{
156 //TODO at the moment we don't share ScriptContainer instances.
157
158 //if(d->m_scriptcontainers.contains(scriptname))
159 // return d->m_scriptcontainers[scriptname];
160 ScriptContainer* scriptcontainer = new ScriptContainer(scriptname);
161 //ScriptContainer script(this, scriptname);
162 //d->m_scriptcontainers.replace(scriptname, scriptcontainer);
163
164 return scriptcontainer;
165}
166
167Interpreter* Manager::getInterpreter(const TQString& interpretername)
168{
169 setException(0); // clear previous exceptions
170
171 if(! d->interpreterinfos.contains(interpretername)) {
172 setException( new Exception(i18n("No such interpreter '%1'").arg(interpretername)) );
173 return 0;
174 }
175
176 return d->interpreterinfos[interpretername]->getInterpreter();
177}
178
179const TQStringList Manager::getInterpreters()
180{
181 TQStringList list;
182
183 TQMap<TQString, InterpreterInfo*>::Iterator it( d->interpreterinfos.begin() );
184 for(; it != d->interpreterinfos.end(); ++it)
185 list << it.key();
186
187//list << "TestCase";
188
189 return list;
190}
191
193{
194 TQString name = module->getName();
195 //if( d->modules.contains(name) ) return false;
196 d->modules.replace(name, module);
197 return true;
198}
199
200Module::Ptr Manager::loadModule(const TQString& modulename)
201{
202 Module::Ptr module = 0;
203
204 if(d->modules.contains(modulename)) {
205 module = d->modules[modulename];
206 if(module)
207 return module;
208 else
209 krossdebug( TQString("Manager::loadModule(%1) =======> Modulename registered, but module is invalid!").arg(modulename) );
210 }
211
212 KLibLoader* loader = KLibLoader::self();
213 KLibrary* lib = loader->globalLibrary( modulename.latin1() );
214 if(! lib) {
215 krosswarning( TQString("Failed to load module '%1': %2").arg(modulename).arg(loader->lastErrorMessage()) );
216 return 0;
217 }
218 krossdebug( TQString("Successfully loaded module '%1'").arg(modulename) );
219
220 def_module_func func;
221 func = (def_module_func) lib->symbol("init_module");
222
223 if(! func) {
224 krosswarning( TQString("Failed to determinate init function in module '%1'").arg(modulename) );
225 return 0;
226 }
227
228 try {
229 module = (Kross::Api::Module*) (func)(this);
230 }
232 krosswarning( e->toString() );
233 module = 0;
234 }
235 lib->unload();
236
237 if(! module) {
238 krosswarning( TQString("Failed to load module '%1'").arg(modulename) );
239 return 0;
240 }
241
242 // Don't remember module cause we like to have freeing it handled by the caller.
243 //d->modules.replace(modulename, module);
244
245 //krossdebug( TQString("Kross::Api::Manager::loadModule modulename='%1' module='%2'").arg(modulename).arg(module->toString()) );
246 return module;
247}
248
Common exception class used for representing exceptions in Kross.
Definition exception.h:39
TDESharedPtr< Exception > Ptr
Shared pointer to implement reference-counting.
Definition exception.h:45
Each interpreter is able to define options we could use to manipulate the interpreter behaviour.
Definition interpreter.h:50
TQMap< TQString, Option * > Map
Map of options.
Definition interpreter.h:56
While the Interpreter is the implemented interpreter this class is used to provide some abstract info...
Definition interpreter.h:42
Base class for interpreters.
MainModule(const TQString &name)
Constructor.
void setException(Exception::Ptr exception)
Set the Exception this module throwed.
The Manager class is the main entry point to work with Kross.
Definition manager.h:55
Module::Ptr loadModule(const TQString &modulename)
Load an external module and return it.
Definition manager.cpp:200
const TQString getInterpreternameForFile(const TQString &file)
Definition manager.cpp:142
Manager()
Constructor.
Definition manager.cpp:78
const TQStringList getInterpreters()
Definition manager.cpp:179
~Manager()
Destructor.
Definition manager.cpp:120
bool hasInterpreterInfo(const TQString &interpretername) const
Definition manager.cpp:132
InterpreterInfo * getInterpreterInfo(const TQString &interpretername)
Definition manager.cpp:137
TQMap< TQString, InterpreterInfo * > getInterpreterInfos()
Definition manager.cpp:127
static Manager * scriptManager()
Return the Manager instance.
Definition manager.cpp:67
TDESharedPtr< ScriptContainer > getScriptContainer(const TQString &scriptname)
Return the existing ScriptContainer with scriptname or create a new ScriptContainer instance and asso...
Definition manager.cpp:154
Interpreter * getInterpreter(const TQString &interpretername)
Return the Interpreter instance defined by the interpretername.
Definition manager.cpp:167
bool addModule(Module::Ptr module)
Add the an external module to the global shared list of loaded modules.
Definition manager.cpp:192
TDESharedPtr< Module > Ptr
Shared pointer to implement reference-counting.
Definition module.h:41
The common Object class all other object-classes are inheritated from.
Definition object.h:51
The ScriptContainer class is something like a single standalone scriptfile.
TDESharedPtr< ScriptContainer > Ptr
Shared pointer to implement reference-counting.
The common Kross API used as common codebase.
Definition callable.h:31
static TDESharedPtr< Manager > m_manager
The Manager-singleton instance is NULL by default till the Manager::scriptManager() method got called...
Definition manager.cpp:63
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:31
void krossdebug(const TQString &s)
Debugging function.
void krosswarning(const TQString &s)
Warning function.