[gnome-music/wip/jfelder/lastfm-ui-part-2: 17/22] scrobbler: Add a "not available" state



commit c7e92c4f79f2502baf9f03ef840f013cf3eb9285
Author: Jean Felder <jfelder src gnome org>
Date:   Tue Jan 14 21:02:40 2020 +0100

    scrobbler: Add a "not available" state
    
    GNOME onlince accounts can be compiled without Last.fm support. In
    that case, it is useless to look for an existing account. It also
    means that no account can be configured.

 gnomemusic/scrobbler.py       | 50 ++++++++++++++++++++++++++++++++-----------
 gnomemusic/widgets/appmenu.py |  2 +-
 2 files changed, 39 insertions(+), 13 deletions(-)
---
diff --git a/gnomemusic/scrobbler.py b/gnomemusic/scrobbler.py
index fbaf377b..f3cc5ac3 100644
--- a/gnomemusic/scrobbler.py
+++ b/gnomemusic/scrobbler.py
@@ -27,9 +27,8 @@ from hashlib import md5
 import logging
 
 import gi
-gi.require_version('Goa', '1.0')
-gi.require_version('Soup', '2.4')
-from gi.repository import GLib, Goa, GObject, Soup
+gi.require_versions({"Goa": "1.0", "GoaBackend": "1.0", "Soup": "2.4"})
+from gi.repository import GLib, Goa, GoaBackend, GObject, Soup
 
 from gnomemusic import log
 import gnomemusic.utils as utils
@@ -43,11 +42,18 @@ class GoaLastFM(GObject.GObject):
     """
 
     class State(IntEnum):
-        """GoaLastFM account State"""
+        """GoaLastFM account State.
 
-        NOT_CONFIGURED = 0
-        DISABLED = 1
-        ENABLED = 2
+        NOT_AVAILABLE: GOA does not handle Last.fm accounts
+        NOT_CONFIGURED: GOA handles Last.fm but no user account has
+                        been configured
+        DISABLED: a user account exists, but it is disabled
+        ENABLED: a user account exists and is enabled
+        """
+        NOT_AVAILABLE = 0
+        NOT_CONFIGURED = 1
+        DISABLED = 2
+        ENABLED = 3
 
     def __repr__(self):
         return '<GoaLastFM>'
@@ -57,15 +63,33 @@ class GoaLastFM(GObject.GObject):
         super().__init__()
 
         self._client = None
+        self._state = GoaLastFM.State.NOT_AVAILABLE
         self._reset_attributes()
-        Goa.Client.new(None, self._new_client_callback)
+        GoaBackend.Provider.get_all(self._get_all_providers_cb, None)
 
     def _reset_attributes(self):
         self._account = None
         self._authentication = None
-        self._state = GoaLastFM.State.NOT_CONFIGURED
         self._music_disabled_id = None
-        self.notify("state")
+
+    def _get_all_providers_cb(self, source, result, data):
+        try:
+            retrieved, providers = GoaBackend.Provider.get_all_finish(result)
+        except GLib.Error as error:
+            logger.warning("Error: {}, {}".format(
+                Goa.Error(error.code), error.message))
+            return
+
+        if retrieved is False:
+            logger.warning("Unable to get the list of GoaProvider.")
+            return
+
+        for provider in providers:
+            if provider.get_provider_name() == "Last.fm":
+                self._state = GoaLastFM.State.NOT_CONFIGURED
+                Goa.Client.new(None, self._new_client_callback)
+                self.notify("state")
+                break
 
     @log
     def _new_client_callback(self, source, result):
@@ -87,7 +111,9 @@ class GoaLastFM(GObject.GObject):
         account = obj.get_account()
         if account.props.provider_type == "lastfm":
             self._account.disconnect(self._music_disabled_id)
+            self._state = GoaLastFM.State.NOT_CONFIGURED
             self._reset_attributes()
+            self.notify("state")
 
     @log
     def _find_lastfm_account(self):
@@ -164,7 +190,7 @@ class LastFmScrobbler(GObject.GObject):
         self._report = self._settings.get_boolean("lastfm-report")
 
         self._scrobbled = False
-        self._account_state = GoaLastFM.State.NOT_CONFIGURED
+        self._account_state = GoaLastFM.State.NOT_AVAILABLE
 
         self._goa_lastfm = GoaLastFM()
         self._goa_lastfm.bind_property(
@@ -173,7 +199,7 @@ class LastFmScrobbler(GObject.GObject):
         self._soup_session = Soup.Session.new()
         self._scrobble_cache = []
 
-    @GObject.Property(type=int, default=GoaLastFM.State.NOT_CONFIGURED)
+    @GObject.Property(type=int, default=GoaLastFM.State.NOT_AVAILABLE)
     def account_state(self):
         """Get the Last.fm account state
 
diff --git a/gnomemusic/widgets/appmenu.py b/gnomemusic/widgets/appmenu.py
index 101c447a..837f4318 100644
--- a/gnomemusic/widgets/appmenu.py
+++ b/gnomemusic/widgets/appmenu.py
@@ -54,7 +54,7 @@ class AppMenu(Gtk.PopoverMenu):
 
     def _on_scrobbler_state_changed(self, klass, args):
         state = self._lastfm_scrobbler.props.account_state
-        if state == GoaLastFM.State.NOT_CONFIGURED:
+        if state <= GoaLastFM.State.NOT_CONFIGURED:
             self._lastfm_box.props.sensitive = False
             if self._lastfm_switcher_id is not None:
                 self._lastfm_switch.disconnect(self._lastfm_switcher_id)


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