0
+#include <json-glib/json-glib.h>
0
+#include <json-glib/json-gobject.h>
0
+#include "tweet-config.h"
0
+#define TWEET_CONFIG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TWEET_TYPE_CONFIG, TweetConfigPrivate))
0
+struct _TweetConfigPrivate
0
+static guint config_signals[LAST_SIGNAL] = { 0, };
0
+static TweetConfig *default_config = NULL;
0
+G_DEFINE_TYPE (TweetConfig, tweet_config, G_TYPE_OBJECT);
0
+tweet_config_finalize (GObject *gobject)
0
+ TweetConfigPrivate *priv = TWEET_CONFIG (gobject)->priv;
0
+ g_free (priv->username);
0
+ g_free (priv->password);
0
+ G_OBJECT_CLASS (tweet_config_parent_class)->finalize (gobject);
0
+tweet_config_set_property (GObject *gobject,
0
+ TweetConfigPrivate *priv = TWEET_CONFIG (gobject)->priv;
0
+ g_free (priv->username);
0
+ priv->username = g_value_dup_string (value);
0
+ g_free (priv->password);
0
+ priv->password = g_value_dup_string (value);
0
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
0
+tweet_config_get_property (GObject *gobject,
0
+ TweetConfigPrivate *priv = TWEET_CONFIG (gobject)->priv;
0
+ g_value_set_string (value, priv->username);
0
+ g_value_set_string (value, priv->password);
0
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
0
+tweet_config_class_init (TweetConfigClass *klass)
0
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
0
+ g_type_class_add_private (klass, sizeof (TweetConfigPrivate));
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
+ g_object_class_install_property (gobject_class,
0
+ g_param_spec_string ("username",
0
+ g_object_class_install_property (gobject_class,
0
+ g_param_spec_string ("password",
0
+ config_signals[CHANGED] =
0
+ g_signal_new (g_intern_static_string ("changed"),
0
+ G_TYPE_FROM_CLASS (gobject_class),
0
+ G_STRUCT_OFFSET (TweetConfigClass, changed),
0
+ g_cclosure_marshal_VOID__VOID,
0
+tweet_config_init (TweetConfig *config)
0
+ config->priv = TWEET_CONFIG_GET_PRIVATE (config);
0
+tweet_config_get_default (void)
0
+ if (G_UNLIKELY (default_config == NULL))
0
+ filename = g_build_filename (g_get_user_config_dir (),
0
+ if (g_file_get_contents (filename, &contents, &length, &error))
0
+ default_config = TWEET_CONFIG (json_construct_gobject (TWEET_TYPE_CONFIG,
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
+ g_warning ("Unable to load configuration file at `%s': %s",
0
+ default_config = NULL;
0
+ /* no configuration object - fall back to an empty one */
0
+ default_config = g_object_new (TWEET_TYPE_CONFIG, NULL);
0
+ return default_config;
0
+tweet_config_get_username (TweetConfig *config)
0
+ g_return_val_if_fail (TWEET_IS_CONFIG (config), NULL);
0
+ return config->priv->username;
0
+tweet_config_get_password (TweetConfig *config)
0
+ g_return_val_if_fail (TWEET_IS_CONFIG (config), NULL);
0
+ return config->priv->password;
0
+tweet_config_save (TweetConfig *config)
0
+ conf_dir = g_build_filename (g_get_user_config_dir (), "tweet", NULL);
0
+ if (g_mkdir_with_parents (conf_dir, 0700) == -1)
0
+ g_warning ("Unable to create the configuration directory: %s",
0
+ filename = g_build_filename (conf_dir, "tweet.json", NULL);
0
+ buffer = json_serialize_gobject (G_OBJECT (config), &len);
0
+ g_warning ("Unable to serialize the configuration");
0
+ g_file_set_contents (filename, buffer, len, &error);
0
+ g_warning ("Unable to save configuration file: %s", error->message);
Comments
No one has commented yet.