source: nscp/modules/SysTray/TrayIcon.cpp @ 2a94f3f

0.4.00.4.10.4.2stable
Last change on this file since 2a94f3f was 2a94f3f, checked in by Michael Medin <michael@…>, 8 years ago
  • Refactored out NSCLient Listener as a separate module
  • Added initial NRPE listener module (not yet implemented only a shell)
  • Changed Module API (Inject function has new syntax)
  • Added some tokenizer function to charEx
  • Added new wrapper function to inject Command
  • Minor changes in relation to refactor work
  • Property mode set to 100644
File size: 3.9 KB
Line 
1#include "stdafx.h"
2#include ".\trayicon.h"
3#include "resource.h"
4#include <strEx.h>
5#include <ShellAPI.h>
6
7namespace TrayIcon
8{
9        HWND ghDlgWnd = NULL;
10        HANDLE ghMutex;
11        std::string defaultCommand;
12}
13
14BOOL CALLBACK TrayIcon::InjectDialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) {
15        switch (uMsg)
16        {
17        case WM_INITDIALOG:
18                SetDlgItemText(hwndDlg, IDC_COMMAND, TrayIcon::defaultCommand.c_str());
19        case WM_COMMAND:
20                switch (LOWORD(wParam))
21                {
22                case IDOK:
23                        {
24                                char *c=new char[1024];
25                                if (GetDlgItemText(hwndDlg, IDC_COMMAND, c, 1023))
26                                        TrayIcon::defaultCommand = c;
27                                delete [] c;
28                        }
29
30                case IDCANCEL:
31                        EndDialog(hwndDlg, wParam);
32                        return TRUE;
33                }
34        }
35        return FALSE;
36}
37
38INT_PTR CALLBACK TrayIcon::DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
39{
40        switch (uMsg)
41        {
42        case WM_INITDIALOG:
43                addIcon(hwndDlg);
44                break;
45
46        case WM_ICON_NOTIFY:
47                if (lParam==WM_RBUTTONDOWN) {
48                        HMENU hMenu = LoadMenu(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDR_POPUP));
49                        HMENU hSubMenu = GetSubMenu(hMenu, 0);
50                        const RECT r = {0, 0, 0, 0};
51                        POINT pt;
52                        GetCursorPos(&pt);
53                        SetForegroundWindow(ghDlgWnd);
54                        int cmd = TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_RETURNCMD, pt.x, pt.y, 0, ghDlgWnd, &r);
55                        DestroyMenu(hMenu);
56                        switch (cmd) {
57                        case ID_POPUP_STOPSERVICE:
58                                NSCModuleHelper::StopService();
59                                break;
60                        case ID_POPUP_INJECTCOMMAND:
61                                if (TrayIcon::defaultCommand.empty())
62                                        TrayIcon::defaultCommand = NSCModuleHelper::getSettingsString("systray", "defaultCommand", "");
63                                if (DialogBox(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_INJECTDIALOG),NULL,InjectDialogProc) == IDOK) {
64                                        // @todo NSCModuleHelper::InjectCommand(TrayIcon::defaultCommand);
65                                }
66                                break;
67                        case ID_POPUP_SHOWLOG:
68                                {
69                                        long long err = reinterpret_cast<long long>(ShellExecute(ghDlgWnd, "open",
70                                                (NSCModuleHelper::getBasePath() + NSCModuleHelper::getSettingsString("log", "file", "")).c_str(),
71                                                NULL, NULL, SW_SHOWNORMAL));
72                                        if (err <=32) {
73                                                        NSC_LOG_ERROR("ShellExecute failed : " + strEx::itos(err));
74                                                }
75                                }
76                        }
77                        return TRUE;
78                }
79                break;
80        }
81        return FALSE;
82}
83void TrayIcon::addIcon(HWND hWnd) {
84        assert(NSCModuleWrapper::getModule() != NULL);
85        assert(hWnd != NULL);
86
87        NOTIFYICONDATA ndata;
88        ndata.cbSize=sizeof(NOTIFYICONDATA);
89        ndata.hWnd=hWnd;
90        ndata.uID=2000;
91        ndata.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
92        ndata.uCallbackMessage=WM_ICON_NOTIFY;
93        ndata.hIcon=::LoadIcon(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDI_STANDBY));
94        strncpy(ndata.szTip,(NSCModuleHelper::getApplicationName() + " - " + NSCModuleHelper::getApplicationVersionString()).c_str(), 63);
95        Shell_NotifyIcon(NIM_ADD,&ndata);
96}
97
98void TrayIcon::removeIcon(void) {
99        NOTIFYICONDATA ndata;
100        ndata.hWnd=ghDlgWnd;
101        ndata.uID=2000;
102        Shell_NotifyIcon(NIM_DELETE,&ndata);
103}
104
105void TrayIcon::createDialog(void) {
106        ghMutex = ::CreateMutex(NULL, TRUE, NULL);
107        if (!ghMutex)
108                throw std::string("Could not create mutex.");
109
110        ghDlgWnd = ::CreateDialog(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_NSTRAYDLG),NULL,DialogProc);
111
112        MSG Msg;
113        while(::GetMessage(&Msg, ghDlgWnd, 0, 0))
114        {
115                if (Msg.message == WM_MY_CLOSE)
116                        break;
117                if (!::IsWindow(ghDlgWnd) || !::IsDialogMessage(ghDlgWnd, &Msg)) {
118                        ::TranslateMessage(&Msg);
119                        ::DispatchMessage(&Msg);
120                }
121        }
122        removeIcon();
123
124        ::DestroyWindow(ghDlgWnd);
125        ::ReleaseMutex(ghMutex);
126}
127void TrayIcon::destroyDialog(void) {
128        ::PostMessage(ghDlgWnd, WM_MY_CLOSE, NULL, NULL);
129}
130bool TrayIcon::waitForTermination(DWORD timeout /* = 5000L */) {
131        DWORD dwWaitResult = WaitForSingleObject(ghMutex, timeout);
132        switch (dwWaitResult) {
133                // The thread got mutex ownership.
134        case WAIT_OBJECT_0:
135                ReleaseMutex(ghMutex);
136                CloseHandle(ghMutex);
137                return true;
138                // Cannot get mutex ownership due to time-out.
139        case WAIT_TIMEOUT:
140                return false;
141
142                // Got ownership of the abandoned mutex object.
143        case WAIT_ABANDONED:
144                return false;
145        }
146        return false;
147}
Note: See TracBrowser for help on using the repository browser.