comparison frontends/src/primitivus/chat.py @ 223:86d249b6d9b7

Files reorganisation
author Goffi <goffi@goffi.org>
date Wed, 29 Dec 2010 01:06:29 +0100
parents frontends/primitivus/chat.py@3198bfd66daa
children fd9b7834d98a
comparison
equal deleted inserted replaced
222:3198bfd66daa 223:86d249b6d9b7
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 Primitivus: a SAT frontend
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 import urwid
23 from quick_frontend.quick_contact_list import QuickContactList
24 from quick_frontend.quick_chat import QuickChat
25 from urwid_satext import sat_widgets
26 import time
27 from tools.jid import JID
28 from card_game import CardGame
29 from urwid_satext.files_management import FileDialog
30
31
32 class ChatText(urwid.FlowWidget):
33 """Manage the printing of chat message"""
34
35 def __init__(self, parent, timestamp, nick, my_mess, message, align='left'):
36 self.parent = parent
37 self.timestamp = time.localtime(timestamp)
38 self.nick = nick
39 self.my_mess = my_mess
40 self.message = unicode(message)
41 self.align = align
42
43 def selectable(self):
44 return True
45
46 def keypress(self, size, key):
47 return key
48
49 def rows(self,size,focus=False):
50 return self.display_widget(size, focus).rows(size, focus)
51
52 def render(self, size, focus=False):
53 canvas = urwid.CompositeCanvas(self.display_widget(size, focus).render(size, focus))
54 if focus:
55 canvas.set_cursor(self.get_cursor_coords(size))
56 return canvas
57
58 def get_cursor_coords(self, size):
59 #(maxcol,) = size
60 return 0, 0
61
62 def display_widget(self, size, focus):
63 render_txt = []
64 if self.parent.show_timestamp:
65 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
66 render_txt.append(('date',"[%s]" % time.strftime(time_format, self.timestamp).decode('utf-8')))
67 if self.parent.show_short_nick:
68 render_txt.append(('my_nick' if self.my_mess else 'other_nick',"**" if self.my_mess else "*"))
69 else:
70 render_txt.append(('my_nick' if self.my_mess else 'other_nick',"[%s] " % self.nick))
71 render_txt.append(self.message)
72 return urwid.Text(render_txt, align=self.align)
73
74 class Chat(urwid.WidgetWrap, QuickChat):
75
76 def __init__(self, target, host, type='one2one'):
77 self.target = target
78 QuickChat.__init__(self, target, host, type)
79 self.content = urwid.SimpleListWalker([])
80 self.text_list = urwid.ListBox(self.content)
81 self.chat_widget = urwid.Frame(self.text_list)
82 self.chat_colums = urwid.Columns([('weight', 8, self.chat_widget)])
83 self.chat_colums = urwid.Columns([('weight', 8, self.chat_widget)])
84 self.pile = urwid.Pile([self.chat_colums])
85 urwid.WidgetWrap.__init__(self, self.__getDecoration(self.pile))
86 self.setType(type)
87 self.day_change = time.strptime(time.strftime("%a %b %d 00:00:00 %Y")) #struct_time of day changing time
88 self.show_timestamp = True
89 self.show_short_nick = False
90 self.show_title = 1 #0: clip title; 1: full title; 2: no title
91 self.subject = None
92
93 def keypress(self, size, key):
94 if key == "meta p": #user wants to (un)hide the presents panel
95 if self.type == 'group':
96 widgets = self.chat_colums.widget_list
97 if self.present_panel in widgets:
98 self.__removePresentPanel()
99 else:
100 self.__appendPresentPanel()
101 elif key == "meta t": #user wants to (un)hide timestamp
102 self.show_timestamp = not self.show_timestamp
103 for wid in self.content:
104 wid._invalidate()
105 elif key == "meta n": #user wants to (not) use short nick
106 self.show_short_nick = not self.show_short_nick
107 for wid in self.content:
108 wid._invalidate()
109 elif key == "meta l": #user wants to (un)hide widget decoration
110 show = not isinstance(self._w, sat_widgets.LabelLine)
111 self.showDecoration(show)
112 self._invalidate()
113 elif key == "meta s": #user wants to (un)hide group's subject or change its apperance
114 if self.subject:
115 self.show_title = (self.show_title + 1) % 3
116 if self.show_title == 0:
117 self.setSubject(self.subject,'clip')
118 elif self.show_title == 1:
119 self.setSubject(self.subject,'space')
120 elif self.show_title == 2:
121 self.chat_widget.header = None
122 self._invalidate()
123
124
125 return super(Chat, self).keypress(size, key)
126
127 def getMenu(self):
128 """Return Menu bar"""
129 menu = sat_widgets.Menu(self.host.loop)
130 if self.type == 'group':
131 game = _("Game")
132 menu.addMenu(game, "Tarot", self.onTarotRequest)
133 elif self.type == 'one2one':
134 menu.addMenu(_("Action"), _("Send file"), self.onSendFileRequest)
135 return menu
136
137 def setType(self, type):
138 QuickChat.setType(self, type)
139 if type == 'one2one':
140 self.historyPrint(profile=self.host.profile)
141 elif type == 'group':
142 if len(self.chat_colums.widget_list) == 1:
143 present_widget = self.__buildPresentList()
144 self.present_panel = sat_widgets.VerticalSeparator(present_widget)
145 self.__appendPresentPanel()
146
147 def __getDecoration(self, widget):
148 return sat_widgets.LabelLine(widget, sat_widgets.SurroundedText(unicode(self.target)))
149
150 def showDecoration(self, show=True):
151 """Show/Hide the decoration around the chat window"""
152 if show:
153 main_widget = self.__getDecoration(self.pile)
154 else:
155 main_widget = self.pile
156 self._w = main_widget
157
158
159 def __buildPresentList(self):
160 self.present_wid = sat_widgets.GenericList([],option_type = sat_widgets.ClickableText)
161 return self.present_wid
162
163 def __appendPresentPanel(self):
164 self.chat_colums.widget_list.append(self.present_panel)
165 self.chat_colums.column_types.append(('weight', 2))
166
167 def __removePresentPanel(self):
168 self.chat_colums.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.chat_colums
169 self.chat_colums.widget_list.remove(self.present_panel)
170 del self.chat_colums.column_types[-1]
171
172 def __appendGamePanel(self, widget):
173 assert (len(self.pile.widget_list) == 1)
174 self.pile.widget_list.insert(0,widget)
175 self.pile.item_types.insert(0,('weight', 1))
176 self.pile.widget_list.insert(1,urwid.Filler(urwid.Divider('-')))
177 self.pile.item_types.insert(1,('fixed', 1))
178 self.host.redraw()
179
180 def __removeGamePanel(self):
181 assert (len(self.pile.widget_list) == 3)
182 self.pile.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.chat_colums
183 del self.pile.widget_list[0]
184 del self.pile.item_types[0]
185 self.host.redraw()
186
187 def setSubject(self, subject, wrap='space'):
188 """Set title for a group chat"""
189 QuickChat.setSubject(self, subject)
190 self.subject = subject
191 self.subj_wid = urwid.Text(unicode(subject.replace('\n','|') if wrap == 'clip' else subject ),
192 align='left' if wrap=='clip' else 'center',wrap=wrap)
193 self.chat_widget.header = urwid.AttrMap(self.subj_wid,'title')
194 self.host.redraw()
195
196 def setPresents(self, param_nicks):
197 """Set the users presents in the contact list for a group chat
198 @param nicks: list of nicknames
199 """
200 nicks = [unicode(nick) for nick in param_nicks] #FIXME: should be done in DBus bridge
201 nicks.sort()
202 QuickChat.setPresents(self, nicks)
203 self.present_wid.changeValues(nicks)
204 self.host.redraw()
205
206 def replaceUser(self, param_nick):
207 """Add user if it is not in the group list"""
208 nick = unicode(param_nick) #FIXME: should be done in DBus bridge
209 if "facebook" in nick:
210 self.host.debug()
211 QuickChat.replaceUser(self, nick)
212 presents = self.present_wid.getAllValues()
213 if nick not in presents:
214 presents.append(nick)
215 presents.sort()
216 self.present_wid.changeValues(presents)
217 self.host.redraw()
218
219 def removeUser(self, param_nick):
220 """Remove a user from the group list"""
221 nick = unicode(param_nick) #FIXME: should be done in DBus bridge
222 QuickChat.removeUser(self, nick)
223 self.present_wid.deleteValue(nick)
224 self.host.redraw()
225
226 def printMessage(self, from_jid, msg, profile, timestamp=""):
227 assert isinstance(from_jid, JID)
228 try:
229 jid,nick,mymess = QuickChat.printMessage(self, from_jid, msg, profile, timestamp)
230 except TypeError:
231 return
232 my_jid = self.host.profiles[profile]['whoami']
233 self.content.append(ChatText(self, timestamp or None, nick, mymess, msg))
234 self.text_list.set_focus(len(self.content)-1)
235 self.host.redraw()
236
237 def printInfo(self, msg, type='normal'):
238 """Print general info
239 @param msg: message to print
240 @type: one of:
241 normal: general info like "toto has joined the room"
242 me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
243 """
244 self.content.append(sat_widgets.ClickableText(msg))
245 self.text_list.set_focus(len(self.content)-1)
246 self.host.redraw()
247
248 def startGame(self, game_type, referee, players):
249 """Configure the chat window to start a game"""
250 if game_type=="Tarot":
251 try:
252 self.tarot_wid = CardGame(self, referee, players, self.nick)
253 self.__appendGamePanel(self.tarot_wid)
254 except e:
255 self.host.debug()
256
257 def getGame(self, game_type):
258 """Return class managing the game type"""
259 #TODO: check that the game is launched, and manage errors
260 if game_type=="Tarot":
261 return self.tarot_wid
262
263 #MENU EVENTS#
264 def onTarotRequest(self, menu):
265 if len(self.occupants) != 4:
266 self.host.showPopUp(sat_widgets.Alert(_("Can't start game"), _("You need to be exactly 4 peoples in the room to start a Tarot game"), ok_cb=self.host.removePopUp))
267 else:
268 self.host.bridge.tarotGameCreate(self.id, list(self.occupants), self.host.profile)
269
270 def onSendFileRequest(self, menu):
271 dialog = FileDialog(ok_cb=self.onFileSelected, cancel_cb=self.host.removePopUp)
272 self.host.showPopUp(dialog, 80, 80)
273
274 #MISC EVENTS#
275 def onFileSelected(self, filepath):
276 self.host.removePopUp()
277 full_jid = self.host.CM.get_full(self.target)
278 id = self.host.bridge.sendFile(full_jid, filepath)
279 self.host.addProgress(id,filepath)