[gnome-music/wip/mschraal/core] coremodel: Remove coresong parameter from set_playlist_model



commit 88ae0c934e5b016966bf597de5a9e1a81dad627f
Author: Jean Felder <jfelder src gnome org>
Date:   Mon Jul 8 23:42:54 2019 +0200

    coremodel: Remove coresong parameter from set_playlist_model
    
    Fix the play_plause method (the song is not restarted anymore).

 gnomemusic/coremodel.py                  | 18 +---------------
 gnomemusic/player.py                     | 35 ++++++++++++++++++++++++++------
 gnomemusic/views/playlistsview.py        | 17 ++++------------
 gnomemusic/views/searchview.py           |  5 ++---
 gnomemusic/views/songsview.py            |  5 +++--
 gnomemusic/widgets/albumwidget.py        |  3 +--
 gnomemusic/widgets/artistalbumswidget.py |  4 +---
 7 files changed, 41 insertions(+), 46 deletions(-)
---
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 319059bd..10907fa8 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -151,7 +151,7 @@ class CoreModel(GObject.GObject):
 
         return albums_model_sort
 
-    def set_playlist_model(self, playlist_type, coresong, model):
+    def set_playlist_model(self, playlist_type, model):
 
         def _on_items_changed(model, position, removed, added):
             if removed > 0:
@@ -195,10 +195,6 @@ class CoreModel(GObject.GObject):
                         "state", model_song, "state",
                         GObject.BindingFlags.SYNC_CREATE)
 
-                    song_id = coresong.props.media.get_id()
-                    if song.props.media.get_id() == song_id:
-                        song.props.state = SongWidget.State.PLAYING
-
                 self.emit("playlist-loaded")
             elif playlist_type == PlayerPlaylist.Type.ARTIST:
                 self._playlist_model.remove_all()
@@ -222,10 +218,6 @@ class CoreModel(GObject.GObject):
                         "state", model_song, "state",
                         GObject.BindingFlags.SYNC_CREATE)
 
-                    song_id = coresong.props.media.get_id()
-                    if song.props.media.get_id() == song_id:
-                        song.props.state = SongWidget.State.PLAYING
-
                 self.emit("playlist-loaded")
             elif playlist_type == PlayerPlaylist.Type.SONGS:
                 if self._song_signal_id:
@@ -239,8 +231,6 @@ class CoreModel(GObject.GObject):
 
                     if song.props.state == SongWidget.State.PLAYING:
                         song.props.state = SongWidget.State.PLAYED
-                    if song is coresong:
-                        song.props.state = SongWidget.State.PLAYING
 
                 self._song_signal_id = self._songliststore.props.model.connect(
                     "items-changed", _on_items_changed)
@@ -256,9 +246,6 @@ class CoreModel(GObject.GObject):
                 for song in self._song_search_flatten:
                     self._playlist_model.append(song)
 
-                    if song is coresong:
-                        song.props.state = SongWidget.State.PLAYING
-
                 self._search_signal_id = self._song_search_flatten.connect(
                     "items-changed", _on_items_changed)
 
@@ -276,9 +263,6 @@ class CoreModel(GObject.GObject):
 
                     self._playlist_model.append(song)
 
-                    if model_song is coresong:
-                        song.props.state = SongWidget.State.PLAYING
-
                     song.bind_property(
                         "state", model_song, "state",
                         GObject.BindingFlags.SYNC_CREATE)
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 412353e1..5116f50b 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -24,7 +24,7 @@
 
 from enum import IntEnum
 from itertools import chain
-from random import randint
+from random import randint, randrange
 import logging
 import time
 
@@ -272,6 +272,31 @@ class PlayerPlaylist(GObject.GObject):
 
         return None
 
+    def set_song(self, song):
+        """Sets current song.
+
+        If no song is provided, a song is automatically selected.
+
+        :param CoreSong song: song to set
+        :returns: The selected song
+        :rtype: CoreSong
+        """
+        if song is None:
+            if self.props.repeat_mode == RepeatMode.SHUFFLE:
+                position = randrange(0, self._model.get_n_items())
+            else:
+                position = 0
+            song = self._model.get_item(position)
+            song.props.state = SongWidget.State.PLAYING
+            return song
+
+        for coresong in self._model:
+            if coresong == song:
+                coresong.props.state = SongWidget.State.PLAYING
+                return song
+
+        return None
+
     @log
     def _on_repeat_mode_changed(self, klass, param):
 
@@ -499,12 +524,10 @@ class Player(GObject.GObject):
         :param bool song_changed: indicate if a new song must be loaded
         """
         if self.props.current_song is None:
-            return
-
-        if coresong is None:
-            coresong = self._playlist.props.current_song
+            coresong = self._playlist.set_song(coresong)
 
-        self._load(coresong)
+        if coresong is not None:
+            self._load(coresong)
 
         self._gst_player.props.state = Playback.PLAYING
 
diff --git a/gnomemusic/views/playlistsview.py b/gnomemusic/views/playlistsview.py
index e6683e6a..bee28ca0 100644
--- a/gnomemusic/views/playlistsview.py
+++ b/gnomemusic/views/playlistsview.py
@@ -22,14 +22,12 @@
 # code, but you are not obligated to do so.  If you do not wish to do so,
 # delete this exception statement from your version.
 
-from random import randrange
-
 from gettext import gettext as _
 
 from gi.repository import Gdk, GObject, Gio, Gtk
 
 from gnomemusic import log
-from gnomemusic.player import PlayerPlaylist, RepeatMode
+from gnomemusic.player import PlayerPlaylist
 from gnomemusic.views.baseview import BaseView
 from gnomemusic.widgets.notificationspopup import PlaylistNotification
 from gnomemusic.widgets.playlistcontextmenu import PlaylistContextMenu
@@ -226,20 +224,13 @@ class PlaylistsView(BaseView):
         return song_widget
 
     def _song_activated(self, widget=None, event=None):
-        # FIXME: Selection should be automatic in the player
+        coresong = None
         if widget is not None:
             coresong = widget.props.coresong
-        else:
-            position = 0
-            if self.player.props.repeat_mode == RepeatMode.SHUFFLE:
-                position = randrange(
-                    0, self._current_playlist.props.model.get_n_items())
-            coresong = self._current_playlist.props.model.get_item(position)
 
         self._coremodel.set_playlist_model(
-            PlayerPlaylist.Type.PLAYLIST, coresong,
-            self._current_playlist.props.model)
-        self.player.play()
+            PlayerPlaylist.Type.PLAYLIST, self._current_playlist.props.model)
+        self.player.play(coresong)
 
         return True
 
diff --git a/gnomemusic/views/searchview.py b/gnomemusic/views/searchview.py
index ac724a4c..6eed8afa 100644
--- a/gnomemusic/views/searchview.py
+++ b/gnomemusic/views/searchview.py
@@ -160,9 +160,8 @@ class SearchView(BaseView):
             # self.emit('song-activated', widget)
 
             self._coremodel.set_playlist_model(
-                PlayerPlaylist.Type.SEARCH_RESULT, widget.props.coresong,
-                self._model)
-            self.player.play()
+                PlayerPlaylist.Type.SEARCH_RESULT, self._model)
+            self.player.play(widget.props.coresong)
 
         # FIXME: Need to ignore the event from the checkbox.
         # if self.props.selection_mode:
diff --git a/gnomemusic/views/songsview.py b/gnomemusic/views/songsview.py
index 9016c66f..6d21ead0 100644
--- a/gnomemusic/views/songsview.py
+++ b/gnomemusic/views/songsview.py
@@ -209,8 +209,9 @@ class SongsView(BaseView):
         itr = self._view.props.model.get_iter(path)
         coresong = self._view.props.model[itr][5]
         self._window._app._coremodel.set_playlist_model(
-            PlayerPlaylist.Type.SONGS, coresong, self._view.props.model)
-        self.player.play()
+            PlayerPlaylist.Type.SONGS, self._view.props.model)
+
+        self.player.play(coresong)
 
     @log
     def _on_view_clicked(self, gesture, n_press, x, y):
diff --git a/gnomemusic/widgets/albumwidget.py b/gnomemusic/widgets/albumwidget.py
index ee665db8..dba32f17 100644
--- a/gnomemusic/widgets/albumwidget.py
+++ b/gnomemusic/widgets/albumwidget.py
@@ -168,8 +168,7 @@ class AlbumWidget(Gtk.EventBox):
 
         signal_id = coremodel.connect("playlist-loaded", _on_playlist_loaded)
         coremodel.set_playlist_model(
-            PlayerPlaylist.Type.ALBUM, song_widget.props.coresong,
-            self._album_model)
+            PlayerPlaylist.Type.ALBUM, self._album_model)
 
         return True
 
diff --git a/gnomemusic/widgets/artistalbumswidget.py b/gnomemusic/widgets/artistalbumswidget.py
index e9345d88..c7e2fe90 100644
--- a/gnomemusic/widgets/artistalbumswidget.py
+++ b/gnomemusic/widgets/artistalbumswidget.py
@@ -111,9 +111,7 @@ class ArtistAlbumsWidget(Gtk.Box):
             coremodel.disconnect(signal_id)
 
         signal_id = coremodel.connect("playlist-loaded", _on_playlist_loaded)
-        coremodel.set_playlist_model(
-            PlayerPlaylist.Type.ARTIST, song_widget.props.coresong,
-            self._model)
+        coremodel.set_playlist_model(PlayerPlaylist.Type.ARTIST, self._model)
 
     def _add_album(self, corealbum):
         widget = ArtistAlbumWidget(


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