forked from lxqt/libqtxdg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdgmenurules.cpp
288 lines (204 loc) · 8.31 KB
/
xdgmenurules.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* Razor - a lightweight, Qt based, desktop toolset
* http://razor-qt.org
*
* Copyright: 2010-2011 Razor team
* Authors:
* Alexander Sokoloff <[email protected]>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
/*********************************************************************
See: http://standards.freedesktop.org/desktop-entry-spec
*********************************************************************/
#include "xdgmenurules.h"
#include "xmlhelper.h"
#include <QStringList>
#include <QDebug>
/************************************************
************************************************/
XdgMenuRule::XdgMenuRule(const QDomElement& element, QObject* parent) :
QObject(parent)
{
Q_UNUSED(element)
}
/************************************************
************************************************/
XdgMenuRule::~XdgMenuRule()
{
}
/************************************************
The <Or> element contains a list of matching rules. If any of the matching rules
inside the <Or> element match a desktop entry, then the entire <Or> rule matches
the desktop entry.
************************************************/
XdgMenuRuleOr::XdgMenuRuleOr(const QDomElement& element, QObject* parent) :
XdgMenuRule(element, parent)
{
//qDebug() << "Create OR rule";
DomElementIterator iter(element, "");
while(iter.hasNext())
{
QDomElement e = iter.next();
if (e.tagName() == "Or")
mChilds.append(new XdgMenuRuleOr(e, this));
else if (e.tagName() == "And")
mChilds.append(new XdgMenuRuleAnd(e, this));
else if (e.tagName() == "Not")
mChilds.append(new XdgMenuRuleNot(e, this));
else if (e.tagName() == "Filename")
mChilds.append(new XdgMenuRuleFileName(e, this));
else if (e.tagName() == "Category")
mChilds.append(new XdgMenuRuleCategory(e, this));
else if (e.tagName() == "All")
mChilds.append(new XdgMenuRuleAll(e, this));
else
qWarning() << "Unknown rule" << e.tagName();
}
}
/************************************************
************************************************/
bool XdgMenuRuleOr::check(const QString& desktopFileId, const XdgDesktopFile& desktopFile)
{
for (QLinkedList<XdgMenuRule*>::Iterator i=mChilds.begin(); i!=mChilds.end(); ++i)
if ((*i)->check(desktopFileId, desktopFile)) return true;
return false;
}
/************************************************
The <And> element contains a list of matching rules. If each of the matching rules
inside the <And> element match a desktop entry, then the entire <And> rule matches
the desktop entry.
************************************************/
XdgMenuRuleAnd::XdgMenuRuleAnd(const QDomElement& element, QObject *parent) :
XdgMenuRuleOr(element, parent)
{
// qDebug() << "Create AND rule";
}
/************************************************
************************************************/
bool XdgMenuRuleAnd::check(const QString& desktopFileId, const XdgDesktopFile& desktopFile)
{
for (QLinkedList<XdgMenuRule*>::Iterator i=mChilds.begin(); i!=mChilds.end(); ++i)
if (!(*i)->check(desktopFileId, desktopFile)) return false;
return mChilds.count();
}
/************************************************
The <Not> element contains a list of matching rules. If any of the matching rules
inside the <Not> element matches a desktop entry, then the entire <Not> rule does
not match the desktop entry. That is, matching rules below <Not> have a logical OR
relationship.
************************************************/
XdgMenuRuleNot::XdgMenuRuleNot(const QDomElement& element, QObject *parent) :
XdgMenuRuleOr(element, parent)
{
// qDebug() << "Create NOT rule";
}
/************************************************
************************************************/
bool XdgMenuRuleNot::check(const QString& desktopFileId, const XdgDesktopFile& desktopFile)
{
return ! XdgMenuRuleOr::check(desktopFileId, desktopFile);
}
/************************************************
The <Filename> element is the most basic matching rule. It matches a desktop entry
if the desktop entry has the given desktop-file id. See Desktop-File Id.
************************************************/
XdgMenuRuleFileName::XdgMenuRuleFileName(const QDomElement& element, QObject *parent) :
XdgMenuRule(element, parent)
{
//qDebug() << "Create FILENAME rule";
mId = element.text();
}
/************************************************
************************************************/
bool XdgMenuRuleFileName::check(const QString& desktopFileId, const XdgDesktopFile& desktopFile)
{
Q_UNUSED(desktopFile)
//qDebug() << "XdgMenuRuleFileName:" << desktopFileId << mId;
return desktopFileId == mId;
}
/************************************************
The <Category> element is another basic matching predicate. It matches a desktop entry
if the desktop entry has the given category in its Categories field.
************************************************/
XdgMenuRuleCategory::XdgMenuRuleCategory(const QDomElement& element, QObject *parent) :
XdgMenuRule(element, parent)
{
mCategory = element.text();
}
/************************************************
************************************************/
bool XdgMenuRuleCategory::check(const QString& desktopFileId, const XdgDesktopFile& desktopFile)
{
Q_UNUSED(desktopFileId)
return desktopFile.value("Categories").toString().split(';').contains(mCategory);
}
/************************************************
The <All> element is a matching rule that matches all desktop entries.
************************************************/
XdgMenuRuleAll::XdgMenuRuleAll(const QDomElement& element, QObject *parent) :
XdgMenuRule(element, parent)
{
}
/************************************************
************************************************/
bool XdgMenuRuleAll::check(const QString& desktopFileId, const XdgDesktopFile& desktopFile)
{
Q_UNUSED(desktopFileId)
Q_UNUSED(desktopFile)
return true;
}
/************************************************
************************************************/
XdgMenuRules::XdgMenuRules(QObject* parent) :
QObject(parent)
{
}
/************************************************
************************************************/
XdgMenuRules::~XdgMenuRules()
{
}
/************************************************
************************************************/
void XdgMenuRules::addInclude(const QDomElement& element)
{
mIncludeRules.append(new XdgMenuRuleOr(element, this));
}
/************************************************
************************************************/
void XdgMenuRules::addExclude(const QDomElement& element)
{
mExcludeRules.append(new XdgMenuRuleOr(element, this));
}
/************************************************
************************************************/
bool XdgMenuRules::checkInclude(const QString& desktopFileId, const XdgDesktopFile& desktopFile)
{
for (QLinkedList<XdgMenuRule*>::Iterator i=mIncludeRules.begin(); i!=mIncludeRules.end(); ++i)
if ((*i)->check(desktopFileId, desktopFile)) return true;
return false;
}
/************************************************
************************************************/
bool XdgMenuRules::checkExclude(const QString& desktopFileId, const XdgDesktopFile& desktopFile)
{
for (QLinkedList<XdgMenuRule*>::Iterator i=mExcludeRules.begin(); i!=mExcludeRules.end(); ++i)
if ((*i)->check(desktopFileId, desktopFile)) return true;
return false;
}