comparison sat_frontends/quick_frontend/quick_chat.py @ 2849:c2858e63cd82

quick frontend(chat): added setLocked/setUnlocked methods: those methods replace manual change of self._locked and manual creation/handling of caching.
author Goffi <goffi@goffi.org>
date Sun, 10 Mar 2019 18:02:58 +0100
parents c055a3a4ecb0
children 272dc905ff20
comparison
equal deleted inserted replaced
2848:b9da74c9d46e 2849:c2858e63cd82
274 @param type_: can be C.CHAT_ONE2ONE for single conversation or C.CHAT_GROUP for 274 @param type_: can be C.CHAT_ONE2ONE for single conversation or C.CHAT_GROUP for
275 chat à la IRC 275 chat à la IRC
276 """ 276 """
277 self.lang = "" # default language to use for messages 277 self.lang = "" # default language to use for messages
278 quick_widgets.QuickWidget.__init__(self, host, target, profiles=profiles) 278 quick_widgets.QuickWidget.__init__(self, host, target, profiles=profiles)
279 self._locked = True # True when we are waiting for history/search
280 # messageNew signals are cached when locked
281 self._cache = OrderedDict()
282 assert type_ in (C.CHAT_ONE2ONE, C.CHAT_GROUP) 279 assert type_ in (C.CHAT_ONE2ONE, C.CHAT_GROUP)
283 self.current_target = target 280 self.current_target = target
284 self.type = type_ 281 self.type = type_
285 self.encrypted = False # True if this session is currently encrypted 282 self.encrypted = False # True if this session is currently encrypted
283 self._locked = False
284 self.setLocked()
286 if type_ == C.CHAT_GROUP: 285 if type_ == C.CHAT_GROUP:
287 if target.resource: 286 if target.resource:
288 raise exceptions.InternalError( 287 raise exceptions.InternalError(
289 u"a group chat entity can't have a resource" 288 u"a group chat entity can't have a resource"
290 ) 289 )
315 lt.tm_isdst, 314 lt.tm_isdst,
316 ) # struct_time of day changing time 315 ) # struct_time of day changing time
317 if self.host.AVATARS_HANDLER: 316 if self.host.AVATARS_HANDLER:
318 self.host.addListener("avatar", self.onAvatar, profiles) 317 self.host.addListener("avatar", self.onAvatar, profiles)
319 318
319 def setLocked(self):
320 """Set locked flag
321
322 To be set when we are waiting for history/search
323 """
324 # FIXME: we don't use getter/setter here because of pyjamas
325 # TODO: use proper getter/setter once we get rid of pyjamas
326 if self._locked:
327 log.warning(u"{wid} is already locked!".format(wid=self))
328 return
329 self._locked = True
330 # messageNew signals are cached when locked
331 self._cache = OrderedDict()
332 log.debug(u"{wid} is now locked".format(wid=self))
333
334 def setUnlocked(self):
335 if not self._locked:
336 log.debug(u"{wid} was already unlocked".format(wid=self))
337 return
338 self._locked = False
339 for uid, data in self._cache.iteritems():
340 if uid not in self.messages:
341 self.messageNew(*data)
342 else:
343 log.debug(u"discarding message already in history: {data}, ".format(data=data))
344 del self._cache
345 log.debug(u"{wid} is now unlocked".format(wid=self))
346
320 def postInit(self): 347 def postInit(self):
321 """Method to be called by frontend after widget is initialised 348 """Method to be called by frontend after widget is initialised
322 349
323 handle the display of history and subject 350 handle the display of history and subject
324 """ 351 """
460 Must probably be overriden by frontend to clear widget 487 Must probably be overriden by frontend to clear widget
461 @param size (int): number of messages 488 @param size (int): number of messages
462 @param filters (str): patterns to filter the history results 489 @param filters (str): patterns to filter the history results
463 @param profile (str): %(doc_profile)s 490 @param profile (str): %(doc_profile)s
464 """ 491 """
465 self._locked = True 492 self.setLocked()
466 self._cache = OrderedDict()
467 self.messages.clear() 493 self.messages.clear()
468 self.historyPrint(size, filters, profile) 494 self.historyPrint(size, filters, profile)
469 495
470 def _onHistoryPrinted(self): 496 def _onHistoryPrinted(self):
471 """Method called when history is printed (or failed) 497 """Method called when history is printed (or failed)
472 498
473 unlock the widget, and can be used to refresh or scroll down 499 unlock the widget, and can be used to refresh or scroll down
474 the focus after the history is printed 500 the focus after the history is printed
475 """ 501 """
476 self._locked = False 502 self.setUnlocked()
477 for data in self._cache.itervalues():
478 self.messageNew(*data)
479 del self._cache
480 503
481 def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT, filters=None, profile="@NONE@"): 504 def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT, filters=None, profile="@NONE@"):
482 """Print the current history 505 """Print the current history
483 506
507 Note: self.setUnlocked will be called once history is printed
484 @param size (int): number of messages 508 @param size (int): number of messages
485 @param search (str): pattern to filter the history results 509 @param search (str): pattern to filter the history results
486 @param profile (str): %(doc_profile)s 510 @param profile (str): %(doc_profile)s
487 """ 511 """
488 if filters is None: 512 if filters is None:
522 # if previous_day != message_day: 546 # if previous_day != message_day:
523 # self.printDayChange(message_day) 547 # self.printDayChange(message_day)
524 # previous_day = message_day 548 # previous_day = message_day
525 for data in history: 549 for data in history:
526 uid, timestamp, from_jid, to_jid, message, subject, type_, extra = data 550 uid, timestamp, from_jid, to_jid, message, subject, type_, extra = data
527 # cached messages may already be in history
528 # so we check it to avoid duplicates, they'll be added later
529 if uid in self._cache:
530 continue
531 from_jid = jid.JID(from_jid) 551 from_jid = jid.JID(from_jid)
532 to_jid = jid.JID(to_jid) 552 to_jid = jid.JID(to_jid)
533 # if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or 553 # if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or
534 # (self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)): 554 # (self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)):
535 # continue 555 # continue