[gnome-music/wip/mschraal/core] playlistview: Add back playlist renaming



commit ac84b9b8e53b94144529c5811c131fb8f0eaab15
Author: Marinus Schraal <mschraal gnome org>
Date:   Mon Jul 8 16:24:35 2019 +0200

    playlistview: Add back playlist renaming

 gnomemusic/grilowrappers/grltrackerplaylists.py | 37 ++++++++++++++++++++++++-
 gnomemusic/views/playlistsview.py               | 17 ++++++------
 2 files changed, 45 insertions(+), 9 deletions(-)
---
diff --git a/gnomemusic/grilowrappers/grltrackerplaylists.py b/gnomemusic/grilowrappers/grltrackerplaylists.py
index ad5e8a0b..198c3cdd 100644
--- a/gnomemusic/grilowrappers/grltrackerplaylists.py
+++ b/gnomemusic/grilowrappers/grltrackerplaylists.py
@@ -3,10 +3,11 @@ import time
 from gettext import gettext as _
 
 import gi
-gi.require_versions({"Grl": "0.3", 'Tracker': "2.0"})
+gi.require_versions({"Grl": "0.3"})
 from gi.repository import Gio, Grl, GLib, GObject
 
 from gnomemusic.coresong import CoreSong
+from gnomemusic.trackerwrapper import TrackerWrapper
 import gnomemusic.utils as utils
 
 
@@ -153,6 +154,7 @@ class Playlist(GObject.GObject):
         self._coremodel = coremodel
         self._coreselection = coreselection
         self._grilo = grilo
+        self._tracker = TrackerWrapper().props.tracker
 
         self._fast_options = Grl.OperationOptions()
         self._fast_options.set_resolution_flags(
@@ -227,6 +229,39 @@ class Playlist(GObject.GObject):
         self._source.query(
             query, self.METADATA_KEYS, options, _add_to_playlist_cb, None)
 
+    def rename(self, new_name):
+        """Rename a playlist
+
+        :param str new_name: new playlist name
+        """
+        def update_cb(conn, res, data):
+            # FIXME: Check for failure.
+            conn.update_finish(res)
+            # FIXME: Requery instead?
+            self.props.title = new_name
+
+        query = """
+        INSERT OR REPLACE {
+            ?playlist nie:title "%(title)s"
+        }
+        WHERE {
+            ?playlist a nmm:Playlist ;
+                      a nfo:MediaList .
+            OPTIONAL {
+                ?playlist nfo:hasMediaFileListEntry ?entry .
+            }
+            FILTER (
+                tracker:id(?playlist) = %(playlist_id)s
+            )
+        }
+        """.replace("\n", " ").strip() % {
+            'title': new_name,
+            'playlist_id': self.props.pl_id
+        }
+
+        self._tracker.update_async(
+            query, GLib.PRIORITY_LOW, None, update_cb, None)
+
 
 class SmartPlaylist(Playlist):
     """Base class for smart playlists"""
diff --git a/gnomemusic/views/playlistsview.py b/gnomemusic/views/playlistsview.py
index aa123b4f..b44670a6 100644
--- a/gnomemusic/views/playlistsview.py
+++ b/gnomemusic/views/playlistsview.py
@@ -24,7 +24,7 @@
 
 from gettext import gettext as _
 
-from gi.repository import GObject, Gtk
+from gi.repository import GObject, Gio, Gtk
 
 from gnomemusic import log
 from gnomemusic.player import PlayerPlaylist
@@ -92,11 +92,11 @@ class PlaylistsView(BaseView):
         # self._playlist_delete_action.connect(
         #     'activate', self._stage_playlist_for_deletion)
         # self._window.add_action(self._playlist_delete_action)
-        # self._playlist_rename_action = Gio.SimpleAction.new(
-        #     'playlist_rename', None)
-        # self._playlist_rename_action.connect(
-        #     'activate', self._stage_playlist_for_renaming)
-        # self._window.add_action(self._playlist_rename_action)
+        self._playlist_rename_action = Gio.SimpleAction.new(
+            'playlist_rename', None)
+        self._playlist_rename_action.connect(
+            'activate', self._stage_playlist_for_renaming)
+        self._window.add_action(self._playlist_rename_action)
 
         self._grid.insert_row(0)
         self._grid.attach(self._pl_ctrls, 1, 0, 1, 1)
@@ -175,6 +175,8 @@ class PlaylistsView(BaseView):
         self._update_songs_count(playlist.props.count)
         playlist.connect("notify::count", self._on_song_count_changed)
 
+        self._playlist_rename_action.set_enabled(not playlist.props.is_smart)
+
     def _on_song_count_changed(self, playlist, value):
         self._update_songs_count(playlist.props.count)
 
@@ -218,8 +220,7 @@ class PlaylistsView(BaseView):
         selection.props.text = new_name
 
         pl_torename = selection.playlist
-        pl_torename.props.title = new_name
-        self._playlists.rename(pl_torename, new_name)
+        pl_torename.rename(new_name)
 
     @log
     def _populate(self, data=None):


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