From 9be38622e0074993b93ed11d532da57e1c097620 Mon Sep 17 00:00:00 2001 From: ValdikSS Date: Sun, 28 Aug 2016 22:45:52 +0300 Subject: [PATCH] Functionality to import configuration file from the command line --- main.c | 63 ++++++++++++++++++++++----------------- main.h | 2 ++ openvpn-gui-res.h | 1 + options.c | 5 ++++ res/openvpn-gui-res-en.rc | 1 + 5 files changed, 45 insertions(+), 27 deletions(-) mode change 100644 => 100755 main.c mode change 100644 => 100755 main.h mode change 100644 => 100755 openvpn-gui-res.h mode change 100644 => 100755 options.c mode change 100644 => 100755 res/openvpn-gui-res-en.rc diff --git a/main.c b/main.c old mode 100644 new mode 100755 index f94ba9e..9030a05 --- a/main.c +++ b/main.c @@ -59,7 +59,6 @@ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); static void ShowSettingsDialog(); void CloseApplication(HWND hwnd); -void ImportConfigFile(); /* Class name and window title */ TCHAR szClassName[ ] = _T("OpenVPN-GUI"); @@ -160,6 +159,8 @@ 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) @@ -172,8 +173,6 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance, UpdateRegistry(); /* Checks version change and update keys/values */ GetRegistryKeys(); - /* Parse command-line options */ - ProcessCommandLine(&o, GetCommandLine()); EnsureDirExists(o.config_dir); @@ -364,7 +363,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM } #endif if (LOWORD(wParam) == IDM_IMPORT) { - ImportConfigFile(); + ImportConfigFile(NULL); } if (LOWORD(wParam) == IDM_SETTINGS) { ShowSettingsDialog(); @@ -528,34 +527,44 @@ CloseApplication(HWND hwnd) } void -ImportConfigFile() +ImportConfigFile(PTCHAR config_file) { - - TCHAR filter[2*_countof(o.ext_string)+5]; - - _sntprintf_0(filter, _T("*.%s%c*.%s%c"), o.ext_string, _T('\0'), o.ext_string, _T('\0')); - - OPENFILENAME fn; TCHAR source[MAX_PATH] = _T(""); + PTCHAR fileName; + BOOL dlgresult; - fn.lStructSize = sizeof(OPENFILENAME); - fn.hwndOwner = NULL; - fn.lpstrFilter = filter; - fn.lpstrCustomFilter = NULL; - fn.nFilterIndex = 1; - fn.lpstrFile = source; - fn.nMaxFile = MAX_PATH; - fn.lpstrFileTitle = NULL; - fn.lpstrInitialDir = NULL; - fn.lpstrTitle = NULL; - fn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST; - fn.lpstrDefExt = NULL; - - if (GetOpenFileName(&fn)) + if (config_file == NULL) { + OPENFILENAME fn; + TCHAR filter[2*_countof(o.ext_string)+5]; + _sntprintf_0(filter, _T("*.%s%c*.%s%c"), o.ext_string, _T('\0'), o.ext_string, _T('\0')); + + fn.lStructSize = sizeof(OPENFILENAME); + fn.hwndOwner = NULL; + fn.lpstrFilter = filter; + fn.lpstrCustomFilter = NULL; + fn.nFilterIndex = 1; + fn.lpstrFile = source; + fn.nMaxFile = MAX_PATH; + fn.lpstrFileTitle = NULL; + fn.lpstrInitialDir = NULL; + fn.lpstrTitle = NULL; + fn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST; + fn.lpstrDefExt = NULL; + + dlgresult = 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) + { TCHAR destination[MAX_PATH]; - PTCHAR fileName = source + fn.nFileOffset; _sntprintf_0(destination, _T("%s\\%s"), o.config_dir, fileName); @@ -570,7 +579,7 @@ ImportConfigFile() { if (GetLastError() == ERROR_FILE_EXISTS) { - fileName[_tcslen(fileName) - _tcslen(o.ext_string) - 1] = _T('\0'); + PathRemoveExtension(fileName); ShowLocalizedMsg(IDS_ERR_IMPORT_EXISTS, fileName); return; } diff --git a/main.h b/main.h old mode 100644 new mode 100755 index c85b983..d3fcadc --- a/main.h +++ b/main.h @@ -114,4 +114,6 @@ void PrintDebugMsg(TCHAR *msg); DWORD GetDllVersion(LPCTSTR lpszDllName); +void ImportConfigFile(PTCHAR); + #endif diff --git a/openvpn-gui-res.h b/openvpn-gui-res.h old mode 100644 new mode 100755 index 5e38139..8a3227e --- a/openvpn-gui-res.h +++ b/openvpn-gui-res.h @@ -292,6 +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 /* Save password related messages */ #define IDS_NFO_DELETE_PASS 2001 diff --git a/options.c b/options.c old mode 100644 new mode 100755 index 454f3a5..b30b1f1 --- a/options.c +++ b/options.c @@ -87,6 +87,11 @@ 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; diff --git a/res/openvpn-gui-res-en.rc b/res/openvpn-gui-res-en.rc old mode 100644 new mode 100755 index e5596da..c7c7e0d --- a/res/openvpn-gui-res-en.rc +++ b/res/openvpn-gui-res-en.rc @@ -429,6 +429,7 @@ 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""?" /* save/delete password */ IDS_NFO_DELETE_PASS "Press OK to delete saved passwords for config ""%s"""