[gnome-music/wip/mschraal/core: 113/208] coredisc: Move more here



commit 1e019428ca05640cf6c2e3d98081c8462945c2cf
Author: Marinus Schraal <mschraal gnome org>
Date:   Fri Jun 21 21:29:05 2019 +0200

    coredisc: Move more here

 gnomemusic/coredisc.py                       | 34 +++++++++++++++++++++----
 gnomemusic/coremodel.py                      | 26 -------------------
 gnomemusic/grilowrappers/grltrackersource.py | 38 ++++++++++++++++++++--------
 3 files changed, 57 insertions(+), 41 deletions(-)
---
diff --git a/gnomemusic/coredisc.py b/gnomemusic/coredisc.py
index e5c357f9..a8e9319d 100644
--- a/gnomemusic/coredisc.py
+++ b/gnomemusic/coredisc.py
@@ -17,6 +17,7 @@ class CoreDisc(GObject.GObject):
         super().__init__()
 
         self._coremodel = coremodel
+        self._old_album_ids = []
         self.props.disc_nr = nr
 
         self._filter_model = Dazzle.ListModelFilter.new(
@@ -30,22 +31,21 @@ class CoreDisc(GObject.GObject):
 
         self._coremodel.get_model().connect(
             "items-changed", self._on_core_changed)
-        self.props.model.connect("items-changed", self._on_list_items_changed)
 
-        self._coremodel._get_album_disc(
+        self._get_album_disc(
             self.props.media, self.props.disc_nr, self._filter_model)
 
     def update(self, media):
         self.props.media = media
 
     def _on_core_changed(self, model, position, removed, added):
-        self._coremodel._get_album_disc(
+        self._get_album_disc(
             self.props.media, self.props.disc_nr, self._filter_model)
 
-    def _on_list_items_changed(self, model, pos, removed, added):
+    def _update_duration(self):
         duration = 0
 
-        for coresong in model:
+        for coresong in self.props.model:
             duration += coresong.props.duration
 
         self.props.duration = duration
@@ -61,3 +61,27 @@ class CoreDisc(GObject.GObject):
             return func(a, b, *user_data)
 
         return wrap
+
+    def _get_album_disc(self, media, discnr, model):
+        album_ids = []
+        model_filter = model
+
+        def _filter_func(core_song):
+            return core_song.props.media.get_id() in album_ids
+
+        def _reverse_sort(song_a, song_b, data=None):
+            return song_a.props.track_number - song_b.props.track_number
+
+        def _callback(source, dunno, media, something, something2):
+            if media is None:
+                if sorted(album_ids) == sorted(self._old_album_ids):
+                    return
+                model_filter.set_filter_func(_filter_func)
+                self._old_album_ids = album_ids
+                self._update_duration()
+                return
+
+            album_ids.append(media.get_id())
+
+        self._coremodel._grilo.populate_album_disc_songs(
+            media, discnr, _callback)
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index f668bb24..0cbebe81 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -180,32 +180,6 @@ class CoreModel(GObject.GObject):
 
                 self.emit("playlist-loaded")
 
-
-    def _get_album_disc(self, media, discnr, model):
-        albums_ids = []
-        model_filter = model
-
-        def _filter_func(core_song):
-            return core_song.props.media.get_id() in albums_ids
-
-        def _reverse_sort(song_a, song_b, data=None):
-            return song_a.props.track_number - song_b.props.track_number
-
-        def _callback(source, dunno, media, something, something2):
-            if media is None:
-                model_filter.set_filter_func(_filter_func)
-                # model_sort.set_model(model_filter)
-                # model_sort.set_sort_func(
-                #     self._wrap_list_store_sort_func(_reverse_sort))
-                return
-
-            albums_ids.append(media.get_id())
-
-        # For POC sake, use old grilo
-        self._grilo.populate_album_disc_songs(media, discnr, _callback)
-
-        return model_filter
-
     @log
     def get_albums_model(self):
         return self._album_model_sort
diff --git a/gnomemusic/grilowrappers/grltrackersource.py b/gnomemusic/grilowrappers/grltrackersource.py
index 0ff55dba..2039f464 100644
--- a/gnomemusic/grilowrappers/grltrackersource.py
+++ b/gnomemusic/grilowrappers/grltrackersource.py
@@ -69,7 +69,7 @@ class GrlTrackerSource(GObject.GObject):
                 self._add_media(media)
             elif change_type == Grl.SourceChangeType.CHANGED:
                 print("CHANGED", media.get_id())
-                self._requery_media(media.get_id())
+                self._requery_media(media.get_id(), True)
             elif change_type == Grl.SourceChangeType.REMOVED:
                 print("REMOVED", media.get_id())
                 self._remove_media(media)
@@ -78,14 +78,18 @@ class GrlTrackerSource(GObject.GObject):
     def _remove_media(self, media):
         removed_url = media.get_url()
 
-        coresong = self._hash.pop(media.get_id())
+        try:
+            coresong = self._hash.pop(media.get_id())
+        except KeyError:
+            return
+
         for idx, coresong_model in enumerate(self._model):
             if coresong_model is coresong:
                 print(
                     "removing", coresong.props.media.get_id(),
                     coresong.props.title)
                 self._model.remove(idx)
-                # break
+                break
         # for idx, coresong in enumerate(self._model):
         #     print(coresong.props.title)
         #     if coresong.props.url == removed_url:
@@ -93,7 +97,7 @@ class GrlTrackerSource(GObject.GObject):
         #         self._model.remove(idx)
         #         self._hash.pop(coresong.media.get_id(), 0)
 
-    def _requery_media(self, grilo_id):
+    def _requery_media(self, grilo_id, only_update=False):
         query = """
             SELECT DISTINCT
                 rdf:type(?song)
@@ -124,13 +128,18 @@ class GrlTrackerSource(GObject.GObject):
 
         options = self._fast_options.copy()
 
-        self._source.query(
-            query, self.METADATA_KEYS, options, self._update_media)
+        if only_update:
+            self._source.query(
+                query, self.METADATA_KEYS, options, self._only_update_media)
+        else:
+            self._source.query(
+                query, self.METADATA_KEYS, options, self._update_media)
 
     def _add_media(self, media):
         self._requery_media(media.get_id())
 
-    def _update_media(self, source, op_id, media, user_data, error):
+
+    def _only_update_media(self, source, op_id, media, user_data, error):
         if error:
             print("ERROR", error)
             return
@@ -139,11 +148,20 @@ class GrlTrackerSource(GObject.GObject):
             # print("NO MEDIA", source, op_id, media, error)
             return
 
-        def _onic(model, pos, r, a):
-            print(model, pos, r, a)
+        print("ONLY UPDATE")
+        self._hash[media.get_id()].update(media)
+        print("UPDATE ID", media.get_id(), media.get_title())
+
+    def _update_media(self, source, op_id, media, user_data, error):
+        if error:
+            print("ERROR", error)
+            return
+
+        if not media:
+            # print("NO MEDIA", source, op_id, media, error)
+            return
 
         song = CoreSong(media)
-        self._model.connect("items-changed", _onic)
         self._model.append(song)
         self._hash[media.get_id()] = song
 


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