[gnome-music/wip/mschraal/core: 24/164] VERY FLAKEY REMOVAL



commit f29f0066a8b8f4efef421a31902876da3264690c
Author: Marinus Schraal <mschraal gnome org>
Date:   Thu May 9 01:00:52 2019 +0200

    VERY FLAKEY REMOVAL

 gnomemusic/coregrilo.py | 16 ++++++++++++++--
 gnomemusic/coremodel.py | 40 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 49 insertions(+), 7 deletions(-)
---
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index 91f918c1..8f33aa77 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -7,6 +7,10 @@ from gnomemusic.coresong import CoreSong
 
 class CoreGrilo(GObject.GObject):
 
+    __gsignals__ = {
+        "media-removed": (GObject.SignalFlags.RUN_FIRST, None, (Grl.Media,))
+    }
+
     METADATA_KEYS = [
         Grl.METADATA_KEY_ALBUM,
         Grl.METADATA_KEY_ALBUM_ARTIST,
@@ -28,11 +32,13 @@ class CoreGrilo(GObject.GObject):
     def __repr__(self):
         return "<CoreGrilo>"
 
-    def __init__(self, model, table):
+    def __init__(self, model, table, url_hash):
         super().__init__()
 
         self._model = model
         self._table = table
+        # Only way to figure out removed items
+        self._url_table = url_hash
 
         Grl.init(None)
 
@@ -58,12 +64,17 @@ class CoreGrilo(GObject.GObject):
                 "content-changed", self._on_content_changed)
 
     def _on_content_changed(self, source, medias, change_type, loc_unknown):
-        print("Content changed")
+        # print("Content changed")
 
         for media in medias:
             if change_type == Grl.SourceChangeType.CHANGED:
                 print("CHANGED", media.get_id())
                 self._requery_media(media.get_id())
+            if change_type == Grl.SourceChangeType.REMOVED:
+                # print("REMOVED", media.get_id(), media.get_title(), media.get_url())
+                # print(self._table[media.get_id()])
+                self.emit("media-removed", media)
+                # print(self._table[media.get_id()])
 
     def _requery_media(self, grilo_id):
         query = """
@@ -161,5 +172,6 @@ class CoreGrilo(GObject.GObject):
         song = CoreSong(media)
         self._model.append(song)
         self._table[media.get_id()] = song
+        self._url_table[media.get_url()] = song
 
         # print(song.props.title, song.props.url)
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 2e3234fa..c67dc922 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -12,9 +12,12 @@ class CoreModel(GObject.GObject):
         super().__init__()
 
         self._model = Gio.ListStore()
+        self._album_store = None
         self._hash = {}
+        self._url_hash = {}
 
-        self._grilo = CoreGrilo(self._model, self._hash)
+        self._grilo = CoreGrilo(self._model, self._hash, self._url_hash)
+        self._grilo.connect("media-removed", self._on_media_removed)
 
     @log
     def get_model(self):
@@ -22,7 +25,7 @@ class CoreModel(GObject.GObject):
 
     @log
     def get_album_model(self, media):
-        store = Gio.ListStore()
+        self._album_store = Gio.ListStore()
         album_id = media.get_id()
 
         def _reverse_sort(song_a, song_b):
@@ -30,15 +33,42 @@ class CoreModel(GObject.GObject):
 
         def _callback(source, dunno, media, something, something2):
             if media is None:
-                store.sort(_reverse_sort)
+                self._album_store.sort(_reverse_sort)
                 return
 
             print("media", media)
 
             song = self._hash[media.get_id()]
-            store.append(song)
+            self._album_store.append(song)
 
         # For POC sake, use old grilo
         grilo.populate_album_songs(media, _callback)
 
-        return store
+        return self._album_store
+
+    @log
+    def _on_media_removed(self, klass, media):
+        try:
+            old_song = self._url_hash[media.get_url()]
+            print("SUCCES")
+        except KeyError:
+            print("KeyError", media.get_url())
+            return
+
+        for i in range(self._model.get_n_items()):
+            if old_song == self._model[i]:
+                print("REMOVING index", i)
+                self._model.remove(i)
+                break
+
+        if self._album_store is not None:
+            for i in range(self._album_store.get_n_items()):
+                if old_song == self._album_store[i]:
+                    print("REMOVING index", i)
+                    self._album_store.remove(i)
+                    break
+        
+        print("pop1", self._hash.pop(old_song._media.get_id()))
+        print("pop2", self._url_hash.pop(media.get_url()))
+
+                # print("ITEM IN MODEL", media.get_id(), self._url_hash[media.get_url()]._media.get_id())


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