[gnome-music] Do custom queries when tags is selected



commit 6da2f130f11e51b3aba3c569e8852f08b87b5bb0
Author: Vadim Rutkovsky <vrutkovs redhat com>
Date:   Tue Apr 8 14:17:13 2014 +0200

    Do custom queries when tags is selected
    
    For now just query songs for album name works - other queries need update

 gnomemusic/grilo.py     |    7 +-
 gnomemusic/query.py     |  254 +++++++++++++++++++++++++++++++++++++++++++++++
 gnomemusic/searchbar.py |   42 +++++++-
 3 files changed, 295 insertions(+), 8 deletions(-)
---
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 770ba02..f095c89 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -165,6 +165,10 @@ class Grilo(GObject.GObject):
         self.populate_items(Query.album_songs(album_id), 0, callback, count)
 
     @log
+    def populate_custom_query(self, query, callback, count=-1):
+        self.populate_items(query, 0, callback, count)
+
+    @log
     def populate_items(self, query, offset, callback, count=-1):
         options = self.options.copy()
         options.set_skip(offset)
@@ -182,9 +186,8 @@ class Grilo(GObject.GObject):
         @log
         def _search_callback(src, param, item, id, offset, data):
             if item is not None:
-                #print("%s: %s by %s" % (item.get_url(), item.get_title(), item.get_author()))
                 callback(src, param, item)
-        self.search_source.search(q, [Grl.METADATA_KEY_ID], options, _search_callback, None)
+        self.search_source.search(q, self.METADATA_KEYS, options, _search_callback, None)
 
     @log
     def get_media_from_uri(self, uri, callback):
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index 7e98e29..44c69dd 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -399,5 +399,259 @@ class Query():
         )
     }
     '''.replace('\n', ' ').strip() % {'url': url}
+        return query
+
+
+    #Functions for search
+    # TODO: make those queries actualyl return something
+    @staticmethod
+    def get_albums_with_any_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_albums_with_artist_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_albums_with_album_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_albums_with_track_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_artists_with_any_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_artists_with_artist_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_artists_with_album_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_artists_with_track_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_songs_with_any_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_songs_with_artist_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
+
+        return query
+
+    @staticmethod
+    def get_songs_with_album_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    fn:contains(fn:lower-case(nie:title(nmm:musicAlbum(?song))), '%(name)s')
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'name': name.lower()}
+
+        return query
+
+    @staticmethod
+    def get_songs_with_track_match(name):
+        query = '''
+            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
+            WHERE {
+                ?song a nmm:MusicPiece .
+                FILTER (
+                    nie:url(?song) = "%(url)s"
+                )
+            }
+            '''.replace('\n', ' ').strip() % {'url': name}
 
         return query
diff --git a/gnomemusic/searchbar.py b/gnomemusic/searchbar.py
index 06812cd..4ba34a7 100644
--- a/gnomemusic/searchbar.py
+++ b/gnomemusic/searchbar.py
@@ -1,5 +1,6 @@
-from gi.repository import Gtk, Gd, GObject, Pango, GLib
+from gi.repository import Gtk, Gd, GObject, Pango, GLib, Grl
 from gnomemusic.grilo import grilo
+from gnomemusic.query import Query
 from gnomemusic import log
 import logging
 logger = logging.getLogger(__name__)
@@ -157,12 +158,12 @@ class DropDown(Gd.Revealer):
 
     @log
     def initialize_filters(self, searchbar):
-        sourcesManager = SourceManager('source', "Sources", searchbar._search_entry)
-        sourcesFilter = FilterView(sourcesManager, self)
+        self.sourcesManager = SourceManager('source', "Sources", searchbar._search_entry)
+        sourcesFilter = FilterView(self.sourcesManager, self)
         self._grid.add(sourcesFilter.view)
 
-        searchFieldsManager = BaseManager('search', "Search By", searchbar._search_entry)
-        searchFieldsFilter = FilterView(searchFieldsManager, self)
+        self.searchFieldsManager = BaseManager('search', "Search By", searchbar._search_entry)
+        searchFieldsFilter = FilterView(self.searchFieldsManager, self)
         self._grid.add(searchFieldsFilter.view)
 
         self._grid.show_all()
@@ -237,10 +238,39 @@ class Searchbar(Gd.Revealer):
 
     @log
     def search_entry_changed(self, widget):
+        query_matcher = {
+            'Albums': {
+                'search_all': Query.get_albums_with_any_match,
+                'search_artist': Query.get_albums_with_artist_match,
+                'search_album': Query.get_albums_with_album_match,
+                'search_track': Query.get_albums_with_track_match,
+            },
+            'Artists': {
+                'search_all': Query.get_artists_with_any_match,
+                'search_artist': Query.get_artists_with_artist_match,
+                'search_album': Query.get_artists_with_album_match,
+                'search_track': Query.get_artists_with_track_match,
+            },
+            'Songs': {
+                'search_all': Query.get_songs_with_any_match,
+                'search_artist': Query.get_songs_with_artist_match,
+                'search_album': Query.get_songs_with_album_match,
+                'search_track': Query.get_songs_with_track_match,
+            },
+        }
+
+        fields_filter = self.dropdown.searchFieldsManager.get_active()
+
         self.search_term = self._search_entry.get_text()
         if self.view:
             self.view._model.clear()
-            grilo.search(self.search_term, self.view._add_item)
+            # Check that current source can do Query
+            if grilo.search_source.supported_operations() & Grl.SupportedOps.QUERY:
+                query = query_matcher[self.view.__class__.__name__][fields_filter](self.search_term)
+                grilo.populate_custom_query(query, self.view._add_item)
+            else:
+                # nope, can't do - reverting to Search
+                grilo.search(self.search_term, self.view._add_item)
 
     @log
     def show_bar(self, show):


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