Mercurial > libervia-backend
annotate frontends/wix/chat.py @ 88:59f181e8433a
Tarot
SàT: added forgotten file Tarot plugin
plugin Tarot: distribution first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 12 May 2010 11:55:18 +0930 |
parents | 66d784082930 |
children | 4020931569b8 |
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 |
87 | 111 def startGame(self, game_type, 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")) |
87 | 115 self.tarot_panel = CardPanel(self, players, self.nick) |
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 """ | |
131 debug (_("Adding users %s to room") % nicks) | |
132 if self.type != "group": | |
133 error (_("[INTERNAL] trying to set presents nicks for a non group chat window")) | |
134 return | |
135 for nick in nicks: | |
85 | 136 self.present_panel.presents.replace(nick) |
87 | 137 self.occupants.add(nick) |
75 | 138 |
139 | |
140 def replaceUser(self, nick): | |
141 """Add user if it is not in the group list""" | |
142 debug (_("Replacing user %s") % nick) | |
143 if self.type != "group": | |
144 error (_("[INTERNAL] trying to replace user for a non group chat window")) | |
145 return | |
146 self.present_panel.presents.replace(nick) | |
87 | 147 self.occupants.add(nick) |
75 | 148 |
149 def removeUser(self, nick): | |
150 """Remove a user from the group list""" | |
151 debug(_("Removing user %s") % nick) | |
152 if self.type != "group": | |
153 error (_("[INTERNAL] trying to remove user for a non group chat window")) | |
154 return | |
155 self.present_panel.presents.remove(nick) | |
85 | 156 self.occupants.remove(nick) |
75 | 157 |
76 | 158 def setSubject(self, subject): |
159 """Set title for a group chat""" | |
160 debug(_("Setting subject to %s") % subject) | |
161 if self.type != "group": | |
162 error (_("[INTERNAL] trying to set subject for a non group chat window")) | |
163 return | |
164 self.subjectBox.SetValue(subject) | |
75 | 165 |
72 | 166 |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
167 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
|
168 """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
|
169 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
|
170 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
|
171 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
|
172 |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
173 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
|
174 """create menu bar for one 2 one chat""" |
0 | 175 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
|
176 self.__eraseMenus() |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
177 menuBar = self.GetMenuBar() |
0 | 178 actionMenu = wx.Menu() |
70 | 179 actionMenu.Append(idSEND, _("&SendFile CTRL-s"),_(" Send a file to contact")) |
180 menuBar.Append(actionMenu,_("&Action")) | |
0 | 181 |
182 #events | |
183 wx.EVT_MENU(self, idSEND, self.onSendFile) | |
184 | |
85 | 185 def __createMenus_group(self): |
186 """create menu bar for group chat""" | |
187 info("Creating menus") | |
188 self.__eraseMenus() | |
189 menuBar = self.GetMenuBar() | |
190 actionMenu = wx.Menu() | |
191 actionMenu.Append(idTAROT, _("Start &Tarot game CTRL-t"),_(" Start a Tarot card game")) #tmp | |
192 menuBar.Append(actionMenu,_("&Games")) | |
193 | |
194 #events | |
195 wx.EVT_MENU(self, idTAROT, self.onStartTarot) | |
196 | |
0 | 197 def __del__(self): |
198 wx.Frame.__del__(self) | |
199 | |
200 def onClose(self, event): | |
201 """Close event: we only hide the frame.""" | |
202 event.Veto() | |
203 self.Show() ## this is a workaround to a wxpython bug: | |
204 ## with Raise on hidden frame, Hide doesn't work anymore | |
205 ## TODO: check this and repport bug to wxpython devs | |
206 self.Hide() | |
207 | |
208 def onEnterPressed(self, event): | |
209 """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
|
210 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
|
211 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
|
212 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
|
213 profile_key=self.host.profile) |
0 | 214 self.textBox.Clear() |
215 | |
216 | |
217 | |
66
8147b4f40809
SàT: multi-profile: DBus signals and frontend adaptation (first draft)
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
218 def printMessage(self, from_jid, msg, profile, timestamp=""): |
0 | 219 """Print the message with differents colors depending on where it comes from.""" |
220 jid=JID(from_jid) | |
79 | 221 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
|
222 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 | 223 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 | 224 _font = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.BOLD) |
225 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
|
226 self.chatWindow.AppendText("[%s] " % nick) |
0 | 227 _font = wx.Font(self.font["points"], self.font["family"], wx.ITALIC if mymess else wx.NORMAL, wx.NORMAL) |
228 self.chatWindow.SetDefaultStyle(wx.TextAttr("BLACK", font=_font)) | |
229 self.chatWindow.AppendText("%s\n" % msg) | |
230 if not mymess: | |
231 self.Raise() #FIXME: too intrusive | |
232 | |
233 ### events ### | |
234 | |
235 def onSendFile(self, e): | |
70 | 236 debug(_("Send File")) |
237 filename = wx.FileSelector(_("Choose a file to send"), flags = wx.FD_FILE_MUST_EXIST) | |
0 | 238 if filename: |
70 | 239 debug(_("filename: %s"),filename) |
72 | 240 full_jid = self.host.CM.get_full(self.target) |
0 | 241 id = self.host.bridge.sendFile(full_jid, filename) |
70 | 242 self.host.waitProgress(id, _("File Transfer"), _("Copying %s") % os.path.basename(filename)) |
0 | 243 |
85 | 244 def onStartTarot(self, e): |
245 debug (_("Starting Tarot game")) | |
246 warning (_("FIXME: temporary menu, must be changed")) | |
87 | 247 if len(self.occupants) != 4: |
248 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 | |
249 err_dlg.ShowModal() | |
250 else: | |
251 self.host.bridge.createTarotGame(self.id, list(self.occupants), self.host.profile) |