summaryrefslogtreecommitdiff
path: root/tests/account-store-keyfile.c
blob: efa96b0a76cc2d1f20755e90e097515056be7bec (plain)
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
/*
 * MC account storage inspector: MC 5.0-compatible single keyfile backend
 *
 * 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 <glib.h>
#include <string.h>

#include "account-store-keyfile.h"

static const gchar *default_config (void)
{
  return g_build_filename (g_get_user_data_dir (), "telepathy",
      "mission-control", "accounts.cfg", NULL);
}

static GKeyFile * default_keyfile (void)
{
  GError *error = NULL;
  static GKeyFile *keyfile = NULL;
  const gchar *path = NULL;

  if (keyfile != NULL)
    return keyfile;

  path = default_config ();

  keyfile = g_key_file_new ();

  if (!g_key_file_load_from_file (keyfile, path, 0, &error))
    {
      if (error != NULL)
        g_warning ("keyfile '%s' error: %s", path, error->message);
      else
        g_warning ("keyfile '%s' error: unknown error", path);

      g_key_file_free (keyfile);
      g_error_free (error);
      keyfile = NULL;
    }

  return keyfile;
}

static gboolean commit_changes (void)
{
  gsize n = 0;
  gchar *data = NULL;
  gboolean done = FALSE;
  GKeyFile *keyfile = default_keyfile ();
  const gchar *config = default_config ();

  data = g_key_file_to_data (keyfile, &n, NULL);
  done = g_file_set_contents (config, data, n, NULL);

  g_free (data);

  return done;
}

gchar *
keyfile_get (const gchar *account,
    const gchar *key)
{
  return g_key_file_get_string (default_keyfile (), account, key, NULL);
}

gboolean
keyfile_set (const gchar *account,
    const gchar *key,
    const gchar *value)
{
  GKeyFile *keyfile = NULL;

  keyfile = default_keyfile ();

  if (keyfile == NULL)
    return FALSE;

  g_key_file_set_string (keyfile, account, key, value);

  return commit_changes ();
}

gboolean
keyfile_delete (const gchar *account)
{
  GKeyFile *keyfile = default_keyfile ();

  g_key_file_remove_group (keyfile, account, NULL);

  return commit_changes ();
}

gboolean
keyfile_exists (const gchar *account)
{
  return g_key_file_has_group (default_keyfile (), account);
}

GStrv
keyfile_list (void)
{
  return g_key_file_get_groups (default_keyfile (), NULL);
}