public
Description: A Clutter-based Twitter client
Homepage: http://live.gnome.org/Tweet
Clone URL: git://github.com/ebassi/tweet.git
Search Repo:
Add configuration singleton for Tweet

TweetConfig is a singleton object that stores and loads the
configuration; it uses JSON as a serialization format for
the class properties.
Emmanuele Bassi (author)
Thu Apr 17 21:35:47 -0700 2008
commit  437c516113cd12b7e058668f002a03cccaa61c2f
tree    8475e73307d2121313035ccce7f5f144768b772d
parent  9966e1eda420cd0ba4dc854650fc0919156d4250
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,251 @@
0
+#ifdef HAVE_CONFIG_H
0
+#include "config.h"
0
+#endif
0
+
0
+#include <glib.h>
0
+#include <stdlib.h>
0
+#include <string.h>
0
+#include <errno.h>
0
+
0
+#include <json-glib/json-glib.h>
0
+#include <json-glib/json-gobject.h>
0
+
0
+#include "tweet-config.h"
0
+
0
+#define TWEET_CONFIG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TWEET_TYPE_CONFIG, TweetConfigPrivate))
0
+
0
+struct _TweetConfigPrivate
0
+{
0
+ gchar *username;
0
+ gchar *password;
0
+};
0
+
0
+enum
0
+{
0
+ PROP_0,
0
+
0
+ PROP_USERNAME,
0
+ PROP_PASSWORD
0
+};
0
+
0
+enum
0
+{
0
+ CHANGED,
0
+
0
+ LAST_SIGNAL
0
+};
0
+
0
+static guint config_signals[LAST_SIGNAL] = { 0, };
0
+
0
+static TweetConfig *default_config = NULL;
0
+
0
+G_DEFINE_TYPE (TweetConfig, tweet_config, G_TYPE_OBJECT);
0
+
0
+static void
0
+tweet_config_finalize (GObject *gobject)
0
+{
0
+ TweetConfigPrivate *priv = TWEET_CONFIG (gobject)->priv;
0
+
0
+ g_free (priv->username);
0
+ g_free (priv->password);
0
+
0
+ G_OBJECT_CLASS (tweet_config_parent_class)->finalize (gobject);
0
+}
0
+
0
+static void
0
+tweet_config_set_property (GObject *gobject,
0
+ guint prop_id,
0
+ const GValue *value,
0
+ GParamSpec *pspec)
0
+{
0
+ TweetConfigPrivate *priv = TWEET_CONFIG (gobject)->priv;
0
+
0
+ switch (prop_id)
0
+ {
0
+ case PROP_USERNAME:
0
+ g_free (priv->username);
0
+ priv->username = g_value_dup_string (value);
0
+ break;
0
+
0
+ case PROP_PASSWORD:
0
+ g_free (priv->password);
0
+ priv->password = g_value_dup_string (value);
0
+ break;
0
+
0
+ default:
0
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
0
+ break;
0
+ }
0
+}
0
+
0
+static void
0
+tweet_config_get_property (GObject *gobject,
0
+ guint prop_id,
0
+ GValue *value,
0
+ GParamSpec *pspec)
0
+{
0
+ TweetConfigPrivate *priv = TWEET_CONFIG (gobject)->priv;
0
+
0
+ switch (prop_id)
0
+ {
0
+ case PROP_USERNAME:
0
+ g_value_set_string (value, priv->username);
0
+ break;
0
+
0
+ case PROP_PASSWORD:
0
+ g_value_set_string (value, priv->password);
0
+ break;
0
+
0
+ default:
0
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
0
+ break;
0
+ }
0
+}
0
+
0
+static void
0
+tweet_config_class_init (TweetConfigClass *klass)
0
+{
0
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
0
+
0
+ g_type_class_add_private (klass, sizeof (TweetConfigPrivate));
0
+
0
+ gobject_class->set_property = tweet_config_set_property;
0
+ gobject_class->get_property = tweet_config_get_property;
0
+ gobject_class->finalize = tweet_config_finalize;
0
+
0
+ g_object_class_install_property (gobject_class,
0
+ PROP_USERNAME,
0
+ g_param_spec_string ("username",
0
+ "Username",
0
+ "Username",
0
+ NULL,
0
+ G_PARAM_READWRITE));
0
+ g_object_class_install_property (gobject_class,
0
+ PROP_PASSWORD,
0
+ g_param_spec_string ("password",
0
+ "Password",
0
+ "Password",
0
+ NULL,
0
+ G_PARAM_READWRITE));
0
+
0
+ config_signals[CHANGED] =
0
+ g_signal_new (g_intern_static_string ("changed"),
0
+ G_TYPE_FROM_CLASS (gobject_class),
0
+ G_SIGNAL_RUN_LAST,
0
+ G_STRUCT_OFFSET (TweetConfigClass, changed),
0
+ NULL, NULL,
0
+ g_cclosure_marshal_VOID__VOID,
0
+ G_TYPE_NONE, 0);
0
+}
0
+
0
+static void
0
+tweet_config_init (TweetConfig *config)
0
+{
0
+ config->priv = TWEET_CONFIG_GET_PRIVATE (config);
0
+}
0
+
0
+TweetConfig *
0
+tweet_config_get_default (void)
0
+{
0
+ if (G_UNLIKELY (default_config == NULL))
0
+ {
0
+ gchar *filename;
0
+ gchar *contents;
0
+ gsize length;
0
+ GError *error = NULL;
0
+
0
+ filename = g_build_filename (g_get_user_config_dir (),
0
+ "tweet",
0
+ "tweet.json",
0
+ NULL);
0
+
0
+ if (g_file_get_contents (filename, &contents, &length, &error))
0
+ default_config = TWEET_CONFIG (json_construct_gobject (TWEET_TYPE_CONFIG,
0
+ contents, length,
0
+ &error));
0
+
0
+ if (error)
0
+ {
0
+ /* report every error that it's not "file not found" */
0
+ if (!(error->domain == G_FILE_ERROR &&
0
+ error->code == G_FILE_ERROR_NOENT))
0
+ {
0
+ g_warning ("Unable to load configuration file at `%s': %s",
0
+ filename,
0
+ error->message);
0
+ }
0
+
0
+ g_error_free (error);
0
+ default_config = NULL;
0
+ }
0
+
0
+ /* no configuration object - fall back to an empty one */
0
+ if (!default_config)
0
+ default_config = g_object_new (TWEET_TYPE_CONFIG, NULL);
0
+
0
+ g_free (contents);
0
+ g_free (filename);
0
+ }
0
+
0
+ return default_config;
0
+}
0
+
0
+G_CONST_RETURN gchar *
0
+tweet_config_get_username (TweetConfig *config)
0
+{
0
+ g_return_val_if_fail (TWEET_IS_CONFIG (config), NULL);
0
+
0
+ return config->priv->username;
0
+}
0
+
0
+G_CONST_RETURN gchar *
0
+tweet_config_get_password (TweetConfig *config)
0
+{
0
+ g_return_val_if_fail (TWEET_IS_CONFIG (config), NULL);
0
+
0
+ return config->priv->password;
0
+}
0
+
0
+void
0
+tweet_config_save (TweetConfig *config)
0
+{
0
+ gchar *conf_dir;
0
+ gchar *filename;
0
+ gchar *buffer;
0
+ gsize len;
0
+ GError *error;
0
+
0
+ conf_dir = g_build_filename (g_get_user_config_dir (), "tweet", NULL);
0
+ if (g_mkdir_with_parents (conf_dir, 0700) == -1)
0
+ {
0
+ if (errno != EEXIST)
0
+ g_warning ("Unable to create the configuration directory: %s",
0
+ g_strerror (errno));
0
+ g_free (conf_dir);
0
+ return;
0
+ }
0
+
0
+ filename = g_build_filename (conf_dir, "tweet.json", NULL);
0
+
0
+ buffer = json_serialize_gobject (G_OBJECT (config), &len);
0
+ if (!buffer)
0
+ {
0
+ g_warning ("Unable to serialize the configuration");
0
+ g_free (filename);
0
+ g_free (conf_dir);
0
+ return;
0
+ }
0
+
0
+ error = NULL;
0
+ g_file_set_contents (filename, buffer, len, &error);
0
+ if (error)
0
+ {
0
+ g_warning ("Unable to save configuration file: %s", error->message);
0
+ g_error_free (error);
0
+ }
0
+
0
+ g_free (buffer);
0
+ g_free (filename);
0
+ g_free (conf_dir);
0
+}
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,45 @@
0
+#ifndef __TWEET_CONFIG_H__
0
+#define __TWEET_CONFIG_H__
0
+
0
+#include <glib-object.h>
0
+
0
+G_BEGIN_DECLS
0
+
0
+#define TWEET_TYPE_CONFIG (tweet_config_get_type ())
0
+#define TWEET_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TWEET_TYPE_CONFIG, TweetConfig))
0
+#define TWEET_IS_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TWEET_TYPE_CONFIG))
0
+#define TWEET_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TWEET_TYPE_CONFIG, TweetConfigClass))
0
+#define TWEET_IS_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TWEET_TYPE_CONFIG))
0
+#define TWEET_CONFIG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TWEET_TYPE_CONFIG, TweetConfigClass))
0
+
0
+typedef struct _TweetConfig TweetConfig;
0
+typedef struct _TweetConfigPrivate TweetConfigPrivate;
0
+typedef struct _TweetConfigClass TweetConfigClass;
0
+
0
+struct _TweetConfig
0
+{
0
+ GObject parent_instance;
0
+
0
+ TweetConfigPrivate *priv;
0
+};
0
+
0
+struct _TweetConfigClass
0
+{
0
+ GObjectClass parent_class;
0
+
0
+ void (* changed) (TweetConfig *config);
0
+};
0
+
0
+GType tweet_config_get_type (void) G_GNUC_CONST;
0
+
0
+TweetConfig * tweet_config_get_default (void);
0
+
0
+G_CONST_RETURN gchar *tweet_config_get_username (TweetConfig *config);
0
+G_CONST_RETURN gchar *tweet_config_get_password (TweetConfig *config);
0
+
0
+void tweet_config_save (TweetConfig *config);
0
+
0
+G_END_DECLS
0
+
0
+#endif /* __TWEET_CONFIG_H__ */

Comments

    No one has commented yet.