Mercurial > libervia-backend
annotate frontends/wix/chat.py @ 85:fc7583282d40
Tarot Game plugin: first draft
- SàT: beginning of the plugin
- Tarot Plugin: 1 method createTarotGame et 1 signal tarotGameStarted
- wix: added "Game" menu in group chat to start Tarot game
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 07 May 2010 17:09:30 +0930 |
parents | 9681f18d06bd |
children | 4b5f2d55b6ac |
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 |
0 | 31 |
32 | |
33 idSEND = 1 | |
85 | 34 idTAROT = 2 |
0 | 35 |
36 class Chat(wx.Frame, QuickChat): | |
37 """The chat Window for one to one conversations""" | |
38 | |
72 | 39 def __init__(self, target, host, type='one2one'): |
40 wx.Frame.__init__(self, None, title=target, pos=(0,0), size=(400,200)) | |
41 QuickChat.__init__(self, target, host, type) | |
0 | 42 |
72 | 43 self.splitter = wx.SplitterWindow(self, -1) |
44 | |
45 self.conv_panel = wx.Panel(self.splitter) | |
46 self.conv_panel.sizer = wx.BoxSizer(wx.VERTICAL) | |
76 | 47 self.subjectBox = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_READONLY) |
72 | 48 self.chatWindow = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_MULTILINE | wx.TE_RICH | wx.TE_READONLY) |
49 self.textBox = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_PROCESS_ENTER) | |
76 | 50 self.conv_panel.sizer.Add(self.subjectBox, flag=wx.EXPAND) |
72 | 51 self.conv_panel.sizer.Add(self.chatWindow, 1, flag=wx.EXPAND) |
52 self.conv_panel.sizer.Add(self.textBox, flag=wx.EXPAND) | |
53 self.conv_panel.SetSizer(self.conv_panel.sizer) | |
54 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
|
55 self.SetMenuBar(wx.MenuBar()) |
0 | 56 |
57 #events | |
58 self.Bind(wx.EVT_CLOSE, self.onClose, self) | |
59 self.Bind(wx.EVT_TEXT_ENTER, self.onEnterPressed, self.textBox) | |
60 | |
61 #fonts | |
62 self.font={} | |
63 self.font["points"] = self.chatWindow.GetFont().GetPointSize() | |
64 self.font["family"] = self.chatWindow.GetFont().GetFamily() | |
65 | |
79 | 66 self.setType(self.type) |
67 | |
0 | 68 #misc |
69 self.textBox.SetFocus() | |
70 self.Hide() #We hide because of the show toggle | |
72 | 71 |
72 def __createPresents(self): | |
73 """Create a list of present people in a group chat""" | |
74 self.present_panel = wx.Panel(self.splitter) | |
75 self.present_panel.sizer = wx.BoxSizer(wx.VERTICAL) | |
76 self.present_panel.SetBackgroundColour(wx.BLUE) | |
77 self.present_panel.presents = ContactList(self.present_panel, self.host, type='nicks') | |
78 self.present_panel.presents.SetMinSize(wx.Size(80,20)) | |
79 self.present_panel.sizer.Add(self.present_panel.presents, 1, wx.EXPAND) | |
80 self.present_panel.SetSizer(self.present_panel.sizer) | |
81 self.splitter.SplitVertically(self.present_panel, self.conv_panel, 80) | |
82 | |
83 def setType(self, type): | |
84 QuickChat.setType(self, type) | |
85 if type is 'group' and not self.splitter.IsSplit(): | |
86 self.__createPresents() | |
76 | 87 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
|
88 self.__eraseMenus() |
85 | 89 self.__createMenus_group() |
79 | 90 self.historyPrint(profile=self.host.profile) |
72 | 91 elif type is 'one2one' and self.splitter.IsSplit(): |
92 self.splitter.Unsplit(self.present_panel) | |
93 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
|
94 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
|
95 self.subjectBox.Hide() |
85 | 96 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
|
97 self.__createMenus_O2O() |
79 | 98 self.nick = None |
76 | 99 else: |
100 self.subjectBox.Hide() | |
85 | 101 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
|
102 self.__createMenus_O2O() |
79 | 103 self.historyPrint(profile=self.host.profile) |
85 | 104 |
72 | 105 def setPresents(self, nicks): |
106 """Set the users presents in the contact list for a group chat | |
107 @param nicks: list of nicknames | |
108 """ | |
109 debug (_("Adding users %s to room") % nicks) | |
110 if self.type != "group": | |
111 error (_("[INTERNAL] trying to set presents nicks for a non group chat window")) | |
112 return | |
113 for nick in nicks: | |
85 | 114 self.present_panel.presents.replace(nick) |
115 if nick != self.nick: | |
116 self.occupants.add(nick) | |
75 | 117 |
118 | |
119 def replaceUser(self, nick): | |
120 """Add user if it is not in the group list""" | |
121 debug (_("Replacing user %s") % nick) | |
122 if self.type != "group": | |
123 error (_("[INTERNAL] trying to replace user for a non group chat window")) | |
124 return | |
125 self.present_panel.presents.replace(nick) | |
85 | 126 if nick != self.nick: |
127 self.occupants.add(nick) | |
75 | 128 |
129 def removeUser(self, nick): | |
130 """Remove a user from the group list""" | |
131 debug(_("Removing user %s") % nick) | |
132 if self.type != "group": | |
133 error (_("[INTERNAL] trying to remove user for a non group chat window")) | |
134 return | |
135 self.present_panel.presents.remove(nick) | |
85 | 136 self.occupants.remove(nick) |
75 | 137 |
76 | 138 def setSubject(self, subject): |
139 """Set title for a group chat""" | |
140 debug(_("Setting subject to %s") % subject) | |
141 if self.type != "group": | |
142 error (_("[INTERNAL] trying to set subject for a non group chat window")) | |
143 return | |
144 self.subjectBox.SetValue(subject) | |
75 | 145 |
72 | 146 |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
147 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
|
148 """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
|
149 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
|
150 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
|
151 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
|
152 |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
153 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
|
154 """create menu bar for one 2 one chat""" |
0 | 155 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
|
156 self.__eraseMenus() |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
157 menuBar = self.GetMenuBar() |
0 | 158 actionMenu = wx.Menu() |
70 | 159 actionMenu.Append(idSEND, _("&SendFile CTRL-s"),_(" Send a file to contact")) |
160 menuBar.Append(actionMenu,_("&Action")) | |
0 | 161 |
162 #events | |
163 wx.EVT_MENU(self, idSEND, self.onSendFile) | |
164 | |
85 | 165 def __createMenus_group(self): |
166 """create menu bar for group chat""" | |
167 info("Creating menus") | |
168 self.__eraseMenus() | |
169 menuBar = self.GetMenuBar() | |
170 actionMenu = wx.Menu() | |
171 actionMenu.Append(idTAROT, _("Start &Tarot game CTRL-t"),_(" Start a Tarot card game")) #tmp | |
172 menuBar.Append(actionMenu,_("&Games")) | |
173 | |
174 #events | |
175 wx.EVT_MENU(self, idTAROT, self.onStartTarot) | |
176 | |
0 | 177 def __del__(self): |
178 wx.Frame.__del__(self) | |
179 | |
180 def onClose(self, event): | |
181 """Close event: we only hide the frame.""" | |
182 event.Veto() | |
183 self.Show() ## this is a workaround to a wxpython bug: | |
184 ## with Raise on hidden frame, Hide doesn't work anymore | |
185 ## TODO: check this and repport bug to wxpython devs | |
186 self.Hide() | |
187 | |
188 def onEnterPressed(self, event): | |
189 """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
|
190 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
|
191 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
|
192 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
|
193 profile_key=self.host.profile) |
0 | 194 self.textBox.Clear() |
195 | |
196 | |
197 | |
66
8147b4f40809
SàT: multi-profile: DBus signals and frontend adaptation (first draft)
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
198 def printMessage(self, from_jid, msg, profile, timestamp=""): |
0 | 199 """Print the message with differents colors depending on where it comes from.""" |
200 jid=JID(from_jid) | |
79 | 201 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
|
202 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 | 203 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 | 204 _font = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.BOLD) |
205 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
|
206 self.chatWindow.AppendText("[%s] " % nick) |
0 | 207 _font = wx.Font(self.font["points"], self.font["family"], wx.ITALIC if mymess else wx.NORMAL, wx.NORMAL) |
208 self.chatWindow.SetDefaultStyle(wx.TextAttr("BLACK", font=_font)) | |
209 self.chatWindow.AppendText("%s\n" % msg) | |
210 if not mymess: | |
211 self.Raise() #FIXME: too intrusive | |
212 | |
213 ### events ### | |
214 | |
215 def onSendFile(self, e): | |
70 | 216 debug(_("Send File")) |
217 filename = wx.FileSelector(_("Choose a file to send"), flags = wx.FD_FILE_MUST_EXIST) | |
0 | 218 if filename: |
70 | 219 debug(_("filename: %s"),filename) |
72 | 220 full_jid = self.host.CM.get_full(self.target) |
0 | 221 id = self.host.bridge.sendFile(full_jid, filename) |
70 | 222 self.host.waitProgress(id, _("File Transfer"), _("Copying %s") % os.path.basename(filename)) |
0 | 223 |
85 | 224 def onStartTarot(self, e): |
225 debug (_("Starting Tarot game")) | |
226 warning (_("FIXME: temporary menu, must be changed")) | |
227 self.host.bridge.createTarotGame(self.id, list(self.occupants), self.host.profile) |