comparison frontends/src/primitivus/chat.py @ 1748:3a6cd1c14974

frontends: small message refactorisation: - factorize some treatments that were done both in Primitivus and Libervia - the way it was done, especially with QuickChat.printMessage returning a tuple, was quite disturbing - FIXME: we can probably remove some arguments and do a deeper factorization e.g with QuickChatText
author souliane <souliane@mailoo.org>
date Sat, 12 Dec 2015 12:18:54 +0100
parents 7751b5a51586
children 77870d2e2902
comparison
equal deleted inserted replaced
1747:40b7f18ac704 1748:3a6cd1c14974
295 self.host.redraw() 295 self.host.redraw()
296 296
297 def onPrivateCreated(self, widget): 297 def onPrivateCreated(self, widget):
298 self.host.contact_lists[widget.profile].specialResourceVisible(widget.target) 298 self.host.contact_lists[widget.profile].specialResourceVisible(widget.target)
299 299
300 def printMessage(self, from_jid, msg, extra=None, profile=C.PROF_KEY_NONE): 300 def printMessage(self, nick, my_message, message, timestamp, extra=None, profile=C.PROF_KEY_NONE):
301 assert isinstance(from_jid, jid.JID) 301 """Print message in chat window.
302 if extra is None: 302
303 extra = {} 303 @param nick (unicode): author nick
304 try: 304 @param my_message (boolean): True if profile is the author
305 timestamp = float(extra['timestamp']) 305 @param message (unicode): message content
306 except KeyError: 306 @param extra (dict): extra data
307 timestamp = None 307 """
308 try: 308 new_text = ChatText(self, timestamp, nick, my_message, message)
309 nick, mymess = QuickChat.printMessage(self, from_jid, msg, extra, profile)
310 except TypeError:
311 # None is returned, the message is managed
312 return
313 new_text = ChatText(self, timestamp, nick, mymess, msg)
314
315 if timestamp and self.content: 309 if timestamp and self.content:
316 for idx in range(len(self.content) - 1, -1, -1): 310 for idx in range(len(self.content) - 1, -1, -1):
317 current_text = self.content[idx] 311 current_text = self.content[idx]
318 older = new_text.timestamp < current_text.timestamp 312 older = new_text.timestamp < current_text.timestamp
319 if older and idx > 0: 313 if older and idx > 0:
320 continue # the new message is older, we need to insert it upper 314 continue # the new message is older, we need to insert it upper
321 315
322 # we discard double messages, to avoid backlog / history conflict 316 # we discard double messages, to avoid backlog / history conflict
323 # FIXME: messages that have been sent several times will be displayed only once 317 # FIXME: messages that have been sent several times will be displayed only once
324 if ((idx and self.content[idx - 1].message == msg) or 318 if ((idx and self.content[idx - 1].message == message) or
325 (self.content[idx].message == msg) or 319 (self.content[idx].message == message) or
326 (idx < len(self.content) - 2 and self.content[idx + 1].message)): 320 (idx < len(self.content) - 2 and self.content[idx + 1].message)):
327 return 321 return
328 322
329 self.content.insert(0 if older else idx + 1, new_text) 323 self.content.insert(0 if older else idx + 1, new_text)
330 break 324 break
332 self.content.append(new_text) 326 self.content.append(new_text)
333 if not timestamp: 327 if not timestamp:
334 # XXX: do not send notifications for each line of the history being displayed 328 # XXX: do not send notifications for each line of the history being displayed
335 # FIXME: this must be changed in the future if the timestamp is passed with 329 # FIXME: this must be changed in the future if the timestamp is passed with
336 # all messages and not only with the messages coming from the history. 330 # all messages and not only with the messages coming from the history.
337 self._notify(from_jid, msg) 331 self._notify(nick, message)
338 332
339 def printInfo(self, msg, type_='normal', extra=None): 333 def printInfo(self, msg, type_='normal', extra=None):
340 """Print general info 334 """Print general info
341 @param msg: message to print 335 @param msg: message to print
342 @type_: one of: 336 @type_: one of:
352 timestamp = None 346 timestamp = None
353 _widget = ChatText(self, timestamp, None, False, msg, is_info=True) 347 _widget = ChatText(self, timestamp, None, False, msg, is_info=True)
354 self.content.append(_widget) 348 self.content.append(_widget)
355 self._notify(msg=msg) 349 self._notify(msg=msg)
356 350
357 def _notify(self, from_jid="somebody", msg=""): 351 def _notify(self, contact="somebody", msg=""):
358 """Notify the user of a new message if primitivus doesn't have the focus. 352 """Notify the user of a new message if primitivus doesn't have the focus.
359 @param from_jid: contact who wrote to the users 353
360 @param msg: the message that has been received 354 @param contact (unicode): contact who wrote to the users
355 @param msg (unicode): the message that has been received
361 """ 356 """
362 if msg == "": 357 if msg == "":
363 return 358 return
364 if self.text_list.get_focus()[1] == len(self.content) - 2: 359 if self.text_list.get_focus()[1] == len(self.content) - 2:
365 # we don't change focus if user is not at the bottom 360 # we don't change focus if user is not at the bottom
366 # as that mean that he is probably watching discussion history 361 # as that mean that he is probably watching discussion history
367 self.text_list.focus_position = len(self.content) - 1 362 self.text_list.focus_position = len(self.content) - 1
368 self.host.redraw() 363 self.host.redraw()
369 if not self.host.x_notify.hasFocus(): 364 if not self.host.x_notify.hasFocus():
370 if self.type == C.CHAT_ONE2ONE: 365 if self.type == C.CHAT_ONE2ONE:
371 self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % from_jid) 366 self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % contact)
372 elif self.nick is not None and self.nick.lower() in msg.lower(): 367 elif self.nick is not None and self.nick.lower() in msg.lower():
373 self.host.x_notify.sendNotification(_("Primitivus: %(user)s mentioned you in room '%(room)s'") % {'user': from_jid, 'room': self.target}) 368 self.host.x_notify.sendNotification(_("Primitivus: %(user)s mentioned you in room '%(room)s'") % {'user': contact, 'room': self.target})
374 369
375 # MENU EVENTS # 370 # MENU EVENTS #
376 def onTarotRequest(self, menu): 371 def onTarotRequest(self, menu):
377 # TODO: move this to plugin_misc_tarot with dynamic menu 372 # TODO: move this to plugin_misc_tarot with dynamic menu
378 if len(self.occupants) != 4: 373 if len(self.occupants) != 4: