File indexing completed on 2024-12-08 13:34:35

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