refactoring
This commit is contained in:
		
							parent
							
								
									004c0fbdf5
								
							
						
					
					
						commit
						b1d206d2ec
					
				
					 4 changed files with 34 additions and 34 deletions
				
			
		| 
						 | 
					@ -163,7 +163,7 @@ class ConnectionJingle(object):
 | 
				
			||||||
            transport = JingleTransportIBB()
 | 
					            transport = JingleTransportIBB()
 | 
				
			||||||
        c = JingleFileTransfer(jingle, transport=transport,
 | 
					        c = JingleFileTransfer(jingle, transport=transport,
 | 
				
			||||||
            file_props=file_props, use_security=use_security)
 | 
					            file_props=file_props, use_security=use_security)
 | 
				
			||||||
        jingle.hash_algo = self.__hash_support(contact) 
 | 
					        file_props.algo = self.__hash_support(contact) 
 | 
				
			||||||
        jingle.add_content('file' + helpers.get_random_string_16(), c)
 | 
					        jingle.add_content('file' + helpers.get_random_string_16(), c)
 | 
				
			||||||
        jingle.start_session()
 | 
					        jingle.start_session()
 | 
				
			||||||
        return c.transport.sid
 | 
					        return c.transport.sid
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -117,12 +117,9 @@ class JingleFileTransfer(JingleContent):
 | 
				
			||||||
            conn=self.session.connection, stanza=stanza, jingle_content=content,
 | 
					            conn=self.session.connection, stanza=stanza, jingle_content=content,
 | 
				
			||||||
            FT_content=self))
 | 
					            FT_content=self))
 | 
				
			||||||
        self._listen_host()
 | 
					        self._listen_host()
 | 
				
			||||||
        # Delete this after file_props refactoring this shouldn't be necesary
 | 
					 | 
				
			||||||
        self.session.file_hash = self.file_props.hash_
 | 
					 | 
				
			||||||
        self.session.hash_algo = self.file_props.algo
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __on_session_initiate_sent(self, stanza, content, error, action):
 | 
					    def __on_session_initiate_sent(self, stanza, content, error, action):
 | 
				
			||||||
        # Calculate file_hash in a new thread
 | 
					        # Calculate file hash in a new thread
 | 
				
			||||||
        # if we haven't sent the hash already.
 | 
					        # if we haven't sent the hash already.
 | 
				
			||||||
        if self.file_props.hash_ is None:
 | 
					        if self.file_props.hash_ is None:
 | 
				
			||||||
            self.hashThread = threading.Thread(target=self.__send_hash)
 | 
					            self.hashThread = threading.Thread(target=self.__send_hash)
 | 
				
			||||||
| 
						 | 
					@ -137,7 +134,7 @@ class JingleFileTransfer(JingleContent):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _calcHash(self):
 | 
					    def _calcHash(self):
 | 
				
			||||||
        # Caculates the hash and returns a xep-300 hash stanza
 | 
					        # Caculates the hash and returns a xep-300 hash stanza
 | 
				
			||||||
        if self.session.hash_algo == None:
 | 
					        if self.file_props.algo == None:
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            file_ = open(self.file_props.file_name, 'r')
 | 
					            file_ = open(self.file_props.file_name, 'r')
 | 
				
			||||||
| 
						 | 
					@ -145,14 +142,14 @@ class JingleFileTransfer(JingleContent):
 | 
				
			||||||
            # can't open file
 | 
					            # can't open file
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        h = xmpp.Hashes()
 | 
					        h = xmpp.Hashes()
 | 
				
			||||||
        hash_ = h.calculateHash(self.session.hash_algo, file_)
 | 
					        hash_ = h.calculateHash(self.file_props.algo, file_)
 | 
				
			||||||
        # DEBUG
 | 
					        # DEBUG
 | 
				
			||||||
        #hash_ = '1294809248109223'
 | 
					        #hash_ = '1294809248109223'
 | 
				
			||||||
        if not hash_:
 | 
					        if not hash_:
 | 
				
			||||||
            # Hash alogrithm not supported
 | 
					            # Hash alogrithm not supported
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        self.file_props.hash_ = hash_
 | 
					        self.file_props.hash_ = hash_
 | 
				
			||||||
        h.addHash(hash_, self.session.hash_algo)
 | 
					        h.addHash(hash_, self.file_props.algo)
 | 
				
			||||||
        return h
 | 
					        return h
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __on_session_accept(self, stanza, content, error, action):
 | 
					    def __on_session_accept(self, stanza, content, error, action):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -98,34 +98,33 @@ class JingleSession(object):
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            self.iq_ids = []
 | 
					            self.iq_ids = []
 | 
				
			||||||
        self.accepted = True # is this session accepted by user
 | 
					        self.accepted = True # is this session accepted by user
 | 
				
			||||||
        # Hash algorithm that we are using to calculate the integrity of the 
 | 
					 | 
				
			||||||
        # file. Could be 'md5', 'sha-1', etc...
 | 
					 | 
				
			||||||
        self.hash_algo = None
 | 
					 | 
				
			||||||
        self.file_hash = None
 | 
					 | 
				
			||||||
        # Tells whether this session is a file transfer or not
 | 
					        # Tells whether this session is a file transfer or not
 | 
				
			||||||
        self.session_type_FT = False
 | 
					        self.session_type_FT = False
 | 
				
			||||||
        # callbacks to call on proper contents
 | 
					        # callbacks to call on proper contents
 | 
				
			||||||
        # use .prepend() to add new callbacks, especially when you're going
 | 
					        # use .prepend() to add new callbacks, especially when you're going
 | 
				
			||||||
        # to send error instead of ack
 | 
					        # to send error instead of ack
 | 
				
			||||||
        self.callbacks = {
 | 
					        self.callbacks = {
 | 
				
			||||||
                'content-accept':       [self.__on_content_accept, self.__broadcast,
 | 
					                'content-accept':       [self.__on_content_accept, 
 | 
				
			||||||
                        self.__ack],
 | 
					                                         self.__broadcast, self.__ack],
 | 
				
			||||||
                'content-add':          [self.__on_content_add, self.__broadcast,
 | 
					                'content-add':          [self.__on_content_add, self.__broadcast,
 | 
				
			||||||
                        self.__ack], #TODO
 | 
					                                         self.__ack], #TODO
 | 
				
			||||||
                'content-modify':       [self.__ack], #TODO
 | 
					                'content-modify':       [self.__ack], #TODO
 | 
				
			||||||
                'content-reject':       [self.__ack, self.__on_content_remove], #TODO
 | 
					                'content-reject':       [self.__ack, self.__on_content_remove],
 | 
				
			||||||
                'content-remove':       [self.__ack, self.__on_content_remove],
 | 
					                'content-remove':       [self.__ack, self.__on_content_remove],
 | 
				
			||||||
                'description-info':     [self.__broadcast, self.__ack], #TODO
 | 
					                'description-info':     [self.__broadcast, self.__ack], #TODO
 | 
				
			||||||
                'security-info':        [self.__ack], #TODO
 | 
					                'security-info':        [self.__ack], #TODO
 | 
				
			||||||
                'session-accept':       [self.__on_session_accept, self.__on_content_accept,
 | 
					                'session-accept':       [self.__on_session_accept,
 | 
				
			||||||
                        self.__broadcast],
 | 
					                                         self.__on_content_accept,
 | 
				
			||||||
                'session-info':         [self.__broadcast, self.__on_session_info, self.__ack],
 | 
					                                         self.__broadcast],
 | 
				
			||||||
                'session-initiate':     [self.__on_session_initiate, self.__broadcast,
 | 
					                'session-info':         [self.__broadcast, 
 | 
				
			||||||
                        self.__ack],
 | 
					                                         self.__on_session_info, self.__ack],
 | 
				
			||||||
                'session-terminate':    [self.__on_session_terminate, self.__broadcast_all,
 | 
					                'session-initiate':     [self.__on_session_initiate, 
 | 
				
			||||||
                        self.__ack],
 | 
					                                         self.__broadcast, self.__ack],
 | 
				
			||||||
 | 
					                'session-terminate':    [self.__on_session_terminate, 
 | 
				
			||||||
 | 
					                                         self.__broadcast_all, self.__ack],
 | 
				
			||||||
                'transport-info':       [self.__broadcast, self.__ack],
 | 
					                'transport-info':       [self.__broadcast, self.__ack],
 | 
				
			||||||
                'transport-replace':    [self.__broadcast, self.__on_transport_replace], #TODO
 | 
					                'transport-replace':    [self.__broadcast, 
 | 
				
			||||||
 | 
					                                         self.__on_transport_replace], #TODO
 | 
				
			||||||
                'transport-accept':     [self.__ack], #TODO
 | 
					                'transport-accept':     [self.__ack], #TODO
 | 
				
			||||||
                'transport-reject':     [self.__ack], #TODO
 | 
					                'transport-reject':     [self.__ack], #TODO
 | 
				
			||||||
                'iq-result':            [self.__broadcast],
 | 
					                'iq-result':            [self.__broadcast],
 | 
				
			||||||
| 
						 | 
					@ -327,7 +326,8 @@ class JingleSession(object):
 | 
				
			||||||
                self.__send_error(stanza, 'bad-request')
 | 
					                self.__send_error(stanza, 'bad-request')
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
            # FIXME: If we aren't initiated and it's not a session-initiate...
 | 
					            # FIXME: If we aren't initiated and it's not a session-initiate...
 | 
				
			||||||
            if action not in ['session-initiate','session-terminate'] and self.state == JingleStates.ended:
 | 
					            if action not in ['session-initiate','session-terminate'] \
 | 
				
			||||||
 | 
					                    and self.state == JingleStates.ended:
 | 
				
			||||||
                self.__send_error(stanza, 'item-not-found', 'unknown-session')
 | 
					                self.__send_error(stanza, 'item-not-found', 'unknown-session')
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
| 
						 | 
					@ -364,6 +364,7 @@ class JingleSession(object):
 | 
				
			||||||
            elif child.getNamespace() == xmpp.NS_STANZAS:
 | 
					            elif child.getNamespace() == xmpp.NS_STANZAS:
 | 
				
			||||||
                error_name = child.getName()
 | 
					                error_name = child.getName()
 | 
				
			||||||
        self.__dispatch_error(error_name, text, error.getAttr('type'))
 | 
					        self.__dispatch_error(error_name, text, error.getAttr('type'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # FIXME: Not sure when we would want to do that...
 | 
					        # FIXME: Not sure when we would want to do that...
 | 
				
			||||||
    def transport_replace(self):
 | 
					    def transport_replace(self):
 | 
				
			||||||
        transport = JingleTransportIBB()
 | 
					        transport = JingleTransportIBB()
 | 
				
			||||||
| 
						 | 
					@ -416,6 +417,8 @@ class JingleSession(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __on_session_info(self, stanza, jingle, error, action):
 | 
					    def __on_session_info(self, stanza, jingle, error, action):
 | 
				
			||||||
        # TODO: ringing, active, (un)hold, (un)mute
 | 
					        # TODO: ringing, active, (un)hold, (un)mute
 | 
				
			||||||
 | 
					        if self.state != JingleStates.active:
 | 
				
			||||||
 | 
					            raise OutOfOrder
 | 
				
			||||||
        payload = jingle.getPayload()
 | 
					        payload = jingle.getPayload()
 | 
				
			||||||
        for p in payload:
 | 
					        for p in payload:
 | 
				
			||||||
            if p.getName() == 'checksum':
 | 
					            if p.getName() == 'checksum':
 | 
				
			||||||
| 
						 | 
					@ -423,13 +426,13 @@ class JingleSession(object):
 | 
				
			||||||
                                        namespace=xmpp.NS_HASHES)
 | 
					                                        namespace=xmpp.NS_HASHES)
 | 
				
			||||||
                algo = hash_.getAttr('algo')
 | 
					                algo = hash_.getAttr('algo')
 | 
				
			||||||
                if algo in xmpp.Hashes.supported:
 | 
					                if algo in xmpp.Hashes.supported:
 | 
				
			||||||
                    self.hash_algo = algo
 | 
					                    file_props = FilesProp.getFileProp(self.connection.name,
 | 
				
			||||||
                    data = hash_.getData()
 | 
					                                                       self.sid)
 | 
				
			||||||
                    # This only works because there is only one session
 | 
					                    file_props.algo = algo
 | 
				
			||||||
                    # per file in jingleFT
 | 
					                    file_props.hash_ = hash_.getData()
 | 
				
			||||||
                    self.file_hash = data
 | 
					 | 
				
			||||||
                    raise xmpp.NodeProcessed
 | 
					                    raise xmpp.NodeProcessed
 | 
				
			||||||
        self.__send_error(stanza, 'feature-not-implemented', 'unsupported-info', type_='modify')
 | 
					        self.__send_error(stanza, 'feature-not-implemented', 'unsupported-info', 
 | 
				
			||||||
 | 
					                          type_='modify')
 | 
				
			||||||
        raise xmpp.NodeProcessed
 | 
					        raise xmpp.NodeProcessed
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __on_content_remove(self, stanza, jingle, error, action):
 | 
					    def __on_content_remove(self, stanza, jingle, error, action):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -938,7 +938,7 @@ class Interface:
 | 
				
			||||||
        session = gajim.connections[account].get_jingle_session(jid=None,
 | 
					        session = gajim.connections[account].get_jingle_session(jid=None,
 | 
				
			||||||
            sid=file_props.session_sid)
 | 
					            sid=file_props.session_sid)
 | 
				
			||||||
        ft_win = self.instances['file_transfers']
 | 
					        ft_win = self.instances['file_transfers']
 | 
				
			||||||
        if not session.file_hash:
 | 
					        if not file_props.hash_:
 | 
				
			||||||
            # We disn't get the hash, sender probably don't support that
 | 
					            # We disn't get the hash, sender probably don't support that
 | 
				
			||||||
            jid = unicode(file_props.sender)
 | 
					            jid = unicode(file_props.sender)
 | 
				
			||||||
            self.popup_ft_result(account, jid, file_props)
 | 
					            self.popup_ft_result(account, jid, file_props)
 | 
				
			||||||
| 
						 | 
					@ -948,12 +948,12 @@ class Interface:
 | 
				
			||||||
            file_ = open(file_props.file_name, 'r')
 | 
					            file_ = open(file_props.file_name, 'r')
 | 
				
			||||||
        except:
 | 
					        except:
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        hash_ = h.calculateHash(session.hash_algo, file_)
 | 
					        hash_ = h.calculateHash(file_props.algo, file_)
 | 
				
			||||||
        file_.close()
 | 
					        file_.close()
 | 
				
			||||||
        # If the hash we received and the hash of the file are the same,
 | 
					        # If the hash we received and the hash of the file are the same,
 | 
				
			||||||
        # then the file is not corrupt
 | 
					        # then the file is not corrupt
 | 
				
			||||||
        jid = unicode(file_props.sender)
 | 
					        jid = unicode(file_props.sender)
 | 
				
			||||||
        if session.file_hash == hash_:
 | 
					        if file_props.hash_ == hash_:
 | 
				
			||||||
            gobject.idle_add(self.popup_ft_result, account, jid, file_props)
 | 
					            gobject.idle_add(self.popup_ft_result, account, jid, file_props)
 | 
				
			||||||
            gobject.idle_add(ft_win.set_status, file_props, 'ok')
 | 
					            gobject.idle_add(ft_win.set_status, file_props, 'ok')
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue