Mercurial > libervia-backend
annotate frontends/wix/chat.py @ 121:03d8bcc67182
misc documentation
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 08 Jul 2010 14:19:30 +0800 |
parents | 1ca5f254ce41 |
children | 29998cd0ed8d |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 wix: a SAT frontend | |
57 | 6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
0 | 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 | |
23 | |
24 import wx | |
25 import os.path | |
26 import pdb | |
85 | 27 from logging import debug, info, error, warning |
0 | 28 from tools.jid import JID |
29 from quick_frontend.quick_chat import QuickChat | |
72 | 30 from contact_list import ContactList |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
31 from card_game import CardPanel |
0 | 32 |
33 | |
34 idSEND = 1 | |
85 | 35 idTAROT = 2 |
0 | 36 |
37 class Chat(wx.Frame, QuickChat): | |
38 """The chat Window for one to one conversations""" | |
39 | |
72 | 40 def __init__(self, target, host, type='one2one'): |
41 wx.Frame.__init__(self, None, title=target, pos=(0,0), size=(400,200)) | |
42 QuickChat.__init__(self, target, host, type) | |
0 | 43 |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
44 self.sizer = wx.BoxSizer(wx.VERTICAL) |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
45 self.SetSizer(self.sizer) |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
46 |
72 | 47 self.splitter = wx.SplitterWindow(self, -1) |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
48 self.sizer.Add(self.splitter, 1, flag = wx.EXPAND) |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
49 |
72 | 50 self.conv_panel = wx.Panel(self.splitter) |
51 self.conv_panel.sizer = wx.BoxSizer(wx.VERTICAL) | |
76 | 52 self.subjectBox = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_READONLY) |
72 | 53 self.chatWindow = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_MULTILINE | wx.TE_RICH | wx.TE_READONLY) |
54 self.textBox = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_PROCESS_ENTER) | |
76 | 55 self.conv_panel.sizer.Add(self.subjectBox, flag=wx.EXPAND) |
72 | 56 self.conv_panel.sizer.Add(self.chatWindow, 1, flag=wx.EXPAND) |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
57 self.conv_panel.sizer.Add(self.textBox, 0, flag=wx.EXPAND) |
72 | 58 self.conv_panel.SetSizer(self.conv_panel.sizer) |
59 self.splitter.Initialize(self.conv_panel) | |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
60 self.SetMenuBar(wx.MenuBar()) |
0 | 61 |
62 #events | |
63 self.Bind(wx.EVT_CLOSE, self.onClose, self) | |
64 self.Bind(wx.EVT_TEXT_ENTER, self.onEnterPressed, self.textBox) | |
65 | |
66 #fonts | |
67 self.font={} | |
68 self.font["points"] = self.chatWindow.GetFont().GetPointSize() | |
69 self.font["family"] = self.chatWindow.GetFont().GetFamily() | |
70 | |
79 | 71 self.setType(self.type) |
72 | |
0 | 73 #misc |
74 self.textBox.SetFocus() | |
75 self.Hide() #We hide because of the show toggle | |
72 | 76 |
77 def __createPresents(self): | |
78 """Create a list of present people in a group chat""" | |
79 self.present_panel = wx.Panel(self.splitter) | |
80 self.present_panel.sizer = wx.BoxSizer(wx.VERTICAL) | |
81 self.present_panel.SetBackgroundColour(wx.BLUE) | |
82 self.present_panel.presents = ContactList(self.present_panel, self.host, type='nicks') | |
83 self.present_panel.presents.SetMinSize(wx.Size(80,20)) | |
84 self.present_panel.sizer.Add(self.present_panel.presents, 1, wx.EXPAND) | |
85 self.present_panel.SetSizer(self.present_panel.sizer) | |
86 self.splitter.SplitVertically(self.present_panel, self.conv_panel, 80) | |
87 | |
88 def setType(self, type): | |
89 QuickChat.setType(self, type) | |
90 if type is 'group' and not self.splitter.IsSplit(): | |
91 self.__createPresents() | |
76 | 92 self.subjectBox.Show() |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
93 self.__eraseMenus() |
85 | 94 self.__createMenus_group() |
79 | 95 self.historyPrint(profile=self.host.profile) |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
96 self.sizer.Layout() |
72 | 97 elif type is 'one2one' and self.splitter.IsSplit(): |
98 self.splitter.Unsplit(self.present_panel) | |
99 del self.present_panel | |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
100 self.GetMenuBar().Show() |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
101 self.subjectBox.Hide() |
85 | 102 self.__eraseMenus() |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
103 self.__createMenus_O2O() |
79 | 104 self.nick = None |
76 | 105 else: |
106 self.subjectBox.Hide() | |
85 | 107 self.__eraseMenus() |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
108 self.__createMenus_O2O() |
79 | 109 self.historyPrint(profile=self.host.profile) |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
110 |
90 | 111 def startGame(self, game_type, referee, players): |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
112 """Configure the chat window to start a game""" |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
113 if game_type=="Tarot": |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
114 debug (_("configure chat window for Tarot game")) |
90 | 115 self.tarot_panel = CardPanel(self, referee, players, self.nick) |
87 | 116 self.sizer.Prepend(self.tarot_panel, 0, flag=wx.EXPAND) |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
117 self.sizer.Layout() |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
118 self.Fit() |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
85
diff
changeset
|
119 |
87 | 120 def getGame(self, game_type): |
121 """Return class managing the game type""" | |
122 #TODO: check that the game is launched, and manage errors | |
123 if game_type=="Tarot": | |
124 return self.tarot_panel | |
125 | |
85 | 126 |
72 | 127 def setPresents(self, nicks): |
128 """Set the users presents in the contact list for a group chat | |
129 @param nicks: list of nicknames | |
130 """ | |
120 | 131 QuickChat.setPresents(self, nicks) |
72 | 132 for nick in nicks: |
85 | 133 self.present_panel.presents.replace(nick) |
87 | 134 self.occupants.add(nick) |
75 | 135 |
136 | |
137 def replaceUser(self, nick): | |
138 """Add user if it is not in the group list""" | |
139 debug (_("Replacing user %s") % nick) | |
140 if self.type != "group": | |
141 error (_("[INTERNAL] trying to replace user for a non group chat window")) | |
142 return | |
120 | 143 QuickChat.replaceUser(self, nick) |
144 | |
75 | 145 def removeUser(self, nick): |
146 """Remove a user from the group list""" | |
120 | 147 QuickChat.removeUser(self, nick) |
75 | 148 self.present_panel.presents.remove(nick) |
85 | 149 self.occupants.remove(nick) |
75 | 150 |
76 | 151 def setSubject(self, subject): |
152 """Set title for a group chat""" | |
120 | 153 QuickChat.setSubject(self, subject) |
76 | 154 self.subjectBox.SetValue(subject) |
75 | 155 |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
156 def __eraseMenus(self): |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
157 """erase all menus""" |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
158 menuBar = self.GetMenuBar() |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
159 for i in range(menuBar.GetMenuCount()): |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
160 menuBar.Remove(i) |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
161 |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
162 def __createMenus_O2O(self): |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
163 """create menu bar for one 2 one chat""" |
0 | 164 info("Creating menus") |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
165 self.__eraseMenus() |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
166 menuBar = self.GetMenuBar() |
0 | 167 actionMenu = wx.Menu() |
70 | 168 actionMenu.Append(idSEND, _("&SendFile CTRL-s"),_(" Send a file to contact")) |
169 menuBar.Append(actionMenu,_("&Action")) | |
0 | 170 |
171 #events | |
172 wx.EVT_MENU(self, idSEND, self.onSendFile) | |
173 | |
85 | 174 def __createMenus_group(self): |
175 """create menu bar for group chat""" | |
176 info("Creating menus") | |
177 self.__eraseMenus() | |
178 menuBar = self.GetMenuBar() | |
179 actionMenu = wx.Menu() | |
180 actionMenu.Append(idTAROT, _("Start &Tarot game CTRL-t"),_(" Start a Tarot card game")) #tmp | |
181 menuBar.Append(actionMenu,_("&Games")) | |
182 | |
183 #events | |
184 wx.EVT_MENU(self, idTAROT, self.onStartTarot) | |
185 | |
0 | 186 def __del__(self): |
187 wx.Frame.__del__(self) | |
188 | |
189 def onClose(self, event): | |
190 """Close event: we only hide the frame.""" | |
191 event.Veto() | |
192 self.Show() ## this is a workaround to a wxpython bug: | |
193 ## with Raise on hidden frame, Hide doesn't work anymore | |
194 ## TODO: check this and repport bug to wxpython devs | |
195 self.Hide() | |
196 | |
197 def onEnterPressed(self, event): | |
198 """Behaviour when enter pressed in send line.""" | |
77
1ae680f9682e
wix: MUC groupchat management + short nick shown in chat window instead of full jid when possible
Goffi <goffi@goffi.org>
parents:
76
diff
changeset
|
199 self.host.bridge.sendMessage(self.target.short if self.type=='group' else self.target, |
1ae680f9682e
wix: MUC groupchat management + short nick shown in chat window instead of full jid when possible
Goffi <goffi@goffi.org>
parents:
76
diff
changeset
|
200 event.GetString(), |
1ae680f9682e
wix: MUC groupchat management + short nick shown in chat window instead of full jid when possible
Goffi <goffi@goffi.org>
parents:
76
diff
changeset
|
201 type = "groupchat" if self.type=='group' else "chat", |
1ae680f9682e
wix: MUC groupchat management + short nick shown in chat window instead of full jid when possible
Goffi <goffi@goffi.org>
parents:
76
diff
changeset
|
202 profile_key=self.host.profile) |
0 | 203 self.textBox.Clear() |
204 | |
205 | |
206 | |
66
8147b4f40809
SàT: multi-profile: DBus signals and frontend adaptation (first draft)
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
207 def printMessage(self, from_jid, msg, profile, timestamp=""): |
0 | 208 """Print the message with differents colors depending on where it comes from.""" |
209 jid=JID(from_jid) | |
79 | 210 print "printMessage, jid=",jid,"type=",self.type |
80
9681f18d06bd
wix: basic dialog to join MUC room, jid node is now displayed in conversations instead of full jid
Goffi <goffi@goffi.org>
parents:
79
diff
changeset
|
211 nick = jid.resource if self.type == "group" else (self.host.CM.getAttr(jid,'nick') or self.host.CM.getAttr(jid,'name') or jid.node) |
79 | 212 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 |
0 | 213 _font = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.BOLD) |
214 self.chatWindow.SetDefaultStyle(wx.TextAttr( "BLACK" if mymess else "BLUE", font=_font)) | |
77
1ae680f9682e
wix: MUC groupchat management + short nick shown in chat window instead of full jid when possible
Goffi <goffi@goffi.org>
parents:
76
diff
changeset
|
215 self.chatWindow.AppendText("[%s] " % nick) |
0 | 216 _font = wx.Font(self.font["points"], self.font["family"], wx.ITALIC if mymess else wx.NORMAL, wx.NORMAL) |
217 self.chatWindow.SetDefaultStyle(wx.TextAttr("BLACK", font=_font)) | |
218 self.chatWindow.AppendText("%s\n" % msg) | |
219 if not mymess: | |
120 | 220 #TODO: use notification system |
221 self.RequestUserAttention() #FIXME: do this only if in background. | |
222 self.Show() #gof: FIXME: to check | |
223 #self.Raise() #FIXME: too intrusive | |
0 | 224 |
225 ### events ### | |
226 | |
227 def onSendFile(self, e): | |
70 | 228 debug(_("Send File")) |
229 filename = wx.FileSelector(_("Choose a file to send"), flags = wx.FD_FILE_MUST_EXIST) | |
0 | 230 if filename: |
70 | 231 debug(_("filename: %s"),filename) |
72 | 232 full_jid = self.host.CM.get_full(self.target) |
0 | 233 id = self.host.bridge.sendFile(full_jid, filename) |
70 | 234 self.host.waitProgress(id, _("File Transfer"), _("Copying %s") % os.path.basename(filename)) |
0 | 235 |
85 | 236 def onStartTarot(self, e): |
237 debug (_("Starting Tarot game")) | |
238 warning (_("FIXME: temporary menu, must be changed")) | |
87 | 239 if len(self.occupants) != 4: |
240 err_dlg = wx.MessageDialog(self, _("You need to be exactly 4 peoples in the room to start a Tarot game"), _("Can't start game"), style = wx.OK | wx.ICON_ERROR) #FIXME: gof: temporary only, need to choose the people with who the game has to be started | |
241 err_dlg.ShowModal() | |
242 else: | |
90 | 243 self.host.bridge.tarotGameCreate(self.id, list(self.occupants), self.host.profile) |