Functionality to import configuration file from the command line

pull/76/head
ValdikSS 2016-08-28 22:45:52 +03:00
parent c3099acc59
commit 9be38622e0
5 changed files with 45 additions and 27 deletions

63
main.c Normal file → Executable file
View File

@ -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;
}

2
main.h Normal file → Executable file
View File

@ -114,4 +114,6 @@ void PrintDebugMsg(TCHAR *msg);
DWORD GetDllVersion(LPCTSTR lpszDllName);
void ImportConfigFile(PTCHAR);
#endif

1
openvpn-gui-res.h Normal file → Executable file
View File

@ -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

5
options.c Normal file → Executable file
View File

@ -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;

1
res/openvpn-gui-res-en.rc Normal file → Executable file
View File

@ -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"""