From 0edb624fecc9a4031c8aa9d06dadceec9feb2f33 Mon Sep 17 00:00:00 2001 From: Thibaut GIRKA Date: Thu, 26 Nov 2009 21:53:44 +0100 Subject: [PATCH 1/2] Renamed a few methods, refactor on_*_button_toggled, use getattr/setattr instead of eval/self.__dict__ --- src/chat_control.py | 78 +++++++++++++++--------------------- src/common/jingle.py | 4 +- src/common/jingle_content.py | 12 +++--- src/common/jingle_rtp.py | 2 +- 4 files changed, 42 insertions(+), 54 deletions(-) diff --git a/src/chat_control.py b/src/chat_control.py index 554ac1b02..13ed056d9 100644 --- a/src/chat_control.py +++ b/src/chat_control.py @@ -1510,22 +1510,23 @@ class ChatControl(ChatControlBase): def _update_jingle(self, jingle_type): if jingle_type not in ('audio', 'video'): return - if self.__dict__[jingle_type + '_state'] in ( + banner_image = getattr(self, '_' + jingle_type + '_banner_image') + if getattr(self, jingle_type + '_state') in ( self.JINGLE_STATE_NOT_AVAILABLE, self.JINGLE_STATE_AVAILABLE): - self.__dict__['_' + jingle_type + '_banner_image'].hide() + banner_image.hide() else: - self.__dict__['_' + jingle_type + '_banner_image'].show() + banner_image.show() if self.audio_state == self.JINGLE_STATE_CONNECTING: - self.__dict__['_' + jingle_type + '_banner_image'].set_from_stock( + banner_image.set_from_stock( gtk.STOCK_CONVERT, 1) elif self.audio_state == self.JINGLE_STATE_CONNECTION_RECEIVED: - self.__dict__['_' + jingle_type + '_banner_image'].set_from_stock( + banner_image.set_from_stock( gtk.STOCK_NETWORK, 1) elif self.audio_state == self.JINGLE_STATE_CONNECTED: - self.__dict__['_' + jingle_type + '_banner_image'].set_from_stock( + banner_image.set_from_stock( gtk.STOCK_CONNECT, 1) elif self.audio_state == self.JINGLE_STATE_ERROR: - self.__dict__['_' + jingle_type + '_banner_image'].set_from_stock( + banner_image.set_from_stock( gtk.STOCK_DIALOG_WARNING, 1) self.update_toolbar() @@ -1566,27 +1567,27 @@ class ChatControl(ChatControlBase): if state in states: jingle_state = states[state] - if self.__dict__[jingle_type + '_state'] == jingle_state: + if getattr(self, jingle_type + '_state') == jingle_state: return - self.__dict__[jingle_type + '_state'] = jingle_state + setattr(self, jingle_type + '_state', jingle_state) # Destroy existing session with the user when he signs off # We need to do that before modifying the sid if state == 'not_available': gajim.connections[self.account].delete_jingle_session( - self.contact.get_full_jid(), self.__dict__[jingle_type + '_sid']) + self.contact.get_full_jid(), getattr(self, jingle_type + '_sid')) if state in ('not_available', 'available', 'stop'): - self.__dict__[jingle_type + '_sid'] = None + setattr(self, jingle_type + '_sid', None) if state in ('connection_received', 'connecting'): - self.__dict__[jingle_type + '_sid'] = sid + setattr(self, jingle_type + '_sid', sid) if state in ('connecting', 'connected', 'connection_received'): - self.__dict__['_' + jingle_type + '_button'].set_active(True) + getattr(self, '_' + jingle_type + '_button').set_active(True) elif state in ('not_available', 'stop'): - self.__dict__['_' + jingle_type + '_button'].set_active(False) + getattr(self, '_' + jingle_type + '_button').set_active(False) - eval('self.update_' + jingle_type)() + getattr(self, 'update_' + jingle_type)() def set_audio_state(self, state, sid=None, reason=None): self._set_jingle_state('audio', state, sid=sid, reason=reason) @@ -1799,45 +1800,32 @@ class ChatControl(ChatControlBase): banner_name_label.set_markup(label_text) banner_name_label.set_tooltip_text(label_tooltip) - def on_audio_button_toggled(self, widget): + def on_jingle_button_toggled(self, widget, jingle_type): + path_to_img = os.path.join(gajim.DATA_DIR, 'pixmaps', '%s_%s.png' + % ({'audio': 'mic', 'video': 'cam'}[jingle_type], + {True: 'active', False: 'inactive'}[widget.get_active()])) + if widget.get_active(): - path_to_img = os.path.join(gajim.DATA_DIR, 'pixmaps', - 'mic_active.png') - if self.audio_state == self.JINGLE_STATE_AVAILABLE: - sid = gajim.connections[self.account].startVoIP( - self.contact.get_full_jid()) - self.set_audio_state('connecting', sid) + if getattr(self, jingle_type + '_state') == self.JINGLE_STATE_AVAILABLE: + sid = getattr(gajim.connections[self.account], + 'start_' + jingle_type)(self.contact.get_full_jid()) + getattr(self, 'set_' + jingle_type + '_state')('connecting', sid) else: - path_to_img = os.path.join(gajim.DATA_DIR, 'pixmaps', - 'mic_inactive.png') session = gajim.connections[self.account].get_jingle_session( - self.contact.get_full_jid(), self.audio_sid) + self.contact.get_full_jid(), getattr(self, jingle_type + '_sid')) if session: - content = session.get_content('audio') + content = session.get_content(jingle_type) if content: session.remove_content(content.creator, content.name) - img = self._audio_button.get_property('image') + + img = getattr(self, '_' + jingle_type + '_button').get_property('image') img.set_from_file(path_to_img) + def on_audio_button_toggled(self, widget): + self.on_jingle_button_toggled(widget, 'audio') + def on_video_button_toggled(self, widget): - if widget.get_active(): - path_to_img = os.path.join(gajim.DATA_DIR, 'pixmaps', - 'cam_active.png') - if self.video_state == self.JINGLE_STATE_AVAILABLE: - sid = gajim.connections[self.account].startVideoIP( - self.contact.get_full_jid()) - self.set_video_state('connecting', sid) - else: - path_to_img = os.path.join(gajim.DATA_DIR, 'pixmaps', - 'cam_inactive.png') - session = gajim.connections[self.account].get_jingle_session( - self.contact.get_full_jid(), self.video_sid) - if session: - content = session.get_content('video') - if content: - session.remove_content(content.creator, content.name) - img = self._video_button.get_property('image') - img.set_from_file(path_to_img) + self.on_jingle_button_toggled(widget, 'video') def _toggle_gpg(self): if not self.gpg_is_active and not self.contact.keyID: diff --git a/src/common/jingle.py b/src/common/jingle.py index c4f7bc22d..9f8d626a4 100644 --- a/src/common/jingle.py +++ b/src/common/jingle.py @@ -105,7 +105,7 @@ class ConnectionJingle(object): raise xmpp.NodeProcessed - def startVoIP(self, jid): + def start_audio(self, jid): if self.get_jingle_session(jid, media='audio'): return self.get_jingle_session(jid, media='audio').sid jingle = self.get_jingle_session(jid, media='video') @@ -118,7 +118,7 @@ class ConnectionJingle(object): jingle.start_session() return jingle.sid - def startVideoIP(self, jid): + def start_video(self, jid): if self.get_jingle_session(jid, media='video'): return self.get_jingle_session(jid, media='video').sid jingle = self.get_jingle_session(jid, media='audio') diff --git a/src/common/jingle_content.py b/src/common/jingle_content.py index 8aa19b45c..5ebcefd9f 100644 --- a/src/common/jingle_content.py +++ b/src/common/jingle_content.py @@ -66,10 +66,10 @@ class JingleContent(object): 'iq-result': [], 'iq-error': [], # these are called when *we* sent these stanzas - 'content-accept-sent': [self.__fillJingleStanza], - 'content-add-sent': [self.__fillJingleStanza], - 'session-initiate-sent': [self.__fillJingleStanza], - 'session-accept-sent': [self.__fillJingleStanza], + 'content-accept-sent': [self.__fill_jingle_stanza], + 'content-add-sent': [self.__fill_jingle_stanza], + 'session-initiate-sent': [self.__fill_jingle_stanza], + 'session-accept-sent': [self.__fill_jingle_stanza], 'session-terminate-sent': [], } @@ -112,11 +112,11 @@ class JingleContent(object): content.addChild(self.transport.make_transport([candidate])) self.session.send_transport_info(content) - def __fillJingleStanza(self, stanza, content, error, action): + def __fill_jingle_stanza(self, stanza, content, error, action): """ Add our things to session-initiate stanza """ - self._fillContent(content) + self._fill_content(content) self.sent = True content.addChild(node=self.transport.make_transport()) diff --git a/src/common/jingle_rtp.py b/src/common/jingle_rtp.py index 879bf4a2b..8a36c9eca 100644 --- a/src/common/jingle_rtp.py +++ b/src/common/jingle_rtp.py @@ -121,7 +121,7 @@ class JingleRTPContent(JingleContent): def _stop_dtmf(self): self.p2psession.stop_telephony_event(farsight.DTMF_METHOD_RTP_RFC4733) - def _fillContent(self, content): + def _fill_content(self, content): content.addChild(xmpp.NS_JINGLE_RTP + ' description', attrs={'media': self.media}, payload=self.iter_codecs()) From ee452a57a2678f08257cac7233c6b60501e852ae Mon Sep 17 00:00:00 2001 From: Thibaut GIRKA Date: Thu, 26 Nov 2009 22:09:34 +0100 Subject: [PATCH 2/2] [Jingle] Renamed callbacks to match name conventions --- src/common/jingle.py | 4 +- src/common/jingle_content.py | 14 +++--- src/common/jingle_rtp.py | 20 ++++----- src/common/jingle_session.py | 86 ++++++++++++++++++------------------ 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/common/jingle.py b/src/common/jingle.py index 9f8d626a4..65803a2ed 100644 --- a/src/common/jingle.py +++ b/src/common/jingle.py @@ -86,7 +86,7 @@ class ConnectionJingle(object): id = stanza.getID() if (jid, id) in self.__iq_responses.keys(): - self.__iq_responses[(jid, id)].stanzaCB(stanza) + self.__iq_responses[(jid, id)].on_stanza(stanza) del self.__iq_responses[(jid, id)] raise xmpp.NodeProcessed @@ -101,7 +101,7 @@ class ConnectionJingle(object): self.add_jingle(newjingle) # we already have such session in dispatcher... - self.__sessions[(jid, sid)].stanzaCB(stanza) + self.__sessions[(jid, sid)].on_stanza(stanza) raise xmpp.NodeProcessed diff --git a/src/common/jingle_content.py b/src/common/jingle_content.py index 5ebcefd9f..4f0ae0b1d 100644 --- a/src/common/jingle_content.py +++ b/src/common/jingle_content.py @@ -48,18 +48,18 @@ class JingleContent(object): self.callbacks = { # these are called when *we* get stanzas - 'content-accept': [self.__transportInfoCB], - 'content-add': [self.__transportInfoCB], + 'content-accept': [self.__on_transport_info], + 'content-add': [self.__on_transport_info], 'content-modify': [], 'content-reject': [], 'content-remove': [], 'description-info': [], 'security-info': [], - 'session-accept': [self.__transportInfoCB], + 'session-accept': [self.__on_transport_info], 'session-info': [], - 'session-initiate': [self.__transportInfoCB], + 'session-initiate': [self.__on_transport_info], 'session-terminate': [], - 'transport-info': [self.__transportInfoCB], + 'transport-info': [self.__on_transport_info], 'transport-replace': [], 'transport-accept': [], 'transport-reject': [], @@ -82,7 +82,7 @@ class JingleContent(object): """ pass - def stanzaCB(self, stanza, content, error, action): + def on_stanza(self, stanza, content, error, action): """ Called when something related to our content was sent by peer """ @@ -90,7 +90,7 @@ class JingleContent(object): for callback in self.callbacks[action]: callback(stanza, content, error, action) - def __transportInfoCB(self, stanza, content, error, action): + def __on_transport_info(self, stanza, content, error, action): """ Got a new transport candidate """ diff --git a/src/common/jingle_rtp.py b/src/common/jingle_rtp.py index 8a36c9eca..78b8c23dd 100644 --- a/src/common/jingle_rtp.py +++ b/src/common/jingle_rtp.py @@ -47,14 +47,14 @@ class JingleRTPContent(JingleContent): self.candidates_ready = False # True when local candidates are prepared - self.callbacks['session-initiate'] += [self.__getRemoteCodecsCB] - self.callbacks['content-add'] += [self.__getRemoteCodecsCB] - self.callbacks['content-accept'] += [self.__getRemoteCodecsCB, - self.__contentAcceptCB] - self.callbacks['session-accept'] += [self.__getRemoteCodecsCB, - self.__contentAcceptCB] - self.callbacks['session-accept-sent'] += [self.__contentAcceptCB] - self.callbacks['content-accept-sent'] += [self.__contentAcceptCB] + self.callbacks['session-initiate'] += [self.__on_remote_codecs] + self.callbacks['content-add'] += [self.__on_remote_codecs] + self.callbacks['content-accept'] += [self.__on_remote_codecs, + self.__on_content_accept] + self.callbacks['session-accept'] += [self.__on_remote_codecs, + self.__on_content_accept] + self.callbacks['session-accept-sent'] += [self.__on_content_accept] + self.callbacks['content-accept-sent'] += [self.__on_content_accept] self.callbacks['session-terminate'] += [self.__stop] self.callbacks['session-terminate-sent'] += [self.__stop] @@ -172,7 +172,7 @@ class JingleRTPContent(JingleContent): else: print name - def __contentAcceptCB(self, stanza, content, error, action): + def __on_content_accept(self, stanza, content, error, action): if self.accepted: if self.transport.remote_candidates: self.p2pstream.set_remote_candidates(self.transport.remote_candidates) @@ -181,7 +181,7 @@ class JingleRTPContent(JingleContent): self.p2pstream.set_property('direction', farsight.DIRECTION_BOTH) self.session.content_negociated(self.media) - def __getRemoteCodecsCB(self, stanza, content, error, action): + def __on_remote_codecs(self, stanza, content, error, action): ''' Get peer codecs from what we get from peer. ''' if self.got_codecs: return diff --git a/src/common/jingle_session.py b/src/common/jingle_session.py index b6238319a..d6f090fce 100644 --- a/src/common/jingle_session.py +++ b/src/common/jingle_session.py @@ -89,28 +89,28 @@ class JingleSession(object): # use .prepend() to add new callbacks, especially when you're going # to send error instead of ack self.callbacks = { - 'content-accept': [self.__contentAcceptCB, self.__broadcastCB, - self.__defaultCB], - 'content-add': [self.__contentAddCB, self.__broadcastCB, - self.__defaultCB], #TODO - 'content-modify': [self.__defaultCB], #TODO - 'content-reject': [self.__defaultCB, self.__contentRemoveCB], #TODO - 'content-remove': [self.__defaultCB, self.__contentRemoveCB], - 'description-info': [self.__broadcastCB, self.__defaultCB], #TODO - 'security-info': [self.__defaultCB], #TODO - 'session-accept': [self.__sessionAcceptCB, self.__contentAcceptCB, - self.__broadcastCB, self.__defaultCB], - 'session-info': [self.__sessionInfoCB, self.__broadcastCB, self.__defaultCB], - 'session-initiate': [self.__sessionInitiateCB, self.__broadcastCB, - self.__defaultCB], - 'session-terminate': [self.__sessionTerminateCB, self.__broadcastAllCB, - self.__defaultCB], - 'transport-info': [self.__broadcastCB, self.__defaultCB], - 'transport-replace': [self.__broadcastCB, self.__transportReplaceCB], #TODO - 'transport-accept': [self.__defaultCB], #TODO - 'transport-reject': [self.__defaultCB], #TODO + 'content-accept': [self.__on_content_accept, self.__broadcast, + self.__ack], + 'content-add': [self.__on_content_add, self.__broadcast, + self.__ack], #TODO + 'content-modify': [self.__ack], #TODO + 'content-reject': [self.__ack, self.__on_content_remove], #TODO + 'content-remove': [self.__ack, self.__on_content_remove], + 'description-info': [self.__broadcast, self.__ack], #TODO + 'security-info': [self.__ack], #TODO + 'session-accept': [self.__on_session_accept, self.__on_content_accept, + self.__broadcast, self.__ack], + 'session-info': [self.__on_session_info, self.__broadcast, self.__ack], + 'session-initiate': [self.__on_session_initiate, self.__broadcast, + self.__ack], + 'session-terminate': [self.__on_session_terminate, self.__broadcast_all, + self.__ack], + 'transport-info': [self.__broadcast, self.__ack], + 'transport-replace': [self.__broadcast, self.__on_transport_replace], #TODO + 'transport-accept': [self.__ack], #TODO + 'transport-reject': [self.__ack], #TODO 'iq-result': [], - 'iq-error': [self.__errorCB], + 'iq-error': [self.__on_error], } def approve_session(self): @@ -262,7 +262,7 @@ class JingleSession(object): jingle.addChild(node=content) self.connection.connection.send(stanza) - def stanzaCB(self, stanza): + def on_stanza(self, stanza): """ A callback for ConnectionJingle. It gets stanza, then tries to send it to all internally registered callbacks. First one to raise @@ -300,14 +300,14 @@ class JingleSession(object): # FIXME self.__send_error(stanza, 'unexpected-request', 'out-of-order') - def __defaultCB(self, stanza, jingle, error, action): + def __ack(self, stanza, jingle, error, action): """ Default callback for action stanzas -- simple ack and stop processing """ response = stanza.buildReply('result') self.connection.connection.send(response) - def __errorCB(self, stanza, jingle, error, action): + def __on_error(self, stanza, jingle, error, action): # FIXME text = error.getTagData('text') jingle_error = None @@ -322,7 +322,7 @@ class JingleSession(object): if xmpp_error == 'item-not-found': self.connection.delete_jingle_session(self.peerjid, self.sid) - def __transportReplaceCB(self, stanza, jingle, error, action): + def __on_transport_replace(self, stanza, jingle, error, action): for content in jingle.iterTags('content'): creator = content['creator'] name = content['name'] @@ -350,14 +350,14 @@ class JingleSession(object): self.connection.connection.send(stanza) raise xmpp.NodeProcessed - def __sessionInfoCB(self, stanza, jingle, error, action): + def __on_session_info(self, stanza, jingle, error, action): # TODO: ringing, active, (un)hold, (un)mute payload = jingle.getPayload() if len(payload) > 0: self.__send_error(stanza, 'feature-not-implemented', 'unsupported-info') raise xmpp.NodeProcessed - def __contentRemoveCB(self, stanza, jingle, error, action): + def __on_content_remove(self, stanza, jingle, error, action): for content in jingle.iterTags('content'): creator = content['creator'] name = content['name'] @@ -372,13 +372,13 @@ class JingleSession(object): reason.setTag('success') self._session_terminate(reason) - def __sessionAcceptCB(self, stanza, jingle, error, action): + def __on_session_accept(self, stanza, jingle, error, action): # FIXME if self.state != JingleStates.pending: raise OutOfOrder self.state = JingleStates.active - def __contentAcceptCB(self, stanza, jingle, error, action): + def __on_content_accept(self, stanza, jingle, error, action): """ Called when we get content-accept stanza or equivalent one (like session-accept) @@ -389,7 +389,7 @@ class JingleSession(object): # TODO name = content['name'] - def __contentAddCB(self, stanza, jingle, error, action): + def __on_content_add(self, stanza, jingle, error, action): if self.state == JingleStates.ended: raise OutOfOrder @@ -407,7 +407,7 @@ class JingleSession(object): self.connection.dispatch('JINGLE_INCOMING', (self.peerjid, self.sid, contents)) - def __sessionInitiateCB(self, stanza, jingle, error, action): + def __on_session_initiate(self, stanza, jingle, error, action): """ We got a jingle session request from other entity, therefore we are the receiver... Unpack the data, inform the user @@ -433,7 +433,7 @@ class JingleSession(object): # TODO: http://xmpp.org/extensions/xep-0166.html#session-terminate reason = xmpp.Node('reason') reason.setTag('unsupported-applications') - self.__defaultCB(stanza, jingle, error, action) + self.__ack(stanza, jingle, error, action) self._session_terminate(reason) raise xmpp.NodeProcessed @@ -441,7 +441,7 @@ class JingleSession(object): # TODO: http://xmpp.org/extensions/xep-0166.html#session-terminate reason = xmpp.Node('reason') reason.setTag('unsupported-transports') - self.__defaultCB(stanza, jingle, error, action) + self.__ack(stanza, jingle, error, action) self._session_terminate(reason) raise xmpp.NodeProcessed @@ -451,7 +451,7 @@ class JingleSession(object): self.connection.dispatch('JINGLE_INCOMING', (self.peerjid, self.sid, contents)) - def __broadcastCB(self, stanza, jingle, error, action): + def __broadcast(self, stanza, jingle, error, action): """ Broadcast the stanza contents to proper content handlers """ @@ -459,9 +459,9 @@ class JingleSession(object): name = content['name'] creator = content['creator'] cn = self.contents[(creator, name)] - cn.stanzaCB(stanza, content, error, action) + cn.on_stanza(stanza, content, error, action) - def __sessionTerminateCB(self, stanza, jingle, error, action): + def __on_session_terminate(self, stanza, jingle, error, action): self.connection.delete_jingle_session(self.peerjid, self.sid) reason, text = self.__reason_from_stanza(jingle) if reason not in ('success', 'cancel', 'decline'): @@ -473,12 +473,12 @@ class JingleSession(object): self.connection.dispatch('JINGLE_DISCONNECTED', (self.peerjid, self.sid, None, text)) - def __broadcastAllCB(self, stanza, jingle, error, action): + def __broadcast_all(self, stanza, jingle, error, action): """ Broadcast the stanza to all content handlers """ for content in self.contents.itervalues(): - content.stanzaCB(stanza, None, error, action) + content.on_stanza(stanza, None, error, action) def __parse_contents(self, jingle): #TODO: Needs some reworking @@ -572,7 +572,7 @@ class JingleSession(object): assert self.state == JingleStates.ended stanza, jingle = self.__make_jingle('session-initiate') self.__append_contents(jingle) - self.__broadcastCB(stanza, jingle, None, 'session-initiate-sent') + self.__broadcast(stanza, jingle, None, 'session-initiate-sent') self.connection.connection.send(stanza) self.state = JingleStates.pending @@ -580,7 +580,7 @@ class JingleSession(object): assert self.state == JingleStates.pending stanza, jingle = self.__make_jingle('session-accept') self.__append_contents(jingle) - self.__broadcastCB(stanza, jingle, None, 'session-accept-sent') + self.__broadcast(stanza, jingle, None, 'session-accept-sent') self.connection.connection.send(stanza) self.state = JingleStates.active @@ -596,7 +596,7 @@ class JingleSession(object): stanza, jingle = self.__make_jingle('session-terminate') if reason is not None: jingle.addChild(node=reason) - self.__broadcastAllCB(stanza, jingle, None, 'session-terminate-sent') + self.__broadcast_all(stanza, jingle, None, 'session-terminate-sent') self.connection.connection.send(stanza) reason, text = self.__reason_from_stanza(jingle) if reason not in ('success', 'cancel', 'decline'): @@ -614,7 +614,7 @@ class JingleSession(object): assert self.state != JingleStates.ended stanza, jingle = self.__make_jingle('content-add') self.__append_content(jingle, content) - self.__broadcastCB(stanza, jingle, None, 'content-add-sent') + self.__broadcast(stanza, jingle, None, 'content-add-sent') self.connection.connection.send(stanza) def __content_accept(self, content): @@ -622,7 +622,7 @@ class JingleSession(object): assert self.state != JingleStates.ended stanza, jingle = self.__make_jingle('content-accept') self.__append_content(jingle, content) - self.__broadcastCB(stanza, jingle, None, 'content-accept-sent') + self.__broadcast(stanza, jingle, None, 'content-accept-sent') self.connection.connection.send(stanza) def __content_reject(self, content):