File indexing completed on 2022-10-04 17:30:22

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 // SPDX-FileCopyrightText: 2020 Harald Sitter <sitter@kde.org>
0004 
0005 #ifndef SCRIPT_PLUGIN_H
0006 #define SCRIPT_PLUGIN_H
0007 
0008 #include "plugin.h"
0009 
0010 #include <QJSEngine>
0011 
0012 class QMenu;
0013 
0014 class KRenameFile;
0015 
0016 namespace Ui
0017 {
0018 class ScriptPluginWidget;
0019 };
0020 
0021 class ScriptPlugin : public QObject, public Plugin
0022 {
0023     Q_OBJECT
0024 public:
0025     /** Create a new ScriptPlugin from
0026      *
0027      */
0028     explicit ScriptPlugin(PluginLoader *loader);
0029 
0030     ~ScriptPlugin() override;
0031 
0032     /**
0033      * @returns a name of the plugin that can be displayed
0034      *          to the user. This name should be internationalized.
0035      */
0036     inline const QString name() const override;
0037 
0038     /**
0039      * @returns the type of the plugin.
0040      */
0041     inline int type() const override;
0042 
0043     /**
0044      * @returns an icon for this plugin.
0045      */
0046     const QIcon icon() const override;
0047 
0048     /**
0049      * @returns true if this plugins is always enabled
0050      *
0051      * Warning: If you return true here, the user has no possibility to
0052      *          disable this plugin.
0053      */
0054     inline bool enabledByDefault() const override;
0055 
0056     /**
0057      * This function is the core of your plugin.
0058      *
0059      * It does the actual processing of a file, filename or token depending of the type
0060      * of your plugin.
0061      *
0062      * \see type()
0063      *
0064      * @param b the parent BatchRenamer instance calling this plugin
0065      * @param index the index of the current file (i.e. the first file has index 0,
0066      *              the second file to be renamed has index 1 ....)
0067      * @param filenameOrToken this parameter depends on the type of your plugin.
0068      *                        If type is ePluginType_File, this is the absolute path
0069      *                        or URL to the renamed file.
0070      *                        If type is ePluginType_Filename, this is the filename
0071      *                        (without path) as created by KRename.
0072      *                        If type is ePluginType_Token, this is the contents of a token
0073      *                        in brackets. If your plugin supports the token [example],
0074      *                        KRename will pass the strign "example" to your method.
0075      * @param eCurrentType the current type of plugin that is requested (for plugins that support more than one type)
0076      *
0077      * @returns the result of the function, depending on type().
0078      * @returns an empty QString if this plugin has nothing to do.
0079      * @returns A new filename if type is ePluginType_Filename
0080      * @returns the value of the token if type is ePluginType_Token
0081      * @returns an error message or an empty QString if type is ePluginType_File
0082      */
0083     QString processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType eCurrentType) override;
0084 
0085     /** Get a list of all tokens supported by this plugin.
0086      *
0087      *  If the token type != ePluginType_Token you have to return an empty list
0088      *
0089      *  @returns a list of all supported tokens.
0090      */
0091     inline const QStringList &supportedTokens() const override;
0092 
0093     /** Returns help descriptions for the supported tokens
0094      *
0095      *  The returned stringlist contains strings that are the tokens
0096      *  and the description separated by ;;
0097      *
0098      *  @returns a stringlist containing help on the supported tokens
0099      */
0100     inline const QStringList &help() const override;
0101 
0102     /** Create a user interface for this plugin
0103      *
0104      *  @param parent the parent widget of this plugin
0105      *
0106      *  This is implemented here for all FilePlugin based classed
0107      */
0108     void createUI(QWidget *parent) const override;
0109 
0110     /** Load the plugin configuration.
0111      *
0112      *  Called when plugins should load their configuration.
0113      *
0114      *  @param group config group where the configuration should be read from
0115      */
0116     void loadConfig(KConfigGroup &group) override;
0117 
0118     /** Save the plugin configuration.
0119      *
0120      *  Called when plugins should save their configuration.
0121      *
0122      *  @param group config group where the configuration should be stored
0123      */
0124     void saveConfig(KConfigGroup &group) const override;
0125 
0126 protected:
0127     /**
0128      *  Adds a token to the list of supported tokens
0129      *
0130      *  @param token will be a supported token from now on
0131      *
0132      *  @see supports
0133      */
0134     inline void addSupportedToken(const QString &token)
0135     {
0136         m_keys.append(token);
0137     }
0138 
0139     /**
0140      * Set all KRename internal variables on the internal
0141      * Interpreter object.
0142      *
0143      * @param file the KRenameFile where the values can be retrieved
0144      * @param index index of the current file
0145      */
0146     void initKRenameVars(const KRenameFile &file, int index);
0147 
0148     /**
0149      * Insert a variable in the definitions textfield
0150      * at the current cursor position
0151      *
0152      * @param name variable name
0153      */
0154     void insertVariable(const char *name);
0155 
0156 private Q_SLOTS:
0157     void slotEnableControls();
0158     void slotAdd();
0159     void slotRemove();
0160     void slotSave();
0161     void slotLoad();
0162     void slotTest();
0163 
0164     void slotInsertIndex();
0165     void slotInsertUrl();
0166     void slotInsertFilename();
0167     void slotInsertExtension();
0168     void slotInsertDirectory();
0169 
0170 private:
0171     QString             m_name;
0172     QString             m_icon;
0173 
0174     QStringList         m_keys;
0175     QStringList         m_help;
0176     QJSEngine           m_engine;
0177     QWidget            *m_parent;
0178     QMenu              *m_menu;
0179 
0180     Ui::ScriptPluginWidget *m_widget;
0181 
0182     static const char  *s_pszFileDialogLocation; ///< Static URL for KFileDialog
0183     static const char *s_pszVarNameIndex;
0184     static const char *s_pszVarNameUrl;
0185     static const char *s_pszVarNameFilename;
0186     static const char *s_pszVarNameExtension;
0187     static const char *s_pszVarNameDirectory;
0188 };
0189 
0190 inline const QString ScriptPlugin::name() const
0191 {
0192     return m_name;
0193 }
0194 
0195 inline bool ScriptPlugin::enabledByDefault() const
0196 {
0197     return true;
0198 }
0199 
0200 inline int ScriptPlugin::type() const
0201 {
0202     return ePluginType_Token;
0203 }
0204 
0205 inline const QStringList &ScriptPlugin::supportedTokens() const
0206 {
0207     return m_keys;
0208 }
0209 
0210 inline const QStringList &ScriptPlugin::help() const
0211 {
0212     return m_help;
0213 }
0214 
0215 #endif // FILE_PLUGIN_H