1 /*
2 This file is part of Telegram Desktop,
3 the official desktop application for the Telegram messaging service.
4 
5 For license and copyright information please follow this link:
6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
7 */
8 #include "platform/win/windows_autostart_task.h"
9 
10 #include "base/platform/win/base_windows_winrt.h"
11 
12 #include <winrt/Windows.ApplicationModel.h>
13 #include <winrt/Windows.Foundation.h>
14 #include <winrt/Windows.System.h>
15 
16 namespace Platform::AutostartTask {
17 namespace {
18 
19 using namespace winrt::Windows::ApplicationModel;
20 using namespace winrt::Windows::System;
21 using namespace winrt::Windows::Foundation;
22 
IsEnabled(StartupTaskState state)23 [[nodiscard]] bool IsEnabled(StartupTaskState state) {
24 	switch (state) {
25 	case StartupTaskState::Enabled:
26 	case StartupTaskState::EnabledByPolicy:
27 		return true;
28 	case StartupTaskState::Disabled:
29 	case StartupTaskState::DisabledByPolicy:
30 	case StartupTaskState::DisabledByUser:
31 	default:
32 		return false;
33 	}
34 }
35 
36 } // namespace
37 
Toggle(bool enabled,Fn<void (bool)> done)38 void Toggle(bool enabled, Fn<void(bool)> done) {
39 	if (!base::WinRT::Supported()) {
40 		return;
41 	}
42 	const auto processEnableResult = [=](StartupTaskState state) {
43 		LOG(("Startup Task: Enable finished, state: %1").arg(int(state)));
44 
45 		done(IsEnabled(state));
46 	};
47 	const auto processTask = [=](StartupTask task) {
48 		LOG(("Startup Task: Got it, state: %1, requested: %2"
49 			).arg(int(task.State())
50 			).arg(Logs::b(enabled)));
51 
52 		if (IsEnabled(task.State()) == enabled) {
53 			return;
54 		}
55 		if (!enabled) {
56 			LOG(("Startup Task: Disabling."));
57 			task.Disable();
58 			return;
59 		}
60 		LOG(("Startup Task: Requesting enable."));
61 		const auto asyncState = task.RequestEnableAsync();
62 		if (!done) {
63 			return;
64 		}
65 		asyncState.Completed([=](
66 				IAsyncOperation<StartupTaskState> operation,
67 				AsyncStatus status) {
68 			base::WinRT::Try([&] {
69 				processEnableResult(operation.GetResults());
70 			});
71 		});
72 	};
73 	base::WinRT::Try([&] {
74 		StartupTask::GetAsync(L"TelegramStartupTask").Completed([=](
75 				IAsyncOperation<StartupTask> operation,
76 				AsyncStatus status) {
77 			base::WinRT::Try([&] {
78 				processTask(operation.GetResults());
79 			});
80 		});
81 	});
82 }
83 
RequestState(Fn<void (bool)> callback)84 void RequestState(Fn<void(bool)> callback) {
85 	Expects(callback != nullptr);
86 
87 	if (!base::WinRT::Supported()) {
88 		return;
89 	}
90 	const auto processTask = [=](StartupTask task) {
91 		DEBUG_LOG(("Startup Task: Got value, state: %1"
92 			).arg(int(task.State())));
93 
94 		callback(IsEnabled(task.State()));
95 	};
96 	base::WinRT::Try([&] {
97 		StartupTask::GetAsync(L"TelegramStartupTask").Completed([=](
98 				IAsyncOperation<StartupTask> operation,
99 				AsyncStatus status) {
100 			base::WinRT::Try([&] {
101 				processTask(operation.GetResults());
102 			});
103 		});
104 	});
105 }
106 
OpenSettings()107 void OpenSettings() {
108 	Launcher::LaunchUriAsync(Uri(L"ms-settings:startupapps"));
109 }
110 
111 } // namespace Platform::AutostartTask
112