[gnome-music/wip/jfelder/lastfm-ui-v1: 4/6] scrobbler: Add a setter to can_scrobble property



commit e133df0286d406d1960a048b2cb0b3a2710a79a8
Author: Jean Felder <jfelder src gnome org>
Date:   Sat Jan 4 19:50:20 2020 +0100

    scrobbler: Add a setter to can_scrobble property
    
    Music is reported to Last.fm if the "report-music" setting is set to
    True and if there is a configured Last.fm account with music enabled.
    
    The can_scrobble setter has the following behavior:
    - If no account is configured, nothing happens.
    - If the new value is True, "report-music" is changed and music
    support in the Last.fm is enabled if necessary.
    - If the new value is False, "report-music" is changed but the Last.fm
    account is not changed.
    
    An account_state property is also added to LastFmScrobbler. It is
    bounded to the state property of GoaLastFM and allows to emit
    notifications when can_scrobble property value changes.

 gnomemusic/scrobbler.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 51 insertions(+), 3 deletions(-)
---
diff --git a/gnomemusic/scrobbler.py b/gnomemusic/scrobbler.py
index 207a3499..6c8714e7 100644
--- a/gnomemusic/scrobbler.py
+++ b/gnomemusic/scrobbler.py
@@ -120,6 +120,10 @@ class GoaLastFM(GObject.GObject):
         """
         return self._state
 
+    def enable_music(self):
+        """Enable music suport of the Last.fm account"""
+        self._account.props.music_disabled = False
+
     @GObject.Property
     def secret(self):
         """Retrieve the Last.fm client secret"""
@@ -159,11 +163,34 @@ class LastFmScrobbler(GObject.GObject):
         self._report = self._settings.get_boolean("report-music")
 
         self._scrobbled = False
+        self._account_state = GoaLastFM.State.NOT_CONFIGURED
+
         self._goa_lastfm = GoaLastFM()
+        self._goa_lastfm.bind_property(
+            "state", self, "account-state", GObject.BindingFlags.SYNC_CREATE)
+
         self._soup_session = Soup.Session.new()
 
-    @GObject.Property(
-        type=bool, default=False, flags=GObject.ParamFlags.READABLE)
+    @GObject.Property(type=int, default=GoaLastFM.State.NOT_CONFIGURED)
+    def account_state(self):
+        """Get the Last.fm account state
+
+        :returns: state of the Last.fm account
+        :rtype: GoaLastFM.State
+        """
+        return self._account_state
+
+    @account_state.setter
+    def account_state(self, value):
+        """Set the Last.fm account state
+
+        The account state depends on GoaLast.fm state property.
+        :param GoaLastFM.State value: new state
+        """
+        self._account_state = value
+        self.notify("can-scrobble")
+
+    @GObject.Property(type=bool, default=False)
     def can_scrobble(self):
         """Get can scrobble status
 
@@ -174,9 +201,30 @@ class LastFmScrobbler(GObject.GObject):
         :returns: True is music is reported to Last.fm
         :rtype: bool
         """
-        return (self._goa_lastfm.props.state == GoaLastFM.State.ENABLED
+        return (self.props.account_state == GoaLastFM.State.ENABLED
                 and self._report is True)
 
+    @can_scrobble.setter
+    def can_scrobble(self, value):
+        """Set the can_scrobble status
+
+        If no account is configured, nothing happens.
+        If the new value is True, "report-music" is changed and music
+        support in the Last.fm is enabled if necessary.
+        If the new value is False, "report-music" is changed but the
+        Last.fm account is not changed.
+        :param bool value: new value
+        """
+        if self.props.account_state == GoaLastFM.State.NOT_CONFIGURED:
+            return
+
+        if (value is True
+                and self.props.account_state == GoaLastFM.State.DISABLED):
+            self._goa_lastfm.enable_music()
+
+        self._settings.set_boolean("report-music", value)
+        self._report = value
+
     @GObject.Property(type=bool, default=False)
     def scrobbled(self):
         """Bool indicating current scrobble status"""


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