view frontends/wix/chat.py @ 75:7322a41f8a8e

Basic user joined/left management - plugin XEP-0045: user joined./left signal is sended - wix: user are adder/removed when these signals are catched
author Goffi <goffi@goffi.org>
date Mon, 29 Mar 2010 16:54:53 +1100
parents f271fff3a713
children 8becde8a967c
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
from contact_list import ContactList


idSEND           = 1

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

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

        self.splitter = wx.SplitterWindow(self, -1)
        
        self.conv_panel = wx.Panel(self.splitter)
        self.conv_panel.sizer = wx.BoxSizer(wx.VERTICAL)
        self.chatWindow = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_MULTILINE | wx.TE_RICH | wx.TE_READONLY)
        self.textBox = wx.TextCtrl(self.conv_panel, -1, style = wx.TE_PROCESS_ENTER)
        self.conv_panel.sizer.Add(self.chatWindow, 1, flag=wx.EXPAND)
        self.conv_panel.sizer.Add(self.textBox, flag=wx.EXPAND)
        self.conv_panel.SetSizer(self.conv_panel.sizer)
        self.splitter.Initialize(self.conv_panel)
        self.setType(self.type)

        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(profile=self.host.profile)

        #misc
        self.textBox.SetFocus()
        self.Hide() #We hide because of the show toggle
   
    def __createPresents(self):
        """Create a list of present people in a group chat"""
        self.present_panel = wx.Panel(self.splitter)
        self.present_panel.sizer = wx.BoxSizer(wx.VERTICAL)
        self.present_panel.SetBackgroundColour(wx.BLUE)
        self.present_panel.presents = ContactList(self.present_panel, self.host, type='nicks')
        self.present_panel.presents.SetMinSize(wx.Size(80,20))
        self.present_panel.sizer.Add(self.present_panel.presents, 1, wx.EXPAND)
        self.present_panel.SetSizer(self.present_panel.sizer)
        self.splitter.SplitVertically(self.present_panel, self.conv_panel, 80)

    def setType(self, type):
        QuickChat.setType(self, type)
        if type is 'group' and not self.splitter.IsSplit():
            self.__createPresents()
        elif type is 'one2one' and self.splitter.IsSplit():
            self.splitter.Unsplit(self.present_panel)
            del self.present_panel

    def setPresents(self, nicks):
        """Set the users presents in the contact list for a group chat
        @param nicks: list of nicknames
        """
        debug (_("Adding users %s to room") % nicks)
        if self.type != "group":
            error (_("[INTERNAL] trying to set presents nicks for a non group chat window"))
            return
        for nick in nicks:
           self.present_panel.presents.replace(nick)
    
    
    def replaceUser(self, nick):
        """Add user if it is not in the group list"""
        debug (_("Replacing user %s") % nick)
        if self.type != "group":
            error (_("[INTERNAL] trying to replace user for a non group chat window"))
            return
        self.present_panel.presents.replace(nick)
      
    def removeUser(self, nick):
        """Remove a user from the group list"""
        debug(_("Removing user %s") % nick)
        if self.type != "group":
            error (_("[INTERNAL] trying to remove user for a non group chat window"))
            return
        self.present_panel.presents.remove(nick)



    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):
        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.target, event.GetString(), profile_key=self.host.profile)
        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.target)
            id = self.host.bridge.sendFile(full_jid, filename)
            self.host.waitProgress(id, _("File Transfer"), _("Copying %s") % os.path.basename(filename))