1 /*
2  * MC account storage backend inspector, default backend
3  *
4  * Copyright © 2010 Nokia Corporation
5  * Copyright © 2010 Collabora Ltd.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "config.h"
23 #include <glib.h>
24 #include <string.h>
25 
26 #include "account-store-default.h"
27 
default_config(void)28 static const gchar *default_config (void)
29 {
30   return g_build_filename (g_get_user_data_dir (), "telepathy",
31       "mission-control", "accounts.cfg", NULL);
32 }
33 
default_keyfile(void)34 static GKeyFile * default_keyfile (void)
35 {
36   GError *error = NULL;
37   static GKeyFile *keyfile = NULL;
38   const gchar *path = NULL;
39 
40   if (keyfile != NULL)
41     return keyfile;
42 
43   path = default_config ();
44 
45   keyfile = g_key_file_new ();
46 
47   if (!g_key_file_load_from_file (keyfile, path, 0, &error))
48     {
49       if (error != NULL)
50         g_warning ("keyfile '%s' error: %s", path, error->message);
51       else
52         g_warning ("keyfile '%s' error: unknown error", path);
53 
54       g_key_file_free (keyfile);
55       g_error_free (error);
56       keyfile = NULL;
57     }
58 
59   return keyfile;
60 }
61 
commit_changes(void)62 static gboolean commit_changes (void)
63 {
64   gsize n = 0;
65   gchar *data = NULL;
66   gboolean done = FALSE;
67   GKeyFile *keyfile = default_keyfile ();
68   const gchar *config = default_config ();
69 
70   data = g_key_file_to_data (keyfile, &n, NULL);
71   done = g_file_set_contents (config, data, n, NULL);
72 
73   g_free (data);
74 
75   return done;
76 }
77 
78 gchar *
default_get(const gchar * account,const gchar * key)79 default_get (const gchar *account,
80     const gchar *key)
81 {
82   return g_key_file_get_string (default_keyfile (), account, key, NULL);
83 }
84 
85 gboolean
default_set(const gchar * account,const gchar * key,const gchar * value)86 default_set (const gchar *account,
87     const gchar *key,
88     const gchar *value)
89 {
90   GKeyFile *keyfile = NULL;
91 
92   keyfile = default_keyfile ();
93 
94   if (keyfile == NULL)
95     return FALSE;
96 
97   g_key_file_set_string (keyfile, account, key, value);
98 
99   return commit_changes ();
100 }
101 
102 gboolean
default_delete(const gchar * account)103 default_delete (const gchar *account)
104 {
105   GKeyFile *keyfile = default_keyfile ();
106 
107   g_key_file_remove_group (keyfile, account, NULL);
108 
109   return commit_changes ();
110 }
111 
112 gboolean
default_exists(const gchar * account)113 default_exists (const gchar *account)
114 {
115   return g_key_file_has_group (default_keyfile (), account);
116 }
117 
118 GStrv
default_list(void)119 default_list (void)
120 {
121   return g_key_file_get_groups (default_keyfile (), NULL);
122 }
123