[gnome-music/wip/jfelder/multimedia-keys: 3/4] window: Create media_keys_proxy only once



commit f8cbfed4fa578087c20c542d8a4c77c7525a0db1
Author: Jean Felder <jfelder src gnome org>
Date:   Wed Feb 21 11:05:50 2018 +0100

    window: Create media_keys_proxy only once
    
    Refactor media_player_keys code.
    Correctly register warnings and errors from media_keys.

 gnomemusic/window.py | 49 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 30 insertions(+), 19 deletions(-)
---
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index 18a19d1..18d74d8 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -67,7 +67,6 @@ class Window(Gtk.ApplicationWindow):
     def __init__(self, app):
         super().__init__(application=app, title=_("Music"))
 
-        self.connect('focus-in-event', self._windows_focus_cb)
         self.settings = Gio.Settings.new('org.gnome.Music')
         self.add_action(self.settings.create_action('repeat'))
         self.set_size_request(200, 100)
@@ -93,6 +92,10 @@ class Window(Gtk.ApplicationWindow):
         self.notifications_popup = NotificationsPopup()
         self._overlay.add_overlay(self.notifications_popup)
 
+        self._media_keys_proxy = None
+        self._init_media_keys_proxy()
+        self.connect('focus-in-event', self._windows_focus_cb)
+
         self.window_size_update_timeout = None
         self.connect("window-state-event", self._on_window_state_event)
         self.connect("configure-event", self._on_configure_event)
@@ -144,34 +147,42 @@ class Window(Gtk.ApplicationWindow):
         self.settings.set_boolean('window-maximized', 'GDK_WINDOW_STATE_MAXIMIZED' in 
event.new_window_state.value_names)
 
     @log
-    def _grab_media_player_keys(self):
-        self.proxy = Gio.DBusProxy.new_sync(Gio.bus_get_sync(Gio.BusType.SESSION, None),
-                                            Gio.DBusProxyFlags.NONE,
-                                            None,
-                                            'org.gnome.SettingsDaemon.MediaKeys',
-                                            '/org/gnome/SettingsDaemon/MediaKeys',
-                                            'org.gnome.SettingsDaemon.MediaKeys',
-                                            None)
-        self.proxy.call_sync('GrabMediaPlayerKeys',
-                             GLib.Variant('(su)', ('Music', 0)),
-                             Gio.DBusCallFlags.NONE,
-                             -1,
-                             None)
-        self.proxy.connect('g-signal', self._handle_media_keys)
+    def _init_media_keys_proxy(self):
+        self._media_keys_proxy = Gio.DBusProxy.new_sync(
+            Gio.bus_get_sync(Gio.BusType.SESSION, None),
+            Gio.DBusProxyFlags.NONE, None,
+            'org.gnome.SettingsDaemon.MediaKeys',
+            '/org/gnome/SettingsDaemon/MediaKeys',
+            'org.gnome.SettingsDaemon.MediaKeys', None)
+
+        self._grab_media_player_keys()
+        self._media_keys_proxy.connect('g-signal', self._handle_media_keys)
 
     @log
     def _windows_focus_cb(self, window, event):
+        self._grab_media_player_keys()
+
+    @log
+    def _grab_media_player_keys(self):
+        if not self._media_keys_proxy:
+            return
+
         try:
-            self._grab_media_player_keys()
+            self._media_keys_proxy.call_sync(
+                'GrabMediaPlayerKeys', GLib.Variant('(su)', ('Music', 0)),
+                Gio.DBusCallFlags.NONE, -1, None)
         except GLib.GError:
-            # We cannot grab media keys if no settings daemon is running
-            pass
+            logger.error('Failed to contact settings daemon, cannot grab '
+                         'media keys')
 
     @log
     def _handle_media_keys(self, proxy, sender, signal, parameters):
         if signal != 'MediaPlayerKeyPressed':
-            print('Received an unexpected signal \'%s\' from media player'.format(signal))
+            logger.warning(
+                'Error: Received an unexpected signal \'{}\' from media '
+                'player'.format(signal))
             return
+
         response = parameters.get_child_value(1).get_string()
         if 'Play' in response:
             self.player.play_pause()


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