comparison frontends/primitivus/chat.py @ 124:961e0898271f

primitivus chat window - management of one 2 one / group chat - timestamp displayed - added shortcuts for showing/hiding panels - color used - fixed vcard bug (contact displayed even if not from current profile if vcard changed/not in cache) - added VerticalSeparator widget - *List widgets can now use an other widget than SelectableText - new UnselectableText widget
author Goffi <goffi@goffi.org>
date Thu, 08 Jul 2010 19:47:54 +0800
parents 1ca5f254ce41
children 8d611eb9ae48
comparison
equal deleted inserted replaced
123:34766e0cf970 124:961e0898271f
20 """ 20 """
21 21
22 import urwid 22 import urwid
23 from quick_frontend.quick_contact_list import QuickContactList 23 from quick_frontend.quick_contact_list import QuickContactList
24 from quick_frontend.quick_chat import QuickChat 24 from quick_frontend.quick_chat import QuickChat
25 from custom_widgets import Password,List,InputDialog,ConfirmDialog,Alert,SelectableText,LabelLine,SurroundedText 25 import custom_widgets
26 import time
27 from tools.jid import JID
26 28
29
30 class ChatText(urwid.FlowWidget):
31 """Manage the printing of chat message"""
32
33 def __init__(self, parent, timestamp, my_jid, from_jid, message, align='left'):
34 self.parent = parent
35 self.timestamp = time.localtime(timestamp)
36 self.my_jid = my_jid
37 self.from_jid = from_jid
38 self.message = unicode(message)
39 self.align = align
40
41 def selectable(self):
42 return True
43
44 def keypress(self, size, key):
45 return key
46
47 def rows(self,size,focus=False):
48 return self.display_widget(size, focus).rows(size, focus)
49
50 def render(self, size, focus=False):
51 return self.display_widget(size, focus).render(size, focus)
52
53 def display_widget(self, size, focus):
54 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
55 render_txt = []
56 if self.parent.show_timestamp:
57 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
58 render_txt.append(('date',"[%s]" % time.strftime(time_format, self.timestamp)))
59 if self.parent.show_short_nick:
60 render_txt.append(('my_nick' if my_mess else 'other_nick',"**" if my_mess else "*"))
61 else:
62 render_txt.append(('my_nick' if my_mess else 'other_nick',"[%s] " % self.from_jid))
63 render_txt.append(self.message)
64 return urwid.Text(render_txt, align=self.align)
27 65
28 class Chat(urwid.WidgetWrap, QuickChat): 66 class Chat(urwid.WidgetWrap, QuickChat):
29 67
30 def __init__(self, target, host, type='one2one'): 68 def __init__(self, target, host, type='one2one'):
69 self.target = target
31 QuickChat.__init__(self, target, host, type) 70 QuickChat.__init__(self, target, host, type)
32 self.content = urwid.SimpleListWalker([]) 71 self.content = urwid.SimpleListWalker([])
33 self.text_list = urwid.ListBox(self.content) 72 self.text_list = urwid.ListBox(self.content)
34 main_widget = LabelLine( 73 self.chat_widget = urwid.Frame(self.text_list)
35 urwid.Frame(self.text_list), SurroundedText(str(target)) 74 self.columns = urwid.Columns([('weight', 8, self.chat_widget)])
36 ) 75 urwid.WidgetWrap.__init__(self, self.__getDecoration(self.columns))
37 urwid.WidgetWrap.__init__(self, main_widget)
38 self.setType(type) 76 self.setType(type)
39 77 self.day_change = time.strptime(time.strftime("%a %b %d 00:00:00 %Y")) #struct_time of day changing time
78 self.show_timestamp = True
79 self.show_short_nick = False
80 self.show_title = True
81 self.subject = None
82
83 def keypress(self, size, key):
84 if key == "meta p": #user wants to (un)hide the presents panel
85 if self.type == 'group':
86 widgets = self.columns.widget_list
87 if self.present_panel in widgets:
88 self.__removePresentPanel()
89 else:
90 self.__appendPresentPanel()
91 elif key == "meta t": #user wants to (un)hide timestamp
92 self.show_timestamp = not self.show_timestamp
93 for wid in self.content:
94 wid._invalidate()
95 elif key == "meta n": #user wants to (not) use short nick
96 self.show_short_nick = not self.show_short_nick
97 for wid in self.content:
98 wid._invalidate()
99 elif key == "meta l": #user wants to (un)hide widget decoration
100 show = not self._w.__class__ == custom_widgets.LabelLine
101 self.showDecoration(show)
102 self._invalidate()
103 elif key == "meta s": #user wants to (un)hide group's subject
104 if self.subject:
105 self.show_title = not self.show_title
106 if self.show_title:
107 self.setSubject(self.subject)
108 else:
109 self.chat_widget.header = None
110 self._invalidate()
111
112
113 return super(Chat, self).keypress(size, key)
114
40 def setType(self, type): 115 def setType(self, type):
41 QuickChat.setType(self, type) 116 QuickChat.setType(self, type)
42 if type == 'one2one': 117 if type == 'one2one':
43 self.historyPrint(profile=self.host.profile) 118 self.historyPrint(profile=self.host.profile)
44 119 elif type == 'group':
120 if len(self.columns.widget_list) == 1:
121 present_widget = self.__buildPresentList()
122 self.present_panel = custom_widgets.VerticalSeparator(present_widget)
123 self.__appendPresentPanel()
124
125 def __getDecoration(self, widget):
126 return custom_widgets.LabelLine(widget, custom_widgets.SurroundedText(unicode(self.target)))
127
128 def showDecoration(self, show=True):
129 if show:
130 main_widget = self.__getDecoration(self.columns)
131 else:
132 main_widget = self.columns
133 self._w = main_widget
134
135
136 def __buildPresentList(self):
137 self.present_wid = custom_widgets.GenericList([],option_type = custom_widgets.UnselectableText)
138 return self.present_wid
139
140 def __appendPresentPanel(self):
141 self.columns.widget_list.append(self.present_panel)
142 self.columns.column_types.append(('weight', 2))
143
144 def __removePresentPanel(self):
145 self.columns.set_focus(0) #necessary as the focus change to the next object, we can go out of range if we are on the last object of self.columns
146 self.columns.widget_list.remove(self.present_panel)
147 del self.columns.column_types[-1]
148
45 def setSubject(self, subject): 149 def setSubject(self, subject):
46 """Set title for a group chat""" 150 """Set title for a group chat"""
47 QuickChat.setSubject(self, subject) 151 QuickChat.setSubject(self, subject)
48 self._w.base_widget.header = urwid.AttrMap(urwid.Text(unicode(subject),align='center'),'title') 152 self.subject = subject
153 self.chat_widget.header = urwid.AttrMap(urwid.Text(unicode(subject),align='center'),'title')
49 154
155 def setPresents(self, param_nicks):
156 """Set the users presents in the contact list for a group chat
157 @param nicks: list of nicknames
158 """
159 nicks = [unicode(nick) for nick in param_nicks] #FIXME: should be done in DBus bridge
160 nicks.sort()
161 QuickChat.setPresents(self, nicks)
162 self.present_wid.changeValues(nicks)
163 self.host.redraw()
164
165 def replaceUser(self, param_nick):
166 """Add user if it is not in the group list"""
167 nick = unicode(param_nick) #FIXME: should be done in DBus bridge
168 if "facebook" in nick:
169 self.host.debug()
170 QuickChat.replaceUser(self, nick)
171 presents = self.present_wid.getAllValues()
172 if nick not in presents:
173 presents.append(nick)
174 presents.sort()
175 self.present_wid.changeValues(presents)
176 self.host.redraw()
177
178 def removeUser(self, param_nick):
179 """Remove a user from the group list"""
180 nick = unicode(param_nick) #FIXME: should be done in DBus bridge
181 QuickChat.removeUser(self, nick)
182 self.present_wid.deleteValue(nick)
183 self.host.redraw()
50 184
51 def printMessage(self, from_jid, msg, profile, timestamp=""): 185 def printMessage(self, from_jid, msg, profile, timestamp=""):
52 self.content.append(SelectableText("[%s] " % from_jid + msg)) 186 assert (from_jid.__class__ == JID)
187 my_jid = self.host.profiles[profile]['whoami']
188 self.content.append(ChatText(self, timestamp or None, my_jid, from_jid, msg))
53 self.text_list.set_focus(len(self.content)-1) 189 self.text_list.set_focus(len(self.content)-1)
54 self.host.redraw() 190 self.host.redraw()