00001 /*************************************************************************** 00002 * interpreter.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 "interpreter.h" 00021 #include "script.h" 00022 #include "action.h" 00023 #include "manager.h" 00024 00025 #include <klibloader.h> 00026 00027 extern "C" 00028 { 00029 typedef void* (*def_interpreter_func)(Kross::InterpreterInfo*); 00030 } 00031 00032 using namespace Kross; 00033 00034 /************************************************************************* 00035 * InterpreterInfo 00036 */ 00037 00038 namespace Kross { 00039 00041 class InterpreterInfo::Private 00042 { 00043 public: 00045 QString interpretername; 00047 QString library; 00049 QString wildcard; 00051 QStringList mimetypes; 00053 Option::Map options; 00055 Interpreter* interpreter; 00056 }; 00057 00058 } 00059 00060 InterpreterInfo::InterpreterInfo(const QString& interpretername, const QString& library, const QString& wildcard, QStringList mimetypes, Option::Map options) 00061 : d( new Private() ) 00062 { 00063 d->interpretername = interpretername; 00064 d->library = library; 00065 d->wildcard = wildcard; 00066 d->mimetypes = mimetypes; 00067 d->options = options; 00068 d->interpreter = 0; 00069 } 00070 00071 InterpreterInfo::~InterpreterInfo() 00072 { 00073 delete d->interpreter; 00074 //d->interpreter = 0; 00075 delete d; 00076 } 00077 00078 const QString InterpreterInfo::interpreterName() const 00079 { 00080 return d->interpretername; 00081 } 00082 00083 const QString InterpreterInfo::wildcard() const 00084 { 00085 return d->wildcard; 00086 } 00087 00088 const QStringList InterpreterInfo::mimeTypes() const 00089 { 00090 return d->mimetypes; 00091 } 00092 00093 bool InterpreterInfo::hasOption(const QString& key) const 00094 { 00095 return d->options.contains(key); 00096 } 00097 00098 InterpreterInfo::Option* InterpreterInfo::option(const QString name) const 00099 { 00100 return d->options[name]; 00101 } 00102 00103 const QVariant InterpreterInfo::optionValue(const QString name, QVariant defaultvalue) const 00104 { 00105 Option* o = option(name); 00106 return o ? o->value : defaultvalue; 00107 } 00108 00109 InterpreterInfo::Option::Map InterpreterInfo::options() 00110 { 00111 return d->options; 00112 } 00113 00114 Interpreter* InterpreterInfo::interpreter() 00115 { 00116 if(d->interpreter) // buffered 00117 return d->interpreter; 00118 00119 #ifdef KROSS_INTERPRETER_DEBUG 00120 krossdebug( QString("Loading the interpreter library for %1").arg(d->interpretername) ); 00121 #endif 00122 // Load the krosspython library. 00123 KLibLoader *libloader = KLibLoader::self(); 00124 00125 KLibrary* library = libloader->globalLibrary( d->library.toLatin1().data() ); 00126 if(! library) { 00127 /* 00128 setException( 00129 new Exception( QString("Could not load library \"%1\" for the \"%2\" interpreter.").arg(d->library).arg(d->interpretername) ) 00130 ); 00131 */ 00132 krosswarning( QString("Could not load library \"%1\" for the \"%2\" interpreter.").arg(d->library).arg(d->interpretername) ); 00133 return 0; 00134 } 00135 00136 // Get the extern "C" krosspython_instance function. 00137 def_interpreter_func interpreter_func; 00138 interpreter_func = (def_interpreter_func) library->symbol("krossinterpreter"); 00139 if(! interpreter_func) { 00140 //setException( new Exception("Failed to load symbol in krosspython library.") ); 00141 krosswarning("Failed to load the 'krossinterpreter' symbol from the library."); 00142 } 00143 else { 00144 // and execute the extern krosspython_instance function. 00145 d->interpreter = (Interpreter*) (interpreter_func)(this); 00146 if(! d->interpreter) { 00147 krosswarning("Failed to load the Interpreter instance from library."); 00148 } 00149 else { 00150 // Job done. The library is loaded and our Interpreter* points 00151 // to the external Kross::Python::Interpreter* instance. 00152 #ifdef KROSS_INTERPRETER_DEBUG 00153 krossdebug("Successfully loaded Interpreter instance from library."); 00154 #endif 00155 } 00156 } 00157 00158 // finally unload the library. 00159 library->unload(); 00160 00161 return d->interpreter; 00162 } 00163 00164 /************************************************************************* 00165 * Interpreter 00166 */ 00167 00168 namespace Kross { 00169 00171 class Interpreter::Private 00172 { 00173 public: 00175 InterpreterInfo* interpreterinfo; 00176 00177 Private(InterpreterInfo* info) : interpreterinfo(info) {} 00178 }; 00179 00180 } 00181 00182 Interpreter::Interpreter(InterpreterInfo* info) 00183 : ErrorInterface() 00184 , d( new Private(info) ) 00185 { 00186 } 00187 00188 Interpreter::~Interpreter() 00189 { 00190 delete d; 00191 } 00192 00193 InterpreterInfo* Interpreter::interpreterInfo() const 00194 { 00195 return d->interpreterinfo; 00196 } 00197 00198 void Interpreter::virtual_hook(int id, void* data) 00199 { 00200 Q_UNUSED(id); 00201 Q_UNUSED(data); 00202 }