1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
* A demonstration plugin that diverts account storage to an alternate location.
*
* Copyright © 2010 Nokia Corporation
* Copyright © 2010 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <mission-control-plugins/mission-control-plugins.h>
#define DONT_DIVERT "fakecm/fakeprotocol/dontdivert"
#define CONFFILE "mcp-test-diverted-account-plugin.conf"
#define PLUGIN_NAME "diverted-keyfile"
#define PLUGIN_PRIORITY MCP_ACCOUNT_STORAGE_PLUGIN_PRIO_NORMAL
#define PLUGIN_DESCRIPTION \
"Test plugin that grabs all accounts it receives (except '" \
DONT_DIVERT "*') and diverts them to '" CONFFILE \
"' in g_get_user_cache_dir () instead of the usual location."
#define DEBUG g_debug
typedef struct {
GObject parent;
GKeyFile *keyfile;
gboolean save;
gboolean loaded;
} AccountDiversionPlugin;
typedef struct {
GObjectClass parent_class;
} AccountDiversionPluginClass;
GType account_diversion_plugin_get_type (void) G_GNUC_CONST;
static void account_storage_iface_init (McpAccountStorageIface *,
gpointer);
#define ACCOUNT_DIVERSION_PLUGIN(o) \
(G_TYPE_CHECK_INSTANCE_CAST ((o), account_diversion_plugin_get_type (), \
AccountDiversionPlugin))
G_DEFINE_TYPE_WITH_CODE (AccountDiversionPlugin, account_diversion_plugin,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
account_storage_iface_init));
static void
account_diversion_plugin_init (AccountDiversionPlugin *self)
{
DEBUG ("account_diversion_plugin_init");
self->keyfile = g_key_file_new ();
self->save = FALSE;
self->loaded = FALSE;
}
static void
account_diversion_plugin_class_init (AccountDiversionPluginClass *cls)
{
DEBUG ("account_diversion_plugin_class_init");
}
static gchar *
_conf_filename (void)
{
static gchar *file = NULL;
if (file == NULL)
{
const gchar *dir = g_get_user_cache_dir ();
file = g_build_path (G_DIR_SEPARATOR_S, dir, CONFFILE, NULL);
}
return file;
}
static gboolean
_have_config (void)
{
const gchar *file = _conf_filename ();
DEBUG ("checking for %s", file);
return g_file_test (file, G_FILE_TEST_EXISTS);
}
static void
_create_config (void)
{
gchar *file = _conf_filename ();
gchar *dir = g_path_get_dirname (file);
g_mkdir_with_parents (dir, 0700);
g_free (dir);
g_file_set_contents (file, "# diverted accounts\n", -1, NULL);
DEBUG ("created %s", file);
}
static gboolean
_set (McpAccountStorage *self,
McpAccountManager *am,
const gchar *account,
const gchar *key,
GVariant *val,
McpParameterFlags flags)
{
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
gchar *val_str;
if (g_str_has_prefix (account, DONT_DIVERT))
return FALSE;
adp->save = TRUE;
val_str = mcp_account_manager_escape_variant_for_keyfile (am, val);
g_key_file_set_value (adp->keyfile, account, key, val_str);
g_free (val_str);
return TRUE;
}
static gboolean
_set_attribute (McpAccountStorage *self,
McpAccountManager *am,
const gchar *account,
const gchar *attribute,
GVariant *val,
McpAttributeFlags flags)
{
return _set (self, am, account, attribute, val, flags);
}
static gboolean
_set_parameter (McpAccountStorage *self,
McpAccountManager *am,
const gchar *account,
const gchar *parameter,
GVariant *val,
McpParameterFlags flags)
{
gchar *param = g_strdup_printf ("param-%s", parameter);
gboolean ret;
ret = _set (self, am, account, param, val, flags);
g_free (param);
return ret;
}
static gboolean
_get (const McpAccountStorage *self,
const McpAccountManager *am,
const gchar *account,
const gchar *key)
{
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
if (key != NULL)
{
gchar *v = g_key_file_get_value (adp->keyfile, account, key, NULL);
if (v == NULL)
return FALSE;
mcp_account_manager_set_value (am, account, key, v);
g_free (v);
}
else
{
gsize i;
gsize n;
GStrv keys = g_key_file_get_keys (adp->keyfile, account, &n, NULL);
if (keys == NULL)
n = 0;
for (i = 0; i < n; i++)
{
gchar *v = g_key_file_get_value (adp->keyfile, account, keys[i], NULL);
if (v != NULL)
mcp_account_manager_set_value (am, account, keys[i], v);
g_free (v);
}
g_strfreev (keys);
}
return TRUE;
}
static gboolean
_delete (const McpAccountStorage *self,
const McpAccountManager *am,
const gchar *account,
const gchar *key)
{
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
if (key == NULL)
{
if (g_key_file_remove_group (adp->keyfile, account, NULL))
adp->save = TRUE;
}
else
{
gsize n;
GStrv keys;
if (g_key_file_remove_key (adp->keyfile, account, key, NULL))
adp->save = TRUE;
keys = g_key_file_get_keys (adp->keyfile, account, &n, NULL);
if (keys == NULL || n == 0)
g_key_file_remove_group (adp->keyfile, account, NULL);
g_strfreev (keys);
}
return TRUE;
}
static gboolean
_commit (const McpAccountStorage *self,
const McpAccountManager *am,
const gchar *account_name G_GNUC_UNUSED)
{
gsize n;
gchar *data;
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
gboolean rval = FALSE;
/* This simple implementation ignores account_name and commits everything:
* we're writing out the whole keyfile anyway */
if (!adp->save)
return TRUE;
if (!_have_config ())
_create_config ();
data = g_key_file_to_data (adp->keyfile, &n, NULL);
rval = g_file_set_contents (_conf_filename (), data, n, NULL);
adp->save = !rval;
g_free (data);
return rval;
}
static GList *
_list (const McpAccountStorage *self,
const McpAccountManager *am)
{
gsize i;
gsize n;
GStrv accounts;
GList *rval = NULL;
AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
if (!_have_config ())
_create_config ();
if (!adp->loaded)
adp->loaded = g_key_file_load_from_file (adp->keyfile, _conf_filename (),
G_KEY_FILE_KEEP_COMMENTS, NULL);
accounts = g_key_file_get_groups (adp->keyfile, &n);
for (i = 0; i < n; i++)
rval = g_list_prepend (rval, g_strdup (accounts[i]));
g_strfreev (accounts);
return rval;
}
static void
account_storage_iface_init (McpAccountStorageIface *iface,
gpointer unused G_GNUC_UNUSED)
{
iface->name = PLUGIN_NAME;
iface->desc = PLUGIN_DESCRIPTION;
iface->priority = PLUGIN_PRIORITY;
iface->get = _get;
iface->set_attribute = _set_attribute;
iface->set_parameter = _set_parameter;
iface->delete = _delete;
iface->commit = _commit;
iface->list = _list;
}
GObject *
mcp_plugin_ref_nth_object (guint n)
{
DEBUG ("Initializing mcp-account-diversion-plugin (n=%u)", n);
switch (n)
{
case 0:
return g_object_new (account_diversion_plugin_get_type (), NULL);
default:
return NULL;
}
}
|