forked from lxqt/libqtxdg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdgmenuapplinkprocessor.cpp
315 lines (251 loc) · 9.16 KB
/
xdgmenuapplinkprocessor.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* 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 */
#include "xdgmenu.h"
#include "xdgmenuapplinkprocessor.h"
#include "xmlhelper.h"
#include "xdgdesktopfile.h"
#include <QDir>
/************************************************
************************************************/
XdgMenuApplinkProcessor::XdgMenuApplinkProcessor(QDomElement& element, XdgMenu* menu, XdgMenuApplinkProcessor *parent) :
QObject(parent)
{
mElement = element;
mParent = parent;
mMenu = menu;
mOnlyUnallocated = element.attribute("onlyUnallocated") == "1";
MutableDomElementIterator i(element, "Menu");
while(i.hasNext())
{
QDomElement e = i.next();
mChilds.append(new XdgMenuApplinkProcessor(e, mMenu, this));
}
}
/************************************************
************************************************/
XdgMenuApplinkProcessor::~XdgMenuApplinkProcessor()
{
}
/************************************************
************************************************/
void XdgMenuApplinkProcessor::run()
{
step1();
step2();
}
/************************************************
************************************************/
void XdgMenuApplinkProcessor::step1()
{
fillAppFileInfoList();
createRules();
// Check Include rules & mark as allocated ............
XdgMenuAppFileInfoHashIterator i(mAppFileInfoHash);
while(i.hasNext())
{
i.next();
XdgDesktopFile* file = i.value()->desktopFile();
if (mRules.checkInclude(i.key(), *file))
{
if (!mOnlyUnallocated)
i.value()->setAllocated(true);
if (!mRules.checkExclude(i.key(), *file))
{
mSelected.append(i.value());
}
}
}
// Process childs menus ...............................
foreach (XdgMenuApplinkProcessor* child, mChilds)
child->step1();
}
/************************************************
************************************************/
void XdgMenuApplinkProcessor::step2()
{
// Create AppLinks elements ...........................
QDomDocument doc = mElement.ownerDocument();
foreach (XdgMenuAppFileInfo* fileInfo, mSelected)
{
if (mOnlyUnallocated && fileInfo->allocated())
continue;
XdgDesktopFile* file = fileInfo->desktopFile();
// Means "this application exists, but don't display it in the menus".
if (file->value("NoDisplay").toBool())
continue;
// Hidden should have been called Deleted. It means the user deleted
// (at his level) something that was present
if (file->value("Hidden").toBool())
continue;
// File name of a binary on disk used to determine if the program is
// actually installed. If not, entry may not show in menus, etc.
QString s = file->value("TryExec").toString();
if (!s.isEmpty() && !checkTryExec(s))
continue;
// A list of strings identifying the environments that should display/not
// display a given desktop entry.
// OnlyShowIn ........
if (file->contains("OnlyShowIn"))
{
QString s = ";" + file->value("OnlyShowIn").toString() + ";";
bool show = false;
foreach (QString env, mMenu->environments())
{
if (s.contains(env, Qt::CaseInsensitive))
{
show = true;
break;
}
}
if (!show)
continue;
}
// NotShowIn .........
if (file->contains("NotShowIn"))
{
QString s = ";" + file->value("NotShowIn").toString() + ";";
bool show = true;
foreach (QString env, mMenu->environments())
{
if (s.contains(env, Qt::CaseInsensitive))
{
show = false;
break;
}
}
if (!show)
continue;
}
QDomElement appLink = doc.createElement("AppLink");
appLink.setAttribute("id", fileInfo->id());
appLink.setAttribute("title", file->localizedValue("Name").toString());
appLink.setAttribute("comment", file->localizedValue("Comment").toString());
appLink.setAttribute("genericName", file->localizedValue("GenericName").toString());
appLink.setAttribute("exec", file->value("Exec").toString());
appLink.setAttribute("terminal", file->value("Terminal").toBool());
appLink.setAttribute("startupNotify", file->value("StartupNotify").toBool());
appLink.setAttribute("path", file->value("Path").toString());
appLink.setAttribute("icon", file->value("Icon").toString());
appLink.setAttribute("desktopFile", file->fileName());
mElement.appendChild(appLink);
}
// Process childs menus ...............................
foreach (XdgMenuApplinkProcessor* child, mChilds)
child->step2();
}
/************************************************
For each <Menu> element, build a pool of desktop entries by collecting entries found
in each <AppDir> for the menu element. If two entries have the same desktop-file id,
the entry for the earlier (closer to the top of the file) <AppDir> must be discarded.
Next, add to the pool the entries for any <AppDir>s specified by ancestor <Menu>
elements. If a parent menu has a duplicate entry (same desktop-file id), the entry
for the child menu has priority.
************************************************/
void XdgMenuApplinkProcessor::fillAppFileInfoList()
{
// Build a pool by collecting entries found in <AppDir>
{
MutableDomElementIterator i(mElement, "AppDir");
i.toBack();
while(i.hasPrevious())
{
QDomElement e = i.previous();
findDesktopFiles(e.text(), "");
mElement.removeChild(e);
}
}
// Add the entries for ancestor <Menu> ................
if (mParent)
{
XdgMenuAppFileInfoHashIterator i(mParent->mAppFileInfoHash);
while(i.hasNext())
{
i.next();
//if (!mAppFileInfoHash.contains(i.key()))
mAppFileInfoHash.insert(i.key(), i.value());
}
}
}
/************************************************
************************************************/
void XdgMenuApplinkProcessor::findDesktopFiles(const QString& dirName, const QString& prefix)
{
QDir dir(dirName);
mMenu->addWatchPath(dir.absolutePath());
QFileInfoList files = dir.entryInfoList(QStringList("*.desktop"), QDir::Files);
foreach (QFileInfo file, files)
{
XdgDesktopFile* f = XdgDesktopFileCache::getFile(file.canonicalFilePath());
if (f)
mAppFileInfoHash.insert(prefix + file.fileName(), new XdgMenuAppFileInfo(f, prefix + file.fileName(), this));
}
// Working recursively ............
QFileInfoList dirs = dir.entryInfoList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot);
foreach (QFileInfo d, dirs)
{
QString dn = d.canonicalFilePath();
if (dn != dirName)
{
findDesktopFiles(dn, QString("%1%2-").arg(prefix, d.fileName()));
}
}
}
/************************************************
Create rules
************************************************/
void XdgMenuApplinkProcessor::createRules()
{
MutableDomElementIterator i(mElement, "");
while(i.hasNext())
{
QDomElement e = i.next();
if (e.tagName()=="Include")
{
mRules.addInclude(e);
mElement.removeChild(e);
}
else if (e.tagName()=="Exclude")
{
mRules.addExclude(e);
mElement.removeChild(e);
}
}
}
/************************************************
Check if the program is actually installed.
************************************************/
bool XdgMenuApplinkProcessor::checkTryExec(const QString& progName)
{
if (progName.startsWith(QDir::separator()))
return QFileInfo(progName).isExecutable();
QStringList dirs = QString(getenv("PATH")).split(":");
foreach (QString dir, dirs)
{
if (QFileInfo(QDir(dir), progName).isExecutable())
return true;
}
return false;
}