comparison sat_frontends/primitivus/chat.py @ 2881:13e0a260e7b8

primitivus (chat): use the factorised code for user moved info messages.
author Goffi <goffi@goffi.org>
date Thu, 28 Mar 2019 08:40:38 +0100
parents 003b8b4b56a7
children 5bba8953061e
comparison
equal deleted inserted replaced
2880:dd650f3e070f 2881:13e0a260e7b8
36 36
37 37
38 OCCUPANTS_FOOTER = _(u"{} occupants") 38 OCCUPANTS_FOOTER = _(u"{} occupants")
39 39
40 40
41 class MessageWidget(urwid.WidgetWrap): 41 class MessageWidget(urwid.WidgetWrap, quick_chat.MessageWidget):
42 def __init__(self, mess_data): 42 def __init__(self, mess_data):
43 """ 43 """
44 @param mess_data(quick_chat.Message, None): message data 44 @param mess_data(quick_chat.Message, None): message data
45 None: used only for non text widgets (e.g.: focus separator) 45 None: used only for non text widgets (e.g.: focus separator)
46 """ 46 """
274 self.updateFooter() 274 self.updateFooter()
275 self.parent.host.redraw() # FIXME: should not be necessary 275 self.parent.host.redraw() # FIXME: should not be necessary
276 276
277 277
278 class Chat(PrimitivusWidget, quick_chat.QuickChat): 278 class Chat(PrimitivusWidget, quick_chat.QuickChat):
279 def __init__( 279 def __init__(self, host, target, type_=C.CHAT_ONE2ONE, nick=None, occupants=None,
280 self, 280 subject=None, profiles=None):
281 host,
282 target,
283 type_=C.CHAT_ONE2ONE,
284 nick=None,
285 occupants=None,
286 subject=None,
287 profiles=None,
288 ):
289 quick_chat.QuickChat.__init__( 281 quick_chat.QuickChat.__init__(
290 self, host, target, type_, nick, occupants, subject, profiles=profiles 282 self, host, target, type_, nick, occupants, subject, profiles=profiles
291 ) 283 )
292 self.filters = [] # list of filter callbacks to apply 284 self.filters = [] # list of filter callbacks to apply
293 self.mess_walker = urwid.SimpleListWalker([]) 285 self.mess_walker = urwid.SimpleListWalker([])
312 self.focus_marker_set = None # True if a new marker has been inserted 304 self.focus_marker_set = None # True if a new marker has been inserted
313 self.show_timestamp = True 305 self.show_timestamp = True
314 self.show_short_nick = False 306 self.show_short_nick = False
315 self.show_title = 1 # 0: clip title; 1: full title; 2: no title 307 self.show_title = 1 # 0: clip title; 1: full title; 2: no title
316 self.postInit() 308 self.postInit()
309
310 @property
311 def message_widgets_rev(self):
312 return reversed(self.mess_walker)
317 313
318 def keypress(self, size, key): 314 def keypress(self, size, key):
319 if key == a_key["OCCUPANTS_HIDE"]: # user wants to (un)hide the occupants panel 315 if key == a_key["OCCUPANTS_HIDE"]: # user wants to (un)hide the occupants panel
320 if self.type == C.CHAT_GROUP: 316 if self.type == C.CHAT_GROUP:
321 widgets = [widget for (widget, options) in self.chat_colums.contents] 317 widgets = [widget for (widget, options) in self.chat_colums.contents]
327 self.show_timestamp = not self.show_timestamp 323 self.show_timestamp = not self.show_timestamp
328 self.redraw() 324 self.redraw()
329 elif key == a_key["SHORT_NICKNAME"]: # user wants to (not) use short nick 325 elif key == a_key["SHORT_NICKNAME"]: # user wants to (not) use short nick
330 self.show_short_nick = not self.show_short_nick 326 self.show_short_nick = not self.show_short_nick
331 self.redraw() 327 self.redraw()
332 elif ( 328 elif (key == a_key["SUBJECT_SWITCH"]):
333 key == a_key["SUBJECT_SWITCH"] 329 # user wants to (un)hide group's subject or change its apperance
334 ): # user wants to (un)hide group's subject or change its apperance
335 if self.subject: 330 if self.subject:
336 self.show_title = (self.show_title + 1) % 3 331 self.show_title = (self.show_title + 1) % 3
337 if self.show_title == 0: 332 if self.show_title == 0:
338 self.setSubject(self.subject, "clip") 333 self.setSubject(self.subject, "clip")
339 elif self.show_title == 1: 334 elif self.show_title == 1:
418 # self.update(entity) 413 # self.update(entity)
419 414
420 def createMessage(self, message): 415 def createMessage(self, message):
421 self.appendMessage(message) 416 self.appendMessage(message)
422 417
423 def _user_moved(self, message):
424 """return true if message is a user left/joined message
425
426 @param message(quick_chat.Message): message to add
427 """
428 if message.type != C.MESS_TYPE_INFO:
429 return False
430 try:
431 info_type = message.extra["info_type"]
432 except KeyError:
433 return False
434 else:
435 return info_type in quick_chat.ROOM_USER_MOVED
436
437 def _scrollDown(self): 418 def _scrollDown(self):
438 """scroll down message only if we are already at the bottom (minus 1)""" 419 """scroll down message only if we are already at the bottom (minus 1)"""
439 current_focus = self.mess_widgets.focus_position 420 current_focus = self.mess_widgets.focus_position
440 bottom = len(self.mess_walker) - 1 421 bottom = len(self.mess_walker) - 1
441 if current_focus == bottom - 1: 422 if current_focus == bottom - 1:
449 @param message(quick_chat.Message): message to add 430 @param message(quick_chat.Message): message to add
450 """ 431 """
451 if self.filters: 432 if self.filters:
452 if not all([f(message) for f in self.filters]): 433 if not all([f(message) for f in self.filters]):
453 return 434 return
454 if self._user_moved(message): 435
455 for wid in reversed(self.mess_walker): 436 if self.handleUserMoved(message):
456 # we merge in/out messages if no message was sent meanwhile 437 return
457 if not isinstance(wid, MessageWidget):
458 continue
459 if wid.mess_data.type != C.MESS_TYPE_INFO:
460 break
461 if (
462 wid.info_type in quick_chat.ROOM_USER_MOVED
463 and wid.mess_data.nick == message.nick
464 ):
465 try:
466 count = wid.reentered_count
467 except AttributeError:
468 count = wid.reentered_count = 1
469 nick = wid.mess_data.nick
470 if message.info_type == quick_chat.ROOM_USER_LEFT:
471 wid.message = _(u"<= {nick} has left the room ({count})").format(
472 nick=nick, count=count
473 )
474 else:
475 wid.message = _(
476 u"<=> {nick} re-entered the room ({count})"
477 ).format(nick=nick, count=count)
478 wid.reentered_count += 1
479 return
480 438
481 if ( 439 if (
482 self.host.selected_widget != self or not self.host.x_notify.hasFocus() 440 self.host.selected_widget != self or not self.host.x_notify.hasFocus()
483 ) and self.focus_marker_set is not None: 441 ) and self.focus_marker_set is not None:
484 if not self.focus_marker_set and not self._locked and self.mess_walker: 442 if not self.focus_marker_set and not self._locked and self.mess_walker:
493 self.focus_marker_set = False 451 self.focus_marker_set = False
494 452
495 wid = MessageWidget(message) 453 wid = MessageWidget(message)
496 self.mess_walker.append(wid) 454 self.mess_walker.append(wid)
497 self._scrollDown() 455 self._scrollDown()
498 if self._user_moved(message): 456 if self.isUserMoved(message):
499 return # no notification for moved messages 457 return # no notification for moved messages
500 458
501 # notifications 459 # notifications
502 460
503 if self._locked: 461 if self._locked: