[rhythmbox] audioscrobbler: RBAudioscrobbler is constructed with username + session key



commit d4df5e1e24370cd90514c4d3a83b981a9028caae
Author: Jamie Nicol <jamie thenicols net>
Date:   Thu Jul 15 13:06:32 2010 +0100

    audioscrobbler: RBAudioscrobbler is constructed with username + session key
    
    instead of creating scrobbler without authentication info, and using the same
    scrobbler instance when user changes, create a new scrobbler instance each time
    a different user logs in. Cleaner interface and stops unwanted behaviour such
    as songs being queued for submission when user is logged out.

 .../rb-audioscrobbler-profile-source.c             |   74 +++++++++++---------
 .../rb-audioscrobbler-radio-source.c               |    1 +
 plugins/audioscrobbler/rb-audioscrobbler.c         |   70 ++++++++----------
 plugins/audioscrobbler/rb-audioscrobbler.h         |    8 +--
 4 files changed, 76 insertions(+), 77 deletions(-)
---
diff --git a/plugins/audioscrobbler/rb-audioscrobbler-profile-source.c b/plugins/audioscrobbler/rb-audioscrobbler-profile-source.c
index d0cd059..7b4617e 100644
--- a/plugins/audioscrobbler/rb-audioscrobbler-profile-source.c
+++ b/plugins/audioscrobbler/rb-audioscrobbler-profile-source.c
@@ -79,6 +79,10 @@ struct _RBAudioscrobblerProfileSourcePrivate {
 	GtkWidget *scrobbler_submit_count_label;
 	GtkWidget *scrobbler_submit_time_label;
 
+	GtkWidget *station_creator_type_combo;
+	GtkWidget *station_creator_arg_label;
+	GtkWidget *station_creator_arg_entry;
+
 	GtkWidget *recent_tracks_area;
 	GtkWidget *recent_tracks_table;
 	GtkWidget *top_tracks_area;
@@ -90,10 +94,6 @@ struct _RBAudioscrobblerProfileSourcePrivate {
 	GtkWidget *recommended_artists_area;
 	GtkWidget *recommended_artists_table;
 
-	GtkWidget *station_creator_type_combo;
-	GtkWidget *station_creator_arg_label;
-	GtkWidget *station_creator_arg_entry;
-
 	GHashTable *button_to_popup_menu_map;
 	GHashTable *popup_menu_to_data_map;
 
@@ -350,32 +350,16 @@ rb_audioscrobbler_profile_source_constructed (GObject *object)
 	                                                         rb_audioscrobbler_account_get_login_status (source->priv->account),
 	                                                         source);
 
-	/* create the scrobbler, if it is enabled */
+	/* scrobbling enabled gconf stuff */
 	scrobbling_enabled_conf_key = g_strdup_printf (CONF_AUDIOSCROBBLER_ENABLE_SCROBBLING,
 	                                               rb_audioscrobbler_service_get_name (source->priv->service));
 	source->priv->scrobbling_enabled_notification_id =
 		eel_gconf_notification_add (scrobbling_enabled_conf_key,
 				            (GConfClientNotifyFunc) rb_audioscrobbler_profile_source_scrobbling_enabled_changed_cb,
 				            source);
-	if (eel_gconf_get_boolean (scrobbling_enabled_conf_key)) {
-		source->priv->audioscrobbler =
-			rb_audioscrobbler_new (source->priv->service,
-				               RB_SHELL_PLAYER (rb_shell_get_player (shell)));
-		rb_audioscrobbler_set_authentication_details (source->priv->audioscrobbler,
-			                                      rb_audioscrobbler_account_get_username (source->priv->account),
-			                                      rb_audioscrobbler_account_get_session_key (source->priv->account));
-		g_signal_connect (source->priv->audioscrobbler,
-			          "authentication-error",
-			          (GCallback)rb_audioscrobbler_profile_source_scrobbler_authentication_error_cb,
-			          source);
-		g_signal_connect (source->priv->audioscrobbler,
-			          "statistics-changed",
-			          (GCallback)rb_audioscrobbler_profile_source_scrobbler_statistics_changed_cb,
-			          source);
-		rb_audioscrobbler_statistics_changed (source->priv->audioscrobbler);
-		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (source->priv->scrobbling_enabled_check),
-		                              TRUE);
-	}
+	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (source->priv->scrobbling_enabled_check),
+	                              eel_gconf_get_boolean (scrobbling_enabled_conf_key));
+
 
 	g_object_unref (shell);
 	g_free (scrobbling_enabled_conf_key);
@@ -674,10 +658,9 @@ rb_audioscrobbler_profile_source_scrobbling_enabled_changed_cb (GConfClient *cli
 		g_object_get (source, "shell", &shell, NULL);
 		source->priv->audioscrobbler =
 			rb_audioscrobbler_new (source->priv->service,
-				               RB_SHELL_PLAYER (rb_shell_get_player (shell)));
-		rb_audioscrobbler_set_authentication_details (source->priv->audioscrobbler,
-			                                      rb_audioscrobbler_account_get_username (source->priv->account),
-			                                      rb_audioscrobbler_account_get_session_key (source->priv->account));
+				               RB_SHELL_PLAYER (rb_shell_get_player (shell)),
+                                               rb_audioscrobbler_account_get_username (source->priv->account),
+			                       rb_audioscrobbler_account_get_session_key (source->priv->account));
 		g_signal_connect (source->priv->audioscrobbler,
 			          "authentication-error",
 			          (GCallback)rb_audioscrobbler_profile_source_scrobbler_authentication_error_cb,
@@ -699,6 +682,7 @@ rb_audioscrobbler_profile_source_login_status_change_cb (RBAudioscrobblerAccount
 	RBAudioscrobblerProfileSource *source;
 	const char *username;
 	const char *session_key;
+	char *scrobbling_enabled_conf_key;
 	char *label_text = NULL;
 	char *button_text = NULL;
 	gboolean show_login_bar;
@@ -709,11 +693,34 @@ rb_audioscrobbler_profile_source_login_status_change_cb (RBAudioscrobblerAccount
 	username = rb_audioscrobbler_account_get_username (source->priv->account);
 	session_key = rb_audioscrobbler_account_get_session_key (source->priv->account);
 
-	/* update the audioscrobbler with new authentication */
+	/* delete old scrobbler */
 	if (source->priv->audioscrobbler != NULL) {
-		rb_audioscrobbler_set_authentication_details (source->priv->audioscrobbler,
-			                                      username,
-			                                      session_key);
+		g_object_unref (source->priv->audioscrobbler);
+		source->priv->audioscrobbler = NULL;
+	}
+
+	/* create new scrobbler if new user has logged in and scrobbling is enabled */
+	scrobbling_enabled_conf_key = g_strdup_printf (CONF_AUDIOSCROBBLER_ENABLE_SCROBBLING,
+	                                               rb_audioscrobbler_service_get_name (source->priv->service));
+	if (status == RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_IN &&
+	    eel_gconf_get_boolean (scrobbling_enabled_conf_key)) {
+		RBShell *shell;
+		g_object_get (source, "shell", &shell, NULL);
+		source->priv->audioscrobbler =
+			rb_audioscrobbler_new (source->priv->service,
+				               RB_SHELL_PLAYER (rb_shell_get_player (shell)),
+                                               rb_audioscrobbler_account_get_username (source->priv->account),
+			                       rb_audioscrobbler_account_get_session_key (source->priv->account));
+		g_signal_connect (source->priv->audioscrobbler,
+			          "authentication-error",
+			          (GCallback)rb_audioscrobbler_profile_source_scrobbler_authentication_error_cb,
+			          source);
+		g_signal_connect (source->priv->audioscrobbler,
+			          "statistics-changed",
+			          (GCallback)rb_audioscrobbler_profile_source_scrobbler_statistics_changed_cb,
+			          source);
+		rb_audioscrobbler_statistics_changed (source->priv->audioscrobbler);
+		g_object_unref (shell);
 	}
 
 	/* set the new user details */
@@ -773,9 +780,10 @@ rb_audioscrobbler_profile_source_login_status_change_cb (RBAudioscrobblerAccount
 	if (show_profile == TRUE) {
 		gtk_widget_show (source->priv->profile_vbox);
 	} else {
-		gtk_widget_hide_all (source->priv->profile_vbox);
+		gtk_widget_hide (source->priv->profile_vbox);
 	}
 
+	g_free (scrobbling_enabled_conf_key);
 	g_free (label_text);
 	g_free (button_text);
 }
diff --git a/plugins/audioscrobbler/rb-audioscrobbler-radio-source.c b/plugins/audioscrobbler/rb-audioscrobbler-radio-source.c
index 6a3bb04..29f9420 100644
--- a/plugins/audioscrobbler/rb-audioscrobbler-radio-source.c
+++ b/plugins/audioscrobbler/rb-audioscrobbler-radio-source.c
@@ -280,6 +280,7 @@ rb_audioscrobbler_radio_source_class_init (RBAudioscrobblerRadioSourceClass *kla
 	object_class->set_property = rb_audioscrobbler_radio_source_set_property;
 
 	source_class = RB_SOURCE_CLASS (klass);
+	source_class->impl_can_rename = (RBSourceFeatureFunc) rb_true_function;
 	source_class->impl_can_copy = (RBSourceFeatureFunc) rb_false_function;
 	source_class->impl_can_delete = (RBSourceFeatureFunc) rb_false_function;
 	source_class->impl_can_pause = (RBSourceFeatureFunc) rb_false_function;
diff --git a/plugins/audioscrobbler/rb-audioscrobbler.c b/plugins/audioscrobbler/rb-audioscrobbler.c
index 8b1209f..6e9851c 100644
--- a/plugins/audioscrobbler/rb-audioscrobbler.c
+++ b/plugins/audioscrobbler/rb-audioscrobbler.c
@@ -187,7 +187,9 @@ enum
 {
 	PROP_0,
 	PROP_SERVICE,
-	PROP_SHELL_PLAYER
+	PROP_SHELL_PLAYER,
+	PROP_USERNAME,
+	PROP_SESSION_KEY
 };
 
 enum
@@ -260,6 +262,22 @@ rb_audioscrobbler_class_init (RBAudioscrobblerClass *klass)
 							      RB_TYPE_SHELL_PLAYER,
 							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
+	g_object_class_install_property (object_class,
+	                                 PROP_USERNAME,
+	                                 g_param_spec_string ("username",
+	                                                      "Username",
+	                                                      "Username of the user who is scrobbling data",
+	                                                      NULL,
+                                                              G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
+
+	g_object_class_install_property (object_class,
+	                                 PROP_SESSION_KEY,
+	                                 g_param_spec_string ("session-key",
+	                                                      "Session Key",
+	                                                      "Session key used to authenticate the user",
+	                                                      NULL,
+                                                              G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
+
 	/**
 	 * RBAudioscrobbler::authentication-error:
 	 * @account: the #RBAudioscrobblerAccount
@@ -408,11 +426,15 @@ rb_audioscrobbler_finalize (GObject *object)
 
 RBAudioscrobbler*
 rb_audioscrobbler_new (RBAudioscrobblerService *service,
-                       RBShellPlayer *shell_player)
+                       RBShellPlayer *shell_player,
+                       const char *username,
+                       const char *session_key)
 {
 	return g_object_new (RB_TYPE_AUDIOSCROBBLER,
 	                     "service", service,
 			     "shell-player", shell_player,
+	                     "username", username,
+	                     "session_key", session_key,
 			     NULL);
 }
 
@@ -436,6 +458,12 @@ rb_audioscrobbler_set_property (GObject *object,
 					 G_CALLBACK (rb_audioscrobbler_song_changed_cb),
 					 audioscrobbler, 0);
 		break;
+	case PROP_USERNAME:
+		audioscrobbler->priv->username = g_value_dup_string (value);
+		break;
+	case PROP_SESSION_KEY:
+		audioscrobbler->priv->session_key = g_value_dup_string (value);
+		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 		break;
@@ -460,22 +488,6 @@ rb_audioscrobbler_get_property (GObject *object,
 	}
 }
 
-void
-rb_audioscrobbler_set_authentication_details (RBAudioscrobbler *audioscrobbler,
-                                              const char *username,
-                                              const char *session_key)
-{
-	g_free (audioscrobbler->priv->username);
-	audioscrobbler->priv->username = g_strdup (username);
-
-	g_free (audioscrobbler->priv->session_key);
-	audioscrobbler->priv->session_key = g_strdup (session_key);
-
-	/* need a new handshake with new details */
-	audioscrobbler->priv->handshake = FALSE;
-	audioscrobbler->priv->handshake_next = 0;
-}
-
 /* emits the statistics-changed signal */
 void
 rb_audioscrobbler_statistics_changed (RBAudioscrobbler *audioscrobbler)
@@ -828,9 +840,7 @@ rb_audioscrobbler_should_handshake (RBAudioscrobbler *audioscrobbler)
 	/* Perform handshake if necessary. Only perform handshake if
 	 *   - we have no current handshake; AND
 	 *   - we have waited the appropriate amount of time between
-	 *     handshakes; AND
-	 *   - we have a non-empty username AND
-	 *   - we have a non-empty session key
+	 *     handshakes;
 	 */
 	if (audioscrobbler->priv->handshake) {
 		return FALSE;
@@ -843,24 +853,6 @@ rb_audioscrobbler_should_handshake (RBAudioscrobbler *audioscrobbler)
 		return FALSE;
 	}
 
-	if ((audioscrobbler->priv->username == NULL) ||
-	    (strcmp (audioscrobbler->priv->username, "") == 0)) {
-		rb_debug ("No username set");
-		return FALSE;
-	}
-
-	if ((audioscrobbler->priv->username == NULL) ||
-	    (strcmp (audioscrobbler->priv->username, "") == 0)) {
-		rb_debug ("No username set");
-		return FALSE;
-	}
-
-	if ((audioscrobbler->priv->session_key == NULL) ||
-	    (strcmp (audioscrobbler->priv->username, "") == 0)) {
-		rb_debug ("No session key set");
-		return FALSE;
-	}
-
 	return TRUE;
 }
 
diff --git a/plugins/audioscrobbler/rb-audioscrobbler.h b/plugins/audioscrobbler/rb-audioscrobbler.h
index 62c3fb8..e96fb7e 100644
--- a/plugins/audioscrobbler/rb-audioscrobbler.h
+++ b/plugins/audioscrobbler/rb-audioscrobbler.h
@@ -71,11 +71,9 @@ typedef struct
 GType			rb_audioscrobbler_get_type (void);
 
 RBAudioscrobbler *	rb_audioscrobbler_new (RBAudioscrobblerService *service,
-                                               RBShellPlayer *shell_player);
-
-void                    rb_audioscrobbler_set_authentication_details (RBAudioscrobbler *audioscrobbler,
-                                                                      const char *username,
-                                                                      const char *session_key);
+                                               RBShellPlayer *shell_player,
+                                               const char *username,
+                                               const char *session_key);
 
 void                    rb_audioscrobbler_statistics_changed (RBAudioscrobbler *audioscrobbler);
 



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