specific_win.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "platform/win/specific_win.h"
  8. #include "platform/win/main_window_win.h"
  9. #include "platform/win/notifications_manager_win.h"
  10. #include "platform/win/windows_app_user_model_id.h"
  11. #include "platform/win/windows_dlls.h"
  12. #include "platform/win/windows_autostart_task.h"
  13. #include "base/platform/base_platform_info.h"
  14. #include "base/platform/win/base_windows_co_task_mem.h"
  15. #include "base/platform/win/base_windows_shlobj_h.h"
  16. #include "base/platform/win/base_windows_winrt.h"
  17. #include "base/call_delayed.h"
  18. #include "ui/boxes/confirm_box.h"
  19. #include "lang/lang_keys.h"
  20. #include "mainwindow.h"
  21. #include "mainwidget.h"
  22. #include "history/history_location_manager.h"
  23. #include "storage/localstorage.h"
  24. #include "core/application.h"
  25. #include "window/window_controller.h"
  26. #include "core/crash_reports.h"
  27. #include <QtCore/QOperatingSystemVersion>
  28. #include <QtWidgets/QApplication>
  29. #include <QtGui/QDesktopServices>
  30. #include <QtGui/QWindow>
  31. #include <Shobjidl.h>
  32. #include <ShObjIdl_core.h>
  33. #include <shellapi.h>
  34. #include <openssl/conf.h>
  35. #include <openssl/engine.h>
  36. #include <openssl/err.h>
  37. #include <dbghelp.h>
  38. #include <Shlwapi.h>
  39. #include <Strsafe.h>
  40. #include <Windowsx.h>
  41. #include <WtsApi32.h>
  42. #include <SDKDDKVer.h>
  43. #include <sal.h>
  44. #include <Psapi.h>
  45. #include <strsafe.h>
  46. #include <ObjBase.h>
  47. #include <propvarutil.h>
  48. #include <functiondiscoverykeys.h>
  49. #include <intsafe.h>
  50. #include <guiddef.h>
  51. #include <locale.h>
  52. #include <ShellScalingApi.h>
  53. #ifndef DCX_USESTYLE
  54. #define DCX_USESTYLE 0x00010000
  55. #endif
  56. #ifndef WM_NCPOINTERUPDATE
  57. #define WM_NCPOINTERUPDATE 0x0241
  58. #define WM_NCPOINTERDOWN 0x0242
  59. #define WM_NCPOINTERUP 0x0243
  60. #endif
  61. using namespace ::Platform;
  62. namespace {
  63. bool themeInited = false;
  64. bool finished = true;
  65. QMargins simpleMargins, margins;
  66. HICON bigIcon = 0, smallIcon = 0, overlayIcon = 0;
  67. [[nodiscard]] uint64 WindowIdFromHWND(HWND value) {
  68. return (reinterpret_cast<uint64>(value) & 0xFFFFFFFFULL);
  69. }
  70. struct FindToActivateRequest {
  71. uint64 processId = 0;
  72. uint64 windowId = 0;
  73. HWND result = nullptr;
  74. uint32 resultLevel = 0; // Larger is better.
  75. };
  76. BOOL CALLBACK FindToActivate(HWND hwnd, LPARAM lParam) {
  77. const auto request = reinterpret_cast<FindToActivateRequest*>(lParam);
  78. DWORD dwProcessId;
  79. ::GetWindowThreadProcessId(hwnd, &dwProcessId);
  80. if ((uint64)dwProcessId != request->processId) {
  81. return TRUE;
  82. }
  83. // Found a Top-Level window.
  84. if (WindowIdFromHWND(hwnd) == request->windowId) {
  85. request->result = hwnd;
  86. request->resultLevel = 3;
  87. return FALSE;
  88. }
  89. const auto data = static_cast<uint32>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
  90. if ((data != 1 && data != 2) || (data <= request->resultLevel)) {
  91. return TRUE;
  92. }
  93. request->result = hwnd;
  94. request->resultLevel = data;
  95. return TRUE;
  96. }
  97. void DeleteMyModules() {
  98. constexpr auto kMaxPathLong = 32767;
  99. auto exePath = std::array<WCHAR, kMaxPathLong + 1>{ 0 };
  100. const auto exeLength = GetModuleFileName(
  101. nullptr,
  102. exePath.data(),
  103. kMaxPathLong + 1);
  104. if (!exeLength || exeLength >= kMaxPathLong + 1) {
  105. return;
  106. }
  107. const auto exe = std::wstring(exePath.data());
  108. const auto last1 = exe.find_last_of('\\');
  109. const auto last2 = exe.find_last_of('/');
  110. const auto last = std::max(
  111. (last1 == std::wstring::npos) ? -1 : int(last1),
  112. (last2 == std::wstring::npos) ? -1 : int(last2));
  113. if (last < 0) {
  114. return;
  115. }
  116. const auto modules = exe.substr(0, last + 1) + L"modules";
  117. const auto deleteOne = [&](const wchar_t *name, const wchar_t *arch) {
  118. const auto path = modules + L'\\' + arch + L'\\' + name;
  119. DeleteFile(path.c_str());
  120. };
  121. const auto deleteBoth = [&](const wchar_t *name) {
  122. deleteOne(name, L"x86");
  123. deleteOne(name, L"x64");
  124. };
  125. const auto removeOne = [&](const std::wstring &name) {
  126. const auto path = modules + L'\\' + name;
  127. RemoveDirectory(path.c_str());
  128. };
  129. const auto removeBoth = [&](const std::wstring &name) {
  130. removeOne(L"x86\\" + name);
  131. removeOne(L"x64\\" + name);
  132. };
  133. deleteBoth(L"d3d\\d3dcompiler_47.dll");
  134. removeBoth(L"d3d");
  135. removeOne(L"x86");
  136. removeOne(L"x64");
  137. RemoveDirectory(modules.c_str());
  138. }
  139. bool ManageAppLink(
  140. bool create,
  141. bool silent,
  142. const GUID &folderId,
  143. const wchar_t *args,
  144. const wchar_t *description) {
  145. if (cExeName().isEmpty()) {
  146. return false;
  147. }
  148. PWSTR startupFolder;
  149. HRESULT hr = SHGetKnownFolderPath(
  150. folderId,
  151. KF_FLAG_CREATE,
  152. nullptr,
  153. &startupFolder);
  154. const auto guard = gsl::finally([&] {
  155. CoTaskMemFree(startupFolder);
  156. });
  157. if (!SUCCEEDED(hr)) {
  158. WCHAR buffer[64];
  159. const auto size = base::array_size(buffer) - 1;
  160. const auto length = StringFromGUID2(folderId, buffer, size);
  161. if (length > 0 && length <= size) {
  162. buffer[length] = 0;
  163. if (!silent) LOG(("App Error: could not get %1 folder: %2").arg(buffer).arg(hr));
  164. }
  165. return false;
  166. }
  167. const auto lnk = QString::fromWCharArray(startupFolder)
  168. + '\\'
  169. + AppFile.utf16()
  170. + u".lnk"_q;
  171. if (!create) {
  172. QFile::remove(lnk);
  173. return true;
  174. }
  175. const auto shellLink = base::WinRT::TryCreateInstance<IShellLink>(
  176. CLSID_ShellLink);
  177. if (!shellLink) {
  178. if (!silent) LOG(("App Error: could not create instance of IID_IShellLink %1").arg(hr));
  179. return false;
  180. }
  181. QString exe = QDir::toNativeSeparators(cExeDir() + cExeName()), dir = QDir::toNativeSeparators(QDir(cWorkingDir()).absolutePath());
  182. shellLink->SetArguments(args);
  183. shellLink->SetPath(exe.toStdWString().c_str());
  184. shellLink->SetWorkingDirectory(dir.toStdWString().c_str());
  185. shellLink->SetDescription(description);
  186. if (const auto propertyStore = shellLink.try_as<IPropertyStore>()) {
  187. PROPVARIANT appIdPropVar;
  188. hr = InitPropVariantFromString(AppUserModelId::Id().c_str(), &appIdPropVar);
  189. if (SUCCEEDED(hr)) {
  190. hr = propertyStore->SetValue(AppUserModelId::Key(), appIdPropVar);
  191. PropVariantClear(&appIdPropVar);
  192. if (SUCCEEDED(hr)) {
  193. hr = propertyStore->Commit();
  194. }
  195. }
  196. }
  197. const auto persistFile = shellLink.try_as<IPersistFile>();
  198. if (!persistFile) {
  199. if (!silent) LOG(("App Error: could not create interface IID_IPersistFile %1").arg(hr));
  200. return false;
  201. }
  202. hr = persistFile->Save(lnk.toStdWString().c_str(), TRUE);
  203. if (!SUCCEEDED(hr)) {
  204. if (!silent) LOG(("App Error: could not save IPersistFile to path %1").arg(lnk));
  205. return false;
  206. }
  207. return true;
  208. }
  209. } // namespace
  210. QString psAppDataPath() {
  211. static const int maxFileLen = MAX_PATH * 10;
  212. WCHAR wstrPath[maxFileLen];
  213. if (GetEnvironmentVariable(L"APPDATA", wstrPath, maxFileLen)) {
  214. QDir appData(QString::fromStdWString(std::wstring(wstrPath)));
  215. #ifdef OS_WIN_STORE
  216. return appData.absolutePath() + u"/Telegram Desktop UWP/"_q;
  217. #else // OS_WIN_STORE
  218. return appData.absolutePath() + '/' + AppName.utf16() + '/';
  219. #endif // OS_WIN_STORE
  220. }
  221. return QString();
  222. }
  223. QString psAppDataPathOld() {
  224. static const int maxFileLen = MAX_PATH * 10;
  225. WCHAR wstrPath[maxFileLen];
  226. if (GetEnvironmentVariable(L"APPDATA", wstrPath, maxFileLen)) {
  227. QDir appData(QString::fromStdWString(std::wstring(wstrPath)));
  228. return appData.absolutePath() + '/' + AppNameOld.utf16() + '/';
  229. }
  230. return QString();
  231. }
  232. void psDoCleanup() {
  233. try {
  234. Platform::AutostartToggle(false);
  235. psSendToMenu(false, true);
  236. AppUserModelId::CleanupShortcut();
  237. DeleteMyModules();
  238. } catch (...) {
  239. }
  240. }
  241. int psCleanup() {
  242. __try
  243. {
  244. psDoCleanup();
  245. }
  246. __except(EXCEPTION_EXECUTE_HANDLER)
  247. {
  248. return 0;
  249. }
  250. return 0;
  251. }
  252. void psDoFixPrevious() {
  253. try {
  254. static const int bufSize = 4096;
  255. DWORD checkType = 0;
  256. DWORD checkSize = bufSize * 2;
  257. WCHAR checkStr[bufSize] = { 0 };
  258. HKEY newKey1 = nullptr;
  259. HKEY newKey2 = nullptr;
  260. HKEY oldKey1 = nullptr;
  261. HKEY oldKey2 = nullptr;
  262. const auto appId = AppId.utf16();
  263. const auto newKeyStr1 = QString("Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%1_is1").arg(appId).toStdWString();
  264. const auto newKeyStr2 = QString("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%1_is1").arg(appId).toStdWString();
  265. const auto oldKeyStr1 = QString("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%1_is1").arg(appId).toStdWString();
  266. const auto oldKeyStr2 = QString("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%1_is1").arg(appId).toStdWString();
  267. const auto newKeyRes1 = RegOpenKeyEx(HKEY_CURRENT_USER, newKeyStr1.c_str(), 0, KEY_READ, &newKey1);
  268. const auto newKeyRes2 = RegOpenKeyEx(HKEY_CURRENT_USER, newKeyStr2.c_str(), 0, KEY_READ, &newKey2);
  269. const auto oldKeyRes1 = RegOpenKeyEx(HKEY_LOCAL_MACHINE, oldKeyStr1.c_str(), 0, KEY_READ, &oldKey1);
  270. const auto oldKeyRes2 = RegOpenKeyEx(HKEY_LOCAL_MACHINE, oldKeyStr2.c_str(), 0, KEY_READ, &oldKey2);
  271. const auto existNew1 = (newKeyRes1 == ERROR_SUCCESS) && (RegQueryValueEx(newKey1, L"InstallDate", 0, &checkType, (BYTE*)checkStr, &checkSize) == ERROR_SUCCESS); checkSize = bufSize * 2;
  272. const auto existNew2 = (newKeyRes2 == ERROR_SUCCESS) && (RegQueryValueEx(newKey2, L"InstallDate", 0, &checkType, (BYTE*)checkStr, &checkSize) == ERROR_SUCCESS); checkSize = bufSize * 2;
  273. const auto existOld1 = (oldKeyRes1 == ERROR_SUCCESS) && (RegQueryValueEx(oldKey1, L"InstallDate", 0, &checkType, (BYTE*)checkStr, &checkSize) == ERROR_SUCCESS); checkSize = bufSize * 2;
  274. const auto existOld2 = (oldKeyRes2 == ERROR_SUCCESS) && (RegQueryValueEx(oldKey2, L"InstallDate", 0, &checkType, (BYTE*)checkStr, &checkSize) == ERROR_SUCCESS); checkSize = bufSize * 2;
  275. if (newKeyRes1 == ERROR_SUCCESS) RegCloseKey(newKey1);
  276. if (newKeyRes2 == ERROR_SUCCESS) RegCloseKey(newKey2);
  277. if (oldKeyRes1 == ERROR_SUCCESS) RegCloseKey(oldKey1);
  278. if (oldKeyRes2 == ERROR_SUCCESS) RegCloseKey(oldKey2);
  279. if (existNew1 || existNew2) {
  280. if (existOld1) RegDeleteKey(HKEY_LOCAL_MACHINE, oldKeyStr1.c_str());
  281. if (existOld2) RegDeleteKey(HKEY_LOCAL_MACHINE, oldKeyStr2.c_str());
  282. }
  283. QString userDesktopLnk, commonDesktopLnk;
  284. WCHAR userDesktopFolder[MAX_PATH], commonDesktopFolder[MAX_PATH];
  285. HRESULT userDesktopRes = SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY, 0, SHGFP_TYPE_CURRENT, userDesktopFolder);
  286. HRESULT commonDesktopRes = SHGetFolderPath(0, CSIDL_COMMON_DESKTOPDIRECTORY, 0, SHGFP_TYPE_CURRENT, commonDesktopFolder);
  287. if (SUCCEEDED(userDesktopRes)) {
  288. userDesktopLnk = QString::fromWCharArray(userDesktopFolder) + "\\Telegram.lnk";
  289. }
  290. if (SUCCEEDED(commonDesktopRes)) {
  291. commonDesktopLnk = QString::fromWCharArray(commonDesktopFolder) + "\\Telegram.lnk";
  292. }
  293. QFile userDesktopFile(userDesktopLnk), commonDesktopFile(commonDesktopLnk);
  294. if (QFile::exists(userDesktopLnk) && QFile::exists(commonDesktopLnk) && userDesktopLnk != commonDesktopLnk) {
  295. QFile::remove(commonDesktopLnk);
  296. }
  297. } catch (...) {
  298. }
  299. }
  300. int psFixPrevious() {
  301. __try
  302. {
  303. psDoFixPrevious();
  304. }
  305. __except(EXCEPTION_EXECUTE_HANDLER)
  306. {
  307. return 0;
  308. }
  309. return 0;
  310. }
  311. namespace Platform {
  312. namespace ThirdParty {
  313. namespace {
  314. void StartOpenSSL() {
  315. // Don't use dynamic OpenSSL config, it can load unwanted DLLs.
  316. OPENSSL_load_builtin_modules();
  317. ENGINE_load_builtin_engines();
  318. ERR_clear_error();
  319. OPENSSL_no_config();
  320. }
  321. } // namespace
  322. void start() {
  323. StartOpenSSL();
  324. Dlls::CheckLoadedModules();
  325. }
  326. } // namespace ThirdParty
  327. void start() {
  328. // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale#utf-8-support
  329. setlocale(LC_ALL, ".UTF8");
  330. const auto appUserModelId = AppUserModelId::Id();
  331. SetCurrentProcessExplicitAppUserModelID(appUserModelId.c_str());
  332. LOG(("AppUserModelID: %1").arg(appUserModelId));
  333. }
  334. void finish() {
  335. }
  336. void SetApplicationIcon(const QIcon &icon) {
  337. QApplication::setWindowIcon(icon);
  338. }
  339. QString SingleInstanceLocalServerName(const QString &hash) {
  340. return u"Global\\"_q + hash + '-' + cGUIDStr();
  341. }
  342. #if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
  343. std::optional<bool> IsDarkMode() {
  344. static const auto kSystemVersion = QOperatingSystemVersion::current();
  345. static const auto kDarkModeAddedVersion = QOperatingSystemVersion(
  346. QOperatingSystemVersion::Windows,
  347. 10,
  348. 0,
  349. 17763);
  350. static const auto kSupported = (kSystemVersion >= kDarkModeAddedVersion);
  351. if (!kSupported) {
  352. return std::nullopt;
  353. }
  354. HIGHCONTRAST hcf = {};
  355. hcf.cbSize = static_cast<UINT>(sizeof(HIGHCONTRAST));
  356. if (SystemParametersInfo(SPI_GETHIGHCONTRAST, hcf.cbSize, &hcf, FALSE)
  357. && (hcf.dwFlags & HCF_HIGHCONTRASTON)) {
  358. return std::nullopt;
  359. }
  360. const auto keyName = L""
  361. "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
  362. const auto valueName = L"AppsUseLightTheme";
  363. auto key = HKEY();
  364. auto result = RegOpenKeyEx(HKEY_CURRENT_USER, keyName, 0, KEY_READ, &key);
  365. if (result != ERROR_SUCCESS) {
  366. return std::nullopt;
  367. }
  368. DWORD value = 0, type = 0, size = sizeof(value);
  369. result = RegQueryValueEx(key, valueName, 0, &type, (LPBYTE)&value, &size);
  370. RegCloseKey(key);
  371. if (result != ERROR_SUCCESS) {
  372. return std::nullopt;
  373. }
  374. return (value == 0);
  375. }
  376. #endif // Qt < 6.5.0
  377. bool AutostartSupported() {
  378. return true;
  379. }
  380. void AutostartRequestStateFromSystem(Fn<void(bool)> callback) {
  381. #ifdef OS_WIN_STORE
  382. AutostartTask::RequestState([=](bool enabled) {
  383. crl::on_main([=] {
  384. callback(enabled);
  385. });
  386. });
  387. #endif // OS_WIN_STORE
  388. }
  389. void AutostartToggle(bool enabled, Fn<void(bool)> done) {
  390. #ifdef OS_WIN_STORE
  391. const auto requested = enabled;
  392. const auto callback = [=](bool enabled) { crl::on_main([=] {
  393. if (!Core::IsAppLaunched()) {
  394. return;
  395. }
  396. done(enabled);
  397. if (!requested || enabled) {
  398. return;
  399. } else if (const auto window = Core::App().activeWindow()) {
  400. window->show(Ui::MakeConfirmBox({
  401. .text = tr::lng_settings_auto_start_disabled_uwp(),
  402. .confirmed = [](Fn<void()> close) {
  403. AutostartTask::OpenSettings();
  404. close();
  405. },
  406. .confirmText = tr::lng_settings_open_system_settings(),
  407. }));
  408. }
  409. }); };
  410. AutostartTask::Toggle(
  411. enabled,
  412. done ? Fn<void(bool)>(callback) : nullptr);
  413. #else // OS_WIN_STORE
  414. const auto silent = !done;
  415. const auto success = ManageAppLink(
  416. enabled,
  417. silent,
  418. FOLDERID_Startup,
  419. L"-autostart",
  420. L"Telegram autorun link.\n"
  421. "You can disable autorun in Telegram settings.");
  422. if (done) {
  423. done(enabled && success);
  424. }
  425. #endif // OS_WIN_STORE
  426. }
  427. bool AutostartSkip() {
  428. #ifdef OS_WIN_STORE
  429. return false;
  430. #else // OS_WIN_STORE
  431. return !cAutoStart();
  432. #endif // OS_WIN_STORE
  433. }
  434. void WriteCrashDumpDetails() {
  435. #ifndef TDESKTOP_DISABLE_CRASH_REPORTS
  436. PROCESS_MEMORY_COUNTERS data = { 0 };
  437. if (Dlls::GetProcessMemoryInfo
  438. && Dlls::GetProcessMemoryInfo(
  439. GetCurrentProcess(),
  440. &data,
  441. sizeof(data))) {
  442. const auto mb = 1024 * 1024;
  443. CrashReports::dump()
  444. << "Memory-usage: "
  445. << (data.PeakWorkingSetSize / mb)
  446. << " MB (peak), "
  447. << (data.WorkingSetSize / mb)
  448. << " MB (current)\n";
  449. CrashReports::dump()
  450. << "Pagefile-usage: "
  451. << (data.PeakPagefileUsage / mb)
  452. << " MB (peak), "
  453. << (data.PagefileUsage / mb)
  454. << " MB (current)\n";
  455. }
  456. #endif // TDESKTOP_DISABLE_CRASH_REPORTS
  457. }
  458. void SetWindowPriority(not_null<QWidget*> window, uint32 priority) {
  459. const auto hwnd = reinterpret_cast<HWND>(window->winId());
  460. Assert(hwnd != nullptr);
  461. SetWindowLongPtr(hwnd, GWLP_USERDATA, static_cast<LONG_PTR>(priority));
  462. }
  463. uint64 ActivationWindowId(not_null<QWidget*> window) {
  464. return WindowIdFromHWND(reinterpret_cast<HWND>(window->winId()));
  465. }
  466. void ActivateOtherProcess(uint64 processId, uint64 windowId) {
  467. auto request = FindToActivateRequest{
  468. .processId = processId,
  469. .windowId = windowId,
  470. };
  471. ::EnumWindows((WNDENUMPROC)FindToActivate, (LPARAM)&request);
  472. if (const auto hwnd = request.result) {
  473. ::SetForegroundWindow(hwnd);
  474. ::SetFocus(hwnd);
  475. }
  476. }
  477. } // namespace Platform
  478. namespace {
  479. void _psLogError(const char *str, LSTATUS code) {
  480. LPWSTR errorTextFormatted = nullptr;
  481. auto formatFlags = FORMAT_MESSAGE_FROM_SYSTEM
  482. | FORMAT_MESSAGE_ALLOCATE_BUFFER
  483. | FORMAT_MESSAGE_IGNORE_INSERTS;
  484. FormatMessage(
  485. formatFlags,
  486. NULL,
  487. code,
  488. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  489. (LPTSTR)&errorTextFormatted,
  490. 0,
  491. 0);
  492. auto errorText = errorTextFormatted
  493. ? errorTextFormatted
  494. : L"(Unknown error)";
  495. LOG((str).arg(code).arg(QString::fromStdWString(errorText)));
  496. LocalFree(errorTextFormatted);
  497. }
  498. bool _psOpenRegKey(LPCWSTR key, PHKEY rkey) {
  499. DEBUG_LOG(("App Info: opening reg key %1...").arg(QString::fromStdWString(key)));
  500. LSTATUS status = RegOpenKeyEx(HKEY_CURRENT_USER, key, 0, KEY_QUERY_VALUE | KEY_WRITE, rkey);
  501. if (status != ERROR_SUCCESS) {
  502. if (status == ERROR_FILE_NOT_FOUND) {
  503. status = RegCreateKeyEx(HKEY_CURRENT_USER, key, 0, 0, REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE | KEY_WRITE, 0, rkey, 0);
  504. if (status != ERROR_SUCCESS) {
  505. QString msg = u"App Error: could not create '%1' registry key, error %2"_q.arg(QString::fromStdWString(key)).arg(u"%1: %2"_q);
  506. _psLogError(msg.toUtf8().constData(), status);
  507. return false;
  508. }
  509. } else {
  510. QString msg = u"App Error: could not open '%1' registry key, error %2"_q.arg(QString::fromStdWString(key)).arg(u"%1: %2"_q);
  511. _psLogError(msg.toUtf8().constData(), status);
  512. return false;
  513. }
  514. }
  515. return true;
  516. }
  517. bool _psSetKeyValue(HKEY rkey, LPCWSTR value, QString v) {
  518. static const int bufSize = 4096;
  519. DWORD defaultType, defaultSize = bufSize * 2;
  520. WCHAR defaultStr[bufSize] = { 0 };
  521. if (RegQueryValueEx(rkey, value, 0, &defaultType, (BYTE*)defaultStr, &defaultSize) != ERROR_SUCCESS || defaultType != REG_SZ || defaultSize != (v.size() + 1) * 2 || QString::fromStdWString(defaultStr) != v) {
  522. WCHAR tmp[bufSize] = { 0 };
  523. if (!v.isEmpty()) StringCbPrintf(tmp, bufSize, v.replace(QChar('%'), u"%%"_q).toStdWString().c_str());
  524. LSTATUS status = RegSetValueEx(rkey, value, 0, REG_SZ, (BYTE*)tmp, (wcslen(tmp) + 1) * sizeof(WCHAR));
  525. if (status != ERROR_SUCCESS) {
  526. QString msg = u"App Error: could not set %1, error %2"_q.arg(value ? ('\'' + QString::fromStdWString(value) + '\'') : u"(Default)"_q).arg("%1: %2");
  527. _psLogError(msg.toUtf8().constData(), status);
  528. return false;
  529. }
  530. }
  531. return true;
  532. }
  533. }
  534. namespace Platform {
  535. PermissionStatus GetPermissionStatus(PermissionType type) {
  536. if (type == PermissionType::Microphone) {
  537. PermissionStatus result = PermissionStatus::Granted;
  538. HKEY hKey;
  539. LSTATUS res = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\CapabilityAccessManager\\ConsentStore\\microphone", 0, KEY_QUERY_VALUE, &hKey);
  540. if (res == ERROR_SUCCESS) {
  541. wchar_t buf[20];
  542. DWORD length = sizeof(buf);
  543. res = RegQueryValueEx(hKey, L"Value", NULL, NULL, (LPBYTE)buf, &length);
  544. if (res == ERROR_SUCCESS) {
  545. if (wcscmp(buf, L"Deny") == 0) {
  546. result = PermissionStatus::Denied;
  547. }
  548. }
  549. RegCloseKey(hKey);
  550. }
  551. return result;
  552. }
  553. return PermissionStatus::Granted;
  554. }
  555. void RequestPermission(PermissionType type, Fn<void(PermissionStatus)> resultCallback) {
  556. resultCallback(PermissionStatus::Granted);
  557. }
  558. void OpenSystemSettingsForPermission(PermissionType type) {
  559. if (type == PermissionType::Microphone) {
  560. crl::on_main([] {
  561. ShellExecute(
  562. nullptr,
  563. L"open",
  564. L"ms-settings:privacy-microphone",
  565. nullptr,
  566. nullptr,
  567. SW_SHOWDEFAULT);
  568. });
  569. }
  570. }
  571. bool OpenSystemSettings(SystemSettingsType type) {
  572. if (type == SystemSettingsType::Audio) {
  573. crl::on_main([] {
  574. WinExec("control.exe mmsys.cpl", SW_SHOW);
  575. //QDesktopServices::openUrl(QUrl("ms-settings:sound"));
  576. });
  577. }
  578. return true;
  579. }
  580. void NewVersionLaunched(int oldVersion) {
  581. if (oldVersion <= 4009009) {
  582. AppUserModelId::CheckPinned();
  583. }
  584. if (oldVersion > 0 && oldVersion < 2008012) {
  585. // Reset icons cache, because we've changed the application icon.
  586. if (Dlls::SHChangeNotify) {
  587. Dlls::SHChangeNotify(
  588. SHCNE_ASSOCCHANGED,
  589. SHCNF_IDLIST,
  590. nullptr,
  591. nullptr);
  592. }
  593. }
  594. }
  595. QImage DefaultApplicationIcon() {
  596. return Window::Logo();
  597. }
  598. } // namespace Platform
  599. void psSendToMenu(bool send, bool silent) {
  600. ManageAppLink(
  601. send,
  602. silent,
  603. FOLDERID_SendTo,
  604. L"-sendpath",
  605. L"Telegram send to link.\n"
  606. "You can disable send to menu item in Telegram settings.");
  607. }
  608. bool psLaunchMaps(const Data::LocationPoint &point) {
  609. const auto aar = base::WinRT::TryCreateInstance<
  610. IApplicationAssociationRegistration
  611. >(CLSID_ApplicationAssociationRegistration);
  612. if (!aar) {
  613. return false;
  614. }
  615. auto handler = base::CoTaskMemString();
  616. const auto result = aar->QueryCurrentDefault(
  617. L"bingmaps",
  618. AT_URLPROTOCOL,
  619. AL_EFFECTIVE,
  620. handler.put());
  621. if (FAILED(result)
  622. || !handler
  623. || !handler.data()
  624. || std::wstring(handler.data()) == L"bingmaps") {
  625. return false;
  626. }
  627. const auto url = u"bingmaps:?lvl=16&collection=point.%1_%2_Point"_q;
  628. return QDesktopServices::openUrl(
  629. url.arg(point.latAsString()).arg(point.lonAsString()));
  630. }
  631. // Stub while we still support Windows 7.
  632. extern "C" {
  633. STDAPI GetDpiForMonitor(
  634. _In_ HMONITOR hmonitor,
  635. _In_ MONITOR_DPI_TYPE dpiType,
  636. _Out_ UINT *dpiX,
  637. _Out_ UINT *dpiY) {
  638. return Dlls::GetDpiForMonitor
  639. ? Dlls::GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY)
  640. : E_FAIL;
  641. }
  642. } // extern "C"