comparison frontends/src/primitivus/chat.py @ 678:a630b94280d5

primitivus: code factorization for user notification
author souliane <souliane@mailoo.org>
date Mon, 21 Oct 2013 15:52:28 +0200
parents 6821fc06a324
children f7878ad3c846
comparison
equal deleted inserted replaced
677:9a50aa7feefb 678:a630b94280d5
260 self.host.redraw() 260 self.host.redraw()
261 261
262 def printMessage(self, from_jid, msg, profile, timestamp=""): 262 def printMessage(self, from_jid, msg, profile, timestamp=""):
263 assert isinstance(from_jid, JID) 263 assert isinstance(from_jid, JID)
264 try: 264 try:
265 jid,nick,mymess = QuickChat.printMessage(self, from_jid, msg, profile, timestamp) 265 jid, nick, mymess = QuickChat.printMessage(self, from_jid, msg, profile, timestamp)
266 except TypeError: 266 except TypeError:
267 return 267 return
268 268
269 new_text = ChatText(self, timestamp or None, nick, mymess, msg) 269 new_text = ChatText(self, timestamp or None, nick, mymess, msg)
270 270
271 if timestamp and self.content: 271 if timestamp and self.content:
272 for idx in range(len(self.content)-1,-1,-1): 272 for idx in range(len(self.content) - 1, -1, -1):
273 current_text = self.content[idx] 273 current_text = self.content[idx]
274 if new_text.timestamp < current_text.timestamp and idx > 0: 274 if new_text.timestamp < current_text.timestamp and idx > 0:
275 continue #the new message is older, we need to insert it upper 275 continue # the new message is older, we need to insert it upper
276 276
277 #we discard double messages, to avoid backlog / history conflict 277 #we discard double messages, to avoid backlog / history conflict
278 if ((idx and self.content[idx-1].message == msg) or 278 if ((idx and self.content[idx - 1].message == msg) or
279 (self.content[idx].message == msg) or 279 (self.content[idx].message == msg) or
280 (idx<len(self.content)-2 and self.content[idx+1].message)): 280 (idx < len(self.content) - 2 and self.content[idx + 1].message)):
281 return 281 return
282 282
283 self.content.insert(idx+1, new_text) 283 self.content.insert(idx + 1, new_text)
284 break 284 break
285 else: 285 else:
286 self.content.append(new_text) 286 self.content.append(new_text)
287 287 self._notify(from_jid, msg)
288 if self.text_list.get_focus()[1] == len(self.content)-2: 288
289 #we don't change focus if user is not at the bottom 289 def printInfo(self, msg, type_='normal', timestamp=""):
290 #as that mean that he is probably watching discussion history
291 self.text_list.set_focus(len(self.content)-1)
292 self.host.redraw()
293 if not self.host.x_notify.hasFocus():
294 if self.type=="one2one":
295 self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % from_jid)
296 elif self.getUserNick().lower() in msg.lower():
297 self.host.x_notify.sendNotification(_("Primitivus: Somebody pinged your name in %s room") % self.target)
298
299 def printInfo(self, msg, type='normal', timestamp=""):
300 """Print general info 290 """Print general info
301 @param msg: message to print 291 @param msg: message to print
302 @type: one of: 292 @type: one of:
303 normal: general info like "toto has joined the room" 293 normal: general info like "toto has joined the room"
304 me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist" 294 me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
305 """ 295 """
306 #FIXME: duplicated code, this must be refactored
307 _widget = ChatText(self, timestamp or None, None, False, msg, is_info=True) 296 _widget = ChatText(self, timestamp or None, None, False, msg, is_info=True)
308 self.content.append(_widget) 297 self.content.append(_widget)
309 if self.text_list.get_focus()[1] == len(self.content)-2: 298 self._notify(msg=msg)
299
300 def _notify(self, from_jid="somebody", msg=""):
301 """Notify the user of a new message if primitivus doesn't have the focus.
302 @param from_jid: contact who wrote to the users
303 @param msg: the message that has been received
304 """
305 if self.text_list.get_focus()[1] == len(self.content) - 2:
310 #we don't change focus if user is not at the bottom 306 #we don't change focus if user is not at the bottom
311 #as that mean that he is probably watching discussion history 307 #as that mean that he is probably watching discussion history
312 self.text_list.set_focus(len(self.content)-1) 308 self.text_list.set_focus(len(self.content) - 1)
313 self.host.redraw() 309 self.host.redraw()
314 if not self.host.x_notify.hasFocus(): 310 if not self.host.x_notify.hasFocus():
315 if self.type=="one2one": 311 if self.type == "one2one":
316 self.host.x_notify.sendNotification(_("Primitivus: there is a message about you")) 312 self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % from_jid)
317 elif self.getUserNick().lower() in msg.lower(): 313 elif self.getUserNick().lower() in msg.lower():
318 self.host.x_notify.sendNotification(_("Primitivus: Somebody is talking about you in %s room") % self.target) 314 self.host.x_notify.sendNotification(_("Primitivus: %s mentioned you in room '%s'") % (from_jid, self.target))
319 315
320 def startGame(self, game_type, referee, players): 316 def startGame(self, game_type, referee, players):
321 """Configure the chat window to start a game""" 317 """Configure the chat window to start a game"""
322 if game_type=="Tarot": 318 if game_type=="Tarot":
323 self.tarot_wid = CardGame(self, referee, players, self.nick) 319 self.tarot_wid = CardGame(self, referee, players, self.nick)