[gnome-music/wip/mschraal/core] grltrackerwrapper: Restore old XDG_MUSIC_DIR behaviour



commit 7e51468475d4e66e091799d6b2f2ca6b19a4544c
Author: Marinus Schraal <mschraal gnome org>
Date:   Sun Jul 14 14:47:59 2019 +0200

    grltrackerwrapper: Restore old XDG_MUSIC_DIR behaviour

 gnomemusic/grilowrappers/grltrackerwrapper.py | 82 +++++++++++++++++++++++----
 1 file changed, 71 insertions(+), 11 deletions(-)
---
diff --git a/gnomemusic/grilowrappers/grltrackerwrapper.py b/gnomemusic/grilowrappers/grltrackerwrapper.py
index 5fc7395a..941497dc 100644
--- a/gnomemusic/grilowrappers/grltrackerwrapper.py
+++ b/gnomemusic/grilowrappers/grltrackerwrapper.py
@@ -87,6 +87,27 @@ class GrlTrackerWrapper(GObject.GObject):
     def source(self):
         return self._source
 
+    @staticmethod
+    def _location_filter():
+        try:
+            music_dir = GLib.get_user_special_dir(
+                GLib.UserDirectory.DIRECTORY_MUSIC)
+            assert music_dir is not None
+        except (TypeError, AssertionError):
+            print("XDG Music dir is not set")
+            return
+
+        music_dir = Tracker.sparql_escape_string(
+            GLib.filename_to_uri(music_dir))
+
+        query = """
+        FILTER (STRSTARTS(nie:url(?song), '%(music_dir)s/'))
+        """.replace('\n', ' ').strip() % {
+            'music_dir': music_dir
+        }
+
+        return query
+
     def _on_content_changed(self, source, medias, change_type, loc_unknown):
         for media in medias:
             if change_type == Grl.SourceChangeType.ADDED:
@@ -122,8 +143,11 @@ class GrlTrackerWrapper(GObject.GObject):
                     nmm:performer ?performer .
             OPTIONAL { ?song nmm:composer/nmm:artistName ?composer . }
             OPTIONAL { ?album nmm:albumArtist/nmm:artistName ?album_artist . }
+            %(location_filter)s
         } GROUP BY ?album
-        """.replace('\n', ' ').strip()
+        """.replace('\n', ' ').strip() % {
+            'location_filter': self._location_filter()
+        }
 
         def check_album_cb(source, op_id, media, user_data, error):
             if error:
@@ -168,8 +192,11 @@ class GrlTrackerWrapper(GObject.GObject):
             ?song a nmm:MusicPiece;
                     nmm:musicAlbum ?album;
                     nmm:performer ?artist_class .
+            %(location_filter)s
         } GROUP BY ?artist_class
-        """.replace('\n', ' ').strip()
+        """.replace('\n', ' ').strip() % {
+            'location_filter': self._location_filter()
+        }
 
         def check_artist_cb(source, op_id, media, user_data, error):
             if error:
@@ -241,8 +268,10 @@ class GrlTrackerWrapper(GObject.GObject):
                 FILTER (?tag = nao:predefined-tag-favorite)
             }
             FILTER ( tracker:id(?song) = %(media_id)s )
+            %(location_filter)s
         }
         """.replace('\n', ' ').strip() % {
+            'location_filter': self._location_filter(),
             'media_id': media_id
         }
 
@@ -329,8 +358,11 @@ class GrlTrackerWrapper(GObject.GObject):
                 ?song nao:hasTag ?tag .
                 FILTER (?tag = nao:predefined-tag-favorite)
             }
+            %(location_filter)s
+        }
+        """.replace('\n', ' ').strip() % {
+            'location_filter': self._location_filter()
         }
-        """.replace('\n', ' ').strip()
 
         options = self._fast_options.copy()
         self._source.query(query, self.METADATA_KEYS, options, _add_to_model)
@@ -366,8 +398,11 @@ class GrlTrackerWrapper(GObject.GObject):
                     nmm:performer ?performer .
             OPTIONAL { ?song nmm:composer/nmm:artistName ?composer . }
             OPTIONAL { ?album nmm:albumArtist/nmm:artistName ?album_artist . }
+            %(location_filter)s
         } GROUP BY ?album
-        """.replace('\n', ' ').strip()
+        """.replace('\n', ' ').strip() % {
+            'location_filter': self._location_filter()
+        }
 
         options = self._fast_options.copy()
 
@@ -398,8 +433,11 @@ class GrlTrackerWrapper(GObject.GObject):
             ?song a nmm:MusicPiece;
                     nmm:musicAlbum ?album;
                     nmm:performer ?artist .
+            %(location_filter)s
         } GROUP BY ?artist
-        """.replace('\n', ' ').strip()
+        """.replace('\n', ' ').strip() % {
+            'location_filter': self._location_filter()
+        }
 
         options = self._fast_options.copy()
 
@@ -427,9 +465,11 @@ class GrlTrackerWrapper(GObject.GObject):
                     nmm:performer ?artist .
             FILTER ( tracker:id(?album_artist) = %(artist_id)s
                      || tracker:id(?artist) = %(artist_id)s )
+            %(location_filter)s
         }
         """.replace('\n', ' ').strip() % {
-            'artist_id': int(artist_id)
+            'artist_id': int(artist_id),
+            'location_filter': self._location_filter()
         }
 
         albums = []
@@ -473,9 +513,11 @@ class GrlTrackerWrapper(GObject.GObject):
             ?song a nmm:MusicPiece;
                     nmm:musicAlbum ?album .
             FILTER ( tracker:id(?album) = %(album_id)s )
+            %(location_filter)s
         }
         """.replace('\n', ' ').strip() % {
-            'album_id': int(album_id)
+            'album_id': int(album_id),
+            'location_filter': self._location_filter()
         }
 
         def _disc_nr_cb(source, op_id, media, user_data, error):
@@ -528,10 +570,12 @@ class GrlTrackerWrapper(GObject.GObject):
             FILTER ( tracker:id(?album) = %(album_id)s
                      && nmm:setNumber(nmm:musicAlbumDisc(?song)) = %(disc_nr)s
             )
+            %(location_filter)s
         }
         """.replace('\n', ' ').strip() % {
             'album_id': album_id,
             'disc_nr': disc_nr,
+            'location_filter': self._location_filter()
         }
 
         options = self._fast_options.copy()
@@ -570,8 +614,12 @@ class GrlTrackerWrapper(GObject.GObject):
                     tracker:unaccent(?match4)), "%(name)s")
                 || CONTAINS(tracker:case-fold(?match4), "%(name)s")
             )
+            %(location_filter)s
+        }
+        """.replace('\n', ' ').strip() % {
+            'location_filter': self._location_filter(),
+            'name': term
         }
-        """.replace('\n', ' ').strip() % {'name': term}
 
         filter_ids = []
 
@@ -621,8 +669,12 @@ class GrlTrackerWrapper(GObject.GObject):
                     tracker:unaccent(?match4)), "%(name)s")
                 || CONTAINS(tracker:case-fold(?match4), "%(name)s")
             )
+            %(location_filter)s
+        }
+        """.replace('\n', ' ').strip() % {
+            'location_filter': self._location_filter(),
+            'name': term
         }
-        """.replace('\n', ' ').strip() % {'name': term}
 
         album_filter_ids = []
 
@@ -674,8 +726,12 @@ class GrlTrackerWrapper(GObject.GObject):
                     tracker:unaccent(?match4)), "%(name)s")
                 || CONTAINS(tracker:case-fold(?match4), "%(name)s")
             )
+            %(location_filter)s
+        }
+        """.replace('\n', ' ').strip() % {
+            'location_filter': self._location_filter(),
+            'name': term
         }
-        """.replace('\n', ' ').strip() % {'name': term}
 
         artist_filter_ids = []
 
@@ -737,9 +793,11 @@ class GrlTrackerWrapper(GObject.GObject):
             FILTER (
                 tracker:id(?album) = %(album_id)s
             )
+            %(location_filter)s
         }
         """.replace("\n", " ").strip() % {
                 'album_id': album_id,
+                'location_filter': self._location_filter()
         }
 
         return query
@@ -766,9 +824,11 @@ class GrlTrackerWrapper(GObject.GObject):
                 NOT EXISTS { ?song a nmm:Video }
                 && NOT EXISTS { ?song a nmm:Playlist }
             )
+            %(location_filter)s
         }
         """.replace("\n", " ").strip() % {
-                'song_id': song_id
+            'location_filter': self._location_filter(),
+            'song_id': song_id
         }
 
         return query


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