1/*
2 * Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *
17 */
18
19#ifndef menufile_h
20#define menufile_h
21
22#include <QtXml/qdom.h>
23
24#include <QList>
25#include <QStringList>
26
27class MenuFile
28{
29public:
30 MenuFile(const QString &file);
31 ~MenuFile();
32
33 bool load();
34 bool save();
35 void create();
36 QString error() const { return m_error; } // Returns the last error message
37
38 void restoreMenuSystem(const QString &);
39
40 enum ActionType {
41 ADD_ENTRY = 0,
42 REMOVE_ENTRY,
43 ADD_MENU,
44 REMOVE_MENU,
45 MOVE_MENU
46 };
47
48 struct ActionAtom
49 {
50 ActionType action;
51 QString arg1;
52 QString arg2;
53 };
54
55 /**
56 * Create action atom and push it on the stack
57 */
58 ActionAtom *pushAction(ActionType action, const QString &arg1, const QString &arg2);
59
60 /**
61 * Pop @p atom from the stack.
62 * @p atom must be last item on the stack
63 */
64 void popAction(ActionAtom *atom);
65
66 /**
67 * Perform the specified action
68 */
69 void performAction(const ActionAtom *);
70
71 /**
72 * Perform all actions currently on the stack, remove them from the stack and
73 * save result
74 * @return whether save was successful
75 */
76 bool performAllActions();
77
78 /**
79 * Returns whether the stack contains any actions
80 */
81 bool dirty() const;
82
83 void addEntry(const QString &menuName, const QString &menuId);
84 void removeEntry(const QString &menuName, const QString &menuId);
85
86 void addMenu(const QString &menuName, const QString &menuFile);
87 void moveMenu(const QString &oldMenu, const QString &newMenu);
88 void removeMenu(const QString &menuName);
89
90 void setLayout(const QString &menuName, const QStringList &layout);
91
92 /**
93 * Returns a unique menu-name for a new menu under @p menuName
94 * inspired by @p newMenu and not part of @p excludeList
95 */
96 QString uniqueMenuName(const QString &menuName, const QString &newMenu, const QStringList &excludeList);
97
98protected:
99 /**
100 * Finds menu @p menuName in @p elem.
101 * If @p create is true, the menu is created if it doesn't exist yet.
102 * @return The menu dom-node of @p menuName
103 */
104 QDomElement findMenu(QDomElement elem, const QString &menuName, bool create);
105
106private:
107 QString m_error;
108 QString m_fileName;
109
110 QDomDocument m_doc;
111 bool m_bDirty;
112
113 QList<ActionAtom*> m_actionList;
114 QStringList m_removedEntries;
115};
116
117
118#endif
119