tray code cleanup and fixes

* make tray icon reflect connection status again
 * prevent tray context menu popup on right click
 * display proxy settings tab only if enabled
pull/1/head
Heiko Hund 2010-04-23 12:57:00 +02:00
parent a43a0d2bb7
commit f373ca17f7
3 changed files with 367 additions and 484 deletions

49
main.c
View File

@ -201,8 +201,14 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated")); s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
/* Load application icon */
HICON hIcon = LoadLocalizedIcon(ID_ICO_APP);
if (hIcon) {
SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) (hIcon));
SendMessage(hwnd, WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) (hIcon));
}
CreatePopupMenus(); /* Create popup menus */ CreatePopupMenus(); /* Create popup menus */
LoadAppIcon(); /* Load App Icon */
ShowTrayIcon(); ShowTrayIcon();
if (o.allow_service[0]=='1' || o.service_only[0]=='1') if (o.allow_service[0]=='1' || o.service_only[0]=='1')
CheckServiceStatus(); // Check if service is running or not CheckServiceStatus(); // Check if service is running or not
@ -214,7 +220,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
case WM_NOTIFYICONTRAY: case WM_NOTIFYICONTRAY:
OnNotifyTray(lParam); // Manages message from tray OnNotifyTray(lParam); // Manages message from tray
return TRUE; break;
case WM_COMMAND: case WM_COMMAND:
if ( (LOWORD(wParam) >= IDM_CONNECTMENU) && (LOWORD(wParam) < IDM_CONNECTMENU + MAX_CONFIGS) ) { if ( (LOWORD(wParam) >= IDM_CONNECTMENU) && (LOWORD(wParam) < IDM_CONNECTMENU + MAX_CONFIGS) ) {
@ -357,20 +363,29 @@ static void
ShowSettingsDialog() ShowSettingsDialog()
{ {
PROPSHEETPAGE psp[2]; PROPSHEETPAGE psp[2];
psp[0].dwSize = sizeof(PROPSHEETPAGE); int page_number = 0;
psp[0].dwFlags = PSP_DLGINDIRECT;
psp[0].hInstance = o.hInstance; /* Proxy tab */
psp[0].pResource = LocalizedDialogResource(ID_DLG_PROXY); if (o.allow_proxy[0] == '1' && o.service_only[0] == '0') {
psp[0].pfnDlgProc = ProxySettingsDialogFunc; psp[page_number].dwSize = sizeof(PROPSHEETPAGE);
psp[0].lParam = 0; psp[page_number].dwFlags = PSP_DLGINDIRECT;
psp[0].pfnCallback = NULL; psp[page_number].hInstance = o.hInstance;
psp[1].dwSize = sizeof(PROPSHEETPAGE); psp[page_number].pResource = LocalizedDialogResource(ID_DLG_PROXY);
psp[1].dwFlags = PSP_DLGINDIRECT; psp[page_number].pfnDlgProc = ProxySettingsDialogFunc;
psp[1].hInstance = o.hInstance; psp[page_number].lParam = 0;
psp[1].pResource = LocalizedDialogResource(ID_DLG_GENERAL); psp[page_number].pfnCallback = NULL;
psp[1].pfnDlgProc = LanguageSettingsDlgProc; ++page_number;
psp[1].lParam = 0; }
psp[1].pfnCallback = NULL;
/* General tab */
psp[page_number].dwSize = sizeof(PROPSHEETPAGE);
psp[page_number].dwFlags = PSP_DLGINDIRECT;
psp[page_number].hInstance = o.hInstance;
psp[page_number].pResource = LocalizedDialogResource(ID_DLG_GENERAL);
psp[page_number].pfnDlgProc = LanguageSettingsDlgProc;
psp[page_number].lParam = 0;
psp[page_number].pfnCallback = NULL;
++page_number;
PROPSHEETHEADER psh; PROPSHEETHEADER psh;
psh.dwSize = sizeof(PROPSHEETHEADER); psh.dwSize = sizeof(PROPSHEETHEADER);
@ -379,7 +394,7 @@ ShowSettingsDialog()
psh.hInstance = o.hInstance; psh.hInstance = o.hInstance;
psh.hIcon = LoadLocalizedIcon(ID_ICO_APP); psh.hIcon = LoadLocalizedIcon(ID_ICO_APP);
psh.pszCaption = LoadLocalizedString(IDS_SETTINGS_CAPTION); psh.pszCaption = LoadLocalizedString(IDS_SETTINGS_CAPTION);
psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE); psh.nPages = page_number;
psh.nStartPage = 0; psh.nStartPage = 0;
psh.ppsp = (LPCPROPSHEETPAGE) &psp; psh.ppsp = (LPCPROPSHEETPAGE) &psp;
psh.pfnCallback = NULL; psh.pfnCallback = NULL;

742
tray.c
View File

@ -2,6 +2,7 @@
* OpenVPN-GUI -- A Windows GUI for OpenVPN. * OpenVPN-GUI -- A Windows GUI for OpenVPN.
* *
* Copyright (C) 2004 Mathias Sundman <mathias@nilings.se> * Copyright (C) 2004 Mathias Sundman <mathias@nilings.se>
* 2010 Heiko Hund <heikoh@users.sf.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -18,18 +19,11 @@
* distribution); if not, write to the Free Software Foundation, Inc., * distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#define _WIN32_IE 0x0500
#define NIF_INFO 0x00000010 #define _WIN32_IE 0x0500
// Notify Icon Infotip flags
#define NIIF_NONE 0x00000000
// icon flags are mutually exclusive
// and take only the lowest 2 bits
#define NIIF_INFO 0x00000001
#define NIIF_WARNING 0x00000002
#define NIIF_ERROR 0x00000003
#define NIIF_ICON_MASK 0x0000000F
#include <windows.h> #include <windows.h>
#include <shellapi.h>
#include <tchar.h> #include <tchar.h>
#include <time.h> #include <time.h>
@ -44,7 +38,7 @@
#include "openvpn-gui-res.h" #include "openvpn-gui-res.h"
#include "localization.h" #include "localization.h"
//POPUP MENU /* Popup Menus */
HMENU hMenu; HMENU hMenu;
HMENU hMenuConn[MAX_CONFIGS]; HMENU hMenuConn[MAX_CONFIGS];
HMENU hMenuService; HMENU hMenuService;
@ -52,489 +46,371 @@ HMENU hMenuService;
NOTIFYICONDATA ni; NOTIFYICONDATA ni;
extern options_t o; extern options_t o;
// Mouse clicks on tray
void OnNotifyTray(LPARAM lParam) /* Create popup menus */
void
CreatePopupMenus()
{ {
POINT pt; // point structure int i;
int connected_config; for (i = 0; i < o.num_configs; i++)
int i; hMenuConn[i] = CreatePopupMenu();
// Right click, show the menu hMenuService = CreatePopupMenu();
switch(lParam) { hMenu = CreatePopupMenu();
case WM_RBUTTONDOWN:
/* Re-read configs and re-create menus if no connection is running */ if (o.num_configs == 1) {
if (CountConnState(disconnected) == o.num_configs) /* Create Main menu with actions */
{ if (o.service_only[0] == '0') {
DestroyPopupMenus(); AppendMenu(hMenu, MF_STRING, IDM_CONNECTMENU, LoadLocalizedString(IDS_MENU_CONNECT));
BuildFileList(); AppendMenu(hMenu, MF_STRING, IDM_DISCONNECTMENU, LoadLocalizedString(IDS_MENU_DISCONNECT));
CreatePopupMenus(); AppendMenu(hMenu, MF_STRING, IDM_STATUSMENU, LoadLocalizedString(IDS_MENU_STATUS));
AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
}
else {
AppendMenu(hMenu, MF_STRING, IDM_SERVICE_START, LoadLocalizedString(IDS_MENU_SERVICEONLY_START));
AppendMenu(hMenu, MF_STRING, IDM_SERVICE_STOP, LoadLocalizedString(IDS_MENU_SERVICEONLY_STOP));
AppendMenu(hMenu, MF_STRING, IDM_SERVICE_RESTART, LoadLocalizedString(IDS_MENU_SERVICEONLY_RESTART));
AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
} }
GetCursorPos(&pt); // get the cursors position AppendMenu(hMenu, MF_STRING, IDM_VIEWLOGMENU, LoadLocalizedString(IDS_MENU_VIEWLOG));
SetForegroundWindow(o.hWnd); // set the foreground window
TrackPopupMenu(hMenu,TPM_RIGHTALIGN,pt.x,pt.y,0,o.hWnd,NULL);// track the popup
break;
case WM_LBUTTONDOWN: if (o.allow_edit[0] == '1')
break; AppendMenu(hMenu, MF_STRING, IDM_EDITMENU, LoadLocalizedString(IDS_MENU_EDITCONFIG));
case WM_LBUTTONDBLCLK:
if (o.service_only[0]=='1')
{
/* Start OpenVPN Service */
if (o.service_state == service_disconnected)
{
MyStartService();
}
else if (o.service_state == service_connected)
{
/* Stop OpenVPN service */
if (MessageBox(NULL, LoadLocalizedString(IDS_MENU_ASK_STOP_SERVICE), _T(PACKAGE_NAME), MB_YESNO | MB_SETFOREGROUND) == IDYES)
{
MyStopService();
}
}
}
else
{
/* Open Status window if only one connection is running */
connected_config = -1;
for (i=0; i < o.num_configs; i++)
{
if(o.conn[i].state != disconnected)
{
if (connected_config == -1)
{
connected_config = i;
}
else
{
connected_config = -1;
break;
}
}
}
if (connected_config != -1)
{
ShowWindow(o.conn[connected_config].hwndStatus, SW_SHOW);
SetForegroundWindow(o.conn[connected_config].hwndStatus);
}
/* Re-read configs and re-create menus if no connection is running */
if (CountConnState(disconnected) == o.num_configs)
{
DestroyPopupMenus();
BuildFileList();
CreatePopupMenus();
/* Start connection if only one config exist */
if ((o.num_configs == 1) && (o.conn[0].state == disconnected))
StartOpenVPN(0);
}
}
break;
}
PostMessage(o.hWnd,WM_NULL,0,0);// post a null message
}
void OnDestroyTray()
{
//Destroy popup menu
DestroyMenu(hMenu);
//Remove Icon from Tray
Shell_NotifyIcon(NIM_DELETE, &ni);
}
/* Create Popup Menus */
void CreatePopupMenus()
{
int i;
/* Create Main menu */
hMenu=CreatePopupMenu();
/* Create a menu for every config */
for (i=0; i < o.num_configs; i++)
hMenuConn[i]=CreatePopupMenu();
/* Create the Service Menu */
hMenuService=CreatePopupMenu();
/* Put something on the menus */
CreateItemList(hMenu);
}
/* Destroy Popup Menus */
void DestroyPopupMenus()
{
int i;
/* Destroy Main menu */
DestroyMenu(hMenu);
/* Destroy all config submenus */
for (i=0; i < o.num_configs; i++)
DestroyMenu(hMenuConn[i]);
/* Destroy the Service Menu */
DestroyMenu(hMenuService);
}
void CreateItemList()
{
int i;
if (o.num_configs == 1)
{
/* Create Main menu with actions */
if (o.service_only[0]=='0')
{
AppendMenu(hMenu,MF_STRING, IDM_CONNECTMENU, LoadLocalizedString(IDS_MENU_CONNECT));
AppendMenu(hMenu,MF_STRING, IDM_DISCONNECTMENU, LoadLocalizedString(IDS_MENU_DISCONNECT));
AppendMenu(hMenu,MF_STRING, IDM_STATUSMENU, LoadLocalizedString(IDS_MENU_STATUS));
AppendMenu(hMenu,MF_SEPARATOR,0,0);
}
else
{
AppendMenu(hMenu,MF_STRING, IDM_SERVICE_START, LoadLocalizedString(IDS_MENU_SERVICEONLY_START));
AppendMenu(hMenu,MF_STRING, IDM_SERVICE_STOP, LoadLocalizedString(IDS_MENU_SERVICEONLY_STOP));
AppendMenu(hMenu,MF_STRING, IDM_SERVICE_RESTART, LoadLocalizedString(IDS_MENU_SERVICEONLY_RESTART));
AppendMenu(hMenu,MF_SEPARATOR,0,0);
}
AppendMenu(hMenu,MF_STRING, IDM_VIEWLOGMENU, LoadLocalizedString(IDS_MENU_VIEWLOG));
if (o.allow_edit[0]=='1')
{
AppendMenu(hMenu,MF_STRING, IDM_EDITMENU, LoadLocalizedString(IDS_MENU_EDITCONFIG));
}
#ifndef DISABLE_CHANGE_PASSWORD #ifndef DISABLE_CHANGE_PASSWORD
if (o.allow_password[0]=='1') if (o.allow_password[0] == '1')
{ AppendMenu(hMenu, MF_STRING, IDM_PASSPHRASEMENU, LoadLocalizedString(IDS_MENU_PASSPHRASE));
AppendMenu(hMenu,MF_STRING, IDM_PASSPHRASEMENU, LoadLocalizedString(IDS_MENU_PASSPHRASE));
}
#endif #endif
AppendMenu(hMenu,MF_SEPARATOR,0,0); AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
if (o.allow_service[0]=='1' && o.service_only[0]=='0')
if (o.allow_service[0] == '1' && o.service_only[0] == '0')
{ {
AppendMenu(hMenu,MF_POPUP,(UINT) hMenuService, LoadLocalizedString(IDS_MENU_SERVICE)); AppendMenu(hMenu, MF_POPUP, (UINT) hMenuService, LoadLocalizedString(IDS_MENU_SERVICE));
AppendMenu(hMenu,MF_SEPARATOR,0,0); AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
} }
//TODO: if (o.allow_proxy[0]=='1' && o.service_only[0]=='0')
AppendMenu(hMenu,MF_STRING ,IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS));
AppendMenu(hMenu,MF_STRING ,IDM_ABOUT, LoadLocalizedString(IDS_MENU_ABOUT));
AppendMenu(hMenu,MF_STRING ,IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE));
SetMenuStatus(0, disconnected); AppendMenu(hMenu, MF_STRING ,IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS));
AppendMenu(hMenu, MF_STRING ,IDM_ABOUT, LoadLocalizedString(IDS_MENU_ABOUT));
AppendMenu(hMenu, MF_STRING ,IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE));
SetMenuStatus(0, disconnected);
} }
else else {
{ /* Create Main menu with all connections */
/* Create Main menu with all connections */ int i;
for (i=0; i < o.num_configs; i++) for (i = 0; i < o.num_configs; i++)
AppendMenu(hMenu,MF_POPUP,(UINT) hMenuConn[i],o.conn[i].config_name); AppendMenu(hMenu, MF_POPUP, (UINT) hMenuConn[i], o.conn[i].config_name);
if (o.num_configs > 0)
AppendMenu(hMenu,MF_SEPARATOR,0,0);
if (o.allow_service[0]=='1' && o.service_only[0]=='0')
{
AppendMenu(hMenu,MF_POPUP,(UINT) hMenuService, LoadLocalizedString(IDS_MENU_SERVICE));
AppendMenu(hMenu,MF_SEPARATOR,0,0);
}
if (o.service_only[0]=='1')
{
AppendMenu(hMenu,MF_STRING, IDM_SERVICE_START, LoadLocalizedString(IDS_MENU_SERVICEONLY_START));
AppendMenu(hMenu,MF_STRING, IDM_SERVICE_STOP, LoadLocalizedString(IDS_MENU_SERVICEONLY_STOP));
AppendMenu(hMenu,MF_STRING, IDM_SERVICE_RESTART, LoadLocalizedString(IDS_MENU_SERVICEONLY_RESTART));
AppendMenu(hMenu,MF_SEPARATOR,0,0);
}
//TODO: if (o.allow_proxy[0]=='1' && o.service_only[0]=='0')
AppendMenu(hMenu,MF_STRING ,IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS));
AppendMenu(hMenu,MF_STRING ,IDM_ABOUT, LoadLocalizedString(IDS_MENU_ABOUT));
AppendMenu(hMenu,MF_STRING ,IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE));
/* Create PopUp menus for every connection */ if (o.num_configs > 0)
for (i=0; i < o.num_configs; i++) AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
{
if (o.service_only[0]=='0') if (o.service_only[0] == '0' && o.allow_service[0] == '1') {
{ AppendMenu(hMenu, MF_POPUP, (UINT) hMenuService, LoadLocalizedString(IDS_MENU_SERVICE));
AppendMenu(hMenuConn[i],MF_STRING, (UINT_PTR)IDM_CONNECTMENU+i, LoadLocalizedString(IDS_MENU_CONNECT)); AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
AppendMenu(hMenuConn[i],MF_STRING, (UINT_PTR)IDM_DISCONNECTMENU+i, LoadLocalizedString(IDS_MENU_DISCONNECT)); }
AppendMenu(hMenuConn[i],MF_STRING, (UINT_PTR)IDM_STATUSMENU+i, LoadLocalizedString(IDS_MENU_STATUS)); else if (o.service_only[0] == '1') {
AppendMenu(hMenuConn[i],MF_SEPARATOR,0,0); AppendMenu(hMenu, MF_STRING, IDM_SERVICE_START, LoadLocalizedString(IDS_MENU_SERVICEONLY_START));
AppendMenu(hMenu, MF_STRING, IDM_SERVICE_STOP, LoadLocalizedString(IDS_MENU_SERVICEONLY_STOP));
AppendMenu(hMenu, MF_STRING, IDM_SERVICE_RESTART, LoadLocalizedString(IDS_MENU_SERVICEONLY_RESTART));
AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
}
AppendMenu(hMenu, MF_STRING ,IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS));
AppendMenu(hMenu, MF_STRING ,IDM_ABOUT, LoadLocalizedString(IDS_MENU_ABOUT));
AppendMenu(hMenu, MF_STRING ,IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE));
/* Create popup menus for every connection */
for (i=0; i < o.num_configs; i++) {
if (o.service_only[0] == '0') {
AppendMenu(hMenuConn[i], MF_STRING, (UINT_PTR) IDM_CONNECTMENU + i, LoadLocalizedString(IDS_MENU_CONNECT));
AppendMenu(hMenuConn[i], MF_STRING, (UINT_PTR) IDM_DISCONNECTMENU + i, LoadLocalizedString(IDS_MENU_DISCONNECT));
AppendMenu(hMenuConn[i], MF_STRING, (UINT_PTR) IDM_STATUSMENU + i, LoadLocalizedString(IDS_MENU_STATUS));
AppendMenu(hMenuConn[i], MF_SEPARATOR, 0, 0);
} }
AppendMenu(hMenuConn[i], MF_STRING, (UINT_PTR)IDM_VIEWLOGMENU+i, LoadLocalizedString(IDS_MENU_VIEWLOG));
if (o.allow_edit[0]=='1') { AppendMenu(hMenuConn[i], MF_STRING, (UINT_PTR) IDM_VIEWLOGMENU + i, LoadLocalizedString(IDS_MENU_VIEWLOG));
AppendMenu(hMenuConn[i], MF_STRING, (UINT_PTR)IDM_EDITMENU+i, LoadLocalizedString(IDS_MENU_EDITCONFIG));
} if (o.allow_edit[0] == '1')
AppendMenu(hMenuConn[i], MF_STRING, (UINT_PTR) IDM_EDITMENU + i, LoadLocalizedString(IDS_MENU_EDITCONFIG));
#ifndef DISABLE_CHANGE_PASSWORD #ifndef DISABLE_CHANGE_PASSWORD
if (o.allow_password[0]=='1') if (o.allow_password[0] == '1')
{ AppendMenu(hMenuConn[i], MF_STRING, (UINT_PTR) IDM_PASSPHRASEMENU + i, LoadLocalizedString(IDS_MENU_PASSPHRASE));
AppendMenu(hMenuConn[i], MF_STRING, (UINT_PTR)IDM_PASSPHRASEMENU+i, LoadLocalizedString(IDS_MENU_PASSPHRASE));
}
#endif #endif
SetMenuStatus(i, disconnected); SetMenuStatus(i, disconnected);
} }
} }
/* Create Service menu */ /* Create service menu */
if (o.allow_service[0]=='1' && o.service_only[0]=='0') if (o.allow_service[0] == '1' && o.service_only[0] == '0')
{ {
AppendMenu(hMenuService,MF_STRING, IDM_SERVICE_START, LoadLocalizedString(IDS_MENU_SERVICE_START)); AppendMenu(hMenuService, MF_STRING, IDM_SERVICE_START, LoadLocalizedString(IDS_MENU_SERVICE_START));
AppendMenu(hMenuService,MF_STRING, IDM_SERVICE_STOP, LoadLocalizedString(IDS_MENU_SERVICE_STOP)); AppendMenu(hMenuService, MF_STRING, IDM_SERVICE_STOP, LoadLocalizedString(IDS_MENU_SERVICE_STOP));
AppendMenu(hMenuService,MF_STRING, IDM_SERVICE_RESTART, LoadLocalizedString(IDS_MENU_SERVICE_RESTART)); AppendMenu(hMenuService, MF_STRING, IDM_SERVICE_RESTART, LoadLocalizedString(IDS_MENU_SERVICE_RESTART));
} }
SetServiceMenuStatus(); SetServiceMenuStatus();
} }
BOOL LoadAppIcon() /* Destroy popup menus */
static void
DestroyPopupMenus()
{ {
int i;
for (i = 0; i < o.num_configs; i++)
DestroyMenu(hMenuConn[i]);
// Load icon from resource DestroyMenu(hMenuService);
HICON hIcon = LoadLocalizedIcon(ID_ICO_APP); DestroyMenu(hMenu);
if (hIcon) {
SendMessage(o.hWnd, WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) (hIcon));
SendMessage(o.hWnd, WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) (hIcon)); //ALT+TAB icon
return TRUE;
}
return FALSE;
} }
void ShowTrayIcon() /*
{ * Handle mouse clicks on tray icon
ni.cbSize = sizeof(ni);
ni.uID = 0;
_tcsncpy(ni.szTip, LoadLocalizedString(IDS_TIP_DEFAULT), _tsizeof(ni.szTip));
ni.hWnd = o.hWnd;
ni.uFlags = NIF_MESSAGE | NIF_TIP | NIF_ICON; // We want to use icon, tip, and callback message
ni.uCallbackMessage = WM_NOTIFYICONTRAY; // Our custom callback message (WM_APP + 1)
//Load selected icon
ni.hIcon = LoadLocalizedIcon(ID_ICO_DISCONNECTED);
Shell_NotifyIcon(NIM_ADD, &ni);
}
/* SetTrayIcon(int connected)
* connected=0 -> DisConnected
* connected=1 -> Connecting
* connected=2 -> Connected
*/ */
void SetTrayIcon(conn_state_t connected) void
OnNotifyTray(LPARAM lParam)
{ {
TCHAR msg[500]; POINT pt;
TCHAR msg_connected[100];
TCHAR msg_connecting[100];
TCHAR connected_since[50];
int i, first_conn;
int config=0;
ni.cbSize = sizeof(ni); switch (lParam) {
ni.uID = 0; case WM_RBUTTONUP:
ni.hWnd = o.hWnd; /* Recreate popup menus */
ni.uFlags = NIF_MESSAGE | NIF_TIP | NIF_ICON; // We want to use icon, tip, and callback message DestroyPopupMenus();
ni.uCallbackMessage = WM_NOTIFYICONTRAY; // Our custom callback message (WM_APP + 1) if (CountConnState(disconnected) == o.num_configs)
BuildFileList();
_tcsncpy(msg, LoadLocalizedString(IDS_TIP_DEFAULT), _tsizeof(ni.szTip)); CreatePopupMenus();
_tcsncpy(msg_connected, LoadLocalizedString(IDS_TIP_CONNECTED), _tsizeof(msg_connected));
_tcsncpy(msg_connecting, LoadLocalizedString(IDS_TIP_CONNECTING), _tsizeof(msg_connecting));
first_conn=1; GetCursorPos(&pt);
for (i=0; i < o.num_configs; i++) SetForegroundWindow(o.hWnd);
{ TrackPopupMenu(hMenu, TPM_RIGHTALIGN, pt.x, pt.y, 0, o.hWnd, NULL);
if(o.conn[i].state == connected) PostMessage(o.hWnd, WM_NULL, 0, 0);
{ break;
/* Append connection name to Icon Tip Msg */
if (first_conn) case WM_LBUTTONDBLCLK:
_tcsncat(msg, msg_connected, _tsizeof(msg) - _tcslen(msg) - 1); if (o.service_only[0] == '1') {
else /* Start or stop OpenVPN service */
_tcsncat(msg, _T(", "), _tsizeof(msg) - _tcslen(msg) - 1); if (o.service_state == service_disconnected) {
_tcsncat(msg, o.conn[i].config_name, _tsizeof(msg) - _tcslen(msg) - 1); MyStartService();
first_conn=0; }
config=i; else if (o.service_state == service_connected
&& MessageBox(NULL, LoadLocalizedString(IDS_MENU_ASK_STOP_SERVICE),
_T(PACKAGE_NAME), MB_YESNO | MB_SETFOREGROUND) == IDYES) {
MyStopService();
}
} }
} else {
int disconnected_conns = CountConnState(disconnected);
first_conn=1; if (disconnected_conns == o.num_configs) {
for (i=0; i < o.num_configs; i++) /* Reread configs and recreate menus if no connection is running */
{ DestroyPopupMenus();
if((o.conn[i].state == connecting) || BuildFileList();
(o.conn[i].state == reconnecting)) CreatePopupMenus();
{
/* Append connection name to Icon Tip Msg */ /* Start connection if only one config exist */
if (first_conn) if (o.num_configs == 1 && o.conn[0].state == disconnected)
_tcsncat(msg, msg_connecting, _tsizeof(msg) - _tcslen(msg) - 1); StartOpenVPN(0);
else }
_tcsncat(msg, _T(", "), _tsizeof(msg) - _tcslen(msg) - 1); else if (disconnected_conns == o.num_configs - 1) {
_tcsncat(msg, o.conn[i].config_name, _tsizeof(msg) - _tcslen(msg) - 1); /* Show status window if only one connection is running */
first_conn=0; int i;
for (i = 0; i < o.num_configs; i++) {
if (o.conn[i].state != disconnected) {
ShowWindow(o.conn[i].hwndStatus, SW_SHOW);
SetForegroundWindow(o.conn[i].hwndStatus);
break;
}
}
}
} }
break;
} }
if (CountConnState(connected) == 1)
{
/* Append "Connected Since and Assigned IP msg" */
time_t con_time;
con_time=time(NULL);
_tcsftime(connected_since, _tsizeof(connected_since), _T("%b %d, %H:%M"),
localtime(&o.conn[config].connected_since));
_tcsncat(msg, LoadLocalizedString(IDS_TIP_CONNECTED_SINCE), _tsizeof(msg) - _tcslen(msg) - 1);
_tcsncat(msg, connected_since, _tsizeof(msg) - _tcslen(msg) - 1);
if (_tcslen(o.conn[config].ip) > 0)
{
TCHAR assigned_ip[100];
_sntprintf_0(assigned_ip, LoadLocalizedString(IDS_TIP_ASSIGNED_IP), o.conn[config].ip);
_tcsncat(msg, assigned_ip, _tsizeof(msg) - _tcslen(msg) - 1);
}
}
_tcsncpy(ni.szTip, msg, _tsizeof(ni.szTip));
//Load selected icon
if (connected==2)
ni.hIcon = LoadLocalizedIcon(ID_ICO_CONNECTED);
else if (connected==1)
ni.hIcon = LoadLocalizedIcon(ID_ICO_CONNECTING);
else if (connected==0)
ni.hIcon = LoadLocalizedIcon(ID_ICO_DISCONNECTED);
Shell_NotifyIcon(NIM_MODIFY, &ni);
} }
void ShowTrayBalloon(TCHAR *infotitle_msg, TCHAR *info_msg)
void
OnDestroyTray()
{
DestroyMenu(hMenu);
Shell_NotifyIcon(NIM_DELETE, &ni);
}
void
ShowTrayIcon()
{ {
ni.cbSize = sizeof(ni); ni.cbSize = sizeof(ni);
ni.uID = 0; ni.uID = 0;
ni.hWnd = o.hWnd; ni.hWnd = o.hWnd;
ni.uFlags = NIF_INFO; /* We want to show a balloon */ ni.uFlags = NIF_MESSAGE | NIF_TIP | NIF_ICON;
ni.uTimeout = 5000; ni.uCallbackMessage = WM_NOTIFYICONTRAY;
ni.dwInfoFlags = NIIF_INFO; /* Show an Info Icon */ ni.hIcon = LoadLocalizedIcon(ID_ICO_DISCONNECTED);
_tcsncpy(ni.szInfo, info_msg, _tsizeof(ni.szInfo)); _tcsncpy(ni.szTip, LoadLocalizedString(IDS_TIP_DEFAULT), _tsizeof(ni.szTip));
_tcsncpy(ni.szInfoTitle, infotitle_msg, _tsizeof(ni.szInfoTitle));
Shell_NotifyIcon(NIM_MODIFY, &ni); Shell_NotifyIcon(NIM_ADD, &ni);
} }
void
void SetMenuStatus (int config, conn_state_t state) SetTrayIcon(conn_state_t state)
{ {
/* bCheck values: TCHAR msg[500];
* 0 - Not Connected TCHAR msg_connected[100];
* 1 - Connecting TCHAR msg_connecting[100];
* 2 - Connected TCHAR connected_since[50];
* 4 - Disconnecting int i, config = 0;
*/ BOOL first_conn;
UINT icon_id;
_tcsncpy(msg, LoadLocalizedString(IDS_TIP_DEFAULT), _tsizeof(ni.szTip));
_tcsncpy(msg_connected, LoadLocalizedString(IDS_TIP_CONNECTED), _tsizeof(msg_connected));
_tcsncpy(msg_connecting, LoadLocalizedString(IDS_TIP_CONNECTING), _tsizeof(msg_connecting));
unsigned int iState; first_conn = TRUE;
for (i = 0; i < o.num_configs; i++) {
if (o.num_configs == 1) if (o.conn[i].state == connected) {
{ /* Append connection name to Icon Tip Msg */
if (state == disconnected) _tcsncat(msg, (first_conn ? msg_connected : _T(", ")), _tsizeof(msg) - _tcslen(msg) - 1);
{ _tcsncat(msg, o.conn[i].config_name, _tsizeof(msg) - _tcslen(msg) - 1);
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_ENABLED); first_conn = FALSE;
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_GRAYED); config = i;
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_GRAYED);
}
if (state == connecting)
{
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_ENABLED);
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_ENABLED);
}
if (state == connected)
{
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_ENABLED);
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_ENABLED);
}
if (state == disconnecting)
{
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_ENABLED);
}
}
else
{
iState = ((state == connected) || (state == disconnecting)) ?
MF_CHECKED : MF_UNCHECKED ;
CheckMenuItem (hMenu, (UINT) hMenuConn[config], iState) ;
if (state == disconnected)
{
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_CONNECTMENU + config, MF_ENABLED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_DISCONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_STATUSMENU + config, MF_GRAYED);
}
if (state == connecting)
{
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_CONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_DISCONNECTMENU + config, MF_ENABLED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_STATUSMENU + config, MF_ENABLED);
}
if (state == connected)
{
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_CONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_DISCONNECTMENU + config, MF_ENABLED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_STATUSMENU + config, MF_ENABLED);
}
if (state == disconnecting)
{
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_CONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_DISCONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_STATUSMENU + config, MF_ENABLED);
} }
} }
first_conn = TRUE;
for (i = 0; i < o.num_configs; i++) {
if (o.conn[i].state == connecting || o.conn[i].state == reconnecting) {
/* Append connection name to Icon Tip Msg */
_tcsncat(msg, (first_conn ? msg_connecting : _T(", ")), _tsizeof(msg) - _tcslen(msg) - 1);
_tcsncat(msg, o.conn[i].config_name, _tsizeof(msg) - _tcslen(msg) - 1);
first_conn = FALSE;
}
}
if (CountConnState(connected) == 1) {
/* Append "Connected since and assigned IP" to message */
_tcsftime(connected_since, _tsizeof(connected_since), _T("%b %d, %H:%M"),
localtime(&o.conn[config].connected_since));
_tcsncat(msg, LoadLocalizedString(IDS_TIP_CONNECTED_SINCE), _tsizeof(msg) - _tcslen(msg) - 1);
_tcsncat(msg, connected_since, _tsizeof(msg) - _tcslen(msg) - 1);
if (_tcslen(o.conn[config].ip) > 0) {
TCHAR assigned_ip[100];
_sntprintf_0(assigned_ip, LoadLocalizedString(IDS_TIP_ASSIGNED_IP), o.conn[config].ip);
_tcsncat(msg, assigned_ip, _tsizeof(msg) - _tcslen(msg) - 1);
}
}
icon_id = ID_ICO_CONNECTING;
if (state == connected)
icon_id = ID_ICO_CONNECTED;
else if (state == disconnected)
icon_id = ID_ICO_DISCONNECTED;
ni.cbSize = sizeof(ni);
ni.uID = 0;
ni.hWnd = o.hWnd;
ni.hIcon = LoadLocalizedIcon(icon_id);
ni.uFlags = NIF_MESSAGE | NIF_TIP | NIF_ICON;
ni.uCallbackMessage = WM_NOTIFYICONTRAY;
_tcsncpy(ni.szTip, msg, _tsizeof(ni.szTip));
Shell_NotifyIcon(NIM_MODIFY, &ni);
} }
void SetServiceMenuStatus()
void
ShowTrayBalloon(TCHAR *infotitle_msg, TCHAR *info_msg)
{ {
HMENU hMenuHandle; ni.cbSize = sizeof(ni);
ni.uID = 0;
if (o.allow_service[0]=='0' && o.service_only[0]=='0') ni.hWnd = o.hWnd;
return; ni.uFlags = NIF_INFO;
ni.uTimeout = 5000;
if (o.service_only[0]=='1') ni.dwInfoFlags = NIIF_INFO;
hMenuHandle = hMenu; _tcsncpy(ni.szInfo, info_msg, _tsizeof(ni.szInfo));
else _tcsncpy(ni.szInfoTitle, infotitle_msg, _tsizeof(ni.szInfoTitle));
hMenuHandle = hMenuService;
if ((o.service_state == service_noaccess) ||
(o.service_state == service_connecting))
{
/* Service is disabled */
EnableMenuItem(hMenuHandle, IDM_SERVICE_START, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_STOP, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_RESTART, MF_GRAYED);
}
else if (o.service_state == service_connected)
{
/* Service is running */
EnableMenuItem(hMenuHandle, IDM_SERVICE_START, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_STOP, MF_ENABLED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_RESTART, MF_ENABLED);
}
else
{
/* Service is not running */
EnableMenuItem(hMenuHandle, IDM_SERVICE_START, MF_ENABLED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_STOP, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_RESTART, MF_GRAYED);
}
Shell_NotifyIcon(NIM_MODIFY, &ni);
} }
void
SetMenuStatus(int config, conn_state_t state)
{
if (o.num_configs == 1) {
if (state == disconnected) {
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_ENABLED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_GRAYED);
}
else if (state == connecting || state == connected) {
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_ENABLED);
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_ENABLED);
}
else if (state == disconnecting) {
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_ENABLED);
}
}
else {
UINT iState = (state == connected || state == disconnecting) ? MF_CHECKED : MF_UNCHECKED;
CheckMenuItem(hMenu, (UINT) hMenuConn[config], iState);
if (state == disconnected) {
EnableMenuItem(hMenuConn[config], (UINT_PTR) IDM_CONNECTMENU + config, MF_ENABLED);
EnableMenuItem(hMenuConn[config], (UINT_PTR) IDM_DISCONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR) IDM_STATUSMENU + config, MF_GRAYED);
}
else if (state == connecting || state == connected) {
EnableMenuItem(hMenuConn[config], (UINT_PTR) IDM_CONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR) IDM_DISCONNECTMENU + config, MF_ENABLED);
EnableMenuItem(hMenuConn[config], (UINT_PTR) IDM_STATUSMENU + config, MF_ENABLED);
}
else if (state == disconnecting) {
EnableMenuItem(hMenuConn[config], (UINT_PTR) IDM_CONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR) IDM_DISCONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR) IDM_STATUSMENU + config, MF_ENABLED);
}
}
}
void
SetServiceMenuStatus()
{
HMENU hMenuHandle;
if (o.allow_service[0] == '0' && o.service_only[0] == '0')
return;
if (o.service_only[0] == '1')
hMenuHandle = hMenu;
else
hMenuHandle = hMenuService;
if (o.service_state == service_noaccess
|| o.service_state == service_connecting) {
EnableMenuItem(hMenuHandle, IDM_SERVICE_START, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_STOP, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_RESTART, MF_GRAYED);
}
else if (o.service_state == service_connected) {
EnableMenuItem(hMenuHandle, IDM_SERVICE_START, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_STOP, MF_ENABLED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_RESTART, MF_ENABLED);
}
else {
EnableMenuItem(hMenuHandle, IDM_SERVICE_START, MF_ENABLED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_STOP, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_RESTART, MF_GRAYED);
}
}

60
tray.h
View File

@ -2,6 +2,7 @@
* OpenVPN-GUI -- A Windows GUI for OpenVPN. * OpenVPN-GUI -- A Windows GUI for OpenVPN.
* *
* Copyright (C) 2004 Mathias Sundman <mathias@nilings.se> * Copyright (C) 2004 Mathias Sundman <mathias@nilings.se>
* 2010 Heiko Hund <heikoh@users.sf.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -19,45 +20,36 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifndef TRAY_H
#define TRAY_H
#include "options.h" #include "options.h"
#define WM_NOTIFYICONTRAY (WM_APP + 1) #define WM_NOTIFYICONTRAY (WM_APP + 1)
//Popup Menu items #define IDM_SETTINGS 221
#define IDM_ABOUT 222
#define IDM_CLOSE 223
#define IDM_SETTINGS 221 #define IDM_CONNECTMENU 300
#define IDM_ABOUT 222 #define IDM_DISCONNECTMENU 400
#define IDM_STATUSMENU 500
#define IDM_VIEWLOGMENU 600
#define IDM_EDITMENU 700
#define IDM_PASSPHRASEMENU 800
#define IDM_CLOSE 223 #define IDM_SERVICEMENU 900
#define IDM_SERVICE_START 901
#define IDM_SERVICE_STOP 902
#define IDM_SERVICE_RESTART 903
#define IDM_TEXT_CONN1 "Office" void CreatePopupMenus();
#define IDM_CONN1 301 void OnNotifyTray(LPARAM);
#define IDM_TEXT_CONN2 "Home" void OnDestroyTray(void);
#define IDM_CONN2 302 void ShowTrayIcon();
void SetTrayIcon(conn_state_t);
#define IDM_CONNECTMENU 300 void SetMenuStatus(int, conn_state_t);
#define IDM_DISCONNECTMENU 400 void SetServiceMenuStatus();
#define IDM_STATUSMENU 500 void ShowTrayBalloon(TCHAR *, TCHAR *);
#define IDM_VIEWLOGMENU 600
#define IDM_EDITMENU 700
#define IDM_PASSPHRASEMENU 800
/* Service submenu */
#define IDM_SERVICEMENU 900
#define IDM_SERVICE_START 901
#define IDM_SERVICE_STOP 902
#define IDM_SERVICE_RESTART 903
void CreatePopupMenus(); //Create popup menus
void DestroyPopupMenus(); //Destroy popup menus
void OnNotifyTray(LPARAM lParam); //Tray message (mouse clicks on tray icon)
void OnDestroyTray(void); //WM_DESTROY message
void ShowTrayIcon(); //Put app icon in systray
void SetTrayIcon(conn_state_t connected); //Change systray icon
BOOL LoadAppIcon(); //Application icon
void CreateItemList(); //Crate Popup menu
void SetMenuStatus (int config, conn_state_t state); //Mark connection as connected/disconnected
void SetServiceMenuStatus(); //Diabled Service menu items.
void ShowTrayBalloon(TCHAR *infotitle_msg, TCHAR *info_msg);
#endif