diff options
author | Anton Khirnov <wyskas@gmail.com> | 2009-03-07 13:30:11 +0100 |
---|---|---|
committer | Anton Khirnov <wyskas@gmail.com> | 2009-03-07 13:30:11 +0100 |
commit | d827aded064c538eee0e96e4aebf97f19821b40c (patch) | |
tree | 8385b21afec038ba911f52efb58ac8e6af5a569d /nephilim/mpclient.py | |
parent | 371d1cd71c442b5edeab10b0ae94dbecb9f4059f (diff) |
mpclient: better handling of status changes.
also remove dangerous 'connected' signal.
Diffstat (limited to 'nephilim/mpclient.py')
-rw-r--r-- | nephilim/mpclient.py | 112 |
1 files changed, 47 insertions, 65 deletions
diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py index 91afd40..c2a583b 100644 --- a/nephilim/mpclient.py +++ b/nephilim/mpclient.py @@ -13,16 +13,11 @@ class MPClient(QtCore.QObject): _cur_lib = None _cur_playlist = None _cur_song = None + _status = {'volume' : None, 'repeat' : None, 'random' : None, + 'playlist' : None, 'playlistlength' : None, 'state' : None, + 'time' : None, 'length' : None, 'xfade' : None, 'updatings_db' : None} - # objects used for comparison with previous value - _cur_songid = None - _cur_time = None - _cur_state = None - _cur_volume = None - _updatings_db = None - _cur_plist_id = None - - _timer_id = None + _timer_id = None _retr_mutex = QtCore.QMutex() @@ -49,15 +44,13 @@ class MPClient(QtCore.QObject): self._client = None return False - self.emit(QtCore.SIGNAL('connected')) - self._update_lib() self._update_playlist() self._update_current_song() - self._timer_id = self.startTimer(500) - self.emit(QtCore.SIGNAL('state_changed'), 'stop', self.status()['state']) - self.emit(QtCore.SIGNAL('ready')) + self.emit(QtCore.SIGNAL('connected')) + self.timerEvent(None) + self._timer_id = self.startTimer(500) return True def disconnect_mpd(self): @@ -89,6 +82,10 @@ class MPClient(QtCore.QObject): return None return self._cur_song + def status(self): + """Get current MPD status.""" + return self._status + def update_db(self, paths = None): """Starts MPD database update.""" if not paths: @@ -98,22 +95,6 @@ class MPClient(QtCore.QObject): self._client.update(path) self._client.command_list_end() - def status(self): - """Get current status""" - if self.is_connected() == False: - return None - ret = self._retrieve(self._client.status) - if not ret: - return None - if 'time' in ret: - len = int(ret['time'][ret['time'].find(':')+1:]) - cur = int(ret['time'][:ret['time'].find(':')]) - ret['length'] = len - ret['time'] = cur - else: - ret['length'] = 0 - ret['time'] = 0 - return ret def outputs(self): """Returns an array of configured MPD audio outputs.""" @@ -246,12 +227,31 @@ class MPClient(QtCore.QObject): self._cur_song = None else: self._cur_song = Song(song) + def _update_status(self): + """Get current status""" + if self.is_connected() == False: + return None + ret = self._retrieve(self._client.status) + if not ret: + return None + if 'time' in ret: + cur, len = ret['time'].split(':') + ret['length'] = int(len) + ret['time'] = int(cur) + else: + ret['length'] = 0 + ret['time'] = 0 + + if not 'updatings_db' in ret: + ret['updatings_db'] = 0 + return ret def timerEvent(self, event): """Check for changes since last check.""" - status = self.status() + old_status = self._status + self._status = self._update_status() - if not status: + if not self._status: self._client = None self.emit(QtCore.SIGNAL('disconnected')) self.killTimer(self._timer_id) @@ -269,38 +269,20 @@ class MPClient(QtCore.QObject): self.emit(QtCore.SIGNAL('song_changed'), cur_id) self._cur_songid = cur_id - # check if the time has changed - if 'time' in status: - cur_time = status['time'] - if cur_time != self._cur_time: - self.emit(QtCore.SIGNAL('time_changed'), cur_time) - self._cur_time = cur_time - - # check if the playing state has changed - if 'state' in status: - cur_state = status['state'] - if cur_state != self._cur_state: - self.emit(QtCore.SIGNAL('state_changed'), cur_state) - self._cur_state = cur_state - - #check if the volume has changed - if 'volume' in status: - cur_vol = int(status['volume']) - if cur_vol != self._cur_volume: - self.emit(QtCore.SIGNAL('volume_changed'), cur_vol) - self._cur_volume = cur_vol - - if 'playlist' in status: - cur_plist_id = int(status['playlist']) - if cur_plist_id != self._cur_plist_id: - self._update_playlist() - self.emit(QtCore.SIGNAL('playlist_changed')) - self._cur_plist_id = cur_plist_id - - " update has started" - if 'updatings_db' in status and self._updatings_db == None: - self._updatings_db = status['updatings_db'] + if self._status['time'] != old_status['time']: + self.emit(QtCore.SIGNAL('time_changed'), self._status['time']) + + if self._status['state'] != old_status['state']: + self.emit(QtCore.SIGNAL('state_changed'), self._status['state']) + + if self._status['volume'] != old_status['volume']: + self.emit(QtCore.SIGNAL('volume_changed'), int(self._status['volume'])) + + if self._status['playlist'] != old_status['playlist']: + self._update_playlist() + self.emit(QtCore.SIGNAL('playlist_changed')) + + if self._status['updatings_db'] and not old_status['updatings_db']: self.emit(QtCore.SIGNAL('update_started')) - if not('updatings_db' in status) and self._updatings_db: - self._updatings_db = None + if not self._status['updatings_db'] and old_status['updatings_db']: self.emit(QtCore.SIGNAL('update_finished')) |