This commit is contained in:
TingPing 2015-04-08 06:27:07 -04:00
parent 77141cb080
commit 8ff5b53525

View File

@ -1767,13 +1767,13 @@ hexchat_free_array (hexchat_plugin *ph, char **strv)
#define DEFAULT_GROUP_TEXT_LEN 10 #define DEFAULT_GROUP_TEXT_LEN 10
static char * static char *
pluginpref_create_filename (hexchat_plugin *pl) pluginpref_create_filename (hexchat_plugin *pl, const char *extension)
{ {
char *confname, *tmp, *filename; char *confname, *tmp, *filename;
tmp = g_strdup (pl->name); tmp = g_strdup (pl->name);
canonalize_key (tmp); canonalize_key (tmp);
confname = g_strdup_printf ("%s%caddon_%s.conf", get_xdir(), G_DIR_SEPARATOR, tmp); confname = g_strdup_printf ("%s%caddon_%s.%s", get_xdir(), G_DIR_SEPARATOR, tmp, extension);
filename = g_filename_from_utf8 (confname, -1, NULL, NULL, NULL); filename = g_filename_from_utf8 (confname, -1, NULL, NULL, NULL);
g_free (confname); g_free (confname);
g_free (tmp); g_free (tmp);
@ -1784,14 +1784,14 @@ pluginpref_create_filename (hexchat_plugin *pl)
/* Migration simply means prepending a valid group /* Migration simply means prepending a valid group
* to the old format, it is otherwise compatable */ * to the old format, it is otherwise compatable */
static gboolean static gboolean
pluginpref_migrate_file (const char *filename) pluginpref_migrate_file_real (const char *old_filename, const char *new_filename)
{ {
char *contents; char *contents;
gsize len, newlen; gsize len, newlen;
gboolean ret; gboolean ret;
/* Just read the entire file, ideally it is fairly small */ /* Just read the entire file, ideally it is fairly small */
if (!g_file_get_contents (filename, &contents, &len, NULL)) if (!g_file_get_contents (old_filename, &contents, &len, NULL))
return FALSE; return FALSE;
/* Avoid duplicating data and just prepend the new group */ /* Avoid duplicating data and just prepend the new group */
@ -1800,63 +1800,71 @@ pluginpref_migrate_file (const char *filename)
memmove (contents + DEFAULT_GROUP_TEXT_LEN, contents, len); memmove (contents + DEFAULT_GROUP_TEXT_LEN, contents, len);
memcpy (contents, DEFAULT_GROUP_TEXT, DEFAULT_GROUP_TEXT_LEN); memcpy (contents, DEFAULT_GROUP_TEXT, DEFAULT_GROUP_TEXT_LEN);
ret = g_file_set_contents (filename, contents, (gssize)MIN(newlen, G_MAXSSIZE), NULL); ret = g_file_set_contents (new_filename, contents, (gssize)MIN(newlen, G_MAXSSIZE), NULL);
g_free (contents); g_free (contents);
return ret; return ret;
} }
static void
pluginpref_migrate_file (hexchat_plugin *pl, char *filename)
{
char *old_filename;
if (g_file_test (filename, G_FILE_TEST_EXISTS))
return;
old_filename = pluginpref_create_filename (pl, "conf");
if (g_file_test (old_filename, G_FILE_TEST_EXISTS))
{
if (pluginpref_migrate_file_real (old_filename, filename))
{
/* Migration worked, move the old one as a backup incase
* keyfile is invalid and data lost but also to avoid confusion */
char *backup_filename = pluginpref_create_filename (pl, "bak");
g_rename (old_filename, backup_filename);
g_free (backup_filename);
}
}
g_free (old_filename);
}
static GKeyFile * static GKeyFile *
pluginpref_load_file (hexchat_plugin *pl) pluginpref_load_file (hexchat_plugin *pl)
{ {
char *filename; char *filename;
GKeyFile *file; GKeyFile *file;
GError *error = NULL;
filename = pluginpref_create_filename (pl); filename = pluginpref_create_filename (pl, "ini");
if (!filename) if (!filename)
return NULL; return NULL;
file = g_key_file_new (); file = g_key_file_new ();
if (!g_key_file_load_from_file (file, filename, G_KEY_FILE_NONE, &error))
{
/* If it is an old format config we can migrate it */
if (error->code == G_KEY_FILE_ERROR_GROUP_NOT_FOUND)
{
if (pluginpref_migrate_file (filename))
{
/* Try again */
if (g_key_file_load_from_file (file, filename, G_KEY_FILE_NONE, NULL))
goto success;
}
else
{
/* File not found is acceptable, it will be created when
* something is set */
goto success;
}
}
else if (error->code == G_KEY_FILE_ERROR_NOT_FOUND)
goto success;
g_free (filename);
g_error_free (error);
g_key_file_free (file);
return NULL;
}
success:
if (error) pluginpref_migrate_file (pl, filename);
g_error_free (error);
/* It failing doesn't matter, a new file will just be created */
g_key_file_load_from_file (file, filename, G_KEY_FILE_NONE, NULL);
g_free (filename); g_free (filename);
return file; return file;
} }
#define PLUGINPREF_INIT_KEYFILE(pl,ret) G_STMT_START \
if (!pl->keyfile) \
{ \
if (!(pl->keyfile = pluginpref_load_file (pl))) \
return ret; \
} \
G_STMT_END
static void static void
pluginpref_save (hexchat_plugin *pl) pluginpref_save (hexchat_plugin *pl)
{ {
gchar *data, *filename; gchar *data, *filename;
gsize length; gsize length;
filename = pluginpref_create_filename (pl); filename = pluginpref_create_filename (pl, "ini");
if (!filename) if (!filename)
return; return;
@ -1874,11 +1882,7 @@ pluginpref_save (hexchat_plugin *pl)
int int
hexchat_pluginpref_set_str (hexchat_plugin *pl, const char *var, const char *value) hexchat_pluginpref_set_str (hexchat_plugin *pl, const char *var, const char *value)
{ {
if (!pl->keyfile) PLUGINPREF_INIT_KEYFILE (pl, 0);
{
if (!(pl->keyfile = pluginpref_load_file (pl)))
return 0;
}
g_key_file_set_string (pl->keyfile, DEFAULT_GROUP, var, value); g_key_file_set_string (pl->keyfile, DEFAULT_GROUP, var, value);
pluginpref_save (pl); pluginpref_save (pl);
@ -1888,11 +1892,7 @@ hexchat_pluginpref_set_str (hexchat_plugin *pl, const char *var, const char *val
char * char *
hexchat_pluginpref_get_str_ptr (hexchat_plugin *pl, const char *var) hexchat_pluginpref_get_str_ptr (hexchat_plugin *pl, const char *var)
{ {
if (!pl->keyfile) PLUGINPREF_INIT_KEYFILE (pl, 0);
{
if (!(pl->keyfile = pluginpref_load_file (pl)))
return 0;
}
return g_key_file_get_string (pl->keyfile, DEFAULT_GROUP, var, NULL); return g_key_file_get_string (pl->keyfile, DEFAULT_GROUP, var, NULL);
} }
@ -1915,11 +1915,7 @@ hexchat_pluginpref_get_str (hexchat_plugin *pl, const char *var, char *dest)
int int
hexchat_pluginpref_set_int (hexchat_plugin *pl, const char *var, int value) hexchat_pluginpref_set_int (hexchat_plugin *pl, const char *var, int value)
{ {
if (!pl->keyfile) PLUGINPREF_INIT_KEYFILE (pl, 0);
{
if (!(pl->keyfile = pluginpref_load_file (pl)))
return 0;
}
g_key_file_set_integer (pl->keyfile, DEFAULT_GROUP, var, value); g_key_file_set_integer (pl->keyfile, DEFAULT_GROUP, var, value);
pluginpref_save (pl); pluginpref_save (pl);
@ -1932,11 +1928,7 @@ hexchat_pluginpref_get_int (hexchat_plugin *pl, const char *var)
int value; int value;
GError *error = NULL; GError *error = NULL;
if (!pl->keyfile) PLUGINPREF_INIT_KEYFILE (pl, -1);
{
if (!(pl->keyfile = pluginpref_load_file (pl)))
return -1;
}
value = g_key_file_get_integer (pl->keyfile, DEFAULT_GROUP, var, &error); value = g_key_file_get_integer (pl->keyfile, DEFAULT_GROUP, var, &error);
if (error) if (error)
@ -1958,11 +1950,7 @@ hexchat_pluginpref_delete (hexchat_plugin *pl, const char *var)
{ {
gboolean ret; gboolean ret;
if (!pl->keyfile) PLUGINPREF_INIT_KEYFILE (pl, 0);
{
if (!(pl->keyfile = pluginpref_load_file (pl)))
return 0;
}
ret = g_key_file_remove_key (pl->keyfile, DEFAULT_GROUP, var, NULL); ret = g_key_file_remove_key (pl->keyfile, DEFAULT_GROUP, var, NULL);
pluginpref_save (pl); pluginpref_save (pl);
@ -1972,11 +1960,7 @@ hexchat_pluginpref_delete (hexchat_plugin *pl, const char *var)
char ** char **
hexchat_pluginpref_list_keys (hexchat_plugin *pl) hexchat_pluginpref_list_keys (hexchat_plugin *pl)
{ {
if (!pl->keyfile) PLUGINPREF_INIT_KEYFILE (pl, NULL);
{
if (!(pl->keyfile = pluginpref_load_file (pl)))
return NULL;
}
return g_key_file_get_keys (pl->keyfile, DEFAULT_GROUP, NULL, NULL); return g_key_file_get_keys (pl->keyfile, DEFAULT_GROUP, NULL, NULL);
} }