comparison frontends/src/quick_frontend/quick_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 c74015dc2785
children d047535e3ed5
comparison
equal deleted inserted replaced
1747:40b7f18ac704 1748:3a6cd1c14974
138 log_msg = _(u"now we print the history") 138 log_msg = _(u"now we print the history")
139 if size != C.HISTORY_LIMIT_DEFAULT: 139 if size != C.HISTORY_LIMIT_DEFAULT:
140 log_msg += _(u" (%d messages)" % size) 140 log_msg += _(u" (%d messages)" % size)
141 log.debug(log_msg) 141 log.debug(log_msg)
142 142
143 target = self.target.bare
144
143 def onHistory(history): 145 def onHistory(history):
144 for line in history: 146 for line in history:
145 timestamp, from_jid, to_jid, message, type_, extra = line # FIXME: extra is unused ! 147 timestamp, from_jid, to_jid, message, type_, extra = line # FIXME: extra is unused !
146 if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or 148 if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or
147 (self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)): 149 (self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)):
148 continue 150 continue
149 self.printMessage(jid.JID(from_jid), message, {'timestamp':timestamp}, profile) 151 extra["timestamp"] = timestamp
152 self.newMessage(jid.JID(from_jid), target, message, type_, extra, profile)
150 self.afterHistoryPrint() 153 self.afterHistoryPrint()
151 154
152 def onHistoryError(err): 155 def onHistoryError(err):
153 log.error(_("Can't get history")) 156 log.error(_("Can't get history"))
154
155 target = self.target.bare
156 157
157 self.host.bridge.getHistory(unicode(self.host.profiles[profile].whoami.bare), unicode(target), size, True, search, profile, callback=onHistory, errback=onHistoryError) 158 self.host.bridge.getHistory(unicode(self.host.profiles[profile].whoami.bare), unicode(target), size, True, search, profile, callback=onHistory, errback=onHistoryError)
158 159
159 def _get_nick(self, entity): 160 def _get_nick(self, entity):
160 """Return nick of this entity when possible""" 161 """Return nick of this entity when possible"""
179 def newMessage(self, from_jid, target, msg, type_, extra, profile): 180 def newMessage(self, from_jid, target, msg, type_, extra, profile):
180 if self.type == C.CHAT_GROUP and target.resource and type_ != C.MESS_TYPE_GROUPCHAT: 181 if self.type == C.CHAT_GROUP and target.resource and type_ != C.MESS_TYPE_GROUPCHAT:
181 # we have a private message, we forward it to a private conversation widget 182 # we have a private message, we forward it to a private conversation widget
182 chat_widget = self.getOrCreatePrivateWidget(target) 183 chat_widget = self.getOrCreatePrivateWidget(target)
183 chat_widget.newMessage(from_jid, target, msg, type_, extra, profile) 184 chat_widget.newMessage(from_jid, target, msg, type_, extra, profile)
185 return
186 if type_ == C.MESS_TYPE_INFO:
187 self.printInfo(msg, extra=extra)
184 else: 188 else:
185 if type_ == C.MESS_TYPE_INFO: 189 nick = self._get_nick(from_jid)
186 self.printInfo(msg, extra=extra) 190 if msg.startswith('/me '):
191 self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', extra=extra)
187 else: 192 else:
188 self.printMessage(from_jid, msg, extra, profile) 193 # my_message is True if message comes from local user
189 194 my_message = (from_jid.resource == self.nick) if self.type == C.CHAT_GROUP else (from_jid.bare == self.host.profiles[profile].whoami.bare)
190 def printMessage(self, from_jid, msg, extra=None, profile=C.PROF_KEY_NONE): 195 try:
191 """Print message in chat window. Must be implemented by child class""" 196 timestamp = float(extra['timestamp'])
192 nick = self._get_nick(from_jid) 197 except KeyError:
193 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 198 timestamp = None
194 if msg.startswith('/me '): 199 self.printMessage(nick, my_message, msg, timestamp, extra, profile)
195 self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', extra=extra) 200
196 return 201 def printMessage(self, nick, my_message, message, timestamp, extra=None, profile=C.PROF_KEY_NONE):
197 return nick, mymess 202 """Print message in chat window.
203
204 @param nick (unicode): author nick
205 @param my_message (boolean): True if profile is the author
206 @param message (unicode): message content
207 @param extra (dict): extra data
208 """
209 raise NotImplementedError
198 210
199 def printInfo(self, msg, type_='normal', extra=None): 211 def printInfo(self, msg, type_='normal', extra=None):
200 """Print general info 212 """Print general info.
201 @param msg: message to print 213
202 @type_: one of: 214 @param msg (unicode): message to print
203 normal: general info like "toto has joined the room" 215 @param type_ (unicode):
204 me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist" 216 - 'normal': general info like "toto has joined the room"
217 - 'me': "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
205 @param extra (dict): message data 218 @param extra (dict): message data
206 """ 219 """
207 raise NotImplementedError 220 raise NotImplementedError
208 221
209 def getEntityStates(self, entity): 222 def getEntityStates(self, entity):