[rhythmbox] audioscrobbler: Account loads and saves username using gconf



commit 93deaa3594955a2212bcc2664cf3e94393ec33af
Author: Jamie Nicol <jamie thenicols net>
Date:   Tue May 25 18:35:51 2010 +0100

    audioscrobbler: Account loads and saves username using gconf
    
    Username is shown in configuration dialog and any edits are saved.
    Functionality moved from RBAudioscrobbler to RBAudioscrobblerAccount

 plugins/audioscrobbler/audioscrobbler-prefs.ui     |    4 +-
 plugins/audioscrobbler/rb-audioscrobbler-account.c |  118 ++++++++++++++++++++
 plugins/audioscrobbler/rb-audioscrobbler-account.h |    5 +
 3 files changed, 125 insertions(+), 2 deletions(-)
---
diff --git a/plugins/audioscrobbler/audioscrobbler-prefs.ui b/plugins/audioscrobbler/audioscrobbler-prefs.ui
index 68eef9d..2ac11d3 100644
--- a/plugins/audioscrobbler/audioscrobbler-prefs.ui
+++ b/plugins/audioscrobbler/audioscrobbler-prefs.ui
@@ -63,8 +63,8 @@
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="invisible_char">&#x25CF;</property>
-                        <signal name="focus_out_event" handler="rb_audioscrobbler_username_entry_focus_out_event_cb"/>
-                        <signal name="activate" handler="rb_audioscrobbler_username_entry_activate_cb"/>
+                        <signal name="focus_out_event" handler="rb_audioscrobbler_account_username_entry_focus_out_event_cb"/>
+                        <signal name="activate" handler="rb_audioscrobbler_account_username_entry_activate_cb"/>
                       </object>
                       <packing>
                         <property name="position">1</property>
diff --git a/plugins/audioscrobbler/rb-audioscrobbler-account.c b/plugins/audioscrobbler/rb-audioscrobbler-account.c
index ebb4047..b2fb29c 100644
--- a/plugins/audioscrobbler/rb-audioscrobbler-account.c
+++ b/plugins/audioscrobbler/rb-audioscrobbler-account.c
@@ -26,6 +26,11 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  */
 
+#include <string.h>
+
+#include <gconf/gconf.h>
+#include "eel-gconf-extensions.h"
+
 #include "rb-audioscrobbler-account.h"
 #include "rb-builder-helpers.h"
 #include "rb-debug.h"
@@ -36,11 +41,17 @@ struct _RBAudioscrobblerAccountPrivate
 {
 	RBShell *shell;
 
+	/* Authentication info */
+	gchar* username;
+
 	/* Widgets for the prefs pane */
 	GtkWidget *config_widget;
 	GtkWidget *username_entry;
 	GtkWidget *username_label;
 	GtkWidget *auth_link;
+
+	/* Preference notifications */
+	guint notification_username_id;
 };
 
 #define RB_AUDIOSCROBBLER_ACCOUNT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RB_TYPE_AUDIOSCROBBLER_ACCOUNT, RBAudioscrobblerAccountPrivate))
@@ -56,6 +67,14 @@ static void	     rb_audioscrobbler_account_set_property (GObject *object,
 static void          rb_audioscrobbler_account_dispose (GObject *object);
 static void          rb_audioscrobbler_account_finalize (GObject *object);
 
+static void          rb_audioscrobbler_account_import_settings (RBAudioscrobblerAccount *account);
+static void          rb_audioscrobbler_account_preferences_sync (RBAudioscrobblerAccount *account);
+
+static void          rb_audioscrobbler_account_gconf_changed_cb (GConfClient *client,
+                                                                 guint cnxn_id,
+                                                                 GConfEntry *entry,
+                                                                 RBAudioscrobblerAccount *account);
+
 enum
 {
 	PROP_0,
@@ -97,17 +116,43 @@ static void
 rb_audioscrobbler_account_init (RBAudioscrobblerAccount *account)
 {
 	account->priv = RB_AUDIOSCROBBLER_ACCOUNT_GET_PRIVATE (account);
+
+	account->priv->username = NULL;
+
+	rb_audioscrobbler_account_import_settings (account);
+
+	account->priv->notification_username_id =
+		eel_gconf_notification_add (CONF_AUDIOSCROBBLER_USERNAME,
+		                            (GConfClientNotifyFunc) rb_audioscrobbler_account_gconf_changed_cb,
+		                            account);
+
+	rb_audioscrobbler_account_preferences_sync (account);
 }
 
 static void
 rb_audioscrobbler_account_dispose (GObject *object)
 {
+	RBAudioscrobblerAccount *account;
+
+	account = RB_AUDIOSCROBBLER_ACCOUNT (object);
+
+	if (account->priv->notification_username_id != 0) {
+		eel_gconf_notification_remove (account->priv->notification_username_id);
+		account->priv->notification_username_id = 0;
+	}
+
 	G_OBJECT_CLASS (rb_audioscrobbler_account_parent_class)->dispose (object);
 }
 
 static void
 rb_audioscrobbler_account_finalize (GObject *object)
 {
+	RBAudioscrobblerAccount *account;
+
+	account = RB_AUDIOSCROBBLER_ACCOUNT (object);
+
+	g_free (account->priv->username);
+
 	G_OBJECT_CLASS (rb_audioscrobbler_account_parent_class)->finalize (object);
 }
 
@@ -155,6 +200,29 @@ rb_audioscrobbler_account_set_property (GObject *object,
 	}
 }
 
+static void
+rb_audioscrobbler_account_import_settings (RBAudioscrobblerAccount *account)
+{
+	/* import gconf settings. */
+	g_free (account->priv->username);
+	account->priv->username = eel_gconf_get_string (CONF_AUDIOSCROBBLER_USERNAME);
+}
+
+static void
+rb_audioscrobbler_account_preferences_sync (RBAudioscrobblerAccount *account)
+{
+	char *v;
+
+	if (account->priv->config_widget == NULL)
+		return;
+
+	rb_debug ("Syncing data with preferences window");
+
+	v = account->priv->username;
+	gtk_entry_set_text (GTK_ENTRY (account->priv->username_entry),
+	                    v ? v : "");
+}
+
 GtkWidget *
 rb_audioscrobbler_account_get_config_widget (RBAudioscrobblerAccount *account,
                                              RBPlugin *plugin)
@@ -177,5 +245,55 @@ rb_audioscrobbler_account_get_config_widget (RBAudioscrobblerAccount *account,
 
 	rb_builder_boldify_label (builder, "audioscrobbler_label");
 
+	rb_audioscrobbler_account_preferences_sync (account);
+
 	return account->priv->config_widget;
 }
+
+static void
+rb_audioscrobbler_account_gconf_changed_cb (GConfClient *client,
+                                            guint cnxn_id,
+                                            GConfEntry *entry,
+                                            RBAudioscrobblerAccount *account)
+{
+	rb_debug ("GConf key updated: \"%s\"", entry->key);
+	if (strcmp (entry->key, CONF_AUDIOSCROBBLER_USERNAME) == 0) {
+		const char *username;
+
+		username = gconf_value_get_string (entry->value);
+		if (rb_safe_strcmp (username, account->priv->username) == 0) {
+			rb_debug ("username not modified");
+			return;
+		}
+
+		g_free (account->priv->username);
+		account->priv->username = NULL;
+
+		if (username != NULL) {
+			account->priv->username = g_strdup (username);
+		}
+
+		if (account->priv->username_entry) {
+			char *v = account->priv->username;
+			gtk_entry_set_text (GTK_ENTRY (account->priv->username_entry),
+					    v ? v : "");
+		}
+	} else {
+		rb_debug ("Unhandled GConf key updated: \"%s\"", entry->key);
+	}
+}
+
+void
+rb_audioscrobbler_account_username_entry_focus_out_event_cb (GtkWidget *widget,
+                                                             RBAudioscrobblerAccount *account)
+{
+	eel_gconf_set_string (CONF_AUDIOSCROBBLER_USERNAME,
+                              gtk_entry_get_text (GTK_ENTRY (widget)));
+}
+
+void
+rb_audioscrobbler_account_username_entry_activate_cb (GtkEntry *entry,
+                                                      RBAudioscrobblerAccount *account)
+{
+	gtk_widget_grab_focus (account->priv->auth_link);
+}
diff --git a/plugins/audioscrobbler/rb-audioscrobbler-account.h b/plugins/audioscrobbler/rb-audioscrobbler-account.h
index e958724..6526b56 100644
--- a/plugins/audioscrobbler/rb-audioscrobbler-account.h
+++ b/plugins/audioscrobbler/rb-audioscrobbler-account.h
@@ -65,6 +65,11 @@ RBAudioscrobblerAccount *       rb_audioscrobbler_account_new (RBShell *shell);
 GtkWidget *                     rb_audioscrobbler_account_get_config_widget (RBAudioscrobblerAccount *account,
                                                                              RBPlugin *plugin);
 
+void                            rb_audioscrobbler_account_username_entry_focus_out_event_cb (GtkWidget *widget,
+                                                                                             RBAudioscrobblerAccount *account);
+void                            rb_audioscrobbler_account_username_entry_activate_cb (GtkEntry *entry,
+                                                                                      RBAudioscrobblerAccount *account);
+
 G_END_DECLS
 
 #endif /* __RB_AUDIOSCROBBLER_ACCOUNT_H */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]