forked from flameshot-org/flameshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinlnkfileparse.cpp
More file actions
160 lines (136 loc) · 4.53 KB
/
winlnkfileparse.cpp
File metadata and controls
160 lines (136 loc) · 4.53 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
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
#include "winlnkfileparse.h"
#include <QDir>
#include <QDirIterator>
#include <QFileSystemModel>
#include <QImageWriter>
#include <QRegularExpression>
#include <QSettings>
#include <QString>
#include <shlobj.h>
WinLnkFileParser::WinLnkFileParser()
{
QStringList sListImgFileExt;
for (const auto& ext : QImageWriter::supportedImageFormats()) {
sListImgFileExt.append(ext);
}
this->getImageFileExtAssociates(sListImgFileExt);
}
DesktopAppData WinLnkFileParser::parseLnkFile(const QFileInfo& fiLnk,
bool& ok) const
{
DesktopAppData res;
ok = true;
QFileInfo fiSymlink(fiLnk.symLinkTarget());
if (!fiSymlink.exists() || !fiSymlink.fileName().endsWith(".exe") ||
fiSymlink.baseName().contains("unins")) {
ok = false;
return res;
}
res.name = fiLnk.baseName();
res.exec = fiSymlink.absoluteFilePath();
// Get icon from exe
QFileSystemModel* model = new QFileSystemModel;
model->setRootPath(fiSymlink.path());
res.icon = model->fileIcon(model->index(fiSymlink.filePath()));
if (m_GraphicAppsList.contains(fiSymlink.fileName())) {
res.categories = QStringList() << "Graphics";
} else {
res.categories = QStringList() << "Utility";
}
for (const auto& app : m_appList) {
if (app.exec == res.exec) {
ok = false;
break;
}
}
if (res.exec.isEmpty() || res.name.isEmpty()) {
ok = false;
}
return res;
}
int WinLnkFileParser::processDirectory(const QDir& dir)
{
QStringList sListMenuFilter;
sListMenuFilter << "Accessibility"
<< "Administrative Tools"
<< "Setup"
<< "System Tools"
<< "Uninstall"
<< "Update"
<< "Updater"
<< "Windows PowerShell";
const QString sMenuFilter("\\b(" + sListMenuFilter.join('|') + ")\\b");
QRegularExpression regexfilter(sMenuFilter);
bool ok;
int length = m_appList.length();
// Go through all subfolders and *.lnk files
QDirIterator it(dir.absolutePath(),
{ "*.lnk" },
QDir::NoFilter,
QDirIterator::Subdirectories);
while (it.hasNext()) {
QFileInfo fiLnk(it.next());
if (!regexfilter.match(fiLnk.absoluteFilePath()).hasMatch()) {
DesktopAppData app = parseLnkFile(fiLnk, ok);
if (ok) {
m_appList.append(app);
}
}
}
return m_appList.length() - length;
}
QVector<DesktopAppData> WinLnkFileParser::getAppsByCategory(
const QString& category)
{
QVector<DesktopAppData> res;
for (const DesktopAppData& app : qAsConst(m_appList)) {
if (app.categories.contains(category)) {
res.append(app);
}
}
std::sort(res.begin(), res.end(), CompareAppByName());
return res;
}
QMap<QString, QVector<DesktopAppData>> WinLnkFileParser::getAppsByCategory(
const QStringList& categories)
{
QMap<QString, QVector<DesktopAppData>> res;
QVector<DesktopAppData> tmpAppList;
for (const QString& category : categories) {
tmpAppList = getAppsByCategory(category);
for (const DesktopAppData& app : qAsConst(tmpAppList)) {
res[category].append(app);
}
}
return res;
}
QString WinLnkFileParser::getAllUsersStartMenuPath()
{
QString sRet("");
WCHAR path[MAX_PATH];
HRESULT hr = SHGetFolderPathW(NULL, CSIDL_COMMON_PROGRAMS, NULL, 0, path);
if (SUCCEEDED(hr)) {
sRet = QDir(QString::fromWCharArray(path)).absolutePath();
}
return sRet;
}
void WinLnkFileParser::getImageFileExtAssociates(const QStringList& sListImgExt)
{
const QString sReg("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\"
"CurrentVersion\\Explorer\\FileExts\\.%1\\OpenWithList");
for (const auto& sExt : qAsConst(sListImgExt)) {
QString sPath(sReg.arg(sExt));
QSettings registry(sPath, QSettings::NativeFormat);
for (const auto& key : registry.allKeys()) {
if (1 == key.size()) { // Keys for OpenWith apps are a, b, c, ...
QString sVal = registry.value(key, "").toString();
if (sVal.endsWith(".exe") &&
!m_GraphicAppsList.contains(sVal)) {
m_GraphicAppsList << sVal;
}
}
}
}
}