diff options
author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2012-09-07 13:11:50 +0100 |
---|---|---|
committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2012-09-07 17:35:13 +0100 |
commit | 9a932a687f75be5238c533d928f914292a5e9c43 (patch) | |
tree | 927c3b182ecce434bd16a4969d045a87b4e413cc | |
parent | bd9334449d2ff1db6158ecfbb41f65327a40a078 (diff) | |
download | telepathy-mission-control-9a932a687f75be5238c533d928f914292a5e9c43.tar.gz |
Default account backend: restructure to be able to look in multiple places
This isolates all file-writing into _commit, and all file-reading into
am_default_load_keyfile.
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=35896
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
-rw-r--r-- | src/mcd-account-manager-default.c | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/src/mcd-account-manager-default.c b/src/mcd-account-manager-default.c index 603ecd8b..e7b9f065 100644 --- a/src/mcd-account-manager-default.c +++ b/src/mcd-account-manager-default.c @@ -612,6 +612,8 @@ _commit (const McpAccountStorage *self, if (!amd->save) return TRUE; + DEBUG ("Saving accounts to %s", amd->filename); + if (!_have_config (amd)) _create_config (amd); @@ -625,6 +627,31 @@ _commit (const McpAccountStorage *self, return rval; } +static void +am_default_load_keyfile (McdAccountManagerDefault *self, + const gchar *filename) +{ + GError *error = NULL; + + if (g_key_file_load_from_file (self->keyfile, filename, + G_KEY_FILE_KEEP_COMMENTS, &error)) + { + DEBUG ("Loaded accounts from %s", filename); + } + else + { + DEBUG ("Failed to load accounts from %s: %s", filename, error->message); + g_error_free (error); + + /* Start with a blank configuration, but do not save straight away; + * we don't want to overwrite a corrupt-but-maybe-recoverable + * configuration file with an empty one until given a reason to + * do so. */ + g_key_file_load_from_data (self->keyfile, INITIAL_CONFIG, -1, + G_KEY_FILE_KEEP_COMMENTS, NULL); + } +} + static GList * _list (const McpAccountStorage *self, const McpAccountManager *am) @@ -635,12 +662,26 @@ _list (const McpAccountStorage *self, GList *rval = NULL; McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self); - if (!_have_config (amd)) - _create_config (amd); + if (!amd->loaded && g_file_test (amd->filename, G_FILE_TEST_EXISTS)) + { + /* If the file exists, but loading it fails, we deliberately + * do not fall through to the "initial configuration" case, + * because we don't want to overwrite a corrupted file + * with an empty one until an actual write takes place. */ + am_default_load_keyfile (amd, amd->filename); + amd->loaded = TRUE; + } if (!amd->loaded) - amd->loaded = g_key_file_load_from_file (amd->keyfile, amd->filename, - G_KEY_FILE_KEEP_COMMENTS, NULL); + { + DEBUG ("Creating initial account data"); + g_key_file_load_from_data (amd->keyfile, INITIAL_CONFIG, -1, + G_KEY_FILE_KEEP_COMMENTS, NULL); + amd->loaded = TRUE; + /* create the placeholder file */ + amd->save = TRUE; + _commit (self, am, NULL); + } accounts = g_key_file_get_groups (amd->keyfile, &n); |