Import v2

pull/76/head
ValdikSS 2016-11-13 17:41:55 +03:00
parent 111db15756
commit 20c6ad51a7
7 changed files with 87 additions and 34 deletions

45
main.c
View File

@ -105,6 +105,7 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance,
MSG messages; /* Here messages to the application are saved */ MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */ WNDCLASSEX wincl; /* Data structure for the windowclass */
DWORD shell32_version; DWORD shell32_version;
BOOL already_running = 0;
/* Initialize handlers for manangement interface notifications */ /* Initialize handlers for manangement interface notifications */
mgmt_rtmsg_handler handler[] = { mgmt_rtmsg_handler handler[] = {
@ -159,22 +160,28 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance,
PrintDebug(_T("Shell32.dll version: 0x%lx"), shell32_version); PrintDebug(_T("Shell32.dll version: 0x%lx"), shell32_version);
#endif #endif
/* Parse command-line options */
ProcessCommandLine(&o, GetCommandLine());
/* Check if a previous instance is already running. */ /* Check if a previous instance is already running. */
if ((FindWindow (szClassName, NULL)) != NULL) if ((FindWindow (szClassName, NULL)) != NULL)
{ {
/* GUI already running */ /* GUI already running */
ShowLocalizedMsg(IDS_ERR_GUI_ALREADY_RUNNING); already_running = 1;
exit(1);
} }
else
{
UpdateRegistry(); /* Checks version change and update keys/values */ UpdateRegistry(); /* Checks version change and update keys/values */
}
GetRegistryKeys(); GetRegistryKeys();
EnsureDirExists(o.config_dir); EnsureDirExists(o.config_dir);
/* Parse command-line options */
ProcessCommandLine(&o, GetCommandLine());
if (already_running)
{
ShowLocalizedMsg(IDS_ERR_GUI_ALREADY_RUNNING);
exit(1);
}
if (!CheckVersion()) { if (!CheckVersion()) {
exit(1); exit(1);
@ -352,7 +359,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
ViewLog(LOWORD(wParam) - IDM_VIEWLOGMENU); ViewLog(LOWORD(wParam) - IDM_VIEWLOGMENU);
} }
if ( (LOWORD(wParam) >= IDM_EDITMENU) && (LOWORD(wParam) < IDM_EDITMENU + MAX_CONFIGS) ) { 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) ) { if ( (LOWORD(wParam) >= IDM_CLEARPASSMENU) && (LOWORD(wParam) < IDM_CLEARPASSMENU + MAX_CONFIGS) ) {
DisablePasswordSave(&o.conn[LOWORD(wParam) - IDM_CLEARPASSMENU]); DisablePasswordSave(&o.conn[LOWORD(wParam) - IDM_CLEARPASSMENU]);
@ -526,12 +533,27 @@ CloseApplication(HWND hwnd)
DestroyWindow(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 void
ImportConfigFile(PTCHAR config_file) ImportConfigFile(PTCHAR config_file)
{ {
TCHAR source[MAX_PATH] = _T(""); TCHAR source[MAX_PATH] = _T("");
PTCHAR fileName; PTCHAR fileName;
BOOL dlgresult; BOOL result = 1;
if (config_file == NULL) if (config_file == NULL)
{ {
@ -553,16 +575,15 @@ ImportConfigFile(PTCHAR config_file)
fn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST; fn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
fn.lpstrDefExt = NULL; fn.lpstrDefExt = NULL;
dlgresult = GetOpenFileName(&fn) && (fileName = source + fn.nFileOffset); result = GetOpenFileName(&fn) && (fileName = source + fn.nFileOffset);
} }
else else
{ {
fileName = PathFindFileName(config_file); fileName = PathFindFileName(config_file);
dlgresult = ShowLocalizedMsgEx(MB_YESNO, _T(PACKAGE_NAME), IDS_ASK_IMPORT_CONFIRM, fileName) == IDYES;
_tcscpy(source, config_file); _tcscpy(source, config_file);
} }
if (dlgresult) if (result)
{ {
TCHAR destination[MAX_PATH]; TCHAR destination[MAX_PATH];
@ -570,7 +591,7 @@ ImportConfigFile(PTCHAR config_file)
destination[_tcslen(destination) - _tcslen(o.ext_string) - 1] = _T('\0'); 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); _sntprintf_0(destination, _T("%s\\%s"), destination, fileName);

1
main.h
View File

@ -115,5 +115,6 @@ void PrintDebugMsg(TCHAR *msg);
DWORD GetDllVersion(LPCTSTR lpszDllName); DWORD GetDllVersion(LPCTSTR lpszDllName);
void ImportConfigFile(PTCHAR); void ImportConfigFile(PTCHAR);
void ShowConfigFileDialog(PTCHAR config_file);
#endif #endif

View File

@ -292,7 +292,7 @@
#define IDS_ERR_IMPORT_EXISTS 1901 #define IDS_ERR_IMPORT_EXISTS 1901
#define IDS_ERR_IMPORT_FAILED 1902 #define IDS_ERR_IMPORT_FAILED 1902
#define IDS_NFO_IMPORT_SUCCESS 1903 #define IDS_NFO_IMPORT_SUCCESS 1903
#define IDS_ASK_IMPORT_CONFIRM 1904 #define IDS_ASK_IMPORT_OR_EDIT 1904
/* Save password related messages */ /* Save password related messages */
#define IDS_NFO_DELETE_PASS 2001 #define IDS_NFO_DELETE_PASS 2001

View File

@ -41,6 +41,7 @@
#include "localization.h" #include "localization.h"
#include "misc.h" #include "misc.h"
#include "registry.h" #include "registry.h"
#include "viewlog.h"
#define streq(x, y) (_tcscmp((x), (y)) == 0) #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); ShowLocalizedMsgEx(MB_OK, caption, IDS_NFO_USAGE);
exit(0); 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]) else if (streq(p[0], _T("connect")) && p[1])
{ {
++i; ++i;
@ -149,6 +145,16 @@ add_option(options_t *options, int i, TCHAR **p)
{ {
++i; ++i;
PrintDebug (L"Deprecated option: '%s' ignored.", p[0]); 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]) else if (streq(p[0], _T("allow_service")) && p[1])
{ {

View File

@ -429,7 +429,8 @@ BEGIN
IDS_ERR_IMPORT_FAILED "Failed to import file. The following path could not be created.\n\n" \ 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." "%s\n\nMake sure you have the right permissions."
IDS_NFO_IMPORT_SUCCESS "File imported successfully." 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 */ /* save/delete password */
IDS_NFO_DELETE_PASS "Press OK to delete saved passwords for config ""%s""" IDS_NFO_DELETE_PASS "Press OK to delete saved passwords for config ""%s"""

View File

@ -57,6 +57,12 @@ void ViewLog(int config)
/* Try first using file association */ /* Try first using file association */
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); /* Safe to init COM multiple times */ CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); /* Safe to init COM multiple times */
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
{
if (AssocQueryString(0, ASSOCSTR_EXECUTABLE, L".txt", NULL, assocexe, &assocexe_num) == S_OK) 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); status = ShellExecuteW (o.hWnd, L"open", assocexe, o.conn[config].log_path, o.log_dir, SW_SHOWNORMAL);
if (status > (HINSTANCE) 32) /* Success */ if (status > (HINSTANCE) 32) /* Success */
@ -64,6 +70,7 @@ void ViewLog(int config)
else else
PrintDebug (L"Opening log file using ShellExecute with verb = open failed" PrintDebug (L"Opening log file using ShellExecute with verb = open failed"
" for config '%s' (status = %lu)", o.conn[config].config_name, status); " 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); _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); CloseHandle(proc_info.hProcess);
} }
/* either config or config_path */
void EditConfig(int config) 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]; TCHAR assocexe[2*MAX_PATH];
DWORD assocexe_num; DWORD assocexe_num;
@ -112,19 +121,34 @@ void EditConfig(int config)
CLEAR (sa); CLEAR (sa);
CLEAR (sd); CLEAR (sd);
/* Try first using file association */ if (config_path == NULL)
_sntprintf_0(filename, L"%s\\%s", o.conn[config].config_dir, o.conn[config].config_file); {
// 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 */ 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".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) if (status > (HINSTANCE) 32)
return; return;
else else
PrintDebug (L"Opening config file using ShellExecute with verb = open failed" 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 */ /* fill in STARTUPINFO struct */
GetStartupInfo(&start_info); GetStartupInfo(&start_info);
@ -135,13 +159,13 @@ void EditConfig(int config)
start_info.hStdOutput = NULL; start_info.hStdOutput = NULL;
if (!CreateProcess(NULL, if (!CreateProcess(NULL,
filename, //commandline fullpath, //commandline
NULL, NULL,
NULL, NULL,
TRUE, TRUE,
CREATE_NEW_CONSOLE, CREATE_NEW_CONSOLE,
NULL, NULL,
o.conn[config].config_dir, //start-up dir config_dir, //start-up dir
&start_info, &start_info,
&proc_info)) &proc_info))
{ {

View File

@ -20,4 +20,4 @@
*/ */
void ViewLog(int config); void ViewLog(int config);
void EditConfig(int config); void EditConfig(int config, PTCHAR config_path);