-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.cpp
More file actions
190 lines (170 loc) · 6.99 KB
/
Copy pathutils.cpp
File metadata and controls
190 lines (170 loc) · 6.99 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
#include "utils.h"
#include <QCoreApplication>
#include <QDesktopServices>
#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QProcess>
#include <QStandardPaths>
#include <QRegularExpression>
QString getLatestGitHubReleaseVersion(const QString &owner, const QString &repo)
{
QEventLoop loop;
QNetworkAccessManager manager;
QNetworkRequest request(
QUrl(QString("https://api.github.com/repos/%1/%2/releases/latest").arg(owner, repo)));
request.setHeader(QNetworkRequest::UserAgentHeader, "Mozilla/5.0");
QNetworkReply *reply = manager.get(request);
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
QJsonObject jsonObj = doc.object();
QString latestVersion = jsonObj.value("tag_name").toString();
reply->deleteLater();
return latestVersion;
} else {
qDebug() << "Error:" << reply->errorString();
reply->deleteLater();
return QString("0.0.0");
}
}
QString getContinuousGitHubReleaseVersion(const QString &owner, const QString &repo)
{
QEventLoop loop;
QNetworkAccessManager manager;
QNetworkRequest request(
QUrl(QString("https://api.github.com/repos/%1/%2/releases/tags/continuous").arg(owner, repo)));
request.setHeader(QNetworkRequest::UserAgentHeader, "Mozilla/5.0");
QNetworkReply *reply = manager.get(request);
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
QJsonObject jsonObj = doc.object();
QString body = jsonObj.value("body").toString();
QRegularExpression rx("(continuous-\\d+-g[a-f0-9]+)");
QRegularExpressionMatch match = rx.match(body);
reply->deleteLater();
if (match.hasMatch()) {
return match.captured(1);
}
return jsonObj.value("tag_name").toString();
} else {
qDebug() << "Error:" << reply->errorString();
reply->deleteLater();
return QString("0.0.0");
}
}
bool fileExists(const QString &filePath)
{
QFileInfo checkFile(filePath);
return checkFile.exists();
}
bool openFileExplorer(const QString &path)
{
QDir dir(path);
if (!dir.exists()) {
qDebug() << "Path does not exist:" << path;
return false;
}
QUrl url = QUrl::fromLocalFile(dir.absolutePath());
return QDesktopServices::openUrl(url);
}
bool setOSRunOnStartup(bool enable)
{
QString appName = QCoreApplication::applicationName();
QString appPath = QCoreApplication::applicationFilePath();
#ifdef Q_OS_WIN
QString startupPath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)
+ QDir::separator() + "Startup";
QString linkPath = startupPath + QDir::separator() + appName + ".lnk";
if (enable) {
QFile::remove(linkPath);
// Use Windows Script Host to create a shortcut with arguments
QString script =
"Set oWS = WScript.CreateObject(\"WScript.Shell\")\n"
"sLinkFile = \"" + linkPath.replace("/", "\\") + "\"\n"
"Set oLink = oWS.CreateShortcut(sLinkFile)\n"
"oLink.TargetPath = \"" + appPath.replace("/", "\\") + "\"\n"
"oLink.WorkingDirectory = \"" + QDir::toNativeSeparators(QCoreApplication::applicationDirPath()) + "\"\n"
"oLink.Arguments = \"--tray\"\n"
"oLink.Save\n";
QString vbsPath = QDir::temp().filePath("create_shortcut.vbs");
QFile vbsFile(vbsPath);
if (vbsFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
vbsFile.write(script.toUtf8());
vbsFile.close();
QProcess::execute("wscript", QStringList() << vbsPath);
vbsFile.remove();
return QFile::exists(linkPath);
}
return false;
}
QFile::remove(linkPath);
return false;
#elif defined(Q_OS_LINUX)
QString appDir = QCoreApplication::applicationDirPath();
QString configPath = QDir::homePath() + QDir::separator() + ".config";
QString autostartPath = configPath + QDir::separator() + "autostart";
QString desktopFilePath = autostartPath + QDir::separator() + appName + "-autostart.desktop";
QFile::remove(desktopFilePath);
if (enable) {
QFile::remove(desktopFilePath);
QDir configDir(configPath);
QFile desktopFile(desktopFilePath);
if (!configDir.exists("autostart")) {
configDir.mkdir("autostart");
}
if (desktopFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&desktopFile);
out << "[Desktop Entry]\n";
out << "Path=" + appDir + "\n";
out << "Type=Application\n";
out << "Hidden=false\n";
out << "NoDisplay=false\n";
out << "X-GNOME-Autostart-enabled=true\n";
out << "Exec=" << appPath << " --tray\n";
out << "Name=" << appName << "\n";
out << "Icon=" << appName << "\n";
out << "Comment=Auto-starts " << appName << " on boot\n";
desktopFile.close();
return true;
}
}
return false;
#endif
}
bool downloadAndExtractHeadsetControl(const QString &channel, const QString &destDir)
{
QString url;
#ifdef Q_OS_WIN
if (channel == "continuous") {
url = "https://github.com/Sapd/HeadsetControl/releases/download/continuous/headsetcontrol-windows-x86_64.exe";
QString command = QString("powershell -Command \"Invoke-WebRequest -Uri '%1' -OutFile '%2\\headsetcontrol.exe'\"").arg(url, destDir);
QProcess process;
process.startCommand(command);
process.waitForFinished(-1);
return process.exitCode() == 0;
} else {
url = "https://github.com/Sapd/HeadsetControl/releases/latest/download/headsetcontrol-windows-x86_64.zip";
QString command = QString("powershell -Command \"Invoke-WebRequest -Uri '%1' -OutFile '%2\\hc.zip'; if (Test-Path '%2\\hc.zip') { Expand-Archive -Path '%2\\hc.zip' -DestinationPath '%2' -Force; Remove-Item '%2\\hc.zip' } else { exit 1 }\"").arg(url, destDir);
QProcess process;
process.startCommand(command);
process.waitForFinished(-1);
return process.exitCode() == 0;
}
#else
if (channel == "continuous") {
url = "https://github.com/Sapd/HeadsetControl/releases/download/continuous/headsetcontrol-linux-x86_64.zip";
} else {
url = "https://github.com/Sapd/HeadsetControl/releases/latest/download/headsetcontrol-linux-x86_64.zip";
}
QString command = QString("bash -c \"wget -q -O '%2/hc.zip' '%1' && unzip -o '%2/hc.zip' -d '%2' && rm '%2/hc.zip'\"").arg(url, destDir);
QProcess process;
process.startCommand(command);
process.waitForFinished(-1);
return process.exitCode() == 0;
#endif
}