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

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2002 Dominik Seichter <domseichter@web.de>
0003 
0004 #ifndef FILE_PLUGIN_H
0005 #define FILE_PLUGIN_H
0006 
0007 #include "plugin.h"
0008 
0009 class KService;
0010 
0011 class FilePlugin : public Plugin
0012 {
0013 public:
0014     /** Create a new FilePlugin from a KService
0015      *
0016      *  @param service pointer to a KService
0017      */
0018     FilePlugin(PluginLoader *loader, KService *service);
0019 
0020     ~FilePlugin() override;
0021 
0022     /**
0023      * @returns a name of the plugin that can be displayed
0024      *          to the user. This name should be internationalized.
0025      */
0026     inline const QString name() const override;
0027 
0028     /**
0029      * @returns the type of the plugin.
0030      */
0031     inline int type() const override;
0032 
0033     /**
0034      * @returns an icon for this plugin.
0035      */
0036     const QIcon icon() const override;
0037 
0038     /**
0039      * @returns true if this plugins is always enabled
0040      *
0041      * Warning: If you return true here, the user has no possibility to
0042      *          disable this plugin.
0043      */
0044     inline bool enabledByDefault() const override;
0045 
0046     /**
0047      * This function is the core of your plugin.
0048      *
0049      * It does the actual processing of a file, filename or token depending of the type
0050      * of your plugin.
0051      *
0052      * \see type()
0053      *
0054      * @param b the parent BatchRenamer instance calling this plugin
0055      * @param index the index of the current file (i.e. the first file has index 0,
0056      *              the second file to be renamed has index 1 ....)
0057      * @param filenameOrToken this parameter depends on the type of your plugin.
0058      *                        If type is ePluginType_File, this is the absolute path
0059      *                        or URL to the renamed file.
0060      *                        If type is ePluginType_Filename, this is the filename
0061      *                        (without path) as created by KRename.
0062      *                        If type is ePluginType_Token, this is the contents of a token
0063      *                        in brackets. If your plugin supports the token [example],
0064      *                        KRename will pass the strign "example" to your method.
0065      * @param eCurrentType the current type of plugin that is requested (for plugins that support more than one type)
0066      *
0067      * @returns the result of the function, depending on type().
0068      * @returns an empty QString if this plugin has nothing to do.
0069      * @returns A new filename if type is ePluginType_Filename
0070      * @returns the value of the token if type is ePluginType_Token
0071      * @returns an error message or an empty QString if type is ePluginType_File
0072      */
0073     QString processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType eCurrentType) override = 0;
0074 
0075     /** Get a list of all tokens supported by this plugin.
0076      *
0077      *  If the token type != ePluginType_Token you have to return an empty list
0078      *
0079      *  @returns a list of all supported tokens.
0080      */
0081     inline const QStringList &supportedTokens() const override;
0082 
0083     /** Returns help descriptions for the supported tokens
0084      *
0085      *  The returned stringlist contains strings that are the tokens
0086      *  and the description separated by ;;
0087      *
0088      *  @returns a stringlist containing help on the supported tokens
0089      */
0090     inline const QStringList &help() const override;
0091 
0092     /** Create a user interface for this plugin
0093      *
0094      *  @param parent the parent widget of this plugin
0095      *
0096      *  This is implemented here for all FilePlugin based classed
0097      */
0098     void createUI(QWidget *parent) const override;
0099 
0100 protected:
0101     FilePlugin(PluginLoader *loader);
0102 
0103     /**
0104      *  Checks if a token is supported by this plugin.
0105      *
0106      *  @param token a token
0107      *  @returns true if the token is supported
0108      *
0109      *  @see addSupportedToken
0110      */
0111     bool supports(const QString &token);
0112 
0113     /**
0114      *  Adds a token to the list of supported tokens
0115      *
0116      *  @param token will be a supported token from now on
0117      *
0118      *  @see supports
0119      */
0120     inline void addSupportedToken(const QString &token)
0121     {
0122         m_keys.append(token);
0123     }
0124 
0125 protected:
0126 
0127     QString m_name;
0128     QString m_comment;
0129     QString m_icon;
0130 
0131 private:
0132     QStringList m_keys;
0133 };
0134 
0135 inline const QString FilePlugin::name() const
0136 {
0137     return m_name;
0138 }
0139 
0140 inline bool FilePlugin::enabledByDefault() const
0141 {
0142     return true;
0143 }
0144 
0145 inline int FilePlugin::type() const
0146 {
0147     return ePluginType_Token;
0148 }
0149 
0150 inline const QStringList &FilePlugin::supportedTokens() const
0151 {
0152     return m_keys;
0153 }
0154 
0155 inline const QStringList &FilePlugin::help() const
0156 {
0157     return m_keys;
0158 }
0159 
0160 #endif // FILE_PLUGIN_H