[gnome-music/wip/jfelder/playlists-core-rewrite-prep-work: 7/11] mpris: Use the playlists managed by Playlists



commit a55513f269d7a197dfe6ff64789bcd3768d5f448
Author: Jean Felder <jfelder src gnome org>
Date:   Mon Jul 1 23:39:14 2019 +0200

    mpris: Use the playlists managed by Playlists
    
    Instead of relying on grilo to populate the dialog with playlists, use
    the new API from Playlists class.
    
    It is based on some work done by Georges Basile Stavracas Neto.

 gnomemusic/mpris.py               | 83 ++++++++++++++++-----------------------
 gnomemusic/playlists.py           | 23 +++++++++--
 gnomemusic/views/playlistsview.py | 10 ++---
 3 files changed, 57 insertions(+), 59 deletions(-)
---
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index 7adec777..44b87a1f 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -29,7 +29,6 @@ from gi.repository import Gio, GLib
 
 from gnomemusic import log
 from gnomemusic.albumartcache import lookup_art_file_from_cache
-from gnomemusic.grilo import grilo
 from gnomemusic.gstplayer import Playback
 from gnomemusic.player import PlayerPlaylist, RepeatMode
 from gnomemusic.playlists import Playlists
@@ -293,13 +292,10 @@ class MPRIS(DBusInterface):
             'playlist-changed', self._on_player_playlist_changed)
 
         self._playlists = Playlists.get_default()
-        self._playlists.connect(
-            'playlist-created', self._on_playlists_count_changed)
-        self._playlists.connect(
-            'playlist-deleted', self._on_playlists_count_changed)
+        self._playlists_model = None
         self._playlists.connect('playlist-renamed', self._on_playlist_renamed)
-        grilo.connect('ready', self._on_grilo_ready)
-        self._stored_playlists = []
+        self._playlists.connect("notify::ready", self._on_playlists_loading)
+
         self._player_previous_type = None
         self._path_list = []
         self._metadata_list = []
@@ -437,37 +433,23 @@ class MPRIS(DBusInterface):
     def _get_playlist_dbus_path(self, playlist):
         """Convert a playlist to a D-Bus path
 
-        :param Grl.media playlist: The playlist object
+        :param Playlist playlist: The playlist object
         :return: a D-Bus id to uniquely identify the playlist
         :rtype: str
         """
         if playlist:
-            id_ = playlist.get_id()
+            pl_id = playlist.props.pl_id
         else:
-            id_ = 'Invalid'
-
-        return '/org/mpris/MediaPlayer2/Playlist/{}'.format(id_)
+            pl_id = "Invalid"
 
-    @log
-    def _get_playlist_from_dbus_path(self, playlist_path):
-        for playlist in self._stored_playlists:
-            if playlist_path == self._get_playlist_dbus_path(playlist):
-                return playlist
-        return None
+        return "/org/mpris/MediaPlayer2/Playlist/{}".format(pl_id)
 
     @log
     def _get_mpris_playlist_from_playlist(self, playlist):
-        playlist_name = utils.get_media_title(playlist)
+        playlist_name = playlist.props.title
         path = self._get_playlist_dbus_path(playlist)
         return (path, playlist_name, "")
 
-    @log
-    def _get_playlist_from_id(self, playlist_id):
-        for playlist in self._stored_playlists:
-            if playlist_id == playlist.get_id():
-                return playlist
-        return None
-
     @log
     def _get_active_playlist(self):
         """Get Active Maybe_Playlist
@@ -484,7 +466,8 @@ class MPRIS(DBusInterface):
         if self._player.get_playlist_type() != PlayerPlaylist.Type.PLAYLIST:
             return (False, ("/", "", ""))
 
-        playlist = self._get_playlist_from_id(self._player.get_playlist_id())
+        playlist_id = self._player.get_playlist_id()
+        playlist = self._playlists.get_playlist_from_id(playlist_id)
         mpris_playlist = self._get_mpris_playlist_from_playlist(playlist)
         return (True, mpris_playlist)
 
@@ -571,23 +554,21 @@ class MPRIS(DBusInterface):
         self._properties_changed(
             MPRIS.MEDIA_PLAYER2_PLAYLISTS_IFACE, properties, [])
 
-    @log
-    def _reload_playlists(self):
-        def _populate_cb(source, param, item, remaining=0, data=None):
-            if item:
-                self._stored_playlists.append(item)
-            else:
-                playlists_nr = len(self._stored_playlists)
-                self._properties_changed(
-                    MPRIS.MEDIA_PLAYER2_PLAYLISTS_IFACE,
-                    {'PlaylistCount': GLib.Variant('u', playlists_nr), }, [])
-
-        self._stored_playlists = []
-        grilo.populate_playlists(0, _populate_cb)
+    def _on_playlists_loading(self, klass, param):
+        if not self._playlists.props.ready:
+            return
+
+        self._playlists_model = self._playlists.get_playlists()
+        self._playlists_model.connect(
+            "items-changed", self._on_playlists_count_changed)
+        self._on_playlists_count_changed(None, None, None, None)
 
     @log
-    def _on_playlists_count_changed(self, playlists, item):
-        self._reload_playlists()
+    def _on_playlists_count_changed(self, klass, position, removed, added):
+        playlist_count = len(self._playlists_model)
+        properties = {"PlaylistCount": GLib.Variant("u", playlist_count)}
+        self._properties_changed(
+            MPRIS.MEDIA_PLAYER2_PLAYLISTS_IFACE, properties, [])
 
     @log
     def _on_playlist_renamed(self, playlists, renamed_playlist):
@@ -595,10 +576,6 @@ class MPRIS(DBusInterface):
             renamed_playlist)
         self._dbus_emit_signal('PlaylistChanged', {'Playlist': mpris_playlist})
 
-    @log
-    def _on_grilo_ready(self, grilo):
-        self._reload_playlists()
-
     def _raise(self):
         """Brings user interface to the front (MPRIS Method)."""
         self._app.do_activate()
@@ -734,8 +711,14 @@ class MPRIS(DBusInterface):
 
         :param str playlist_path: The id of the playlist to activate.
         """
-        playlist_id = self._get_playlist_from_dbus_path(playlist_path).get_id()
-        self._playlists.activate_playlist(playlist_id)
+        selected_playlist = None
+        for playlist in self._playlists_model:
+            if playlist_path == self._get_playlist_dbus_path(playlist):
+                selected_playlist = playlist
+                break
+
+        if selected_playlist is not None:
+            self._playlists.activate_playlist(selected_playlist)
 
     def _get_playlists(self, index, max_count, order, reverse):
         """Gets a set of playlists (MPRIS Method).
@@ -751,7 +734,7 @@ class MPRIS(DBusInterface):
             return []
 
         mpris_playlists = [self._get_mpris_playlist_from_playlist(playlist)
-                           for playlist in self._stored_playlists]
+                           for playlist in self._playlists_model]
 
         if not reverse:
             return mpris_playlists[index:index + max_count]
@@ -822,7 +805,7 @@ class MPRIS(DBusInterface):
                 'CanEditTracks': GLib.Variant('b', False)
             }
         elif interface_name == MPRIS.MEDIA_PLAYER2_PLAYLISTS_IFACE:
-            playlist_count = len(self._stored_playlists)
+            playlist_count = len(self._playlists_model)
             active_playlist = self._get_active_playlist()
             return {
                 'PlaylistCount': GLib.Variant('u', playlist_count),
diff --git a/gnomemusic/playlists.py b/gnomemusic/playlists.py
index a36044d3..3b9d9aa2 100644
--- a/gnomemusic/playlists.py
+++ b/gnomemusic/playlists.py
@@ -158,7 +158,8 @@ class Favorites(SmartPlaylist):
 class Playlists(GObject.GObject):
 
     __gsignals__ = {
-        'activate-playlist': (GObject.SignalFlags.RUN_FIRST, None, (str,)),
+        "activate-playlist": (
+            GObject.SignalFlags.RUN_FIRST, None, (Playlist,)),
         'playlist-created': (
             GObject.SignalFlags.RUN_FIRST, None, (Grl.Media,)
         ),
@@ -563,6 +564,20 @@ class Playlists(GObject.GObject):
         model_filter.set_filter_func(user_playlists_filter)
         return model_filter
 
+    @log
+    def get_playlist_from_id(self, playlist_id):
+        """Gets a playlist from its id.
+
+        :param str playlist_id: playlist id
+        :returns: the corresponding playlist
+        :rtype: Playlist
+
+        """
+        for playlist in self._playlists_model:
+            if playlist_id == playlist.props.pl_id:
+                return playlist
+        return None
+
     @log
     def get_smart_playlist(self, name):
         """SmartPlaylist getter
@@ -574,15 +589,15 @@ class Playlists(GObject.GObject):
         return self._smart_playlists[name]
 
     @log
-    def activate_playlist(self, playlist_id):
+    def activate_playlist(self, playlist):
         """Activates a playlist.
 
         Selects a playlist and start playing.
 
-        :param str playlist_id: playlist id
+        :param Playlist playlist: playlist
         """
         # FIXME: just a proxy
-        self.emit('activate-playlist', playlist_id)
+        self.emit("activate-playlist", playlist)
 
     @log
     def stage_playlist_for_deletion(self, playlist, index):
diff --git a/gnomemusic/views/playlistsview.py b/gnomemusic/views/playlistsview.py
index 7c1fb247..cf6804a1 100644
--- a/gnomemusic/views/playlistsview.py
+++ b/gnomemusic/views/playlistsview.py
@@ -481,7 +481,7 @@ class PlaylistsView(BaseView):
                 break
 
     @log
-    def _on_playlist_activation_request(self, klass, playlist_id):
+    def _on_playlist_activation_request(self, klass, playlist):
         """Selects and starts playing a playlist.
 
         If the view has not been populated yet, populate it and then
@@ -489,16 +489,16 @@ class PlaylistsView(BaseView):
         requested playlist and start playing.
 
         :param Playlists klass: Playlists object
-        :param str playlist_id: requested playlist id
+        :param Playlist playlist: requested playlist
         """
         if not self._init:
             self._plays_songs_on_activation = True
-            self._populate(playlist_id)
+            self._populate(playlist.props.pl_id)
             return
 
         playlist_row = None
         for row in self._sidebar:
-            if row.playlist.props.pl_id == playlist_id:
+            if row.playlist == playlist:
                 playlist_row = row
                 break
 
@@ -506,7 +506,7 @@ class PlaylistsView(BaseView):
             return
 
         selection = self._sidebar.get_selected_row()
-        if selection.get_index() == row.get_index():
+        if selection.get_index() == playlist_row.get_index():
             self._on_play_activate(None)
         else:
             self._plays_songs_on_activation = True


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