source: nscp/trunk/modules/SysTray/TrayIcon.cpp @ 81cf57a

Last change on this file since 81cf57a was 2b180e0, checked in by Michael Medin <michael@…>, 6 years ago

Cleanup build enviornment a bit and updated to it builds on w64 (-regular expressions support (boost))
Fixed an issue with the nsclient-listener getting the wrong password

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#include "stdafx.h"
22#include ".\trayicon.h"
23#include "resource.h"
24#include <strEx.h>
25#include <ShellAPI.h>
26
27unsigned IconWidget_::threadProc(LPVOID lpParameter)
28{
29        createDialog();
30        return 0;
31}
32
33
34void IconWidget_::createDialog(void) {
35        hDlgWnd = ::CreateDialog(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_NSTRAYDLG),NULL,TrayIcon::DialogProc);
36
37        MSG Msg;
38        while(::GetMessage(&Msg, hDlgWnd, 0, 0))
39        {
40                if (Msg.message == WM_MY_CLOSE)
41                        break;
42                if (!::IsWindow(hDlgWnd) || !::IsDialogMessage(hDlgWnd, &Msg)) {
43                        ::TranslateMessage(&Msg);
44                        ::DispatchMessage(&Msg);
45                }
46        }
47        TrayIcon::removeIcon(hDlgWnd);
48
49        ::DestroyWindow(hDlgWnd);
50}
51void IconWidget_::exitThread(void) {
52        ::PostMessage(hDlgWnd, WM_MY_CLOSE, NULL, NULL);
53
54}
55
56namespace TrayIcon
57{
58        std::string defaultCommand;
59}
60
61/*
62INT_PTR CALLBACK DialogProc(          HWND hwndDlg,
63    UINT uMsg,
64    WPARAM wParam,
65    LPARAM lParam
66);
67*/
68INT_PTR CALLBACK TrayIcon::InjectDialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) {
69        switch (uMsg)
70        {
71        case WM_INITDIALOG:
72                SetDlgItemText(hwndDlg, IDC_COMMAND, TrayIcon::defaultCommand.c_str());
73        case WM_COMMAND:
74                switch (LOWORD(wParam))
75                {
76                case IDOK:
77                        {
78                                char *c=new char[1024];
79                                if (GetDlgItemText(hwndDlg, IDC_COMMAND, c, 1023))
80                                        TrayIcon::defaultCommand = c;
81                                delete [] c;
82                        }
83
84                case IDCANCEL:
85                        EndDialog(hwndDlg, wParam);
86                        return TRUE;
87                }
88        }
89        return FALSE;
90}
91
92INT_PTR CALLBACK TrayIcon::DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
93{
94        switch (uMsg)
95        {
96        case WM_INITDIALOG:
97                addIcon(hwndDlg);
98                break;
99
100        case WM_ICON_NOTIFY:
101                if (lParam==WM_RBUTTONDOWN) {
102                        HMENU hMenu = LoadMenu(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDR_POPUP));
103                        HMENU hSubMenu = GetSubMenu(hMenu, 0);
104                        const RECT r = {0, 0, 0, 0};
105                        POINT pt;
106                        GetCursorPos(&pt);
107                        SetForegroundWindow(hwndDlg);
108                        int cmd = TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_RETURNCMD, pt.x, pt.y, 0, hwndDlg, &r);
109                        DestroyMenu(hMenu);
110                        switch (cmd) {
111                        case ID_POPUP_STOPSERVICE:
112                                NSCModuleHelper::StopService();
113                                break;
114                        case ID_POPUP_INJECTCOMMAND:
115                                if (TrayIcon::defaultCommand.empty())
116                                        TrayIcon::defaultCommand = NSCModuleHelper::getSettingsString("systray", "defaultCommand", "");
117                                if (DialogBox(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_INJECTDIALOG),NULL,InjectDialogProc) == IDOK) {
118                                        // @todo NSCModuleHelper::InjectCommand(TrayIcon::defaultCommand);
119                                }
120                                break;
121                        case ID_POPUP_SHOWLOG:
122                                {
123                                        long long err = reinterpret_cast<long long>(ShellExecute(hwndDlg, "open",
124                                                (NSCModuleHelper::getBasePath() + NSCModuleHelper::getSettingsString("log", "file", "")).c_str(),
125                                                NULL, NULL, SW_SHOWNORMAL));
126                                        if (err <=32) {
127                                                        NSC_LOG_ERROR("ShellExecute failed : " + strEx::itos(err));
128                                                }
129                                }
130                        }
131                        return TRUE;
132                }
133                break;
134        }
135        return FALSE;
136}
137void TrayIcon::addIcon(HWND hWnd) {
138        assert(NSCModuleWrapper::getModule() != NULL);
139        assert(hWnd != NULL);
140
141        NOTIFYICONDATA ndata;
142        ndata.cbSize=sizeof(NOTIFYICONDATA);
143        ndata.hWnd=hWnd;
144        ndata.uID=2000;
145        ndata.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
146        ndata.uCallbackMessage=WM_ICON_NOTIFY;
147        ndata.hIcon=::LoadIcon(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDI_STANDBY));
148        strncpy(ndata.szTip,(NSCModuleHelper::getApplicationName() + " - " + NSCModuleHelper::getApplicationVersionString()).c_str(), 63);
149        Shell_NotifyIcon(NIM_ADD,&ndata);
150}
151
152void TrayIcon::removeIcon(HWND hWnd) {
153        NOTIFYICONDATA ndata;
154        ndata.cbSize=sizeof(NOTIFYICONDATA);
155        ndata.hWnd=hWnd;
156        ndata.uID=2000;
157        Shell_NotifyIcon(NIM_DELETE,&ndata);
158}
Note: See TracBrowser for help on using the repository browser.