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