File indexing completed on 2024-02-18 05:27:41

0001 /*
0002  *   SPDX-FileCopyrightText: 2003 Waldo Bastian <bastian@kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only
0005  *
0006  */
0007 
0008 #ifndef menufile_h
0009 #define menufile_h
0010 
0011 #include <qdom.h>
0012 
0013 #include <QList>
0014 #include <QStringList>
0015 
0016 class MenuFile
0017 {
0018 public:
0019     static const QString MF_MENU, MF_PUBLIC_ID, MF_SYSTEM_ID, MF_NAME, MF_INCLUDE, MF_EXCLUDE, MF_FILENAME, MF_DELETED, MF_NOTDELETED, MF_MOVE, MF_OLD, MF_NEW,
0020         MF_DIRECTORY, MF_LAYOUT, MF_MENUNAME, MF_SEPARATOR, MF_MERGE;
0021 
0022     explicit MenuFile(const QString &file);
0023     ~MenuFile();
0024 
0025     bool load();
0026     bool save();
0027     void create();
0028     QString error() const
0029     {
0030         return m_error;
0031     } // Returns the last error message
0032 
0033     void restoreMenuSystem(const QString &);
0034 
0035     enum ActionType {
0036         ADD_ENTRY = 0,
0037         REMOVE_ENTRY,
0038         ADD_MENU,
0039         REMOVE_MENU,
0040         MOVE_MENU,
0041     };
0042 
0043     struct ActionAtom {
0044         ActionType action;
0045         QString arg1;
0046         QString arg2;
0047     };
0048 
0049     /**
0050      * Create action atom and push it on the stack
0051      */
0052     ActionAtom *pushAction(ActionType action, const QString &arg1, const QString &arg2);
0053 
0054     /**
0055      * Pop @p atom from the stack.
0056      * @p atom must be last item on the stack
0057      */
0058     void popAction(ActionAtom *atom);
0059 
0060     /**
0061      * Perform the specified action
0062      */
0063     void performAction(const ActionAtom *);
0064 
0065     /**
0066      * Perform all actions currently on the stack, remove them from the stack and
0067      * save result
0068      * @return whether save was successful
0069      */
0070     bool performAllActions();
0071 
0072     /**
0073      * Returns whether the stack contains any actions
0074      */
0075     bool dirty() const;
0076 
0077     void addEntry(const QString &menuName, const QString &menuId);
0078     void removeEntry(const QString &menuName, const QString &menuId);
0079 
0080     void addMenu(const QString &menuName, const QString &menuFile);
0081     void moveMenu(const QString &oldMenu, const QString &newMenu);
0082     void removeMenu(const QString &menuName);
0083 
0084     void setLayout(const QString &menuName, const QStringList &layout);
0085 
0086     /**
0087      * Returns a unique menu-name for a new menu under @p menuName
0088      * inspired by @p newMenu and not part of @p excludeList
0089      */
0090     QString uniqueMenuName(const QString &menuName, const QString &newMenu, const QStringList &excludeList);
0091 
0092 protected:
0093     /**
0094      * Finds menu @p menuName in @p elem.
0095      * If @p create is true, the menu is created if it doesn't exist yet.
0096      * @return The menu dom-node of @p menuName
0097      */
0098     QDomElement findMenu(QDomElement elem, const QString &menuName, bool create);
0099 
0100 private:
0101     QString m_error;
0102     QString m_fileName;
0103 
0104     QDomDocument m_doc;
0105     bool m_bDirty;
0106 
0107     QList<ActionAtom *> m_actionList;
0108     QStringList m_removedEntries;
0109 };
0110 
0111 #endif