2006-10-07 James Livingston <doclivingston@gmail.com>
[rhythmbox.git] / plugins / audioscrobbler / rb-lastfm-source.c
blob8e438dc77b9cfbce786a9deb32387edf155f6aac
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * arch-tag: Implementation of last.fm station source object
5 * Copyright (C) 2006 Matt Novenstern <fisxoj@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* The author would like to extend thanks to Ian Holmes, author of Last Exit,
24 * an alternative last.fm player written in C#, the code of which was
25 * extraordinarily useful in the creation of this code
28 /* TODO List
29 * - if subscriber, make user radio (low priority)
30 * - "recommendation radio" with percentage setting (0=obscure, 100=popular)
31 * - watch username gconf entries, create/update neighbour station
35 #include <config.h>
37 #include <string.h>
39 #include <glib/gi18n.h>
40 #include <gtk/gtk.h>
41 #include <glade/glade.h>
43 #include <gconf/gconf-value.h>
45 #include <libsoup/soup.h>
46 #include <libsoup/soup-uri.h>
48 #include "md5.h"
50 #include "eel-gconf-extensions.h"
52 #include "rb-proxy-config.h"
53 #include "rb-preferences.h"
55 #include "rb-audioscrobbler.h"
56 #include "rb-lastfm-source.h"
58 #include "rhythmdb-query-model.h"
59 #include "rb-glade-helpers.h"
60 #include "rb-stock-icons.h"
61 #include "rb-entry-view.h"
62 #include "rb-property-view.h"
63 #include "rb-util.h"
64 #include "rb-file-helpers.h"
65 #include "rb-preferences.h"
66 #include "rb-dialog.h"
67 #include "rb-station-properties-dialog.h"
68 #include "rb-new-station-dialog.h"
69 #include "rb-debug.h"
70 #include "eel-gconf-extensions.h"
71 #include "rb-shell-player.h"
73 #define LASTFM_URL "http://ws.audioscrobbler.com"
74 #define PLATFORM_STRING "linux"
75 #define RB_LASTFM_VERSION "1.1.1"
76 #define EXTRA_URI_ENCODE_CHARS "&+"
79 static void rb_lastfm_source_class_init (RBLastfmSourceClass *klass);
80 static void rb_lastfm_source_init (RBLastfmSource *source);
81 static GObject *rb_lastfm_source_constructor (GType type, guint n_construct_properties,
82 GObjectConstructParam *construct_properties);
83 static void rb_lastfm_source_finalize (GObject *object);
84 static void rb_lastfm_source_set_property (GObject *object,
85 guint prop_id,
86 const GValue *value,
87 GParamSpec *pspec);
88 static void rb_lastfm_source_get_property (GObject *object,
89 guint prop_id,
90 GValue *value,
91 GParamSpec *pspec);
93 static void rb_lastfm_source_songs_view_sort_order_changed_cb (RBEntryView *view,
94 RBLastfmSource *source);
95 static void rb_lastfm_source_do_query (RBLastfmSource *source);
97 /* source-specific methods */
98 static void rb_lastfm_source_do_handshake (RBLastfmSource *source);
99 static char* rb_lastfm_source_get_playback_uri (RhythmDBEntry *entry, gpointer data);
100 static void rb_lastfm_perform (RBLastfmSource *lastfm,
101 const char *url,
102 char *post_data, /* this takes ownership */
103 SoupMessageCallbackFn response_handler);
104 static void rb_lastfm_message_cb (SoupMessage *req, gpointer user_data);
105 static void rb_lastfm_change_station (RBLastfmSource *source, const char *station);
107 static void rb_lastfm_proxy_config_changed_cb (RBProxyConfig *config,
108 RBLastfmSource *source);
109 static void rb_lastfm_source_drag_cb (GtkWidget *widget,
110 GdkDragContext *dc,
111 gint x, gint y,
112 GtkSelectionData *selection_data,
113 guint info, guint time,
114 RBLastfmSource *source);
116 static void rb_lastfm_source_dispose (GObject *object);
118 /* RBSource implementation methods */
119 static void impl_delete (RBSource *asource);
120 static GList *impl_get_ui_actions (RBSource *source);
121 static RBEntryView *impl_get_entry_view (RBSource *asource);
122 static void impl_get_status (RBSource *asource, char **text, char **progress_text, float *progress);
123 static RBSourceEOFType impl_handle_eos (RBSource *asource);
124 static gboolean impl_receive_drag (RBSource *source, GtkSelectionData *data);
125 static void impl_activate (RBSource *source);
127 static void rb_lastfm_source_new_station (char *uri, char *title, RBLastfmSource *source);
128 static void rb_lastfm_source_skip_track (GtkAction *action, RBLastfmSource *source);
129 static void rb_lastfm_source_love_track (GtkAction *action, RBLastfmSource *source);
130 static void rb_lastfm_source_ban_track (GtkAction *action, RBLastfmSource *source);
131 static char *rb_lastfm_source_title_from_uri (char *uri);
132 static void rb_lastfm_source_add_station_cb (GtkButton *button, gpointer *data);
134 static void rb_lastfm_source_new_song_cb (GObject *player_backend, gpointer data, RBLastfmSource *source);
135 static void rb_lastfm_song_changed_cb (RBShellPlayer *player, RhythmDBEntry *entry, RBLastfmSource *source);
137 static GValue *streaming_title_request_cb (RhythmDB *db,
138 RhythmDBEntry *entry,
139 RBLastfmSource *source);
140 static GValue *streaming_album_request_cb (RhythmDB *db,
141 RhythmDBEntry *entry,
142 RBLastfmSource *source);
143 static GValue *streaming_artist_request_cb (RhythmDB *db,
144 RhythmDBEntry *entry,
145 RBLastfmSource *source);
146 static void extra_metadata_gather_cb (RhythmDB *db,
147 RhythmDBEntry *entry,
148 GHashTable *data,
149 RBLastfmSource *source);
150 static void playing_source_changed_cb (RBShellPlayer *player,
151 RBSource *source,
152 RBLastfmSource *lastfm_source);
153 #ifdef HAVE_GSTREAMER_0_10
154 /* can't be bothered creating a whole header file just for this: */
155 GType rb_lastfm_src_get_type (void);
156 #endif
158 struct RBLastfmSourcePrivate
160 GtkWidget *vbox;
161 GtkWidget *paned;
162 GtkWidget *vbox2;
163 GtkWidget *hbox;
164 /*GtkWidget *tuner;*/
165 GtkWidget *txtbox;
166 GtkWidget *gobutton;
167 GtkWidget *typecombo;
168 GtkWidget *label;
169 RhythmDB *db;
171 GtkActionGroup *action_group;
173 RBEntryView *stations;
175 RBShellPlayer *shell_player;
176 RhythmDBEntryType entry_type;
177 char *session;
179 gboolean subscriber;
180 char *base_url;
181 char *base_path;
182 char *stream_url;
183 gboolean framehack;
184 char *update_url;
185 gboolean banned;
186 gboolean connected;
188 /*RhythmDBEntry *pending_entry;*/
189 char *streaming_title;
190 char *streaming_artist;
191 char *streaming_album;
193 enum {
194 OK = 0,
195 COMMUNICATING,
196 FAILED,
197 NO_ARTIST,
198 BANNED
199 } status;
201 SoupSession *soup_session;
202 RBProxyConfig *proxy_config;
204 gint buffering_id;
205 guint buffering;
208 G_DEFINE_TYPE (RBLastfmSource, rb_lastfm_source, RB_TYPE_SOURCE);
209 #define RB_LASTFM_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RB_TYPE_LASTFM_SOURCE, RBLastfmSourcePrivate))
211 enum
213 PROP_0,
214 PROP_ENTRY_TYPE,
215 PROP_PROXY_CONFIG
218 static GtkActionEntry rb_lastfm_source_actions [] =
220 { "LastfmSkipSong", GTK_STOCK_MEDIA_FORWARD, N_("Next Song"), NULL,
221 N_("Skip the current track"),
222 G_CALLBACK (rb_lastfm_source_skip_track) },
223 { "LastfmLoveSong", GTK_STOCK_ADD, N_("Love Song"), NULL,
224 N_("Mark this song as loved"),
225 G_CALLBACK (rb_lastfm_source_love_track) },
226 { "LastfmBanSong", GTK_STOCK_CANCEL, N_("Ban Song"), NULL,
227 N_("Ban the current track from being played again"),
228 G_CALLBACK (rb_lastfm_source_ban_track) }
231 static const GtkTargetEntry lastfm_drag_types[] = {
232 { "text/plain", 0, 0 },
233 { "_NETSCAPE_URL", 0, 1 }
236 static void
237 rb_lastfm_source_class_init (RBLastfmSourceClass *klass)
239 GObjectClass *object_class = G_OBJECT_CLASS (klass);
240 RBSourceClass *source_class = RB_SOURCE_CLASS (klass);
242 object_class->finalize = rb_lastfm_source_finalize;
243 object_class->dispose = rb_lastfm_source_dispose;
244 object_class->constructor = rb_lastfm_source_constructor;
246 object_class->set_property = rb_lastfm_source_set_property;
247 object_class->get_property = rb_lastfm_source_get_property;
249 source_class->impl_can_copy = (RBSourceFeatureFunc) rb_false_function;
250 source_class->impl_can_delete = (RBSourceFeatureFunc) rb_true_function;
251 source_class->impl_can_pause = (RBSourceFeatureFunc) rb_false_function;
252 source_class->impl_delete = impl_delete;
253 source_class->impl_get_entry_view = impl_get_entry_view;
254 source_class->impl_get_status = impl_get_status;
255 source_class->impl_get_ui_actions = impl_get_ui_actions;
256 source_class->impl_handle_eos = impl_handle_eos;
257 source_class->impl_receive_drag = impl_receive_drag;
258 source_class->impl_activate = impl_activate;
260 g_object_class_install_property (object_class,
261 PROP_ENTRY_TYPE,
262 g_param_spec_boxed ("entry-type",
263 "Entry type",
264 "Type of the entries which should be displayed by this source",
265 RHYTHMDB_TYPE_ENTRY_TYPE,
266 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
267 g_object_class_install_property (object_class,
268 PROP_PROXY_CONFIG,
269 g_param_spec_object ("proxy-config",
270 "RBProxyConfig",
271 "RBProxyConfig object",
272 RB_TYPE_PROXY_CONFIG,
273 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
274 g_type_class_add_private (klass, sizeof (RBLastfmSourcePrivate));
276 #ifdef HAVE_GSTREAMER_0_10
277 rb_lastfm_src_get_type ();
278 #endif
281 static void
282 rb_lastfm_source_init (RBLastfmSource *source)
284 gint size;
285 GdkPixbuf *pixbuf;
287 source->priv = RB_LASTFM_SOURCE_GET_PRIVATE (source);
289 source->priv->vbox = gtk_vbox_new (FALSE, 5);
291 gtk_container_add (GTK_CONTAINER (source), source->priv->vbox);
293 gtk_icon_size_lookup (GTK_ICON_SIZE_LARGE_TOOLBAR, &size, NULL);
294 pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
295 "stock_channel",
296 size,
297 0, NULL);
298 rb_source_set_pixbuf (RB_SOURCE (source), pixbuf);
299 if (pixbuf != NULL) {
300 g_object_unref (pixbuf);
304 static void
305 rb_lastfm_source_finalize (GObject *object)
307 RBLastfmSource *source;
309 g_return_if_fail (object != NULL);
310 g_return_if_fail (RB_IS_LASTFM_SOURCE (object));
312 source = RB_LASTFM_SOURCE (object);
314 g_return_if_fail (source->priv != NULL);
316 rb_debug ("finalizing lastfm source");
318 if (source->priv->db) {
319 g_object_unref (source->priv->db);
320 source->priv->db = NULL;
323 g_free (source->priv->streaming_title);
324 g_free (source->priv->streaming_artist);
325 g_free (source->priv->streaming_album);
327 g_object_unref (G_OBJECT (source->priv->proxy_config));
329 G_OBJECT_CLASS (rb_lastfm_source_parent_class)->finalize (object);
332 static GObject *
333 rb_lastfm_source_constructor (GType type, guint n_construct_properties,
334 GObjectConstructParam *construct_properties)
336 RBLastfmSource *source;
337 RBLastfmSourceClass *klass;
338 RBShell *shell;
339 GObject *player_backend;
341 klass = RB_LASTFM_SOURCE_CLASS (g_type_class_peek (RB_TYPE_LASTFM_SOURCE));
343 source = RB_LASTFM_SOURCE (G_OBJECT_CLASS (rb_lastfm_source_parent_class)
344 ->constructor (type, n_construct_properties, construct_properties));
346 g_object_get (G_OBJECT (source), "shell", &shell, NULL);
347 g_object_get (G_OBJECT (shell),
348 "db", &source->priv->db,
349 "shell-player", &source->priv->shell_player,
350 NULL);
351 g_object_unref (G_OBJECT (shell));
353 /* Set up station tuner */
354 /*source->priv->tuner = gtk_vbox_new (FALSE, 5); */
355 source->priv->vbox2 = gtk_vbox_new (FALSE, 5);
356 source->priv->hbox = gtk_hbox_new (FALSE, 5);
358 source->priv->label = gtk_label_new (_("Enter the artist or global tag to build a radio station out of:"));
359 g_object_set (source->priv->label, "xalign", 0.0, NULL);
361 source->priv->gobutton = gtk_button_new_with_label (_("Add"));
362 g_signal_connect_object (G_OBJECT (source->priv->gobutton),
363 "clicked",
364 G_CALLBACK (rb_lastfm_source_add_station_cb),
365 source, 0);
366 source->priv->typecombo = gtk_combo_box_new_text ();
367 gtk_combo_box_append_text (GTK_COMBO_BOX (source->priv->typecombo), _("Artist"));
368 gtk_combo_box_append_text (GTK_COMBO_BOX (source->priv->typecombo), _("Tag"));
369 gtk_combo_box_set_active (GTK_COMBO_BOX (source->priv->typecombo), 0);
371 source->priv->txtbox = gtk_entry_new ();
373 gtk_box_pack_end_defaults (GTK_BOX (source->priv->hbox),
374 GTK_WIDGET (source->priv->gobutton));
376 gtk_box_pack_end_defaults (GTK_BOX (source->priv->hbox),
377 GTK_WIDGET (source->priv->txtbox));
379 gtk_box_pack_start_defaults (GTK_BOX (source->priv->hbox),
380 GTK_WIDGET (source->priv->typecombo));
382 gtk_box_pack_end_defaults (GTK_BOX (source->priv->vbox2),
383 GTK_WIDGET (source->priv->hbox));
385 gtk_box_pack_end_defaults (GTK_BOX (source->priv->vbox2),
386 GTK_WIDGET (source->priv->label));
388 /* set up stations view */
389 source->priv->stations = rb_entry_view_new (source->priv->db,
390 G_OBJECT (source->priv->shell_player),
391 NULL,
392 FALSE, FALSE);
393 /*rb_entry_view_append_column (source->priv->stations, RB_ENTRY_VIEW_COL_TITLE, TRUE);*/
394 rb_entry_view_append_column (source->priv->stations, RB_ENTRY_VIEW_COL_GENRE, TRUE);
395 rb_entry_view_append_column (source->priv->stations, RB_ENTRY_VIEW_COL_RATING, TRUE);
396 rb_entry_view_append_column (source->priv->stations, RB_ENTRY_VIEW_COL_LAST_PLAYED, TRUE);
397 g_signal_connect_object (G_OBJECT (source->priv->stations),
398 "sort-order-changed",
399 G_CALLBACK (rb_lastfm_source_songs_view_sort_order_changed_cb),
400 source, 0);
402 /* Drag and drop URIs */
403 g_signal_connect_object (G_OBJECT (source->priv->stations),
404 "drag_data_received",
405 G_CALLBACK (rb_lastfm_source_drag_cb),
406 source, 0);
407 g_signal_connect_object (G_OBJECT (source->priv->shell_player),
408 "playing-song-changed",
409 G_CALLBACK (rb_lastfm_song_changed_cb),
410 source, 0);
412 gtk_drag_dest_set (GTK_WIDGET (source->priv->stations),
413 GTK_DEST_DEFAULT_ALL,
414 lastfm_drag_types, 2,
415 GDK_ACTION_COPY | GDK_ACTION_MOVE);
417 /* Pack the vbox */
418 /*gtk_paned_pack1 (GTK_PANED (source->priv->paned),
419 GTK_WIDGET (source->priv->tuner), FALSE, FALSE); */
421 /*source->priv->paned = gtk_vpaned_new ();
422 gtk_paned_pack2 (GTK_PANED (source->priv->paned),
423 GTK_WIDGET (source->priv->stations), TRUE, FALSE); */
425 gtk_box_pack_start (GTK_BOX (source->priv->vbox), GTK_WIDGET (source->priv->vbox2), FALSE, FALSE, 5);
426 gtk_box_pack_start_defaults (GTK_BOX (source->priv->vbox), GTK_WIDGET (source->priv->stations));
429 gtk_widget_show_all (GTK_WIDGET (source));
432 source->priv->action_group = _rb_source_register_action_group (RB_SOURCE (source),
433 "LastfmActions",
434 rb_lastfm_source_actions,
435 G_N_ELEMENTS (rb_lastfm_source_actions),
436 source);
438 rb_lastfm_source_do_query (source);
440 g_signal_connect_object (G_OBJECT (source->priv->db),
441 "entry-extra-metadata-request::" RHYTHMDB_PROP_STREAM_SONG_TITLE,
442 G_CALLBACK (streaming_title_request_cb),
443 source, 0);
445 g_signal_connect_object (G_OBJECT (source->priv->db),
446 "entry-extra-metadata-request::" RHYTHMDB_PROP_STREAM_SONG_ARTIST,
447 G_CALLBACK (streaming_artist_request_cb),
448 source, 0);
450 g_signal_connect_object (G_OBJECT (source->priv->db),
451 "entry-extra-metadata-request::" RHYTHMDB_PROP_STREAM_SONG_ALBUM,
452 G_CALLBACK (streaming_album_request_cb),
453 source, 0);
455 g_signal_connect_object (G_OBJECT (source->priv->db),
456 "entry-extra-metadata-gather",
457 G_CALLBACK (extra_metadata_gather_cb),
458 source, 0);
460 g_object_get (source->priv->shell_player, "player", &player_backend, NULL);
461 g_signal_connect_object (player_backend,
462 "event::rb-lastfm-new-song",
463 G_CALLBACK (rb_lastfm_source_new_song_cb),
464 source,
466 source->priv->buffering = -1;
467 g_signal_connect_object (source->priv->shell_player, "playing-source-changed",
468 G_CALLBACK (playing_source_changed_cb),
469 source, 0);
471 return G_OBJECT (source);
474 static void
475 rb_lastfm_source_set_property (GObject *object,
476 guint prop_id,
477 const GValue *value,
478 GParamSpec *pspec)
480 RBLastfmSource *source = RB_LASTFM_SOURCE (object);
482 switch (prop_id) {
483 case PROP_ENTRY_TYPE:
484 source->priv->entry_type = g_value_get_boxed (value);
485 break;
486 case PROP_PROXY_CONFIG:
487 source->priv->proxy_config = g_value_get_object (value);
488 g_object_ref (G_OBJECT (source->priv->proxy_config));
489 g_signal_connect_object (G_OBJECT (source->priv->proxy_config),
490 "config-changed",
491 G_CALLBACK (rb_lastfm_proxy_config_changed_cb),
492 source, 0);
493 break;
494 default:
495 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
496 break;
500 static void
501 rb_lastfm_source_get_property (GObject *object,
502 guint prop_id,
503 GValue *value,
504 GParamSpec *pspec)
506 RBLastfmSource *source = RB_LASTFM_SOURCE (object);
508 switch (prop_id) {
509 case PROP_ENTRY_TYPE:
510 g_value_set_boxed (value, source->priv->entry_type);
511 break;
512 default:
513 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
514 break;
518 static gchar *
519 mkmd5 (char *string)
521 md5_state_t md5state;
522 guchar md5pword[16];
523 gchar md5_response[33];
525 int j = 0;
527 memset (md5_response, 0, sizeof (md5_response));
529 md5_init (&md5state);
530 md5_append (&md5state, (unsigned char*)string, strlen (string));
531 md5_finish (&md5state, md5pword);
533 for (j = 0; j < 16; j++) {
534 char a[3];
535 sprintf (a, "%02x", md5pword[j]);
536 md5_response[2*j] = a[0];
537 md5_response[2*j+1] = a[1];
540 return (g_strdup (md5_response));
543 RBSource *
544 rb_lastfm_source_new (RBShell *shell)
546 RBSource *source;
547 RBProxyConfig *proxy_config;
548 RhythmDBEntryType entry_type;
549 char *uri;
550 RhythmDB *db;
551 char *username;
553 g_object_get (G_OBJECT (shell), "db", &db, NULL);
555 /* register entry type if it's not already registered */
556 entry_type = rhythmdb_entry_type_get_by_name (db, "lastfm-station");
557 if (entry_type == RHYTHMDB_ENTRY_TYPE_INVALID) {
558 entry_type = rhythmdb_entry_register_type (db, "lastfm-station");
559 entry_type->save_to_disk = TRUE;
560 entry_type->can_sync_metadata = (RhythmDBEntryCanSyncFunc) rb_true_function;
561 entry_type->sync_metadata = (RhythmDBEntrySyncFunc) rb_null_function;
562 entry_type->get_playback_uri = (RhythmDBEntryStringFunc) rb_lastfm_source_get_playback_uri;
565 g_object_get (G_OBJECT (shell), "proxy-config", &proxy_config, NULL);
567 source = RB_SOURCE (g_object_new (RB_TYPE_LASTFM_SOURCE,
568 "name", _("Last.fm"),
569 "shell", shell,
570 "entry-type", entry_type,
571 "proxy-config", proxy_config,
572 NULL));
573 rb_shell_register_entry_type_for_source (shell, source, entry_type);
575 entry_type->get_playback_uri_data = source;
577 /* create default neighbour radio station */
578 username = eel_gconf_get_string (CONF_AUDIOSCROBBLER_USERNAME);
579 if (username != NULL) {
580 RhythmDBEntry *entry;
582 uri = g_strdup_printf ("lastfm://user/%s/neighbours", username);
583 entry = rhythmdb_entry_lookup_by_location (db, uri);
584 if (entry == NULL) {
585 rb_lastfm_source_new_station (uri, _("Neighbour Radio"), RB_LASTFM_SOURCE (source));
586 } else {
587 rhythmdb_entry_unref (entry);
589 g_free (uri);
590 g_free (username);
593 g_object_unref (db);
594 g_object_unref (proxy_config);
595 return source;
598 static GList*
599 impl_get_ui_actions (RBSource *source)
601 GList *actions = NULL;
603 actions = g_list_prepend (actions, g_strdup ("LastfmLoveSong"));
604 actions = g_list_prepend (actions, g_strdup ("LastfmBanSong"));
605 actions = g_list_prepend (actions, g_strdup ("LastfmSkipSong"));
607 return actions;
610 static RBEntryView *
611 impl_get_entry_view (RBSource *asource)
613 RBLastfmSource *source = RB_LASTFM_SOURCE (asource);
615 return source->priv->stations;
618 static RBSourceEOFType
619 impl_handle_eos (RBSource *asource)
621 return RB_SOURCE_EOF_RETRY;
624 static void
625 impl_get_status (RBSource *asource, char **text, char **progress_text, float *progress)
627 RBLastfmSource *source = RB_LASTFM_SOURCE (asource);
629 if (source->priv->buffering != -1) {
630 *progress = ((float)source->priv->buffering)/100;
631 *text = g_strdup (_("Buffering"));
632 } else {
633 switch (source->priv->status) {
634 case NO_ARTIST:
635 *text = g_strdup (_("No such artist. Check your spelling"));
636 break;
638 case FAILED:
639 *text = g_strdup (_("Handshake failed"));
640 break;
642 case BANNED:
643 *text = g_strdup (_("The server marked you as banned"));
644 break;
646 case COMMUNICATING:
647 case OK:
649 RhythmDBQueryModel *model;
650 guint num_entries;
652 g_object_get (asource, "query-model", &model, NULL);
653 num_entries = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), NULL);
654 g_object_unref (model);
656 *text = g_strdup_printf (ngettext ("%d station", "%d stations", num_entries), num_entries);
657 break;
663 static void
664 impl_delete (RBSource *asource)
666 RBLastfmSource *source = RB_LASTFM_SOURCE (asource);
667 GList *l;
669 for (l = rb_entry_view_get_selected_entries (source->priv->stations); l != NULL; l = g_list_next (l)) {
670 rhythmdb_entry_delete (source->priv->db, l->data);
673 rhythmdb_commit (source->priv->db);
676 static void
677 rb_lastfm_source_songs_view_sort_order_changed_cb (RBEntryView *view,
678 RBLastfmSource *source)
680 rb_debug ("sort order changed");
682 rb_entry_view_resort_model (view);
685 static void
686 rb_lastfm_source_do_query (RBLastfmSource *source)
688 RhythmDBQueryModel *station_query_model;
689 GPtrArray *query;
691 query = rhythmdb_query_parse (source->priv->db,
692 RHYTHMDB_QUERY_PROP_EQUALS,
693 RHYTHMDB_PROP_TYPE,
694 source->priv->entry_type,
695 RHYTHMDB_QUERY_END);
696 station_query_model = rhythmdb_query_model_new_empty (source->priv->db);
697 rhythmdb_do_full_query_parsed (source->priv->db,
698 RHYTHMDB_QUERY_RESULTS (station_query_model),
699 query);
701 rhythmdb_query_free (query);
702 query = NULL;
704 rb_entry_view_set_model (source->priv->stations, station_query_model);
705 g_object_set (G_OBJECT (source), "query-model", station_query_model, NULL);
707 g_object_unref (G_OBJECT (station_query_model));
710 static void
711 rb_lastfm_source_do_handshake (RBLastfmSource *source)
713 char *password;
714 char *username;
715 char *md5password;
716 char *handshake_url;
718 if (source->priv->connected) {
719 return;
722 username = eel_gconf_get_string (CONF_AUDIOSCROBBLER_USERNAME);
723 if (username == NULL) {
724 rb_debug ("no last.fm username");
725 return;
728 password = eel_gconf_get_string (CONF_AUDIOSCROBBLER_PASSWORD);
729 if (password == NULL) {
730 rb_debug ("no last.fm password");
731 return;
734 md5password = mkmd5 (password);
735 g_free (password);
737 handshake_url = g_strdup_printf ("%s/radio/handshake.php?version=1.1.1&platform=linux&"
738 "username=%s&passwordmd5=%s&debug=0&partner=",
739 LASTFM_URL,
740 username,
741 md5password);
742 rb_debug ("Last.fm sending handshake");
743 g_object_ref (source);
744 rb_lastfm_perform (source, handshake_url, NULL, rb_lastfm_message_cb);
745 g_free (handshake_url);
746 g_free (username);
747 g_free (md5password);
750 static char *
751 rb_lastfm_source_get_playback_uri (RhythmDBEntry *entry, gpointer data)
753 char *location;
754 RBLastfmSource *source;
756 if (entry == NULL) {
757 rb_debug ("NULL entry");
758 return NULL;
761 source = RB_LASTFM_SOURCE (data);
762 if (source == NULL) {
763 rb_debug ("NULL source pointer");
764 return NULL;
768 if (!source->priv->connected) {
769 rb_debug ("not connected");
770 return NULL;
772 source = RB_LASTFM_SOURCE (data);
774 location = g_strdup_printf ("xrblastfm://%s", source->priv->stream_url + strlen("http://"));
775 rb_debug ("playback uri: %s", location);
776 return location;
779 static void
780 rb_lastfm_perform (RBLastfmSource *source,
781 const char *url,
782 char *post_data,
783 SoupMessageCallbackFn response_handler)
785 SoupMessage *msg;
786 msg = soup_message_new ("GET", url);
788 if (msg == NULL)
789 return;
791 soup_message_set_http_version (msg, SOUP_HTTP_1_1);
793 rb_debug ("Last.fm communicating with %s", url);
795 if (post_data != NULL) {
796 rb_debug ("POST data: %s", post_data);
797 soup_message_set_request (msg,
798 "application/x-www-form-urlencoded",
799 SOUP_BUFFER_SYSTEM_OWNED,
800 post_data,
801 strlen (post_data));
804 /* create soup session, if we haven't got one yet */
805 if (!source->priv->soup_session) {
806 SoupUri *uri;
808 uri = rb_proxy_config_get_libsoup_uri (source->priv->proxy_config);
809 source->priv->soup_session = soup_session_async_new_with_options (
810 "proxy-uri", uri,
811 NULL);
812 if (uri)
813 soup_uri_free (uri);
816 soup_session_queue_message (source->priv->soup_session,
817 msg,
818 (SoupMessageCallbackFn) response_handler,
819 source);
820 source->priv->status = COMMUNICATING;
821 rb_source_notify_status_changed (RB_SOURCE(source));
824 static void
825 rb_lastfm_message_cb (SoupMessage *req, gpointer user_data)
827 RBLastfmSource *source = RB_LASTFM_SOURCE (user_data);
828 char *body;
829 char **pieces;
830 int i;
832 if ((req->response).body == NULL) {
833 rb_debug ("Lastfm: Server failed to respond");
834 return;
837 body = g_malloc0 ((req->response).length + 1);
838 memcpy (body, (req->response).body, (req->response).length);
840 rb_debug ("response body: %s", body);
842 if (strstr (body, "ERROR - no such artist") != NULL) {
843 source->priv->status = NO_ARTIST;
846 g_strstrip (body);
847 pieces = g_strsplit (body, "\n", 6);
848 for (i = 0; pieces[i] != NULL; i++) {
849 gchar **values = g_strsplit (pieces[i], "=", 2);
850 if (strcmp (values[0], "session") == 0) {
851 if (strcmp (values[1], "FAILED") == 0) {
852 source->priv->status = FAILED;
853 rb_debug ("Lastfm failed to connect to the server");
854 break;
856 source->priv->status = OK;
857 source->priv->session = g_strdup (values[1]);
858 rb_debug ("session ID: %s", source->priv->session);
859 source->priv->connected = TRUE;
860 } else if (strcmp (values[0], "stream_url") == 0) {
861 source->priv->stream_url = g_strdup (values[1]);
862 rb_debug ("stream url: %s", source->priv->stream_url);
863 } else if (strcmp (values[0], "subscriber") == 0) {
864 if (strcmp (values[1], "0") == 0) {
865 source->priv->subscriber = FALSE;
866 } else {
867 source->priv->subscriber = TRUE;
869 } else if (strcmp (values[0], "framehack") ==0 ) {
870 if (strcmp (values[1], "0") == 0) {
871 source->priv->framehack = FALSE;
872 } else {
873 source->priv->framehack = TRUE;
875 } else if (strcmp (values[0], "base_url") ==0) {
876 source->priv->base_url = g_strdup (values[1]);
877 } else if (strcmp (values[0], "base_path") ==0) {
878 source->priv->base_path = g_strdup (values[1]);
879 } else if (strcmp (values[0], "update_url") ==0) {
880 source->priv->update_url = g_strdup (values[1]);
881 } else if (strcmp (values[0], "banned") ==0) {
882 if (strcmp (values[1], "0") ==0) {
883 source->priv->banned = FALSE;
884 } else {
885 source->priv->status = BANNED;
886 source->priv->banned = TRUE;
887 source->priv->connected = FALSE;
889 } else if (strcmp (values[0], "response") == 0) {
890 if (strcmp (values[1], "OK") == 0) {
891 source->priv->status = OK;
892 rb_debug ("Successfully communicated");
893 source->priv->connected = TRUE;
894 } else {
895 source->priv->connected = FALSE;
897 } else if (strcmp (values[0], "stationname") == 0) {
898 gchar **data = g_strsplit (g_strdown(pieces[i - 1]), "=",2);
899 RhythmDBEntry *entry;
900 GValue titlestring = {0,};
902 rb_debug ("Received station name from server: %s", values[1]);
903 entry = rhythmdb_entry_lookup_by_location (source->priv->db, data[1]);
904 g_value_init (&titlestring, G_TYPE_STRING);
905 g_value_set_string (&titlestring, values[1]);
907 if (entry == NULL) {
908 entry = rhythmdb_entry_new (source->priv->db, source->priv->entry_type, data[1]);
909 rhythmdb_entry_set (source->priv->db, entry, RHYTHMDB_PROP_GENRE, &titlestring);
910 } else {
911 rhythmdb_entry_set (source->priv->db, entry, RHYTHMDB_PROP_GENRE, &titlestring);
913 g_value_unset (&titlestring);
914 rhythmdb_commit (source->priv->db);
919 g_strfreev (pieces);
920 g_free (body);
922 /* doesn't work yet
923 if (source->priv->pending_entry) {
924 rb_shell_player_play_entry (source->priv->shell_player,
925 source->priv->pending_entry,
926 NULL);
927 rhythmdb_entry_unref (source->priv->pending_entry);
928 source->priv->pending_entry = NULL;
932 rb_source_notify_status_changed (RB_SOURCE (source));
933 g_object_unref (source);
936 static void
937 rb_lastfm_change_station (RBLastfmSource *source, const char *station)
939 char *url;
940 if (!source->priv->connected) {
941 rb_lastfm_source_do_handshake (source);
942 return;
945 url = g_strdup_printf("%s/radio/adjust.php?session=%s&url=%s&debug=0",
946 LASTFM_URL,
947 source->priv->session,
948 station);
950 g_object_ref (source);
951 rb_lastfm_perform (source, url, NULL, rb_lastfm_message_cb);
952 g_free (url);
955 static void
956 rb_lastfm_proxy_config_changed_cb (RBProxyConfig *config,
957 RBLastfmSource *source)
959 SoupUri *uri;
961 if (source->priv->soup_session) {
962 uri = rb_proxy_config_get_libsoup_uri (config);
963 g_object_set (G_OBJECT (source->priv->soup_session),
964 "proxy-uri", uri,
965 NULL);
966 if (uri)
967 soup_uri_free (uri);
971 static void
972 rb_lastfm_source_new_station (char *uri, char *title, RBLastfmSource *source)
974 RhythmDBEntry *entry;
975 GValue v = {0,};
977 rb_debug ("adding lastfm: %s, %s",uri, title);
979 entry = rhythmdb_entry_lookup_by_location (source->priv->db, uri);
980 if (entry) {
981 rb_debug ("uri %s already in db", uri);
982 return;
985 entry = rhythmdb_entry_new (source->priv->db, source->priv->entry_type, uri);
986 g_value_init (&v, G_TYPE_STRING);
987 g_value_set_string (&v, title);
988 rhythmdb_entry_set (source->priv->db, entry, RHYTHMDB_PROP_GENRE, &v);
989 g_value_unset (&v);
991 g_value_init (&v, G_TYPE_DOUBLE);
992 g_value_set_double (&v, 0.0);
993 rhythmdb_entry_set (source->priv->db, entry, RHYTHMDB_PROP_RATING, &v);
995 rhythmdb_commit (source->priv->db);
998 static void
999 rb_lastfm_source_dispose (GObject *object)
1001 RBLastfmSource *source;
1003 source = RB_LASTFM_SOURCE (object);
1005 if (source->priv->db) {
1006 g_object_unref (source->priv->db);
1007 source->priv->db = NULL;
1010 G_OBJECT_CLASS (rb_lastfm_source_parent_class)->dispose (object);
1013 static void
1014 rb_lastfm_source_command (RBLastfmSource *source, const char *query_string, const char *status)
1016 char *url;
1017 if (!source->priv->connected) {
1018 rb_lastfm_source_do_handshake (source);
1019 return;
1022 url = g_strdup_printf ("%s/radio/control.php?session=%s&debug=0&%s",
1023 LASTFM_URL,
1024 source->priv->session,
1025 query_string);
1026 g_object_ref (source);
1027 rb_lastfm_perform (source, url, NULL, rb_lastfm_message_cb);
1028 g_free (url);
1030 rb_source_notify_status_changed (RB_SOURCE (source));
1033 static void
1034 rb_lastfm_source_love_track (GtkAction *action, RBLastfmSource *source)
1036 rb_lastfm_source_command (source, "command=love", _("Marking song loved..."));
1039 static void
1040 rb_lastfm_source_skip_track (GtkAction *action, RBLastfmSource *source)
1042 rb_lastfm_source_command (source, "command=skip", _("Skipping song..."));
1045 static void
1046 rb_lastfm_source_ban_track (GtkAction *action, RBLastfmSource *source)
1048 rb_lastfm_source_command (source, "command=ban", _("Banning song..."));
1052 static void
1053 rb_lastfm_source_drag_cb (GtkWidget *widget,
1054 GdkDragContext *dc,
1055 gint x, gint y,
1056 GtkSelectionData *selection_data,
1057 guint info, guint time,
1058 RBLastfmSource *source)
1060 impl_receive_drag (RB_SOURCE (source) , selection_data);
1063 static gboolean
1064 impl_receive_drag (RBSource *asource, GtkSelectionData *selection_data)
1066 char *uri;
1067 char *title = NULL;
1068 RBLastfmSource *source = RB_LASTFM_SOURCE (asource);
1070 uri = (char *)selection_data->data;
1071 rb_debug ("parsing uri %s", uri);
1073 if (strstr (uri, "lastfm://") == NULL)
1074 return FALSE;
1076 title = rb_lastfm_source_title_from_uri (uri);
1078 rb_lastfm_source_new_station (uri, title, source);
1079 return TRUE;
1083 static char *
1084 rb_lastfm_source_title_from_uri (char *uri)
1086 char *title = NULL;
1087 char *unesc_title;
1088 gchar **data = g_strsplit (uri, "/", 0);
1090 if (strstr (uri, "globaltags") != NULL)
1091 title = g_strdup_printf (_("Global Tag %s"), data[3]);
1093 if (title == NULL && strcmp (data[2], "artist") == 0) {
1094 /* Check if the station is from an artist page, if not, it is a similar
1095 * artist station, and the server should return a name that change_station
1096 * will handle for us.
1098 if (data[4] != NULL) {
1099 if (strcmp (data[4], "similarartists") == 0)
1100 title = g_strdup_printf (_("Artists similar to %s"), data[3]);
1101 if (strcmp (data[4], "fans") == 0)
1102 title = g_strdup_printf (_("Artists Liked by fans of %s"), data[3]);
1107 if (title == NULL && strcmp (data[2], "user") == 0) {
1108 if (strcmp(data[4], "neighbours") == 0)
1109 title = g_strdup_printf (_("%s's Neighbour Radio"), data[3]);
1110 if (strcmp(data[4], "recommended") == 0)
1111 title = g_strdup_printf (_("%s's Recommended Radio: %s percent"), data[3], data[5]);
1112 /* subscriber? */
1115 if (title == NULL) {
1116 title = g_strstrip (uri);
1119 g_strfreev (data);
1120 unesc_title = gnome_vfs_unescape_string (title, NULL);
1121 g_free (title);
1122 return unesc_title;
1125 static void
1126 rb_lastfm_source_add_station_cb (GtkButton *button, gpointer *data)
1128 RBLastfmSource *source = RB_LASTFM_SOURCE (data);
1129 const gchar *add;
1130 char *title;
1131 char *uri;
1133 add = gtk_entry_get_text (GTK_ENTRY (source->priv->txtbox));
1135 if (strcmp (gtk_combo_box_get_active_text (GTK_COMBO_BOX (source->priv->typecombo)), "Artist") == 0) {
1136 uri = g_strdup_printf ("lastfm://artist/%s/similarartists", add);
1137 } else {
1138 uri = g_strdup_printf ("lastfm://globaltags/%s", add);
1141 gtk_entry_set_text (GTK_ENTRY (source->priv->txtbox), "");
1142 title = rb_lastfm_source_title_from_uri (uri);
1143 rb_lastfm_source_new_station (uri, title, source);
1145 g_free(uri);
1146 g_free(title);
1149 static void
1150 rb_lastfm_source_metadata_cb (SoupMessage *req, RBLastfmSource *source)
1152 char *body;
1153 char **pieces;
1154 int p;
1155 RhythmDBEntry *entry;
1157 entry = rb_shell_player_get_playing_entry (source->priv->shell_player);
1158 if (entry == NULL || rhythmdb_entry_get_entry_type (entry) != source->priv->entry_type) {
1159 rb_debug ("got response to metadata request, but not playing from this source");
1160 return;
1163 rb_debug ("got response to metadata request");
1164 body = g_malloc0 ((req->response).length + 1);
1165 memcpy (body, (req->response).body, (req->response).length);
1167 g_strstrip (body);
1168 pieces = g_strsplit (body, "\n", 0);
1170 for (p = 0; pieces[p] != NULL; p++) {
1171 gchar **values;
1172 GValue v = {0,};
1174 values = g_strsplit (pieces[p], "=", 2);
1175 if (strcmp (values[0], "station") == 0) {
1176 } else if (strcmp (values[0], "station_url") == 0) {
1177 } else if (strcmp (values[0], "stationfeed") == 0) {
1178 } else if (strcmp (values[0], "stationfeed_url") == 0) {
1179 } else if (strcmp (values[0], "artist") == 0) {
1180 rb_debug ("artist -> %s", values[1]);
1181 g_free (source->priv->streaming_artist);
1182 source->priv->streaming_artist = g_strdup (values[1]);
1184 g_value_init (&v, G_TYPE_STRING);
1185 g_value_set_string (&v, values[1]);
1186 rhythmdb_emit_entry_extra_metadata_notify (source->priv->db,
1187 entry,
1188 RHYTHMDB_PROP_STREAM_SONG_ARTIST,
1189 &v);
1190 g_value_unset (&v);
1191 } else if (strcmp (values[0], "album") == 0) {
1192 rb_debug ("album -> %s", values[1]);
1193 g_free (source->priv->streaming_album);
1194 source->priv->streaming_album = g_strdup (values[1]);
1196 g_value_init (&v, G_TYPE_STRING);
1197 g_value_set_string (&v, values[1]);
1198 rhythmdb_emit_entry_extra_metadata_notify (source->priv->db,
1199 entry,
1200 RHYTHMDB_PROP_STREAM_SONG_ALBUM,
1201 &v);
1202 g_value_unset (&v);
1203 } else if (strcmp (values[0], "track") == 0) {
1204 rb_debug ("track -> %s", values[1]);
1206 g_free (source->priv->streaming_title);
1207 source->priv->streaming_title = g_strdup (values[1]);
1209 g_value_init (&v, G_TYPE_STRING);
1210 g_value_set_string (&v, values[1]);
1211 rhythmdb_emit_entry_extra_metadata_notify (source->priv->db,
1212 entry,
1213 RHYTHMDB_PROP_STREAM_SONG_TITLE,
1214 &v);
1215 g_value_unset (&v);
1216 } else if (strcmp (values[0], "albumcover_small") == 0) {
1217 } else if (strcmp (values[0], "albumcover_medium") == 0) {
1218 } else if (strcmp (values[0], "albumcover_large") == 0) {
1219 } else if (strcmp (values[0], "trackprogress") == 0) {
1220 } else if (strcmp (values[0], "trackduration") == 0) {
1221 } else if (strcmp (values[0], "artist_url") == 0) {
1222 } else if (strcmp (values[0], "album_url") == 0) {
1223 } else if (strcmp (values[0], "track_url") == 0) {
1224 } else if (strcmp (values[0], "discovery") == 0) {
1225 } else {
1226 rb_debug ("got unknown value: %s", values[0]);
1229 g_strfreev (values);
1232 g_strfreev (pieces);
1233 g_free (body);
1235 source->priv->status = OK;
1236 rb_source_notify_status_changed (RB_SOURCE (source));
1239 static void
1240 rb_lastfm_source_new_song_cb (GObject *player_backend,
1241 gpointer data,
1242 RBLastfmSource *source)
1244 char *uri;
1245 rb_debug ("got new song");
1247 uri = g_strdup_printf ("http://%s%s/np.php?session=%s&debug=0",
1248 source->priv->base_url,
1249 source->priv->base_path,
1250 source->priv->session);
1251 rb_lastfm_perform (source, uri, NULL, (SoupMessageCallbackFn) rb_lastfm_source_metadata_cb);
1252 g_free (uri);
1255 static gboolean
1256 check_entry_type (RBLastfmSource *source, RhythmDBEntry *entry)
1258 RhythmDBEntryType entry_type;
1259 gboolean matches = FALSE;
1261 g_object_get (source, "entry-type", &entry_type, NULL);
1262 if (entry != NULL && rhythmdb_entry_get_entry_type (entry) == entry_type)
1263 matches = TRUE;
1264 g_boxed_free (RHYTHMDB_TYPE_ENTRY_TYPE, entry_type);
1266 return matches;
1269 static void
1270 rb_lastfm_song_changed_cb (RBShellPlayer *player,
1271 RhythmDBEntry *entry,
1272 RBLastfmSource *source)
1274 const char *location;
1276 g_free (source->priv->streaming_title);
1277 g_free (source->priv->streaming_album);
1278 g_free (source->priv->streaming_artist);
1279 source->priv->streaming_title = NULL;
1280 source->priv->streaming_album = NULL;
1281 source->priv->streaming_artist = NULL;
1283 if (check_entry_type (source, entry)) {
1284 location = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LOCATION);
1285 /* this bit doesn't work */
1287 if (!source->priv->connected) {
1288 rb_lastfm_source_do_handshake (source);
1289 source->priv->pending_entry = rhythmdb_entry_ref (entry);
1290 rb_debug ("will play station %s once connected", location);
1291 } else {
1292 rb_debug ("switching to station %s", location);
1293 rb_lastfm_change_station (source, location);
1296 rb_lastfm_change_station (source, location);
1297 } else {
1298 rb_debug ("non-lastfm entry being played");
1302 static GValue *
1303 streaming_title_request_cb (RhythmDB *db,
1304 RhythmDBEntry *entry,
1305 RBLastfmSource *source)
1307 GValue *value;
1308 if (check_entry_type (source, entry) == FALSE ||
1309 entry != rb_shell_player_get_playing_entry (source->priv->shell_player) ||
1310 source->priv->streaming_title == NULL)
1311 return NULL;
1313 rb_debug ("returning streaming title \"%s\" to extra metadata request", source->priv->streaming_title);
1314 value = g_new0 (GValue, 1);
1315 g_value_init (value, G_TYPE_STRING);
1316 g_value_set_string (value, source->priv->streaming_title);
1317 return value;
1320 static GValue *
1321 streaming_artist_request_cb (RhythmDB *db,
1322 RhythmDBEntry *entry,
1323 RBLastfmSource *source)
1325 GValue *value;
1327 if (check_entry_type (source, entry) == FALSE ||
1328 entry != rb_shell_player_get_playing_entry (source->priv->shell_player) ||
1329 source->priv->streaming_artist == NULL)
1330 return NULL;
1332 rb_debug ("returning streaming artist \"%s\" to extra metadata request", source->priv->streaming_artist);
1333 value = g_new0 (GValue, 1);
1334 g_value_init (value, G_TYPE_STRING);
1335 g_value_set_string (value, source->priv->streaming_artist);
1336 return value;
1339 static GValue *
1340 streaming_album_request_cb (RhythmDB *db,
1341 RhythmDBEntry *entry,
1342 RBLastfmSource *source)
1344 GValue *value;
1346 if (check_entry_type (source, entry) == FALSE ||
1347 entry != rb_shell_player_get_playing_entry (source->priv->shell_player) ||
1348 source->priv->streaming_artist == NULL)
1349 return NULL;
1351 rb_debug ("returning streaming album \"%s\" to extra metadata request", source->priv->streaming_album);
1352 value = g_new0 (GValue, 1);
1353 g_value_init (value, G_TYPE_STRING);
1354 g_value_set_string (value, source->priv->streaming_album);
1355 return value;
1358 static void
1359 extra_metadata_gather_cb (RhythmDB *db,
1360 RhythmDBEntry *entry,
1361 GHashTable *data,
1362 RBLastfmSource *source)
1364 /* our extra metadata only applies to the playing entry */
1365 if (entry != rb_shell_player_get_playing_entry (source->priv->shell_player) ||
1366 check_entry_type (source, entry) == FALSE)
1367 return;
1369 if (source->priv->streaming_title != NULL) {
1370 GValue *value;
1372 value = g_new0 (GValue, 1);
1373 g_value_init (value, G_TYPE_STRING);
1374 g_value_set_string (value, source->priv->streaming_title);
1375 g_hash_table_insert (data, g_strdup (RHYTHMDB_PROP_STREAM_SONG_TITLE), value);
1378 if (source->priv->streaming_artist != NULL) {
1379 GValue *value;
1381 value = g_new0 (GValue, 1);
1382 g_value_init (value, G_TYPE_STRING);
1383 g_value_set_string (value, source->priv->streaming_artist);
1384 g_hash_table_insert (data, g_strdup (RHYTHMDB_PROP_STREAM_SONG_ARTIST), value);
1387 if (source->priv->streaming_album != NULL) {
1388 GValue *value;
1390 value = g_new0 (GValue, 1);
1391 g_value_init (value, G_TYPE_STRING);
1392 g_value_set_string (value, source->priv->streaming_album);
1393 g_hash_table_insert (data, g_strdup (RHYTHMDB_PROP_STREAM_SONG_ALBUM), value);
1397 static void
1398 impl_activate (RBSource *source)
1400 rb_lastfm_source_do_handshake (RB_LASTFM_SOURCE (source));
1403 static void
1404 buffering_cb (GObject *backend, guint progress, RBLastfmSource *source)
1406 if (progress == 0)
1407 return;
1409 if (progress == 100)
1410 progress = 0;
1412 rb_debug ("buffer at %d%%", progress);
1414 GDK_THREADS_ENTER ();
1415 source->priv->buffering = progress;
1416 rb_source_notify_status_changed (RB_SOURCE (source));
1417 GDK_THREADS_LEAVE ();
1420 static void
1421 playing_source_changed_cb (RBShellPlayer *player,
1422 RBSource *source,
1423 RBLastfmSource *lastfm_source)
1425 GObject *backend;
1427 g_object_get (player, "player", &backend, NULL);
1429 if (source == RB_SOURCE (lastfm_source)) {
1430 rb_debug ("connecting buffering signal handler");
1431 if (lastfm_source->priv->buffering_id == 0) {
1432 lastfm_source->priv->buffering_id =
1433 g_signal_connect_object (backend, "buffering",
1434 G_CALLBACK (buffering_cb),
1435 lastfm_source, 0);
1437 /* display 'connecting' status until we get a buffering message */
1438 lastfm_source->priv->buffering = -1;
1439 rb_source_notify_status_changed (RB_SOURCE (lastfm_source));
1440 } else if (lastfm_source->priv->buffering_id > 0) {
1441 rb_debug ("disconnecting buffering signal handler");
1442 g_signal_handler_disconnect (backend, lastfm_source->priv->buffering_id);
1443 lastfm_source->priv->buffering_id = 0;
1445 lastfm_source->priv->buffering = -1;
1446 rb_source_notify_status_changed (RB_SOURCE (lastfm_source));
1449 g_object_unref (backend);