2
* A demonstration plugin that diverts account storage to an alternate location.
4
* Copyright © 2010 Nokia Corporation
5
* Copyright © 2010 Collabora Ltd.
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.
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.
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
22
#include <mission-control-plugins/mission-control-plugins.h>
24
#define CONFFILE "mcp-test-diverted-account-plugin.conf"
26
#define PLUGIN_NAME "diverted-keyfile"
27
#define PLUGIN_PRIORITY MCP_ACCOUNT_STORAGE_PLUGIN_PRIO_NORMAL
28
#define PLUGIN_DESCRIPTION \
29
"Test plugin that grabs all accounts it receives and diverts them to '" \
30
CONFFILE "' in g_get_user_cache_dir () instead of the usual location"
39
} AccountDiversionPlugin;
42
GObjectClass parent_class;
43
} AccountDiversionPluginClass;
45
GType account_diversion_plugin_get_type (void) G_GNUC_CONST;
46
static void account_storage_iface_init (McpAccountStorageIface *,
50
#define ACCOUNT_DIVERSION_PLUGIN(o) \
51
(G_TYPE_CHECK_INSTANCE_CAST ((o), account_diversion_plugin_get_type (), \
52
AccountDiversionPlugin))
54
G_DEFINE_TYPE_WITH_CODE (AccountDiversionPlugin, account_diversion_plugin,
56
G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
57
account_storage_iface_init));
60
account_diversion_plugin_init (AccountDiversionPlugin *self)
62
DEBUG ("account_diversion_plugin_init");
63
self->keyfile = g_key_file_new ();
69
account_diversion_plugin_class_init (AccountDiversionPluginClass *cls)
71
DEBUG ("account_diversion_plugin_class_init");
77
static gchar *file = NULL;
81
const gchar *dir = g_get_user_cache_dir ();
83
file = g_build_path (G_DIR_SEPARATOR_S, dir, CONFFILE, NULL);
92
const gchar *file = _conf_filename ();
94
DEBUG ("checking for %s", file);
95
return g_file_test (file, G_FILE_TEST_EXISTS);
101
gchar *file = _conf_filename ();
102
gchar *dir = g_path_get_dirname (file);
104
g_mkdir_with_parents (dir, 0700);
107
g_file_set_contents (file, "# diverted accounts\n", -1, NULL);
108
DEBUG ("created %s", file);
112
_set (const McpAccountStorage *self,
113
const McpAccountManager *am,
114
const gchar *account,
118
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
121
g_key_file_set_value (adp->keyfile, account, key, val);
127
_get (const McpAccountStorage *self,
128
const McpAccountManager *am,
129
const gchar *account,
132
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
136
gchar *v = g_key_file_get_value (adp->keyfile, account, key, NULL);
141
mcp_account_manager_set_value (am, account, key, v);
148
GStrv keys = g_key_file_get_keys (adp->keyfile, account, &n, NULL);
153
for (i = 0; i < n; i++)
155
gchar *v = g_key_file_get_value (adp->keyfile, account, keys[i], NULL);
158
mcp_account_manager_set_value (am, account, keys[i], v);
170
_delete (const McpAccountStorage *self,
171
const McpAccountManager *am,
172
const gchar *account,
175
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
179
if (g_key_file_remove_group (adp->keyfile, account, NULL))
187
if (g_key_file_remove_key (adp->keyfile, account, key, NULL))
190
keys = g_key_file_get_keys (adp->keyfile, account, &n, NULL);
192
if (keys == NULL || n == 0)
193
g_key_file_remove_group (adp->keyfile, account, NULL);
203
_commit (const McpAccountStorage *self,
204
const McpAccountManager *am)
208
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
209
gboolean rval = FALSE;
214
if (!_have_config ())
217
data = g_key_file_to_data (adp->keyfile, &n, NULL);
218
rval = g_file_set_contents (_conf_filename (), data, n, NULL);
226
_list (const McpAccountStorage *self,
227
const McpAccountManager *am)
233
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
235
if (!_have_config ())
239
adp->loaded = g_key_file_load_from_file (adp->keyfile, _conf_filename (),
240
G_KEY_FILE_KEEP_COMMENTS, NULL);
242
accounts = g_key_file_get_groups (adp->keyfile, &n);
244
for (i = 0; i < n; i++)
245
rval = g_list_prepend (rval, g_strdup (accounts[i]));
247
g_strfreev (accounts);
253
account_storage_iface_init (McpAccountStorageIface *iface,
254
gpointer unused G_GNUC_UNUSED)
256
mcp_account_storage_iface_set_name (iface, PLUGIN_NAME);
257
mcp_account_storage_iface_set_desc (iface, PLUGIN_DESCRIPTION);
258
mcp_account_storage_iface_set_priority (iface, PLUGIN_PRIORITY);
260
mcp_account_storage_iface_implement_get (iface, _get);
261
mcp_account_storage_iface_implement_set (iface, _set);
262
mcp_account_storage_iface_implement_delete (iface, _delete);
263
mcp_account_storage_iface_implement_commit (iface, _commit);
264
mcp_account_storage_iface_implement_list (iface, _list);
269
mcp_plugin_ref_nth_object (guint n)
271
DEBUG ("Initializing mcp-account-diversion-plugin (n=%u)", n);
276
return g_object_new (account_diversion_plugin_get_type (), NULL);