forked from MatsuriDayo/nekoray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoRun.cpp
231 lines (184 loc) · 7.84 KB
/
AutoRun.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
#include "AutoRun.hpp"
#include <QApplication>
#include <QDir>
#include "3rdparty/fix_old_qt.h"
#include "main/NekoGui.hpp"
// macOS headers (possibly OBJ-c)
#if defined(Q_OS_MACOS)
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#endif
#ifdef Q_OS_WIN
#include <QSettings>
QString Windows_GenAutoRunString() {
auto appPath = QApplication::applicationFilePath();
appPath = "\"" + QDir::toNativeSeparators(appPath) + "\"";
appPath += " -tray";
return appPath;
}
void AutoRun_SetEnabled(bool enable) {
// 以程序名称作为注册表中的键
// 根据键获取对应的值(程序路径)
auto appPath = QApplication::applicationFilePath();
QFileInfo fInfo(appPath);
QString name = fInfo.baseName();
QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
if (enable) {
settings.setValue(name, Windows_GenAutoRunString());
} else {
settings.remove(name);
}
}
bool AutoRun_IsEnabled() {
QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
// 以程序名称作为注册表中的键
// 根据键获取对应的值(程序路径)
auto appPath = QApplication::applicationFilePath();
QFileInfo fInfo(appPath);
QString name = fInfo.baseName();
return settings.value(name).toString() == Windows_GenAutoRunString();
}
#endif
#ifdef Q_OS_MACOS
void AutoRun_SetEnabled(bool enable) {
// From
// https://github.com/nextcloud/desktop/blob/master/src/common/utility_mac.cpp
QString filePath = QDir(QCoreApplication::applicationDirPath() + QLatin1String("/../..")).absolutePath();
CFStringRef folderCFStr = CFStringCreateWithCString(0, filePath.toUtf8().data(), kCFStringEncodingUTF8);
CFURLRef urlRef = CFURLCreateWithFileSystemPath(0, folderCFStr, kCFURLPOSIXPathStyle, true);
LSSharedFileListRef loginItems = LSSharedFileListCreate(0, kLSSharedFileListSessionLoginItems, 0);
if (loginItems && enable) {
// Insert an item to the list.
LSSharedFileListItemRef item =
LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemLast, 0, 0, urlRef, 0, 0);
if (item) CFRelease(item);
CFRelease(loginItems);
} else if (loginItems && !enable) {
// We need to iterate over the items and check which one is "ours".
UInt32 seedValue;
CFArrayRef itemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue);
CFStringRef appUrlRefString = CFURLGetString(urlRef);
for (int i = 0; i < CFArrayGetCount(itemsArray); i++) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef) CFArrayGetValueAtIndex(itemsArray, i);
CFURLRef itemUrlRef = NULL;
if (LSSharedFileListItemResolve(item, 0, &itemUrlRef, NULL) == noErr && itemUrlRef) {
CFStringRef itemUrlString = CFURLGetString(itemUrlRef);
if (CFStringCompare(itemUrlString, appUrlRefString, 0) == kCFCompareEqualTo) {
LSSharedFileListItemRemove(loginItems, item); // remove it!
}
CFRelease(itemUrlRef);
}
}
CFRelease(itemsArray);
CFRelease(loginItems);
}
CFRelease(folderCFStr);
CFRelease(urlRef);
}
bool AutoRun_IsEnabled() {
// From
// https://github.com/nextcloud/desktop/blob/master/src/common/utility_mac.cpp
// this is quite some duplicate code with setLaunchOnStartup, at some
// point we should fix this FIXME.
bool returnValue = false;
QString filePath = QDir(QCoreApplication::applicationDirPath() + QLatin1String("/../..")).absolutePath();
CFStringRef folderCFStr = CFStringCreateWithCString(0, filePath.toUtf8().data(), kCFStringEncodingUTF8);
CFURLRef urlRef = CFURLCreateWithFileSystemPath(0, folderCFStr, kCFURLPOSIXPathStyle, true);
LSSharedFileListRef loginItems = LSSharedFileListCreate(0, kLSSharedFileListSessionLoginItems, 0);
if (loginItems) {
// We need to iterate over the items and check which one is "ours".
UInt32 seedValue;
CFArrayRef itemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue);
CFStringRef appUrlRefString = CFURLGetString(urlRef); // no need for release
for (int i = 0; i < CFArrayGetCount(itemsArray); i++) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef) CFArrayGetValueAtIndex(itemsArray, i);
CFURLRef itemUrlRef = NULL;
if (LSSharedFileListItemResolve(item, 0, &itemUrlRef, NULL) == noErr && itemUrlRef) {
CFStringRef itemUrlString = CFURLGetString(itemUrlRef);
if (CFStringCompare(itemUrlString, appUrlRefString, 0) == kCFCompareEqualTo) {
returnValue = true;
}
CFRelease(itemUrlRef);
}
}
CFRelease(itemsArray);
}
CFRelease(loginItems);
CFRelease(folderCFStr);
CFRelease(urlRef);
return returnValue;
}
#endif
#ifdef Q_OS_LINUX
#include <QStandardPaths>
#include <QProcessEnvironment>
#include <QTextStream>
#define NEWLINE "\r\n"
// launchatlogin.cpp
// ShadowClash
//
// Created by TheWanderingCoel on 2018/6/12.
// Copyright © 2019 Coel Wu. All rights reserved.
//
QString getUserAutostartDir_private() {
QString config = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
config += QLatin1String("/autostart/");
return config;
}
void AutoRun_SetEnabled(bool enable) {
// From https://github.com/nextcloud/desktop/blob/master/src/common/utility_unix.cpp
QString appName = QCoreApplication::applicationName();
QString userAutoStartPath = getUserAutostartDir_private();
QString desktopFileLocation = userAutoStartPath + appName + QLatin1String(".desktop");
QStringList appCmdList;
// nekoray: launcher
if (qEnvironmentVariable("NKR_FROM_LAUNCHER") == "1") {
appCmdList << QApplication::applicationDirPath() + "/launcher"
<< "--";
} else {
if (QProcessEnvironment::systemEnvironment().contains("APPIMAGE")) {
appCmdList << QProcessEnvironment::systemEnvironment().value("APPIMAGE");
} else {
appCmdList << QApplication::applicationFilePath();
}
}
appCmdList << "-tray";
if (NekoGui::dataStore->flag_use_appdata) {
appCmdList << "-appdata";
}
if (enable) {
if (!QDir().exists(userAutoStartPath) && !QDir().mkpath(userAutoStartPath)) {
// qCWarning(lcUtility) << "Could not create autostart folder"
// << userAutoStartPath;
return;
}
QFile iniFile(desktopFileLocation);
if (!iniFile.open(QIODevice::WriteOnly)) {
// qCWarning(lcUtility) << "Could not write auto start entry" <<
// desktopFileLocation;
return;
}
QTextStream ts(&iniFile);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
ts.setCodec("UTF-8");
#endif
ts << QLatin1String("[Desktop Entry]") << NEWLINE
<< QLatin1String("Name=") << appName << NEWLINE
<< QLatin1String("Exec=") << appCmdList.join(" ") << NEWLINE
<< QLatin1String("Terminal=") << "false" << NEWLINE
<< QLatin1String("Categories=") << "Network" << NEWLINE
<< QLatin1String("Type=") << "Application" << NEWLINE
<< QLatin1String("StartupNotify=") << "false" << NEWLINE
<< QLatin1String("X-GNOME-Autostart-enabled=") << "true" << NEWLINE;
ts.flush();
iniFile.close();
} else {
QFile::remove(desktopFileLocation);
}
}
bool AutoRun_IsEnabled() {
QString appName = QCoreApplication::applicationName();
QString desktopFileLocation = getUserAutostartDir_private() + appName + QLatin1String(".desktop");
return QFile::exists(desktopFileLocation);
}
#endif