[gnome-music/wip/jfelder/smoothscale-player: 3/8] player: Define state GObject property



commit 751bb7fd458e7477991074f5f3381ab77b77b882
Author: Jean Felder <jfelder src gnome org>
Date:   Tue Oct 2 14:56:24 2018 +0200

    player: Define state GObject property
    
    This property will also be used by Smoothscale in addition to mpris
    and PlayerToolbar.

 gnomemusic/gstplayer.py             |  3 ++-
 gnomemusic/inhibitsuspend.py        | 15 +++++++--------
 gnomemusic/mpris.py                 |  6 +++---
 gnomemusic/player.py                | 33 +++++++++++++++------------------
 gnomemusic/widgets/playertoolbar.py |  6 +++---
 5 files changed, 30 insertions(+), 33 deletions(-)
---
diff --git a/gnomemusic/gstplayer.py b/gnomemusic/gstplayer.py
index 13dcb920..e3c7e028 100644
--- a/gnomemusic/gstplayer.py
+++ b/gnomemusic/gstplayer.py
@@ -206,7 +206,7 @@ class GstPlayer(GObject.GObject):
 
         return Playback.STOPPED
 
-    @GObject.Property
+    @GObject.Property(type=int)
     def state(self):
         """Current state of the player
 
@@ -310,6 +310,7 @@ class GstPlayer(GObject.GObject):
         self._player.seek_simple(
             Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT,
             seconds * Gst.SECOND)
+        self.state = self.state
 
     @log
     def _start_plugin_installation(
diff --git a/gnomemusic/inhibitsuspend.py b/gnomemusic/inhibitsuspend.py
index b6fc9d03..e6ea5f6a 100644
--- a/gnomemusic/inhibitsuspend.py
+++ b/gnomemusic/inhibitsuspend.py
@@ -51,8 +51,7 @@ class InhibitSuspend(GObject.GObject):
         self._player = player
         self._inhibit_cookie = 0
 
-        self._player.connect(
-            'playback-status-changed', self._on_playback_status_changed)
+        self._player.connect('notify::state', self._on_player_state_changed)
 
         self._settings = Gio.Settings.new('org.gnome.Music')
         self._should_inhibit = self._settings.get_boolean('inhibit-suspend')
@@ -79,14 +78,14 @@ class InhibitSuspend(GObject.GObject):
     @log
     def _on_inhibit_suspend_changed(self, settings, value):
         self._should_inhibit = value
-        self._on_playback_status_changed(None)
+        self._on_player_state_changed(None, None)
 
     @log
-    def _on_playback_status_changed(self, arguments):
-        if (self._player.get_playback_status() == Playback.PLAYING
-                or self._player.get_playback_status() == Playback.LOADING):
+    def _on_player_state_changed(self, klass, arguments):
+        if (self._player.props.state == Playback.PLAYING
+                or self._player.props.state == Playback.LOADING):
             self._inhibit_suspend()
 
-        if (self._player.get_playback_status() == Playback.PAUSED
-                or self._player.get_playback_status() == Playback.STOPPED):
+        if (self._player.props.state == Playback.PAUSED
+                or self._player.props.state == Playback.STOPPED):
             self._uninhibit_suspend()
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index 078e2324..d488d8c7 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -224,7 +224,7 @@ class MediaPlayer2Service(Server):
         self.player = app.get_active_window()._player
         self.player.connect(
             'song-changed', self._on_current_song_changed)
-        self.player.connect('playback-status-changed', self._on_playback_status_changed)
+        self.player.connect('notify::state', self._on_player_state_changed)
         self.player.connect('notify::repeat-mode', self._on_repeat_mode_changed)
         self.player.connect('volume-changed', self._on_volume_changed)
         self.player.connect('prev-next-invalidated', self._on_prev_next_invalidated)
@@ -242,7 +242,7 @@ class MediaPlayer2Service(Server):
 
     @log
     def _get_playback_status(self):
-        state = self.player.get_playback_status()
+        state = self.player.props.state
         if state == Playback.PLAYING:
             return 'Playing'
         elif state == Playback.PAUSED:
@@ -429,7 +429,7 @@ class MediaPlayer2Service(Server):
                                [])
 
     @log
-    def _on_playback_status_changed(self, data=None):
+    def _on_player_state_changed(self, klass, args):
         self.PropertiesChanged(MediaPlayer2Service.MEDIA_PLAYER2_PLAYER_IFACE,
                                {
                                    'PlaybackStatus': GLib.Variant('s', self._get_playback_status()),
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 79cfd4ac..9b09830b 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -497,7 +497,6 @@ class Player(GObject.GObject):
 
     __gsignals__ = {
         'clock-tick': (GObject.SignalFlags.RUN_FIRST, None, (int,)),
-        'playback-status-changed': (GObject.SignalFlags.RUN_FIRST, None, ()),
         'playlist-changed': (GObject.SignalFlags.RUN_FIRST, None, ()),
         'prev-next-invalidated': (GObject.SignalFlags.RUN_FIRST, None, ()),
         'seeked': (GObject.SignalFlags.RUN_FIRST, None, (int,)),
@@ -506,6 +505,8 @@ class Player(GObject.GObject):
         'volume-changed': (GObject.SignalFlags.RUN_FIRST, None, ()),
     }
 
+    state = GObject.Property(type=int, default=Playback.STOPPED)
+
     def __repr__(self):
         return '<Player>'
 
@@ -530,6 +531,10 @@ class Player(GObject.GObject):
         self._player = GstPlayer()
         self._player.connect('clock-tick', self._on_clock_tick)
         self._player.connect('eos', self._on_eos)
+        self._player.bind_property(
+            'state', self, 'state',
+            GObject.BindingFlags.SYNC_CREATE |
+            GObject.BindingFlags.BIDIRECTIONAL)
 
         root_window = parent_window.get_toplevel()
         self._inhibit_suspend = InhibitSuspend(root_window, self)
@@ -564,11 +569,11 @@ class Player(GObject.GObject):
         :returns: True if a song is currently played.
         :rtype: bool
         """
-        return self._player.state == Playback.PLAYING
+        return self.props.state == Playback.PLAYING
 
     @log
     def _load(self, song):
-        self._player.state = Playback.LOADING
+        self.props.state = Playback.LOADING
         self._time_stamp = int(time.time())
 
         url_ = song.get_url()
@@ -598,23 +603,20 @@ class Player(GObject.GObject):
                 and not self._playlist.set_song(song_index)):
             return False
 
-        if self._player.state != Playback.PAUSED:
+        if self.props.state != Playback.PAUSED:
             self._load(self._playlist.props.current_song)
 
-        self._player.state = Playback.PLAYING
-        self.emit('playback-status-changed')
+        self.props.state = Playback.PLAYING
 
     @log
     def pause(self):
         """Pause"""
-        self._player.state = Playback.PAUSED
-        self.emit('playback-status-changed')
+        self.props.state = Playback.PAUSED
 
     @log
     def stop(self):
         """Stop"""
-        self._player.state = Playback.STOPPED
-        self.emit('playback-status-changed')
+        self.props.state = Playback.STOPPED
 
     @log
     def next(self):
@@ -634,7 +636,7 @@ class Player(GObject.GObject):
         position = self._player.position
         if position >= 5:
             self._player.seek(0)
-            self._player.state = Playback.PLAYING
+            self.props.state = Playback.PLAYING
             return
 
         if self._playlist.previous():
@@ -643,7 +645,7 @@ class Player(GObject.GObject):
     @log
     def play_pause(self):
         """Toggle play/pause state"""
-        if self._player.state == Playback.PLAYING:
+        if self.props.state == Playback.PLAYING:
             self.pause()
         else:
             self.play()
@@ -660,7 +662,7 @@ class Player(GObject.GObject):
         playlist_changed = self._playlist.set_playlist(
             playlist_type, playlist_id, model, iter_)
 
-        if self._player.state == Playback.PLAYING:
+        if self.props.state == Playback.PLAYING:
             self.emit('prev-next-invalidated')
 
         if playlist_changed:
@@ -806,11 +808,6 @@ class Player(GObject.GObject):
         """GstPlayer getter"""
         return self._player
 
-    @log
-    def get_playback_status(self):
-        # FIXME: Just a proxy right now.
-        return self._player.state
-
     @log
     def get_position(self):
         return self._player.position
diff --git a/gnomemusic/widgets/playertoolbar.py b/gnomemusic/widgets/playertoolbar.py
index 5091dd13..d764802f 100644
--- a/gnomemusic/widgets/playertoolbar.py
+++ b/gnomemusic/widgets/playertoolbar.py
@@ -84,7 +84,7 @@ class PlayerToolbar(Gtk.ActionBar):
         self._player.connect('song-changed', self._update_view)
         self._player.connect('prev-next-invalidated', self._sync_prev_next)
         self._player.connect('notify::repeat-mode', self._sync_repeat_image)
-        self._player.connect('playback-status-changed', self._sync_playing)
+        self._player.connect('notify::state', self._sync_playing)
 
     @Gtk.Template.Callback()
     @log
@@ -132,11 +132,11 @@ class PlayerToolbar(Gtk.ActionBar):
         self._repeat_image.set_from_icon_name(icon, Gtk.IconSize.MENU)
 
     @log
-    def _sync_playing(self, player):
+    def _sync_playing(self, klass, args):
         if not self._main_window.props.selection_mode:
             self.show()
 
-        if self._player.get_playback_status() == Playback.PLAYING:
+        if self._player.props.state == Playback.PLAYING:
             image = self._pause_image
             tooltip = _("Pause")
         else:


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