-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexTask.cpp
More file actions
438 lines (383 loc) · 11.9 KB
/
IndexTask.cpp
File metadata and controls
438 lines (383 loc) · 11.9 KB
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
#include "IndexTask.h"
#include <QDir>
#include <QFileInfo>
#include <QCryptographicHash>
#include "ChineseLetterHelper.h"
#include "LogFile.h"
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif
#ifdef Q_OS_LINUX
#include <QSettings>
#include <QStandardPaths>
#include <libintl.h>
#endif
bool IndexTask::m_indexing = false;
void IndexTask::index()
{
m_run = true;
start();
}
void IndexTask::stop()
{
m_run = false;
wait();
}
#ifdef Q_OS_LINUX
static QStringList getDefaultLinuxIndexPaths()
{
QStringList paths;
paths.append("/usr/share/applications");
paths.append(QString("%1/.local/share/applications").arg(QDir::homePath()));
paths.append("/usr/share/ubuntu/applications");
paths.append("/var/lib/snapd/desktop/applications");
return paths;
}
static bool hasAppImageSuffix(const QString& filePath)
{
return QFileInfo(filePath).suffix().compare("AppImage", Qt::CaseInsensitive) == 0;
}
static bool isAppImageLauncher(const QFileInfo& fileInfo)
{
if (hasAppImageSuffix(fileInfo.filePath()))
{
return fileInfo.isExecutable();
}
if (fileInfo.isSymLink())
{
QFileInfo targetInfo(fileInfo.symLinkTarget());
return hasAppImageSuffix(targetInfo.filePath()) && targetInfo.isExecutable();
}
return false;
}
static QString normalizedDesktopExec(QSettings& desktopFile)
{
QString exec = desktopFile.value("/Desktop Entry/Exec").toString();
exec = exec.replace(QRegExp("%[a-zA-Z]"), "").trimmed();
return exec.split(' ', QString::SkipEmptyParts).join(' ');
}
static QString firstCommandToken(const QString& command)
{
QString trimmed = command.trimmed();
if (trimmed.isEmpty())
{
return QString();
}
if (trimmed.startsWith('"') || trimmed.startsWith('\''))
{
const QChar quote = trimmed[0];
const int end = trimmed.indexOf(quote, 1);
if (end > 0)
{
return trimmed.mid(1, end - 1);
}
}
return trimmed.split(' ', QString::SkipEmptyParts).first();
}
static QString appImageDedupeKeyFromPath(const QString& filePath)
{
const QString canonicalPath = QFileInfo(filePath).canonicalFilePath();
return QString("appimage:%1").arg(canonicalPath.isEmpty() ? filePath : canonicalPath);
}
static QString desktopDedupeKey(const QString& desktopFilePath)
{
QSettings desktopFile(desktopFilePath, QSettings::IniFormat);
desktopFile.setIniCodec("UTF8");
const QString exec = normalizedDesktopExec(desktopFile);
const QString execPath = firstCommandToken(exec);
if (hasAppImageSuffix(execPath))
{
return appImageDedupeKeyFromPath(execPath);
}
if (!exec.isEmpty())
{
return QString("desktop:%1").arg(exec);
}
const QString canonicalPath = QFileInfo(desktopFilePath).canonicalFilePath();
return QString("desktop-file:%1").arg(canonicalPath.isEmpty() ? desktopFilePath : canonicalPath);
}
static QString appImageDedupeKey(const QFileInfo& fileInfo)
{
const QString sourcePath = fileInfo.isSymLink() ? fileInfo.symLinkTarget() : fileInfo.filePath();
return appImageDedupeKeyFromPath(sourcePath);
}
static QString appImageThumbnailPath(const QString& filePath)
{
const QString uri = QString("file://%1").arg(filePath);
const QByteArray hash = QCryptographicHash::hash(uri.toUtf8(), QCryptographicHash::Md5).toHex();
const QString cacheRoot = QString("%1/.cache/thumbnails").arg(QDir::homePath());
QStringList thumbnailPaths;
thumbnailPaths.append(QString("%1/large/%2.png").arg(cacheRoot, QString::fromLatin1(hash)));
thumbnailPaths.append(QString("%1/normal/%2.png").arg(cacheRoot, QString::fromLatin1(hash)));
foreach (const QString& thumbnailPath, thumbnailPaths)
{
if (QFileInfo::exists(thumbnailPath))
{
return thumbnailPath;
}
}
return QString();
}
static void getAppImageProgramInfo(const QFileInfo& fileInfo, ProgramInfo& info)
{
info.name = fileInfo.completeBaseName();
if (info.name.isEmpty())
{
info.name = fileInfo.fileName();
}
info.pyname = ChineseLetterHelper::GetFirstLetters(info.name);
info.path = fileInfo.filePath();
const QString iconSourcePath = fileInfo.isSymLink() ? fileInfo.symLinkTarget() : fileInfo.filePath();
info.iconPath = appImageThumbnailPath(iconSourcePath);
if (info.iconPath.isEmpty())
{
info.iconPath = "application-x-executable";
}
info.desktopPath.clear();
}
bool IndexTask::shouldSkipDesktopEntry(QSettings &desktopFile)
{
// 无 Exec 无法启动
if (!desktopFile.contains("/Desktop Entry/Exec"))
{
return true;
}
// 仅索引应用类型;Type 缺省视为 Application
QString type = desktopFile.value("/Desktop Entry/Type").toString();
if (!type.isEmpty() && type.compare("Application", Qt::CaseInsensitive) != 0)
{
return true;
}
// NoDisplay / Hidden:不应展示或已被用户“删除”的条目
if (desktopFile.value("/Desktop Entry/NoDisplay").toString()
.compare("true", Qt::CaseInsensitive) == 0)
{
return true;
}
if (desktopFile.value("/Desktop Entry/Hidden").toString()
.compare("true", Qt::CaseInsensitive) == 0)
{
return true;
}
// OnlyShowIn / NotShowIn:按当前桌面环境过滤(如 xscreensaver 的 OnlyShowIn=MATE)
QStringList currentDesktops =
QString::fromLocal8Bit(qgetenv("XDG_CURRENT_DESKTOP"))
.split(':', QString::SkipEmptyParts);
QString onlyShowIn = desktopFile.value("/Desktop Entry/OnlyShowIn").toString();
if (!onlyShowIn.isEmpty())
{
bool match = false;
foreach (const QString &env, onlyShowIn.split(';', QString::SkipEmptyParts))
{
if (currentDesktops.contains(env, Qt::CaseInsensitive))
{
match = true;
break;
}
}
if (!match)
{
return true;
}
}
QString notShowIn = desktopFile.value("/Desktop Entry/NotShowIn").toString();
if (!notShowIn.isEmpty())
{
foreach (const QString &env, notShowIn.split(';', QString::SkipEmptyParts))
{
if (currentDesktops.contains(env, Qt::CaseInsensitive))
{
return true;
}
}
}
// 屏保等非日常应用类别
QStringList categories = desktopFile.value("/Desktop Entry/Categories")
.toString().split(';', QString::SkipEmptyParts);
if (categories.contains("Screensaver", Qt::CaseInsensitive))
{
return true;
}
// TryExec:指定的可执行文件不存在则不索引
QString tryExec = desktopFile.value("/Desktop Entry/TryExec").toString().trimmed();
if (!tryExec.isEmpty())
{
bool exists = tryExec.startsWith('/')
? QFileInfo::exists(tryExec)
: !QStandardPaths::findExecutable(tryExec).isEmpty();
if (!exists)
{
return true;
}
}
return false;
}
bool IndexTask::appendProgramInfo(const ProgramInfo& info, const QString& dedupeKey)
{
if (info.name.isEmpty())
{
return false;
}
if (!dedupeKey.isEmpty())
{
if (m_seenProgramKeys.contains(dedupeKey))
{
return false;
}
m_seenProgramKeys.insert(dedupeKey);
}
m_programs.append(info);
return true;
}
void IndexTask::getProgramInfo(QString desktopFilePath, ProgramInfo &info)
{
QSettings desktopFile(desktopFilePath, QSettings::IniFormat);
desktopFile.setIniCodec("UTF8");
if (shouldSkipDesktopEntry(desktopFile))
{
return;
}
QString appName = desktopFile.value("/Desktop Entry/Name").toString();
if (desktopFile.contains("/Desktop Entry/X-Ubuntu-Gettext-Domain"))
{
QString domain = desktopFile.value("/Desktop Entry/X-Ubuntu-Gettext-Domain").toString();
QByteArray bDomain = domain.toUtf8();
QByteArray bAppName = appName.toUtf8();
appName = QString::fromUtf8(dgettext(bDomain.data(), bAppName.data()));
}
else if (desktopFile.contains("/Desktop Entry/Name[zh_CN]"))
{
appName = desktopFile.value("/Desktop Entry/Name[zh_CN]").toString();
}
info.name = appName;
info.pyname = ChineseLetterHelper::GetFirstLetters(info.name);
info.desktopPath = desktopFilePath; // 启动时交给 gio launch 按规范处理
QString exec = normalizedDesktopExec(desktopFile);
QString execPath = firstCommandToken(exec);
if (execPath.startsWith("/"))
{
info.path = execPath;
}
else
{
info.path = QString("/usr/bin/") + execPath;
}
QString icon = desktopFile.value("/Desktop Entry/Icon").toString();
if (icon.isEmpty())
{
info.iconPath = "application-x-executable";
}
else
{
info.iconPath = icon;
}
}
#endif
void IndexTask::indexProgram(QString strDir)
{
if (!m_run)
{
return;
}
QDir sourceDir(strDir);
if(!sourceDir.exists()){
return;
}
sourceDir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
QFileInfoList fileInfoList = sourceDir.entryInfoList();
foreach(QFileInfo fileInfo, fileInfoList){
if(fileInfo.isDir()){
indexProgram(fileInfo.filePath());
}
else{
#ifdef Q_OS_WIN32
if (fileInfo.suffix() == "exe" ||
fileInfo.suffix() == "lnk" ||
fileInfo.suffix() == "bat")
{
ProgramInfo info;
info.name = fileInfo.fileName();
info.path = fileInfo.filePath();
info.pyname = ChineseLetterHelper::GetFirstLetters(info.name);
m_programs.append(info);
}
#else
if (fileInfo.suffix() == "desktop")
{
ProgramInfo info;
getProgramInfo(fileInfo.filePath(), info);
appendProgramInfo(info, desktopDedupeKey(fileInfo.filePath()));
}
else if (isAppImageLauncher(fileInfo))
{
ProgramInfo info;
getAppImageProgramInfo(fileInfo, info);
appendProgramInfo(info, appImageDedupeKey(fileInfo));
}
#endif
}
}
}
void IndexTask::run()
{
m_indexing = true;
#ifdef Q_OS_WIN32
IndexDatabase database("IndexTask", "IndexCacheTemp.db");
database.load();
database.clear();
QStringList& indexPath = GetSettings()->m_indexCfg.indexPath;
for (int i = 0; i < indexPath.size() && m_run; i++)
{
wchar_t drive[10] = {0};
indexPath[i].toWCharArray(drive);
qLog(QString("Index drive %1 ...").arg(indexPath[i]));
if (GetSettings()->m_indexCfg.enableNormalIndex)
{
indexProgram(indexPath[i]);
if (!m_programs.isEmpty())
{
database.insert(m_programs);
m_programs.clear();
}
}
else
{
USN nextusn;
if (!fs->Traverse(drive,&nextusn))
{
qLog("Index from USN failed,use normal traverse");
indexProgram(indexPath[i]);
if (!m_programs.isEmpty())
{
database.insert(m_programs);
}
}
else
{
fs->SaveToDataBase(&database);
}
}
}
#else
IndexDatabase database("IndexTask");
database.load();
database.clear();
m_seenProgramKeys.clear();
QStringList indexPath = GetSettings()->m_indexCfg.indexPath;
if (indexPath.isEmpty())
{
indexPath = getDefaultLinuxIndexPaths();
}
for (int i = 0; i < indexPath.size() && m_run; i++)
{
qLog(QString("Index path %1 ...").arg(indexPath[i]));
indexProgram(indexPath[i]);
}
if (!m_programs.isEmpty())
{
database.insert(m_programs);
m_programs.clear();
}
#endif
m_indexing = false;
}