[gnome-music/wip/mschraal/core] coremodel: Add user playlist creation logic



commit 94e78dbe0087ef76d0ef6c34a4ddfbc4d381eae6
Author: Jean Felder <jfelder src gnome org>
Date:   Tue Jul 9 14:44:44 2019 +0200

    coremodel: Add user playlist creation logic

 gnomemusic/coregrilo.py                         | 11 +++++++
 gnomemusic/grilowrappers/grltrackerplaylists.py | 42 +++++++++++++++++++++++++
 gnomemusic/widgets/playlistdialog.py            |  2 +-
 3 files changed, 54 insertions(+), 1 deletion(-)
---
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index 28bbc3dd..3ec1368d 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -172,3 +172,14 @@ class CoreGrilo(GObject.GObject):
             if wrapper.source.props.source_id == "grl-tracker-source":
                 wrapper.finish_playlist_deletion(playlist, deleted)
                 break
+
+    def create_playlist(self, playlist_title, callback):
+        """Creates a new user playlist.
+
+        :param str playlist_title: playlist title
+        :param callback: function to perform once, the playlist is created
+        """
+        for wrapper in self._wrappers:
+            if wrapper.source.props.source_id == "grl-tracker-source":
+                wrapper.create_playlist(playlist_title, callback)
+                break
diff --git a/gnomemusic/grilowrappers/grltrackerplaylists.py b/gnomemusic/grilowrappers/grltrackerplaylists.py
index fb192e17..7c5757be 100644
--- a/gnomemusic/grilowrappers/grltrackerplaylists.py
+++ b/gnomemusic/grilowrappers/grltrackerplaylists.py
@@ -108,6 +108,9 @@ class GrlTrackerPlaylists(GObject.GObject):
             coreselection=self._coreselection, grilo=self._grilo)
 
         self._model.append(playlist)
+        callback = data
+        if callback is not None:
+            callback(playlist)
 
     def _playlists_filter(self, playlist):
         return playlist not in self._pls_todelete
@@ -163,6 +166,45 @@ class GrlTrackerPlaylists(GObject.GObject):
         self._tracker.update_async(
             query, GLib.PRIORITY_LOW, None, _delete_cb, None)
 
+    def create_playlist(self, playlist_title, callback):
+        """Creates a new user playlist.
+
+        :param str playlist_title: playlist title
+        :param callback: function to perform once, the playlist is created
+        """
+        def _create_cb(conn, res, data):
+            result = conn.update_blank_finish(res)
+            playlist_urn = result[0][0]['playlist']
+            query = """
+            SELECT
+                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
+                {
+                    ?playlist a nmm:Playlist .
+                    FILTER ( <%(playlist_urn)s> = ?playlist )
+                }
+            """.replace("\n", " ").strip() % {"playlist_urn": playlist_urn}
+
+            options = self._fast_options.copy()
+            self._source.query(
+                query, self.METADATA_KEYS, options, self._add_user_playlist,
+                callback)
+
+        query = """
+            INSERT {
+                _:playlist a nmm:Playlist ;
+                           a nfo:MediaList ;
+                             nie:title "%(title)s" ;
+                             nfo:entryCounter 0 .
+            }
+            """.replace("\n", " ").strip() % {"title": playlist_title}
+        self._tracker.update_blank_async(
+            query, GLib.PRIORITY_LOW, None, _create_cb, None)
+
 
 class Playlist(GObject.GObject):
     """ Base class of all playlists """
diff --git a/gnomemusic/widgets/playlistdialog.py b/gnomemusic/widgets/playlistdialog.py
index 26014d52..bd042f41 100644
--- a/gnomemusic/widgets/playlistdialog.py
+++ b/gnomemusic/widgets/playlistdialog.py
@@ -131,7 +131,7 @@ class PlaylistDialog(Gtk.Dialog):
 
         text = self._add_playlist_entry.props.text
         if text:
-            self._playlists.create_playlist(text, select_and_close_dialog)
+            self._coremodel.create_playlist(text, select_and_close_dialog)
 
     @Gtk.Template.Callback()
     @log


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