~ubuntu-branches/ubuntu/precise/telepathy-mission-control-5/precise

« back to all changes in this revision

Viewing changes to test/twisted/mcp-account-diversion.c

Tags: upstream-5.5.2
Import upstream version 5.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * A demonstration plugin that diverts account storage to an alternate location.
 
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 <mission-control-plugins/mission-control-plugins.h>
 
23
 
 
24
#define CONFFILE "mcp-test-diverted-account-plugin.conf"
 
25
 
 
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"
 
31
 
 
32
#define DEBUG g_debug
 
33
 
 
34
typedef struct {
 
35
  GObject parent;
 
36
  GKeyFile *keyfile;
 
37
  gboolean save;
 
38
  gboolean loaded;
 
39
} AccountDiversionPlugin;
 
40
 
 
41
typedef struct {
 
42
  GObjectClass parent_class;
 
43
} AccountDiversionPluginClass;
 
44
 
 
45
GType account_diversion_plugin_get_type (void) G_GNUC_CONST;
 
46
static void account_storage_iface_init (McpAccountStorageIface *,
 
47
    gpointer);
 
48
 
 
49
 
 
50
#define ACCOUNT_DIVERSION_PLUGIN(o) \
 
51
  (G_TYPE_CHECK_INSTANCE_CAST ((o), account_diversion_plugin_get_type (), \
 
52
      AccountDiversionPlugin))
 
53
 
 
54
G_DEFINE_TYPE_WITH_CODE (AccountDiversionPlugin, account_diversion_plugin,
 
55
    G_TYPE_OBJECT,
 
56
    G_IMPLEMENT_INTERFACE (MCP_TYPE_ACCOUNT_STORAGE,
 
57
        account_storage_iface_init));
 
58
 
 
59
static void
 
60
account_diversion_plugin_init (AccountDiversionPlugin *self)
 
61
{
 
62
  DEBUG ("account_diversion_plugin_init");
 
63
  self->keyfile = g_key_file_new ();
 
64
  self->save = FALSE;
 
65
  self->loaded = FALSE;
 
66
}
 
67
 
 
68
static void
 
69
account_diversion_plugin_class_init (AccountDiversionPluginClass *cls)
 
70
{
 
71
  DEBUG ("account_diversion_plugin_class_init");
 
72
}
 
73
 
 
74
static gchar *
 
75
_conf_filename (void)
 
76
{
 
77
  static gchar *file = NULL;
 
78
 
 
79
  if (file == NULL)
 
80
    {
 
81
      const gchar *dir = g_get_user_cache_dir ();
 
82
 
 
83
      file = g_build_path (G_DIR_SEPARATOR_S, dir, CONFFILE, NULL);
 
84
    }
 
85
 
 
86
  return file;
 
87
}
 
88
 
 
89
static gboolean
 
90
_have_config (void)
 
91
{
 
92
  const gchar *file = _conf_filename ();
 
93
 
 
94
  DEBUG ("checking for %s", file);
 
95
  return g_file_test (file, G_FILE_TEST_EXISTS);
 
96
}
 
97
 
 
98
static void
 
99
_create_config (void)
 
100
{
 
101
  gchar *file = _conf_filename ();
 
102
  gchar *dir = g_path_get_dirname (file);
 
103
 
 
104
  g_mkdir_with_parents (dir, 0700);
 
105
  g_free (dir);
 
106
 
 
107
  g_file_set_contents (file, "# diverted accounts\n", -1, NULL);
 
108
  DEBUG ("created %s", file);
 
109
}
 
110
 
 
111
static gboolean
 
112
_set (const McpAccountStorage *self,
 
113
    const McpAccountManager *am,
 
114
    const gchar *account,
 
115
    const gchar *key,
 
116
    const gchar *val)
 
117
{
 
118
  AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
 
119
 
 
120
  adp->save = TRUE;
 
121
  g_key_file_set_value (adp->keyfile, account, key, val);
 
122
 
 
123
  return TRUE;
 
124
}
 
125
 
 
126
static gboolean
 
127
_get (const McpAccountStorage *self,
 
128
    const McpAccountManager *am,
 
129
    const gchar *account,
 
130
    const gchar *key)
 
131
{
 
132
  AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
 
133
 
 
134
  if (key != NULL)
 
135
    {
 
136
      gchar *v = g_key_file_get_value (adp->keyfile, account, key, NULL);
 
137
 
 
138
      if (v == NULL)
 
139
        return FALSE;
 
140
 
 
141
      mcp_account_manager_set_value (am, account, key, v);
 
142
      g_free (v);
 
143
    }
 
144
  else
 
145
    {
 
146
      gsize i;
 
147
      gsize n;
 
148
      GStrv keys = g_key_file_get_keys (adp->keyfile, account, &n, NULL);
 
149
 
 
150
      if (keys == NULL)
 
151
        n = 0;
 
152
 
 
153
      for (i = 0; i < n; i++)
 
154
        {
 
155
          gchar *v = g_key_file_get_value (adp->keyfile, account, keys[i], NULL);
 
156
 
 
157
          if (v != NULL)
 
158
            mcp_account_manager_set_value (am, account, keys[i], v);
 
159
 
 
160
          g_free (v);
 
161
        }
 
162
 
 
163
      g_strfreev (keys);
 
164
    }
 
165
 
 
166
  return TRUE;
 
167
}
 
168
 
 
169
static gboolean
 
170
_delete (const McpAccountStorage *self,
 
171
      const McpAccountManager *am,
 
172
      const gchar *account,
 
173
      const gchar *key)
 
174
{
 
175
  AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
 
176
 
 
177
  if (key == NULL)
 
178
    {
 
179
      if (g_key_file_remove_group (adp->keyfile, account, NULL))
 
180
        adp->save = TRUE;
 
181
    }
 
182
  else
 
183
    {
 
184
      gsize n;
 
185
      GStrv keys;
 
186
 
 
187
      if (g_key_file_remove_key (adp->keyfile, account, key, NULL))
 
188
        adp->save = TRUE;
 
189
 
 
190
      keys = g_key_file_get_keys (adp->keyfile, account, &n, NULL);
 
191
 
 
192
      if (keys == NULL || n == 0)
 
193
        g_key_file_remove_group (adp->keyfile, account, NULL);
 
194
 
 
195
      g_strfreev (keys);
 
196
    }
 
197
 
 
198
  return TRUE;
 
199
}
 
200
 
 
201
 
 
202
static gboolean
 
203
_commit (const McpAccountStorage *self,
 
204
    const McpAccountManager *am)
 
205
{
 
206
  gsize n;
 
207
  gchar *data;
 
208
  AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
 
209
  gboolean rval = FALSE;
 
210
 
 
211
  if (!adp->save)
 
212
    return TRUE;
 
213
 
 
214
  if (!_have_config ())
 
215
    _create_config ();
 
216
 
 
217
  data = g_key_file_to_data (adp->keyfile, &n, NULL);
 
218
  rval = g_file_set_contents (_conf_filename (), data, n, NULL);
 
219
  adp->save = !rval;
 
220
  g_free (data);
 
221
 
 
222
  return rval;
 
223
}
 
224
 
 
225
static GList *
 
226
_list (const McpAccountStorage *self,
 
227
    const McpAccountManager *am)
 
228
{
 
229
  gsize i;
 
230
  gsize n;
 
231
  GStrv accounts;
 
232
  GList *rval = NULL;
 
233
  AccountDiversionPlugin *adp = ACCOUNT_DIVERSION_PLUGIN (self);
 
234
 
 
235
  if (!_have_config ())
 
236
    _create_config ();
 
237
 
 
238
  if (!adp->loaded)
 
239
    adp->loaded = g_key_file_load_from_file (adp->keyfile, _conf_filename (),
 
240
        G_KEY_FILE_KEEP_COMMENTS, NULL);
 
241
 
 
242
  accounts = g_key_file_get_groups (adp->keyfile, &n);
 
243
 
 
244
  for (i = 0; i < n; i++)
 
245
    rval = g_list_prepend (rval, g_strdup (accounts[i]));
 
246
 
 
247
  g_strfreev (accounts);
 
248
 
 
249
  return rval;
 
250
}
 
251
 
 
252
static void
 
253
account_storage_iface_init (McpAccountStorageIface *iface,
 
254
    gpointer unused G_GNUC_UNUSED)
 
255
{
 
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);
 
259
 
 
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);
 
265
}
 
266
 
 
267
 
 
268
GObject *
 
269
mcp_plugin_ref_nth_object (guint n)
 
270
{
 
271
  DEBUG ("Initializing mcp-account-diversion-plugin (n=%u)", n);
 
272
 
 
273
  switch (n)
 
274
    {
 
275
    case 0:
 
276
      return g_object_new (account_diversion_plugin_get_type (), NULL);
 
277
 
 
278
    default:
 
279
      return NULL;
 
280
    }
 
281
}