diff options
Diffstat (limited to 'nephilim/mpclient.py')
-rw-r--r-- | nephilim/mpclient.py | 121 |
1 files changed, 33 insertions, 88 deletions
diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py index 2ee58b2..91afd40 100644 --- a/nephilim/mpclient.py +++ b/nephilim/mpclient.py @@ -8,7 +8,6 @@ from clSong import Song class MPClient(QtCore.QObject): """This class offers another layer above pympd, with usefull events.""" _client = None # MPD client - _listeners = None # array of listeners: { event: (listeners)* } # cached objects _cur_lib = None @@ -27,24 +26,9 @@ class MPClient(QtCore.QObject): _retr_mutex = QtCore.QMutex() - events = { - 'beforeSongChange':'curSongID', - 'onSongChange':'oldSongID, newSongID', - 'onTimeChange':'oldTime, newTime', - 'onStateChange':'oldState, newState', - 'onVolumeChange':'oldVolume, newVolume', - 'onConnect':'', - 'onDisconnect':'', - 'onReady':'', # when connected, and initialisation is ready - 'onUpdateDBStart':'', # start updating database - 'onUpdateDBFinish':'', # when updating database has finished - 'onPlaylistChange' : '', - } - def __init__(self): QtCore.QObject.__init__(self) self._client = None - self._listeners = {} self._cur_songid = -1 self._cur_time = -1 @@ -54,10 +38,7 @@ class MPClient(QtCore.QObject): self._cur_playlist = [] self._cur_plist_id = -1 - for event in self.events: - self._listeners[event] = [] - - def connect(self, host, port): + def connect_mpd(self, host, port): """Connect to MPD@host:port. Returns Tue at success, False otherwise.""" if self._client: return True @@ -68,18 +49,18 @@ class MPClient(QtCore.QObject): self._client = None return False - self._raiseEvent('onConnect', None) + self.emit(QtCore.SIGNAL('connected')) self._update_lib() self._update_playlist() self._update_current_song() self._timer_id = self.startTimer(500) - self._raiseEvent('onStateChange', {'oldState':'stop', 'newState':self.status()['state']}) - self._raiseEvent('onReady', None) + self.emit(QtCore.SIGNAL('state_changed'), 'stop', self.status()['state']) + self.emit(QtCore.SIGNAL('ready')) return True - def disconnect(self): + def disconnect_mpd(self): """Disconnect from MPD.""" if self._client: self._client.close() @@ -186,12 +167,7 @@ class MPClient(QtCore.QObject): def next(self): """Move on to the next song in the playlist.""" self._playCalled = False - self._raiseEvent('beforeSongChange', {'curSongID': self._cur_songid}) - # we only switch to the next song, if some of beforeSongChange's listeners - # didn't explicitly call play. If it did, then it ain't good to immediatly - # skip to the next song! - if not self._playCalled: - self._client.next() + self._client.next() def previous(self): """Move back to the previous song in the playlist.""" self._client.previous() @@ -236,17 +212,6 @@ class MPClient(QtCore.QObject): volume = min(100, max(0, volume)) self._client.setvol(volume) - def add_listener(self, event, callback): - """Add callback to the listeners for event.""" - if not(event in self.events): - raise Exception("Unknown event "+event) - self._listeners[event].append(callback) - def remove_listener(self, event, callback): - """Remove callback from listeners for event.""" - if not(event in self.events): - raise Exception("Unknown event "+event) - self._listeners[event].remove(callback) - def _retrieve(self, method): """Makes sure only one call is made at a time to MPD.""" self._retr_mutex.lock() @@ -282,80 +247,60 @@ class MPClient(QtCore.QObject): else: self._cur_song = Song(song) - class SimpleThread(Thread): - callback = None - params = None - def __init__(self,callback,params): - Thread.__init__(self) - self.callback = callback - self.params = params - def run(self): - self.callback(self.params) - - def _raiseEvent(self, event, params): - """Call all listeners for event with parameters params.""" - if not(event in self.events): - raise Exception("Unknown raised event "+event) - - for listener in self._listeners[event]: - self.SimpleThread(listener, params).run() - def timerEvent(self, event): """Check for changes since last check.""" status = self.status() - if status == None: + if not status: self._client = None - self._raiseEvent('onDisconnect', None) + self.emit(QtCore.SIGNAL('disconnected')) self.killTimer(self._timer_id) return + # check if song has changed self._update_current_song() - " check if song has changed" song = self._cur_song if song: - curID = song.getID() + cur_id = song.getID() else: - curID = -1 + cur_id = -1 - if curID != self._cur_songid: - self._raiseEvent('onSongChange', {'oldSongID':self._cur_songid, 'newSongID':curID}) - self._cur_songid = curID + if cur_id != self._cur_songid: + self.emit(QtCore.SIGNAL('song_changed'), cur_id) + self._cur_songid = cur_id - " check if the time has changed" + # check if the time has changed if 'time' in status: - curTime = status['time'] - if curTime!=self._cur_time: - self._raiseEvent('onTimeChange', {'oldTime':self._cur_time, 'newTime':curTime}) - self._cur_time = curTime - if curTime>=status['length']-1: - self._raiseEvent('beforeSongChange', {'curSongID':curID}) - - " check if the playing state has changed" + 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: - curState = status['state'] - if curState!=self._cur_state: - self._raiseEvent('onStateChange', {'oldState':self._cur_state, 'newState':curState}) - self._cur_state = curState + 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" + #check if the volume has changed if 'volume' in status: - curVolume = int(status['volume']) - if curVolume!=self._cur_volume: - self._raiseEvent('onVolumeChange', {'oldVolume':self._cur_volume, 'newVolume':curVolume}) - self._cur_volume = curVolume + 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._raiseEvent('onPlaylistChange', None) + 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'] - self._raiseEvent('onUpdateDBStart', {}) + self.emit(QtCore.SIGNAL('update_started')) if not('updatings_db' in status) and self._updatings_db: self._updatings_db = None - self._raiseEvent('onUpdateDBFinish') + self.emit(QtCore.SIGNAL('update_finished')) |