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; return xdir;
} }
static void int
check_prefs_dir (void) check_config_dir (void)
{ {
char *dir = get_xdir (); return g_access (get_xdir (), F_OK);
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);
}
}
} }
static char * static char *
@ -615,11 +604,10 @@ convert_with_fallback (const char *str, const char *fallback)
} }
void void
load_config (void) load_default_config(void)
{ {
char *cfg, *sp, *buf;
const char *username, *realname; const char *username, *realname;
int res, val, i; char *sp;
#ifdef WIN32 #ifdef WIN32
char out[256]; char out[256];
#endif #endif
@ -797,46 +785,89 @@ load_config (void)
/* private variables */ /* private variables */
prefs.local_ip = 0xffffffff; 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 *)username);
g_free ((char *)realname); 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)
{ {
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);
} 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); 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
{
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) if (prefs.hex_gui_win_height < 138)
prefs.hex_gui_win_height = 138; prefs.hex_gui_win_height = 138;
if (prefs.hex_gui_win_width < 106) if (prefs.hex_gui_win_width < 106)
@ -845,6 +876,8 @@ load_config (void)
sp = strchr (prefs.hex_irc_user_name, ' '); sp = strchr (prefs.hex_irc_user_name, ' ');
if (sp) if (sp)
sp[0] = 0; /* spaces in username would break the login */ sp[0] = 0; /* spaces in username would break the login */
return 0;
} }
int int
@ -853,7 +886,8 @@ save_config (void)
int fh, i; int fh, i;
char *config, *new_config; char *config, *new_config;
check_prefs_dir (); if (check_config_dir () != 0)
make_config_dirs ();
config = default_file (); config = default_file ();
new_config = g_strconcat (config, ".new", NULL); 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_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); int cfg_put_color (int fh, int r, int g, int b, char *var);
char *get_xdir (void); 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); int save_config (void);
void list_free (GSList ** list); void list_free (GSList ** list);
void list_loadconf (char *file, GSList ** list, char *defaultconf); void list_loadconf (char *file, GSList ** list, char *defaultconf);

View File

@ -1013,9 +1013,10 @@ hexchat_execv (char * const argv[])
#endif #endif
} }
void static void
set_locale(void) set_locale (void)
{ {
#ifdef WIN32
const char const *langs[]={ const char const *langs[]={
"af", "sq", "am", "ast", "az", "eu", "be", "bg", "ca", "zh_CN", /* 0 .. 9 */ "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 */ "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"); strcat (hexchat_lang, "en");
putenv (hexchat_lang); putenv (hexchat_lang);
#endif
} }
int int
@ -1073,12 +1075,20 @@ main (int argc, char *argv[])
g_type_init (); g_type_init ();
#endif #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 */ /* we MUST do this after load_config () AND before fe_init (thus gtk_init) otherwise it will fail */
set_locale(); set_locale ();
#endif
#ifdef SOCKS #ifdef SOCKS
SOCKSinit (argv[0]); SOCKSinit (argv[0]);