comparison browser_side/panels.py @ 235:b304cdf13a3b

browser and server side: XHTML handling, first draft: - added the extra parameter for getHistory and newMessage to manage xhtml - if present, XHTML is shown instead of regular text message
author Goffi <goffi@goffi.org>
date Thu, 07 Nov 2013 15:22:00 +0100
parents 0ed09cc0566f
children b911f2b43fd4
comparison
equal deleted inserted replaced
234:d4e73d9140af 235:b304cdf13a3b
597 self.host.setSelected(None) 597 self.host.setSelected(None)
598 598
599 599
600 class ChatText(HTMLPanel): 600 class ChatText(HTMLPanel):
601 601
602 def __init__(self, timestamp, nick, mymess, msg): 602 def __init__(self, timestamp, nick, mymess, msg, xhtml = None):
603 _date = datetime.fromtimestamp(float(timestamp or time())) 603 _date = datetime.fromtimestamp(float(timestamp or time()))
604 _msg_class = ["chat_text_msg"] 604 _msg_class = ["chat_text_msg"]
605 if mymess: 605 if mymess:
606 _msg_class.append("chat_text_mymess") 606 _msg_class.append("chat_text_mymess")
607 HTMLPanel.__init__(self, "<span class='chat_text_timestamp'>%(timestamp)s</span> <span class='chat_text_nick'>%(nick)s</span> <span class='%(msg_class)s'>%(msg)s</span>" % 607 HTMLPanel.__init__(self, "<span class='chat_text_timestamp'>%(timestamp)s</span> <span class='chat_text_nick'>%(nick)s</span> <span class='%(msg_class)s'>%(msg)s</span>" %
608 {"timestamp": _date.strftime("%H:%M"), 608 {"timestamp": _date.strftime("%H:%M"),
609 "nick": "[%s]" % html_sanitize(nick), 609 "nick": "[%s]" % html_sanitize(nick),
610 "msg_class": ' '.join(_msg_class), 610 "msg_class": ' '.join(_msg_class),
611 "msg": addURLToText(html_sanitize(msg))} 611 "msg": addURLToText(html_sanitize(msg)) if not xhtml else xhtml} #FIXME: images and external links must be removed according to preferences
612 ) 612 )
613 self.setStyleName('chatText') 613 self.setStyleName('chatText')
614 614
615 615
616 class Occupant(HTML): 616 class Occupant(HTML):
765 def getHistoryCB(history): 765 def getHistoryCB(history):
766 # display day change 766 # display day change
767 day_format = "%A, %d %b %Y" 767 day_format = "%A, %d %b %Y"
768 previous_day = datetime.now().strftime(day_format) 768 previous_day = datetime.now().strftime(day_format)
769 for line in history: 769 for line in history:
770 timestamp, from_jid, to_jid, message, mess_type = line 770 timestamp, from_jid, to_jid, message, mess_type, extra = line
771 message_day = datetime.fromtimestamp(float(timestamp or time())).strftime(day_format) 771 message_day = datetime.fromtimestamp(float(timestamp or time())).strftime(day_format)
772 if previous_day != message_day: 772 if previous_day != message_day:
773 self.printInfo("* " + message_day) 773 self.printInfo("* " + message_day)
774 previous_day = message_day 774 previous_day = message_day
775 self.printMessage(from_jid, message, timestamp) 775 self.printMessage(from_jid, message, extra, timestamp)
776 self.host.bridge.call('getHistory', getHistoryCB, self.host.whoami.bare, self.target.bare, size, True) 776 self.host.bridge.call('getHistory', getHistoryCB, self.host.whoami.bare, self.target.bare, size, True)
777 777
778 def printInfo(self, msg, type='normal'): 778 def printInfo(self, msg, type='normal'):
779 """Print general info 779 """Print general info
780 @param msg: message to print 780 @param msg: message to print
789 _wid.setStyleName('chatTextMe') 789 _wid.setStyleName('chatTextMe')
790 else: 790 else:
791 _wid.setStyleName('chatTextInfo') 791 _wid.setStyleName('chatTextInfo')
792 self.content.add(_wid) 792 self.content.add(_wid)
793 793
794 def printMessage(self, from_jid, msg, timestamp=None): 794 def printMessage(self, from_jid, msg, extra, timestamp=None):
795 """Print message in chat window. Must be implemented by child class""" 795 """Print message in chat window. Must be implemented by child class"""
796 _jid = JID(from_jid) 796 _jid = JID(from_jid)
797 nick = _jid.node if self.type == 'one2one' else _jid.resource 797 nick = _jid.node if self.type == 'one2one' else _jid.resource
798 mymess = _jid.resource == self.nick if self.type == "group" else _jid.bare == self.host.whoami.bare # mymess = True if message comes from local user 798 mymess = _jid.resource == self.nick if self.type == "group" else _jid.bare == self.host.whoami.bare # mymess = True if message comes from local user
799 if msg.startswith('/me '): 799 if msg.startswith('/me '):
800 self.printInfo('* %s %s' % (nick, msg[4:]), type='me') 800 self.printInfo('* %s %s' % (nick, msg[4:]), type='me')
801 return 801 return
802 self.content.add(ChatText(timestamp, nick, mymess, msg)) 802 self.content.add(ChatText(timestamp, nick, mymess, msg, extra.get('xhtml')))
803 self.content_scroll.scrollToBottom() 803 self.content_scroll.scrollToBottom()
804 804
805 def startGame(self, game_type, referee, players): 805 def startGame(self, game_type, referee, players):
806 """Configure the chat window to start a game""" 806 """Configure the chat window to start a game"""
807 if game_type == "Tarot": 807 if game_type == "Tarot":