mirror of https://github.com/OpenVPN/openvpn-gui
URL profile import: refactor ImportConfigFile
Factor out importing part (everything except file open dialog) into separate function, which can be used when importing profile from URL. Signed-off-by: Lev Stipakov <lev@openvpn.net>pull/446/head
parent
9ded7996ab
commit
78ee9b981d
51
main.c
51
main.c
|
@ -65,7 +65,7 @@
|
||||||
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
|
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
|
||||||
static void ShowSettingsDialog();
|
static void ShowSettingsDialog();
|
||||||
void CloseApplication(HWND hwnd);
|
void CloseApplication(HWND hwnd);
|
||||||
void ImportConfigFile();
|
void ImportConfigFileFromDisk();
|
||||||
void ImportConfigFromAS();
|
void ImportConfigFromAS();
|
||||||
|
|
||||||
/* Class name and window title */
|
/* Class name and window title */
|
||||||
|
@ -543,7 +543,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
|
||||||
|
|
||||||
/* we first check global menu items which do not require a connnection index */
|
/* we first check global menu items which do not require a connnection index */
|
||||||
if (LOWORD(wParam) == IDM_IMPORT_FILE) {
|
if (LOWORD(wParam) == IDM_IMPORT_FILE) {
|
||||||
ImportConfigFile();
|
ImportConfigFileFromDisk();
|
||||||
}
|
}
|
||||||
else if (LOWORD(wParam) == IDM_IMPORT_AS) {
|
else if (LOWORD(wParam) == IDM_IMPORT_AS) {
|
||||||
ImportConfigFromAS();
|
ImportConfigFromAS();
|
||||||
|
@ -756,9 +756,8 @@ CloseApplication(HWND hwnd)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ImportConfigFile()
|
ImportConfigFileFromDisk()
|
||||||
{
|
{
|
||||||
|
|
||||||
TCHAR filter[2*_countof(o.ext_string)+5];
|
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'));
|
_sntprintf_0(filter, _T("*.%s%c*.%s%c"), o.ext_string, _T('\0'), o.ext_string, _T('\0'));
|
||||||
|
@ -781,49 +780,7 @@ ImportConfigFile()
|
||||||
|
|
||||||
if (GetOpenFileName(&fn))
|
if (GetOpenFileName(&fn))
|
||||||
{
|
{
|
||||||
|
ImportConfigFile(source);
|
||||||
TCHAR destination[MAX_PATH];
|
|
||||||
PTCHAR fileName = source + fn.nFileOffset;
|
|
||||||
|
|
||||||
/* check if the source points to the config_dir */
|
|
||||||
if (wcsncmp(source, o.global_config_dir, wcslen(o.global_config_dir)) == 0
|
|
||||||
|| wcsncmp(source, o.config_dir, wcslen(o.config_dir)) == 0)
|
|
||||||
{
|
|
||||||
ShowLocalizedMsg(IDS_ERR_IMPORT_SOURCE, source);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_sntprintf_0(destination, _T("%s\\%s"), o.config_dir, fileName);
|
|
||||||
|
|
||||||
destination[_tcslen(destination) - _tcslen(o.ext_string) - 1] = _T('\0');
|
|
||||||
|
|
||||||
if (EnsureDirExists(destination))
|
|
||||||
{
|
|
||||||
|
|
||||||
_sntprintf_0(destination, _T("%s\\%s"), destination, fileName);
|
|
||||||
|
|
||||||
bool no_overwrite = TRUE;
|
|
||||||
while(!CopyFile(source, destination, no_overwrite))
|
|
||||||
{
|
|
||||||
if (GetLastError() != ERROR_FILE_EXISTS || !no_overwrite)
|
|
||||||
{
|
|
||||||
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Copy file <%s> to <%s> failed (error = %lu)",
|
|
||||||
source, destination, GetLastError());
|
|
||||||
ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* A file with same name exists. Ask the user whether to replace or not. */
|
|
||||||
if (ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_NFO_IMPORT_OVERWRITE, fileName) == IDNO)
|
|
||||||
return;
|
|
||||||
|
|
||||||
/* try again with overwrite allowed */
|
|
||||||
no_overwrite = FALSE;
|
|
||||||
}
|
|
||||||
ShowLocalizedMsg(IDS_NFO_IMPORT_SUCCESS);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
50
misc.c
50
misc.c
|
@ -31,11 +31,13 @@
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <shellapi.h>
|
#include <shellapi.h>
|
||||||
|
|
||||||
|
#include "localization.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "manage.h"
|
#include "manage.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
#include "openvpn-gui-res.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper function to do base64 conversion through CryptoAPI
|
* Helper function to do base64 conversion through CryptoAPI
|
||||||
|
@ -632,3 +634,51 @@ open_url(const wchar_t *url)
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern options_t o;
|
||||||
|
|
||||||
|
void
|
||||||
|
ImportConfigFile(const TCHAR* source)
|
||||||
|
{
|
||||||
|
TCHAR fileName[MAX_PATH] = _T("");
|
||||||
|
TCHAR ext[MAX_PATH] = _T("");
|
||||||
|
_wsplitpath(source, NULL, NULL, fileName, ext);
|
||||||
|
|
||||||
|
/* check if the source points to the config_dir */
|
||||||
|
if (wcsncmp(source, o.global_config_dir, wcslen(o.global_config_dir)) == 0
|
||||||
|
|| wcsncmp(source, o.config_dir, wcslen(o.config_dir)) == 0)
|
||||||
|
{
|
||||||
|
ShowLocalizedMsg(IDS_ERR_IMPORT_SOURCE, source);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
TCHAR destination[MAX_PATH];
|
||||||
|
_sntprintf_0(destination, _T("%s\\%s"), o.config_dir, fileName);
|
||||||
|
|
||||||
|
if (EnsureDirExists(destination))
|
||||||
|
{
|
||||||
|
_sntprintf_0(destination, _T("%s\\%s.%s"), destination, fileName, o.ext_string);
|
||||||
|
|
||||||
|
bool no_overwrite = TRUE;
|
||||||
|
while (!CopyFile(source, destination, no_overwrite))
|
||||||
|
{
|
||||||
|
if (GetLastError() != ERROR_FILE_EXISTS || !no_overwrite)
|
||||||
|
{
|
||||||
|
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Copy file <%s> to <%s> failed (error = %lu)",
|
||||||
|
source, destination, GetLastError());
|
||||||
|
ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* A file with same name exists. Ask the user whether to replace or not. */
|
||||||
|
if (ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_NFO_IMPORT_OVERWRITE, fileName) == IDNO)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* try again with overwrite allowed */
|
||||||
|
no_overwrite = FALSE;
|
||||||
|
}
|
||||||
|
ShowLocalizedMsg(IDS_NFO_IMPORT_SUCCESS);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED, destination);
|
||||||
|
}
|
||||||
|
|
2
misc.h
2
misc.h
|
@ -71,4 +71,6 @@ DWORD md_final(md_ctx *ctx, BYTE *md);
|
||||||
/* Open specified http/https URL using ShellExecute. */
|
/* Open specified http/https URL using ShellExecute. */
|
||||||
BOOL open_url(const wchar_t *url);
|
BOOL open_url(const wchar_t *url);
|
||||||
|
|
||||||
|
void ImportConfigFile(const TCHAR* path);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue