]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/libwin32/wintray.cpp
kes Reapply my bat.conf install script in qt-console. I think I
[bacula/bacula] / bacula / src / win32 / libwin32 / wintray.cpp
1 //  Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
2 //
3 //  This file was part of the vnc system.
4 //
5 //  The vnc system is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 2 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
18 //  USA.
19 //
20 // If the source code for the vnc system is not available from the place 
21 // whence you received this file, check http://www.uk.research.att.com/vnc or contact
22 // the authors on vnc@uk.research.att.com for information on obtaining it.
23 //
24 // This file has been adapted to the Win32 version of Bacula
25 // by Kern E. Sibbald.  Many thanks to ATT and James Weatherall,
26 // the original author, for providing an excellent template.
27 //
28 // Copyright 2000-2004, Kern E. Sibbald
29 //
30
31
32
33 // Tray
34
35 // Implementation of a system tray icon & menu for Bacula
36
37 #include "bacula.h"
38 #include "jcr.h"
39 #include "winbacula.h"
40 #include "winservice.h"
41 #include "winres.h"
42 #include "wintray.h"
43
44 // Constants
45 #ifdef properties_implemented
46 const UINT MENU_PROPERTIES_SHOW = RegisterWindowMessage("Bacula.Properties.User.Show");
47 const UINT MENU_DEFAULT_PROPERTIES_SHOW = RegisterWindowMessage("Bacula.Properties.Default.Show");
48 #endif
49 const UINT MENU_ABOUTBOX_SHOW = RegisterWindowMessage("Bacula.AboutBox.Show");
50 const UINT MENU_STATUS_SHOW = RegisterWindowMessage("Bacula.Status.Show");
51 const char *MENU_CLASS_NAME = "BaculaFD Tray Icon";
52
53 extern void terminate_filed(int sig);
54 extern char *bac_status(char *buf, int buf_len);
55 extern int bacstat;
56
57 // Implementation
58
59 bacMenu::bacMenu()
60 {
61    // Create a dummy window to handle tray icon messages
62    WNDCLASSEX wndclass;
63
64    wndclass.cbSize                 = sizeof(wndclass);
65    wndclass.style                  = 0;
66    wndclass.lpfnWndProc    = bacMenu::WndProc;
67    wndclass.cbClsExtra             = 0;
68    wndclass.cbWndExtra             = 0;
69    wndclass.hInstance              = hAppInstance;
70    wndclass.hIcon                  = LoadIcon(NULL, IDI_APPLICATION);
71    wndclass.hCursor                = LoadCursor(NULL, IDC_ARROW);
72    wndclass.hbrBackground  = (HBRUSH) GetStockObject(WHITE_BRUSH);
73    wndclass.lpszMenuName   = (const char *) NULL;
74    wndclass.lpszClassName  = MENU_CLASS_NAME;
75    wndclass.hIconSm                = LoadIcon(NULL, IDI_APPLICATION);
76
77    RegisterClassEx(&wndclass);
78
79    m_hwnd = CreateWindow(MENU_CLASS_NAME,
80                            MENU_CLASS_NAME,
81                            WS_OVERLAPPEDWINDOW,
82                            CW_USEDEFAULT,
83                            CW_USEDEFAULT,
84                            200, 200,
85                            NULL,
86                            NULL,
87                            hAppInstance,
88                            NULL);
89    if (m_hwnd == NULL) {
90       PostQuitMessage(0);
91       return;
92    }
93
94    // record which client created this window
95    SetWindowLong(m_hwnd, GWL_USERDATA, (LONG) this);
96
97    // Timer to trigger icon updating
98    SetTimer(m_hwnd, 1, 5000, NULL);
99
100    // Load the icons for the tray
101    m_idle_icon    = LoadIcon(hAppInstance, MAKEINTRESOURCE(IDI_IDLE));
102    m_running_icon = LoadIcon(hAppInstance, MAKEINTRESOURCE(IDI_RUNNING));
103    m_error_icon   = LoadIcon(hAppInstance, MAKEINTRESOURCE(IDI_JOB_ERROR));
104    m_warn_icon    = LoadIcon(hAppInstance, MAKEINTRESOURCE(IDI_JOB_WARNING));
105
106    // Load the popup menu
107    m_hmenu = LoadMenu(hAppInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
108
109    // Install the tray icon!
110    AddTrayIcon();
111 }
112
113 bacMenu::~bacMenu()
114 {
115    // Remove the tray icon
116    DelTrayIcon();
117         
118    // Destroy the loaded menu
119    if (m_hmenu != NULL)
120       DestroyMenu(m_hmenu);
121 }
122
123 void
124 bacMenu::AddTrayIcon()
125 {
126    SendTrayMsg(NIM_ADD, bacstat);
127 }
128
129 void
130 bacMenu::DelTrayIcon()
131 {
132    SendTrayMsg(NIM_DELETE, 0);
133 }
134
135
136 void
137 bacMenu::UpdateTrayIcon(int bacstat)
138 {
139    (void)bac_status(NULL, 0);
140    SendTrayMsg(NIM_MODIFY, bacstat);
141 }
142
143 void
144 bacMenu::SendTrayMsg(DWORD msg, int bacstat)
145 {
146    struct s_last_job *job;
147    
148    // Create the tray icon message
149    m_nid.hWnd = m_hwnd;
150    m_nid.cbSize = sizeof(m_nid);
151    m_nid.uID = IDI_BACULA;                  // never changes after construction
152    switch (bacstat) {
153    case 0:
154       m_nid.hIcon = m_idle_icon;
155       break;
156    case JS_Running:
157       m_nid.hIcon = m_running_icon;
158       break;
159    case JS_ErrorTerminated:
160       m_nid.hIcon = m_error_icon;
161       break;
162    default:
163       if (last_jobs->size() > 0) {
164          job = (struct s_last_job *)last_jobs->last();
165          if (job->Errors) {
166             m_nid.hIcon = m_warn_icon;
167          } else {
168             m_nid.hIcon = m_idle_icon;
169          }
170       } else {
171          m_nid.hIcon = m_idle_icon;
172       }
173       break;
174    }
175
176    m_nid.uFlags = NIF_ICON | NIF_MESSAGE;
177    m_nid.uCallbackMessage = WM_TRAYNOTIFY;
178
179
180    // Use resource string as tip if there is one
181    if (LoadString(hAppInstance, IDI_BACULA, m_nid.szTip, sizeof(m_nid.szTip))) {
182        m_nid.uFlags |= NIF_TIP;
183    }
184
185    // Try to add the Bacula status to the tip string, if possible
186    if (m_nid.uFlags & NIF_TIP) {
187        bac_status(m_nid.szTip, sizeof(m_nid.szTip));
188    }
189
190    // Send the message
191    if (Shell_NotifyIcon(msg, &m_nid)) {
192       EnableMenuItem(m_hmenu, ID_CLOSE, MF_ENABLED);
193    } else {
194       if (!bacService::RunningAsService()) {
195          if (msg == NIM_ADD) {
196             // The tray icon couldn't be created, so use the Properties dialog
197             // as the main program window
198          // removed because it causes quit when not running as a
199          // service in use with BartPe.
200          // PostQuitMessage(0);
201          }
202       }
203    }
204 }
205
206 // Process window messages
207 LRESULT CALLBACK bacMenu::WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
208 {
209    // This is a static method, so we don't know which instantiation we're 
210    // dealing with. We use Allen Hadden's (ahadden@taratec.com) suggestion 
211    // from a newsgroup to get the pseudo-this.
212    bacMenu *_this = (bacMenu *) GetWindowLong(hwnd, GWL_USERDATA);
213
214    switch (iMsg) {
215
216    // Every five seconds, a timer message causes the icon to update
217    case WM_TIMER:
218       if (bacService::RunningAsService()) {
219           // Attempt to add the icon if it's not already there
220           _this->AddTrayIcon();
221       }
222
223       // Update the icon
224       _this->UpdateTrayIcon(bacstat);
225      break;
226
227    // STANDARD MESSAGE HANDLING
228    case WM_CREATE:
229       return 0;
230
231    case WM_COMMAND:
232       // User has clicked an item on the tray menu
233       switch (LOWORD(wParam)) {
234       case ID_STATUS:
235          // Show the status dialog
236          _this->m_status.Show(TRUE);
237          _this->UpdateTrayIcon(bacstat);
238          break;
239
240       case ID_ABOUT:
241          // Show the About box
242          _this->m_about.Show(TRUE);
243          break;
244
245       case ID_CLOSE:
246          // User selected Close from the tray menu
247          PostMessage(hwnd, WM_CLOSE, 0, 0);
248          break;
249
250       }
251       return 0;
252
253    case WM_TRAYNOTIFY:
254       // User has clicked on the tray icon or the menu
255       {
256          // Get the submenu to use as a pop-up menu
257          HMENU submenu = GetSubMenu(_this->m_hmenu, 0);
258
259          // What event are we responding to, RMB click?
260          if (lParam==WM_RBUTTONUP) {
261             if (submenu == NULL) {
262                     return 0;
263             }
264
265             // Make the first menu item the default (bold font)
266             SetMenuDefaultItem(submenu, 0, TRUE);
267             
268             // Get the current cursor position, to display the menu at
269             POINT mouse;
270             GetCursorPos(&mouse);
271
272             // There's a "bug"
273             // (Microsoft calls it a feature) in Windows 95 that requires calling
274             // SetForegroundWindow. To find out more, search for Q135788 in MSDN.
275             //
276             SetForegroundWindow(_this->m_nid.hWnd);
277
278             // Display the menu at the desired position
279             TrackPopupMenu(submenu,
280                             0, mouse.x, mouse.y, 0,
281                             _this->m_nid.hWnd, NULL);
282
283             return 0;
284          }
285          
286          // Or was there a LMB double click?
287          if (lParam==WM_LBUTTONDBLCLK) {
288              // double click: execute first menu item
289              SendMessage(_this->m_nid.hWnd,
290                          WM_COMMAND, 
291                          GetMenuItemID(submenu, 0),
292                          0);
293          }
294
295          return 0;
296       }
297
298    case WM_CLOSE:
299       if (bacService::RunningAsService()) {
300           _this->DelTrayIcon();
301       }
302       terminate_filed(0);
303       break;
304
305    case WM_DESTROY:
306       // The user wants Bacula to quit cleanly...
307       PostQuitMessage(0);
308       return 0;
309
310    case WM_QUERYENDSESSION:
311       // Are we running as a system service?
312       // Or is the system shutting down (in which case we should check anyway!)
313       if ((!bacService::RunningAsService()) || (lParam == 0)) {
314          // No, so we are about to be killed
315
316          // If there are remote connections then we should verify
317          // that the user is happy about killing them.
318
319          // Finally, post a quit message, just in case
320          PostQuitMessage(0);
321          return TRUE;
322       }
323       return TRUE;
324
325    
326    default:
327       if (iMsg == MENU_ABOUTBOX_SHOW) {
328          // External request to show our About dialog
329          PostMessage(hwnd, WM_COMMAND, MAKELONG(ID_ABOUT, 0), 0);
330          return 0;
331       }
332       if (iMsg == MENU_STATUS_SHOW) {
333          // External request to show our status
334          PostMessage(hwnd, WM_COMMAND, MAKELONG(ID_STATUS, 0), 0);
335          return 0;
336       }
337    }
338
339    // Message not recognised
340    return DefWindowProc(hwnd, iMsg, wParam, lParam);
341 }