-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautostart.cpp
More file actions
116 lines (107 loc) · 3.97 KB
/
Copy pathautostart.cpp
File metadata and controls
116 lines (107 loc) · 3.97 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
#include "autostart.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QString>
#include <QTextStream>
#if defined(Q_OS_WIN)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
void Autostart::setEnabled(bool enable)
{
const wchar_t* runKey =
L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
const wchar_t* approvedKey =
L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run";
const wchar_t* name = L"TrackClick";
HKEY hKey;
if (enable) {
// Write quoted exe path as REG_SZ under the Run key
const std::wstring exe = QString("\"%1\"")
.arg(QDir::toNativeSeparators(QCoreApplication::applicationFilePath()))
.toStdWString();
if (RegCreateKeyExW(HKEY_CURRENT_USER, runKey, 0, nullptr,
REG_OPTION_NON_VOLATILE, KEY_SET_VALUE,
nullptr, &hKey, nullptr) == ERROR_SUCCESS) {
RegSetValueExW(hKey, name, 0, REG_SZ,
reinterpret_cast<const BYTE*>(exe.c_str()),
static_cast<DWORD>((exe.size() + 1) * sizeof(wchar_t)));
RegCloseKey(hKey);
}
// Remove any stale "disabled" entry from StartupApproved so Windows
// treats the Run key entry as approved (absent = enabled by default).
if (RegOpenKeyExW(HKEY_CURRENT_USER, approvedKey, 0,
KEY_SET_VALUE, &hKey) == ERROR_SUCCESS) {
RegDeleteValueW(hKey, name);
RegCloseKey(hKey);
}
} else {
if (RegOpenKeyExW(HKEY_CURRENT_USER, runKey, 0,
KEY_SET_VALUE, &hKey) == ERROR_SUCCESS) {
RegDeleteValueW(hKey, name);
RegCloseKey(hKey);
}
if (RegOpenKeyExW(HKEY_CURRENT_USER, approvedKey, 0,
KEY_SET_VALUE, &hKey) == ERROR_SUCCESS) {
RegDeleteValueW(hKey, name);
RegCloseKey(hKey);
}
}
}
#elif defined(Q_OS_MACOS)
void Autostart::setEnabled(bool enable)
{
const QString plist =
QDir::homePath() + "/Library/LaunchAgents/com.optitrack.trackclick.plist";
if (enable) {
QDir().mkpath(QFileInfo(plist).absolutePath());
QFile f(plist);
if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&f);
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\""
" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>Label</key>\n"
" <string>com.optitrack.trackclick</string>\n"
" <key>ProgramArguments</key>\n"
" <array>\n"
" <string>" << QCoreApplication::applicationFilePath() << "</string>\n"
" </array>\n"
" <key>RunAtLoad</key>\n"
" <true/>\n"
"</dict>\n"
"</plist>\n";
}
} else {
QFile::remove(plist);
}
}
#elif defined(Q_OS_LINUX)
void Autostart::setEnabled(bool enable)
{
const QString dir =
QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/autostart";
const QString path = dir + "/trackclick.desktop";
if (enable) {
QDir().mkpath(dir);
QFile f(path);
if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&f);
out << "[Desktop Entry]\n"
"Type=Application\n"
"Name=TrackClick\n"
"Exec=" << QCoreApplication::applicationFilePath() << "\n"
"Hidden=false\n"
"X-GNOME-Autostart-enabled=true\n";
}
} else {
QFile::remove(path);
}
}
#else
void Autostart::setEnabled(bool) {}
#endif