diff options
author | Anton Khirnov <wyskas@gmail.com> | 2009-09-08 16:53:13 +0200 |
---|---|---|
committer | Anton Khirnov <wyskas@gmail.com> | 2009-09-08 17:25:36 +0200 |
commit | 9d38f9d17149e137b3f0404605a82f741a57fee1 (patch) | |
tree | 4142da99cef22807739c695cf70237157c6e0939 /nephilim/mpclient.py | |
parent | deeda78d5ccfb6a33e214f639cebd34dd790accd (diff) |
mpd: rewrite connecting/disconnecting functions to use signals.
Diffstat (limited to 'nephilim/mpclient.py')
-rw-r--r-- | nephilim/mpclient.py | 93 |
1 files changed, 43 insertions, 50 deletions
diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py index a7b09de..e691989 100644 --- a/nephilim/mpclient.py +++ b/nephilim/mpclient.py @@ -30,6 +30,7 @@ class MPClient(QtCore.QObject): outputs = None # private + __password = None _client = None _cur_song = None _status = {'volume' : 0, 'repeat' : 0, 'random' : 0, @@ -71,7 +72,7 @@ class MPClient(QtCore.QObject): #### public #### def __init__(self, mpclient, name, id, state): - QtCore.QObject.__init__(self, mpclient) + QtCore.QObject.__init__(self) self.mpclient = mpclient self.name = name @@ -97,65 +98,24 @@ class MPClient(QtCore.QObject): self.logger = logging.getLogger('mpclient') def connect_mpd(self, host, port, password = None): - """Connect to MPD@host:port, optionally using password. - Returns True at success, False otherwise.""" - + """Connect to MPD@host:port, optionally using password.""" self.logger.info('Connecting to MPD...') if self._client: self.logger.warning('Attempted to connect when already connected.') - return True - - try: - self._client = mpd.MPDClient() - self._client.connect(host, port) - except socket.error, e: - self.logger.error('Socket error: %s.'%e) - self.disconnect_mpd() - return False - - if password: - self.password(password) - else: - self._commands = list(self._client.commands()) - - if not self.__check_command_ok('listallinfo'): - self.logger.error('Don\'t have MPD read permission, diconnecting.') - return self.disconnect_mpd() + return - self.__update_current_song() - self.__update_outputs() - self._db_update = self.stats()['db_update'] + self._client = mpd.MPDClient() + self._client.connect_changed.connect(lambda val:self.__finish_connect() if val else self.__finish_disconnect()) + self._client.connect_mpd(host, port) + self.__password = password - self.connect_changed.emit(True) - self.logger.info('Successfully connected to MPD.') - self._timer_id = self.startTimer(500) - self._db_timer_id = self.startTimer(10000) - return True def disconnect_mpd(self): """Disconnect from MPD.""" self.logger.info('Disconnecting from MPD...') - if self._client: - try: - self._client.close() - self._client.disconnect() - except (mpd.ConnectionError, socket.error): - pass - self._client = None - else: + if not self._client: self.logger.warning('Attempted to disconnect when not connected.') + self._client.disconnect_mpd() - if self._timer_id: - self.killTimer(self._timer_id) - self._timer_id = None - if self._db_timer_id: - self.killTimer(self._db_timer_id) - self._db_timer_id = None - self._status = dict(MPClient._status) - self._cur_song = None - self._commands = [] - self.outputs = [] - self.connect_changed.emit(False) - self.logger.info('Disconnected from MPD.') def password(self, password): """Use the password to authenticate with MPD.""" self.logger.info('Authenticating with MPD.') @@ -387,6 +347,39 @@ class MPClient(QtCore.QObject): self._client.command_list_end() #### private #### + def __finish_connect(self): + if self.__password: + self.password(self.__password) + else: + self._commands = list(self._client.commands()) + + if not self.__check_command_ok('listallinfo'): + self.logger.error('Don\'t have MPD read permission, diconnecting.') + return self.disconnect_mpd() + + self.__update_current_song() + self.__update_outputs() + self._db_update = self.stats()['db_update'] + + self.connect_changed.emit(True) + self.logger.info('Successfully connected to MPD.') + self._timer_id = self.startTimer(500) + self._db_timer_id = self.startTimer(1000) + def __finish_disconnect(self): + self._client = None + + if self._timer_id: + self.killTimer(self._timer_id) + self._timer_id = None + if self._db_timer_id: + self.killTimer(self._db_timer_id) + self._db_timer_id = None + self._status = dict(MPClient._status) + self._cur_song = None + self._commands = [] + self.outputs = [] + self.connect_changed.emit(False) + self.logger.info('Disconnected from MPD.') def __update_current_song(self): """Update the current song.""" song = self._client.currentsong() |