[gnome-music/wip/mschraal/assorted-cleanups: 21/32] Untie CoreGrilo from CoreModel



commit 4f5614d07642b1ad54762b282002bc916629a016
Author: Marinus Schraal <mschraal gnome org>
Date:   Thu Mar 26 13:33:50 2020 +0100

    Untie CoreGrilo from CoreModel
    
    In the initial core rewrite CoreGrilo and CoreModel became more
    intertwined than intended. With refactorings over time, it can now be
    untangled.
    
    Also rename grilo variables to coregrilo as needed, to clarify the
    reference.

 gnomemusic/albumartcache.py | 22 ++++++++++------------
 gnomemusic/application.py   | 14 ++++++++++++++
 gnomemusic/artistart.py     |  8 +++-----
 gnomemusic/corealbum.py     |  4 ++--
 gnomemusic/coreartist.py    |  3 ++-
 gnomemusic/coredisc.py      |  4 ++--
 gnomemusic/coregrilo.py     |  5 ++---
 gnomemusic/coremodel.py     | 15 +++++++--------
 gnomemusic/coresong.py      | 10 ++++++----
 gnomemusic/window.py        |  7 +++----
 10 files changed, 51 insertions(+), 41 deletions(-)
---
diff --git a/gnomemusic/albumartcache.py b/gnomemusic/albumartcache.py
index fbfd1f0f..cdfc25fa 100644
--- a/gnomemusic/albumartcache.py
+++ b/gnomemusic/albumartcache.py
@@ -544,7 +544,7 @@ class RemoteArt(GObject.GObject):
         self._artist = None
         self._album = None
         self._coresong = None
-        self._grilo = None
+        self._coregrilo = None
 
     def query(self, coresong):
         """Start the remote query
@@ -564,24 +564,22 @@ class RemoteArt(GObject.GObject):
         # FIXME: This is a total hack. It gets CoreModel from the
         # CoreAlbum or CoreSong about and then retrieves the CoreGrilo
         # instance.
-        try:
-            self._grilo = self._coresong._coremodel.props.grilo
-        except AttributeError:
-            self._grilo = self._coresong._grilo
+        self._coregrilo = self._coresong._coregrilo
 
-        if not self._grilo.props.cover_sources:
+        if not self._coregrilo.props.cover_sources:
             self.emit('no-remote-sources')
-            self._grilo.connect(
-                'notify::cover-sources', self._on_grilo_cover_sources_changed)
+            self._coregrilo.connect(
+                "notify::cover-sources",
+                self._on_coregrilo_cover_sources_changed)
         else:
             # FIXME: It seems this Grilo query does not always return,
             # especially on queries with little info.
-            self._grilo.get_album_art_for_item(
+            self._coregrilo.get_album_art_for_item(
                 self._coresong, self._remote_album_art)
 
-    def _on_grilo_cover_sources_changed(self, klass, data):
-        if self._grilo.props.cover_sources:
-            self._grilo.get_album_art_for_item(
+    def _on_coregrilo_cover_sources_changed(self, klass, data):
+        if self._coregrilo.props.cover_sources:
+            self._coregrilo.get_album_art_for_item(
                 self._coresong, self._remote_album_art)
 
     def _delete_callback(self, src, result, data):
diff --git a/gnomemusic/application.py b/gnomemusic/application.py
index 1ec542f6..ba8f4d45 100644
--- a/gnomemusic/application.py
+++ b/gnomemusic/application.py
@@ -34,6 +34,7 @@ from gettext import gettext as _
 
 from gi.repository import Gtk, Gio, GLib, Gdk, GObject
 
+from gnomemusic.coregrilo import CoreGrilo
 from gnomemusic.coremodel import CoreModel
 from gnomemusic.coreselection import CoreSelection
 from gnomemusic.inhibitsuspend import InhibitSuspend
@@ -64,6 +65,9 @@ class Application(Gtk.Application):
         self._log = MusicLogger()
         self._coreselection = CoreSelection()
         self._coremodel = CoreModel(self)
+        # Order is important: CoreGrilo initializes the Grilo sources,
+        # which in turn use CoreModel & CoreSelection extensively.
+        self._coregrilo = CoreGrilo(self)
 
         self._settings = Gio.Settings.new('org.gnome.Music')
         self._lastfm_scrobbler = LastFmScrobbler(self)
@@ -80,6 +84,16 @@ class Application(Gtk.Application):
         style_context.add_provider_for_screen(
             screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
 
+    @GObject.Property(
+        type=CoreGrilo, default=None, flags=GObject.ParamFlags.READABLE)
+    def coregrilo(self):
+        """Get application-wide CoreGrilo instance.
+
+        :returns: The grilo wrapper
+        :rtype: CoreGrilo
+        """
+        return self._coregrilo
+
     @GObject.Property(
         type=MusicLogger, default=None, flags=GObject.ParamFlags.READABLE)
     def log(self):
diff --git a/gnomemusic/artistart.py b/gnomemusic/artistart.py
index c810198b..41716d0a 100644
--- a/gnomemusic/artistart.py
+++ b/gnomemusic/artistart.py
@@ -124,11 +124,11 @@ class ArtistArt(GObject.GObject):
 
     _log = MusicLogger()
 
-    def __init__(self, coreartist, coremodel):
+    def __init__(self, application, coreartist):
         """Initialize the ArtistArt.
 
+        :param Application application: The application object
         :param CoreArtist coreartist: The coreartist to use
-        :param CoreModel coremodel: The main coremodel
         """
         super().__init__()
 
@@ -138,12 +138,10 @@ class ArtistArt(GObject.GObject):
         if self._in_cache():
             return
 
-        grilo = coremodel.props.grilo
-
         self._coreartist.connect(
             "notify::thumbnail", self._on_thumbnail_changed)
 
-        grilo.get_artist_art(self._coreartist)
+        application.props.coregrilo.get_artist_art(self._coreartist)
 
     def _in_cache(self):
         success, thumb_file = MediaArt.get_file(
diff --git a/gnomemusic/corealbum.py b/gnomemusic/corealbum.py
index 325526d8..588001f7 100644
--- a/gnomemusic/corealbum.py
+++ b/gnomemusic/corealbum.py
@@ -49,7 +49,7 @@ class CoreAlbum(GObject.GObject):
         """
         super().__init__()
 
-        self._coremodel = application.props.coremodel
+        self._coregrilo = application.props.coregrilo
         self._model = None
         self._selected = False
         self.update(media)
@@ -76,7 +76,7 @@ class CoreAlbum(GObject.GObject):
         disc_model_sort.set_sort_func(
             utils.wrap_list_store_sort_func(_disc_order_sort))
 
-        self._coremodel.props.grilo.get_album_discs(
+        self._coregrilo.get_album_discs(
             self.props.media, disc_model)
 
         return disc_model_sort
diff --git a/gnomemusic/coreartist.py b/gnomemusic/coreartist.py
index b00a5e29..09bd655e 100644
--- a/gnomemusic/coreartist.py
+++ b/gnomemusic/coreartist.py
@@ -46,6 +46,7 @@ class CoreArtist(GObject.GObject):
         super().__init__()
 
         self._cached_thumbnail_uri = None
+        self._coregrilo = application.props.coregrilo
         self._coremodel = application.props.coremodel
         self._model = None
         self._selected = False
@@ -64,7 +65,7 @@ class CoreArtist(GObject.GObject):
 
         albums_model_sort = Gfm.SortListModel.new(albums_model_filter)
 
-        self._coremodel.props.grilo.get_artist_albums(
+        self._coregrilo.get_artist_albums(
             self.props.media, albums_model_filter)
 
         def _album_sort(album_a, album_b):
diff --git a/gnomemusic/coredisc.py b/gnomemusic/coredisc.py
index f915037d..0d4d212a 100644
--- a/gnomemusic/coredisc.py
+++ b/gnomemusic/coredisc.py
@@ -41,6 +41,7 @@ class CoreDisc(GObject.GObject):
         """
         super().__init__()
 
+        self._coregrilo = application.props.coregrilo
         self._coremodel = application.props.coremodel
         self._filter_model = None
         self._model = None
@@ -111,8 +112,7 @@ class CoreDisc(GObject.GObject):
 
             album_ids.append(media.get_source() + media.get_id())
 
-        self._coremodel.props.grilo.populate_album_disc_songs(
-            media, discnr, _callback)
+        self._coregrilo.populate_album_disc_songs(media, discnr, _callback)
 
     @GObject.Property(
         type=bool, default=False, flags=GObject.BindingFlags.SYNC_CREATE)
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index 4d408acf..9aaf661c 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -51,16 +51,15 @@ class CoreGrilo(GObject.GObject):
     cover_sources = GObject.Property(type=bool, default=False)
     tracker_available = GObject.Property(type=int)
 
-    def __init__(self, coremodel, application):
+    def __init__(self, application):
         """Initiate the CoreGrilo object
 
-        :param CoreModel coremodel: The CoreModel instance to use
         :param Application application: The Application instance to use
         """
         super().__init__()
 
         self._application = application
-        self._coremodel = coremodel
+        self._coremodel = self._application.props.coremodel
         self._coreselection = application.props.coreselection
         self._log = application.props.log
         self._search_wrappers = {}
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 62dd297c..88ae87a2 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -29,7 +29,6 @@ gi.require_version("Gfm", "0.1")
 from gi.repository import GObject, Gio, Gfm, Gtk
 
 from gnomemusic.coreartist import CoreArtist
-from gnomemusic.coregrilo import CoreGrilo
 from gnomemusic.coresong import CoreSong
 from gnomemusic.grilowrappers.grltrackerplaylists import Playlist
 from gnomemusic.player import PlayerPlaylist
@@ -70,7 +69,6 @@ class CoreModel(GObject.GObject):
     }
 
     active_playlist = GObject.Property(type=Playlist, default=None)
-    grilo = GObject.Property(type=CoreGrilo, default=None)
     songs_available = GObject.Property(type=bool, default=False)
 
     def __init__(self, application):
@@ -89,6 +87,7 @@ class CoreModel(GObject.GObject):
         self._songliststore = SongListStore(self._songs_model)
 
         self._application = application
+
         self._albums_model = Gio.ListStore()
         self._albums_model_sort = Gfm.SortListModel.new(self._albums_model)
         self._albums_model_sort.set_sort_func(
@@ -135,8 +134,6 @@ class CoreModel(GObject.GObject):
         self._user_playlists_model_sort.set_sort_func(
             utils.wrap_list_store_sort_func(self._playlists_sort))
 
-        self.props.grilo = CoreGrilo(self, application)
-
         self._songs_model.connect(
             "items-changed", self._on_songs_items_changed)
 
@@ -300,7 +297,7 @@ class CoreModel(GObject.GObject):
 
         :param Playlist playlist: playlist
         """
-        self.props.grilo.stage_playlist_deletion(playlist)
+        self._application.props.coregrilo.stage_playlist_deletion(playlist)
 
     def finish_playlist_deletion(self, playlist, deleted):
         """Finishes playlist deletion.
@@ -308,7 +305,8 @@ class CoreModel(GObject.GObject):
         :param Playlist playlist: playlist
         :param bool deleted: indicates if the playlist has been deleted
         """
-        self.props.grilo.finish_playlist_deletion(playlist, deleted)
+        self._application.props.coregrilo.finish_playlist_deletion(
+            playlist, deleted)
 
     def create_playlist(self, playlist_title, callback):
         """Creates a new user playlist.
@@ -316,10 +314,11 @@ class CoreModel(GObject.GObject):
         :param str playlist_title: playlist title
         :param callback: function to perform once, the playlist is created
         """
-        self.props.grilo.create_playlist(playlist_title, callback)
+        self._application.props.coregrilo.create_playlist(
+            playlist_title, callback)
 
     def search(self, text):
-        self.props.grilo.search(text)
+        self._application.props.coregrilo.search(text)
 
     @GObject.Property(
         type=Gio.ListStore, default=None, flags=GObject.ParamFlags.READABLE)
diff --git a/gnomemusic/coresong.py b/gnomemusic/coresong.py
index 8748df66..eaa67fc5 100644
--- a/gnomemusic/coresong.py
+++ b/gnomemusic/coresong.py
@@ -63,7 +63,7 @@ class CoreSong(GObject.GObject):
         """
         super().__init__()
 
-        self._grilo = application.props.coremodel.props.grilo
+        self._coregrilo = application.props.coregrilo
         self._coreselection = application.props.coreselection
         self._favorite = False
         self._selected = False
@@ -99,7 +99,7 @@ class CoreSong(GObject.GObject):
             return
 
         self.props.media.set_favourite(self._favorite)
-        self._grilo.writeback(self.props.media, Grl.METADATA_KEY_FAVOURITE)
+        self._coregrilo.writeback(self.props.media, Grl.METADATA_KEY_FAVOURITE)
 
     @GObject.Property(type=bool, default=False)
     def selected(self):
@@ -133,11 +133,13 @@ class CoreSong(GObject.GObject):
             return
 
         self.props.media.set_play_count(self.props.play_count + 1)
-        self._grilo.writeback(self.props.media, Grl.METADATA_KEY_PLAY_COUNT)
+        self._coregrilo.writeback(
+            self.props.media, Grl.METADATA_KEY_PLAY_COUNT)
 
     def set_last_played(self):
         if not self._is_tracker:
             return
 
         self.props.media.set_last_played(GLib.DateTime.new_now_utc())
-        self._grilo.writeback(self.props.media, Grl.METADATA_KEY_LAST_PLAYED)
+        self._coregrilo.writeback(
+            self.props.media, Grl.METADATA_KEY_LAST_PLAYED)
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index 8c5a88a8..b636e867 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -179,7 +179,7 @@ class Window(Gtk.ApplicationWindow):
         self._app.props.coremodel.connect(
             "notify::songs-available", self._on_songs_available)
 
-        self._app.props.coremodel.props.grilo.connect(
+        self._app.props.coregrilo.connect(
             "notify::tracker-available", self._on_tracker_available)
 
         if self._app.props.coremodel.props.songs_available:
@@ -190,7 +190,7 @@ class Window(Gtk.ApplicationWindow):
     def _switch_to_empty_view(self):
         did_initial_state = self._settings.get_boolean('did-initial-state')
 
-        state = self._app.props.coremodel.props.grilo.props.tracker_available
+        state = self._app.props.coregrilo.props.tracker_available
         empty_view = self.views[View.EMPTY]
         if state == TrackerState.UNAVAILABLE:
             empty_view.props.state = EmptyView.State.NO_TRACKER
@@ -220,8 +220,7 @@ class Window(Gtk.ApplicationWindow):
         self.props.active_view = self._stack.props.visible_child
 
     def _on_tracker_available(self, klass, value):
-        grilo = self._app.props.coremodel.props.grilo
-        new_state = grilo.props.tracker_available
+        new_state = self._app.props.coregrilo.props.tracker_available
 
         if new_state != TrackerState.AVAILABLE:
             self._switch_to_empty_view()


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