view frontends/wix/chat.py @ 18:6928e3cb73a8

refactoring: using xml params part II
author Goffi <goffi@goffi.org>
date Fri, 06 Nov 2009 23:31:00 +0100
parents c4bc297b82f0
children a5b5fb5fc9fd
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
wix: a SAT frontend
Copyright (C) 2009  Jérôme Poisson (goffi@goffi.org)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""



import wx
import os.path
import pdb
from logging import debug, info, error
from tools.jid  import JID
from quick_frontend.quick_chat import QuickChat


idSEND           = 1

class Chat(wx.Frame, QuickChat):
    """The chat Window for one to one conversations"""

    def __init__(self, to_jid, host):
        wx.Frame.__init__(self, None, title=to_jid, pos=(0,0), size=(400,200))
        QuickChat.__init__(self, to_jid, host) 

        self.chatWindow = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE | wx.TE_RICH | wx.TE_READONLY)
        self.textBox = wx.TextCtrl(self, -1, style = wx.TE_PROCESS_ENTER)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.chatWindow, 1, flag=wx.EXPAND)
        self.sizer.Add(self.textBox, flag=wx.EXPAND)
        self.SetSizer(self.sizer)
        self.SetAutoLayout(True)
        self.createMenus()
        
        #events
        self.Bind(wx.EVT_CLOSE, self.onClose, self)
        self.Bind(wx.EVT_TEXT_ENTER, self.onEnterPressed, self.textBox)

        #fonts
        self.font={}
        self.font["points"] = self.chatWindow.GetFont().GetPointSize()
        self.font["family"] = self.chatWindow.GetFont().GetFamily()
        
        self.historyPrint()

        #misc
        self.textBox.SetFocus()
        self.Hide() #We hide because of the show toggle
    
    def createMenus(self):
        info("Creating menus")
        actionMenu = wx.Menu()
        actionMenu.Append(idSEND, "&SendFile	CTRL-s"," Send a file to contact")
        menuBar = wx.MenuBar()
        menuBar.Append(actionMenu,"&Action")
        self.SetMenuBar(menuBar)

        #events
        wx.EVT_MENU(self, idSEND, self.onSendFile)

    def __del__(self):
        debug ("Chat window destructor")
        wx.Frame.__del__(self)

    def onClose(self, event):
        """Close event: we only hide the frame."""
        event.Veto()
        self.Show()  ## this is a workaround to a wxpython bug:
                     ## with Raise on hidden frame, Hide doesn't work anymore
                     ## TODO: check this and repport bug to wxpython devs
        self.Hide()

    def onEnterPressed(self, event):
        """Behaviour when enter pressed in send line."""
        self.host.bridge.sendMessage(self.to_jid, event.GetString())
        self.textBox.Clear()

        

    def printMessage(self, from_jid, msg, timestamp=""):   #FIXME: factorize with printDistantMessage
        """Print the message with differents colors depending on where it comes from."""
        jid=JID(from_jid)
        mymess = (jid.short == self.host.whoami.short) #mymess = True if message comes from local user
        _font = wx.Font(self.font["points"], self.font["family"], wx.NORMAL, wx.BOLD)
        self.chatWindow.SetDefaultStyle(wx.TextAttr( "BLACK" if mymess else "BLUE", font=_font))
        self.chatWindow.AppendText("[%s] " % jid)
        _font = wx.Font(self.font["points"], self.font["family"], wx.ITALIC if mymess else wx.NORMAL, wx.NORMAL)
        self.chatWindow.SetDefaultStyle(wx.TextAttr("BLACK", font=_font))
        self.chatWindow.AppendText("%s\n" % msg)
        if not mymess:
            self.Raise()  #FIXME: too intrusive

    ### events ###

    def onSendFile(self, e):
        debug("Send File")
        filename = wx.FileSelector("Choose a file to send", flags = wx.FD_FILE_MUST_EXIST)
        if filename:
            debug("filename: %s",filename)
            full_jid = self.host.CM.get_full(self.to_jid)
            id = self.host.bridge.sendFile(full_jid, filename)
            self.host.waitProgress(id, "File Transfer", "Copying %s" % os.path.basename(filename))