--- /home/theregrunner/original/unity_clementine_daemon.py 2015-12-19 17:47:33.233082781 +0100 +++ /usr/share/unity-scopes/clementine/unity_clementine_daemon.py 2015-12-19 19:42:56.487699908 +0100 @@ -24,4 +24,5 @@ import shutil import sqlite3 +import subprocess APP_NAME = 'unity-scope-clementine' @@ -79,12 +80,12 @@ SEARCH_SQL = '''SELECT title, filename, artist, album, albumartist, art_automatic, year, genre, art_manual, track, length FROM songs - WHERE album LIKE '%%%s%%' OR artist LIKE '%%%s%%' OR title LIKE '%%%s%%' ORDER BY track''' + WHERE album LIKE ? OR artist LIKE ? OR title LIKE ? ORDER BY track''' ALBUM_SQL = '''SELECT title, filename, artist, album, albumartist, art_automatic, year, genre, art_manual, track, length FROM songs - WHERE album LIKE '%%%s%%' AND artist LIKE '%%%s%%' ORDER BY track''' + WHERE album LIKE ? AND artist LIKE ? ORDER BY track''' -def get_music_from_clementine(query): +def get_music_from_clementine(query, search=None, album=None, artist=None): ''' Parses Clementine's database into a form we can use using the supplied SQL query @@ -96,5 +97,8 @@ conn = sqlite3.connect(CLEMENTINE_BACKUP_FILE) cursor = conn.cursor() - cursor.execute(query) + if album: + cursor.execute(query, ("%"+album+"%", "%"+artist+"%",)) + else: + cursor.execute(query, ("%"+search+"%", "%"+search+"%", "%"+search+"%",)) tracks = cursor.fetchall() cursor.close() @@ -146,10 +150,13 @@ results = [] shutil.copy2(CLEMENTINE_DBFILE, CLEMENTINE_BACKUP_FILE) - tracks = get_music_from_clementine(SEARCH_SQL % (search, search, search)) + tracks = get_music_from_clementine(SEARCH_SQL, search=search) albumresults = [] for track in tracks: title = "" if track[0] is None else track[0] - uri = "" if track[1] is None else track[1].decode('utf-8') + try: + uri = "" if track[1] is None else track[1].decode('utf-8') + except UnicodeEncodeError: + uri = "" artist = "" if track[2] is None else track[2] album = "" if track[3] is None else track[3] @@ -195,13 +202,17 @@ preview.props.subtitle = self.result.metadata['artist'].get_string() if self.result.uri.startswith("album://"): - tracks = get_music_from_clementine(ALBUM_SQL % (album, artist)) + tracks = get_music_from_clementine(ALBUM_SQL, album=album, artist=artist) for track in tracks: - track = Unity.TrackMetadata.full(track[1].decode('utf-8'), - track[9], - track[0], - track[2], - track[3], - track[10] / 1000000000) - preview.add_track(track) + try: + track = Unity.TrackMetadata.full(track[1].decode('utf-8'), + track[9], + track[0], + track[2], + track[3], + track[10] / 1000000000) + except UnicodeEncodeError: + pass + else: + preview.add_track(track) else: track = Unity.TrackMetadata.full(self.result.uri, @@ -315,22 +326,33 @@ artist = result.metadata['artist'].get_string() + cmd = [] if id == 'show': if result.uri.startswith("album://"): - tracks = get_music_from_clementine(ALBUM_SQL % (album, artist)) - filename = tracks[0][1].decode('utf-8') + tracks = get_music_from_clementine(ALBUM_SQL, album=album, artist=artist) + try: + filename = tracks[0][1].decode('utf-8') + except UnicodeEncodeError: + filename = "" else: filename = result.uri - dirname = os.path.dirname(filename) - os.system("xdg-open '%s'" % str(dirname)) + if filename: + dirname = os.path.dirname(filename) + cmd = ["xdg-open", str(dirname)] else: - albumtracks = '' + cmd = ["clementine", "-a"] if result.uri.startswith('album://'): - tracks = get_music_from_clementine(ALBUM_SQL % (album, artist)) + tracks = get_music_from_clementine(ALBUM_SQL, album=album, artist=artist) for track in tracks: - albumtracks = '%s \'%s\'' % (albumtracks, track[1].decode('utf-8')) + try: + cmd.append(track[1].decode('utf-8')) + except UnicodeEncodeError: + cmd = [] + break else: - albumtracks = result.uri - os.system('clementine -a %s' % albumtracks) + cmd.append(result.uri) + if cmd: + p = subprocess.Popen(cmd) + p.communicate() return Unity.ActivationResponse(handled=Unity.HandledType.HIDE_DASH, goto_uri=None)