[gnome-music/wip/jfelder/tracker3-rebased: 16/49] trackerwrapper: Make play-count property updates go through tracker




commit 36aa5889cd6007825ca251b86a24239ffffdcf53
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Jun 14 16:14:30 2020 +0200

    trackerwrapper: Make play-count 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/coresong.py       |  4 ++--
 gnomemusic/trackerwrapper.py | 26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)
---
diff --git a/gnomemusic/coresong.py b/gnomemusic/coresong.py
index 5536a734..5acc7010 100644
--- a/gnomemusic/coresong.py
+++ b/gnomemusic/coresong.py
@@ -158,8 +158,8 @@ class CoreSong(GObject.GObject):
             return
 
         self.props.media.set_play_count(self.props.play_count + 1)
-        self._coregrilo.writeback(
-            self.props.media, Grl.METADATA_KEY_PLAY_COUNT)
+        self._coregrilo.writeback_tracker(
+            self.props.media, "play-count")
 
     def set_last_played(self):
         if not self._is_tracker:
diff --git a/gnomemusic/trackerwrapper.py b/gnomemusic/trackerwrapper.py
index a8a683c0..37fca3c7 100644
--- a/gnomemusic/trackerwrapper.py
+++ b/gnomemusic/trackerwrapper.py
@@ -159,6 +159,30 @@ class TrackerWrapper(GObject.GObject):
         self._tracker.update_async(
             update, GLib.PRIORITY_LOW, None, _update_favorite_cb)
 
+    def _update_play_count(self, media):
+        update = """
+        DELETE WHERE {
+            <%(urn)s> nie:usageCounter ?count .
+        };
+        INSERT DATA {
+            <%(urn)s> a nmm:MusicPiece ;
+                      nie:usageCounter %(count)d .
+        }
+        """.replace("\n", "").strip() % {
+            "urn": media.get_id(),
+            "count": media.get_play_count(),
+        }
+
+        def _update_play_count_cb(conn, res):
+            try:
+                conn.update_finish(res)
+            except GLib.Error as e:
+                self._log.warning("Unable to update play count: {}".format(
+                    e.message))
+
+        self._tracker.update_async(
+            update, GLib.PRIORITY_LOW, None, _update_play_count_cb)
+
     def update_tag(self, media, tag):
         """Update property of a resource.
 
@@ -167,5 +191,7 @@ class TrackerWrapper(GObject.GObject):
         """
         if tag == "favorite":
             self._update_favorite(media)
+        elif tag == "play-count":
+            self._update_play_count(media)
         else:
             self._log.warning("Unknown tag: '{}'".format(tag))


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