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).
This commit is contained in:
Diogo Sousa 2013-05-13 01:58:08 +01:00
parent 61ed0829bd
commit d9d05e83ba
3 changed files with 107 additions and 59 deletions

View File

@ -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,11 +785,67 @@ 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);
}
if (g_file_get_contents (default_file (), &cfg, NULL, NULL))
int
make_config_dirs (void)
{
char *buf;
if (g_mkdir (get_xdir (), 0700) != 0)
return -1;
buf = g_build_filename (get_xdir (), "addons", NULL);
if (g_mkdir (buf, 0700) != 0)
{
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);
return 0;
}
int
make_dcc_dirs (void)
{
if (g_mkdir (prefs.hex_dcc_dir, 0700) != 0)
return -1;
if (g_mkdir (prefs.hex_dcc_completed_dir, 0700) != 0)
return -1;
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
{
@ -824,19 +868,6 @@ load_config (void)
g_free (cfg);
} else
{
g_mkdir (prefs.hex_dcc_dir, 0700);
g_mkdir (prefs.hex_dcc_completed_dir, 0700);
buf = g_build_filename (get_xdir (), "addons", NULL);
g_mkdir (buf, 0700);
g_free (buf);
buf = g_build_filename (get_xdir (), HEXCHAT_SOUND_DIR, NULL);
g_mkdir (buf, 0700);
g_free (buf);
}
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);

View File

@ -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);

View File

@ -1013,9 +1013,10 @@ hexchat_execv (char * const argv[])
#endif
}
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
#ifdef SOCKS
SOCKSinit (argv[0]);