We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Description: A Clutter-based Twitter client
Homepage: http://live.gnome.org/Tweet
Clone URL: git://github.com/ebassi/tweet.git
Emmanuele Bassi (author)
Thu Apr 17 21:35:47 -0700 2008
commit  437c516113cd12b7e058668f002a03cccaa61c2f
tree    8475e73307d2121313035ccce7f5f144768b772d
parent  9966e1eda420cd0ba4dc854650fc0919156d4250
tweet / src / tweet-config.c
100644 251 lines (203 sloc) 6.513 kb
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
 
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
 
#include <json-glib/json-glib.h>
#include <json-glib/json-gobject.h>
 
#include "tweet-config.h"
 
#define TWEET_CONFIG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TWEET_TYPE_CONFIG, TweetConfigPrivate))
 
struct _TweetConfigPrivate
{
  gchar *username;
  gchar *password;
};
 
enum
{
  PROP_0,
 
  PROP_USERNAME,
  PROP_PASSWORD
};
 
enum
{
  CHANGED,
 
  LAST_SIGNAL
};
 
static guint config_signals[LAST_SIGNAL] = { 0, };
 
static TweetConfig *default_config = NULL;
 
G_DEFINE_TYPE (TweetConfig, tweet_config, G_TYPE_OBJECT);
 
static void
tweet_config_finalize (GObject *gobject)
{
  TweetConfigPrivate *priv = TWEET_CONFIG (gobject)->priv;
 
  g_free (priv->username);
  g_free (priv->password);
 
  G_OBJECT_CLASS (tweet_config_parent_class)->finalize (gobject);
}
 
static void
tweet_config_set_property (GObject *gobject,
                           guint prop_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
  TweetConfigPrivate *priv = TWEET_CONFIG (gobject)->priv;
 
  switch (prop_id)
    {
    case PROP_USERNAME:
      g_free (priv->username);
      priv->username = g_value_dup_string (value);
      break;
 
    case PROP_PASSWORD:
      g_free (priv->password);
      priv->password = g_value_dup_string (value);
      break;
 
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
      break;
    }
}
 
static void
tweet_config_get_property (GObject *gobject,
                           guint prop_id,
                           GValue *value,
                           GParamSpec *pspec)
{
  TweetConfigPrivate *priv = TWEET_CONFIG (gobject)->priv;
 
  switch (prop_id)
    {
    case PROP_USERNAME:
      g_value_set_string (value, priv->username);
      break;
 
    case PROP_PASSWORD:
      g_value_set_string (value, priv->password);
      break;
 
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
      break;
    }
}
 
static void
tweet_config_class_init (TweetConfigClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
  g_type_class_add_private (klass, sizeof (TweetConfigPrivate));
 
  gobject_class->set_property = tweet_config_set_property;
  gobject_class->get_property = tweet_config_get_property;
  gobject_class->finalize = tweet_config_finalize;
 
  g_object_class_install_property (gobject_class,
                                   PROP_USERNAME,
                                   g_param_spec_string ("username",
                                                        "Username",
                                                        "Username",
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_PASSWORD,
                                   g_param_spec_string ("password",
                                                        "Password",
                                                        "Password",
                                                        NULL,
                                                        G_PARAM_READWRITE));
 
  config_signals[CHANGED] =
    g_signal_new (g_intern_static_string ("changed"),
                  G_TYPE_FROM_CLASS (gobject_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (TweetConfigClass, changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
}
 
static void
tweet_config_init (TweetConfig *config)
{
  config->priv = TWEET_CONFIG_GET_PRIVATE (config);
}
 
TweetConfig *
tweet_config_get_default (void)
{
  if (G_UNLIKELY (default_config == NULL))
    {
      gchar *filename;
      gchar *contents;
      gsize length;
      GError *error = NULL;
 
      filename = g_build_filename (g_get_user_config_dir (),
                                   "tweet",
                                   "tweet.json",
                                   NULL);
 
      if (g_file_get_contents (filename, &contents, &length, &error))
        default_config = TWEET_CONFIG (json_construct_gobject (TWEET_TYPE_CONFIG,
                                                               contents, length,
                                                               &error));
 
      if (error)
        {
          /* report every error that it's not "file not found" */
          if (!(error->domain == G_FILE_ERROR &&
                error->code == G_FILE_ERROR_NOENT))
            {
              g_warning ("Unable to load configuration file at `%s': %s",
                         filename,
                         error->message);
            }
 
          g_error_free (error);
          default_config = NULL;
        }
 
      /* no configuration object - fall back to an empty one */
      if (!default_config)
        default_config = g_object_new (TWEET_TYPE_CONFIG, NULL);
 
      g_free (contents);
      g_free (filename);
    }
 
  return default_config;
}
 
G_CONST_RETURN gchar *
tweet_config_get_username (TweetConfig *config)
{
  g_return_val_if_fail (TWEET_IS_CONFIG (config), NULL);
 
  return config->priv->username;
}
 
G_CONST_RETURN gchar *
tweet_config_get_password (TweetConfig *config)
{
  g_return_val_if_fail (TWEET_IS_CONFIG (config), NULL);
 
  return config->priv->password;
}
 
void
tweet_config_save (TweetConfig *config)
{
  gchar *conf_dir;
  gchar *filename;
  gchar *buffer;
  gsize len;
  GError *error;
 
  conf_dir = g_build_filename (g_get_user_config_dir (), "tweet", NULL);
  if (g_mkdir_with_parents (conf_dir, 0700) == -1)
    {
      if (errno != EEXIST)
        g_warning ("Unable to create the configuration directory: %s",
                   g_strerror (errno));
      g_free (conf_dir);
      return;
    }
 
  filename = g_build_filename (conf_dir, "tweet.json", NULL);
 
  buffer = json_serialize_gobject (G_OBJECT (config), &len);
  if (!buffer)
    {
      g_warning ("Unable to serialize the configuration");
      g_free (filename);
      g_free (conf_dir);
      return;
    }
 
  error = NULL;
  g_file_set_contents (filename, buffer, len, &error);
  if (error)
    {
      g_warning ("Unable to save configuration file: %s", error->message);
      g_error_free (error);
    }
 
  g_free (buffer);
  g_free (filename);
  g_free (conf_dir);
}