[gnome-music/gnome-3-12] query: limit uris to xdg music and downloads folder



commit 7fb5825dbd2d7c9ce00184d901a6f8e8bd1013c7
Author: Vadim Rutkovsky <vrutkovs redhat com>
Date:   Mon May 12 17:51:12 2014 +0200

    query: limit uris to xdg music and downloads folder
    
    https://bugzilla.gnome.org/show_bug.cgi?id=706948

 gnomemusic/grilo.py   |    6 +-
 gnomemusic/query.py   |  523 +++++++++++++++++++++++++++++--------------------
 gnomemusic/view.py    |    4 +-
 gnomemusic/widgets.py |    2 +-
 gnomemusic/window.py  |    4 +-
 5 files changed, 317 insertions(+), 222 deletions(-)
---
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 409d8ff..efcea25 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -142,15 +142,15 @@ class Grilo(GObject.GObject):
 
     @log
     def populate_artists(self, offset, callback, count=-1):
-        self.populate_items(Query.ARTISTS, offset, callback, count)
+        self.populate_items(Query.get_artists(), offset, callback, count)
 
     @log
     def populate_albums(self, offset, callback, count=50):
-        self.populate_items(Query.ALBUMS, offset, callback, count)
+        self.populate_items(Query.get_all_albums(), offset, callback, count)
 
     @log
     def populate_songs(self, offset, callback, count=-1):
-        self.populate_items(Query.SONGS, offset, callback, count)
+        self.populate_items(Query.get_songs(), offset, callback, count)
 
     @log
     def populate_album_songs(self, album_id, callback, count=-1):
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index efe8f73..fd0e376 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -25,226 +25,287 @@
 # code, but you are not obligated to do so.  If you do not wish to do so,
 # delete this exception statement from your version.
 
+from gi.repository import Tracker
+from gi.repository import GLib
+
 
 class Query():
 
-    ALBUMS = '''
-    SELECT DISTINCT
-        rdf:type(?album)
-        tracker:id(?album) AS id
-        (
-            SELECT
-                nmm:artistName(?artist)
-            WHERE {
-                ?album nmm:albumArtist ?artist
-            }
-            LIMIT 1
-        ) AS artist
-        nie:title(?album) AS title
-        nie:title(?album) AS album
-        tracker:coalesce(
+    MUSIC_DIR = Tracker.sparql_escape_string(GLib.filename_to_uri(
+        GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_MUSIC)
+    ))
+
+    DOWNLOAD_DIR = Tracker.sparql_escape_string(GLib.filename_to_uri(
+        GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DOWNLOAD)
+    ))
+
+    @staticmethod
+    def get_all_albums():
+        return '''
+        SELECT DISTINCT
+            rdf:type(?album)
+            tracker:id(?album) AS id
             (
                 SELECT
-                    GROUP_CONCAT(
-                        nmm:artistName(?artist),
-                        ','
-                    )
+                    nmm:artistName(?artist)
                 WHERE {
                     ?album nmm:albumArtist ?artist
                 }
-            ),
-            (
-                SELECT
-                    GROUP_CONCAT(
-                        (
-                            SELECT
-                                nmm:artistName(nmm:performer(?_12)) AS perf
-                            WHERE {
-                                ?_12 nmm:musicAlbum ?album
-                            }
-                            GROUP BY ?perf
-                        ),
-                        ','
-                    ) AS album_performer
-                WHERE {
-                }
-            )
-        ) AS author
-        xsd:integer(
+                LIMIT 1
+            ) AS artist
+            nie:title(?album) AS title
+            nie:title(?album) AS album
             tracker:coalesce(
-                nmm:albumTrackCount(?album),
                 (
                     SELECT
-                        COUNT(?_1)
-                    WHERE {
-                        ?_1 nmm:musicAlbum ?album ;
-                            tracker:available 'true'
-                        FILTER (
-                            NOT EXISTS {
-                                ?_1 a nmm:Video
-                            } &&
-                            NOT EXISTS {
-                                ?_1 a nmm:Playlist
-                            }
+                        GROUP_CONCAT(
+                            nmm:artistName(?artist),
+                            ','
                         )
+                    WHERE {
+                        ?album nmm:albumArtist ?artist
                     }
-                )
-            )
-        ) AS childcount
-        (
-            SELECT
-                fn:year-from-dateTime(?c)
-            WHERE {
-                ?_2 nmm:musicAlbum ?album ;
-                    nie:contentCreated ?c ;
-                    tracker:available 'true'
-                FILTER (
-                    NOT EXISTS {
-                        ?song a nmm:Video
-                    } &&
-                    NOT EXISTS {
-                        ?song a nmm:Playlist
+                ),
+                (
+                    SELECT
+                        GROUP_CONCAT(
+                            (
+                                SELECT
+                                    nmm:artistName(nmm:performer(?_12)) AS perf
+                                WHERE {
+                                    ?_12 nmm:musicAlbum ?album
+                                }
+                                GROUP BY ?perf
+                            ),
+                            ','
+                        ) AS album_performer
+                    WHERE {
                     }
                 )
-            }
-            LIMIT 1
-        ) AS creation-date
-        {
-            ?album a nmm:MusicAlbum .
-            FILTER (
-                EXISTS {
-                    ?_3 nmm:musicAlbum ?album ;
+            ) AS author
+            xsd:integer(
+                tracker:coalesce(
+                    nmm:albumTrackCount(?album),
+                    (
+                        SELECT
+                            COUNT(?_1)
+                        WHERE {
+                            ?_1 nmm:musicAlbum ?album ;
+                                tracker:available 'true'
+                            FILTER (
+                                NOT EXISTS {
+                                    ?_1 a nmm:Video
+                                } &&
+                                NOT EXISTS {
+                                    ?_1 a nmm:Playlist
+                                } && (
+                                tracker:uri-is-descendant(
+                                    '%(music_dir)s', nie:url(?_1)
+                                ) ||
+                                tracker:uri-is-descendant(
+                                    '%(download_dir)s', nie:url(?_1)
+                                ))
+                            )
+                        }
+                    )
+                )
+            ) AS childcount
+            (
+                SELECT
+                    fn:year-from-dateTime(?c)
+                WHERE {
+                    ?_2 nmm:musicAlbum ?album ;
+                        nie:contentCreated ?c ;
                         tracker:available 'true'
                     FILTER (
                         NOT EXISTS {
-                            ?_3 a nmm:Video
+                            ?song a nmm:Video
                         } &&
                         NOT EXISTS {
-                            ?_3 a nmm:Playlist
-                        }
+                            ?song a nmm:Playlist
+                        } && (
+                        tracker:uri-is-descendant(
+                            '%(music_dir)s', nie:url(?song)
+                        ) ||
+                        tracker:uri-is-descendant(
+                            '%(download_dir)s', nie:url(?song)
+                        ))
                     )
                 }
-            )
-        }
-    ORDER BY fn:lower-case(?title) ?author ?albumyear
-    '''.replace('\n', ' ').strip()
+                LIMIT 1
+            ) AS creation-date
+            {
+                ?album a nmm:MusicAlbum .
+                FILTER (
+                    EXISTS {
+                        ?_3 nmm:musicAlbum ?album ;
+                            tracker:available 'true'
+                        FILTER (
+                            NOT EXISTS {
+                                ?_3 a nmm:Video
+                            } &&
+                            NOT EXISTS {
+                                ?_3 a nmm:Playlist
+                            } && (
+                            tracker:uri-is-descendant(
+                                '%(music_dir)s', nie:url(?_3)
+                            ) ||
+                            tracker:uri-is-descendant(
+                                '%(download_dir)s', nie:url(?_3)
+                            ))
+                        )
+                    }
+                )
+            }
+        ORDER BY fn:lower-case(?title) ?author ?albumyear
+        '''.replace('\n', ' ').strip() % {'music_dir': Query.MUSIC_DIR,
+                                          'download_dir': Query.DOWNLOAD_DIR}
 
-    ALBUMS_COUNT = '''
-    SELECT
-        COUNT(?album) AS childcount
-    WHERE {
-        ?album a nmm:MusicAlbum
-    }
-    '''.replace('\n', ' ').strip()
+    @staticmethod
+    def get_albums_count():
+        return '''
+        SELECT
+            COUNT(?album) AS childcount
+        WHERE {
+            ?album a nmm:MusicAlbum
+        }
+        '''.replace('\n', ' ').strip()
 
-    ARTISTS = '''
-    SELECT DISTINCT
-        rdf:type(?album)
-        tracker:id(?album) AS id
-        (
-            SELECT
-                nmm:artistName(?artist)
-            WHERE {
-                ?album nmm:albumArtist ?artist
-            }
-            LIMIT 1
-        ) AS artist
-        nie:title(?album) AS title
-        nie:title(?album) AS album
-        tracker:coalesce(
+    @staticmethod
+    def get_artists():
+        return '''
+        SELECT DISTINCT
+            rdf:type(?album)
+            tracker:id(?album) AS id
             (
                 SELECT
-                    GROUP_CONCAT(
-                        nmm:artistName(?artist),
-                        ','
-                    )
+                    nmm:artistName(?artist)
                 WHERE {
                     ?album nmm:albumArtist ?artist
                 }
-            ),
-            (
-                SELECT
-                    GROUP_CONCAT(
-                        (
-                            SELECT
-                                nmm:artistName(nmm:performer(?_12)) AS perf
-                            WHERE {
-                                ?_12 nmm:musicAlbum ?album
-                                FILTER (
-                                    NOT EXISTS {
-                                        ?_12 a nmm:Video
-                                    } &&
-                                    NOT EXISTS {
-                                        ?_12 a nmm:Playlist
-                                    }
-                                )
-                            }
-                            GROUP BY ?perf
-                        ),
-                        ','
-                    ) AS album_performer
-                WHERE {
-                }
-            )
-        ) AS author
-        xsd:integer(
+                LIMIT 1
+            ) AS artist
+            nie:title(?album) AS title
+            nie:title(?album) AS album
             tracker:coalesce(
-                nmm:albumTrackCount(?album),
                 (
                     SELECT
-                        COUNT(?_1)
-                    WHERE {
-                        ?_1 nmm:musicAlbum ?album ;
-                        tracker:available 'true'
-                        FILTER (
-                            NOT EXISTS {
-                                ?_1 a nmm:Video
-                            } &&
-                            NOT EXISTS {
-                                ?_1 a nmm:Playlist
-                            }
+                        GROUP_CONCAT(
+                            nmm:artistName(?artist),
+                            ','
                         )
+                    WHERE {
+                        ?album nmm:albumArtist ?artist
                     }
-                )
-            )
-        ) AS childcount
-        (
-            SELECT
-                fn:year-from-dateTime(?c)
-            WHERE {
-                ?_2 nmm:musicAlbum ?album ;
-                    nie:contentCreated ?c ;
-                    tracker:available 'true'
-                FILTER (
-                    NOT EXISTS {
-                        ?_2 a nmm:Video
-                    } &&
-                    NOT EXISTS {
-                        ?_2 a nmm:Playlist
+                ),
+                (
+                    SELECT
+                        GROUP_CONCAT(
+                            (
+                                SELECT
+                                    nmm:artistName(nmm:performer(?_12)) AS perf
+                                WHERE {
+                                    ?_12 nmm:musicAlbum ?album
+                                    FILTER (
+                                        NOT EXISTS {
+                                            ?_12 a nmm:Video
+                                        } &&
+                                        NOT EXISTS {
+                                            ?_12 a nmm:Playlist
+                                        } && (
+                                        tracker:uri-is-descendant(
+                                            '%(music_dir)s', nie:url(?_12)
+                                        ) ||
+                                        tracker:uri-is-descendant(
+                                            '%(download_dir)s', nie:url(?_12)
+                                        ))
+                                    )
+                                }
+                                GROUP BY ?perf
+                            ),
+                            ','
+                        ) AS album_performer
+                    WHERE {
                     }
                 )
-            }
-            LIMIT 1
-        ) AS creation-date
-        {
-            ?album a nmm:MusicAlbum .
-            FILTER (
-                EXISTS {
-                    ?_3 nmm:musicAlbum ?album ;
+            ) AS author
+            xsd:integer(
+                tracker:coalesce(
+                    nmm:albumTrackCount(?album),
+                    (
+                        SELECT
+                            COUNT(?_1)
+                        WHERE {
+                            ?_1 nmm:musicAlbum ?album ;
+                            tracker:available 'true'
+                            FILTER (
+                                NOT EXISTS {
+                                    ?_1 a nmm:Video
+                                } &&
+                                NOT EXISTS {
+                                    ?_1 a nmm:Playlist
+                                } && (
+                                tracker:uri-is-descendant(
+                                    '%(music_dir)s', nie:url(?_1)
+                                ) ||
+                                tracker:uri-is-descendant(
+                                    '%(download_dir)s', nie:url(?_1)
+                                ))
+                            )
+                        }
+                    )
+                )
+            ) AS childcount
+            (
+                SELECT
+                    fn:year-from-dateTime(?c)
+                WHERE {
+                    ?_2 nmm:musicAlbum ?album ;
+                        nie:contentCreated ?c ;
                         tracker:available 'true'
+                    FILTER (
+                        NOT EXISTS {
+                            ?_2 a nmm:Video
+                        } &&
+                        NOT EXISTS {
+                            ?_2 a nmm:Playlist
+                        } && (
+                        tracker:uri-is-descendant(
+                            '%(music_dir)s', nie:url(?_2)
+                        ) ||
+                        tracker:uri-is-descendant(
+                            '%(download_dir)s', nie:url(?_2)
+                        ))
+                    )
+                }
+                LIMIT 1
+            ) AS creation-date
+            {
+                ?album a nmm:MusicAlbum .
                 FILTER (
-                    NOT EXISTS {
-                        ?_3 a nmm:Video
-                    } &&
-                    NOT EXISTS {
-                        ?_3 a nmm:Playlist
+                    EXISTS {
+                        ?_3 nmm:musicAlbum ?album ;
+                            tracker:available 'true'
+                    FILTER (
+                        NOT EXISTS {
+                            ?_3 a nmm:Video
+                        } &&
+                        NOT EXISTS {
+                            ?_3 a nmm:Playlist
+                        } && (
+                        tracker:uri-is-descendant(
+                            '%(music_dir)s', nie:url(?_3)
+                        ) ||
+                        tracker:uri-is-descendant(
+                            '%(download_dir)s', nie:url(?_3)
+                        ))
+                    )
                     }
                 )
-                }
-            )
-        }
-    ORDER BY fn:lower-case(?author) ?albumyear nie:title(?album)
-    '''.replace('\n', ' ').strip()
+            }
+        ORDER BY fn:lower-case(?author) ?albumyear nie:title(?album)
+        '''.replace('\n', ' ').strip() % {'music_dir': Query.MUSIC_DIR,
+                                          'download_dir': Query.DOWNLOAD_DIR}
 
     ARTISTS_COUNT = '''
     SELECT
@@ -255,16 +316,45 @@ class Query():
     }
     '''.replace('\n', ' ').strip()
 
-    SONGS = '''
-    SELECT DISTINCT
-        rdf:type(?song)
-        tracker:id(?song) AS id
-        nie:url(?song) AS url
-        nie:title(?song) AS title
-        nmm:artistName(nmm:performer(?song)) AS artist
-        nie:title(nmm:musicAlbum(?song)) AS album
-        nfo:duration(?song) AS duration
-        {
+    @staticmethod
+    def get_songs():
+        return '''
+        SELECT DISTINCT
+            rdf:type(?song)
+            tracker:id(?song) AS id
+            nie:url(?song) AS url
+            nie:title(?song) AS title
+            nmm:artistName(nmm:performer(?song)) AS artist
+            nie:title(nmm:musicAlbum(?song)) AS album
+            nfo:duration(?song) AS duration
+            {
+                ?song a nmm:MusicPiece ;
+                      a nfo:FileDataObject
+                FILTER (
+                    NOT EXISTS {
+                        ?song a nmm:Video
+                    } &&
+                    NOT EXISTS {
+                        ?song a nmm:Playlist
+                    } && (
+                    tracker:uri-is-descendant(
+                        '%(music_dir)s', nie:url(?song)
+                    ) ||
+                    tracker:uri-is-descendant(
+                        '%(download_dir)s', nie:url(?song)
+                    ))
+                )
+            }
+        ORDER BY tracker:added(?song)
+        '''.replace('\n', ' ').strip() % {'music_dir': Query.MUSIC_DIR,
+                                          'download_dir': Query.DOWNLOAD_DIR}
+
+    @staticmethod
+    def get_songs_count():
+        return '''
+        SELECT
+            COUNT(?song) AS childcount
+        WHERE {
             ?song a nmm:MusicPiece ;
                   a nfo:FileDataObject
             FILTER (
@@ -273,28 +363,17 @@ class Query():
                 } &&
                 NOT EXISTS {
                     ?song a nmm:Playlist
-                }
+                } && (
+                tracker:uri-is-descendant(
+                    '%(music_dir)s', nie:url(?song)
+                ) ||
+                tracker:uri-is-descendant(
+                    '%(download_dir)s', nie:url(?song)
+                ))
             )
         }
-    ORDER BY tracker:added(?song)
-    '''.replace('\n', ' ').strip()
-
-    SONGS_COUNT = '''
-    SELECT
-        COUNT(?song) AS childcount
-    WHERE {
-        ?song a nmm:MusicPiece ;
-              a nfo:FileDataObject
-        FILTER (
-            NOT EXISTS {
-                ?song a nmm:Video
-            } &&
-            NOT EXISTS {
-                ?song a nmm:Playlist
-            }
-        )
-    }
-    '''.replace('\n', ' ').strip()
+        '''.replace('\n', ' ').strip() % {'music_dir': Query.MUSIC_DIR,
+                                          'download_dir': Query.DOWNLOAD_DIR}
 
     @staticmethod
     def album_songs(album_id):
@@ -320,14 +399,22 @@ class Query():
             } &&
             NOT EXISTS {
                 ?song a nmm:Playlist
-            }
+            } && (
+            tracker:uri-is-descendant(
+                '%(music_dir)s', nie:url(?song)
+            ) ||
+            tracker:uri-is-descendant(
+                '%(download_dir)s', nie:url(?song)
+            ))
         )
     }
     ORDER BY
          nmm:setNumber(nmm:musicAlbumDisc(?song))
          nmm:trackNumber(?song)
          tracker:added(?song)
-    '''.replace('\n', ' ').strip() % {'album_id': album_id}
+    '''.replace('\n', ' ').strip() % {'album_id': album_id,
+                                      'music_dir': Query.MUSIC_DIR,
+                                      'download_dir': Query.DOWNLOAD_DIR}
 
         return query
 
@@ -378,9 +465,17 @@ class Query():
             } &&
             NOT EXISTS {
                 ?song a nmm:Playlist
-            }
+            } && (
+            tracker:uri-is-descendant(
+                '%(music_dir)s', nie:url(?song)
+            ) ||
+            tracker:uri-is-descendant(
+                '%(download_dir)s', nie:url(?song)
+            ))
         )
     }
-    '''.replace('\n', ' ').strip() % {'url': url}
+    '''.replace('\n', ' ').strip() % {'url': url,
+                                      'music_dir': Query.MUSIC_DIR,
+                                      'download_dir': Query.DOWNLOAD_DIR}
 
         return query
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index b817f53..e2e6cd7 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -363,7 +363,7 @@ class Albums(ViewContainer):
     def __init__(self, header_bar, selection_toolbar, player):
         ViewContainer.__init__(self, _("Albums"), header_bar,
                                selection_toolbar, Gd.MainViewType.ICON)
-        self.countQuery = Query.ALBUMS_COUNT
+        self.countQuery = Query.get_albums_count()
         self._albumWidget = Widgets.AlbumWidget(player)
         self.player = player
         self.add(self._albumWidget)
@@ -442,7 +442,7 @@ class Songs(ViewContainer):
     @log
     def __init__(self, header_bar, selection_toolbar, player):
         ViewContainer.__init__(self, _("Songs"), header_bar, selection_toolbar, Gd.MainViewType.LIST)
-        self.countQuery = Query.SONGS_COUNT
+        self.countQuery = Query.get_songs_count()
         self._items = {}
         self.monitors = []
         self.isStarred = None
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index 3c072c3..ee5835d 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -483,7 +483,7 @@ class AllArtistsAlbums(ArtistAlbums):
     def __init__(self, player):
         ArtistAlbums.__init__(self, _("All Artists"), [], player)
         self._offset = 0
-        self.countQuery = Query.ALBUMS_COUNT
+        self.countQuery = Query.get_albums_count()
         self._cached_count = -1
         self._load_more = LoadMoreButton(self._get_remaining_item_count)
         self.pack_end(self._load_more.widget, False, False, 0)
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index d0590ad..33c49b9 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -112,7 +112,7 @@ class Window(Gtk.ApplicationWindow):
     @log
     def _on_changes_pending(self, data=None):
         count = 1
-        cursor = tracker.query(Query.SONGS_COUNT, None)
+        cursor = tracker.query(Query.get_songs_count(), None)
         if cursor is not None and cursor.next(None):
             count = cursor.get_integer(0)
         if not count > 0:
@@ -200,7 +200,7 @@ class Window(Gtk.ApplicationWindow):
         self._box.pack_start(self.selection_toolbar.eventbox, False, False, 0)
         self.add(self._box)
         count = 1
-        cursor = tracker.query(Query.SONGS_COUNT, None)
+        cursor = tracker.query(Query.get_songs_count(), None)
         if cursor is not None and cursor.next(None):
             count = cursor.get_integer(0)
         if count > 0:


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