[gnome-music/wip/jfelder/lastfm-ui-v1: 35/35] appmenu: Add an option to enable Last.fm scrobbler



commit 49cbdab28b5cfdf616c67a56e500fc5068d04251
Author: Jean Felder <jfelder src gnome org>
Date:   Sat Jan 4 15:34:08 2020 +0100

    appmenu: Add an option to enable Last.fm scrobbler
    
    If no Last.fm account is configured, the option stays insensitive.
    If an account, is configured, the switch allows to enable or disable
    music report.
    
    Related: #218

 data/ui/AppMenu.ui              | 55 ++++++++++++++++++++++++++++++++++++++---
 gnomemusic/widgets/appmenu.py   | 38 +++++++++++++++++++++++++++-
 gnomemusic/widgets/headerbar.py |  8 ++++--
 gnomemusic/window.py            |  2 +-
 4 files changed, 96 insertions(+), 7 deletions(-)
---
diff --git a/data/ui/AppMenu.ui b/data/ui/AppMenu.ui
index adaf3ce7..ae301568 100644
--- a/data/ui/AppMenu.ui
+++ b/data/ui/AppMenu.ui
@@ -10,6 +10,55 @@
         <property name="can_focus">False</property>
         <property name="margin">6</property>
         <property name="orientation">vertical</property>
+        <child>
+          <object class="GtkModelButton" id="lastfm_account_button">
+            <property name="can_focus">False</property>
+            <property name="halign">fill</property>
+            <property name="hexpand">False</property>
+            <property name="sensitive">False</property>
+            <property name="visible">True</property>
+            <property name="text" translatable="yes">Last.fm Account</property>
+          </object>
+          <packing>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkBox" id="_lastfm_box">
+            <property name="margin-left">5</property>
+            <property name="margin-right">5</property>
+            <property name="sensitive">False</property>
+            <property name="visible">True</property>
+            <child>
+              <object class="GtkLabel">
+                <property name="can_focus">False</property>
+                <property name="margin-right">12</property>
+                <property name="halign">start</property>
+                <property name="hexpand">False</property>
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Report Music Listening</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkSwitch" id="_lastfm_switch">
+                <property name="can_focus">True</property>
+                <property name="halign">end</property>
+                <property name="visible">True</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkSeparator">
+            <property name="visible">True</property>
+          </object>
+          <packing>
+            <property name="position">2</property>
+          </packing>
+        </child>
         <child>
           <object class="GtkModelButton" id="_keyboard_shortcuts_model_button">
             <property name="halign">fill</property>
@@ -21,7 +70,7 @@
             <property name="text" translatable="yes">_Keyboard Shortcuts</property>
           </object>
           <packing>
-            <property name="position">0</property>
+            <property name="position">3</property>
           </packing>
         </child>
         <child>
@@ -35,7 +84,7 @@
             <property name="text" translatable="yes">_Help</property>
           </object>
           <packing>
-            <property name="position">1</property>
+            <property name="position">4</property>
           </packing>
         </child>
         <child>
@@ -49,7 +98,7 @@
             <property name="text" translatable="yes">_About Music</property>
           </object>
           <packing>
-            <property name="position">2</property>
+            <property name="position">5</property>
           </packing>
         </child>
       </object>
diff --git a/gnomemusic/widgets/appmenu.py b/gnomemusic/widgets/appmenu.py
index a66d9299..101c447a 100644
--- a/gnomemusic/widgets/appmenu.py
+++ b/gnomemusic/widgets/appmenu.py
@@ -24,6 +24,8 @@
 
 from gi.repository import Gtk
 
+from gnomemusic.scrobbler import GoaLastFM
+
 
 @Gtk.Template(resource_path="/org/gnome/Music/ui/AppMenu.ui")
 class AppMenu(Gtk.PopoverMenu):
@@ -31,8 +33,42 @@ class AppMenu(Gtk.PopoverMenu):
 
     __gtype_name__ = "AppMenu"
 
+    _lastfm_box = Gtk.Template.Child()
+    _lastfm_switch = Gtk.Template.Child()
+
     def __repr__(self):
         return "<AppMenu>"
 
-    def __init__(self):
+    def __init__(self, application):
+        """Initialize the application menu
+
+        :param Application application: Application object
+        """
         super().__init__()
+
+        self._lastfm_switcher_id = None
+
+        self._lastfm_scrobbler = application.props.lastfm_scrobbler
+        self._lastfm_scrobbler.connect(
+            "notify::can-scrobble", self._on_scrobbler_state_changed)
+
+    def _on_scrobbler_state_changed(self, klass, args):
+        state = self._lastfm_scrobbler.props.account_state
+        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)
+                self._lastfm_switcher_id = None
+            return
+
+        self._lastfm_box.props.sensitive = True
+        if self._lastfm_switcher_id is None:
+            self._lastfm_switcher_id = self._lastfm_switch.connect(
+                "state-set", self._on_lastfm_switch_active)
+
+        with self._lastfm_switch.handler_block(self._lastfm_switcher_id):
+            can_scrobble = self._lastfm_scrobbler.props.can_scrobble
+            self._lastfm_switch.props.state = can_scrobble
+
+    def _on_lastfm_switch_active(self, klass, state):
+        self._lastfm_scrobbler.props.can_scrobble = state
diff --git a/gnomemusic/widgets/headerbar.py b/gnomemusic/widgets/headerbar.py
index e01d81e1..07a669c0 100644
--- a/gnomemusic/widgets/headerbar.py
+++ b/gnomemusic/widgets/headerbar.py
@@ -107,7 +107,11 @@ class HeaderBar(Gtk.HeaderBar):
         return "<HeaderBar>"
 
     @log
-    def __init__(self):
+    def __init__(self, application):
+        """Initialize Headerbar
+
+        :param Application application: Application object
+        """
         super().__init__()
 
         self._selection_mode = False
@@ -118,7 +122,7 @@ class HeaderBar(Gtk.HeaderBar):
 
         self._selection_menu = SelectionBarMenuButton()
 
-        self._menu_button.set_popover(AppMenu())
+        self._menu_button.set_popover(AppMenu(application))
 
         self.bind_property(
             "selection-mode", self, "show-close-button",
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index 05ebab87..6f3f9ee5 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -109,7 +109,7 @@ class Window(Gtk.ApplicationWindow):
         self._headerbar_stack = Gtk.Stack()
         transition_type = Gtk.StackTransitionType.CROSSFADE
         self._headerbar_stack.props.transition_type = transition_type
-        self._headerbar = HeaderBar()
+        self._headerbar = HeaderBar(self._app)
         self._search_headerbar = SearchHeaderBar(self._app)
         self._search_headerbar.props.stack = self._stack
         self._headerbar_stack.add_named(self._headerbar, "main")


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