windows_autostart_task.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/windows_autostart_task.h"
  8. #include "base/platform/win/base_windows_winrt.h"
  9. #include <winrt/Windows.ApplicationModel.h>
  10. #include <winrt/Windows.Foundation.h>
  11. #include <winrt/Windows.System.h>
  12. namespace Platform::AutostartTask {
  13. namespace {
  14. using namespace winrt::Windows::ApplicationModel;
  15. using namespace winrt::Windows::System;
  16. using namespace winrt::Windows::Foundation;
  17. [[nodiscard]] bool IsEnabled(StartupTaskState state) {
  18. switch (state) {
  19. case StartupTaskState::Enabled:
  20. case StartupTaskState::EnabledByPolicy:
  21. return true;
  22. case StartupTaskState::Disabled:
  23. case StartupTaskState::DisabledByPolicy:
  24. case StartupTaskState::DisabledByUser:
  25. default:
  26. return false;
  27. }
  28. }
  29. } // namespace
  30. void Toggle(bool enabled, Fn<void(bool)> done) {
  31. if (!base::WinRT::Supported()) {
  32. return;
  33. }
  34. const auto processEnableResult = [=](StartupTaskState state) {
  35. LOG(("Startup Task: Enable finished, state: %1").arg(int(state)));
  36. done(IsEnabled(state));
  37. };
  38. const auto processTask = [=](StartupTask task) {
  39. LOG(("Startup Task: Got it, state: %1, requested: %2"
  40. ).arg(int(task.State())
  41. ).arg(Logs::b(enabled)));
  42. if (IsEnabled(task.State()) == enabled) {
  43. return;
  44. }
  45. if (!enabled) {
  46. LOG(("Startup Task: Disabling."));
  47. task.Disable();
  48. return;
  49. }
  50. LOG(("Startup Task: Requesting enable."));
  51. const auto asyncState = task.RequestEnableAsync();
  52. if (!done) {
  53. return;
  54. }
  55. asyncState.Completed([=](
  56. IAsyncOperation<StartupTaskState> operation,
  57. AsyncStatus status) {
  58. base::WinRT::Try([&] {
  59. processEnableResult(operation.GetResults());
  60. });
  61. });
  62. };
  63. base::WinRT::Try([&] {
  64. StartupTask::GetAsync(L"TelegramStartupTask").Completed([=](
  65. IAsyncOperation<StartupTask> operation,
  66. AsyncStatus status) {
  67. base::WinRT::Try([&] {
  68. processTask(operation.GetResults());
  69. });
  70. });
  71. });
  72. }
  73. void RequestState(Fn<void(bool)> callback) {
  74. Expects(callback != nullptr);
  75. if (!base::WinRT::Supported()) {
  76. return;
  77. }
  78. const auto processTask = [=](StartupTask task) {
  79. DEBUG_LOG(("Startup Task: Got value, state: %1"
  80. ).arg(int(task.State())));
  81. callback(IsEnabled(task.State()));
  82. };
  83. base::WinRT::Try([&] {
  84. StartupTask::GetAsync(L"TelegramStartupTask").Completed([=](
  85. IAsyncOperation<StartupTask> operation,
  86. AsyncStatus status) {
  87. base::WinRT::Try([&] {
  88. processTask(operation.GetResults());
  89. });
  90. });
  91. });
  92. }
  93. void OpenSettings() {
  94. Launcher::LaunchUriAsync(Uri(L"ms-settings:startupapps"));
  95. }
  96. } // namespace Platform::AutostartTask