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



commit b6a13fe9896e47b1d7c44c19b821071b5c9ed018
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 0c40fa07..b2477a8d 100644
--- a/gnomemusic/coresong.py
+++ b/gnomemusic/coresong.py
@@ -144,8 +144,8 @@ 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, deque(["last-played"]))
 
     def query_musicbrainz_tags(self, callback):
         """Retrieves metadata keys for this CoreSong
diff --git a/gnomemusic/trackerwrapper.py b/gnomemusic/trackerwrapper.py
index 2b884d23..4f8f1269 100644
--- a/gnomemusic/trackerwrapper.py
+++ b/gnomemusic/trackerwrapper.py
@@ -618,6 +618,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_album(self, media, tags):
         query_delete = """
         DELETE {
@@ -709,6 +734,8 @@ class TrackerWrapper(GObject.GObject):
             self._update_favorite(media)
         elif tag == "play-count":
             self._update_play_count(media)
+        elif tag == "last-played":
+            self._update_last_played(media)
         else:
             self._log.warning("Unknown tag: '{}'".format(tag))
             self.update_tags(media, tags)


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