view frontends/wix/chat.py @ 66:8147b4f40809

SàT: multi-profile: DBus signals and frontend adaptation (first draft) - Quick App: new single_profile parameter in __init__ (default: yes), used to tell if the application use only one profile at the time or not - Quick App: new __check_profile method, tell if the profile is used by the current frontend - Quick App: new methods plug_profile, unplug_profile and clear_profile, must be called by the frontend to tell which profiles to use - DBus Bridge: new methods getProfileName, getProfilesList and createProfile
author Goffi <goffi@goffi.org>
date Wed, 03 Feb 2010 23:35:57 +1100
parents a5b5fb5fc9fd
children 0e50dd3a234a
line wrap: on
line source

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

"""
wix: a SAT frontend
Copyright (C) 2009, 2010  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, profile, timestamp=""):
        """Print the message with differents colors depending on where it comes from."""
        jid=JID(from_jid)
        mymess = (jid.short == self.host.profiles[profile]['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))