[gnome-music/wip/jfelder/mpris-cleanup: 19/25] mpris: Handle signals emissions internally



commit e0d2898f907466f724608ffeea31d5bf097ede62
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 8c494171..f7718ef3 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -63,6 +63,7 @@ class DBusInterface:
 
         method_outargs = {}
         method_inargs = {}
+        signals = {}
         for interface in Gio.DBusNodeInfo.new_for_xml(self.__doc__).interfaces:
 
             for method in interface.methods:
@@ -71,12 +72,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,
@@ -114,6 +121,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)
+
     @staticmethod
     def camelcase_to_snake_case(name):
         s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
@@ -572,10 +590,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):
@@ -646,10 +661,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 = []
@@ -672,11 +684,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()
@@ -793,13 +804,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]