lib

interpreter.cpp

00001 /***************************************************************************
00002  * interpreter.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 "interpreter.h"
00021 #include "script.h"
00022 #include "../main/manager.h"
00023 #include "../main/scriptcontainer.h"
00024 
00025 #include <klibloader.h>
00026 
00027 extern "C"
00028 {
00029     typedef int (*def_interpreter_func)(Kross::Api::InterpreterInfo*);
00030 }
00031 
00032 using namespace Kross::Api;
00033 
00034 /*************************************************************************
00035  * InterpreterInfo
00036  */
00037 
00038 InterpreterInfo::InterpreterInfo(const QString& interpretername, const QString& library, const QString& wildcard, QStringList mimetypes, Option::Map options)
00039     : m_interpretername(interpretername)
00040     , m_library(library)
00041     , m_wildcard(wildcard)
00042     , m_mimetypes(mimetypes)
00043     , m_options(options)
00044     , m_interpreter(0)
00045 {
00046 }
00047 
00048 InterpreterInfo::~InterpreterInfo()
00049 {
00050     for(Option::Map::Iterator it = m_options.begin(); it != m_options.end(); ++it)
00051         delete it.data();
00052 
00053     delete m_interpreter;
00054     m_interpreter = 0;
00055 }
00056 
00057 const QString InterpreterInfo::getInterpretername()
00058 {
00059     return m_interpretername;
00060 }
00061 
00062 const QString InterpreterInfo::getWildcard()
00063 {
00064     return m_wildcard;
00065 }
00066 
00067 const QStringList InterpreterInfo::getMimeTypes()
00068 {
00069     return m_mimetypes;
00070 }
00071 
00072 bool InterpreterInfo::hasOption(const QString& key)
00073 {
00074     return m_options.contains(key);
00075 }
00076 
00077 InterpreterInfo::Option* InterpreterInfo::getOption(const QString name)
00078 {
00079     return m_options[name];
00080 }
00081 
00082 const QVariant InterpreterInfo::getOptionValue(const QString name, QVariant defaultvalue)
00083 {
00084     Option* o = m_options[name];
00085     return o ? o->value : defaultvalue;
00086 }
00087 
00088 InterpreterInfo::Option::Map InterpreterInfo::getOptions()
00089 {
00090     return m_options;
00091 }
00092 
00093 Interpreter* InterpreterInfo::getInterpreter()
00094 {
00095     if(m_interpreter) // buffered
00096         return m_interpreter;
00097 
00098     krossdebug( QString("Loading the interpreter library for %1").arg(m_interpretername) );
00099 
00100     // Load the krosspython library.
00101     KLibLoader *libloader = KLibLoader::self();
00102 
00103     KLibrary* library = libloader->globalLibrary( m_library.latin1() );
00104     if(! library) {
00105         /*
00106         setException(
00107             new Exception( QString("Could not load library \"%1\" for the \"%2\" interpreter.").arg(m_library).arg(m_interpretername) )
00108         );
00109         */
00110         krosswarning( QString("Could not load library \"%1\" for the \"%2\" interpreter.").arg(m_library).arg(m_interpretername) );
00111         return 0;
00112     }
00113 
00114     // Get the extern "C" krosspython_instance function.
00115     def_interpreter_func interpreter_func;
00116     interpreter_func = (def_interpreter_func) library->symbol("krossinterpreter");
00117     if(! interpreter_func) {
00118         //setException( new Exception("Failed to load symbol in krosspython library.") );
00119         krosswarning("Failed to load the 'krossinterpreter' symbol from the library.");
00120     }
00121     else {
00122         // and execute the extern krosspython_instance function.
00123         m_interpreter = (Interpreter*) (interpreter_func)(this);
00124         if(! m_interpreter) {
00125             krosswarning("Failed to load the Interpreter instance from library.");
00126         }
00127         else {
00128             // Job done. The library is loaded and our Interpreter* points
00129             // to the external Kross::Python::Interpreter* instance.
00130             krossdebug("Successfully loaded Interpreter instance from library.");
00131         }
00132     }
00133 
00134     // finally unload the library.
00135     library->unload();
00136 
00137     return m_interpreter;
00138 }
00139 
00140 /*************************************************************************
00141  * Interpreter
00142  */
00143 
00144 Interpreter::Interpreter(InterpreterInfo* info)
00145     : m_interpreterinfo(info)
00146 {
00147 }
00148 
00149 Interpreter::~Interpreter()
00150 {
00151 }
KDE Home | KDE Accessibility Home | Description of Access Keys