p≡p COM server adapter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "stdafx.h"
  2. #include "CMainWindow.h"
  3. #include "GateKeeper.h"
  4. static const GUID nidGUID =
  5. { 0xa4dbdbe1, 0x4051, 0x4d89, { 0xb1, 0x17, 0x62, 0x82, 0x18, 0x5a, 0x61, 0x5c } };
  6. CMainWindow::CMainWindow() :
  7. _schedule_updates(true), CWindowImpl<CMainWindow>()
  8. {
  9. ULONG ulNumLanguages = 0;
  10. PZZWSTR pwszLanguagesBuffer = NULL;
  11. ULONG pcchLanguagesBuffer = 0;
  12. BOOL bResult = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &ulNumLanguages, NULL, &pcchLanguagesBuffer);
  13. assert(bResult);
  14. pwszLanguagesBuffer = new WCHAR[pcchLanguagesBuffer];
  15. bResult = GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &ulNumLanguages, pwszLanguagesBuffer, &pcchLanguagesBuffer);
  16. assert(bResult);
  17. bResult = SetProcessPreferredUILanguages(MUI_LANGUAGE_NAME, pwszLanguagesBuffer, &ulNumLanguages);
  18. assert(bResult);
  19. delete[] pwszLanguagesBuffer;
  20. }
  21. LRESULT CMainWindow::OnCreate(UINT, WPARAM, LPARAM, BOOL&)
  22. {
  23. // remove leftoff before creating a new one
  24. BOOL _b;
  25. OnDestroy(0, 0, 0, _b);
  26. NOTIFYICONDATA nid = {};
  27. nid.cbSize = sizeof(nid);
  28. nid.uFlags = NIF_ICON | NIF_TIP | NIF_GUID | NIF_MESSAGE;
  29. nid.hWnd = m_hWnd;
  30. nid.guidItem = nidGUID;
  31. StringCchCopy(nid.szTip, ARRAYSIZE(nid.szTip), r(IDS_PROJNAME).c_str());
  32. nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_LOGO));
  33. nid.uCallbackMessage = WM_PEP_NOTIFICATION;
  34. Shell_NotifyIcon(NIM_ADD, &nid);
  35. nid = {};
  36. nid.cbSize = sizeof(nid);
  37. nid.uVersion = NOTIFYICON_VERSION_4;
  38. Shell_NotifyIcon(NIM_SETVERSION, &nid);
  39. return S_OK;
  40. }
  41. LRESULT CMainWindow::OnDestroy(UINT, WPARAM, LPARAM, BOOL&)
  42. {
  43. NOTIFYICONDATA nid = {};
  44. nid.cbSize = sizeof(nid);
  45. nid.uFlags = NIF_ICON | NIF_GUID;
  46. nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_LOGO));
  47. nid.guidItem = nidGUID;
  48. Shell_NotifyIcon(NIM_DELETE, &nid);
  49. return S_OK;
  50. }
  51. LRESULT CMainWindow::OnNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  52. {
  53. auto event = LOWORD(lParam);
  54. auto icon_id = HIWORD(lParam);
  55. auto x = GET_X_LPARAM(wParam);
  56. auto y = GET_Y_LPARAM(wParam);
  57. HMENU menuContext;
  58. HMENU _menuContext = NULL;
  59. MENUINFO info;
  60. BOOL enabled;
  61. switch (event) {
  62. case WM_CONTEXTMENU:
  63. case WM_RBUTTONDOWN:
  64. menuContext = LoadMenu(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_POPUPMENU));
  65. info = {};
  66. info.cbSize = sizeof(info);
  67. info.fMask = MIM_APPLYTOSUBMENUS | MIM_STYLE;
  68. info.dwStyle = MNS_AUTODISMISS | MNS_NOTIFYBYPOS;
  69. SetMenuInfo(menuContext, &info);
  70. _menuContext = GetSubMenu(menuContext, 0);
  71. enabled = pEp::GateKeeper::gatekeeper()->update_enabled();
  72. CheckMenuItem(_menuContext, ID_POPUP_SCHEDULEUPDATES, enabled ? MF_CHECKED : MF_UNCHECKED);
  73. SetForegroundWindow(m_hWnd); // this is utter nonsense, but required by TrackPopupMenuEx
  74. POINT point;
  75. GetCursorPos(&point);
  76. TrackPopupMenuEx(_menuContext, TPM_LEFTALIGN | TPM_BOTTOMALIGN, point.x, point.y, m_hWnd, NULL);
  77. PostMessage(WM_NULL, 0, 0); // this is utter nonsense, but required by TrackPopupMenuEx
  78. DestroyMenu(menuContext);
  79. bHandled = true;
  80. break;
  81. default:
  82. bHandled = false;
  83. }
  84. return S_OK;
  85. }
  86. static const auto UPDATE_NOW = 0;
  87. static const auto SCHEDULE_UPDATES = 2;
  88. LRESULT CMainWindow::OnMenuCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  89. {
  90. auto index = wParam;
  91. HMENU hMenu = (HMENU)lParam;
  92. BOOL enabled;
  93. switch (index) {
  94. case UPDATE_NOW:
  95. ShowNotificationInfo(r(IDS_UPDATESTITLE), r(IDS_UPDATESTEXT));
  96. pEp::GateKeeper::gatekeeper()->update_now();
  97. bHandled = true;
  98. break;
  99. case SCHEDULE_UPDATES:
  100. enabled = !pEp::GateKeeper::gatekeeper()->update_enabled();
  101. if (enabled)
  102. pEp::GateKeeper::gatekeeper()->enable_update();
  103. else
  104. pEp::GateKeeper::gatekeeper()->disable_update();
  105. CheckMenuItem(hMenu, ID_POPUP_SCHEDULEUPDATES, enabled ? MF_CHECKED : MF_UNCHECKED);
  106. bHandled = true;
  107. break;
  108. default:
  109. bHandled = false;
  110. }
  111. return S_OK;
  112. }
  113. CMainWindow::tstring CMainWindow::r(UINT resid)
  114. {
  115. LPCTSTR rstr;
  116. size_t rsize = LoadString(GetModuleHandle(NULL), resid, (LPWSTR) &rstr, 0);
  117. tstring str = tstring(rstr, rsize);
  118. return str;
  119. }
  120. void CMainWindow::ShowNotificationInfo(tstring title, tstring text)
  121. {
  122. NOTIFYICONDATA nid = {};
  123. nid.cbSize = sizeof(nid);
  124. nid.uFlags = NIF_GUID | NIF_MESSAGE | NIF_INFO;
  125. nid.dwInfoFlags = NIIF_LARGE_ICON;
  126. nid.hWnd = m_hWnd;
  127. nid.guidItem = nidGUID;
  128. nid.hBalloonIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_LOGO));
  129. StringCchCopy(nid.szInfoTitle, ARRAYSIZE(nid.szInfoTitle), title.substr(0, ARRAYSIZE(nid.szInfoTitle)-1).c_str());
  130. StringCchCopy(nid.szInfo, ARRAYSIZE(nid.szInfo), text.substr(0, ARRAYSIZE(nid.szInfo) - 1).c_str());
  131. nid.uCallbackMessage = WM_PEP_NOTIFICATION;
  132. Shell_NotifyIcon(NIM_MODIFY, &nid);
  133. }