File indexing completed on 2024-04-28 15:29:41

0001 /* This file is part of the KDE project
0002    Copyright (C) 2008 Paulo Moura Guedes <moura@kdewebdev.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  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_PLUGIN_H
0021 #define KROSS_PLUGIN_H
0022 
0023 #include <kparts/plugin.h>
0024 
0025 #include <kross/ui/krossui_export.h>
0026 #include <kross/core/childreninterface.h>
0027 
0028 namespace Kross
0029 {
0030 class ActionCollection;
0031 }
0032 
0033 class QWidget;
0034 
0035 namespace Kross
0036 {
0037 
0038 /**
0039  * The ScriptingPlugin class loads additional actions stored in rc files with the
0040  * KrossScripting format:
0041  *
0042  * \code
0043  * <KrossScripting>
0044  *     <collection name="file" text="File">
0045  *         <script name="dummy_script" text="Dummy Script" comment="Dummy Script example"
0046  *                 file="dummy_script.py" interpreter="python" />
0047  *     </collection>
0048  * </KrossScripting>
0049  * \endcode
0050  *
0051  * The 'name' attribute in collection element will be used to match the menu object name.
0052  * If no menu already exists with this name, a new one is created. In this example, the user will
0053  * see a menu item with the text "Dummy Script" in "File" menu, which will execute the dummy_script.py script.
0054  *
0055  * By default it tries to find kross rc files in %APPDATA%/scripts directory.
0056  * Clients of this class can use slotEditScriptActions() as a way to override and/or extend the
0057  * default script actions (if they exist at all).
0058  *
0059  * You may create multiple instances of ScriptingPlugin by using alternative c'tor.
0060   */
0061 class KROSSUI_EXPORT ScriptingPlugin : public KParts::Plugin
0062 {
0063     Q_OBJECT
0064 public:
0065 
0066     /**
0067      * Constructor.
0068      *
0069      * \param parent The parent QObject this QObject is child of.
0070      */
0071     explicit ScriptingPlugin(QObject *parent = nullptr);
0072 
0073     /**
0074      * Allows having actions defined in a custom location, eg for project-specific actions
0075      *
0076      * \param userActionsFile scripts.rc filepath -- file may be modified by user
0077      * \param referenceActionsDir dir -- %APPDATA%/scripts/%referenceActionsDir% contains standard actions for this plugin instance; has a lower priority than \a userActionsFile.
0078      */
0079     ScriptingPlugin(const QString &collectionName, const QString &userActionsFile, const QString &referenceActionsDir = QString(), QObject *parent = nullptr);
0080 
0081     /**
0082      * Destructor.
0083      */
0084     ~ScriptingPlugin() override;
0085 
0086     /**
0087      * Re-implement in order to load additional kross scripting rc files.
0088      */
0089     void setDOMDocument(const QDomDocument &document, bool merge = false) override;
0090 
0091     /**
0092      * Add a QObject to the list of children. The object will be published to the scripting code.
0093      * \param object The QObject instance that should be added to the list of children.
0094      * \param name The name the QObject should be known under. If not defined, the
0095      * QObject's objectName is used.
0096      */
0097     void addObject(QObject *object, const QString &name/* = QString()*/, ChildrenInterface::Options options/* = ChildrenInterface::NoOption*/);
0098 
0099     ///\deprecated use another addObject overload
0100     void addObject(QObject *object, const QString &name = QString()); //BIC
0101 
0102 protected Q_SLOTS:
0103 
0104     /**
0105      * This slot will open/create a scriptactions.rc file in XDG_DATA_HOME/application/scripts/
0106      * which will override other kross rc files. This allows a user to extend existing menus with new actions.
0107      */
0108     virtual void slotEditScriptActions();
0109 
0110     /**
0111      * Deletes the user rc file, which has the effect of falling back to the default script actions (if any).
0112      */
0113     virtual void slotResetScriptActions();
0114 
0115 private:
0116     QDomDocument buildDomDocument(const QDomDocument &document);
0117     void buildDomDocument(QDomDocument &document, Kross::ActionCollection *collection);
0118     void save();
0119 
0120 private:
0121     class ScriptingPluginPrivate;
0122     ScriptingPluginPrivate *const d;
0123 };
0124 
0125 }
0126 
0127 #endif