From d7488790ab1621e9e01d4c28bf1fa2ef5ceffc46 Mon Sep 17 00:00:00 2001 From: cancel Date: Tue, 14 Jan 2020 15:00:11 +0900 Subject: [PATCH] Move some prefs/conf save stuff out of tui_main to sysmisc --- sysmisc.c | 35 +++++++++++++++++++++++++++++ sysmisc.h | 19 ++++++++++++++++ tui_main.c | 66 ++++-------------------------------------------------- 3 files changed, 58 insertions(+), 62 deletions(-) diff --git a/sysmisc.c b/sysmisc.c index 1c3a8b0..28c7e86 100644 --- a/sysmisc.c +++ b/sysmisc.c @@ -307,3 +307,38 @@ cleanup: conf_save_cancel(p); return err; } + +char const *prefs_save_error_string(Prefs_save_error error) { + switch (error) { + case Prefs_save_ok: + return "No error"; + case Prefs_save_oom: + return "Out of memory"; + case Prefs_save_no_home: + return "Unable to resolve $XDG_CONFIG_HOME or $HOME"; + case Prefs_save_mkdir_failed: + return "Unable to create $XDG_CONFIG_HOME or $HOME/.config directory"; + case Prefs_save_conf_dir_not_dir: + return "Config directory path is not a directory"; + case Prefs_save_old_temp_file_stuck: + return "Unable to remove old orca.conf.tmp file"; + case Prefs_save_temp_file_perm_denied: + return "Permission denied for config directory"; + case Prefs_save_temp_open_failed: + return "Unable to open orca.conf.tmp for writing"; + case Prefs_save_temp_fsync_failed: + return "fsync() reported an when writing temp file.\n" + "Refusing to continue."; + case Prefs_save_temp_close_failed: + return "Unable to close temp file"; + case Prefs_save_rename_failed: + return "Unable to rename orca.conf.tmp to orca.conf"; + case Prefs_save_line_too_long: + return "Line in file is too long"; + case Prefs_save_existing_read_error: + return "Error when reading existing configuration file"; + case Prefs_save_unknown_error: + break; + } + return "Unknown"; +} diff --git a/sysmisc.h b/sysmisc.h index 78ecdb7..d18462f 100644 --- a/sysmisc.h +++ b/sysmisc.h @@ -73,3 +73,22 @@ void conf_save_cancel(Conf_save *p); Conf_save_commit_error conf_save_commit(Conf_save *p); // Finishes. Do not call this with a zeroed `*p`. Afterwards, `*p` will be // zeroed. + +typedef enum { + Prefs_save_ok = 0, + Prefs_save_oom, + Prefs_save_no_home, + Prefs_save_mkdir_failed, + Prefs_save_conf_dir_not_dir, + Prefs_save_old_temp_file_stuck, + Prefs_save_temp_file_perm_denied, + Prefs_save_temp_open_failed, + Prefs_save_temp_fsync_failed, + Prefs_save_temp_close_failed, + Prefs_save_rename_failed, + Prefs_save_line_too_long, + Prefs_save_existing_read_error, + Prefs_save_unknown_error, +} Prefs_save_error; + +char const* prefs_save_error_string(Prefs_save_error error); diff --git a/tui_main.c b/tui_main.c index 30918a1..8724715 100644 --- a/tui_main.c +++ b/tui_main.c @@ -2417,23 +2417,6 @@ static void put_conf_pair(FILE *file, char const *left, char const *right) { fputs("\n", file); } -typedef enum { - Prefs_save_ok = 0, - Prefs_save_oom, - Prefs_save_no_home, - Prefs_save_mkdir_failed, - Prefs_save_conf_dir_not_dir, - Prefs_save_old_temp_file_stuck, - Prefs_save_temp_file_perm_denied, - Prefs_save_temp_open_failed, - Prefs_save_temp_fsync_failed, - Prefs_save_temp_close_failed, - Prefs_save_rename_failed, - Prefs_save_line_too_long, - Prefs_save_existing_read_error, - Prefs_save_unknown_error, -} Prefs_save_error; - Prefs_save_error save_prefs_to_disk(Midi_mode const *midi_mode, int softmargin_y, int softmargin_x, bool softmargins_touched_by_user) { @@ -2578,52 +2561,11 @@ void save_prefs_with_error_message(Midi_mode const *midi_mode, int softmargin_y, bool softmargins_touched_by_user) { Prefs_save_error err = save_prefs_to_disk( midi_mode, softmargin_y, softmargin_x, softmargins_touched_by_user); - char const *msg = "Unknown"; - switch (err) { - case Prefs_save_ok: - return; - case Prefs_save_oom: - msg = "Out of memory"; - break; - case Prefs_save_no_home: - msg = "Unable to resolve $XDG_CONFIG_HOME or $HOME"; - break; - case Prefs_save_mkdir_failed: - msg = "Unable to create $XDG_CONFIG_HOME or $HOME/.config directory"; - break; - case Prefs_save_conf_dir_not_dir: - msg = "Config directory path is not a directory"; - break; - case Prefs_save_old_temp_file_stuck: - msg = "Unable to remove old orca.conf.tmp file"; - break; - case Prefs_save_temp_file_perm_denied: - msg = "Permission denied for config directory"; - break; - case Prefs_save_temp_open_failed: - msg = "Unable to open orca.conf.tmp for writing"; - break; - case Prefs_save_temp_fsync_failed: - msg = "fsync() reported an when writing temp file.\n" - "Refusing to continue."; - break; - case Prefs_save_temp_close_failed: - msg = "Unable to close temp file"; - break; - case Prefs_save_rename_failed: - msg = "Unable to rename orca.conf.tmp to orca.conf"; - break; - case Prefs_save_line_too_long: - msg = "Line in file is too long"; - break; - case Prefs_save_existing_read_error: - msg = "Error when reading existing configuration file"; - break; - case Prefs_save_unknown_error: - break; + if (err) { + char const *msg = prefs_save_error_string(err); + qmsg_printf_push("Config Error", + "Error when writing configuration file:\n%s", msg); } - qmsg_printf_push("Config Error", "Error when writing configuration file:\n%s", - msg); } void print_loading_message(char const *s) {