File indexing completed on 2024-04-14 14:27:09

0001 /***************************************************************************
0002  * interpreter.cpp
0003  * This file is part of the KDE project
0004  * copyright (C)2004-2006 by Sebastian Sauer (mail@dipe.org)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  ***************************************************************************/
0019 
0020 #include "interpreter.h"
0021 #include "script.h"
0022 #include "action.h"
0023 #include "manager.h"
0024 #include "kross_debug.h"
0025 
0026 extern "C"
0027 {
0028     typedef void *(*def_interpreter_func)(int version, Kross::InterpreterInfo *);
0029 }
0030 
0031 using namespace Kross;
0032 
0033 /*************************************************************************
0034  * InterpreterInfo
0035  */
0036 
0037 namespace Kross
0038 {
0039 
0040 /// \internal d-pointer class.
0041 class InterpreterInfo::Private
0042 {
0043 public:
0044     /// The name the interpreter has. Could be something like "python" or "ruby".
0045     QString interpretername;
0046     /// The function-pointer to the module factory function.
0047     QFunctionPointer funcPtr;
0048     /// The file wildcard used to determinate extensions.
0049     QString wildcard;
0050     /// List of mimetypes this interpreter supports.
0051     QStringList mimetypes;
0052     /// A map with options.
0053     Option::Map options;
0054     /// The \a Interpreter instance.
0055     Interpreter *interpreter;
0056 };
0057 
0058 }
0059 
0060 InterpreterInfo::InterpreterInfo(const QString &interpretername, QFunctionPointer funcPtr, const QString &wildcard, const QStringList &mimetypes, const Option::Map &options)
0061     : d(new Private())
0062 {
0063     d->interpretername = interpretername;
0064     d->funcPtr = funcPtr;
0065     d->wildcard = wildcard;
0066     d->mimetypes = mimetypes;
0067     d->options = options;
0068     d->interpreter = nullptr;
0069 }
0070 
0071 InterpreterInfo::~InterpreterInfo()
0072 {
0073     delete d->interpreter;
0074     d->interpreter = nullptr;
0075     delete d;
0076 }
0077 
0078 const QString InterpreterInfo::interpreterName() const
0079 {
0080     return d->interpretername;
0081 }
0082 
0083 const QString InterpreterInfo::wildcard() const
0084 {
0085     return d->wildcard;
0086 }
0087 
0088 const QStringList InterpreterInfo::mimeTypes() const
0089 {
0090     return d->mimetypes;
0091 }
0092 
0093 bool InterpreterInfo::hasOption(const QString &name) const
0094 {
0095     return d->options.contains(name);
0096 }
0097 
0098 InterpreterInfo::Option *InterpreterInfo::option(const QString &name) const
0099 {
0100     return d->options.contains(name) ? d->options[name] : nullptr;
0101 }
0102 
0103 InterpreterInfo::Option::Map &InterpreterInfo::options()
0104 {
0105     return d->options;
0106 }
0107 
0108 const QVariant InterpreterInfo::optionValue(const QString &name, const QVariant &defaultvalue) const
0109 {
0110     return d->options.contains(name) ? d->options[name]->value : defaultvalue;
0111 }
0112 
0113 Interpreter *InterpreterInfo::interpreter()
0114 {
0115     if (d->interpreter) { // buffered
0116         return d->interpreter;
0117     }
0118 
0119     //#ifdef KROSS_INTERPRETER_DEBUG
0120     qCDebug(KROSS_LOG) << "Loading the interpreter library for " << d->interpretername;
0121     //#endif
0122 
0123     // Get the extern "C" krosspython_instance function.
0124     def_interpreter_func interpreter_func = (def_interpreter_func) d->funcPtr;
0125 
0126     // and execute the extern krosspython_instance function.
0127     d->interpreter = interpreter_func
0128                      ? (Interpreter *)(interpreter_func)(KROSS_VERSION, this)
0129                      : nullptr;
0130 
0131     if (! d->interpreter) {
0132         //#ifdef KROSS_INTERPRETER_DEBUG
0133         qCWarning(KROSS_LOG) << "Incompatible interpreter library.";
0134         //#endif
0135     } else {
0136         // Job done. The library is loaded and our Interpreter* points
0137         // to the external Kross::Python::Interpreter* instance.
0138         //#ifdef KROSS_INTERPRETER_DEBUG
0139         qCDebug(KROSS_LOG) << "Successfully loaded Interpreter instance from library.";
0140         //#endif
0141     }
0142 
0143     return d->interpreter;
0144 }
0145 
0146 /*************************************************************************
0147  * Interpreter
0148  */
0149 
0150 namespace Kross
0151 {
0152 
0153 /// \internal d-pointer class.
0154 class Interpreter::Private
0155 {
0156 public:
0157     InterpreterInfo *interpreterinfo;
0158     Private(InterpreterInfo *info) : interpreterinfo(info) {}
0159 };
0160 
0161 }
0162 
0163 Interpreter::Interpreter(InterpreterInfo *info)
0164     : QObject()
0165     , ErrorInterface()
0166     , d(new Private(info))
0167 {
0168 }
0169 
0170 Interpreter::~Interpreter()
0171 {
0172     delete d;
0173 }
0174 
0175 InterpreterInfo *Interpreter::interpreterInfo() const
0176 {
0177     return d->interpreterinfo;
0178 }
0179 
0180 #include "moc_interpreter.cpp"