forked from lxqt/libqtxdg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdgmenureader.cpp
456 lines (363 loc) · 14.4 KB
/
xdgmenureader.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* 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 <sokoloff.a@gmail.com>
*
* 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 "xdgmenureader.h"
#include "xdgmenu.h"
#include "xdgdirs.h"
#include "xmlhelper.h"
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <QString>
#include <QDir>
#include <QDebug>
#include <QtXml/QDomNamedNodeMap>
#include <QtXml/QDomNode>
/************************************************
************************************************/
XdgMenuReader::XdgMenuReader(XdgMenu* menu, XdgMenuReader* parentReader, QObject *parent) :
QObject(parent),
mMenu(menu)
{
mParentReader = parentReader;
if (mParentReader)
mBranchFiles << mParentReader->mBranchFiles;
}
/************************************************
************************************************/
XdgMenuReader::~XdgMenuReader()
{
}
/************************************************
************************************************/
bool XdgMenuReader::load(const QString& fileName, const QString& baseDir)
{
if (fileName.isEmpty())
{
mErrorStr = tr("Menu file not defined.");
return false;
}
QFileInfo fileInfo(QDir(baseDir), fileName);
mFileName = fileInfo.canonicalFilePath();
mDirName = fileInfo.canonicalPath();
if (mBranchFiles.contains(mFileName))
return false; // Recursive loop detected
mBranchFiles << mFileName;
QFile file(mFileName);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
mErrorStr = tr("%1 not loading: %2").arg(fileName).arg(file.errorString());
return false;
}
//qDebug() << "Load file:" << mFileName;
mMenu->addWatchPath(mFileName);
QString errorStr;
int errorLine;
int errorColumn;
if (!mXml.setContent(&file, true, &errorStr, &errorLine, &errorColumn))
{
mErrorStr = tr("Parse error at line %1, column %2:\n%3")
.arg(errorLine)
.arg(errorColumn)
.arg(errorStr);
return false;
}
QDomElement root = mXml.documentElement();
QDomElement debugElement = mXml.createElement("FileInfo");
debugElement.setAttribute("file", mFileName);
if (mParentReader)
debugElement.setAttribute("parent", mParentReader->fileName());
QDomNode null;
root.insertBefore(debugElement, null);
processMergeTags(root);
return true;
}
/************************************************
Duplicate <MergeXXX> elements (that specify the same file) are handled as with
duplicate <AppDir> elements (the last duplicate is used).
************************************************/
void XdgMenuReader::processMergeTags(QDomElement& element)
{
QDomElement n = element.lastChildElement();
QStringList mergedFiles;
while (!n.isNull())
{
QDomElement next = n.previousSiblingElement();
// MergeFile ..................
if (n.tagName() == "MergeFile")
{
processMergeFileTag(n, &mergedFiles);
n.parentNode().removeChild(n);
}
// MergeDir ...................
else if(n.tagName() == "MergeDir")
{
processMergeDirTag(n, &mergedFiles);
n.parentNode().removeChild(n);
}
// DefaultMergeDirs ...........
else if (n.tagName() == "DefaultMergeDirs")
{
processDefaultMergeDirsTag(n, &mergedFiles);
n.parentNode().removeChild(n);
}
// AppDir ...................
else if(n.tagName() == "AppDir")
{
processAppDirTag(n);
n.parentNode().removeChild(n);
}
// DefaultAppDirs .............
else if(n.tagName() == "DefaultAppDirs")
{
processDefaultAppDirsTag(n);
n.parentNode().removeChild(n);
}
// DirectoryDir ...................
else if(n.tagName() == "DirectoryDir")
{
processDirectoryDirTag(n);
n.parentNode().removeChild(n);
}
// DefaultDirectoryDirs ...........
else if(n.tagName() == "DefaultDirectoryDirs")
{
processDefaultDirectoryDirsTag(n);
n.parentNode().removeChild(n);
}
// Menu .......................
else if(n.tagName() == "Menu")
{
processMergeTags(n);
}
n = next;
}
}
/************************************************
Any number of <MergeFile> elements may be listed below a <Menu> element, giving
the name of another menu file to be merged into this one.
If fileName is not an absolute path then the file to be merged should be located
relative to the location of this menu.
If the type attribute is missing or set to "path" then the contents of the
<MergeFile> element indicates the file to be merged.
If the type attribute is set to "parent" and the file that contains this
<MergeFile> element is located under one of the paths specified by
$XDG_CONFIG_DIRS, the contents of the element should be ignored and the remaining
paths specified by $XDG_CONFIG_DIRS are searched for a file with the same relative
filename. The first file encountered should be merged. There should be no merging
at all if no matching file is found. ( Libmenu additional scans ~/.config/menus.)
************************************************/
void XdgMenuReader::processMergeFileTag(QDomElement& element, QStringList* mergedFiles)
{
//qDebug() << "Process " << element;// << "in" << mFileName;
if (element.attribute("type") != "parent")
{
mergeFile(element.text(), element, mergedFiles);
}
else
{
QString relativeName;
QStringList configDirs = XdgDirs::configDirs();
foreach (QString configDir, configDirs)
{
if (mFileName.startsWith(configDir))
{
relativeName = mFileName.mid(configDir.length());
configDirs.removeAll(configDir);
break;
}
}
if (relativeName.isEmpty())
{
QString configHome = XdgDirs::configHome();
if (mFileName.startsWith(configHome))
relativeName = mFileName.mid(configHome.length());
}
if (relativeName.isEmpty())
return;
foreach (QString configDir, configDirs)
{
if (QFileInfo(configDir + relativeName).exists())
{
mergeFile(configDir + relativeName, element, mergedFiles);
return;
}
}
}
}
/************************************************
A <MergeDir> contains the name of a directory. Each file in the given directory
which ends in the ".menu" extension should be merged in the same way that a
<MergeFile> would be. If the filename given as a <MergeDir> is not an absolute
path, it should be located relative to the location of the menu file being parsed.
The files inside the merged directory are not merged in any specified order.
Duplicate <MergeDir> elements (that specify the same directory) are handled as with
duplicate <AppDir> elements (the last duplicate is used).
KDE additional scans ~/.config/menus.
************************************************/
void XdgMenuReader::processMergeDirTag(QDomElement& element, QStringList* mergedFiles)
{
//qDebug() << "Process " << element;// << "in" << mFileName;
mergeDir(element.text(), element, mergedFiles);
element.parentNode().removeChild(element);
}
/************************************************
The element has no content. The element should be treated as if it were a list of
<MergeDir> elements containing the default merge directory locations. When expanding
<DefaultMergeDirs> to a list of <MergeDir>, the default locations that are earlier
in the search path go later in the <Menu> so that they have priority.
Note that a system that uses either gnome-applications.menu or kde-applications.menu
depending on the desktop environment in use must still use applications-merged as the
default merge directory in both cases.
Implementations may chose to use .menu files with names other than application.menu
for tasks or menus other than the main application menu. In that case the first part
of the name of the default merge directory is derived from the name of the .menu file.
************************************************/
void XdgMenuReader::processDefaultMergeDirsTag(QDomElement& element, QStringList* mergedFiles)
{
//qDebug() << "Process " << element;// << "in" << mFileName;
QString menuBaseName = QFileInfo(mMenu->menuFileName()).baseName();
int n = menuBaseName.lastIndexOf('-');
if (n>-1)
menuBaseName = menuBaseName.mid(n+1);
QStringList dirs = XdgDirs::configDirs();
dirs << XdgDirs::configHome();
foreach (QString dir, dirs)
{
mergeDir(QString("%1/menus/%2-merged").arg(dir).arg(menuBaseName), element, mergedFiles);
}
if (menuBaseName == "applications")
mergeFile(QString("%1/menus/applications-kmenuedit.menu").arg(XdgDirs::configHome()), element, mergedFiles);
}
/************************************************
If the filename given as an <AppDir> is not an absolute path, it should be located
relative to the location of the menu file being parsed.
************************************************/
void XdgMenuReader::processAppDirTag(QDomElement& element)
{
//qDebug() << "Process " << element;
addDirTag(element, "AppDir", element.text());
}
/************************************************
The element has no content. The element should be treated as if it were a list of
<AppDir> elements containing the default app dir locations
($XDG_DATA_DIRS/applications/).
menu-cache additional prepends $XDG_DATA_HOME/applications.
************************************************/
void XdgMenuReader::processDefaultAppDirsTag(QDomElement& element)
{
//qDebug() << "Process " << element;
QStringList dirs = XdgDirs::dataDirs();
dirs.prepend(XdgDirs::dataHome(false));
foreach (QString dir, dirs)
{
//qDebug() << "Add AppDir: " << dir + "/applications/";
addDirTag(element, "AppDir", dir + "/applications/");
}
}
/************************************************
If the filename given as a <DirectoryDir> is not an absolute path, it should be
located relative to the location of the menu file being parsed.
************************************************/
void XdgMenuReader::processDirectoryDirTag(QDomElement& element)
{
//qDebug() << "Process " << element;
addDirTag(element, "DirectoryDir", element.text());
}
/************************************************
The element has no content. The element should be treated as if it were a list of
<DirectoryDir> elements containing the default desktop dir locations
($XDG_DATA_DIRS/desktop-directories/).
menu-cache additional prepends $XDG_DATA_HOME/applications.
************************************************/
void XdgMenuReader::processDefaultDirectoryDirsTag(QDomElement& element)
{
//qDebug() << "Process " << element;
QStringList dirs = XdgDirs::dataDirs();
dirs.prepend(XdgDirs::dataHome(false));
foreach (QString dir, dirs)
addDirTag(element, "DirectoryDir", dir + "/desktop-directories/");
}
/************************************************
************************************************/
void XdgMenuReader::addDirTag(QDomElement& previousElement, const QString& tagName, const QString& dir)
{
QFileInfo dirInfo(mDirName, dir);
if (dirInfo.isDir())
{
// qDebug() << "\tAdding " + dirInfo.canonicalFilePath();
QDomElement element = mXml.createElement(tagName);
element.appendChild(mXml.createTextNode(dirInfo.canonicalFilePath()));
previousElement.parentNode().insertBefore(element, previousElement);
}
}
/************************************************
If fileName is not an absolute path then the file to be merged should be located
relative to the location of this menu file.
************************************************/
void XdgMenuReader::mergeFile(const QString& fileName, QDomElement& element, QStringList* mergedFiles)
{
XdgMenuReader reader(mMenu, this);
QFileInfo fileInfo(QDir(mDirName), fileName);
if (!fileInfo.exists())
return;
if (mergedFiles->contains(fileInfo.canonicalFilePath()))
{
//qDebug() << "\tSkip: allredy merged";
return;
}
//qDebug() << "Merge file: " << fileName;
mergedFiles->append(fileInfo.canonicalFilePath());
if (reader.load(fileName, mDirName))
{
//qDebug() << "\tOK";
QDomElement n = reader.xml().firstChildElement().firstChildElement();
while (!n.isNull())
{
// As a special exception, remove the <Name> element from the root
// element of each file being merged.
if (n.tagName() != "Name")
{
QDomNode imp = mXml.importNode(n, true);
element.parentNode().insertBefore(imp, element);
}
n = n.nextSiblingElement();
}
}
}
/************************************************
************************************************/
void XdgMenuReader::mergeDir(const QString& dirName, QDomElement& element, QStringList* mergedFiles)
{
QFileInfo dirInfo(mDirName, dirName);
if (dirInfo.isDir())
{
//qDebug() << "Merge dir: " << dirInfo.canonicalFilePath();
QDir dir = QDir(dirInfo.canonicalFilePath());
const QFileInfoList files = dir.entryInfoList(QStringList() << "*.menu", QDir::Files | QDir::Readable);
foreach (QFileInfo file, files)
mergeFile(file.canonicalFilePath(), element, mergedFiles);
}
}