[gnome-music/wip/jfelder/playlists-core-rewrite-prep-work: 11/20] playlists: Sort playlists
- From: Jean Felder <jfelder src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-music/wip/jfelder/playlists-core-rewrite-prep-work: 11/20] playlists: Sort playlists
- Date: Mon, 1 Jul 2019 22:31:23 +0000 (UTC)
commit d7a20dcc2177acc21a01cc53c2d9844749238e56
Author: Jean Felder <jfelder src gnome org>
Date: Tue Jun 4 16:37:55 2019 +0200
playlists: Sort playlists
Smart playlists are ranked first. Then, playlists are listed in order
of creation.
gnomemusic/playlists.py | 37 +++++++++++++++++++++++++++++++------
gnomemusic/query.py | 1 +
2 files changed, 32 insertions(+), 6 deletions(-)
---
diff --git a/gnomemusic/playlists.py b/gnomemusic/playlists.py
index 499103c9..7416512c 100644
--- a/gnomemusic/playlists.py
+++ b/gnomemusic/playlists.py
@@ -25,6 +25,7 @@
# code, but you are not obligated to do so. If you do not wish to do so,
# delete this exception statement from your version.
+import math
import gi
gi.require_version('Dazzle', '1.0')
@@ -43,6 +44,7 @@ logger = logging.getLogger(__name__)
class Playlist(GObject.Object):
""" Base class of all playlists """
+ creation_date = GObject.Property(type=GLib.DateTime, default=None)
id_ = GObject.Property(type=str, default=None)
is_smart = GObject.Property(type=bool, default=False)
query = GObject.Property(type=str, default=None)
@@ -52,13 +54,32 @@ class Playlist(GObject.Object):
def __repr__(self):
return "<Playlist>"
- def __init__(self, id_=None, query=None, tag_text=None, title=None):
+ def __init__(
+ self, id_=None, query=None, tag_text=None, title=None,
+ creation_date=None):
super().__init__()
self.props.id_ = id_
self.props.query = query
self.props.tag_text = tag_text
self.props.title = title
+ self.props.creation_date = creation_date
+
+ @staticmethod
+ def compare_playlist_func(playlist_a, playlist_b):
+ if playlist_a.props.is_smart:
+ if not playlist_b.props.is_smart:
+ return -1
+ return GLib.strcmp0(playlist_a.props.title, playlist_b.props.title)
+
+ if playlist_b.props.is_smart:
+ return 1
+
+ # cannot use GLib.DateTime.compare
+ # https://gitlab.gnome.org/GNOME/pygobject/issues/334
+ date_diff = playlist_a.props.creation_date.difference(
+ playlist_b.props.creation_date)
+ return math.copysign(1, date_diff)
class SmartPlaylist(Playlist):
@@ -238,9 +259,10 @@ class Playlists(GObject.GObject):
return
playlist = Playlist(
- id_=item.get_id(), title=utils.get_media_title(item))
+ id_=item.get_id(), title=utils.get_media_title(item),
+ creation_date=item.get_creation_date())
- self._playlists_model.append(playlist)
+ self._playlists_model.insert_sorted(playlist, Playlist.compare_playlist_func)
@log
def _create_smart_playlist(self, playlist):
@@ -364,7 +386,8 @@ class Playlists(GObject.GObject):
self.emit("playlist-updated", smart_playlist)
return
- self._playlists_model.append(smart_playlist)
+ self._playlists_model.insert_sorted(
+ smart_playlist, Playlist.compare_playlist_func)
@log
def update_all_smart_playlists(self):
@@ -378,8 +401,10 @@ class Playlists(GObject.GObject):
return
new_playlist = Playlist(
- id_=item.get_id(), title=utils.get_media_title(item))
- self._playlists_model.append(new_playlist)
+ id_=item.get_id(), title=utils.get_media_title(item),
+ creation_date=item.get_creation_date())
+ self._playlists_model.insert_sorted(
+ new_playlist, Playlist.compare_playlist_func)
self.emit('playlist-created', item)
def cursor_callback(cursor, res, data):
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index 227c5f16..ce3cfbea 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -224,6 +224,7 @@ class Query():
rdf:type(?playlist)
tracker:id(?playlist) AS ?id
nie:title(?playlist) AS ?title
+ tracker:added(?playlist) AS ?creation_date
nfo:entryCounter(?playlist) AS ?childcount
{
%(where_clause)s
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]