From d9d05e83ba9975959607302c45a631ee80a2d21e Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Mon, 13 May 2013 01:58:08 +0100 Subject: Restructured a fair bit of cfgfiles.c. Besides making the code cleaner this allows for better error handling (in fact the error message of check_prefs_dir () whould make hexchat abort). --- src/common/cfgfiles.c | 132 +++++++++++++++++++++++++++++++------------------- src/common/cfgfiles.h | 6 ++- src/common/hexchat.c | 22 ++++++--- 3 files changed, 104 insertions(+), 56 deletions(-) (limited to 'src/common') diff --git a/src/common/cfgfiles.c b/src/common/cfgfiles.c index 2532fb93..8b7dbfad 100644 --- a/src/common/cfgfiles.c +++ b/src/common/cfgfiles.c @@ -337,21 +337,10 @@ get_xdir (void) return xdir; } -static void -check_prefs_dir (void) +int +check_config_dir (void) { - char *dir = get_xdir (); - char *msg; - - if (g_access (dir, F_OK) != 0) - { - if (g_mkdir (dir, 0700) != 0) - { - msg = g_strdup_printf ("Cannot create %s", get_xdir ()); - fe_message (msg, FE_MSG_ERROR); - g_free (msg); - } - } + return g_access (get_xdir (), F_OK); } static char * @@ -615,11 +604,10 @@ convert_with_fallback (const char *str, const char *fallback) } void -load_config (void) +load_default_config(void) { - char *cfg, *sp, *buf; const char *username, *realname; - int res, val, i; + char *sp; #ifdef WIN32 char out[256]; #endif @@ -797,46 +785,89 @@ load_config (void) /* private variables */ prefs.local_ip = 0xffffffff; + sp = strchr (prefs.hex_irc_user_name, ' '); + if (sp) + sp[0] = 0; /* spaces in username would break the login */ + g_free ((char *)username); g_free ((char *)realname); +} + +int +make_config_dirs (void) +{ + char *buf; - if (g_file_get_contents (default_file (), &cfg, NULL, NULL)) + if (g_mkdir (get_xdir (), 0700) != 0) + return -1; + + buf = g_build_filename (get_xdir (), "addons", NULL); + if (g_mkdir (buf, 0700) != 0) { - i = 0; - do - { - switch (vars[i].type) - { - case TYPE_STR: - cfg_get_str (cfg, vars[i].name, (char *) &prefs + vars[i].offset, - vars[i].len); - break; - case TYPE_BOOL: - case TYPE_INT: - val = cfg_get_int_with_result (cfg, vars[i].name, &res); - if (res) - *((int *) &prefs + vars[i].offset) = val; - break; - } - i++; - } - while (vars[i].name); + g_free (buf); + return -1; + } + g_free (buf); + + buf = g_build_filename (get_xdir (), HEXCHAT_SOUND_DIR, NULL); + if (g_mkdir (buf, 0700) != 0) + { + g_free (buf); + return -1; + } + g_free (buf); - g_free (cfg); + return 0; +} - } else - { - g_mkdir (prefs.hex_dcc_dir, 0700); - g_mkdir (prefs.hex_dcc_completed_dir, 0700); +int +make_dcc_dirs (void) +{ + if (g_mkdir (prefs.hex_dcc_dir, 0700) != 0) + return -1; - buf = g_build_filename (get_xdir (), "addons", NULL); - g_mkdir (buf, 0700); - g_free (buf); + if (g_mkdir (prefs.hex_dcc_completed_dir, 0700) != 0) + return -1; - buf = g_build_filename (get_xdir (), HEXCHAT_SOUND_DIR, NULL); - g_mkdir (buf, 0700); - g_free (buf); + return 0; +} + +int +load_config (void) +{ + char *cfg, *sp; + int res, val, i; + + g_assert(check_config_dir () == 0); + + if (!g_file_get_contents (default_file (), &cfg, NULL, NULL)) + return -1; + + /* If the config is incomplete we have the default values loaded */ + load_default_config(); + + i = 0; + do + { + switch (vars[i].type) + { + case TYPE_STR: + cfg_get_str (cfg, vars[i].name, (char *) &prefs + vars[i].offset, + vars[i].len); + break; + case TYPE_BOOL: + case TYPE_INT: + val = cfg_get_int_with_result (cfg, vars[i].name, &res); + if (res) + *((int *) &prefs + vars[i].offset) = val; + break; + } + i++; } + while (vars[i].name); + + g_free (cfg); + if (prefs.hex_gui_win_height < 138) prefs.hex_gui_win_height = 138; if (prefs.hex_gui_win_width < 106) @@ -845,6 +876,8 @@ load_config (void) sp = strchr (prefs.hex_irc_user_name, ' '); if (sp) sp[0] = 0; /* spaces in username would break the login */ + + return 0; } int @@ -853,7 +886,8 @@ save_config (void) int fh, i; char *config, *new_config; - check_prefs_dir (); + if (check_config_dir () != 0) + make_config_dirs (); config = default_file (); new_config = g_strconcat (config, ".new", NULL); diff --git a/src/common/cfgfiles.h b/src/common/cfgfiles.h index 103466f3..3a59d26d 100644 --- a/src/common/cfgfiles.h +++ b/src/common/cfgfiles.h @@ -34,7 +34,11 @@ int cfg_put_int (int fh, int value, char *var); int cfg_get_color (char *cfg, char *var, int *r, int *g, int *b); int cfg_put_color (int fh, int r, int g, int b, char *var); char *get_xdir (void); -void load_config (void); +int check_config_dir (void); +void load_default_config (void); +int make_config_dirs (void); +int make_dcc_dirs (void); +int load_config (void); int save_config (void); void list_free (GSList ** list); void list_loadconf (char *file, GSList ** list, char *defaultconf); diff --git a/src/common/hexchat.c b/src/common/hexchat.c index 6d624133..0546f599 100644 --- a/src/common/hexchat.c +++ b/src/common/hexchat.c @@ -1013,9 +1013,10 @@ hexchat_execv (char * const argv[]) #endif } -void -set_locale(void) +static void +set_locale (void) { +#ifdef WIN32 const char const *langs[]={ "af", "sq", "am", "ast", "az", "eu", "be", "bg", "ca", "zh_CN", /* 0 .. 9 */ "zh_TW", "cs", "da", "nl", "en_GB", "en", "et", "fi", "fr", "gl", /* 10 .. 19 */ @@ -1034,6 +1035,7 @@ set_locale(void) strcat (hexchat_lang, "en"); putenv (hexchat_lang); +#endif } int @@ -1073,12 +1075,20 @@ main (int argc, char *argv[]) g_type_init (); #endif - load_config (); + if (check_config_dir () == 0) + { + if (load_config () != 0) + load_default_config (); + } else + { + /* this is probably the first run */ + load_default_config (); + make_config_dirs (); /* FIXME: if this fail display an error (?) */ + make_dcc_dirs (); + } -#ifdef WIN32 /* we MUST do this after load_config () AND before fe_init (thus gtk_init) otherwise it will fail */ - set_locale(); -#endif + set_locale (); #ifdef SOCKS SOCKSinit (argv[0]); -- cgit 1.4.1