[gnome-music/wip/carlosg/tracker3: 4/39] trackerwrapper: Make favorite property updates go through tracker




commit f169d0dc7cdc0696ffd32dce7bd15aa43a26c50f
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Jun 14 15:50:51 2020 +0200

    trackerwrapper: Make favorite property updates go through tracker
    
    This property cannot be written back via grilo with the Tracker3 ownership
    changes. This should be stored in the local store, thus it's better to
    handle it via the trackerwrapper writeback methods.

 gnomemusic/coregrilo.py      | 12 ++++++++++++
 gnomemusic/coresong.py       |  3 ++-
 gnomemusic/trackerwrapper.py | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 1 deletion(-)
---
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index cf5ef453..d2b04f78 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -214,6 +214,18 @@ class CoreGrilo(GObject.GObject):
                     None)
                 break
 
+    def writeback_tracker(self, media, tag):
+        """Use Tracker queries to update tags.
+
+        The tags are associated with a Tracker resource
+        (song, album, artist or external resource), so they can cannot
+        be updated with grilo writeback support.
+
+        :param Grl.Media media: A Grilo media item
+        :param str tag: tag to update
+        """
+        self._tracker_wrapper.update_tag(media, tag)
+
     def search(self, text):
         for wrapper in self._wrappers.values():
             wrapper.search(text)
diff --git a/gnomemusic/coresong.py b/gnomemusic/coresong.py
index 4d9c7819..e4598fa1 100644
--- a/gnomemusic/coresong.py
+++ b/gnomemusic/coresong.py
@@ -102,7 +102,8 @@ class CoreSong(GObject.GObject):
             return
 
         self.props.media.set_favourite(self._favorite)
-        self._coregrilo.writeback(self.props.media, Grl.METADATA_KEY_FAVOURITE)
+        self._coregrilo.writeback_tracker(
+            self.props.media, "favorite")
 
     @GObject.Property(type=bool, default=False)
     def selected(self):
diff --git a/gnomemusic/trackerwrapper.py b/gnomemusic/trackerwrapper.py
index 98ef87e9..a8a683c0 100644
--- a/gnomemusic/trackerwrapper.py
+++ b/gnomemusic/trackerwrapper.py
@@ -125,3 +125,47 @@ class TrackerWrapper(GObject.GObject):
             music_dir)
 
         return query
+
+    def _update_favorite(self, media):
+        """Update favorite state of a song
+
+        :param Grl.Media media: media which contains updated favorite state
+        """
+        if (media.get_favourite()):
+            update = """
+            INSERT DATA {
+                <%(urn)s> a nmm:MusicPiece ;
+                          nao:hasTag nao:predefined-tag-favorite .
+            }
+            """.replace("\n", "").strip() % {
+                "urn": media.get_id(),
+            }
+        else:
+            update = """
+            DELETE DATA {
+                <%(urn)s> nao:hasTag nao:predefined-tag-favorite .
+            }
+            """.replace("\n", "").strip() % {
+                "urn": media.get_id(),
+            }
+
+        def _update_favorite_cb(conn, res):
+            try:
+                conn.update_finish(res)
+            except GLib.Error as e:
+                self._log.warning("Unable to update favorite: {}".format(
+                    e.message))
+
+        self._tracker.update_async(
+            update, GLib.PRIORITY_LOW, None, _update_favorite_cb)
+
+    def update_tag(self, media, tag):
+        """Update property of a resource.
+
+        :param Grl.Media media: media which contains updated tag
+        :param str tag: tag to update
+        """
+        if tag == "favorite":
+            self._update_favorite(media)
+        else:
+            self._log.warning("Unknown tag: '{}'".format(tag))


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