[rhythmbox] audioscrobbler: login through Last.fm source instead of prefs dialog



commit db694ca721cbf571e369a1e5494cf2fe93e297e5
Author: Jamie Nicol <jamie thenicols net>
Date:   Thu Jun 3 17:23:30 2010 +0100

    audioscrobbler: login through Last.fm source instead of prefs dialog
    
    Create class RBAudioscrobblerProfileSource and move login UI to it.
    The source now owns account and audioscrobbler instances, and the
    plugin owns the source instance.

 plugins/audioscrobbler/Makefile.am                 |    2 +
 plugins/audioscrobbler/rb-audioscrobbler-account.c |  184 +++----------
 plugins/audioscrobbler/rb-audioscrobbler-account.h |   11 +-
 plugins/audioscrobbler/rb-audioscrobbler-plugin.c  |   65 +----
 .../rb-audioscrobbler-profile-source.c             |  304 ++++++++++++++++++++
 .../rb-audioscrobbler-profile-source.h             |   64 ++++
 6 files changed, 413 insertions(+), 217 deletions(-)
---
diff --git a/plugins/audioscrobbler/Makefile.am b/plugins/audioscrobbler/Makefile.am
index 9b1071a..0013552 100644
--- a/plugins/audioscrobbler/Makefile.am
+++ b/plugins/audioscrobbler/Makefile.am
@@ -7,6 +7,8 @@ libaudioscrobbler_la_SOURCES = \
 	rb-audioscrobbler-plugin.c			\
 	rb-audioscrobbler-entry.h			\
 	rb-audioscrobbler-entry.c			\
+	rb-audioscrobbler-profile-source.h		\
+	rb-audioscrobbler-profile-source.c		\
 	rb-audioscrobbler-account.h			\
 	rb-audioscrobbler-account.c			\
 	rb-audioscrobbler.c				\
diff --git a/plugins/audioscrobbler/rb-audioscrobbler-account.c b/plugins/audioscrobbler/rb-audioscrobbler-account.c
index 653db4f..1d97b8f 100644
--- a/plugins/audioscrobbler/rb-audioscrobbler-account.c
+++ b/plugins/audioscrobbler/rb-audioscrobbler-account.c
@@ -33,7 +33,6 @@
 #include <libsoup/soup.h>
 #include <libsoup/soup-gnome.h>
 
-#include "rb-audioscrobbler.h"
 #include "rb-audioscrobbler-account.h"
 #include "rb-builder-helpers.h"
 #include "rb-debug.h"
@@ -53,8 +52,6 @@
 
 struct _RBAudioscrobblerAccountPrivate
 {
-	RBShell *shell;
-
 	/* Authentication info */
 	gchar *username;
 	gchar *auth_token;
@@ -71,9 +68,6 @@ struct _RBAudioscrobblerAccountPrivate
 
 	/* HTTP requests session */
 	SoupSession *soup_session;
-
-	/* The scrobbler */
-	RBAudioscrobbler *audioscrobbler;
 };
 
 #define RB_AUDIOSCROBBLER_ACCOUNT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RB_TYPE_AUDIOSCROBBLER_ACCOUNT, RBAudioscrobblerAccountPrivate))
@@ -89,10 +83,6 @@ 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_on_login_status_change_cb (RBAudioscrobblerAccount *account,
-                                                                          RBAudioscrobblerAccountLoginStatus status,
-                                                                          gpointer user_data);
-
 static void          rb_audioscrobbler_account_load_session_settings (RBAudioscrobblerAccount *account);
 static void          rb_audioscrobbler_account_save_session_settings (RBAudioscrobblerAccount *account);
 
@@ -107,7 +97,9 @@ static void          rb_audioscrobbler_account_got_session_key_cb (SoupSession *
 enum
 {
 	PROP_0,
-	PROP_SHELL,
+	PROP_USERNAME,
+	PROP_SESSION_KEY,
+	PROP_LOGIN_STATUS
 };
 
 enum
@@ -128,17 +120,6 @@ rb_audioscrobbler_account_constructed (GObject *object)
 	RB_CHAIN_GOBJECT_METHOD (rb_audioscrobbler_account_parent_class, constructed, object);
 	account = RB_AUDIOSCROBBLER_ACCOUNT (object);
 
-	g_signal_connect (account,
-	                  "login-status-changed",
-	                  (GCallback)rb_audioscrobbler_account_on_login_status_change_cb,
-	                  NULL);
-
-	account->priv->audioscrobbler =
-		rb_audioscrobbler_new (RB_SHELL_PLAYER (rb_shell_get_player (account->priv->shell)),
-		                       LASTFM_SCROBBLER_URL,
-		                       LASTFM_API_KEY,
-		                       LASTFM_API_SECRET);
-
 	rb_audioscrobbler_account_load_session_settings (account);
 }
 
@@ -155,12 +136,29 @@ rb_audioscrobbler_account_class_init (RBAudioscrobblerAccountClass *klass)
 	object_class->set_property = rb_audioscrobbler_account_set_property;
 
 	g_object_class_install_property (object_class,
-	                                 PROP_SHELL,
-	                                 g_param_spec_object ("shell",
-	                                                      "RBShell",
-	                                                      "RBShell object",
-	                                                      RB_TYPE_SHELL,
-                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+	                                 PROP_USERNAME,
+	                                 g_param_spec_string ("username",
+	                                                      "Username",
+	                                                      "Username",
+	                                                      NULL,
+                                                              G_PARAM_READABLE));
+
+	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_READABLE));
+
+	g_object_class_install_property (object_class,
+	                                 PROP_LOGIN_STATUS,
+	                                 g_param_spec_enum ("login-status",
+	                                                     "Login Status",
+	                                                     "Login status",
+	                                                     RB_TYPE_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS,
+	                                                     RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_OUT,
+                                                             G_PARAM_READABLE));
 
 	/**
 	 * RBAudioscrobblerAccount::login-status-changed:
@@ -216,11 +214,6 @@ rb_audioscrobbler_account_dispose (GObject *object)
 		account->priv->soup_session = NULL;
 	}
 
-	if (account->priv->audioscrobbler != NULL) {
-		g_object_unref (account->priv->audioscrobbler);
-		account->priv->audioscrobbler = NULL;
-	}
-
 	G_OBJECT_CLASS (rb_audioscrobbler_account_parent_class)->dispose (object);
 }
 
@@ -239,10 +232,9 @@ rb_audioscrobbler_account_finalize (GObject *object)
 }
 
 RBAudioscrobblerAccount *
-rb_audioscrobbler_account_new (RBShell *shell)
+rb_audioscrobbler_account_new (void)
 {
 	return g_object_new (RB_TYPE_AUDIOSCROBBLER_ACCOUNT,
-	                     "shell", shell,
                              NULL);
 }
 
@@ -255,8 +247,14 @@ rb_audioscrobbler_account_get_property (GObject *object,
 	RBAudioscrobblerAccount *account = RB_AUDIOSCROBBLER_ACCOUNT (object);
 
 	switch (prop_id) {
-	case PROP_SHELL:
-		g_value_set_object (value, account->priv->shell);
+	case PROP_USERNAME:
+		g_value_set_string (value, account->priv->username);
+		break;
+	case PROP_SESSION_KEY:
+		g_value_set_string (value, account->priv->session_key);
+		break;
+	case PROP_LOGIN_STATUS:
+		g_value_set_enum (value, account->priv->login_status);
 		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -270,58 +268,13 @@ rb_audioscrobbler_account_set_property (GObject *object,
                                         const GValue *value,
                                         GParamSpec *pspec)
 {
-	RBAudioscrobblerAccount *account = RB_AUDIOSCROBBLER_ACCOUNT (object);
-
 	switch (prop_id) {
-	case PROP_SHELL:
-		account->priv->shell = g_value_get_object (value);
-		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 		break;
 	}
 }
 
-static void
-rb_audioscrobbler_account_on_login_status_change_cb (RBAudioscrobblerAccount *account,
-                                                     RBAudioscrobblerAccountLoginStatus status,
-                                                     gpointer user_data)
-{
-	char *status_text;
-	char *button_text;
-
-	if (account->priv->audioscrobbler != NULL) {
-		rb_audioscrobbler_set_authentication_details (account->priv->audioscrobbler,
-		                                              account->priv->username,
-		                                              account->priv->session_key);
-	}
-
-	if (account->priv->config_widget == NULL)
-		return;
-
-	switch (status) {
-	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_IN:
-		status_text = g_strdup_printf (_("Logged in as %s"), account->priv->username);
-		button_text = g_strdup (_("Logout"));
-		break;
-	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGING_IN:
-		status_text = g_strdup (_("Waiting for authentication..."));
-		button_text = g_strdup (_("Cancel"));
-		break;
-	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_OUT:
-	default:
-		status_text = g_strdup (_("You are not currently logged in"));
-		button_text = g_strdup (_("Login"));
-		break;
-	}
-
-	gtk_label_set_label (GTK_LABEL (account->priv->login_status_label), status_text);
-	gtk_button_set_label (GTK_BUTTON (account->priv->auth_button), button_text);
-
-	g_free (status_text);
-	g_free (button_text);
-}
-
 static gchar *
 mkmd5 (char *string)
 {
@@ -473,73 +426,6 @@ rb_audioscrobbler_account_logout (RBAudioscrobblerAccount *account)
 	               0, account->priv->login_status);
 }
 
-GtkWidget *
-rb_audioscrobbler_account_get_config_widget (RBAudioscrobblerAccount *account,
-                                             RBPlugin *plugin)
-{
-	GtkBuilder *builder;
-	char *builder_file;
-	char *status_text;
-	char *button_text;
-
-	if (account->priv->config_widget)
-		return account->priv->config_widget;
-
-	builder_file = rb_plugin_find_file (plugin, "audioscrobbler-prefs.ui");
-	g_assert (builder_file != NULL);
-	builder = rb_builder_load (builder_file, account);
-	g_free (builder_file);
-
-	account->priv->config_widget = GTK_WIDGET (gtk_builder_get_object (builder, "audioscrobbler_vbox"));
-	account->priv->login_status_label = GTK_WIDGET (gtk_builder_get_object (builder, "login_status_label"));
-	account->priv->auth_button = GTK_WIDGET (gtk_builder_get_object (builder, "auth_button"));
-
-	rb_builder_boldify_label (builder, "audioscrobbler_label");
-
-
-
-	if (account->priv->audioscrobbler != NULL) {
-		rb_audioscrobbler_set_authentication_details (account->priv->audioscrobbler,
-		                                              account->priv->username,
-		                                              account->priv->session_key);
-	}
-
-	switch (account->priv->login_status) {
-	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_IN:
-		status_text = g_strdup_printf (_("Logged in as %s"), account->priv->username);
-		button_text = g_strdup (_("Logout"));
-		break;
-	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGING_IN:
-		status_text = g_strdup (_("Waiting for authentication..."));
-		button_text = g_strdup (_("Cancel"));
-		break;
-	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_OUT:
-	default:
-		status_text = g_strdup (_("You are not currently logged in"));
-		button_text = g_strdup (_("Login"));
-		break;
-	}
-
-	gtk_label_set_label (GTK_LABEL (account->priv->login_status_label), status_text);
-	gtk_button_set_label (GTK_BUTTON (account->priv->auth_button), button_text);
-
-	g_free (status_text);
-	g_free (button_text);
-
-	return account->priv->config_widget;
-}
-
-void
-rb_audioscrobbler_account_auth_button_clicked_cb (GtkButton *button,
-                                                  RBAudioscrobblerAccount *account)
-{
-	if (account->priv->login_status == RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_OUT) {
-		rb_audioscrobbler_account_authenticate (account);
-	} else {
-		rb_audioscrobbler_account_logout (account);
-	}
-}
-
 /* private authentication functions */
 static void
 rb_audioscrobbler_account_request_token (RBAudioscrobblerAccount *account)
diff --git a/plugins/audioscrobbler/rb-audioscrobbler-account.h b/plugins/audioscrobbler/rb-audioscrobbler-account.h
index 96405a3..b5738a1 100644
--- a/plugins/audioscrobbler/rb-audioscrobbler-account.h
+++ b/plugins/audioscrobbler/rb-audioscrobbler-account.h
@@ -32,9 +32,6 @@
 #include <gtk/gtk.h>
 #include <glib.h>
 
-#include "rb-plugin.h"
-#include "rb-shell.h"
-
 G_BEGIN_DECLS
 
 typedef enum
@@ -73,13 +70,7 @@ typedef struct
 
 GType                           rb_audioscrobbler_account_get_type (void);
 
-RBAudioscrobblerAccount *       rb_audioscrobbler_account_new (RBShell *shell);
-
-GtkWidget *                     rb_audioscrobbler_account_get_config_widget (RBAudioscrobblerAccount *account,
-                                                                             RBPlugin *plugin);
-
-void                            rb_audioscrobbler_account_auth_button_clicked_cb (GtkButton *button,
-                                                                                  RBAudioscrobblerAccount *account);
+RBAudioscrobblerAccount *       rb_audioscrobbler_account_new (void);
 
 void                            rb_audioscrobbler_account_authenticate (RBAudioscrobblerAccount *account);
 void                            rb_audioscrobbler_account_logout (RBAudioscrobblerAccount *account);
diff --git a/plugins/audioscrobbler/rb-audioscrobbler-plugin.c b/plugins/audioscrobbler/rb-audioscrobbler-plugin.c
index 4f83722..cba893f 100644
--- a/plugins/audioscrobbler/rb-audioscrobbler-plugin.c
+++ b/plugins/audioscrobbler/rb-audioscrobbler-plugin.c
@@ -37,7 +37,7 @@
 #include <glib.h>
 #include <glib-object.h>
 
-#include "rb-audioscrobbler-account.h"
+#include "rb-audioscrobbler-profile-source.h"
 #include "rb-plugin.h"
 #include "rb-debug.h"
 #include "rb-shell.h"
@@ -55,8 +55,7 @@ typedef struct
 {
 	RBPlugin parent;
 
-	RBAudioscrobblerAccount *account;
-	GtkWidget *preferences;
+	RBSource *source;
 } RBAudioscrobblerPlugin;
 
 typedef struct
@@ -73,7 +72,6 @@ static void rb_audioscrobbler_plugin_init (RBAudioscrobblerPlugin *plugin);
 static void rb_audioscrobbler_plugin_finalize (GObject *object);
 static void impl_activate (RBPlugin *plugin, RBShell *shell);
 static void impl_deactivate (RBPlugin *plugin, RBShell *shell);
-static GtkWidget* impl_create_configure_dialog (RBPlugin *plugin);
 
 RB_PLUGIN_REGISTER(RBAudioscrobblerPlugin, rb_audioscrobbler_plugin)
 
@@ -87,7 +85,6 @@ rb_audioscrobbler_plugin_class_init (RBAudioscrobblerPluginClass *klass)
 
 	plugin_class->activate = impl_activate;
 	plugin_class->deactivate = impl_deactivate;
-	plugin_class->create_configure_dialog = impl_create_configure_dialog;
 }
 
 static void
@@ -99,15 +96,8 @@ rb_audioscrobbler_plugin_init (RBAudioscrobblerPlugin *plugin)
 static void
 rb_audioscrobbler_plugin_finalize (GObject *object)
 {
-	RBAudioscrobblerPlugin *plugin = RB_AUDIOSCROBBLER_PLUGIN (object);
-
 	rb_debug ("RBAudioscrobblerPlugin finalising");
 
-	g_assert (plugin->account == NULL);
-
-	if (plugin->preferences)
-		gtk_widget_destroy (plugin->preferences);
-
 	G_OBJECT_CLASS (rb_audioscrobbler_plugin_parent_class)->finalize (object);
 }
 
@@ -119,8 +109,9 @@ impl_activate (RBPlugin *bplugin,
 {
 	RBAudioscrobblerPlugin *plugin = RB_AUDIOSCROBBLER_PLUGIN (bplugin);
 
-	g_assert (plugin->account == NULL);
-	plugin->account = rb_audioscrobbler_account_new (shell);
+	plugin->source = rb_audioscrobbler_profile_source_new (shell, bplugin);
+
+	rb_shell_append_source (shell, plugin->source, NULL);
 }
 
 static void
@@ -129,48 +120,6 @@ impl_deactivate	(RBPlugin *bplugin,
 {
 	RBAudioscrobblerPlugin *plugin = RB_AUDIOSCROBBLER_PLUGIN (bplugin);
 
-	g_object_unref (plugin->account);
-	plugin->account = NULL;
+	rb_source_delete_thyself (plugin->source);
+	plugin->source = NULL;
 }
-
-static void
-preferences_response_cb (GtkWidget *dialog, gint response, RBPlugin *plugin)
-{
-	gtk_widget_hide (dialog);
-}
-
-static GtkWidget*
-impl_create_configure_dialog (RBPlugin *bplugin)
-{
-	RBAudioscrobblerPlugin *plugin = RB_AUDIOSCROBBLER_PLUGIN (bplugin);
-	if (plugin->account == NULL)
-		return NULL;
-
-	if (plugin->preferences == NULL) {
-		GtkWidget *widget;
-
-		widget = rb_audioscrobbler_account_get_config_widget (plugin->account, bplugin);
-
-		plugin->preferences = gtk_dialog_new_with_buttons (_("Last.fm Preferences"),
-		                                                         NULL,
-		                                                         GTK_DIALOG_DESTROY_WITH_PARENT,
-		                                                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
-		                                                         NULL);
-                gtk_dialog_set_has_separator (GTK_DIALOG (plugin->preferences), FALSE);
-                gtk_container_set_border_width (GTK_CONTAINER (plugin->preferences), 5);
-                gtk_window_set_resizable (GTK_WINDOW (plugin->preferences), FALSE);
-
-		g_signal_connect (G_OBJECT (plugin->preferences),
-				  "response",
-				  G_CALLBACK (preferences_response_cb),
-				  plugin);
-		gtk_widget_hide_on_delete (plugin->preferences);
-
-		gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (plugin->preferences))),
-				   widget);
-	}
-
-	gtk_widget_show_all (plugin->preferences);
-	return plugin->preferences;
-}
-
diff --git a/plugins/audioscrobbler/rb-audioscrobbler-profile-source.c b/plugins/audioscrobbler/rb-audioscrobbler-profile-source.c
new file mode 100644
index 0000000..06f4a16
--- /dev/null
+++ b/plugins/audioscrobbler/rb-audioscrobbler-profile-source.c
@@ -0,0 +1,304 @@
+/*
+ * rb-audioscrobbler-profile-source.c
+ *
+ * Copyright (C) 2010 Jamie Nicol <jamie thenicols net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * The Rhythmbox authors hereby grant permission for non-GPL compatible
+ * GStreamer plugins to be used and distributed together with GStreamer
+ * and Rhythmbox. This permission is above and beyond the permissions granted
+ * by the GPL license by which Rhythmbox is covered. If you modify this code
+ * you may extend this exception to your version of the code, but you are not
+ * obligated to do so. If you do not wish to do so, delete this exception
+ * statement from your version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
+ */
+
+#include <glib/gi18n.h>
+
+#include "rb-audioscrobbler-profile-source.h"
+#include "rb-audioscrobbler.h"
+#include "rb-audioscrobbler-account.h"
+#include "rb-debug.h"
+#include "rb-util.h"
+
+/* this API key belongs to Jamie Nicol <jamie thenicols net>
+   generated May 2010 for use in the audioscrobbler plugin */
+#define LASTFM_API_KEY "0337ff3c59299b6a31d75164041860b6"
+#define LASTFM_API_SECRET "776c85a04a445efa8f9ed7705473c606"
+#define LASTFM_API_URL "http://ws.audioscrobbler.com/2.0/";
+#define LASTFM_AUTH_URL "http://www.last.fm/api/auth/";
+#define LASTFM_SCROBBLER_URL "http://post.audioscrobbler.com/";
+
+struct _RBAudioscrobblerProfileSourcePrivate {
+	RBAudioscrobblerAccount *account;
+	RBAudioscrobbler *audioscrobbler;
+
+	GtkWidget *main_vbox;
+
+	GtkWidget *login_bar;
+	GtkWidget *login_status_label;
+	GtkWidget *login_response_button;
+};
+
+#define RB_AUDIOSCROBBLER_PROFILE_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RB_TYPE_AUDIOSCROBBLER_PROFILE_SOURCE, RBAudioscrobblerProfileSourcePrivate))
+
+static void rb_audioscrobbler_profile_source_class_init (RBAudioscrobblerProfileSourceClass *klass);
+static void rb_audioscrobbler_profile_source_init (RBAudioscrobblerProfileSource *source);
+static void rb_audioscrobbler_profile_source_constructed (GObject *object);
+static void rb_audioscrobbler_profile_source_dispose (GObject* object);
+static void rb_audioscrobbler_profile_source_finalize (GObject *object);
+
+static void rb_audioscrobbler_profile_source_init_login_ui (RBAudioscrobblerProfileSource *source);
+static void rb_audioscrobbler_profile_source_login_bar_response (GtkInfoBar *info_bar,
+                                                                 gint response_id,
+                                                                 gpointer user_data);
+
+static void rb_audioscrobbler_profile_source_login_status_change_cb (RBAudioscrobblerAccount *account,
+                                                                     RBAudioscrobblerAccountLoginStatus status,
+                                                                     gpointer user_data);
+
+G_DEFINE_TYPE (RBAudioscrobblerProfileSource, rb_audioscrobbler_profile_source, RB_TYPE_SOURCE)
+
+RBSource *
+rb_audioscrobbler_profile_source_new (RBShell *shell, RBPlugin *plugin)
+{
+	RBSource *source;
+	RhythmDB *db;
+	RhythmDBEntryType entry_type;
+	gchar *icon_filename;
+	gint icon_size;
+	GdkPixbuf *icon_pixbuf;
+
+	g_object_get (shell, "db", &db, NULL);
+
+	entry_type = rhythmdb_entry_type_get_by_name (db, "audioscrobbler-radio-track");
+	if (entry_type == RHYTHMDB_ENTRY_TYPE_INVALID) {
+		entry_type = rhythmdb_entry_register_type (db, "audioscrobbler-radio-track");
+		entry_type->save_to_disk = FALSE;
+		entry_type->category = RHYTHMDB_ENTRY_NORMAL;
+	}
+
+	icon_filename = rb_plugin_find_file (plugin, "as-icon.png");
+	gtk_icon_size_lookup (GTK_ICON_SIZE_LARGE_TOOLBAR, &icon_size, NULL);
+	icon_pixbuf = gdk_pixbuf_new_from_file_at_size (icon_filename, icon_size, icon_size, NULL);
+
+	source = RB_SOURCE (g_object_new (RB_TYPE_AUDIOSCROBBLER_PROFILE_SOURCE,
+	                                  "shell", shell,
+	                                  "plugin", plugin,
+	                                  "name", _("Last.fm"),
+	                                  "source-group", RB_SOURCE_GROUP_LIBRARY,
+	                                  "entry-type", entry_type,
+	                                  "icon", icon_pixbuf,
+	                                  NULL));
+
+	rb_shell_register_entry_type_for_source (shell, source, entry_type);
+
+	g_object_unref (db);
+	g_free (icon_filename);
+	g_object_unref (icon_pixbuf);
+
+	return source;
+}
+
+static void
+rb_audioscrobbler_profile_source_class_init (RBAudioscrobblerProfileSourceClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+	object_class->constructed = rb_audioscrobbler_profile_source_constructed;
+	object_class->dispose = rb_audioscrobbler_profile_source_dispose;
+	object_class->finalize = rb_audioscrobbler_profile_source_finalize;
+
+	g_type_class_add_private (klass, sizeof (RBAudioscrobblerProfileSource));
+}
+
+static void
+rb_audioscrobbler_profile_source_init (RBAudioscrobblerProfileSource *source)
+{
+	source->priv = RB_AUDIOSCROBBLER_PROFILE_SOURCE_GET_PRIVATE (source);
+
+	/* create the UI */
+	source->priv->main_vbox = gtk_vbox_new (FALSE, 4);
+	gtk_box_pack_start (GTK_BOX (source), source->priv->main_vbox, TRUE, TRUE, 0);
+	gtk_widget_show_all (source->priv->main_vbox);
+
+	rb_audioscrobbler_profile_source_init_login_ui (source);
+}
+
+static void
+rb_audioscrobbler_profile_source_constructed (GObject *object)
+{
+	RBAudioscrobblerProfileSource *source;
+	RBShell *shell;
+	RBAudioscrobblerAccountLoginStatus login_status;
+
+	RB_CHAIN_GOBJECT_METHOD (rb_audioscrobbler_profile_source_parent_class, constructed, object);
+
+	source = RB_AUDIOSCROBBLER_PROFILE_SOURCE (object);
+	g_object_get (source, "shell", &shell, NULL);
+
+	/* create the account */
+	source->priv->account = rb_audioscrobbler_account_new ();
+	g_signal_connect (source->priv->account,
+	                  "login-status-changed",
+	                  (GCallback)rb_audioscrobbler_profile_source_login_status_change_cb,
+	                  source);
+
+	/* create the scrobbler */
+	source->priv->audioscrobbler =
+		rb_audioscrobbler_new (RB_SHELL_PLAYER (rb_shell_get_player (shell)),
+		                       LASTFM_SCROBBLER_URL,
+		                       LASTFM_API_KEY,
+		                       LASTFM_API_SECRET);
+
+	/* sync account settings to UI and scrobbler settings */
+	g_object_get (source->priv->account, "login-status", &login_status, NULL);
+	rb_audioscrobbler_profile_source_login_status_change_cb (source->priv->account,
+	                                                         login_status,
+	                                                         source);
+
+	g_object_unref (shell);
+}
+
+static void
+rb_audioscrobbler_profile_source_dispose (GObject* object)
+{
+	RBAudioscrobblerProfileSource *source;
+
+	source = RB_AUDIOSCROBBLER_PROFILE_SOURCE (object);
+
+	if (source->priv->audioscrobbler != NULL) {
+		g_object_unref (source->priv->audioscrobbler);
+		source->priv->audioscrobbler = NULL;
+	}
+
+	if (source->priv->account != NULL) {
+		g_object_unref (source->priv->account);
+		source->priv->account = NULL;
+	}
+
+	G_OBJECT_CLASS (rb_audioscrobbler_profile_source_parent_class)->dispose (object);
+}
+
+static void
+rb_audioscrobbler_profile_source_finalize (GObject *object)
+{
+	RBAudioscrobblerProfileSource *source;
+
+	source = RB_AUDIOSCROBBLER_PROFILE_SOURCE (object);
+
+	G_OBJECT_CLASS (rb_audioscrobbler_profile_source_parent_class)->finalize (object);
+}
+
+static void
+rb_audioscrobbler_profile_source_init_login_ui (RBAudioscrobblerProfileSource *source)
+{
+	GtkWidget *content_area;
+
+	source->priv->login_bar = gtk_info_bar_new ();
+	source->priv->login_status_label = gtk_label_new ("");
+	source->priv->login_response_button = gtk_button_new ();
+	content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (source->priv->login_bar));
+	gtk_container_add (GTK_CONTAINER (content_area), source->priv->login_status_label);
+	source->priv->login_response_button =
+		gtk_info_bar_add_button (GTK_INFO_BAR (source->priv->login_bar),
+		                         "", GTK_RESPONSE_OK);
+	g_signal_connect (source->priv->login_bar,
+	                  "response",
+	                  G_CALLBACK (rb_audioscrobbler_profile_source_login_bar_response),
+	                  source);
+	gtk_box_pack_start (GTK_BOX (source->priv->main_vbox), source->priv->login_bar, FALSE, FALSE, 0);
+}
+
+static void
+rb_audioscrobbler_profile_source_login_bar_response (GtkInfoBar *info_bar,
+                                                     gint response_id,
+                                                     gpointer user_data)
+{
+	RBAudioscrobblerProfileSource *source;
+	RBAudioscrobblerAccountLoginStatus status;
+
+	source = RB_AUDIOSCROBBLER_PROFILE_SOURCE (user_data);
+
+	g_object_get (source->priv->account, "login-status", &status, NULL);
+
+	switch (status) {
+	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_OUT:
+		rb_audioscrobbler_account_authenticate (source->priv->account);
+		break;
+	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGING_IN:	
+	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_IN:
+		rb_audioscrobbler_account_logout (source->priv->account);
+		break;
+	default:
+		g_assert_not_reached ();
+		break;
+	}
+}
+static void
+rb_audioscrobbler_profile_source_login_status_change_cb (RBAudioscrobblerAccount *account,
+                                                         RBAudioscrobblerAccountLoginStatus status,
+                                                         gpointer user_data)
+{
+	RBAudioscrobblerProfileSource *source;
+	char *username;
+	char *session_key;
+	char *label_text;
+	char *button_text;
+
+	source = RB_AUDIOSCROBBLER_PROFILE_SOURCE (user_data);
+
+	g_object_get (account,
+	              "username", &username,
+	              "session-key", &session_key,
+	              NULL);
+
+	rb_audioscrobbler_set_authentication_details (source->priv->audioscrobbler,
+	                                              username,
+	                                              session_key);
+
+	switch (status) {
+	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_OUT:
+		gtk_widget_show_all (source->priv->login_bar);
+		label_text = g_strdup (_("You are not currently logged in."));
+		button_text = g_strdup (_("Login"));
+		gtk_info_bar_set_message_type (GTK_INFO_BAR (source->priv->login_bar), GTK_MESSAGE_INFO);
+		break;
+	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGING_IN:
+		gtk_widget_show_all (source->priv->login_bar);
+		label_text = g_strdup (_("Waiting for authentication..."));
+		button_text = g_strdup (_("Cancel"));
+		gtk_info_bar_set_message_type (GTK_INFO_BAR (source->priv->login_bar), GTK_MESSAGE_INFO);
+		break;
+	case RB_AUDIOSCROBBLER_ACCOUNT_LOGIN_STATUS_LOGGED_IN:
+		gtk_widget_show_all (source->priv->login_bar);
+		label_text = g_strdup_printf (_("Logged in as %s."), username);
+		button_text = g_strdup (_("Logout"));
+		gtk_info_bar_set_message_type (GTK_INFO_BAR (source->priv->login_bar), GTK_MESSAGE_INFO);
+		break;
+	default:
+		g_assert_not_reached ();
+		break;
+	}
+
+	gtk_label_set_label (GTK_LABEL (source->priv->login_status_label), label_text);
+	gtk_button_set_label (GTK_BUTTON (source->priv->login_response_button), button_text);
+
+	g_free (username);
+	g_free (session_key);
+	g_free (label_text);
+	g_free (button_text);
+}
diff --git a/plugins/audioscrobbler/rb-audioscrobbler-profile-source.h b/plugins/audioscrobbler/rb-audioscrobbler-profile-source.h
new file mode 100644
index 0000000..7acf09a
--- /dev/null
+++ b/plugins/audioscrobbler/rb-audioscrobbler-profile-source.h
@@ -0,0 +1,64 @@
+/*
+ * rb-audioscrobbler-profile-source.h
+ *
+ * Copyright (C) 2010 Jamie Nicol <jamie thenicols net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * The Rhythmbox authors hereby grant permission for non-GPL compatible
+ * GStreamer plugins to be used and distributed together with GStreamer
+ * and Rhythmbox. This permission is above and beyond the permissions granted
+ * by the GPL license by which Rhythmbox is covered. If you modify this code
+ * you may extend this exception to your version of the code, but you are not
+ * obligated to do so. If you do not wish to do so, delete this exception
+ * statement from your version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
+ */
+
+#ifndef __RB_AUDIOSCROBBLER_PROFILE_SOURCE_H
+#define __RB_AUDIOSCROBBLER_PROFILE_SOURCE_H
+
+#include "rb-source.h"
+#include "rb-shell.h"
+#include "rb-plugin.h"
+
+G_BEGIN_DECLS
+
+#define RB_TYPE_AUDIOSCROBBLER_PROFILE_SOURCE         (rb_audioscrobbler_profile_source_get_type ())
+#define RB_AUDIOSCROBBLER_PROFILE_SOURCE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_AUDIOSCROBBLER_PROFILE_SOURCE, RBAudioscrobblerProfileSource))
+#define RB_AUDIOSCROBBLER_PROFILE_SOURCE_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_AUDIOSCROBBLER_PROFILE_SOURCE, RBAudioscrobblerProfileSourceClass))
+#define RB_IS_AUDIOSCROBBLER_PROFILE_SOURCE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_AUDIOSCROBBLER_PROFILE_SOURCE))
+#define RB_IS_AUDIOSCROBBLER_PROFILE_SOURCE_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_AUDIOSCROBBLER_PROFILE_SOURCE))
+#define RB_AUDIOSCROBBLER_PROFILE_SOURCE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_AUDIOSCROBBLER_PROFILE_SOURCE, RBAudioscrobblerProfileSourceClass))
+
+typedef struct _RBAudioscrobblerProfileSourcePrivate RBAudioscrobblerProfileSourcePrivate;
+
+typedef struct
+{
+	RBSource parent;
+
+	RBAudioscrobblerProfileSourcePrivate *priv;
+} RBAudioscrobblerProfileSource;
+
+typedef struct
+{
+	RBSourceClass parent_class;
+} RBAudioscrobblerProfileSourceClass;
+
+GType rb_audioscrobbler_profile_source_get_type (void);
+RBSource *rb_audioscrobbler_profile_source_new (RBShell *shell, RBPlugin *plugin);
+
+G_END_DECLS
+
+#endif /* __RB_AUDIOSCROBBLER_PROFILE_SOURCE_H */



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