Mercurial > libervia-backend
comparison frontends/src/quick_frontend/quick_chat.py @ 2019:c0ff84243650
quick_frontend(chat): better handling of cached signals when initialising widget
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 24 Jul 2016 18:02:34 +0200 |
parents | 7199e6bdb94e |
children | f67da1cab6d3 |
comparison
equal
deleted
inserted
replaced
2018:7199e6bdb94e | 2019:c0ff84243650 |
---|---|
153 """ | 153 """ |
154 self.lang = '' # default language to use for messages | 154 self.lang = '' # default language to use for messages |
155 quick_widgets.QuickWidget.__init__(self, host, target, profiles=profiles) | 155 quick_widgets.QuickWidget.__init__(self, host, target, profiles=profiles) |
156 self._locked = True # True when we are waiting for history/search | 156 self._locked = True # True when we are waiting for history/search |
157 # messageNew signals are cached when locked | 157 # messageNew signals are cached when locked |
158 self._cache = [] | 158 self._cache = OrderedDict() |
159 assert type_ in (C.CHAT_ONE2ONE, C.CHAT_GROUP) | 159 assert type_ in (C.CHAT_ONE2ONE, C.CHAT_GROUP) |
160 self.current_target = target | 160 self.current_target = target |
161 self.type = type_ | 161 self.type = type_ |
162 if type_ == C.CHAT_GROUP: | 162 if type_ == C.CHAT_GROUP: |
163 if target.resource: | 163 if target.resource: |
286 @param size (int): number of messages | 286 @param size (int): number of messages |
287 @param filters (str): patterns to filter the history results | 287 @param filters (str): patterns to filter the history results |
288 @param profile (str): %(doc_profile)s | 288 @param profile (str): %(doc_profile)s |
289 """ | 289 """ |
290 self._locked = True | 290 self._locked = True |
291 self._cache = OrderedDict() | |
291 self.messages.clear() | 292 self.messages.clear() |
292 self.historyPrint(size, filters, profile) | 293 self.historyPrint(size, filters, profile) |
293 | 294 |
294 def _onHistoryPrinted(self): | 295 def _onHistoryPrinted(self): |
295 """Method called when history is printed (or failed) | 296 """Method called when history is printed (or failed) |
296 | 297 |
297 unlock the widget, and can be used to refresh or scroll down | 298 unlock the widget, and can be used to refresh or scroll down |
298 the focus after the history is printed | 299 the focus after the history is printed |
299 """ | 300 """ |
300 self._locked = False | 301 self._locked = False |
301 for data in self._cache: | 302 for data in self._cache.itervalues(): |
302 self.messageNew(*data) | 303 self.messageNew(*data) |
304 del self._cache | |
303 | 305 |
304 def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT, filters=None, profile='@NONE@'): | 306 def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT, filters=None, profile='@NONE@'): |
305 """Print the current history | 307 """Print the current history |
306 | 308 |
307 @param size (int): number of messages | 309 @param size (int): number of messages |
328 # if previous_day != message_day: | 330 # if previous_day != message_day: |
329 # self.printDayChange(message_day) | 331 # self.printDayChange(message_day) |
330 # previous_day = message_day | 332 # previous_day = message_day |
331 for data in history: | 333 for data in history: |
332 uid, timestamp, from_jid, to_jid, message, subject, type_, extra = data | 334 uid, timestamp, from_jid, to_jid, message, subject, type_, extra = data |
335 # cached messages may already be in history | |
336 # so we check it to avoid duplicates, they'll be added later | |
337 if uid in self._cache: | |
338 continue | |
333 from_jid = jid.JID(from_jid) | 339 from_jid = jid.JID(from_jid) |
334 to_jid = jid.JID(to_jid) | 340 to_jid = jid.JID(to_jid) |
335 # if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or | 341 # if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or |
336 # (self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)): | 342 # (self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)): |
337 # continue | 343 # continue |
343 self._onHistoryPrinted() | 349 self._onHistoryPrinted() |
344 | 350 |
345 self.host.bridge.historyGet(unicode(self.host.profiles[profile].whoami.bare), unicode(target), size, True, filters, profile, callback=_historyGetCb, errback=_historyGetEb) | 351 self.host.bridge.historyGet(unicode(self.host.profiles[profile].whoami.bare), unicode(target), size, True, filters, profile, callback=_historyGetCb, errback=_historyGetEb) |
346 | 352 |
347 def messageNew(self, uid, timestamp, from_jid, to_jid, msg, subject, type_, extra, profile): | 353 def messageNew(self, uid, timestamp, from_jid, to_jid, msg, subject, type_, extra, profile): |
348 log.debug(u"messageNew ==> {}".format((uid, timestamp, from_jid, to_jid, msg, subject, type_, extra, profile))) | |
349 if self._locked: | 354 if self._locked: |
350 self._cache.append(uid, timestamp, from_jid, to_jid, msg, subject, type_, extra, profile) | 355 self._cache[uid] = (uid, timestamp, from_jid, to_jid, msg, subject, type_, extra, profile) |
351 return | 356 return |
352 if self.type == C.CHAT_GROUP: | 357 if self.type == C.CHAT_GROUP: |
353 if to_jid.resource and type_ != C.MESS_TYPE_GROUPCHAT: | 358 if to_jid.resource and type_ != C.MESS_TYPE_GROUPCHAT: |
354 # we have a private message, we forward it to a private conversation widget | 359 # we have a private message, we forward it to a private conversation widget |
355 chat_widget = self.getOrCreatePrivateWidget(to_jid) | 360 chat_widget = self.getOrCreatePrivateWidget(to_jid) |