[gnac] Cleaned playlists handling



commit adf6c97dac9dd24ed5d9d7c7924f6926f33259dc
Author: Benoît Dupasquier <bdupasqu src gnome org>
Date:   Tue Jun 1 18:27:13 2010 +0100

    Cleaned playlists handling

 src/gnac-main.c     |   16 +++------
 src/gnac-playlist.c |   98 ++++++++++++++++++++++++++++++++++++---------------
 src/gnac-playlist.h |    7 ++++
 3 files changed, 81 insertions(+), 40 deletions(-)
---
diff --git a/src/gnac-main.c b/src/gnac-main.c
index 074265f..ec3b599 100644
--- a/src/gnac-main.c
+++ b/src/gnac-main.c
@@ -530,18 +530,12 @@ gnac_add_file(GFile *file)
   filter_info.mime_type = g_file_info_get_content_type(info);
 
   if (filter_info.mime_type != NULL) {
-    /* Playlists */
-    if (g_ascii_strcasecmp(filter_info.mime_type, "audio/x-mpegurl") == 0) {
-      gnac_playlist_parse_m3u(file);
-    } else if (g_ascii_strcasecmp(filter_info.mime_type, "audio/x-scpls") == 0) {
-      gnac_playlist_parse_pls(file);
-    } else if (g_ascii_strcasecmp(filter_info.mime_type, "application/xspf+xml") == 0) {
-      gnac_playlist_parse_xspf(file); 
-
-    /* Filter the files */
-    } else if (gtk_file_filter_filter(current_file_filter, &filter_info)) {
 
-      /* No duplicate files */
+    /* Check whether we have a playlist */
+    if (gnac_playlist_is_mime_playlist(filter_info.mime_type)) {
+      gnac_playlist_parse(file, filter_info.mime_type);
+    /* Check whether the file format is supported */
+    } else if (gtk_file_filter_filter(current_file_filter, &filter_info)) {
       libgnac_info("Add file %s", g_file_get_uri(file));
       libgnac_converter_add(converter, file, &error);
     }
diff --git a/src/gnac-playlist.c b/src/gnac-playlist.c
index 165f370..42333e4 100755
--- a/src/gnac-playlist.c
+++ b/src/gnac-playlist.c
@@ -34,8 +34,41 @@
 #include "gnac-main.h"
 #include "gnac-playlist.h"
 
-// TODO convert content to utf8!
-// work with utf8, needed for all gnome programs
+
+typedef struct {
+  const gchar  *mime;
+  void        (*handler)(GFile *file);
+} GnacPlaylistMimeHandler;
+
+
+static GnacPlaylistMimeHandler mime_playlist[] = {
+  { "audio/x-mpegurl"     , &gnac_playlist_parse_m3u  },
+  { "audio/x-scpls"       , &gnac_playlist_parse_pls  },
+  { "application/xspf+xml", &gnac_playlist_parse_xspf },
+  { NULL, NULL }
+};
+
+
+static void
+gnac_playlist_resolve_uri_and_add(GFile       *parent,
+                                  const gchar *filename)
+{
+  GFile *file;
+
+  if (!g_utf8_validate(filename, -1, NULL)) {
+    g_printerr("%s: %s\n", _("Invalid UTF-8 filename"), filename);
+    return;
+  }
+
+  if (g_str_has_prefix(filename, "file://")) {
+    file = g_file_new_for_uri(filename);
+  } else {
+    file = g_file_get_child(parent, filename);
+  }
+
+  gnac_add_file(file);
+}
+
 
 static gchar *
 gnac_playlist_read_file(GFile *file) {
@@ -55,11 +88,36 @@ gnac_playlist_read_file(GFile *file) {
 }
 
 
+gboolean
+gnac_playlist_is_mime_playlist(const gchar *mime)
+{
+  guint i;
+  for (i = 0; mime_playlist[i].mime; i++) {
+    if (g_ascii_strcasecmp(mime, mime_playlist[i].mime) == 0) {
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
+
+
+void
+gnac_playlist_parse(GFile       *file,
+                    const gchar *mime)
+{
+  guint i;
+  for (i = 0; mime_playlist[i].mime; i++) {
+    if (g_ascii_strcasecmp(mime, mime_playlist[i].mime) == 0) {
+      mime_playlist[i].handler(file);
+    }
+  }
+}
+
+
 void
 gnac_playlist_parse_m3u(GFile *file)
 {
   GFile *parent;
-  GFile *child;
   gchar *contents;
 
   parent = g_file_get_parent(file);
@@ -70,18 +128,12 @@ gnac_playlist_parse_m3u(GFile *file)
     gchar **lines;
     lines = g_strsplit(contents, "\n", -1);
 
-    gint i, len;
+    gint i;
     for (i = 0; lines[i]; i++) {
       /* skip comments and empty lines */
       if (!*lines[i] || *lines[i] == '#') continue;
       g_strchomp(lines[i]);
-      len = g_utf8_strlen(lines[i], -1);
-      if (!g_utf8_validate(lines[i], -1, NULL)) {
-        g_printerr("%s: %s\n", _("Invalid UTF-8 filename"), lines[i]);
-      } else {
-        child = g_file_get_child(parent, lines[i]);
-        gnac_add_file(child);
-      }
+      gnac_playlist_resolve_uri_and_add(parent, lines[i]);
     }
 
     g_strfreev(lines);
@@ -94,8 +146,7 @@ void
 gnac_playlist_parse_pls(GFile *file)
 {
   GFile *parent;
-  GFile *child;
-  gchar  *contents;
+  gchar *contents;
 
   parent = g_file_get_parent(file);
   contents = gnac_playlist_read_file(file);
@@ -105,7 +156,7 @@ gnac_playlist_parse_pls(GFile *file)
     gchar **lines;
     lines = g_strsplit(contents, "\n", -1);
 
-    gint i, len;
+    gint i;
     for (i = 0; lines[i]; i++) {
       /* skip empty lines */
       if (!*lines[i]) continue;
@@ -115,13 +166,7 @@ gnac_playlist_parse_pls(GFile *file)
       key = g_utf8_strdown(pair[0], -1);
       if (pair[1] && g_str_has_prefix(key, "file")) {
         g_strchomp(pair[1]);
-        len = g_utf8_strlen(lines[i], -1);
-        if (!g_utf8_validate(pair[1], -1, NULL)) {
-          g_printerr("%s: %s\n", _("Invalid UTF-8 filename"), pair[1]);
-        } else {
-          child = g_file_get_child(parent, pair[1]);
-          gnac_add_file(child);
-        }
+        gnac_playlist_resolve_uri_and_add(parent, pair[1]);
       }
 
       g_free(key);
@@ -138,8 +183,7 @@ void
 gnac_playlist_parse_xspf(GFile *file)
 {
   GFile *parent;
-  GFile *child;
-  gchar  *contents;
+  gchar *contents;
 
   parent = g_file_get_parent(file);
   contents = gnac_playlist_read_file(file);
@@ -157,12 +201,8 @@ gnac_playlist_parse_xspf(GFile *file)
       if (g_str_has_prefix(line, "<location>")) {
         line = g_strsplit(line, ">", 2)[1];
         line = g_strsplit(line, "<", 2)[0];
-        if (!g_utf8_validate(line, -1, NULL)) {
-          g_printerr("%s: %s\n", _("Invalid UTF-8 filename"), line);
-        } else {
-          child = g_file_get_child(parent, line);
-          gnac_add_file(child);
-        }
+        g_strchomp(line);
+        gnac_playlist_resolve_uri_and_add(parent, line);
       }
     }
 
diff --git a/src/gnac-playlist.h b/src/gnac-playlist.h
index bf8801a..fc8ac5c 100755
--- a/src/gnac-playlist.h
+++ b/src/gnac-playlist.h
@@ -30,6 +30,13 @@
 
 G_BEGIN_DECLS
 
+gboolean
+gnac_playlist_is_mime_playlist(const gchar *mime);
+
+void
+gnac_playlist_parse(GFile       *file,
+                    const gchar *mime);
+
 void
 gnac_playlist_parse_m3u(GFile *file);
 



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