-
Notifications
You must be signed in to change notification settings - Fork 671
/
Copy pathutility_win.cpp
159 lines (128 loc) · 4.07 KB
/
utility_win.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
/*
* Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "utility_win.h"
#include "utility.h"
#include "asserts.h"
#include "filesystembase.h"
#include <comdef.h>
#include <qt_windows.h>
#include <shlguid.h>
#include <shlobj.h>
#include <string>
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QLibrary>
#include <QSettings>
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
namespace {
const QString systemRunPathC() {
return QStringLiteral("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
const QString runPathC() {
return QStringLiteral("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
const QString systemThemesC()
{
return QStringLiteral("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize");
}
}
namespace OCC {
void Utility::setupFavLink(const QString &)
{
}
bool Utility::hasSystemLaunchOnStartup(const QString &appName)
{
QSettings settings(systemRunPathC(), QSettings::NativeFormat);
return settings.contains(appName);
}
bool Utility::hasLaunchOnStartup(const QString &appName)
{
QSettings settings(runPathC(), QSettings::NativeFormat);
return settings.contains(appName);
}
void Utility::setLaunchOnStartup(const QString &appName, const QString &guiName, bool enable)
{
Q_UNUSED(guiName)
QSettings settings(runPathC(), QSettings::NativeFormat);
if (enable) {
settings.setValue(appName, QDir::toNativeSeparators(QCoreApplication::applicationFilePath()));
} else {
settings.remove(appName);
}
}
bool Utility::hasDarkSystray()
{
const QSettings settings(systemThemesC(), QSettings::NativeFormat);
return !settings.value(QStringLiteral("SystemUsesLightTheme"), false).toBool();
}
void Utility::UnixTimeToFiletime(time_t t, FILETIME *filetime)
{
LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
filetime->dwLowDateTime = (DWORD) ll;
filetime->dwHighDateTime = ll >>32;
}
void Utility::FiletimeToLargeIntegerFiletime(FILETIME *filetime, LARGE_INTEGER *hundredNSecs)
{
hundredNSecs->LowPart = filetime->dwLowDateTime;
hundredNSecs->HighPart = filetime->dwHighDateTime;
}
void Utility::UnixTimeToLargeIntegerFiletime(time_t t, LARGE_INTEGER *hundredNSecs)
{
LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
hundredNSecs->LowPart = (DWORD) ll;
hundredNSecs->HighPart = ll >>32;
}
QString Utility::formatWinError(long errorCode)
{
return QStringLiteral("WindowsError: %1: %2").arg(QString::number(errorCode, 16), QString::fromWCharArray(_com_error(errorCode).ErrorMessage()));
}
Utility::NtfsPermissionLookupRAII::NtfsPermissionLookupRAII()
{
qt_ntfs_permission_lookup++;
}
Utility::NtfsPermissionLookupRAII::~NtfsPermissionLookupRAII()
{
qt_ntfs_permission_lookup--;
}
void Utility::printWindowsDebugMessage(const QString &s)
{
OutputDebugStringW(reinterpret_cast<const wchar_t *>(s.utf16()));
}
Utility::Handle::Handle(HANDLE h, std::function<void(HANDLE)> &&close)
: _handle(h)
, _close(std::move(close))
{
}
Utility::Handle::Handle(HANDLE h)
: _handle(h)
, _close(&CloseHandle)
{
}
Utility::Handle::~Handle()
{
close();
}
void Utility::Handle::close()
{
if (_handle != INVALID_HANDLE_VALUE) {
_close(_handle);
_handle = INVALID_HANDLE_VALUE;
}
}
} // namespace OCC