From 46a1bc715cc18e9d782e503cdcba5dfddde780e8 Mon Sep 17 00:00:00 2001 From: Leonardo Basilio Date: Tue, 16 Feb 2016 22:20:53 -0200 Subject: [PATCH] Adding "Import file" feature --- main.c | 54 +++++++++++++++++++++++++++++++++++++++ openvpn-gui-res.h | 6 +++++ res/openvpn-gui-res-en.rc | 5 ++++ res/openvpn-gui-res-pt.rc | 5 ++++ tray.c | 2 ++ tray.h | 1 + 6 files changed, 73 insertions(+) diff --git a/main.c b/main.c index 20e81ab..5f3644d 100644 --- a/main.c +++ b/main.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "tray.h" #include "openvpn.h" @@ -53,6 +54,7 @@ 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"); @@ -349,6 +351,9 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM ShowChangePassphraseDialog(&o.conn[LOWORD(wParam) - IDM_PASSPHRASEMENU]); } #endif + if (LOWORD(wParam) == IDM_IMPORT) { + ImportConfigFile(); + } if (LOWORD(wParam) == IDM_SETTINGS) { ShowSettingsDialog(); } @@ -525,6 +530,55 @@ CloseApplication(HWND hwnd) DestroyWindow(hwnd); } +void +ImportConfigFile() +{ + + TCHAR filter[32]; + + memset(filter, 0, sizeof(filter)); + _sntprintf_0(filter, _T("*.%s *.%s"), o.ext_string, o.ext_string); + filter[2 + _tcslen(o.ext_string)] = _T('\0'); + + OPENFILENAME fn; + TCHAR source[MAX_PATH] = _T(""); + + 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)) + { + + TCHAR destination[MAX_PATH]; + PTCHAR fileName = source + fn.nFileOffset; + + _sntprintf_0(destination, _T("%s\\%s"), o.config_dir, fileName); + + if (!CopyFile(source, destination, TRUE)) + { + if (GetLastError() == ERROR_FILE_EXISTS) + { + fileName[_tcslen(fileName) - _tcslen(o.ext_string) - 1] = _T('\0'); + ShowLocalizedMsg(IDS_ERR_IMPORT_EXISTS, fileName); + } + else + ShowLocalizedMsg(IDS_ERR_IMPORT_FAILED); + } + + } + +} + #ifdef DEBUG void PrintDebugMsg(TCHAR *msg) { diff --git a/openvpn-gui-res.h b/openvpn-gui-res.h index 8377ce3..aee142e 100644 --- a/openvpn-gui-res.h +++ b/openvpn-gui-res.h @@ -107,6 +107,7 @@ #define IDS_MENU_SERVICEONLY_STOP 1020 #define IDS_MENU_SERVICEONLY_RESTART 1021 #define IDS_MENU_ASK_STOP_SERVICE 1022 +#define IDS_MENU_IMPORT 1023 /* LogViewer Dialog */ #define IDS_ERR_START_LOG_VIEWER 1101 @@ -241,4 +242,9 @@ #define IDS_ERR_WRITE_REGVALUE 1812 #define IDS_ERR_GET_PROFILE_DIR 1813 +/* Importation Related */ + +#define IDS_ERR_IMPORT_EXISTS 1901 +#define IDS_ERR_IMPORT_FAILED 1902 + #endif diff --git a/res/openvpn-gui-res-en.rc b/res/openvpn-gui-res-en.rc index 431f4bd..e6e4f7d 100644 --- a/res/openvpn-gui-res-en.rc +++ b/res/openvpn-gui-res-en.rc @@ -160,6 +160,7 @@ BEGIN IDS_TIP_CONNECTED_SINCE "\nConnected since: " IDS_TIP_ASSIGNED_IP "\nAssigned IP: %s" IDS_MENU_SERVICE "OpenVPN Service" + IDS_MENU_IMPORT "Import file…" IDS_MENU_SETTINGS "Settings…" IDS_MENU_CLOSE "Exit" IDS_MENU_CONNECT "Connect" @@ -339,4 +340,8 @@ BEGIN "once as Administrator to update the registry." IDS_ERR_READ_SET_KEY "Error reading and setting registry key ""%s""." IDS_ERR_WRITE_REGVALUE "Error writing registry value ""HKEY_CURRENT_USER\\%s\\%s""." + + /* importation */ + IDS_ERR_IMPORT_EXISTS "A config named ""%s"" already exists." + IDS_ERR_IMPORT_FAILED "Failed to import file." END diff --git a/res/openvpn-gui-res-pt.rc b/res/openvpn-gui-res-pt.rc index 5d1eb5d..6456346 100644 --- a/res/openvpn-gui-res-pt.rc +++ b/res/openvpn-gui-res-pt.rc @@ -147,6 +147,7 @@ BEGIN IDS_TIP_CONNECTED_SINCE "\nConectado desde: " IDS_TIP_ASSIGNED_IP "\nIP atribuído: %s" IDS_MENU_SERVICE "Serviço OpenVPN" + IDS_MENU_IMPORT "Importar arquivo…" IDS_MENU_SETTINGS "Configurações…" IDS_MENU_CLOSE "Sair" IDS_MENU_CONNECT "Conectar" @@ -324,4 +325,8 @@ BEGIN "uma vez como Administrador para alterar o registro." IDS_ERR_READ_SET_KEY "Erro ao ler e ajustar chave de registro ""%s""." IDS_ERR_WRITE_REGVALUE "Erro ao gravar valor da chave de registro ""HKEY_CURRENT_USER\\%s\\%s""." + + /* importation */ + IDS_ERR_IMPORT_EXISTS "Já existe uma configuração com o nome ""%s""." + IDS_ERR_IMPORT_FAILED "Falha ao importar o arquivo." END diff --git a/tray.c b/tray.c index 018b00e..150a106 100644 --- a/tray.c +++ b/tray.c @@ -91,6 +91,7 @@ CreatePopupMenus() AppendMenu(hMenu, MF_SEPARATOR, 0, 0); } + AppendMenu(hMenu, MF_STRING, IDM_IMPORT, LoadLocalizedString(IDS_MENU_IMPORT)); AppendMenu(hMenu, MF_STRING ,IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS)); AppendMenu(hMenu, MF_STRING ,IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE)); @@ -116,6 +117,7 @@ CreatePopupMenus() AppendMenu(hMenu, MF_SEPARATOR, 0, 0); } + AppendMenu(hMenu, MF_STRING, IDM_IMPORT, LoadLocalizedString(IDS_MENU_IMPORT)); AppendMenu(hMenu, MF_STRING, IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS)); AppendMenu(hMenu, MF_STRING, IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE)); diff --git a/tray.h b/tray.h index 9225eff..f417698 100644 --- a/tray.h +++ b/tray.h @@ -33,6 +33,7 @@ #define IDM_SETTINGS 221 #define IDM_CLOSE 223 +#define IDM_IMPORT 224 #define IDM_CONNECTMENU 300 #define IDM_DISCONNECTMENU (MAX_CONFIGS + IDM_CONNECTMENU)