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
This commit is contained in:
souliane 2015-12-12 12:18:54 +01:00
commit 77c0656c85
2 changed files with 51 additions and 43 deletions

View file

@ -297,21 +297,15 @@ class Chat(PrimitivusWidget, QuickChat):
def onPrivateCreated(self, widget):
self.host.contact_lists[widget.profile].specialResourceVisible(widget.target)
def printMessage(self, from_jid, msg, extra=None, profile=C.PROF_KEY_NONE):
assert isinstance(from_jid, jid.JID)
if extra is None:
extra = {}
try:
timestamp = float(extra['timestamp'])
except KeyError:
timestamp = None
try:
nick, mymess = QuickChat.printMessage(self, from_jid, msg, extra, profile)
except TypeError:
# None is returned, the message is managed
return
new_text = ChatText(self, timestamp, nick, mymess, msg)
def printMessage(self, nick, my_message, message, timestamp, extra=None, profile=C.PROF_KEY_NONE):
"""Print message in chat window.
@param nick (unicode): author nick
@param my_message (boolean): True if profile is the author
@param message (unicode): message content
@param extra (dict): extra data
"""
new_text = ChatText(self, timestamp, nick, my_message, message)
if timestamp and self.content:
for idx in range(len(self.content) - 1, -1, -1):
current_text = self.content[idx]
@ -321,8 +315,8 @@ class Chat(PrimitivusWidget, QuickChat):
# we discard double messages, to avoid backlog / history conflict
# FIXME: messages that have been sent several times will be displayed only once
if ((idx and self.content[idx - 1].message == msg) or
(self.content[idx].message == msg) or
if ((idx and self.content[idx - 1].message == message) or
(self.content[idx].message == message) or
(idx < len(self.content) - 2 and self.content[idx + 1].message)):
return
@ -334,7 +328,7 @@ class Chat(PrimitivusWidget, QuickChat):
# XXX: do not send notifications for each line of the history being displayed
# FIXME: this must be changed in the future if the timestamp is passed with
# all messages and not only with the messages coming from the history.
self._notify(from_jid, msg)
self._notify(nick, message)
def printInfo(self, msg, type_='normal', extra=None):
"""Print general info
@ -354,10 +348,11 @@ class Chat(PrimitivusWidget, QuickChat):
self.content.append(_widget)
self._notify(msg=msg)
def _notify(self, from_jid="somebody", msg=""):
def _notify(self, contact="somebody", msg=""):
"""Notify the user of a new message if primitivus doesn't have the focus.
@param from_jid: contact who wrote to the users
@param msg: the message that has been received
@param contact (unicode): contact who wrote to the users
@param msg (unicode): the message that has been received
"""
if msg == "":
return
@ -368,9 +363,9 @@ class Chat(PrimitivusWidget, QuickChat):
self.host.redraw()
if not self.host.x_notify.hasFocus():
if self.type == C.CHAT_ONE2ONE:
self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % from_jid)
self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % contact)
elif self.nick is not None and self.nick.lower() in msg.lower():
self.host.x_notify.sendNotification(_("Primitivus: %(user)s mentioned you in room '%(room)s'") % {'user': from_jid, 'room': self.target})
self.host.x_notify.sendNotification(_("Primitivus: %(user)s mentioned you in room '%(room)s'") % {'user': contact, 'room': self.target})
# MENU EVENTS #
def onTarotRequest(self, menu):

View file

@ -140,20 +140,21 @@ class QuickChat(quick_widgets.QuickWidget):
log_msg += _(u" (%d messages)" % size)
log.debug(log_msg)
target = self.target.bare
def onHistory(history):
for line in history:
timestamp, from_jid, to_jid, message, type_, extra = line # FIXME: extra is unused !
timestamp, from_jid, to_jid, message, type_, extra = line # FIXME: extra is unused !
if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or
(self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)):
continue
self.printMessage(jid.JID(from_jid), message, {'timestamp':timestamp}, profile)
extra["timestamp"] = timestamp
self.newMessage(jid.JID(from_jid), target, message, type_, extra, profile)
self.afterHistoryPrint()
def onHistoryError(err):
log.error(_("Can't get history"))
target = self.target.bare
self.host.bridge.getHistory(unicode(self.host.profiles[profile].whoami.bare), unicode(target), size, True, search, profile, callback=onHistory, errback=onHistoryError)
def _get_nick(self, entity):
@ -181,27 +182,39 @@ class QuickChat(quick_widgets.QuickWidget):
# we have a private message, we forward it to a private conversation widget
chat_widget = self.getOrCreatePrivateWidget(target)
chat_widget.newMessage(from_jid, target, msg, type_, extra, profile)
else:
if type_ == C.MESS_TYPE_INFO:
self.printInfo(msg, extra=extra)
else:
self.printMessage(from_jid, msg, extra, profile)
def printMessage(self, from_jid, msg, extra=None, profile=C.PROF_KEY_NONE):
"""Print message in chat window. Must be implemented by child class"""
nick = self._get_nick(from_jid)
mymess = (from_jid.resource == self.nick) if self.type == C.CHAT_GROUP else (from_jid.bare == self.host.profiles[profile].whoami.bare) #mymess = True if message comes from local user
if msg.startswith('/me '):
self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', extra=extra)
return
return nick, mymess
if type_ == C.MESS_TYPE_INFO:
self.printInfo(msg, extra=extra)
else:
nick = self._get_nick(from_jid)
if msg.startswith('/me '):
self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', extra=extra)
else:
# my_message is True if message comes from local user
my_message = (from_jid.resource == self.nick) if self.type == C.CHAT_GROUP else (from_jid.bare == self.host.profiles[profile].whoami.bare)
try:
timestamp = float(extra['timestamp'])
except KeyError:
timestamp = None
self.printMessage(nick, my_message, msg, timestamp, extra, profile)
def printMessage(self, nick, my_message, message, timestamp, extra=None, profile=C.PROF_KEY_NONE):
"""Print message in chat window.
@param nick (unicode): author nick
@param my_message (boolean): True if profile is the author
@param message (unicode): message content
@param extra (dict): extra data
"""
raise NotImplementedError
def printInfo(self, msg, type_='normal', extra=None):
"""Print general info
@param msg: message to print
@type_: one of:
normal: general info like "toto has joined the room"
me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
"""Print general info.
@param msg (unicode): message to print
@param type_ (unicode):
- 'normal': general info like "toto has joined the room"
- 'me': "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
@param extra (dict): message data
"""
raise NotImplementedError