-
Notifications
You must be signed in to change notification settings - Fork 2
/
JetbrainsApplication.cpp
313 lines (290 loc) · 12.4 KB
/
JetbrainsApplication.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
#include "JetbrainsApplication.h"
#include "ConfigKeys.h"
#include "macros.h"
#include <QDir>
#include <QDomDocument>
#include <QFileInfo>
#include <QMetaType>
#include <KApplicationTrader>
#include <KConfigGroup>
#include <KSharedConfig>
#include "kservice_version.h" // IWYU pragma: keep
#include <utility>
JetbrainsApplication::JetbrainsApplication(const QString &desktopFilePath, bool fileWatcher, bool shouldNotTrimEdition)
: QFileSystemWatcher(nullptr)
, fileWatcher(fileWatcher)
, desktopFilePath(desktopFilePath)
{
KConfigGroup config = KSharedConfig::openConfig(desktopFilePath)->group("Desktop Entry");
iconPath = config.readEntry("Icon");
executablePath = config.readEntry("Exec");
name = filterApplicationName(config.readEntry("Name"));
shortName = QString(name)
.remove(QLatin1String(" Edition"))
.remove(QLatin1String(" Professional"))
.remove(QLatin1String(" Ultimate"))
.remove(QLatin1String(" + JBR11"))
.remove(QLatin1String(" RC"))
.remove(QLatin1String(" EAP"))
.replace(QLatin1String("IntelliJ IDEA"), QLatin1String("IntelliJ"))
.replace(QLatin1String(" Community"), shouldNotTrimEdition ? QStringLiteral(" CE") : QString());
// Allow the user to search for both names like Android Studio
auto nameList = filterApplicationName(QString(name))
.remove(QLatin1String(" Professional"))
.remove(QLatin1String(" Community"))
.remove(QLatin1String(" Ultimate"))
.remove(QLatin1String(" Edu"))
.split(' ');
if (nameList.count() > 0) {
nameArray[0] = nameList.at(0);
if (nameList.count() == 2) {
nameArray[1] = nameList.at(1);
}
}
if (fileWatcher) {
connect(this, &QFileSystemWatcher::fileChanged, this, &JetbrainsApplication::configChanged);
}
}
void JetbrainsApplication::parseXMLFile(const QString &file, QString *debugMessage)
{
// Recent folders are in recentProjectDirectories.xml or in recentProjects.xml located
// If the method is triggered by the file watcher the content is provided
QString fileName = file;
if (fileName.isEmpty()) {
if (this->configFolder.isEmpty()) {
return;
}
if (QFileInfo::exists(this->configFolder + "recentProjectDirectories.xml")) {
fileName = this->configFolder + "recentProjectDirectories.xml";
}
if (QFileInfo::exists(this->configFolder + "recentProjects.xml")) {
fileName = this->configFolder + "recentProjects.xml";
}
// JetBrains Rider has a differently called file
if (QFileInfo::exists(this->configFolder + "recentSolutions.xml")) {
fileName = this->configFolder + "recentSolutions.xml";
}
if (fileName.isEmpty()) {
JBR_FILE_LOG_APPEND("No config file found for " + this->name + " " + this->desktopFilePath)
return;
}
JBR_FILE_LOG_APPEND("Config file found for " + this->name + " " + fileName)
}
if (fileWatcher) {
this->addPath(fileName);
}
if (QFileInfo(fileName).size() == 0) {
JBR_FILE_LOG_APPEND("File " + fileName + "for app " + name + " has empty content")
return;
}
parseOldStyleXMLFile(fileName);
if (recentlyUsed.isEmpty()) {
parseNewStyleXMLFile(fileName);
}
JBR_FILE_LOG_APPEND("===== Recently used project folder for " + this->name + "=====")
if (!recentlyUsed.isEmpty()) {
for (const auto &recent : std::as_const(recentlyUsed)) {
Q_UNUSED(recent)
JBR_FILE_LOG_APPEND(" " + recent.path)
}
} else {
JBR_FILE_LOG_APPEND(" NO PROJECTS")
}
JBR_FILE_LOG_APPEND("\n")
}
void JetbrainsApplication::parseXMLFiles(QList<JetbrainsApplication *> &apps, QString *debugMessage)
{
for (auto &app : std::as_const(apps)) {
app->parseXMLFile(QString(), debugMessage);
}
}
QList<JetbrainsApplication *> JetbrainsApplication::filterApps(QList<JetbrainsApplication *> &apps, QString *debugMessage)
{
QList<JetbrainsApplication *> notEmpty;
JBR_FILE_LOG_APPEND("========== Filter Jetbrains Apps, number of apps: " + QString::number(apps.count()))
for (auto const &app : std::as_const(apps)) {
if (!app->recentlyUsed.empty()) {
notEmpty.append(app);
JBR_FILE_LOG_APPEND("Found projects for: " + app->name + " " + QString::number(app->recentlyUsed.count()))
} else {
JBR_FILE_LOG_APPEND("Found no projects for: " + app->name)
delete app;
}
}
return notEmpty;
}
QMap<QString, QString> JetbrainsApplication::getInstalledApplicationPaths(const KConfigGroup &customMappingConfig, QString *debugMessage)
{
QMap<QString, QString> applicationPaths;
const KService::List apps = KApplicationTrader::query([debugMessage](const KService::Ptr &service) {
// Only take test files into account
if (QStandardPaths::isTestModeEnabled()) {
if (!service->entryPath().contains(".qttest")) {
return false;
}
}
if (service->entryPath().contains("jetbrains-toolbox")) {
return false;
}
if (service->entryPath().contains("jetbrains-") || service->entryPath().contains("com.jetbrains.")) {
JBR_FILE_LOG_APPEND("Found " + service->entryPath() + " based on entry path")
return true;
}
#if QT_VERSION_MAJOR == 6
if (service->property<QString>("StartupWMClass").startsWith("jetbrains-")) {
#elif KSERVICE_VERSION >= QT_VERSION_CHECK(5, 102, 0)
if (service->property("StartupWMClass", QMetaType::QString).toString().startsWith("jetbrains-")) {
#else
const auto stringVariant = QVariant::String;
if (service->property("StartupWMClass", QVariant::String).toString().startsWith("jetbrains-")) {
#endif
JBR_FILE_LOG_APPEND("Found " + service->entryPath() + " based on StartupWMClass")
return true;
}
return false;
});
for (const auto &service : apps) {
applicationPaths.insert(filterApplicationName(service->name()), service->entryPath());
}
// Add manually configured entries
if (!customMappingConfig.isValid()) {
return applicationPaths;
}
if (!customMappingConfig.keyList().isEmpty()) {
JBR_FILE_LOG_APPEND("========== Manually Configured Jetbrains Applications ==========")
for (const auto &mappingEntry : customMappingConfig.entryMap().toStdMap()) {
if (QFile::exists(mappingEntry.first) && QFile::exists(mappingEntry.second)) {
applicationPaths.insert(filterApplicationName(KService(mappingEntry.first).name()), mappingEntry.first);
JBR_FILE_LOG_APPEND(mappingEntry.first)
JBR_FILE_LOG_APPEND(mappingEntry.second)
}
}
}
JBR_FILE_LOG_APPEND("========= Application path map ==========")
for (auto it = applicationPaths.begin(), end = applicationPaths.end(); it != end; ++it) {
JBR_FILE_LOG_APPEND(it.key() + " ==> " + it.value())
}
if (applicationPaths.isEmpty()) {
JBR_FILE_LOG_APPEND("No jetbrains apps were found")
}
return applicationPaths;
}
QString JetbrainsApplication::filterApplicationName(const QString &name)
{
const static QRegularExpression versionPostfixRegex(QStringLiteral(" \\d{4}.\\d(.\\d)?"));
return QString(name)
.remove(QLatin1String(" Release"))
.remove(QLatin1String(" Edition"))
.remove(QLatin1String(" + JBR11"))
.remove(QLatin1String(" RC"))
.remove(QLatin1String(" EAP"))
.remove(versionPostfixRegex);
}
QString JetbrainsApplication::formatOptionText(const QString &formatText, const Project &project)
{
QString txt = QString(formatText)
.replace(QLatin1String(FormatString::PROJECT), project.name)
.replace(QLatin1String(FormatString::APPNAME), this->name)
.replace(QLatin1String(FormatString::APP), this->shortName);
if (txt.contains(QLatin1String(FormatString::DIR))) {
txt.replace(QLatin1String(FormatString::DIR), QString(project.path).replace(QDir::homePath(), QLatin1String("~")));
}
return txt;
}
QDebug operator<<(QDebug d, const JetbrainsApplication *app)
{
d << " name: " << app->name << " desktopFilePath: " << app->desktopFilePath << " executablePath: " << app->executablePath
<< " configFolder: " << app->configFolder << " iconPath: " << app->iconPath << " shortName: " << app->shortName << " recentlyUsed: ";
for (const auto &project : std::as_const(app->recentlyUsed)) {
d << project.name << project.path;
}
return d;
}
void JetbrainsApplication::parseOldStyleXMLFile(const QString &fileName)
{
QDomDocument d;
QFile xmlFile(fileName);
bool opened = xmlFile.open(QFile::ReadOnly);
Q_ASSERT(opened);
d.setContent(&xmlFile);
const QDomNodeList list = d.elementsByTagName(QStringLiteral("option"));
for (int i = 0; i < list.count(); ++i) {
const QDomNode optionNode = list.at(i);
if (!optionNode.isElement()) {
continue;
}
const QDomElement option = optionNode.toElement();
if (option.attribute("name") == QLatin1String("recentPaths")) {
const QDomNodeList recentPathsList = option.elementsByTagName("list");
if (recentPathsList.isEmpty()) {
continue;
}
const QDomNodeList recentPathNodes = recentPathsList.at(0).childNodes();
for (int j = 0; j < recentPathNodes.count(); ++j) {
addRecentlyUsed(recentPathNodes.at(j).toElement().attribute(QStringLiteral("value")));
}
}
}
}
void JetbrainsApplication::parseNewStyleXMLFile(const QString &fileName)
{
// Initialize variables and find element containing the data
QDomDocument d;
QFile xmlFile(fileName);
bool opened = xmlFile.open(QFile::ReadOnly);
Q_ASSERT(opened);
d.setContent(&xmlFile);
QDomNodeList recentPathsMap;
QList<QPair<QString, double>> pathTimestampMap;
const auto component = d.elementsByTagName(QStringLiteral("option"));
for (int i = 0; i < component.count(); ++i) {
if (component.at(i).isElement() && component.at(i).toElement().attribute("name") == "additionalInfo") {
recentPathsMap = component.at(i).firstChild().childNodes();
}
}
// Parse the data
for (int i = 0; i < recentPathsMap.count(); ++i) {
const QDomNode _entry = recentPathsMap.at(i);
if (!_entry.isElement()) {
continue;
}
const QDomElement entry = _entry.toElement();
const QString path = entry.attribute("key");
const QDomElement valueEntry = entry.firstChildElement("value");
const QDomElement metaInfo = valueEntry.firstChildElement("RecentProjectMetaInfo");
double projectOpenTimestamp = metaInfo.lastChildElement("option").attribute("value").toDouble();
pathTimestampMap.append(qMakePair(path, projectOpenTimestamp));
}
std::sort(pathTimestampMap.begin(), pathTimestampMap.end(), [](QPair<QString, double> &first, QPair<QString, double> &second) {
return first.second > second.second;
});
for (const auto &pair : std::as_const(pathTimestampMap)) {
addRecentlyUsed(pair.first);
}
}
void JetbrainsApplication::addRecentlyUsed(const QString &path)
{
QString recentPath = QString(path).replace(QLatin1String("$USER_HOME$"), QDir::homePath());
if (!checkIfProjectsExist || QFileInfo::exists(recentPath)) {
Project project;
project.path = recentPath;
QString projectRootPath = recentPath;
if (QFileInfo(projectRootPath).isFile()) {
projectRootPath = QFileInfo(projectRootPath).dir().absolutePath();
}
QFile nameFile(projectRootPath + QStringLiteral("/.idea/.name"));
if (nameFile.exists()) {
if (nameFile.open(QFile::ReadOnly)) {
project.name = nameFile.readAll();
}
}
if (project.path.endsWith(QLatin1String(".sln"))) {
project.name = QFileInfo(project.path).completeBaseName();
}
if (project.name.isEmpty()) {
project.name = projectRootPath.split('/').last();
}
this->recentlyUsed.append(project);
}
}
#include "moc_JetbrainsApplication.cpp"