fix lazy session binding to reuse sessions and pick them up from pending events

This commit is contained in:
Brendan Taylor 2008-06-02 23:26:40 +00:00
parent d19cb43ee0
commit dc89b61d37
3 changed files with 43 additions and 14 deletions

View File

@ -51,6 +51,8 @@ if dbus_support.supported:
from session import ChatControlSession from session import ChatControlSession
gajim.default_session_type = ChatControlSession
STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd', STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd',
'invisible', 'error'] 'invisible', 'error']
# kind of events we can wait for an answer # kind of events we can wait for an answer
@ -1300,8 +1302,9 @@ sent a message to.'''
# sessions that we haven't received a thread ID in # sessions that we haven't received a thread ID in
idless = filter(lambda s: not s.received_thread_id, sessions) idless = filter(lambda s: not s.received_thread_id, sessions)
# filter out everything exceptthe default session type # filter out everything except the default session type
chat_sessions = filter(lambda s: isinstance(s, ChatControlSession), idless) p = lambda s: isinstance(s, gajim.default_session_type)
chat_sessions = filter(p, idless)
if chat_sessions: if chat_sessions:
# return the session that we last sent a message in # return the session that we last sent a message in
@ -1310,10 +1313,25 @@ sent a message to.'''
else: else:
return None return None
# if deferred is true, the thread ID we're generating is tem def find_controlless_session(self, jid):
'''find an active session that doesn't have a control attached'''
try:
sessions = self.sessions[jid].values()
# filter out everything except the default session type
p = lambda s: isinstance(s, gajim.default_session_type)
chat_sessions = filter(p, sessions)
orphaned = filter(lambda s: not s.control, chat_sessions)
return orphaned[0]
except KeyError:
return None
def make_new_session(self, jid, thread_id=None, type='chat', cls=None): def make_new_session(self, jid, thread_id=None, type='chat', cls=None):
if not cls: if not cls:
cls = ChatControlSession cls = gajim.default_session_type
# determine if this session is a pm session # determine if this session is a pm session
# if not, discard the resource # if not, discard the resource

View File

@ -2898,16 +2898,24 @@ class Interface:
if resource: if resource:
fjid += '/' + resource fjid += '/' + resource
win = self.msg_win_mgr.get_window(fjid, account) ctrl = None
if win: if session:
ctrl = win.get_controls(fjid, account) ctrl = session.control
else: else:
ctrl = self.new_chat(contact, account, resource = resource)
win = self.msg_win_mgr.get_window(fjid, account) win = self.msg_win_mgr.get_window(fjid, account)
if win:
ctrl = win.get_controls(fjid, account)[0]
if not ctrl:
ctrl = self.new_chat(contact, account,
resource = resource, session = session)
# last message is long time ago # last message is long time ago
gajim.last_message_time[account][ctrl.get_full_jid()] = 0 gajim.last_message_time[account][ctrl.get_full_jid()] = 0
win = ctrl.parent_win
win.set_active_tab(ctrl) win.set_active_tab(ctrl)
if gajim.connections[account].is_zeroconf and \ if gajim.connections[account].is_zeroconf and \

View File

@ -153,9 +153,14 @@ class MessageControl:
# Doesn't return None if error # Doesn't return None if error
jid = self.contact.jid jid = self.contact.jid
original_message = message original_message = message
conn = gajim.connections[self.account]
if not self.session: if not self.session:
sess = gajim.connections[self.account].make_new_session(jid) sess = conn.find_controlless_session(jid)
if not sess:
sess = conn.make_new_session(jid)
self.set_session(sess) self.set_session(sess)
xep_200 = bool(self.session) and self.session.enable_encryption xep_200 = bool(self.session) and self.session.enable_encryption
@ -199,9 +204,7 @@ class MessageControl:
return return
# Send and update history # Send and update history
return gajim.connections[self.account].send_message(jid, return conn.send_message(jid, message, keyID, type = type,
message, keyID, type = type, chatstate = chatstate, chatstate = chatstate, msg_id = msg_id, composing_xep = composing_xep,
msg_id = msg_id, composing_xep = composing_xep,
resource = self.resource, user_nick = user_nick, resource = self.resource, user_nick = user_nick,
session = self.session, session = self.session, original_message = original_message)
original_message = original_message)