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



commit b66f84cd45fc7cc060b8a86b1507ef648d4d4ea8
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 0dfeb12c..54d3b4d0 100644
--- a/gnomemusic/scrobbler.py
+++ b/gnomemusic/scrobbler.py
@@ -121,6 +121,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"""
@@ -160,11 +164,34 @@ class LastFmScrobbler(GObject.GObject):
         self._report = self._settings.get_boolean("lastfm-report")
 
         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
 
@@ -175,9 +202,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, "lastfm-report" is changed and music
+        support in the Last.fm is enabled if necessary.
+        If the new value is False, "lastfm-report" 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("lastfm-report", 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]