connection.pt doesn't send events to ui: it just put it in gajim.events_for_ui[account]

gui read this queue
(mutex will come)
This commit is contained in:
Yann Leboulanger 2005-08-09 17:21:35 +00:00
parent 016d70d7da
commit d0eb029f74
4 changed files with 59 additions and 81 deletions

View File

@ -117,19 +117,6 @@ def get_os_info():
class Connection: class Connection:
"""Connection class""" """Connection class"""
def __init__(self, name): def __init__(self, name):
# dict of function to be called for each event
self.handlers = {'ROSTER': [], 'WARNING': [], 'ERROR': [],
'INFORMATION': [], 'STATUS': [], 'NOTIFY': [], 'MSG': [],
'MSGERROR': [], 'MSGSENT': [] , 'SUBSCRIBED': [], 'UNSUBSCRIBED': [],
'SUBSCRIBE': [], 'AGENT_INFO': [], 'REGISTER_AGENT_INFO': [],
'AGENT_INFO_ITEMS': [], 'AGENT_INFO_INFO': [], 'QUIT': [],
'ACC_OK': [], 'MYVCARD': [], 'OS_INFO': [], 'VCARD': [], 'GC_MSG': [],
'GC_SUBJECT': [], 'GC_CONFIG': [], 'BAD_PASSPHRASE': [],
'ROSTER_INFO': [], 'ERROR_ANSWER': [], 'BOOKMARKS': [], 'CON_TYPE': [],
'FILE_REQUEST': [], 'FILE_RCV_COMPLETED': [], 'FILE_PROGRESS': [],
'FILE_REQUEST_ERROR': [], 'FILE_SEND_ERROR': [],
'STANZA_ARRIVED': [], 'STANZA_SENT': [], 'HTTP_AUTH': []
}
self.name = name self.name = name
self.connected = 0 # offline self.connected = 0 # offline
self.connection = None # xmpppy instance self.connection = None # xmpppy instance
@ -142,7 +129,6 @@ class Connection:
self.last_incoming = time.time() self.last_incoming = time.time()
self.keep_alive_sent = False self.keep_alive_sent = False
self.to_be_sent = [] self.to_be_sent = []
self.for_gui = []
self.last_sent = [] self.last_sent = []
self.files_props = {} self.files_props = {}
self.password = gajim.config.get_per('accounts', name, 'password') self.password = gajim.config.get_per('accounts', name, 'password')
@ -156,11 +142,7 @@ class Connection:
def dispatch(self, event, data): def dispatch(self, event, data):
'''always passes account name as first param''' '''always passes account name as first param'''
if not event in self.handlers: gajim.events_for_ui[self.name].append([event, data])
print event, 'is not in:', self.handlers
return
for handler in self.handlers[event]:
handler(self.name, data)
def add_sha(self, p): def add_sha(self, p):
c = p.setTag('x', namespace = common.xmpp.NS_VCARD_UPDATE) c = p.setTag('x', namespace = common.xmpp.NS_VCARD_UPDATE)
@ -1204,7 +1186,8 @@ class Connection:
gajim.log.debug(_('Connected to server with %s'), con_type) gajim.log.debug(_('Connected to server with %s'), con_type)
self.for_gui.append(['CON_TYPE', con_type]) # notify the gui about con_type # notify the gui about con_type
self.dispatch('CON_TYPE', con_type)
con.RegisterHandler('message', self._messageCB) con.RegisterHandler('message', self._messageCB)
con.RegisterHandler('presence', self._presenceCB) con.RegisterHandler('presence', self._presenceCB)
@ -1273,15 +1256,6 @@ class Connection:
return None return None
# END connect # END connect
def register_handler(self, event, function):
if event in self.handlers:
self.handlers[event].append(function)
def unregister_handler(self, event, function):
if event in self.handlers:
if function in self.handlers[event]:
self.handlers[event].remove(function)
def quit(self, kill_core): def quit(self, kill_core):
if kill_core: if kill_core:
if self.connected > 1: if self.connected > 1:
@ -1335,7 +1309,7 @@ class Connection:
if signed: if signed:
p.setTag(common.xmpp.NS_SIGNED + ' x').setData(signed) p.setTag(common.xmpp.NS_SIGNED + ' x').setData(signed)
self.connection.send(p) self.connection.send(p)
self.for_gui.append(['STATUS', 'invisible']) self.dispatch('STATUS', 'invisible')
if initial: if initial:
#ask our VCard #ask our VCard
self.request_vcard(None) self.request_vcard(None)
@ -1364,14 +1338,14 @@ class Connection:
if keyID and USE_GPG: if keyID and USE_GPG:
if self.connected < 2 and self.gpg.passphrase is None: if self.connected < 2 and self.gpg.passphrase is None:
# We didn't set a passphrase # We didn't set a passphrase
self.for_gui.append(['ERROR', (_('OpenPGP Key was not given'), self.dispatch('ERROR', (_('OpenPGP Key was not given'),
_('You will be connected to %s without OpenPGP.') % self.name)]) _('You will be connected to %s without OpenPGP.') % self.name))
else: else:
signed = self.gpg.sign(msg, keyID) signed = self.gpg.sign(msg, keyID)
if signed == 'BAD_PASSPHRASE': if signed == 'BAD_PASSPHRASE':
signed = '' signed = ''
if self.connected < 2: if self.connected < 2:
self.for_gui.append(['BAD_PASSPHRASE', ()]) self.dispatch('BAD_PASSPHRASE', ())
self.status = msg self.status = msg
if show != 'offline' and not self.connected: if show != 'offline' and not self.connected:
self.connection = self.connect() self.connection = self.connect()
@ -1391,7 +1365,7 @@ class Connection:
if self.connection: if self.connection:
self.connection.send(p) self.connection.send(p)
self.for_gui.append(['STATUS', show]) self.dispatch('STATUS', show)
#ask our VCard #ask our VCard
self.request_vcard(None) self.request_vcard(None)
@ -1412,7 +1386,7 @@ class Connection:
self.connection.disconnect() self.connection.disconnect()
except: except:
pass pass
self.for_gui.append(['STATUS', 'offline']) self.dispatch('STATUS', 'offline')
self.connection = None self.connection = None
elif show != 'offline' and self.connected: elif show != 'offline' and self.connected:
was_invisible = self.connected == STATUS_LIST.index('invisible') was_invisible = self.connected == STATUS_LIST.index('invisible')
@ -1433,7 +1407,7 @@ class Connection:
p.setTag(common.xmpp.NS_SIGNED + ' x').setData(signed) p.setTag(common.xmpp.NS_SIGNED + ' x').setData(signed)
if self.connection: if self.connection:
self.connection.send(p) self.connection.send(p)
self.for_gui.append(['STATUS', show]) self.dispatch('STATUS', show)
def send_motd(self, jid, subject = '', msg = ''): def send_motd(self, jid, subject = '', msg = ''):
if not self.connection: if not self.connection:
@ -1861,10 +1835,6 @@ class Connection:
self.connection.send(tosend) self.connection.send(tosend)
self.last_sent.append(time.time()) self.last_sent.append(time.time())
while time.time() < t_limit and len(self.for_gui):
print len(self.for_gui)
tosend = self.for_gui.pop(0)
self.dispatch(tosend[0], tosend[1])
try: try:
if gajim.config.get_per('accounts', self.name, if gajim.config.get_per('accounts', self.name,
'keep_alives_enabled'): # do we want keepalives? 'keep_alives_enabled'): # do we want keepalives?

View File

@ -63,6 +63,8 @@ sleeper_state = {} # whether we pass auto away / xa or not
#'autoaway': autoaway and use sleeper #'autoaway': autoaway and use sleeper
#'autoxa': autoxa and use sleeper #'autoxa': autoxa and use sleeper
status_before_autoaway = {} status_before_autoaway = {}
#queues of events from connections
events_for_ui = {}
socks5quue = None socks5quue = None

View File

@ -1286,6 +1286,7 @@ _('To change the account name, you must be disconnected.')).get_response()
gajim.last_message_time[self.account] gajim.last_message_time[self.account]
gajim.status_before_autoaway[name] = \ gajim.status_before_autoaway[name] = \
gajim.status_before_autoaway[self.account] gajim.status_before_autoaway[self.account]
gajim.events_for_ui[name] = gajim.events_for_ui[self.account]
#upgrade account variable in opened windows #upgrade account variable in opened windows
for kind in ['infos', 'chats', 'gc', 'gc_config']: for kind in ['infos', 'chats', 'gc', 'gc_config']:
@ -1312,6 +1313,7 @@ _('To change the account name, you must be disconnected.')).get_response()
del gajim.encrypted_chats[self.account] del gajim.encrypted_chats[self.account]
del gajim.last_message_time[self.account] del gajim.last_message_time[self.account]
del gajim.status_before_autoaway[self.account] del gajim.status_before_autoaway[self.account]
del gajim.events_for_ui[self.account]
gajim.connections[self.account].name = name gajim.connections[self.account].name = name
gajim.connections[name] = gajim.connections[self.account] gajim.connections[name] = gajim.connections[self.account]
del gajim.connections[self.account] del gajim.connections[self.account]
@ -1366,6 +1368,7 @@ _('To change the account name, you must be disconnected.')).get_response()
gajim.encrypted_chats[name] = [] gajim.encrypted_chats[name] = []
gajim.last_message_time[name] = {} gajim.last_message_time[name] = {}
gajim.status_before_autoaway[name] = '' gajim.status_before_autoaway[name] = ''
gajim.events_for_ui[name] = []
#refresh accounts window #refresh accounts window
if self.plugin.windows.has_key('accounts'): if self.plugin.windows.has_key('accounts'):
self.plugin.windows['accounts'].init_accounts() self.plugin.windows['accounts'].init_accounts()
@ -2465,6 +2468,7 @@ class RemoveAccountWindow:
del gajim.encrypted_chats[self.account] del gajim.encrypted_chats[self.account]
del gajim.last_message_time[self.account] del gajim.last_message_time[self.account]
del gajim.status_before_autoaway[self.account] del gajim.status_before_autoaway[self.account]
del gajim.events_for_ui[self.account]
self.plugin.roster.draw_roster() self.plugin.roster.draw_roster()
if self.plugin.windows.has_key('accounts'): if self.plugin.windows.has_key('accounts'):
self.plugin.windows['accounts'].init_accounts() self.plugin.windows['accounts'].init_accounts()

View File

@ -610,7 +610,8 @@ class Interface:
gajim.sleeper_state[name] = 'off' gajim.sleeper_state[name] = 'off'
gajim.encrypted_chats[name] = [] gajim.encrypted_chats[name] = []
gajim.last_message_time[name] = {} gajim.last_message_time[name] = {}
gajim.status_before_autoaway[name] = {} gajim.status_before_autoaway[name] = ''
gajim.events_for_ui[name] = []
gajim.connections[name].change_status('offline', None, True) gajim.connections[name].change_status('offline', None, True)
gajim.connections[name].connected = 0 gajim.connections[name].connected = 0
if self.windows.has_key('accounts'): if self.windows.has_key('accounts'):
@ -960,46 +961,43 @@ class Interface:
self.make_regexps() self.make_regexps()
def register_handlers(self, con): def register_handlers(self, con):
con.register_handler('ROSTER', self.handle_event_roster) self.handlers = {
con.register_handler('WARNING', self.handle_event_warning) 'ROSTER': self.handle_event_roster,
con.register_handler('ERROR', self.handle_event_error) 'WARNING': self.handle_event_warning,
con.register_handler('INFORMATION', self.handle_event_information) 'ERROR': self.handle_event_error,
con.register_handler('ERROR_ANSWER', self.handle_event_error_answer) 'INFORMATION': self.handle_event_information,
con.register_handler('STATUS', self.handle_event_status) 'ERROR_ANSWER': self.handle_event_error_answer,
con.register_handler('NOTIFY', self.handle_event_notify) 'STATUS': self.handle_event_status,
con.register_handler('MSG', self.handle_event_msg) 'NOTIFY': self.handle_event_notify,
con.register_handler('MSGERROR', self.handle_event_msgerror) 'MSG': self.handle_event_msg,
con.register_handler('MSGSENT', self.handle_event_msgsent) 'MSGERROR': self.handle_event_msgerror,
con.register_handler('SUBSCRIBED', self.handle_event_subscribed) 'MSGSENT': self.handle_event_msgsent,
con.register_handler('UNSUBSCRIBED', self.handle_event_unsubscribed) 'SUBSCRIBED': self.handle_event_subscribed,
con.register_handler('SUBSCRIBE', self.handle_event_subscribe) 'UNSUBSCRIBED': self.handle_event_unsubscribed,
con.register_handler('AGENT_INFO', self.handle_event_agent_info) 'SUBSCRIBE': self.handle_event_subscribe,
con.register_handler('REGISTER_AGENT_INFO', 'AGENT_INFO': self.handle_event_agent_info,
self.handle_event_register_agent_info) 'REGISTER_AGENT_INFO': self.handle_event_register_agent_info,
con.register_handler('AGENT_INFO_ITEMS', 'AGENT_INFO_ITEMS': self.handle_event_agent_info_items,
self.handle_event_agent_info_items) 'AGENT_INFO_INFO': self.handle_event_agent_info_info,
con.register_handler('AGENT_INFO_INFO', 'QUIT': self.handle_event_quit,
self.handle_event_agent_info_info) 'ACC_OK': self.handle_event_acc_ok,
con.register_handler('QUIT', self.handle_event_quit) 'MYVCARD': self.handle_event_myvcard,
con.register_handler('ACC_OK', self.handle_event_acc_ok) 'VCARD': self.handle_event_vcard,
con.register_handler('MYVCARD', self.handle_event_myvcard) 'OS_INFO': self.handle_event_os_info,
con.register_handler('VCARD', self.handle_event_vcard) 'GC_MSG': self.handle_event_gc_msg,
con.register_handler('OS_INFO', self.handle_event_os_info) 'GC_SUBJECT': self.handle_event_gc_subject,
con.register_handler('GC_MSG', self.handle_event_gc_msg) 'GC_CONFIG': self.handle_event_gc_config,
con.register_handler('GC_SUBJECT', self.handle_event_gc_subject) 'BAD_PASSPHRASE': self.handle_event_bad_passphrase,
con.register_handler('GC_CONFIG', self.handle_event_gc_config) 'ROSTER_INFO': self.handle_event_roster_info,
con.register_handler('BAD_PASSPHRASE', self.handle_event_bad_passphrase) 'BOOKMARKS': self.handle_event_bookmarks,
con.register_handler('ROSTER_INFO', self.handle_event_roster_info) 'CON_TYPE': self.handle_event_con_type,
con.register_handler('BOOKMARKS', self.handle_event_bookmarks) 'FILE_REQUEST': self.handle_event_file_request,
con.register_handler('CON_TYPE', self.handle_event_con_type) 'FILE_REQUEST_ERROR': self.handle_event_file_request_error,
con.register_handler('FILE_REQUEST', self.handle_event_file_request) 'FILE_SEND_ERROR': self.handle_event_file_send_error,
con.register_handler('FILE_REQUEST_ERROR', 'STANZA_ARRIVED': self.handle_event_stanza_arrived,
self.handle_event_file_request_error) 'STANZA_SENT': self.handle_event_stanza_sent,
con.register_handler('FILE_SEND_ERROR', 'HTTP_AUTH': self.handle_event_http_auth,
self.handle_event_file_send_error) }
con.register_handler('STANZA_ARRIVED', self.handle_event_stanza_arrived)
con.register_handler('STANZA_SENT', self.handle_event_stanza_sent)
con.register_handler('HTTP_AUTH', self.handle_event_http_auth)
def process_connections(self): def process_connections(self):
try: try:
@ -1013,6 +1011,9 @@ class Interface:
gajim.connections[account].process(0.01) gajim.connections[account].process(0.01)
if gajim.socks5queue.connected: if gajim.socks5queue.connected:
gajim.socks5queue.process(0.01) gajim.socks5queue.process(0.01)
while len(gajim.events_for_ui[account]):
ev = gajim.events_for_ui[account].pop(0)
self.handlers[ev[0]](account, ev[1])
time.sleep(0.01) # so threads in connection.py have time to run time.sleep(0.01) # so threads in connection.py have time to run
return True # renew timeout (loop for ever) return True # renew timeout (loop for ever)
except KeyboardInterrupt: except KeyboardInterrupt:
@ -1117,6 +1118,7 @@ class Interface:
gajim.encrypted_chats[a] = [] gajim.encrypted_chats[a] = []
gajim.last_message_time[a] = {} gajim.last_message_time[a] = {}
gajim.status_before_autoaway[a] = '' gajim.status_before_autoaway[a] = ''
gajim.events_for_ui[a] = []
self.roster = roster_window.RosterWindow(self) self.roster = roster_window.RosterWindow(self)
if gajim.config.get('use_dbus'): if gajim.config.get('use_dbus'):