[gnome-music/wip/jfelder/mpris-cleanup: 20/26] mpris: Handle signals emissions internally



commit 73071ebb1ac774c2a0ce7e8445253cb9a7665e1a
Author: Jean Felder <jfelder src gnome org>
Date:   Tue Oct 16 22:20:10 2018 +0200

    mpris: Handle signals emissions internally

 gnomemusic/mpris.py | 50 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 20 deletions(-)
---
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index 10b61a50..4f935806 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -54,6 +54,7 @@ class DBusInterface:
 
         method_outargs = {}
         method_inargs = {}
+        signals = {}
         for interface in Gio.DBusNodeInfo.new_for_xml(self.__doc__).interfaces:
 
             for method in interface.methods:
@@ -62,12 +63,18 @@ class DBusInterface:
                 method_inargs[method.name] = tuple(
                     arg.signature for arg in method.in_args)
 
+            for signal in interface.signals:
+                args = {arg.name: arg.signature for arg in signal.args}
+                signals[signal.name] = {
+                    'interface': interface.name, 'args': args}
+
             self._con.register_object(
                 object_path=self._path, interface_info=interface,
                 method_call_closure=self.on_method_call)
 
         self._method_inargs = method_inargs
         self._method_outargs = method_outargs
+        self._signals = signals
 
     def on_method_call(
         self, connection, sender, object_path, interface_name, method_name,
@@ -105,6 +112,17 @@ class DBusInterface:
         else:
             invocation.return_value(None)
 
+    def _dbus_emit_signal(self, signal_name, values):
+        signal = self._signals[signal_name]
+        parameters = []
+        for arg_name, arg_signature in signal['args'].items():
+            value = values[arg_name]
+            parameters.append(GLib.Variant(arg_signature, value))
+
+        variant = GLib.Variant.new_tuple(*parameters)
+        self._con.emit_signal(
+            None, self._path, signal['interface'], signal_name, variant)
+
 
 class MPRIS(DBusInterface):
     """
@@ -571,10 +589,7 @@ class MPRIS(DBusInterface):
     def _on_playlist_renamed(self, playlists, renamed_playlist):
         mpris_playlist = self._get_mpris_playlist_from_playlist(
             renamed_playlist)
-        self._con.emit_signal(
-            None, '/org/mpris/MediaPlayer2',
-            MPRIS.MEDIA_PLAYER2_PLAYLISTS_IFACE, 'PlaylistChanged',
-            GLib.Variant.new_tuple(GLib.Variant('(oss)', mpris_playlist)))
+        self._dbus_emit_signal('PlaylistChanged', {'Playlist': mpris_playlist})
 
     @log
     def _on_grilo_ready(self, grilo):
@@ -645,10 +660,7 @@ class MPRIS(DBusInterface):
 
         :param int position_msecond: new position in microseconds.
         """
-        variant = GLib.Variant.new_tuple(GLib.Variant('x', position_msecond))
-        self._con.emit_signal(
-            None, '/org/mpris/MediaPlayer2',
-            MPRIS.MEDIA_PLAYER2_PLAYER_IFACE, 'Seeked', variant)
+        self._dbus_emit_signal("Seeked", {"Position": position_msecond})
 
     def _get_tracks_metadata(self, track_paths):
         metadata = []
@@ -671,11 +683,10 @@ class MPRIS(DBusInterface):
         self.player.play(song_offset=song_offset)
 
     def _track_list_replaced(self, tracks, current_song):
-        self._con.emit_signal(
-            None, '/org/mpris/MediaPlayer2',
-            MPRIS.MEDIA_PLAYER2_TRACKLIST_IFACE, 'TrackListReplaced',
-            GLib.Variant.new_tuple(
-                GLib.Variant('ao', tracks), GLib.Variant('o', current_song)))
+        parameters = {
+            "Tracks": self._path_list,
+            "CurrentTrack": self._get_song_dbus_path()}
+        self._dbus_emit_signal("TrackListReplaced", parameters)
 
     def _activate_playlist(self, playlist_path):
         playlist_id = self._get_playlist_from_dbus_path(playlist_path).get_id()
@@ -792,13 +803,12 @@ class MPRIS(DBusInterface):
 
     def _properties_changed(self, interface_name, changed_properties,
                             invalidated_properties):
-        self._con.emit_signal(
-            None, '/org/mpris/MediaPlayer2', 'org.freedesktop.DBus.Properties',
-            'PropertiesChanged',
-            GLib.Variant.new_tuple(
-                GLib.Variant('s', interface_name),
-                GLib.Variant('a{sv}', changed_properties),
-                GLib.Variant('as', invalidated_properties)))
+        parameters = {
+            'interface_name': interface_name,
+            'changed_properties': changed_properties,
+            'invalidated_properties': invalidated_properties
+        }
+        self._dbus_emit_signal('PropertiesChanged', parameters)
 
     def _introspect(self):
         return self.__doc__


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