/ src / common / notifications / dont_show_again.cpp
dont_show_again.cpp
 1  #include "pch.h"
 2  #include "dont_show_again.h"
 3  
 4  namespace notifications
 5  {
 6      bool disable_toast(const wchar_t* registry_path)
 7      {
 8          HKEY key{};
 9          if (RegCreateKeyExW(HKEY_CURRENT_USER,
10                              registry_path,
11                              0,
12                              nullptr,
13                              REG_OPTION_NON_VOLATILE,
14                              KEY_ALL_ACCESS,
15                              nullptr,
16                              &key,
17                              nullptr) != ERROR_SUCCESS)
18          {
19              return false;
20          }
21          const auto now = timeutil::now();
22          const size_t buf_size = sizeof(now);
23          if (RegSetValueExW(key, nullptr, 0, REG_QWORD, reinterpret_cast<const BYTE*>(&now), buf_size) != ERROR_SUCCESS)
24          {
25              RegCloseKey(key);
26              return false;
27          }
28          RegCloseKey(key);
29          return true;
30      }
31  
32      bool is_toast_disabled(const wchar_t* registry_path, const int64_t disable_interval_in_days)
33      {
34          HKEY key{};
35          if (RegOpenKeyExW(HKEY_CURRENT_USER,
36                            registry_path,
37                            0,
38                            KEY_READ,
39                            &key) != ERROR_SUCCESS)
40          {
41              return false;
42          }
43          std::wstring buffer(std::numeric_limits<time_t>::digits10 + 2, L'\0');
44          time_t last_disabled_time{};
45          DWORD time_size = static_cast<DWORD>(sizeof(last_disabled_time));
46          if (RegGetValueW(
47                  key,
48                  nullptr,
49                  nullptr,
50                  RRF_RT_REG_QWORD,
51                  nullptr,
52                  &last_disabled_time,
53                  &time_size) != ERROR_SUCCESS)
54          {
55              RegCloseKey(key);
56              return false;
57          }
58          RegCloseKey(key);
59          return timeutil::diff::in_days(timeutil::now(), last_disabled_time) < disable_interval_in_days;
60      }
61  }