Mercurial > libervia-backend
annotate frontends/primitivus/chat.py @ 126:1b33b681ebd8
Primitivus: sexy nick in chat window
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 12 Jul 2010 18:54:55 +0800 |
parents | 8d611eb9ae48 |
children | 5a88ad24ccc0 |
rev | line source |
---|---|
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 Primitivus: a SAT frontend |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU General Public License for more details. |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 import urwid |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from quick_frontend.quick_contact_list import QuickContactList |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from quick_frontend.quick_chat import QuickChat |
124 | 25 import custom_widgets |
26 import time | |
27 from tools.jid import JID | |
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 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
124 | 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): | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
51 canvas = urwid.CompositeCanvas(self.display_widget(size, focus).render(size, focus)) |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
52 if focus: |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
53 canvas.set_cursor(self.get_cursor_coords(size)) |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
54 return canvas |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
55 |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
56 def get_cursor_coords(self, size): |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
57 #(maxcol,) = size |
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
58 return 0, 0 |
124 | 59 |
60 def display_widget(self, size, focus): | |
61 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 | |
62 render_txt = [] | |
63 if self.parent.show_timestamp: | |
64 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 | |
65 render_txt.append(('date',"[%s]" % time.strftime(time_format, self.timestamp))) | |
66 if self.parent.show_short_nick: | |
67 render_txt.append(('my_nick' if my_mess else 'other_nick',"**" if my_mess else "*")) | |
68 else: | |
126
1b33b681ebd8
Primitivus: sexy nick in chat window
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
69 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) |
1b33b681ebd8
Primitivus: sexy nick in chat window
Goffi <goffi@goffi.org>
parents:
125
diff
changeset
|
70 render_txt.append(('my_nick' if my_mess else 'other_nick',"[%s] " % nick)) |
124 | 71 render_txt.append(self.message) |
72 return urwid.Text(render_txt, align=self.align) | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 class Chat(urwid.WidgetWrap, QuickChat): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 def __init__(self, target, host, type='one2one'): |
124 | 77 self.target = target |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 QuickChat.__init__(self, target, host, type) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 self.content = urwid.SimpleListWalker([]) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 self.text_list = urwid.ListBox(self.content) |
124 | 81 self.chat_widget = urwid.Frame(self.text_list) |
82 self.columns = urwid.Columns([('weight', 8, self.chat_widget)]) | |
83 urwid.WidgetWrap.__init__(self, self.__getDecoration(self.columns)) | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 self.setType(type) |
124 | 85 self.day_change = time.strptime(time.strftime("%a %b %d 00:00:00 %Y")) #struct_time of day changing time |
86 self.show_timestamp = True | |
87 self.show_short_nick = False | |
88 self.show_title = True | |
89 self.subject = None | |
90 | |
91 def keypress(self, size, key): | |
92 if key == "meta p": #user wants to (un)hide the presents panel | |
93 if self.type == 'group': | |
94 widgets = self.columns.widget_list | |
95 if self.present_panel in widgets: | |
96 self.__removePresentPanel() | |
97 else: | |
98 self.__appendPresentPanel() | |
99 elif key == "meta t": #user wants to (un)hide timestamp | |
100 self.show_timestamp = not self.show_timestamp | |
101 for wid in self.content: | |
102 wid._invalidate() | |
103 elif key == "meta n": #user wants to (not) use short nick | |
104 self.show_short_nick = not self.show_short_nick | |
105 for wid in self.content: | |
106 wid._invalidate() | |
107 elif key == "meta l": #user wants to (un)hide widget decoration | |
108 show = not self._w.__class__ == custom_widgets.LabelLine | |
109 self.showDecoration(show) | |
110 self._invalidate() | |
111 elif key == "meta s": #user wants to (un)hide group's subject | |
112 if self.subject: | |
113 self.show_title = not self.show_title | |
114 if self.show_title: | |
115 self.setSubject(self.subject) | |
116 else: | |
117 self.chat_widget.header = None | |
118 self._invalidate() | |
119 | |
120 | |
121 return super(Chat, self).keypress(size, key) | |
122 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 def setType(self, type): |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 QuickChat.setType(self, type) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 if type == 'one2one': |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 self.historyPrint(profile=self.host.profile) |
124 | 127 elif type == 'group': |
128 if len(self.columns.widget_list) == 1: | |
129 present_widget = self.__buildPresentList() | |
130 self.present_panel = custom_widgets.VerticalSeparator(present_widget) | |
131 self.__appendPresentPanel() | |
132 | |
133 def __getDecoration(self, widget): | |
134 return custom_widgets.LabelLine(widget, custom_widgets.SurroundedText(unicode(self.target))) | |
135 | |
136 def showDecoration(self, show=True): | |
137 if show: | |
138 main_widget = self.__getDecoration(self.columns) | |
139 else: | |
140 main_widget = self.columns | |
141 self._w = main_widget | |
142 | |
143 | |
144 def __buildPresentList(self): | |
125
8d611eb9ae48
primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents:
124
diff
changeset
|
145 self.present_wid = custom_widgets.GenericList([],option_type = custom_widgets.ClickableText) |
124 | 146 return self.present_wid |
147 | |
148 def __appendPresentPanel(self): | |
149 self.columns.widget_list.append(self.present_panel) | |
150 self.columns.column_types.append(('weight', 2)) | |
151 | |
152 def __removePresentPanel(self): | |
153 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 | |
154 self.columns.widget_list.remove(self.present_panel) | |
155 del self.columns.column_types[-1] | |
156 | |
120 | 157 def setSubject(self, subject): |
158 """Set title for a group chat""" | |
159 QuickChat.setSubject(self, subject) | |
124 | 160 self.subject = subject |
161 self.chat_widget.header = urwid.AttrMap(urwid.Text(unicode(subject),align='center'),'title') | |
162 | |
163 def setPresents(self, param_nicks): | |
164 """Set the users presents in the contact list for a group chat | |
165 @param nicks: list of nicknames | |
166 """ | |
167 nicks = [unicode(nick) for nick in param_nicks] #FIXME: should be done in DBus bridge | |
168 nicks.sort() | |
169 QuickChat.setPresents(self, nicks) | |
170 self.present_wid.changeValues(nicks) | |
171 self.host.redraw() | |
120 | 172 |
124 | 173 def replaceUser(self, param_nick): |
174 """Add user if it is not in the group list""" | |
175 nick = unicode(param_nick) #FIXME: should be done in DBus bridge | |
176 if "facebook" in nick: | |
177 self.host.debug() | |
178 QuickChat.replaceUser(self, nick) | |
179 presents = self.present_wid.getAllValues() | |
180 if nick not in presents: | |
181 presents.append(nick) | |
182 presents.sort() | |
183 self.present_wid.changeValues(presents) | |
184 self.host.redraw() | |
185 | |
186 def removeUser(self, param_nick): | |
187 """Remove a user from the group list""" | |
188 nick = unicode(param_nick) #FIXME: should be done in DBus bridge | |
189 QuickChat.removeUser(self, nick) | |
190 self.present_wid.deleteValue(nick) | |
191 self.host.redraw() | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 def printMessage(self, from_jid, msg, profile, timestamp=""): |
124 | 194 assert (from_jid.__class__ == JID) |
195 my_jid = self.host.profiles[profile]['whoami'] | |
196 self.content.append(ChatText(self, timestamp or None, my_jid, from_jid, msg)) | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 self.text_list.set_focus(len(self.content)-1) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 self.host.redraw() |