From 2768d5c7d600e57b424d6e5130634bd23f12d0d6 Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 13 Jan 2014 21:35:32 +0100 Subject: [PATCH] Edit settings dialog should be able to load the active settings, the settings from a file or the default settings --- src/main.c | 133 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 97 insertions(+), 36 deletions(-) diff --git a/src/main.c b/src/main.c index c8dff6ef..318ea311 100644 --- a/src/main.c +++ b/src/main.c @@ -218,26 +218,13 @@ static void set_option(char **options, const char *name, const char *value) } } -static void process_command_line_arguments(char *argv[], char **options) + +static void read_config_file(const char *config_file, char **options) { char line[MAX_CONF_FILE_LINE_SIZE], opt[sizeof(line)], val[sizeof(line)], *p; FILE *fp = NULL; size_t i, cmd_line_opts_start = 1, line_no = 0; - /* Should we use a config file ? */ - if (argv[1] != NULL && argv[1][0] != '-') { - snprintf(config_file, sizeof(config_file), "%s", argv[1]); - cmd_line_opts_start = 2; - } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) { - /* No command line flags specified. Look where binary lives */ - snprintf(config_file, sizeof(config_file)-1, "%s", CONFIG_FILE); - config_file[sizeof(config_file)-1] = 0; - } else { - snprintf(config_file, sizeof(config_file)-1, "%.*s%c%s", - (int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE); - config_file[sizeof(config_file)-1] = 0; - } - fp = fopen(config_file, "r"); /* If config file was set in command line and open failed, die */ @@ -245,16 +232,6 @@ static void process_command_line_arguments(char *argv[], char **options) die("Cannot open config file %s: %s", config_file, strerror(errno)); } -#ifdef CONFIG_FILE2 - /* try alternate config file */ - if (fp == NULL) { - fp = fopen(CONFIG_FILE2, "r"); - if (fp != NULL) { - strcpy(config_file, CONFIG_FILE2); - } - } -#endif - /* Load config file settings first */ if (fp != NULL) { fprintf(stderr, "Loading config file %s\n", config_file); @@ -286,6 +263,46 @@ static void process_command_line_arguments(char *argv[], char **options) (void) fclose(fp); } +} + + +static void process_command_line_arguments(char *argv[], char **options) +{ + char *p; + FILE *fp = NULL; + size_t i, cmd_line_opts_start = 1, line_no = 0; + + /* Should we use a config file ? */ + if (argv[1] != NULL && argv[1][0] != '-') { + snprintf(config_file, sizeof(config_file), "%s", argv[1]); + cmd_line_opts_start = 2; + } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) { + /* No command line flags specified. Look where binary lives */ + snprintf(config_file, sizeof(config_file)-1, "%s", CONFIG_FILE); + config_file[sizeof(config_file)-1] = 0; + } else { + snprintf(config_file, sizeof(config_file)-1, "%.*s%c%s", + (int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE); + config_file[sizeof(config_file)-1] = 0; + } + +#ifdef CONFIG_FILE2 + fp = fopen(config_file, "r"); + + /* try alternate config file */ + if (fp == NULL) { + fp = fopen(CONFIG_FILE2, "r"); + if (fp != NULL) { + strcpy(config_file, CONFIG_FILE2); + } + } + if (fp != NULL) { + fclose(fp); + } +#endif + + /* read all configurations from a config file */ + (void)read_config_file(config_file, options); /* If we're under MacOS and started by launchd, then the second argument is process serial number, -psn_..... @@ -458,7 +475,8 @@ static void start_civetweb(int argc, char *argv[]) #ifdef _WIN32 enum { ID_ICON = 100, ID_QUIT, ID_SETTINGS, ID_SEPARATOR, ID_INSTALL_SERVICE, - ID_REMOVE_SERVICE, ID_STATIC, ID_GROUP, ID_SAVE, ID_RESET_DEFAULTS, + ID_REMOVE_SERVICE, ID_STATIC, ID_GROUP, + ID_SAVE, ID_RESET_DEFAULTS, ID_RESET_FILE, ID_RESET_ACTIVE, ID_STATUS, ID_CONNECT, /* All dynamically created text boxes for options have IDs starting from @@ -578,8 +596,10 @@ static void save_config(HWND hDlg, FILE *fp) static BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP) { FILE *fp; - int i; - const char *name, *value, **options = mg_get_valid_option_names(); + int i, j; + const char *name, *value; + const char **default_options = mg_get_valid_option_names(); + char *file_options[MAX_OPTIONS] = {0}; switch (msg) { case WM_CLOSE: @@ -599,9 +619,44 @@ static BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP) EnableWindow(GetDlgItem(hDlg, ID_SAVE), TRUE); break; case ID_RESET_DEFAULTS: - for (i = 0; options[i * 2] != NULL; i++) { - name = options[i * 2]; - value = options[i * 2 + 1] == NULL ? "" : options[i * 2 + 1]; + for (i = 0; default_options[i * 2] != NULL; i++) { + name = default_options[i * 2]; + value = default_options[i * 2 + 1] == NULL ? "" : default_options[i * 2 + 1]; + if (is_boolean_option(name)) { + CheckDlgButton(hDlg, ID_CONTROLS + i, !strcmp(value, "yes") ? + BST_CHECKED : BST_UNCHECKED); + } else { + SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), value); + } + } + break; + case ID_RESET_FILE: + read_config_file(config_file, file_options); + for (i = 0; default_options[i * 2] != NULL; i++) { + name = default_options[i * 2]; + value = default_options[i * 2 + 1]; + for (j = 0; file_options[j * 2] != NULL; j++) { + if (!strcmp(name, file_options[j * 2])) { + value = file_options[j * 2 + 1]; + } + } + if (value == NULL) { + value = ""; + } + if (is_boolean_option(name)) { + CheckDlgButton(hDlg, ID_CONTROLS + i, !strcmp(value, "yes") ? BST_CHECKED : BST_UNCHECKED); + } else { + SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), value); + } + } + for (i = 0; icy = ((nelems + 1) / 2 + 1) * HEIGHT + 30; DialogBoxIndirectParam(NULL, dia, NULL, DlgProc, (LPARAM) NULL);