[gnome-music/wip/carlosg/tracker3: 23/55] trackerwrapper: Make last-played property updates go through tracker




commit 84e9d36233c907aa9c6cf945c721f500aee37e29
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Jun 14 16:22:16 2020 +0200

    trackerwrapper: Make last-played 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 | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)
---
diff --git a/gnomemusic/coresong.py b/gnomemusic/coresong.py
index 5acc7010..6099b3d5 100644
--- a/gnomemusic/coresong.py
+++ b/gnomemusic/coresong.py
@@ -166,5 +166,5 @@ class CoreSong(GObject.GObject):
             return
 
         self.props.media.set_last_played(GLib.DateTime.new_now_utc())
-        self._coregrilo.writeback(
-            self.props.media, Grl.METADATA_KEY_LAST_PLAYED)
+        self._coregrilo.writeback_tracker(
+            self.props.media, "last-played")
diff --git a/gnomemusic/trackerwrapper.py b/gnomemusic/trackerwrapper.py
index 37fca3c7..6c3d4fa9 100644
--- a/gnomemusic/trackerwrapper.py
+++ b/gnomemusic/trackerwrapper.py
@@ -183,6 +183,31 @@ class TrackerWrapper(GObject.GObject):
         self._tracker.update_async(
             update, GLib.PRIORITY_LOW, None, _update_play_count_cb)
 
+    def _update_last_played(self, media):
+        last_played = media.get_last_played().format_iso8601()
+        update = """
+        DELETE WHERE {
+            <%(urn)s> nie:contentAccessed ?accessed
+        };
+        INSERT DATA {
+            <%(urn)s> a nmm:MusicPiece ;
+                      nie:contentAccessed "%(last_played)s"
+        }
+        """.replace("\n", "").strip() % {
+            "urn": media.get_id(),
+            "last_played": last_played,
+        }
+
+        def _update_last_played_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_last_played_cb)
+
     def update_tag(self, media, tag):
         """Update property of a resource.
 
@@ -191,6 +216,8 @@ class TrackerWrapper(GObject.GObject):
         """
         if tag == "favorite":
             self._update_favorite(media)
+        elif tag == "last-played":
+            self._update_last_played(media)
         elif tag == "play-count":
             self._update_play_count(media)
         else:


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