[totem] main: Fix shuffled items started to play in random order



commit 10b9bb414e8cc560d56e1d3db5dfd85cfb896d0e
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Apr 10 13:50:00 2014 +0200

    main: Fix shuffled items started to play in random order
    
    We need to add all the shuffled items at once, otherwise they'll
    be added out of order to the playlist, and the item to be played
    might be in the middle of the playlist, instead of being the
    first one.

 src/totem-grilo.c   |    9 +++++----
 src/totem-object.c  |   40 ++++++++++++++++++++++++++++++++++++++++
 src/totem-private.h |    2 ++
 3 files changed, 47 insertions(+), 4 deletions(-)
---
diff --git a/src/totem-grilo.c b/src/totem-grilo.c
index 70b204d..32cacec 100644
--- a/src/totem-grilo.c
+++ b/src/totem-grilo.c
@@ -1988,7 +1988,6 @@ play_selection (TotemGrilo *self,
 {
        GtkTreeModel *model;
        GList *list;
-       gboolean first = TRUE;
        GPtrArray *items;
        guint i;
 
@@ -2008,6 +2007,7 @@ play_selection (TotemGrilo *self,
        g_list_free (list);
 
        totem_object_clear_playlist (self->priv->totem);
+       list = NULL;
 
        for (i = 0; i < items->len; i++) {
                GtkTreePath *path = items->pdata[i];
@@ -2032,10 +2032,8 @@ play_selection (TotemGrilo *self,
                }
 
                title = get_title (media);
-               totem_object_add_to_playlist (self->priv->totem, url,
-                                             title, first);
+               list = g_list_prepend (list, totem_playlist_mrl_data_new (url, title));
                g_free (title);
-               first = FALSE;
 
 next_item:
                g_clear_object (&media);
@@ -2043,6 +2041,9 @@ next_item:
        }
 
        g_ptr_array_free (items, FALSE);
+
+       totem_object_add_items_to_playlist (self->priv->totem, g_list_reverse (list));
+
        g_object_set (G_OBJECT (self->priv->browser), "selection-mode", FALSE, NULL);
 }
 
diff --git a/src/totem-object.c b/src/totem-object.c
index 6900d40..2763701 100644
--- a/src/totem-object.c
+++ b/src/totem-object.c
@@ -619,6 +619,23 @@ totem_object_get_current_time (TotemObject *totem)
        return bacon_video_widget_get_current_time (totem->bvw);
 }
 
+static void
+add_items_to_playlist_and_play_cb (TotemPlaylist *playlist, GAsyncResult *async_result, TotemObject *totem)
+{
+       char *mrl, *subtitle;
+
+       /* totem_playlist_add_mrls_finish() never returns an error */
+       totem_playlist_add_mrls_finish (playlist, async_result, NULL);
+
+       totem_signal_unblock_by_data (playlist, totem);
+
+       /* And start playback */
+       mrl = totem_playlist_get_current_mrl (playlist, &subtitle);
+       totem_object_set_mrl_and_play (totem, mrl, subtitle);
+       g_free (mrl);
+       g_free (subtitle);
+}
+
 typedef struct {
        TotemObject *totem;
        gchar *uri;
@@ -719,6 +736,29 @@ totem_object_add_to_playlist (TotemObject *totem,
 }
 
 /**
+ * totem_object_add_items_to_playlist:
+ * @totem: a #TotemObject
+ * @items: a #GList of #TotemPlaylistMrlData
+ *
+ * Add @items to the playlist and play them immediately.
+ * This function takes ownership of both the list and its elements when
+ * called, so don't free either after calling
+ * totem_object_add_items_to_playlist().
+ **/
+void
+totem_object_add_items_to_playlist (TotemObject *totem,
+                                   GList       *items)
+{
+       /* Block all signals from the playlist until we're finished. They're unblocked in the callback, 
add_to_playlist_and_play_cb.
+        * There are no concurrency issues here, since blocking the signals multiple times should require 
them to be unblocked the
+        * same number of times before they fire again. */
+       totem_signal_block_by_data (totem->playlist, totem);
+
+       totem_playlist_add_mrls (totem->playlist, items, TRUE, NULL,
+                                (GAsyncReadyCallback) add_items_to_playlist_and_play_cb, totem);
+}
+
+/**
  * totem_object_clear_playlist:
  * @totem: a #TotemObject
  *
diff --git a/src/totem-private.h b/src/totem-private.h
index cb6eb5c..817a42f 100644
--- a/src/totem-private.h
+++ b/src/totem-private.h
@@ -209,6 +209,8 @@ void        totem_object_volume_toggle_mute         (TotemObject *totem);
 void   totem_object_set_main_page              (TotemObject *totem,
                                                 const char  *page_id);
 const char * totem_object_get_main_page                (Totem *totem);
+void   totem_object_add_items_to_playlist      (TotemObject *totem,
+                                                GList       *items);
 
 /* Signal emission */
 void   totem_file_has_played                   (TotemObject *totem,


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