File indexing completed on 2024-04-21 15:02:58

0001 /***************************************************************************
0002  * manager.h
0003  * This file is part of the KDE project
0004  * copyright (C)2004-2007 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 #ifndef KROSS_MANAGER_H
0021 #define KROSS_MANAGER_H
0022 
0023 #include "krosscore_export.h"
0024 
0025 #if KROSSCORE_ENABLE_DEPRECATED_SINCE(5, 88)
0026 #include <QStringList>
0027 #include <QMap>
0028 #include <QObject>
0029 #include <QUrl>
0030 #include <QScriptable>
0031 
0032 #include "krossconfig.h"
0033 #include "childreninterface.h"
0034 #include "metatype.h"
0035 
0036 namespace Kross
0037 {
0038 
0039 // Forward declarations.
0040 class Interpreter;
0041 class Action;
0042 class ActionCollection;
0043 class InterpreterInfo;
0044 
0045 /**
0046  * The Manager class is a singleton that provides the main entry
0047  * point to deal with the Kross Scripting Framework.
0048  *
0049  * Use \a Interpreter to just work with some implemented
0050  * interpreter like python or ruby. While \a Action implements
0051  * a flexible abstract container to deal with single script files.
0052  * @depreacted Kross is deprecated and will not be part of KDE Frameworks 6.
0053  * For JS scripting QJSEngine should be used, for python scripting
0054  * consider using pybind11.
0055  */
0056 class KROSSCORE_EXPORT Manager
0057     : public QObject
0058     , public QScriptable
0059     , public ChildrenInterface
0060 {
0061     Q_OBJECT
0062 
0063 public:
0064 
0065     /**
0066      * Return the Manager instance. Always use this
0067      * function to access the Manager singleton.
0068      */
0069     KROSSCORE_DEPRECATED_VERSION(5, 88, "Kross is deprecated, see API docs")
0070     static Manager &self();
0071 
0072     /**
0073      * \return a map with \a InterpreterInfo* instances
0074      * used to describe interpreters.
0075      */
0076     QHash<QString, InterpreterInfo *> interpreterInfos() const;
0077 
0078     /**
0079      * \return true if there exists an interpreter with the
0080      * name \p interpretername else false.
0081      */
0082     bool hasInterpreterInfo(const QString &interpretername) const;
0083 
0084     /**
0085      * \return the \a InterpreterInfo* matching to the defined
0086      * \p interpretername or NULL if there does not exists such
0087      * a interpreter.
0088      */
0089     InterpreterInfo *interpreterInfo(const QString &interpretername) const;
0090 
0091     /**
0092      * Return the name of the \a Interpreter that feels responsible
0093      * for the defined \p file .
0094      *
0095      * \param file The filename we should try to determinate the
0096      * interpretername for.
0097      * \return The name of the \a Interpreter which will be used
0098      * to execute the file or QString() if we failed to determinate
0099      * a matching interpreter for the file.
0100      */
0101     const QString interpreternameForFile(const QString &file);
0102 
0103     /**
0104      * Return the \a Interpreter instance defined by
0105      * the interpretername.
0106      *
0107      * \param interpretername The name of the interpreter.
0108      * e.g. "python" or "ruby".
0109      * \return The Interpreter instance or NULL if there does not exists
0110      * an interpreter with such an interpretername.
0111      */
0112     Interpreter *interpreter(const QString &interpretername) const;
0113 
0114     /**
0115      * \return the root \a ActionCollection instance. Each collection
0116      * could have children of other collections and/or
0117      * \a Action instances.
0118      */
0119     ActionCollection *actionCollection() const;
0120 
0121     /**
0122      * \return the \a MetaTypeHandler instance for custom types
0123      * of type \p typeName .
0124      *
0125      * \since 4.2
0126      */
0127     MetaTypeHandler *metaTypeHandler(const QByteArray &typeName) const;
0128 
0129     /**
0130      * Register a handler for custom types.
0131      *
0132      * See also the \a WrapperInterface class.
0133      *
0134      * \param typeName The custom type the handler should handle.
0135      * \param handler Function that should be called to handle
0136      * a custom type.
0137      *
0138      * \since 4.2
0139      */
0140     void registerMetaTypeHandler(const QByteArray &typeName, MetaTypeHandler::FunctionPtr *handler);
0141 
0142     /**
0143      * Register a handler for custom types.
0144      *
0145      * See also the \a WrapperInterface class.
0146      *
0147      * \param typeName The custom type the handler should handle.
0148      * \param handler Function that should be called to handle
0149      * a custom type.
0150      *
0151      * \since 4.2
0152      */
0153     void registerMetaTypeHandler(const QByteArray &typeName, MetaTypeHandler::FunctionPtr2 *handler);
0154 
0155     /**
0156      * Register a handler for custom types.
0157      *
0158      * See also the \a WrapperInterface class.
0159      *
0160      * \param typeName The custom type the handler should handle.
0161      * \param handler Function that should be called to handle
0162      * a custom type.
0163      *
0164      * \since 4.2
0165      */
0166     void registerMetaTypeHandler(const QByteArray &typeName, MetaTypeHandler *handler);
0167 
0168     /**
0169      * Returns true if strict type handling is enabled.
0170      *
0171      * \since 4.2
0172      */
0173     bool strictTypesEnabled() const;
0174 
0175     /**
0176      * Enable more strict type handling. If enabled then scripting-backends don't
0177      * handle unknown pointer-types where no MetaTypeHandler was registered for.
0178      * If disabled, such unknown types will be reinterpret_cast to QObject* what
0179      * allows to also handle unknown QObject's but will also result in a crash
0180      * if the unknown type isn't a QObject. Per default strict type handling is
0181      * enabled.
0182      *
0183      * \since 4.2
0184      */
0185     void setStrictTypesEnabled(bool enabled);
0186 
0187     /**
0188     * \return whether \p typeName has a handler assigned or not.
0189     */
0190     bool hasHandlerAssigned(const QByteArray &typeName) const;
0191 public Q_SLOTS:
0192 
0193     /**
0194      * \return a list of names of all supported scripting interpreters.
0195      * The list may contain for example "python" and "ruby" depending
0196      * on what interpreter-plugins are installed.
0197      */
0198     QStringList interpreters() const;
0199 
0200     /**
0201     * \return true if there exists a \a Action QObject instance
0202     * which is child of this \a Manager instance and is defined as \p name
0203     * else false is returned.
0204     */
0205     bool hasAction(const QString &name);
0206 
0207     /**
0208     * \return the \a Action QObject instance defined with \p name which is
0209     * child of this \a Manager instance. If there exists no such \a Action
0210     * yet, create one.
0211     */
0212     QObject *action(const QString &name);
0213 
0214     /**
0215      * Load and return an external module. Modules are dynamic loadable
0216      * plugins which could be loaded on demand to provide additional
0217      * functionality.
0218      *
0219      * \param modulename The name of the module we should try to load.
0220      * \return The QObject instance that represents the module or NULL
0221      * if loading failed.
0222      */
0223     QObject *module(const QString &modulename);
0224 
0225     /**
0226      * External modules are dynamically loadable and are normally deleted
0227      * when the kross library is unloaded.
0228      * Applications may choose to call deleteModules() instead to control
0229      * deletion of the modules at another time.
0230      */
0231     void deleteModules();
0232 
0233     /**
0234     * Execute a script file.
0235     * \param file The script file that should be executed.
0236     */
0237     bool executeScriptFile(const QUrl &file = QUrl());
0238 
0239     void addQObject(QObject *obj, const QString &name = QString());
0240     QObject *qobject(const QString &name) const;
0241     QStringList qobjectNames() const;
0242 
0243 Q_SIGNALS:
0244 
0245     /**
0246      * This signal is emitted when the execution of a script is started.
0247      */
0248     void started(Kross::Action *);
0249 
0250     /**
0251      * This signal is emitted when the execution of a script is finished.
0252      */
0253     void finished(Kross::Action *);
0254 
0255 private:
0256     /// \internal d-pointer class.
0257     class Private;
0258     /// \internal d-pointer instance.
0259     Private *const d;
0260 
0261 public:
0262 
0263     /**
0264      * The constructor. Use \a self() to access the Manager
0265      * singleton instance and don't call this direct.
0266      */
0267     explicit Manager();
0268 
0269     /**
0270      * Destructor.
0271      */
0272     ~Manager() override;
0273 };
0274 
0275 }
0276 
0277 #endif
0278 #endif
0279