File indexing completed on 2024-04-28 17:06:32

0001 /*
0002     SPDX-FileCopyrightText: 2004 Jonas Bähr <jonas.baehr@web.de>
0003     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef USERACTION_H
0009 #define USERACTION_H
0010 
0011 // QtCore
0012 #include <QList>
0013 #include <QSet>
0014 #include <QString>
0015 
0016 class QDomDocument;
0017 class QDomElement;
0018 class QStringList;
0019 class KrAction;
0020 class QUrl;
0021 class KActionMenu;
0022 
0023 /**
0024  * Useractions are Krusaders backend for user-defined actions on
0025  * current/selected files in its panels and for krusader's internal actions
0026  * which need some parameter.
0027  *
0028  * There are several components:
0029  * - The UserAction class as a Manager
0030  * - The interface to KDE's action-system (the KrAction)
0031  * - The Expander, which parses the commandline for placeholders and calls
0032  *   the internal actions
0033  * - A widget to manipulate the UserAction's Properties via GUI
0034  *   (ActionProperty)
0035  *
0036  * The Useractions are stored in XML-files. Currently there are two main files.
0037  * The first is a global example-file which is read only (read after the other
0038  * actionfiles, duplicates are ignored) and a local file where the actions are
0039  * saved.
0040  * This class reads only the container and passes each action-tag to the new
0041  * KrAction, which reads it's data itself.
0042  */
0043 
0044 class UserAction
0045 {
0046 public:
0047     typedef QList<KrAction *> KrActionList;
0048 
0049     enum ReadMode { renameDoublicated, ignoreDoublicated };
0050 
0051     /**
0052      * The constructor reads all useractions, see readAllFiles()
0053      */
0054     UserAction();
0055     ~UserAction();
0056 
0057     /**
0058      * adds an action to the collection.
0059      */
0060     void addKrAction(KrAction *action)
0061     {
0062         _actions.append(action);
0063     };
0064 
0065     /**
0066      * Use this to access the whole list of registered KrActions.
0067      * currently only used to fill the usermenu with all available actions. This should change...
0068      * @return A reference to the internal KrActionList
0069      */
0070     const KrActionList &actionList()
0071     {
0072         return _actions;
0073     };
0074 
0075     /**
0076      * @return how many useractions exist
0077      */
0078     int count() const
0079     {
0080         return _actions.count();
0081     };
0082 
0083     /**
0084      * removes a KrAction from the internal list but does not delete it.
0085      * @param action the KrAction which should be removed
0086      */
0087     void removeKrAction(KrAction *action);
0088 
0089     /**
0090      * check for each KrAction if it is available for the current location / file and disables it if not
0091      */
0092     void setAvailability();
0093     /**
0094      * same as above but check for a specitic file
0095      * @param currentURL Check for this file
0096      */
0097     void setAvailability(const QUrl &currentURL);
0098 
0099     /**
0100      * Fills a KActionMenu with all available UserActions in the list
0101      * @param menu popupmenu to populate
0102      * @param currentURL the current URL
0103      */
0104     void populateMenu(KActionMenu *menu, const QUrl *currentURL);
0105 
0106     QStringList allCategories();
0107     QStringList allNames();
0108 
0109     /**
0110      * reads all predefined useractionfiles.
0111      */
0112     void readAllFiles();
0113     /**
0114      * writes all actions to the local actionfile
0115      */
0116     bool writeActionFile();
0117     /**
0118      * Reads UserActions from a xml-file.
0119      * @param filename the XML file
0120      * @param mode the read mode
0121      * @param list If provided, all new actions will also be added to this list
0122      */
0123     void readFromFile(const QString &filename, ReadMode mode = renameDoublicated, KrActionList *list = nullptr);
0124     /**
0125      * Reads UserActions from a XML-Element.
0126      * @param element a container with action-elements
0127      * @param mode the read mode
0128      * @param list If provided, all new actions will also be added to this list
0129      */
0130     void readFromElement(const QDomElement &element, ReadMode mode = renameDoublicated, KrActionList *list = nullptr);
0131 
0132     /**
0133      * creates an empty QDomDocument for the UserActions
0134      */
0135     static QDomDocument createEmptyDoc();
0136     /**
0137      * Writes a QDomDocument to an UTF-8 encodes text-file
0138      * @param doc the XML-Tree
0139      * @param filename the filename where to save
0140      * @return true on success, false otherwise
0141      * @warning any existing file will get overwritten!
0142      */
0143     static bool writeToFile(const QDomDocument &doc, const QString &filename);
0144 
0145 private:
0146     KrActionList _actions;
0147     QSet<QString> _defaultActions;
0148     QSet<QString> _deletedActions;
0149 };
0150 
0151 #define ACTION_XML "krusader/useractions.xml"
0152 #define ACTION_XML_EXAMPLES "krusader/useraction_examples.xml"
0153 
0154 #define ACTION_DOCTYPE "KrusaderUserActions"
0155 // in well formed XML the root-element has to have the same name then the doctype:
0156 #define ACTION_ROOT ACTION_DOCTYPE
0157 #define ACTION_PROCESSINSTR "version=\"1.0\" encoding=\"UTF-8\" "
0158 
0159 #endif // ifndef USERACTION_H