tools: gst-play: sort directory entries
authorVivia Nikolaidou <vivia@ahiru.eu>
Mon, 8 Jun 2015 17:32:02 +0000 (20:32 +0300)
committerTim-Philipp Müller <tim@centricular.com>
Mon, 8 Jun 2015 19:25:40 +0000 (20:25 +0100)
When adding a directory to the playlist, the order would be whatever
g_dir_read_name returned. Sorting these using natural sort order.

https://bugzilla.gnome.org/show_bug.cgi?id=750585

tools/gst-play.c

index 26bebd9..2902df3 100644 (file)
@@ -588,6 +588,21 @@ do_play (GstPlay * play)
   g_main_loop_run (play->loop);
 }
 
+static gint
+compare (gconstpointer a, gconstpointer b)
+{
+  gchar *a1, *b1;
+  gint ret;
+
+  a1 = g_utf8_collate_key_for_filename ((gchar *) a, -1);
+  b1 = g_utf8_collate_key_for_filename ((gchar *) b, -1);
+  ret = strcmp (a1, b1);
+  g_free (a1);
+  g_free (b1);
+
+  return ret;
+}
+
 static void
 add_to_playlist (GPtrArray * playlist, const gchar * filename)
 {
@@ -601,17 +616,24 @@ add_to_playlist (GPtrArray * playlist, const gchar * filename)
 
   if ((dir = g_dir_open (filename, 0, NULL))) {
     const gchar *entry;
+    GList *l, *files = NULL;
 
-    /* FIXME: sort entries for each directory? */
     while ((entry = g_dir_read_name (dir))) {
       gchar *path;
 
       path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
-      add_to_playlist (playlist, path);
-      g_free (path);
+      files = g_list_insert_sorted (files, path, compare);
     }
 
     g_dir_close (dir);
+
+    for (l = files; l != NULL; l = l->next) {
+      gchar *path = (gchar *) l->data;
+
+      add_to_playlist (playlist, path);
+      g_free (path);
+    }
+    g_list_free (files);
     return;
   }