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

« back to all changes in this revision

Viewing changes to tests/account-store.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonny Lamb
  • Date: 2011-01-27 17:54:12 UTC
  • mto: (0.12.1 upstream) (7.1.4 maverick)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20110127175412-cijhp5z0763s11cy
Tags: upstream-5.7.2
ImportĀ upstreamĀ versionĀ 5.7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * MC account storage backend inspector
 
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
 
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <stdio.h>
 
27
#include <glib.h>
 
28
#include <glib-object.h>
 
29
 
 
30
#include "account-store-default.h"
 
31
 
 
32
#define DOCSTRING_A \
 
33
  "%s OP BACKEND ACCOUNT [KEY [VALUE]]\n\n"        \
 
34
  "  OP      := <get | set | del | has | list>\n"  \
 
35
  "  BACKEND := <"
 
36
 
 
37
#define DOCSTRING_B \
 
38
  ">\n"                                                                   \
 
39
  "  ACCOUNT := <MANAGER>/<PROTOCOL>/<ACCOUNT-UID>\n"                    \
 
40
  "  KEY     := <manager | protocol | DisplayName | param-<PARAMETER>>\n" \
 
41
  "  VALUE   := <STRING>\n\n"
 
42
 
 
43
#if ENABLE_LIBACCOUNTS_SSO
 
44
#include "account-store-libaccounts.h"
 
45
#endif
 
46
 
 
47
typedef struct {
 
48
  const gchar *name;
 
49
  gchar *  (*get) (const gchar *account, const gchar *key);
 
50
  gboolean (*set) (const gchar *account, const gchar *key, const gchar *value);
 
51
  gboolean (*delete) (const gchar *account);
 
52
  gboolean (*exists) (const gchar *account);
 
53
  GStrv    (*list) (void);
 
54
} Backend;
 
55
 
 
56
typedef enum {
 
57
  OP_UNKNOWN,
 
58
  OP_GET,
 
59
  OP_SET,
 
60
  OP_DELETE,
 
61
  OP_EXISTS,
 
62
  OP_LIST,
 
63
} Operation;
 
64
 
 
65
const Backend backends[] = {
 
66
  { "default",
 
67
    default_get,
 
68
    default_set,
 
69
    default_delete,
 
70
    default_exists,
 
71
    default_list, },
 
72
 
 
73
#if ENABLE_LIBACCOUNTS_SSO
 
74
  { "libaccounts",
 
75
    libaccounts_get,
 
76
    libaccounts_set,
 
77
    libaccounts_delete,
 
78
    libaccounts_exists,
 
79
    libaccounts_list, },
 
80
#endif
 
81
 
 
82
  { NULL }
 
83
};
 
84
 
 
85
static void usage (const gchar *name, const gchar *fmt,
 
86
    ...) G_GNUC_NORETURN;
 
87
 
 
88
#if ENABLE_GNOME_KEYRING
 
89
#include <gnome-keyring.h>
 
90
 
 
91
static void
 
92
setup_default_keyring (void)
 
93
{
 
94
  GnomeKeyringResult result;
 
95
 
 
96
  g_debug ("Setting default keyring to: %s", g_getenv ("MC_KEYRING_NAME"));
 
97
 
 
98
  if (g_getenv ("MC_KEYRING_NAME") != NULL)
 
99
  {
 
100
      const gchar *keyring_name = g_getenv ("MC_KEYRING_NAME");
 
101
 
 
102
      g_debug ("MC Keyring name: %s", keyring_name);
 
103
 
 
104
      if ((result = gnome_keyring_set_default_keyring_sync (keyring_name)) ==
 
105
           GNOME_KEYRING_RESULT_OK)
 
106
      {
 
107
          g_debug ("Successfully set up temporary keyring %s for tests",
 
108
                   keyring_name);
 
109
      }
 
110
      else
 
111
      {
 
112
          g_warning ("Failed to set %s as the default keyring: %s",
 
113
                     keyring_name, gnome_keyring_result_to_message (result));
 
114
      }
 
115
  }
 
116
}
 
117
#endif
 
118
 
 
119
int main (int argc, char **argv)
 
120
{
 
121
  int i;
 
122
  const gchar *op_name = NULL;
 
123
  const gchar *backend = NULL;
 
124
  const gchar *account = NULL;
 
125
  const gchar *setting = NULL;
 
126
  const gchar *value = NULL;
 
127
  const Backend *store = NULL;
 
128
  Operation op = OP_UNKNOWN;
 
129
  gchar *output = NULL;
 
130
  gboolean success = FALSE;
 
131
 
 
132
  g_type_init ();
 
133
  g_set_application_name (argv[0]);
 
134
 
 
135
#if ENABLE_GNOME_KEYRING
 
136
  setup_default_keyring ();
 
137
#endif
 
138
 
 
139
  if (argc < 3)
 
140
    usage (argv[0], "");
 
141
 
 
142
  op_name = argv[1];
 
143
  backend = argv[2];
 
144
 
 
145
  for (i = 0; backends[i].name != NULL; i++)
 
146
    {
 
147
      if (g_str_equal (backends[i].name, backend))
 
148
        {
 
149
          store = &backends[i];
 
150
          break;
 
151
        }
 
152
    }
 
153
 
 
154
  if (store == NULL)
 
155
    usage (argv[0], "No such backend %s", backend);
 
156
 
 
157
  if (g_str_equal (op_name, "get"))
 
158
    op = OP_GET;
 
159
  else if (g_str_equal (op_name, "set"))
 
160
    op = OP_SET;
 
161
  else if (g_str_equal (op_name, "del"))
 
162
    op = OP_DELETE;
 
163
  else if (g_str_equal (op_name, "has"))
 
164
    op = OP_EXISTS;
 
165
  else if (g_str_equal (op_name, "list"))
 
166
    op = OP_LIST;
 
167
 
 
168
  switch (op)
 
169
    {
 
170
      case OP_SET:
 
171
 
 
172
        if (argc >= 6)
 
173
          value = argv[5];
 
174
 
 
175
      case OP_GET:
 
176
 
 
177
        if (argc < 5)
 
178
          usage (argv[0], "op '%s' requires an account and key", op_name);
 
179
 
 
180
        account = argv[3];
 
181
        setting = argv[4];
 
182
 
 
183
        if (account == NULL || *account == '\0')
 
184
          usage (argv[0], "op '%s' requires an account", op_name);
 
185
 
 
186
        if (setting == NULL || *setting == '\0')
 
187
          usage (argv[0], "op '%s' requires a key", op_name);
 
188
 
 
189
        break;
 
190
 
 
191
      case OP_DELETE:
 
192
      case OP_EXISTS:
 
193
 
 
194
        if (argc < 4)
 
195
          usage (argv[0], "op '%s' requires an account", op_name);
 
196
 
 
197
        account = argv[3];
 
198
        break;
 
199
 
 
200
      case OP_LIST:
 
201
        if (argc < 3)
 
202
          usage (argv[0], "op '%s' requires an backend", op_name);
 
203
        break;
 
204
 
 
205
      case OP_UNKNOWN:
 
206
        usage (argv[0], "Unknown operation: %s", op_name);
 
207
    }
 
208
 
 
209
  /* if we got this far, we have all the args we need: */
 
210
  switch (op)
 
211
    {
 
212
      GStrv list;
 
213
 
 
214
      case OP_GET:
 
215
        output = store->get (account, setting);
 
216
        success = output != NULL;
 
217
        break;
 
218
 
 
219
      case OP_SET:
 
220
        success = store->set (account, setting, value);
 
221
        output = g_strdup_printf ("%s.%s set to '%s' in %s",
 
222
            account, setting, value, store->name);
 
223
        break;
 
224
 
 
225
      case OP_DELETE:
 
226
        success = store->delete (account);
 
227
        output = g_strdup_printf ("%s deleted from %s", account, store->name);
 
228
        break;
 
229
 
 
230
      case OP_EXISTS:
 
231
        success = store->exists (account);
 
232
        if (success)
 
233
          output = g_strdup_printf ("Exists in %s", store->name);
 
234
        break;
 
235
 
 
236
      case OP_LIST:
 
237
        list = store->list ();
 
238
        output = g_strjoinv ("\n", list);
 
239
        g_strfreev (list);
 
240
        break;
 
241
 
 
242
      default:
 
243
        output = g_strdup ("Unknown operation");
 
244
    }
 
245
 
 
246
  if (output != NULL)
 
247
    printf ("%s\n", output);
 
248
 
 
249
  g_free (output);
 
250
 
 
251
  return success ? 0 : 1;
 
252
}
 
253
 
 
254
static void
 
255
usage (const gchar *name, const gchar *fmt, ...)
 
256
{
 
257
  guint i;
 
258
  va_list ap;
 
259
 
 
260
  fprintf (stderr, DOCSTRING_A, name);
 
261
 
 
262
  fprintf (stderr, "%s", backends[0].name);
 
263
 
 
264
  for (i = 1; backends[i].name != NULL; i++)
 
265
    fprintf (stderr, " | %s", backends[i].name);
 
266
 
 
267
  fprintf (stderr, DOCSTRING_B);
 
268
 
 
269
  va_start (ap, fmt);
 
270
  vfprintf (stderr, fmt, ap);
 
271
  va_end (ap);
 
272
 
 
273
  exit (1);
 
274
}