forked from UnigramDev/Unigram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeUtils.cpp
More file actions
344 lines (277 loc) · 9.65 KB
/
Copy pathNativeUtils.cpp
File metadata and controls
344 lines (277 loc) · 9.65 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "pch.h"
#include "NativeUtils.h"
#if __has_include("NativeUtils.g.cpp")
#include "NativeUtils.g.cpp"
#endif
#include "Helpers\LibraryHelper.h"
#include "DebugUtils.h"
#include <winrt/Windows.Data.Xml.Dom.h>
#include <winrt/Windows.UI.Notifications.h>
typedef
BOOL
(APIENTRY* pGetKeyboardLayoutNameW)(
_Out_ LPWSTR pwszKLID
);
using namespace winrt::Windows::Data::Xml::Dom;
using namespace winrt::Windows::UI::Notifications;
namespace winrt::Telegram::Native::implementation
{
FatalErrorCallback NativeUtils::Callback;
void NativeUtils::SetFatalErrorCallback(FatalErrorCallback callback)
{
Callback = callback;
}
bool NativeUtils::FileExists(hstring path)
{
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
return GetFileAttributesExFromAppW(path.data(), GetFileExInfoStandard, (void*)&fileInfo);
}
int64_t NativeUtils::GetDirectorySize(hstring path)
{
return GetDirectorySize(path, L"\\*");
}
int64_t NativeUtils::GetDirectorySize(hstring path, hstring filter)
{
return GetDirectorySizeInternal(path.data(), filter.data(), 0);
}
void NativeUtils::CleanDirectory(hstring path, int days)
{
CleanDirectoryInternal(path.data(), days);
}
void NativeUtils::Delete(hstring path)
{
DeleteFile(path.data());
}
void NativeUtils::CleanDirectoryInternal(const std::wstring& path, int days)
{
long diff = 60 * 60 * 1000 * 24 * days;
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
auto currentTime = FileTimeToSeconds(ft);
WIN32_FIND_DATA data;
HANDLE handle = FindFirstFile((path + L"\\*").c_str(), &data);
if (handle == INVALID_HANDLE_VALUE)
{
return;
}
do
{
if (IsBrowsePath(data.cFileName))
{
continue;
}
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
CleanDirectoryInternal(path + L"\\" + data.cFileName, days);
}
else
{
auto lastAccess = FileTimeToSeconds(data.ftLastAccessTime);
auto lastWrite = FileTimeToSeconds(data.ftLastWriteTime);
if (days == 0)
{
DeleteFile((path + L"\\" + data.cFileName).c_str());
}
else if (lastAccess > lastWrite)
{
if (lastAccess + diff < currentTime)
{
DeleteFile((path + L"\\" + data.cFileName).c_str());
}
}
else if (lastWrite + diff < currentTime)
{
DeleteFile((path + L"\\" + data.cFileName).c_str());
}
}
} while (FindNextFile(handle, &data));
FindClose(handle);
}
uint64_t NativeUtils::GetDirectorySizeInternal(const std::wstring& path, const std::wstring& filter, uint64_t size)
{
WIN32_FIND_DATA data;
HANDLE handle = FindFirstFile((path + filter).c_str(), &data);
if (handle == INVALID_HANDLE_VALUE)
{
return size;
}
do
{
if (IsBrowsePath(data.cFileName))
{
continue;
}
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
size = GetDirectorySizeInternal(path + L"\\" + data.cFileName, filter, size);
}
else
{
size += (uint64_t)(data.nFileSizeHigh * (MAXDWORD)+data.nFileSizeLow);
}
} while (FindNextFile(handle, &data));
FindClose(handle);
return size;
}
bool NativeUtils::IsBrowsePath(const std::wstring& path)
{
return (path.find(L".") == 0 || path.find(L"..") == 0);
}
ULONGLONG NativeUtils::FileTimeToSeconds(FILETIME& ft)
{
ULARGE_INTEGER uli;
uli.HighPart = ft.dwHighDateTime;
uli.LowPart = ft.dwLowDateTime;
return uli.QuadPart / 10000;
}
int32_t NativeUtils::GetLastInputTime()
{
typedef BOOL(WINAPI* pGetLastInputInfo)(_Out_ PLASTINPUTINFO);
static const LibraryInstance user32(L"User32.dll", 0x00000001);
static const auto getLastInputInfo = user32.GetMethod<pGetLastInputInfo>("GetLastInputInfo");
if (getLastInputInfo == nullptr)
{
return 0;
}
LASTINPUTINFO lastInput;
lastInput.cbSize = sizeof(LASTINPUTINFO);
if (getLastInputInfo(&lastInput))
{
return lastInput.dwTime;
}
return 0;
}
winrt::Telegram::Native::TextDirectionality NativeUtils::GetDirectionality(hstring value)
{
unsigned int length = value.size();
WORD* type;
type = new WORD[length];
GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE2, value.data(), length, type);
for (int i = 0; i < length; i++)
{
if (IS_HIGH_SURROGATE(value[i]) || IS_LOW_SURROGATE(value[i]))
{
continue;
}
if (type[i] == C2_LEFTTORIGHT)
{
return winrt::Telegram::Native::TextDirectionality::LeftToRight;
}
else if (type[i] == C2_RIGHTTOLEFT)
{
return winrt::Telegram::Native::TextDirectionality::RightToLeft;
}
}
return winrt::Telegram::Native::TextDirectionality::Neutral;
}
hstring NativeUtils::GetCurrentCulture()
{
TCHAR buff[LOCALE_NAME_MAX_LENGTH];
int result = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SNAME, buff, LOCALE_NAME_MAX_LENGTH);
if (result == 0)
{
result = GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_SNAME, buff, LOCALE_NAME_MAX_LENGTH);
if (result == 0)
{
return L"en";
}
}
std::wstring str = buff;
size_t sorting = str.find(L"_");
if (sorting != std::wstring::npos)
{
return str.substr(0, sorting).c_str();
}
return buff;
}
hstring NativeUtils::GetKeyboardCulture()
{
// TODO: I'm not sure about how much expensive this call is.
// At the moment it isn't used extremely often, but we should
// consider caching it (problem is how to invalidate the cache)
static const LibraryInstance user32(L"User32.dll");
static const auto getKeyboardLayoutName = user32.GetMethod<pGetKeyboardLayoutNameW>("GetKeyboardLayoutNameW");
WCHAR name[KL_NAMELENGTH];
if (getKeyboardLayoutName(name))
{
// The layout name looks something like this: 00000410
// Where the first 4 bytes are most likely flags
// And the second half is actually the LCID as a HEX string
unsigned int lcid = std::stoul(name + 4, nullptr, 16);
WCHAR locale[LOCALE_NAME_MAX_LENGTH];
int length = LCIDToLocaleName(lcid, locale, LOCALE_NAME_MAX_LENGTH, 0);
if (length > 0)
{
// The string is null terminated
return hstring(locale, length - 1);
}
}
// TODO: probably better this than an empty string.
return L"en-US";
}
bool NativeUtils::IsFileReadable(hstring path)
{
return IsFileReadableInternal(path, NULL, NULL);
}
bool NativeUtils::IsFileReadable(hstring path, int64_t& fileSize, int64_t& fileTime)
{
return IsFileReadableInternal(path, &fileSize, &fileTime);
}
bool NativeUtils::IsFileReadableInternal(hstring path, int64_t* fileSize, int64_t* fileTime)
{
DWORD desired_access = GENERIC_READ;
// TODO: share mode
DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE;
DWORD creation_disposition = OPEN_ALWAYS;
DWORD native_flags = FILE_FLAG_BACKUP_SEMANTICS;
//if (flags & Direct) {
// native_flags |= FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING;
//}
//if (flags & WinStat) {
// native_flags |= FILE_FLAG_BACKUP_SEMANTICS;
//}
CREATEFILE2_EXTENDED_PARAMETERS extended_parameters;
std::memset(&extended_parameters, 0, sizeof(extended_parameters));
extended_parameters.dwSize = sizeof(extended_parameters);
extended_parameters.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
extended_parameters.dwFileFlags = native_flags;
auto handle = CreateFile2FromAppW(path.c_str(), desired_access, share_mode, creation_disposition, &extended_parameters);
if (handle == INVALID_HANDLE_VALUE)
{
return false;
}
if (fileSize)
{
LARGE_INTEGER pFileSize;
GetFileSizeEx(handle, &pFileSize);
*fileSize = static_cast<int64_t>(pFileSize.QuadPart);
}
if (fileTime)
{
FILETIME pFileTime;
GetFileTime(handle, NULL, NULL, &pFileTime);
*fileTime = static_cast<int64_t>(FileTimeToSeconds(pFileTime));
}
CloseHandle(handle);
return true;
}
bool NativeUtils::IsMediaSupported()
{
HRESULT result;
result = MFStartup(MF_VERSION);
if (result == S_OK)
{
MFShutdown();
}
return result != E_NOTIMPL;
}
hstring NativeUtils::GetBacktrace()
{
return hstring(::GetBacktrace(0));
}
void NativeUtils::Crash()
{
int32_t* ciao = nullptr;
*ciao = 42;
}
} // namespace winrt::Telegram::Native::implementation