comparison frontends/wix/chat.py @ 0:c4bc297b82f0

sat: - first public release, initial commit
author goffi@necton2
date Sat, 29 Aug 2009 13:34:59 +0200
parents
children a5b5fb5fc9fd
comparison
equal deleted inserted replaced
-1:000000000000 0:c4bc297b82f0
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 wix: a SAT frontend
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
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
30
31
32 idSEND = 1
33
34 class Chat(wx.Frame, QuickChat):
35 """The chat Window for one to one conversations"""
36
37 def __init__(self, to_jid, host):
38 wx.Frame.__init__(self, None, title=to_jid, pos=(0,0), size=(400,200))
39 QuickChat.__init__(self, to_jid, host)
40
41 self.chatWindow = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE | wx.TE_RICH | wx.TE_READONLY)
42 self.textBox = wx.TextCtrl(self, -1, style = wx.TE_PROCESS_ENTER)
43 self.sizer = wx.BoxSizer(wx.VERTICAL)
44 self.sizer.Add(self.chatWindow, 1, flag=wx.EXPAND)
45 self.sizer.Add(self.textBox, flag=wx.EXPAND)
46 self.SetSizer(self.sizer)
47 self.SetAutoLayout(True)
48 self.createMenus()
49
50 #events
51 self.Bind(wx.EVT_CLOSE, self.onClose, self)
52 self.Bind(wx.EVT_TEXT_ENTER, self.onEnterPressed, self.textBox)
53
54 #fonts
55 self.font={}
56 self.font["points"] = self.chatWindow.GetFont().GetPointSize()
57 self.font["family"] = self.chatWindow.GetFont().GetFamily()
58
59 self.historyPrint()
60
61 #misc
62 self.textBox.SetFocus()
63 self.Hide() #We hide because of the show toggle
64
65 def createMenus(self):
66 info("Creating menus")
67 actionMenu = wx.Menu()
68 actionMenu.Append(idSEND, "&SendFile CTRL-s"," Send a file to contact")
69 menuBar = wx.MenuBar()
70 menuBar.Append(actionMenu,"&Action")
71 self.SetMenuBar(menuBar)
72
73 #events
74 wx.EVT_MENU(self, idSEND, self.onSendFile)
75
76 def __del__(self):
77 debug ("Chat window destructor")
78 wx.Frame.__del__(self)
79
80 def onClose(self, event):
81 """Close event: we only hide the frame."""
82 event.Veto()
83 self.Show() ## this is a workaround to a wxpython bug:
84 ## with Raise on hidden frame, Hide doesn't work anymore
85 ## TODO: check this and repport bug to wxpython devs
86 self.Hide()
87
88 def onEnterPressed(self, event):
89 """Behaviour when enter pressed in send line."""
90 self.host.bridge.sendMessage(self.to_jid, event.GetString())
91 self.textBox.Clear()
92
93
94
95 def printMessage(self, from_jid, msg, timestamp=""): #FIXME: factorize with printDistantMessage
96 """Print the message with differents colors depending on where it comes from."""
97 jid=JID(from_jid)
98 mymess = (jid.short == self.host.whoami.short) #mymess = True if message comes from local user
99 _font = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.BOLD)
100 self.chatWindow.SetDefaultStyle(wx.TextAttr( "BLACK" if mymess else "BLUE", font=_font))
101 self.chatWindow.AppendText("[%s] " % jid)
102 _font = wx.Font(self.font["points"], self.font["family"], wx.ITALIC if mymess else wx.NORMAL, wx.NORMAL)
103 self.chatWindow.SetDefaultStyle(wx.TextAttr("BLACK", font=_font))
104 self.chatWindow.AppendText("%s\n" % msg)
105 if not mymess:
106 self.Raise() #FIXME: too intrusive
107
108 ### events ###
109
110 def onSendFile(self, e):
111 debug("Send File")
112 filename = wx.FileSelector("Choose a file to send", flags = wx.FD_FILE_MUST_EXIST)
113 if filename:
114 debug("filename: %s",filename)
115 full_jid = self.host.CM.get_full(self.to_jid)
116 id = self.host.bridge.sendFile(full_jid, filename)
117 self.host.waitProgress(id, "File Transfer", "Copying %s" % os.path.basename(filename))
118