diff --git a/main.c b/main.c index 9030a05..5a02a46 100755 --- a/main.c +++ b/main.c @@ -105,6 +105,7 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance, MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ DWORD shell32_version; + BOOL already_running = 0; /* Initialize handlers for manangement interface notifications */ mgmt_rtmsg_handler handler[] = { @@ -159,22 +160,28 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance, PrintDebug(_T("Shell32.dll version: 0x%lx"), shell32_version); #endif - /* Parse command-line options */ - ProcessCommandLine(&o, GetCommandLine()); - /* Check if a previous instance is already running. */ if ((FindWindow (szClassName, NULL)) != NULL) { /* GUI already running */ - ShowLocalizedMsg(IDS_ERR_GUI_ALREADY_RUNNING); - exit(1); + already_running = 1; + } + else + { + UpdateRegistry(); /* Checks version change and update keys/values */ } - - UpdateRegistry(); /* Checks version change and update keys/values */ GetRegistryKeys(); EnsureDirExists(o.config_dir); + /* Parse command-line options */ + ProcessCommandLine(&o, GetCommandLine()); + + if (already_running) + { + ShowLocalizedMsg(IDS_ERR_GUI_ALREADY_RUNNING); + exit(1); + } if (!CheckVersion()) { exit(1); @@ -352,7 +359,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM ViewLog(LOWORD(wParam) - IDM_VIEWLOGMENU); } if ( (LOWORD(wParam) >= IDM_EDITMENU) && (LOWORD(wParam) < IDM_EDITMENU + MAX_CONFIGS) ) { - EditConfig(LOWORD(wParam) - IDM_EDITMENU); + EditConfig(LOWORD(wParam) - IDM_EDITMENU, NULL); } if ( (LOWORD(wParam) >= IDM_CLEARPASSMENU) && (LOWORD(wParam) < IDM_CLEARPASSMENU + MAX_CONFIGS) ) { DisablePasswordSave(&o.conn[LOWORD(wParam) - IDM_CLEARPASSMENU]); @@ -526,12 +533,27 @@ CloseApplication(HWND hwnd) DestroyWindow(hwnd); } +void +ShowConfigFileDialog(PTCHAR config_file) +{ + PTCHAR fileName; + int dlgresult = 0; + + fileName = PathFindFileName(config_file); + dlgresult = ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_ASK_IMPORT_OR_EDIT, fileName); + if (dlgresult == IDYES) + ImportConfigFile(config_file); + else + EditConfig(0, config_file); +} + + void ImportConfigFile(PTCHAR config_file) { TCHAR source[MAX_PATH] = _T(""); PTCHAR fileName; - BOOL dlgresult; + BOOL result = 1; if (config_file == NULL) { @@ -553,16 +575,15 @@ ImportConfigFile(PTCHAR config_file) fn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST; fn.lpstrDefExt = NULL; - dlgresult = GetOpenFileName(&fn) && (fileName = source + fn.nFileOffset); + result = GetOpenFileName(&fn) && (fileName = source + fn.nFileOffset); } else { fileName = PathFindFileName(config_file); - dlgresult = ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_ASK_IMPORT_CONFIRM, fileName) == IDYES; _tcscpy(source, config_file); } - if (dlgresult) + if (result) { TCHAR destination[MAX_PATH]; @@ -570,7 +591,7 @@ ImportConfigFile(PTCHAR config_file) destination[_tcslen(destination) - _tcslen(o.ext_string) - 1] = _T('\0'); - if (EnsureDirExists(destination)) + if (EnsureDirExists(destination) && EnsureDirExists(o.config_dir)) { _sntprintf_0(destination, _T("%s\\%s"), destination, fileName); diff --git a/main.h b/main.h index d3fcadc..5da80cf 100755 --- a/main.h +++ b/main.h @@ -115,5 +115,6 @@ void PrintDebugMsg(TCHAR *msg); DWORD GetDllVersion(LPCTSTR lpszDllName); void ImportConfigFile(PTCHAR); +void ShowConfigFileDialog(PTCHAR config_file); #endif diff --git a/openvpn-gui-res.h b/openvpn-gui-res.h index 8a3227e..c0f6c18 100755 --- a/openvpn-gui-res.h +++ b/openvpn-gui-res.h @@ -292,7 +292,7 @@ #define IDS_ERR_IMPORT_EXISTS 1901 #define IDS_ERR_IMPORT_FAILED 1902 #define IDS_NFO_IMPORT_SUCCESS 1903 -#define IDS_ASK_IMPORT_CONFIRM 1904 +#define IDS_ASK_IMPORT_OR_EDIT 1904 /* Save password related messages */ #define IDS_NFO_DELETE_PASS 2001 diff --git a/options.c b/options.c index b30b1f1..f661aba 100755 --- a/options.c +++ b/options.c @@ -41,6 +41,7 @@ #include "localization.h" #include "misc.h" #include "registry.h" +#include "viewlog.h" #define streq(x, y) (_tcscmp((x), (y)) == 0) @@ -87,11 +88,6 @@ add_option(options_t *options, int i, TCHAR **p) ShowLocalizedMsgEx(MB_OK, caption, IDS_NFO_USAGE); exit(0); } - else if (streq(p[0], _T("import")) && p[1]) - { - ImportConfigFile(p[1]); - exit(0); - } else if (streq(p[0], _T("connect")) && p[1]) { ++i; @@ -149,6 +145,16 @@ add_option(options_t *options, int i, TCHAR **p) { ++i; PrintDebug (L"Deprecated option: '%s' ignored.", p[0]); + } + else if (streq(p[0], _T("edit")) && p[1]) + { + EditConfig(0, p[1]); + exit(0); + } + else if (streq(p[0], _T("configfile_dialog")) && p[1]) + { + ShowConfigFileDialog(p[1]); + exit(0); } else if (streq(p[0], _T("allow_service")) && p[1]) { diff --git a/res/openvpn-gui-res-en.rc b/res/openvpn-gui-res-en.rc index c7c7e0d..000c292 100755 --- a/res/openvpn-gui-res-en.rc +++ b/res/openvpn-gui-res-en.rc @@ -429,7 +429,8 @@ BEGIN IDS_ERR_IMPORT_FAILED "Failed to import file. The following path could not be created.\n\n" \ "%s\n\nMake sure you have the right permissions." IDS_NFO_IMPORT_SUCCESS "File imported successfully." - IDS_ASK_IMPORT_CONFIRM "Do you want to import file ""%s""?" + IDS_ASK_IMPORT_OR_EDIT "Do you want to import ""%s""?\n" \ + "Choose ""yes"" to import or ""no"" to edit config file." /* save/delete password */ IDS_NFO_DELETE_PASS "Press OK to delete saved passwords for config ""%s""" diff --git a/viewlog.c b/viewlog.c index e40606b..9aefd71 100755 --- a/viewlog.c +++ b/viewlog.c @@ -57,13 +57,20 @@ void ViewLog(int config) /* Try first using file association */ CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); /* Safe to init COM multiple times */ - if (AssocQueryString(0, ASSOCSTR_EXECUTABLE, L".txt", NULL, assocexe, &assocexe_num) == S_OK) + if (AssocQueryString(0, ASSOCSTR_EXECUTABLE, L".log", NULL, assocexe, &assocexe_num) == S_OK) status = ShellExecuteW (o.hWnd, L"open", assocexe, o.conn[config].log_path, o.log_dir, SW_SHOWNORMAL); if (status > (HINSTANCE) 32) /* Success */ return; else - PrintDebug (L"Opening log file using ShellExecute with verb = open failed" - " for config '%s' (status = %lu)", o.conn[config].config_name, status); + { + if (AssocQueryString(0, ASSOCSTR_EXECUTABLE, L".txt", NULL, assocexe, &assocexe_num) == S_OK) + status = ShellExecuteW (o.hWnd, L"open", assocexe, o.conn[config].log_path, o.log_dir, SW_SHOWNORMAL); + if (status > (HINSTANCE) 32) /* Success */ + return; + else + PrintDebug (L"Opening log file using ShellExecute with verb = open failed" + " for config '%s' (status = %lu)", o.conn[config].config_name, status); + } _sntprintf_0(filename, _T("%s \"%s\""), o.log_viewer, o.conn[config].log_path); @@ -94,10 +101,12 @@ void ViewLog(int config) CloseHandle(proc_info.hProcess); } - -void EditConfig(int config) +/* either config or config_path */ +void EditConfig(int config, PTCHAR config_path) { - TCHAR filename[2*MAX_PATH]; + TCHAR config_file[2*MAX_PATH]; + TCHAR fullpath[2*MAX_PATH]; + TCHAR config_dir[2*MAX_PATH]; TCHAR assocexe[2*MAX_PATH]; DWORD assocexe_num; @@ -112,19 +121,34 @@ void EditConfig(int config) CLEAR (sa); CLEAR (sd); - /* Try first using file association */ - _sntprintf_0(filename, L"%s\\%s", o.conn[config].config_dir, o.conn[config].config_file); + if (config_path == NULL) + { + // using "config" + _sntprintf_0(fullpath, L"%s\\%s", o.conn[config].config_dir, o.conn[config].config_file); + _tcsncpy(config_dir, o.conn[config].config_dir, _countof(config_dir) - 1); + _tcsncpy(config_file, o.conn[config].config_file, _countof(config_file) - 1); + } + else + { + // using "config_path" + _tcsncpy(fullpath, config_path, _countof(config_dir) - 1); + _tcsncpy(config_dir, config_path, _countof(config_dir) - 1); + PathRemoveFileSpec(config_dir); + _tcsncpy(config_file, config_path, _countof(config_file) - 1); + *config_file = PathFindFileName(config_file); + } + /* Try first using file association */ CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); /* Safe to init COM multiple times */ if (AssocQueryString(0, ASSOCSTR_EXECUTABLE, L".txt", NULL, assocexe, &assocexe_num) == S_OK) - status = ShellExecuteW (o.hWnd, L"open", assocexe, filename, o.conn[config].config_dir, SW_SHOWNORMAL); + status = ShellExecuteW (o.hWnd, L"open", assocexe, fullpath, config_dir, SW_SHOWNORMAL); if (status > (HINSTANCE) 32) return; else PrintDebug (L"Opening config file using ShellExecute with verb = open failed" - " for config '%s' (status = %lu)", o.conn[config].config_name, status); + " for config '%s' (status = %lu)", config_file, status); - _sntprintf_0(filename, _T("%s \"%s\\%s\""), o.editor, o.conn[config].config_dir, o.conn[config].config_file); + _sntprintf_0(fullpath, _T("%s \"%s\""), o.editor, fullpath); /* fill in STARTUPINFO struct */ GetStartupInfo(&start_info); @@ -135,13 +159,13 @@ void EditConfig(int config) start_info.hStdOutput = NULL; if (!CreateProcess(NULL, - filename, //commandline + fullpath, //commandline NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, - o.conn[config].config_dir, //start-up dir + config_dir, //start-up dir &start_info, &proc_info)) { diff --git a/viewlog.h b/viewlog.h index 2faa53f..b1bcd94 100644 --- a/viewlog.h +++ b/viewlog.h @@ -20,4 +20,4 @@ */ void ViewLog(int config); -void EditConfig(int config); +void EditConfig(int config, PTCHAR config_path);