Mercurial > libervia-backend
changeset 190:31632472e857
quick_frontend, wix, primitivus: informations in chat window
- user joined/left information
- messages starting with /me are managed
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 18 Aug 2010 21:42:30 +0800 |
parents | 757518d05833 |
children | 1438a1337732 |
files | frontends/primitivus/chat.py frontends/quick_frontend/quick_chat.py frontends/wix/chat.py |
diffstat | 3 files changed, 72 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/frontends/primitivus/chat.py Wed Aug 18 20:50:45 2010 +0800 +++ b/frontends/primitivus/chat.py Wed Aug 18 21:42:30 2010 +0800 @@ -32,11 +32,11 @@ class ChatText(urwid.FlowWidget): """Manage the printing of chat message""" - def __init__(self, parent, timestamp, my_jid, from_jid, message, align='left'): + def __init__(self, parent, timestamp, nick, my_mess, message, align='left'): self.parent = parent self.timestamp = time.localtime(timestamp) - self.my_jid = my_jid - self.from_jid = from_jid + self.nick = nick + self.my_mess = my_mess self.message = unicode(message) self.align = align @@ -60,16 +60,14 @@ return 0, 0 def display_widget(self, size, focus): - my_mess = (self.from_jid.resource == self.parent.nick) if self.parent.type == "group" else (self.from_jid.short == self.my_jid.short) #mymess = True if message comes from local user render_txt = [] if self.parent.show_timestamp: time_format = "%c" if self.timestamp < self.parent.day_change else "%H:%M" #if the message was sent before today, we print the full date render_txt.append(('date',"[%s]" % time.strftime(time_format, self.timestamp))) if self.parent.show_short_nick: - render_txt.append(('my_nick' if my_mess else 'other_nick',"**" if my_mess else "*")) + render_txt.append(('my_nick' if self.my_mess else 'other_nick',"**" if self.my_mess else "*")) else: - nick = self.from_jid.resource if self.parent.type == "group" else (self.parent.host.CM.getAttr(self.from_jid,'nick') or self.parent.host.CM.getAttr(self.from_jid,'name') or self.from_jid.node) - render_txt.append(('my_nick' if my_mess else 'other_nick',"[%s] " % nick)) + render_txt.append(('my_nick' if self.my_mess else 'other_nick',"[%s] " % self.nick)) render_txt.append(self.message) return urwid.Text(render_txt, align=self.align) @@ -227,8 +225,23 @@ def printMessage(self, from_jid, msg, profile, timestamp=""): assert isinstance(from_jid, JID) + try: + jid,nick,mymess = QuickChat.printMessage(self, from_jid, msg, profile, timestamp) + except TypeError: + return my_jid = self.host.profiles[profile]['whoami'] - self.content.append(ChatText(self, timestamp or None, my_jid, from_jid, msg)) + self.content.append(ChatText(self, timestamp or None, nick, mymess, msg)) + self.text_list.set_focus(len(self.content)-1) + self.host.redraw() + + def printInfo(self, msg, type='normal'): + """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" + """ + self.content.append(custom_widgets.ClickableText(msg)) self.text_list.set_focus(len(self.content)-1) self.host.redraw()
--- a/frontends/quick_frontend/quick_chat.py Wed Aug 18 20:50:45 2010 +0800 +++ b/frontends/quick_frontend/quick_chat.py Wed Aug 18 21:42:30 2010 +0800 @@ -56,7 +56,10 @@ if self.type != "group": error (_("[INTERNAL] trying to replace user for a non group chat window")) raise Exception("INTERNAL ERROR") #TODO: raise proper Exception here + len_before = len(self.occupants) self.occupants.add(nick) + if len_before != len(self.occupants): + self.printInfo("=> %s has joined the room" % nick) def setUserNick(self, nick): """Set the nick of the user, usefull for e.g. change the color of the user""" @@ -69,6 +72,7 @@ error (_("[INTERNAL] trying to remove user for a non group chat window")) raise Exception("INTERNAL ERROR") #TODO: raise proper Exception here self.occupants.remove(nick) + self.printInfo("=> %s has left the room" % nick) def setSubject(self, subject): """Set title for a group chat""" @@ -88,9 +92,29 @@ if keep_last: ##FIXME hack for sortilege self.last_history = stamps[-1] if stamps else None + def _get_nick(self, jid): + """Return nick of this jid when possible""" + return jid.resource if self.type == "group" else (self.host.CM.getAttr(jid,'nick') or self.host.CM.getAttr(jid,'name') or jid.node) + def printMessage(self, from_jid, msg, profile, timestamp): """Print message in chat window. Must be implemented by child class""" + jid=JID(from_jid) + nick = self._get_nick(jid) + mymess = (jid.resource == self.nick) if self.type == "group" else (jid.short == self.host.profiles[profile]['whoami'].short) #mymess = True if message comes from local user + if msg.startswith('/me '): + self.printInfo('* %s %s' % (nick, msg[4:]),type='me') + return + return jid, nick, mymess + + def printInfo(self, msg, type='normal'): + """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" + """ raise NotImplementedError + def startGame(self, game_type, referee, players): """Configure the chat window to start a game"""
--- a/frontends/wix/chat.py Wed Aug 18 20:50:45 2010 +0800 +++ b/frontends/wix/chat.py Wed Aug 18 21:42:30 2010 +0800 @@ -198,14 +198,22 @@ profile_key=self.host.profile) self.textBox.Clear() - + def __blink(self): + """Do wizzz and buzzz to show window to user or + at least inform him of something new""" + #TODO: use notification system + if not self.IsActive(): + self.RequestUserAttention() + if not self.IsShown(): + self.Show() def printMessage(self, from_jid, msg, profile, timestamp=""): """Print the message with differents colors depending on where it comes from.""" - jid=JID(from_jid) + try: + jid,nick,mymess = QuickChat.printMessage(self, from_jid, msg, profile, timestamp) + except TypeError: + return print "printMessage, jid=",jid,"type=",self.type - nick = jid.resource if self.type == "group" else (self.host.CM.getAttr(jid,'nick') or self.host.CM.getAttr(jid,'name') or jid.node) - mymess = (jid.resource == self.nick) if self.type == "group" else (jid.short == self.host.profiles[profile]['whoami'].short) #mymess = True if message comes from local user _font_bold = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.BOLD) _font_normal = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.NORMAL) _font_italic = wx.Font(self.font["points"], self.font["family"], wx.ITALIC if mymess else wx.NORMAL, wx.NORMAL) @@ -218,11 +226,21 @@ self.chatWindow.SetDefaultStyle(wx.TextAttr("BLACK", font=_font_italic)) self.chatWindow.AppendText("%s\n" % msg) if not mymess: - #TODO: use notification system - if not self.IsActive(): - self.RequestUserAttention() - if not self.IsShown(): - self.Show() + self.__blink() + + def printInfo(self, msg, type='normal'): + """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" + """ + _font_bold = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.BOLD) + _font_normal = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.NORMAL) + self.chatWindow.SetDefaultStyle(wx.TextAttr("BLACK", font=_font_bold if type == 'normal' else _font_normal)) + self.chatWindow.AppendText("%s\n" % msg) + if type=="me": + self.__blink() ### events ###