Re: [Rhythmbox-devel] Last.fm Integration Patch #2



On Thu, 2005-09-15 at 17:48 +0200, Ruben Vermeersch wrote:
> I've made a second version of the Last.fm (audioscrobbler) patch for
> rhythmbox. Improvements over the previous patch:
> 
>  * Converted to libsoup (which DAAP uses too), no longer depends on
>    libcurl (configure.ac has been updated for this too)
>  * Cleaner (less duplicated) code, by making all HTTP requests
>    asynchronously.
>  * Nicer preferences dialog (by "Doc" Livingstone)
>  * No longer fills stdout
> 
> And on top of it all, it even works! ;-)

This work fine for me.

I played around with this a bit the other day, and gnome-vfs-ified the
queue loading/saving functions, so I've attached that patch for that.


Cheers,

James "Doc" Livingston
--
> What's that word, it means you feel small and red, starts with an M?
Management. -- Simon Fraser replying to Peter da Silva in asr.
--- shell/rb-audioscrobbler.c	2005-09-16 01:18:34.000000000 +1000
+++ shell/rb-audioscrobbler.c	2005-09-16 03:21:41.000000000 +1000
@@ -1057,128 +1057,149 @@
 
 
 // Queue functions:
-static void
-rb_audioscrobbler_load_queue (RBAudioscrobbler *audioscrobbler)
-{
-	FILE *fd;
-	char *pathname;
-	char line[8192];
-	GSList *list = NULL, *l;
-	gchar **breaks, **breaks2;
 
-	pathname = g_build_filename (rb_dot_dir (), "audioscrobbler.queue", NULL);
+static AudioscrobblerEntry*
+rb_audioscrobbler_load_entry_from_string (const char *string)
+{
+	AudioscrobblerEntry *entry;
+	int i = 0;
+	char **breaks;
 
-	fd = fopen (pathname, "r");
+	entry = g_new0 (AudioscrobblerEntry, 1);
+	audioscrobbler_entry_init (entry);
 
-	rb_debug ("Loading saved Audioscrobbler queue from \"%s\"", pathname);
+	breaks = g_strsplit (string, "&", 6);
 
-	g_free (pathname);
+	for (i = 0; breaks[i] != NULL; i++) {
+		char **breaks2 = g_strsplit (breaks[i], "=", 2);
 
-	if (fd) {
-		while (fgets (line, sizeof (line), fd)) {
-			list = g_slist_append (list, g_strdup (line));
+		if (breaks2[0] != NULL && breaks2[1] != NULL) {
+			if (g_str_has_prefix (breaks2[0], "a")) {
+				g_free (entry->artist);
+				entry->artist = g_strdup (breaks2[1]);
+			}
+			if (g_str_has_prefix (breaks2[0], "t")) {
+				g_free (entry->title);
+				entry->title = g_strdup (breaks2[1]);
+			}
+			if (g_str_has_prefix (breaks2[0], "b")) {
+				g_free (entry->album);
+				entry->album = g_strdup (breaks2[1]);
+			}
+			if (g_str_has_prefix (breaks2[0], "m")) {
+				g_free (entry->mbid);
+				entry->mbid = g_strdup (breaks2[1]);
+			}
+			if (g_str_has_prefix (breaks2[0], "l")) {
+				entry->length = atoi (breaks2[1]);
+			}
+			if (g_str_has_prefix (breaks2[0], "i")) {
+				g_free (entry->timestamp);
+				entry->timestamp = g_strdup (breaks2[1]);
+			}
 		}
-	} else {
-		rb_debug ("Unable to load Audioscrobbler queue from disk: %s", g_strerror (errno));
+
+		g_strfreev (breaks2);
 	}
 
-	for (l = list; l; l = g_slist_next (l)) {
-		AudioscrobblerEntry *entry;
-		int i = 0;
+	g_strfreev (breaks);
 
-		if (l->data != NULL) {
-			entry = g_new0 (AudioscrobblerEntry, 1);
-			audioscrobbler_entry_init (entry);
-
-			breaks = g_strsplit (g_strchomp (l->data), "&", 6);
-
-			for (i = 0; breaks[i] != NULL; i++) {
-				breaks2 = g_strsplit (breaks[i], "=", 2);
-
-				if (breaks2[0] != NULL && breaks2[1] != NULL) {
-					if (g_str_has_prefix (breaks2[0], "a")) {
-						g_free (entry->artist);
-						entry->artist = g_strdup (breaks2[1]);
-					}
-					if (g_str_has_prefix (breaks2[0], "t")) {
-						g_free (entry->title);
-						entry->title = g_strdup (breaks2[1]);
-					}
-					if (g_str_has_prefix (breaks2[0], "b")) {
-						g_free (entry->album);
-						entry->album = g_strdup (breaks2[1]);
-					}
-					if (g_str_has_prefix (breaks2[0], "m")) {
-						g_free (entry->mbid);
-						entry->mbid = g_strdup (breaks2[1]);
-					}
-					if (g_str_has_prefix (breaks2[0], "l")) {
-						entry->length = atoi (breaks2[1]);
-					}
-					if (g_str_has_prefix (breaks2[0], "i")) {
-						g_free (entry->timestamp);
-						entry->timestamp = g_strdup (breaks2[1]);
-					}
-				}
+	if (strcmp (entry->artist, "") == 0 || strcmp (entry->title, "") == 0) {
+		audioscrobbler_entry_free (entry);
+		entry = NULL;
+	}
 
-				g_strfreev (breaks2);
-			}
+	return entry;
+}
 
-			g_strfreev (breaks);
+static gboolean
+rb_audioscrobbler_load_queue (RBAudioscrobbler *audioscrobbler)
+{
+	char *pathname;
+	GnomeVFSResult result;
+	char *data;
+	int size;
+
+	pathname = g_build_filename (rb_dot_dir (), "audioscrobbler.queue", NULL);
+	rb_debug ("Loading Audioscrobbler queue from \"%s\"", pathname);
+
+	result = gnome_vfs_read_entire_file (pathname, &size, &data);
+	g_free (pathname);
+	
+	/* do stuff */
+	if (result == GNOME_VFS_OK) {
+		char *start = data, *end;
+
+		/* scan along the file's data, turning each line into a string */
+		while (start < (data + size)) {
+			AudioscrobblerEntry *entry;
+
+			/* find the end of the line, to terminate the string */
+			end = g_utf8_strchr (start, -1, '\n');
+			*end = 0;
 
-			if (strcmp (entry->artist, "") != 0 &&
-			    strcmp (entry->title, "") != 0) {
-				rb_debug ("Adding saved entry to queue (%s - %s)",
-					entry->artist, entry->title);
+			entry = rb_audioscrobbler_load_entry_from_string (start);
+			if (entry)
 				audioscrobbler->priv->queue = g_slist_append (audioscrobbler->priv->queue, entry);
-			} else {
-				audioscrobbler_entry_free (entry);
-			}
+
+			start = end + 1;
 		}
 	}
 
-	g_slist_foreach (list, (GFunc) g_free, NULL);
-	g_slist_free (list);
+	if (result != GNOME_VFS_OK) {
+		rb_debug ("Unable to load Audioscrobbler queue from disk: %s",
+			  gnome_vfs_result_to_string (result));
+	}
+
+	g_free (data);
+	return (result == GNOME_VFS_OK);
 }
 
-static int
+static gboolean
 rb_audioscrobbler_save_queue (RBAudioscrobbler *audioscrobbler)
 {
-	FILE *fd;
 	char *pathname;
-	GSList *l;
-	AudioscrobblerEntry *entry;
-
-	l = audioscrobbler->priv->queue;
+	GnomeVFSHandle *handle;
+	GnomeVFSResult result;
 
 	pathname = g_build_filename (rb_dot_dir (), "audioscrobbler.queue", NULL);
-
 	rb_debug ("Saving Audioscrobbler queue to \"%s\"", pathname);
 
-	fd = fopen (pathname, "w+");
-
+	result = gnome_vfs_open (&handle, pathname, GNOME_VFS_OPEN_WRITE);
 	g_free (pathname);
 
-	if (fd) {
-		for (; l; l = g_slist_next (l)) {
-			entry = (AudioscrobblerEntry *) l->data;
-			fprintf (fd, "a=%s&t=%s&b=%s&m=%s&l=%d&i=%s\n",
-				 entry->artist,
-				 entry->title,
-				 entry->album,
-				 entry->mbid,
-				 entry->length,
-				 entry->timestamp);
-		}
+	if (result == GNOME_VFS_OK)
+		result = gnome_vfs_seek (handle, GNOME_VFS_SEEK_END, 0);
 
-		fclose (fd);
+	if (result == GNOME_VFS_OK) {
+		GString *s = g_string_new (NULL);
+		GSList *l;
 
-		return 1;
-	} else {
-		rb_debug ("Unable to save Audioscrobbler queue to disk: %s", g_strerror (errno));
+		for (l = audioscrobbler->priv->queue; l; l = g_slist_next (l)) {
+			AudioscrobblerEntry *entry;
+		
+			entry = (AudioscrobblerEntry *) l->data;
+			g_string_printf (s, "a=%s&t=%s&b=%s&m=%s&l=%d&i=%s\n",
+					    entry->artist,
+					    entry->title,
+					    entry->album,
+					    entry->mbid,
+					    entry->length,
+					    entry->timestamp);
+			result = gnome_vfs_write (handle, s->str, s->len, NULL);
+			if (result != GNOME_VFS_OK)
+				break;
+		}
+		g_string_free (s, TRUE);
+	}
 
-		return 0;
+	if (result != GNOME_VFS_OK) {
+		rb_debug ("Unable to save Audioscrobbler queue to disk: %s",
+			  gnome_vfs_result_to_string (result));
 	}
+
+	gnome_vfs_close (handle);
+	return (result == GNOME_VFS_OK);
 }
 
 static void

Attachment: signature.asc
Description: This is a digitally signed message part



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