Index: data/ui/rhythmbox-ui.xml
===================================================================
RCS file: /cvs/gnome/rhythmbox/data/ui/rhythmbox-ui.xml,v
retrieving revision 1.44
diff -u -u -r1.44 rhythmbox-ui.xml
--- data/ui/rhythmbox-ui.xml	20 Feb 2006 06:40:14 -0000	1.44
+++ data/ui/rhythmbox-ui.xml	20 Feb 2006 13:31:39 -0000
@@ -156,6 +156,7 @@
 
   <popup name="IRadioSourcePopup">
     <menuitem name="IRadioSrcPopupNewStation" action="MusicNewInternetRadioStation"/>
+    <menuitem name="IRadioSrcPopupRefreshIcecase" action="RefreshIcecastIRadio"/>
   </popup>
 
   <popup name="IRadioViewPopup">
Index: podcast/rb-podcast-parse.c
===================================================================
RCS file: /cvs/gnome/rhythmbox/podcast/rb-podcast-parse.c,v
retrieving revision 1.18
diff -u -u -r1.18 rb-podcast-parse.c
--- podcast/rb-podcast-parse.c	29 Dec 2005 14:12:36 -0000	1.18
+++ podcast/rb-podcast-parse.c	20 Feb 2006 13:31:39 -0000
@@ -410,7 +410,7 @@
 	GnomeVFSResult result;
 	GnomeVFSFileInfo *info;
 	gint file_size;
-	gchar *buffer;
+	gchar *buffer = NULL;
     
 	struct RBPoadcastLoadContext *ctx = NULL;
 
@@ -447,8 +447,10 @@
     
 	/* first download file by gnome_vfs for use gnome network configuration */
 	result = gnome_vfs_read_entire_file (file_name, &file_size, &buffer);
-	if (result != GNOME_VFS_OK)
+	if (result != GNOME_VFS_OK) {
+		g_free (buffer);
 		return;
+	}
 	    
 
 	/* initializing parse */
Index: rhythmdb/rhythmdb.h
===================================================================
RCS file: /cvs/gnome/rhythmbox/rhythmdb/rhythmdb.h,v
retrieving revision 1.69
diff -u -u -r1.69 rhythmdb.h
--- rhythmdb/rhythmdb.h	18 Feb 2006 09:47:12 -0000	1.69
+++ rhythmdb/rhythmdb.h	20 Feb 2006 13:31:41 -0000
@@ -330,7 +330,6 @@
 RhythmDBEntryType rhythmdb_entry_iradio_get_type        (void);
 RhythmDBEntryType rhythmdb_entry_podcast_post_get_type  (void);
 RhythmDBEntryType rhythmdb_entry_podcast_feed_get_type  (void);
-RhythmDBEntryType rhythmdb_entry_icecast_get_type        (void);
 
 extern GType rhythmdb_property_type_map[RHYTHMDB_NUM_PROPERTIES];
 G_INLINE_FUNC GType rhythmdb_get_property_type		(RhythmDB *db, guint property_id);
Index: sources/rb-iradio-source.c
===================================================================
RCS file: /cvs/gnome/rhythmbox/sources/rb-iradio-source.c,v
retrieving revision 1.110
diff -u -u -r1.110 rb-iradio-source.c
--- sources/rb-iradio-source.c	18 Feb 2006 13:31:57 -0000	1.110
+++ sources/rb-iradio-source.c	20 Feb 2006 13:31:47 -0000
@@ -29,6 +29,9 @@
 #include <glade/glade.h>
 #include <libgnomevfs/gnome-vfs-uri.h>
 #include <libxml/tree.h>
+#include <libxml/entities.h>
+#include <libxml/SAX.h>
+#include <libxml/parserInternals.h>
 
 #include "rb-iradio-source.h"
 
@@ -114,6 +117,8 @@
 						 RBIRadioSource *source);
 static void rb_iradio_source_cmd_new_station (GtkAction *action,
 					      RBIRadioSource *source);
+static void rb_iradio_source_cmd_refresh_icecast (GtkAction *action,
+						  RBIRadioSource *source);
 
 #define CMD_PATH_SHOW_BROWSER "/commands/ShowBrowser"
 #define CMD_PATH_CURRENT_STATION "/commands/CurrentStation"
@@ -126,6 +131,8 @@
 #define CONF_STATE_IRADIO_SORTING CONF_PREFIX "/state/iradio/sorting"
 #define CONF_STATE_SHOW_BROWSER   CONF_PREFIX "/state/iradio/show_browser"
 
+#define ICECAST_DIRECTORY_ADDRESS "http://dir.xiph.org/yp.xml"
+
 struct RBIRadioSourcePrivate
 {
 	RhythmDB *db;
@@ -155,7 +162,10 @@
 {
 	{ "MusicNewInternetRadioStation", GTK_STOCK_NEW, N_("New Internet _Radio Station"), "<control>I",
 	  N_("Create a new Internet Radio station"),
-	  G_CALLBACK (rb_iradio_source_cmd_new_station) }
+	  G_CALLBACK (rb_iradio_source_cmd_new_station) },
+	{ "RefreshIcecastIRadio", GTK_STOCK_REFRESH, N_("Load Icecast Stations"), NULL,
+	  N_("Refresh the list of Icecast radio stations"),
+	  G_CALLBACK (rb_iradio_source_cmd_refresh_icecast) }
 };
 
 static const GtkTargetEntry stations_view_drag_types[] = {
@@ -909,6 +919,7 @@
 	GList *actions = NULL;
 
 	actions = g_list_prepend (actions, "MusicNewInternetRadioStation");
+	actions = g_list_prepend (actions, "RefreshIcecastIRadio");
 
 	return actions;
 }
@@ -937,5 +948,302 @@
 	gtk_dialog_run (GTK_DIALOG (dialog));
 
 	gtk_widget_destroy (dialog);
+}
+
+typedef struct
+{
+	guint in_unknown_elt;
+	xmlParserCtxtPtr xmlctx;
+	GString *buf;
+	RhythmDB *db;
+	
+	GHashTable *table;
+	RhythmDBEntry *entry;
+	gboolean inserted;
+    
+	enum {
+		ICECAST_PARSER_STATE_START,
+		ICECAST_PARSER_STATE_DIRECTORY,
+		ICECAST_PARSER_STATE_ENTRY,
+		ICECAST_PARSER_STATE_SERVER_NAME,
+		ICECAST_PARSER_STATE_LISTEN_URL,
+		ICECAST_PARSER_STATE_SERVER_TYPE,
+		ICECAST_PARSER_STATE_BITRATE,
+		ICECAST_PARSER_STATE_CHANNELS,
+		ICECAST_PARSER_STATE_SAMPLE_RATE,
+		ICECAST_PARSER_STATE_GENRE,
+		ICECAST_PARSER_STATE_CURRENT_SONG,
+		ICECAST_PARSER_STATE_END,
+	} state;
+} IcecastParserContext;
+
+static void
+_icecast_parser_start_element (IcecastParserContext *ctx,
+			       const char *name, const char **attrs)
+{
+	if (ctx->in_unknown_elt) {
+		ctx->in_unknown_elt++;
+		return;
+	}
+
+	rb_debug ("End element: %s state: %d", name, ctx->state);
+
+	switch (ctx->state)
+	{
+	case ICECAST_PARSER_STATE_START:
+		if (strcmp (name, "directory") == 0)
+			ctx->state = ICECAST_PARSER_STATE_DIRECTORY;
+		else
+			rb_debug ("unexpected element '%s' started at top level", name);
+	
+		break;
+
+	case ICECAST_PARSER_STATE_DIRECTORY:
+		if (strcmp (name, "entry") == 0) {
+			ctx->state = ICECAST_PARSER_STATE_ENTRY;
+		} else
+			rb_debug ("unexpected element '%s' started in directory", name);
+		break;
+
+	case ICECAST_PARSER_STATE_ENTRY:
+		if (strcmp (name, "server_name") == 0)
+			ctx->state = ICECAST_PARSER_STATE_SERVER_NAME;
+		else if (strcmp (name, "listen_url") == 0)
+			ctx->state = ICECAST_PARSER_STATE_LISTEN_URL;
+		else if (strcmp (name, "server_type") == 0)
+			ctx->state = ICECAST_PARSER_STATE_SERVER_TYPE;
+		else if (strcmp (name, "bitrate") == 0)
+			ctx->state = ICECAST_PARSER_STATE_BITRATE;
+		else if (strcmp (name, "channels") == 0)
+			ctx->state = ICECAST_PARSER_STATE_CHANNELS;
+		else if (strcmp (name, "samplerate") == 0)
+			ctx->state = ICECAST_PARSER_STATE_SAMPLE_RATE;
+		else if (strcmp (name, "genre") == 0)
+			ctx->state = ICECAST_PARSER_STATE_GENRE;
+		else if (strcmp (name, "current_song") == 0)
+			ctx->state = ICECAST_PARSER_STATE_CURRENT_SONG;
+		else
+			rb_debug ("unknown data element '%s' in entry", name);
+			
+		break;
+
+	case ICECAST_PARSER_STATE_END:
+		rb_debug ("unexpected element '%s' started after directory", name);
+		ctx->in_unknown_elt++;
+		break;
+		
+	case ICECAST_PARSER_STATE_SERVER_NAME:
+	case ICECAST_PARSER_STATE_LISTEN_URL:
+	case ICECAST_PARSER_STATE_SERVER_TYPE:
+	case ICECAST_PARSER_STATE_BITRATE:
+	case ICECAST_PARSER_STATE_CHANNELS:
+	case ICECAST_PARSER_STATE_SAMPLE_RATE:
+	case ICECAST_PARSER_STATE_GENRE:
+	case ICECAST_PARSER_STATE_CURRENT_SONG:
+		rb_debug ("unexpectedly started new element '%s' inside data element", name);
+		ctx->in_unknown_elt++;
+		break;
+	}
+}
+
+static void
+_icecast_parser_set_properties (const char *name, const char *value, IcecastParserContext *ctx)
+{
+	GValue val = {0,};
+	RhythmDBPropType prop;
+
+	if (strcmp (name, "listen_url") == 0) {
+		/* this has already been set */
+		return;
+	} else if (strcmp (name, "server_name") == 0) {
+		prop = RHYTHMDB_PROP_TITLE;
+		g_value_init (&val, G_TYPE_STRING);
+		g_value_set_string (&val, value);
+	} else if (strcmp (name, "server_type") == 0) {
+		prop = RHYTHMDB_PROP_MIMETYPE;
+		g_value_init (&val, G_TYPE_STRING);
+		g_value_set_string (&val, value);
+	} else if (strcmp (name, "genre") == 0) {
+		prop = RHYTHMDB_PROP_GENRE;
+		g_value_init (&val, G_TYPE_STRING);
+		g_value_set_string (&val, value);
+	} else if (strcmp (name, "bitrate") == 0) {
+		prop = RHYTHMDB_PROP_BITRATE;
+		g_value_init (&val, G_TYPE_ULONG);
+		g_value_set_ulong (&val, atoi (value));
+	} else if (strcmp (name, "channels") == 0) {
+		/* ignore */
+		return;
+	} else if (strcmp (name, "samplerate") == 0) {
+		/* ignore */
+		return;
+	} else if (strcmp (name, "current_song") == 0) {
+		/* ignore */
+		return;
+	} else {
+		rb_debug ("unknown data element '%s' with value '%s'", name, value);
+		return;
+	}
+
+	if (ctx->inserted)
+		rhythmdb_entry_set (ctx->db, ctx->entry,
+				    prop, &val);
+	else
+		rhythmdb_entry_set_uninserted (ctx->db, ctx->entry,
+					       prop, &val);
+	
+	g_value_unset (&val);
+}
+
+static void
+_icecast_parser_end_element (IcecastParserContext *ctx, 
+			    const char *name)
+{
+	rb_debug ("End element: %s state: %d", name, ctx->state);
+
+	if (ctx->in_unknown_elt > 0) {
+		ctx->in_unknown_elt--;
+		rb_debug ("Unknown element");
+		return;
+	}
+               
+	switch (ctx->state) {
+        case ICECAST_PARSER_STATE_START:
+		ctx->state = ICECAST_PARSER_STATE_END;
+		break;
+            
+        case ICECAST_PARSER_STATE_DIRECTORY:
+		ctx->state = ICECAST_PARSER_STATE_START;
+		break;
+	
+	case ICECAST_PARSER_STATE_ENTRY:
+	{
+		char *location;
+
+		location = g_hash_table_lookup (ctx->table, "listen_url");
+		if (location) {
+			ctx->entry = rhythmdb_entry_lookup_by_location (ctx->db, location);
+			if (ctx->entry == NULL) {
+				ctx->entry = rhythmdb_entry_new (ctx->db,
+								 RHYTHMDB_ENTRY_TYPE_IRADIO_STATION,
+								 location);
+				ctx->inserted = FALSE;
+			} else {
+				ctx->inserted = TRUE;
+			}
+
+			g_hash_table_foreach (ctx->table,
+					      (GHFunc)_icecast_parser_set_properties,
+					      ctx);
+
+			rhythmdb_commit (ctx->db);
+		} else {
+			rb_debug ("got icecast entry with no listen_url");
+		}
+		
+		/* purge the table for the next entry*/
+		g_hash_table_foreach_remove (ctx->table, (GHRFunc)rb_true_function, NULL);
+		ctx->state = ICECAST_PARSER_STATE_DIRECTORY;
+		break;
+	}
+
+	case ICECAST_PARSER_STATE_SERVER_NAME:
+	case ICECAST_PARSER_STATE_LISTEN_URL:
+	case ICECAST_PARSER_STATE_SERVER_TYPE:
+	case ICECAST_PARSER_STATE_BITRATE:
+	case ICECAST_PARSER_STATE_GENRE:
+	case ICECAST_PARSER_STATE_CHANNELS:
+	case ICECAST_PARSER_STATE_SAMPLE_RATE:
+	case ICECAST_PARSER_STATE_CURRENT_SONG:
+		/* store the data to be processed when the entry is done */
+		if (ctx->buf->str &&ctx->buf->str[0] )
+			g_hash_table_insert (ctx->table, g_strdup (name), g_strdup (ctx->buf->str));
+
+		g_string_truncate (ctx->buf, 0);
+		ctx->state = ICECAST_PARSER_STATE_ENTRY;
+		break;
+	case ICECAST_PARSER_STATE_END:
+		g_assert_not_reached ();
+		break;
+	}
+}
+static void
+_icecast_parser_characters (IcecastParserContext *ctx,
+			    const char *data,
+			    guint len)
+{
+	switch (ctx->state) {
+	case ICECAST_PARSER_STATE_SERVER_NAME:
+	case ICECAST_PARSER_STATE_LISTEN_URL:
+	case ICECAST_PARSER_STATE_SERVER_TYPE:
+	case ICECAST_PARSER_STATE_BITRATE:
+	case ICECAST_PARSER_STATE_CHANNELS:
+	case ICECAST_PARSER_STATE_SAMPLE_RATE:
+	case ICECAST_PARSER_STATE_GENRE:
+	case ICECAST_PARSER_STATE_CURRENT_SONG:
+		g_string_append_len (ctx->buf, data, len);
+           	break;
+        case ICECAST_PARSER_STATE_START:
+        case ICECAST_PARSER_STATE_DIRECTORY:
+        case ICECAST_PARSER_STATE_ENTRY:
+        case ICECAST_PARSER_STATE_END:
+		break;
+	default:
+		g_warning ("Unknown podcast parser state: %d", ctx->state);
+		break;
+	}
+}
+
+
+static gpointer
+_refresh_icecast_thread (RBIRadioSource *source)
+{
+	xmlParserCtxtPtr ctxt;
+	xmlSAXHandlerPtr sax_handler = NULL;
+	GnomeVFSResult result;
+	gint file_size;
+	gchar *buffer = NULL;
+	IcecastParserContext *ctx;
+
+	result = gnome_vfs_read_entire_file (ICECAST_DIRECTORY_ADDRESS, &file_size, &buffer);
+	if (result != GNOME_VFS_OK) {
+		g_free (buffer);
+		return NULL;
+	}
+
+	sax_handler = g_new0 (xmlSAXHandler, 1);
+	sax_handler->startElement = (startElementSAXFunc) _icecast_parser_start_element;
+	sax_handler->endElement = (endElementSAXFunc) _icecast_parser_end_element;
+	sax_handler->characters = (charactersSAXFunc) _icecast_parser_characters;
+	xmlSubstituteEntitiesDefault (1);
+
+	ctx = g_new0 (IcecastParserContext, 1);
+	ctx->buf = g_string_sized_new (512);
+	ctx->db = source->priv->db;
+	ctx->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+                            
+	ctxt = xmlCreateMemoryParserCtxt (buffer, file_size);
+	ctx->xmlctx = ctxt;
+	ctxt->userData = ctx;
+	ctxt->sax = sax_handler;
+	xmlParseDocument (ctxt);
+
+	g_free (sax_handler);
+	ctxt->sax = NULL;
+	xmlFreeParserCtxt (ctxt);
+
+	g_free (buffer);
+	g_string_free (ctx->buf, TRUE);
+	g_hash_table_destroy (ctx->table);
+	g_free (ctx);
+
+	return NULL;
+}
+
+static void
+rb_iradio_source_cmd_refresh_icecast (GtkAction *action,
+				      RBIRadioSource *source)
+{
+	g_thread_create ((GThreadFunc)_refresh_icecast_thread, source, FALSE, NULL);
 }
 
