Use file associations to open config and log

- Use ShellExecute to open config and log files so that
  file associations can be used. If that fails fall-back to
  the default editor (notepad.exe).
- Remove the keys editor and log_viewer from registry.
  The user can change the editor/viewer through fie association.

Signed-off-by: Selva Nair <selva.nair@gmail.com>
pull/62/head
Selva Nair 2016-07-06 16:01:36 -04:00
parent 1c748e382d
commit 964d728a42
3 changed files with 36 additions and 8 deletions

View File

@ -104,6 +104,7 @@ openvpn_gui_LDADD = \
-lwtsapi32 \ -lwtsapi32 \
-lcrypt32 \ -lcrypt32 \
-lnetapi32 \ -lnetapi32 \
-lole32 \
-lsecur32 -lsecur32
openvpn-gui-res.o: $(openvpn_gui_RESOURCES) $(srcdir)/openvpn-gui-res.h openvpn-gui-res.o: $(openvpn_gui_RESOURCES) $(srcdir)/openvpn-gui-res.h

View File

@ -128,13 +128,15 @@ GetRegistryKeys()
if (!GetRegKey(_T("log_append"), o.append_string, _T("0"), _countof(o.append_string))) return(false); if (!GetRegKey(_T("log_append"), o.append_string, _T("0"), _countof(o.append_string))) return(false);
_sntprintf_0(temp_path, _T("%s\\system32\\notepad.exe"), windows_dir); if (o.editor[0] != L'\0') /* set by cmd-line */
if (!GetRegKey(_T("log_viewer"), o.log_viewer, ExpandString (o.editor, _countof(o.editor));
temp_path, _countof(o.log_viewer))) return(false); else
_sntprintf_0(o.editor, _T("%s\\system32\\notepad.exe"), windows_dir);
_sntprintf_0(temp_path, _T("%s\\system32\\notepad.exe"), windows_dir); if (o.log_viewer[0] != L'\0') /* set by cmd-line */
if (!GetRegKey(_T("editor"), o.editor, ExpandString (o.log_viewer, _countof(o.log_viewer));
temp_path, _countof(o.editor))) return(false); else
_sntprintf_0(o.log_viewer, _T("%s\\system32\\notepad.exe"), windows_dir);
if (!GetRegKey(_T("service_only"), o.service_only, _T("0"), _countof(o.service_only))) return(false); if (!GetRegKey(_T("service_only"), o.service_only, _T("0"), _countof(o.service_only))) return(false);

View File

@ -25,6 +25,8 @@
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <stdio.h>
#include <shellapi.h>
#include <objbase.h>
#include "tray.h" #include "tray.h"
#include "openvpn.h" #include "openvpn.h"
@ -37,18 +39,29 @@ extern options_t o;
void ViewLog(int config) void ViewLog(int config)
{ {
TCHAR filename[200]; TCHAR filename[2*MAX_PATH];
STARTUPINFO start_info; STARTUPINFO start_info;
PROCESS_INFORMATION proc_info; PROCESS_INFORMATION proc_info;
SECURITY_ATTRIBUTES sa; SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd; SECURITY_DESCRIPTOR sd;
HINSTANCE status;
CLEAR (start_info); CLEAR (start_info);
CLEAR (proc_info); CLEAR (proc_info);
CLEAR (sa); CLEAR (sa);
CLEAR (sd); CLEAR (sd);
/* Try first using file association */
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); /* Safe to init COM multiple times */
status = ShellExecuteW (o.hWnd, L"open", o.conn[config].log_path, NULL, o.log_dir, SW_SHOWNORMAL);
if (status > (HINSTANCE) 32) /* Success */
return;
else
PrintDebug (L"Opening log file using ShellExecute with verb = open failed"
" 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);
/* fill in STARTUPINFO struct */ /* fill in STARTUPINFO struct */
@ -81,18 +94,30 @@ void ViewLog(int config)
void EditConfig(int config) void EditConfig(int config)
{ {
TCHAR filename[200]; TCHAR filename[2*MAX_PATH];
STARTUPINFO start_info; STARTUPINFO start_info;
PROCESS_INFORMATION proc_info; PROCESS_INFORMATION proc_info;
SECURITY_ATTRIBUTES sa; SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd; SECURITY_DESCRIPTOR sd;
HINSTANCE status;
CLEAR (start_info); CLEAR (start_info);
CLEAR (proc_info); CLEAR (proc_info);
CLEAR (sa); CLEAR (sa);
CLEAR (sd); CLEAR (sd);
/* Try first using file association */
_sntprintf_0(filename, L"%s\\%s", o.conn[config].config_dir, o.conn[config].config_file);
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); /* Safe to init COM multiple times */
status = ShellExecuteW (o.hWnd, L"open", filename, NULL, o.conn[config].config_dir, SW_SHOWNORMAL);
if (status > (HINSTANCE) 32)
return;
else
PrintDebug (L"Opening config file using ShellExecute with verb = open failed"
" for config '%s' (status = %lu)", o.conn[config].config_name, status);
_sntprintf_0(filename, _T("%s \"%s\\%s\""), o.editor, o.conn[config].config_dir, o.conn[config].config_file); _sntprintf_0(filename, _T("%s \"%s\\%s\""), o.editor, o.conn[config].config_dir, o.conn[config].config_file);
/* fill in STARTUPINFO struct */ /* fill in STARTUPINFO struct */