annotate frontends/wix/chat.py @ 72:f271fff3a713

MUC implementation: first draft /!\ the experimental muc branche of wokkel must be used - bridge: new roomJoined signal - wix: contact list widget is now in a separate file, and manage different kinds of presentation - wix: chat window now manage group chat (first draft, not working yet) - wix: constants are now in a separate class, so then can be accessible from everywhere - wix: new menu to join room (do nothing yet, except entering in a test room) - new plugin for xep 0045 (MUC), use wokkel experimental MUC branch - plugins: the profile is now given for get_handler, cause it can be used internally by a plugin (e.g.: xep-0045 plugin)
author Goffi <goffi@goffi.org>
date Sun, 21 Mar 2010 10:28:55 +1100
parents 8f2ed279784b
children 7322a41f8a8e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #!/usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
goffi@necton2
parents:
diff changeset
4 """
goffi@necton2
parents:
diff changeset
5 wix: a SAT frontend
57
a5b5fb5fc9fd updated README and copyright note
Goffi <goffi@goffi.org>
parents: 0
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
7
goffi@necton2
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
goffi@necton2
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
goffi@necton2
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
goffi@necton2
parents:
diff changeset
11 (at your option) any later version.
goffi@necton2
parents:
diff changeset
12
goffi@necton2
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
goffi@necton2
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
goffi@necton2
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
goffi@necton2
parents:
diff changeset
16 GNU General Public License for more details.
goffi@necton2
parents:
diff changeset
17
goffi@necton2
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
goffi@necton2
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
goffi@necton2
parents:
diff changeset
20 """
goffi@necton2
parents:
diff changeset
21
goffi@necton2
parents:
diff changeset
22
goffi@necton2
parents:
diff changeset
23
goffi@necton2
parents:
diff changeset
24 import wx
goffi@necton2
parents:
diff changeset
25 import os.path
goffi@necton2
parents:
diff changeset
26 import pdb
goffi@necton2
parents:
diff changeset
27 from logging import debug, info, error
goffi@necton2
parents:
diff changeset
28 from tools.jid import JID
goffi@necton2
parents:
diff changeset
29 from quick_frontend.quick_chat import QuickChat
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
30 from contact_list import ContactList
0
goffi@necton2
parents:
diff changeset
31
goffi@necton2
parents:
diff changeset
32
goffi@necton2
parents:
diff changeset
33 idSEND = 1
goffi@necton2
parents:
diff changeset
34
goffi@necton2
parents:
diff changeset
35 class Chat(wx.Frame, QuickChat):
goffi@necton2
parents:
diff changeset
36 """The chat Window for one to one conversations"""
goffi@necton2
parents:
diff changeset
37
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
38 def __init__(self, target, host, type='one2one'):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
39 wx.Frame.__init__(self, None, title=target, pos=(0,0), size=(400,200))
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
40 QuickChat.__init__(self, target, host, type)
0
goffi@necton2
parents:
diff changeset
41
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
42 self.splitter = wx.SplitterWindow(self, -1)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
43
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
44 self.conv_panel = wx.Panel(self.splitter)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
45 self.conv_panel.sizer = wx.BoxSizer(wx.VERTICAL)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
46 self.chatWindow = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_MULTILINE | wx.TE_RICH | wx.TE_READONLY)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
47 self.textBox = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_PROCESS_ENTER)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
48 self.conv_panel.sizer.Add(self.chatWindow, 1, flag=wx.EXPAND)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
49 self.conv_panel.sizer.Add(self.textBox, flag=wx.EXPAND)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
50 self.conv_panel.SetSizer(self.conv_panel.sizer)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
51 self.splitter.Initialize(self.conv_panel)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
52 self.setType(self.type)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
53
0
goffi@necton2
parents:
diff changeset
54 self.createMenus()
goffi@necton2
parents:
diff changeset
55
goffi@necton2
parents:
diff changeset
56 #events
goffi@necton2
parents:
diff changeset
57 self.Bind(wx.EVT_CLOSE, self.onClose, self)
goffi@necton2
parents:
diff changeset
58 self.Bind(wx.EVT_TEXT_ENTER, self.onEnterPressed, self.textBox)
goffi@necton2
parents:
diff changeset
59
goffi@necton2
parents:
diff changeset
60 #fonts
goffi@necton2
parents:
diff changeset
61 self.font={}
goffi@necton2
parents:
diff changeset
62 self.font["points"] = self.chatWindow.GetFont().GetPointSize()
goffi@necton2
parents:
diff changeset
63 self.font["family"] = self.chatWindow.GetFont().GetFamily()
goffi@necton2
parents:
diff changeset
64
67
0e50dd3a234a message sending bug fixes + sortilege update
Goffi <goffi@goffi.org>
parents: 66
diff changeset
65 self.historyPrint(profile=self.host.profile)
0
goffi@necton2
parents:
diff changeset
66
goffi@necton2
parents:
diff changeset
67 #misc
goffi@necton2
parents:
diff changeset
68 self.textBox.SetFocus()
goffi@necton2
parents:
diff changeset
69 self.Hide() #We hide because of the show toggle
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
70
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
71 def __createPresents(self):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
72 """Create a list of present people in a group chat"""
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
73 self.present_panel = wx.Panel(self.splitter)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
74 self.present_panel.sizer = wx.BoxSizer(wx.VERTICAL)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
75 self.present_panel.SetBackgroundColour(wx.BLUE)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
76 self.present_panel.presents = ContactList(self.present_panel, self.host, type='nicks')
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
77 self.present_panel.presents.SetMinSize(wx.Size(80,20))
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
78 self.present_panel.sizer.Add(self.present_panel.presents, 1, wx.EXPAND)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
79 self.present_panel.SetSizer(self.present_panel.sizer)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
80 self.splitter.SplitVertically(self.present_panel, self.conv_panel, 80)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
81
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
82 def setType(self, type):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
83 QuickChat.setType(self, type)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
84 if type is 'group' and not self.splitter.IsSplit():
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
85 self.__createPresents()
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
86 elif type is 'one2one' and self.splitter.IsSplit():
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
87 self.splitter.Unsplit(self.present_panel)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
88 del self.present_panel
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
89
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
90 def setPresents(self, nicks):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
91 """Set the users presents in the contact list for a group chat
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
92 @param nicks: list of nicknames
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
93 """
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
94 debug (_("Adding users %s to room") % nicks)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
95 if self.type != "group":
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
96 error (_("[INTERNAL] trying to set presents nicks for a non group chat window"))
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
97 return
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
98 for nick in nicks:
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
99 self.present_panel.presents.replace(nick)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
100
0
goffi@necton2
parents:
diff changeset
101 def createMenus(self):
goffi@necton2
parents:
diff changeset
102 info("Creating menus")
goffi@necton2
parents:
diff changeset
103 actionMenu = wx.Menu()
70
Goffi <goffi@goffi.org>
parents: 67
diff changeset
104 actionMenu.Append(idSEND, _("&SendFile CTRL-s"),_(" Send a file to contact"))
0
goffi@necton2
parents:
diff changeset
105 menuBar = wx.MenuBar()
70
Goffi <goffi@goffi.org>
parents: 67
diff changeset
106 menuBar.Append(actionMenu,_("&Action"))
0
goffi@necton2
parents:
diff changeset
107 self.SetMenuBar(menuBar)
goffi@necton2
parents:
diff changeset
108
goffi@necton2
parents:
diff changeset
109 #events
goffi@necton2
parents:
diff changeset
110 wx.EVT_MENU(self, idSEND, self.onSendFile)
goffi@necton2
parents:
diff changeset
111
goffi@necton2
parents:
diff changeset
112 def __del__(self):
goffi@necton2
parents:
diff changeset
113 wx.Frame.__del__(self)
goffi@necton2
parents:
diff changeset
114
goffi@necton2
parents:
diff changeset
115 def onClose(self, event):
goffi@necton2
parents:
diff changeset
116 """Close event: we only hide the frame."""
goffi@necton2
parents:
diff changeset
117 event.Veto()
goffi@necton2
parents:
diff changeset
118 self.Show() ## this is a workaround to a wxpython bug:
goffi@necton2
parents:
diff changeset
119 ## with Raise on hidden frame, Hide doesn't work anymore
goffi@necton2
parents:
diff changeset
120 ## TODO: check this and repport bug to wxpython devs
goffi@necton2
parents:
diff changeset
121 self.Hide()
goffi@necton2
parents:
diff changeset
122
goffi@necton2
parents:
diff changeset
123 def onEnterPressed(self, event):
goffi@necton2
parents:
diff changeset
124 """Behaviour when enter pressed in send line."""
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
125 self.host.bridge.sendMessage(self.target, event.GetString(), profile_key=self.host.profile)
0
goffi@necton2
parents:
diff changeset
126 self.textBox.Clear()
goffi@necton2
parents:
diff changeset
127
goffi@necton2
parents:
diff changeset
128
goffi@necton2
parents:
diff changeset
129
66
8147b4f40809 SàT: multi-profile: DBus signals and frontend adaptation (first draft)
Goffi <goffi@goffi.org>
parents: 57
diff changeset
130 def printMessage(self, from_jid, msg, profile, timestamp=""):
0
goffi@necton2
parents:
diff changeset
131 """Print the message with differents colors depending on where it comes from."""
goffi@necton2
parents:
diff changeset
132 jid=JID(from_jid)
66
8147b4f40809 SàT: multi-profile: DBus signals and frontend adaptation (first draft)
Goffi <goffi@goffi.org>
parents: 57
diff changeset
133 mymess = (jid.short == self.host.profiles[profile]['whoami'].short) #mymess = True if message comes from local user
0
goffi@necton2
parents:
diff changeset
134 _font = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.BOLD)
goffi@necton2
parents:
diff changeset
135 self.chatWindow.SetDefaultStyle(wx.TextAttr( "BLACK" if mymess else "BLUE", font=_font))
goffi@necton2
parents:
diff changeset
136 self.chatWindow.AppendText("[%s] " % jid)
goffi@necton2
parents:
diff changeset
137 _font = wx.Font(self.font["points"], self.font["family"], wx.ITALIC if mymess else wx.NORMAL, wx.NORMAL)
goffi@necton2
parents:
diff changeset
138 self.chatWindow.SetDefaultStyle(wx.TextAttr("BLACK", font=_font))
goffi@necton2
parents:
diff changeset
139 self.chatWindow.AppendText("%s\n" % msg)
goffi@necton2
parents:
diff changeset
140 if not mymess:
goffi@necton2
parents:
diff changeset
141 self.Raise() #FIXME: too intrusive
goffi@necton2
parents:
diff changeset
142
goffi@necton2
parents:
diff changeset
143 ### events ###
goffi@necton2
parents:
diff changeset
144
goffi@necton2
parents:
diff changeset
145 def onSendFile(self, e):
70
Goffi <goffi@goffi.org>
parents: 67
diff changeset
146 debug(_("Send File"))
Goffi <goffi@goffi.org>
parents: 67
diff changeset
147 filename = wx.FileSelector(_("Choose a file to send"), flags = wx.FD_FILE_MUST_EXIST)
0
goffi@necton2
parents:
diff changeset
148 if filename:
70
Goffi <goffi@goffi.org>
parents: 67
diff changeset
149 debug(_("filename: %s"),filename)
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 70
diff changeset
150 full_jid = self.host.CM.get_full(self.target)
0
goffi@necton2
parents:
diff changeset
151 id = self.host.bridge.sendFile(full_jid, filename)
70
Goffi <goffi@goffi.org>
parents: 67
diff changeset
152 self.host.waitProgress(id, _("File Transfer"), _("Copying %s") % os.path.basename(filename))
0
goffi@necton2
parents:
diff changeset
153