view frontends/primitivus/chat.py @ 160:f494cba56a9e

xml tools: pairs layout is not used anymore if there is no label in dataForm2xml
author Goffi <goffi@goffi.org>
date Fri, 06 Aug 2010 12:10:43 +0800
parents f197b52796ee
children a50953ac6191
line wrap: on
line source

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

"""
Primitivus: 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 urwid
from quick_frontend.quick_contact_list import QuickContactList
from quick_frontend.quick_chat import QuickChat
import custom_widgets
import time
from tools.jid  import JID
from card_game import CardGame


class ChatText(urwid.FlowWidget):
    """Manage the printing of chat message"""
    
    def __init__(self, parent, timestamp, my_jid, from_jid, message, align='left'):
        self.parent = parent
        self.timestamp = time.localtime(timestamp)
        self.my_jid = my_jid
        self.from_jid = from_jid
        self.message = unicode(message)
        self.align = align

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key

    def rows(self,size,focus=False):
        return self.display_widget(size, focus).rows(size, focus)

    def render(self, size, focus=False):
        canvas = urwid.CompositeCanvas(self.display_widget(size, focus).render(size, focus))
        if focus:
            canvas.set_cursor(self.get_cursor_coords(size))
        return canvas

    def get_cursor_coords(self, size):
        #(maxcol,) = size
        return 0, 0

    def display_widget(self, size, focus):
        my_mess = (self.from_jid.resource == self.parent.nick) if self.parent.type == "group" else (self.from_jid.short == self.my_jid.short) #mymess = True if message comes from local user
        render_txt = []
        if self.parent.show_timestamp:
            time_format = "%c" if self.timestamp < self.parent.day_change else "%H:%M" #if the message was sent before today, we print the full date
            render_txt.append(('date',"[%s]" % time.strftime(time_format, self.timestamp)))
        if self.parent.show_short_nick:
            render_txt.append(('my_nick' if my_mess else 'other_nick',"**" if my_mess else "*"))
        else:
            nick = self.from_jid.resource if self.parent.type == "group" else (self.parent.host.CM.getAttr(self.from_jid,'nick') or self.parent.host.CM.getAttr(self.from_jid,'name') or self.from_jid.node)
            render_txt.append(('my_nick' if my_mess else 'other_nick',"[%s] " % nick))
        render_txt.append(self.message)
        return urwid.Text(render_txt, align=self.align)

class Chat(urwid.WidgetWrap, QuickChat):

    def __init__(self, target, host, type='one2one'):
        self.target = target
        QuickChat.__init__(self, target, host, type)
        self.content = urwid.SimpleListWalker([])
        self.text_list = urwid.ListBox(self.content)
        self.chat_widget = urwid.Frame(self.text_list)
        self.chat_colums = urwid.Columns([('weight', 8, self.chat_widget)])
        self.chat_colums = urwid.Columns([('weight', 8, self.chat_widget)])
        self.pile = urwid.Pile([self.chat_colums])
        urwid.WidgetWrap.__init__(self, self.__getDecoration(self.pile))
        self.setType(type)
        self.day_change = time.strptime(time.strftime("%a %b %d 00:00:00  %Y")) #struct_time of day changing time
        self.show_timestamp = True
        self.show_short_nick = False
        self.show_title = 1 #0: clip title; 1: full title; 2: no title
        self.subject = None

    def keypress(self, size, key):
        if key == "meta p": #user wants to (un)hide the presents panel
            if self.type == 'group':
                widgets = self.chat_colums.widget_list
                if self.present_panel in widgets:
                    self.__removePresentPanel()
                else:
                    self.__appendPresentPanel()
        elif key == "meta t": #user wants to (un)hide timestamp
            self.show_timestamp = not self.show_timestamp
            for wid in self.content:
                wid._invalidate()
        elif key == "meta n": #user wants to (not) use short nick
            self.show_short_nick = not self.show_short_nick
            for wid in self.content:
                wid._invalidate()
        elif key == "meta l": #user wants to (un)hide widget decoration
            show = not isinstance(self._w, custom_widgets.LabelLine)
            self.showDecoration(show)
            self._invalidate()
        elif key == "meta s": #user wants to (un)hide group's subject
            if self.subject:
                self.show_title = (self.show_title + 1) % 3
                if self.show_title == 0:
                    self.setSubject(self.subject,'clip')
                elif self.show_title == 1:
                    self.setSubject(self.subject,'space')
                elif self.show_title == 2:
                    self.chat_widget.header = None
                self._invalidate()


        return super(Chat, self).keypress(size, key) 
    
    def getMenu(self):
        """Return Menu bar"""
        menu = custom_widgets.Menu(self.host.loop)
        if self.type == 'group':
            game = _("Game")
            menu.addMenu(game, "Tarot", self.onTarotRequest)
        return menu

    def setType(self, type):
        QuickChat.setType(self, type)
        if type == 'one2one':
            self.historyPrint(profile=self.host.profile)
        elif type == 'group':
            if len(self.chat_colums.widget_list) == 1:
                present_widget = self.__buildPresentList()
                self.present_panel = custom_widgets.VerticalSeparator(present_widget)
                self.__appendPresentPanel()
          
    def __getDecoration(self, widget):
        return custom_widgets.LabelLine(widget, custom_widgets.SurroundedText(unicode(self.target)))

    def showDecoration(self, show=True):
        """Show/Hide the decoration around the chat window"""
        if show:
            main_widget = self.__getDecoration(self.pile)
        else:
            main_widget = self.pile
        self._w = main_widget


    def __buildPresentList(self):
        self.present_wid = custom_widgets.GenericList([],option_type = custom_widgets.ClickableText)
        return self.present_wid
   
    def __appendPresentPanel(self):
        self.chat_colums.widget_list.append(self.present_panel) 
        self.chat_colums.column_types.append(('weight', 2))

    def __removePresentPanel(self):
        self.chat_colums.set_focus(0) #necessary as the focus change to the next object, we can go out of range if we are on the last object of self.chat_colums
        self.chat_colums.widget_list.remove(self.present_panel)
        del self.chat_colums.column_types[-1]
    
    def __appendGamePanel(self, widget):
        assert (len(self.pile.widget_list) == 1)
        self.pile.widget_list.insert(0,widget)
        self.pile.item_types.insert(0,('weight', 1))
        self.pile.widget_list.insert(1,urwid.Filler(urwid.Divider('-')))
        self.pile.item_types.insert(1,('fixed', 1))
        self.host.redraw()

    def __removeGamePanel(self):
        assert (len(self.pile.widget_list) == 3)
        self.pile.set_focus(0) #necessary as the focus change to the next object, we can go out of range if we are on the last object of self.chat_colums
        del self.pile.widget_list[0]
        del self.pile.item_types[0]
        self.host.redraw()

    def setSubject(self, subject, wrap='space'):
        """Set title for a group chat"""
        QuickChat.setSubject(self, subject)
        self.subject = subject
        self.subj_wid = urwid.Text(unicode(subject.replace('\n','|') if wrap == 'clip' else subject ),
                                  align='left' if wrap=='clip' else 'center',wrap=wrap)
        self.chat_widget.header = urwid.AttrMap(self.subj_wid,'title')
        self.host.redraw()

    def setPresents(self, param_nicks):
        """Set the users presents in the contact list for a group chat
        @param nicks: list of nicknames
        """
        nicks = [unicode(nick) for nick in param_nicks] #FIXME: should be done in DBus bridge
        nicks.sort()
        QuickChat.setPresents(self, nicks)
        self.present_wid.changeValues(nicks)
        self.host.redraw()

    def replaceUser(self, param_nick):
        """Add user if it is not in the group list"""
        nick = unicode(param_nick) #FIXME: should be done in DBus bridge
        if "facebook" in nick:
            self.host.debug()
        QuickChat.replaceUser(self, nick)
        presents = self.present_wid.getAllValues()
        if nick not in presents:
            presents.append(nick)
            presents.sort()
            self.present_wid.changeValues(presents)
        self.host.redraw()

    def removeUser(self, param_nick):
        """Remove a user from the group list"""
        nick = unicode(param_nick) #FIXME: should be done in DBus bridge
        QuickChat.removeUser(self, nick)
        self.present_wid.deleteValue(nick)
        self.host.redraw()

    def printMessage(self, from_jid, msg, profile, timestamp=""):
        assert isinstance(from_jid, JID)
        my_jid = self.host.profiles[profile]['whoami']
        self.content.append(ChatText(self, timestamp or None, my_jid, from_jid, msg))
        self.text_list.set_focus(len(self.content)-1)
        self.host.redraw()
    
    def startGame(self, game_type, referee, players):
        """Configure the chat window to start a game"""
        if game_type=="Tarot":
            try:
                self.tarot_wid = CardGame(self, referee, players, self.nick)
                self.__appendGamePanel(self.tarot_wid)
            except e:
                self.host.debug()
    
    def getGame(self, game_type):
        """Return class managing the game type"""
        #TODO: check that the game is launched, and manage errors
        if game_type=="Tarot":
            return self.tarot_wid 

    #MENU EVENTS#
    def onTarotRequest(self, menu):
        if len(self.occupants) != 4:
            self.host.showPopUp(custom_widgets.Alert(_("Can't start game"), _("You need to be exactly 4 peoples in the room to start a Tarot game"), ok_cb=self.host.removePopUp)) 
        else:
            self.host.bridge.tarotGameCreate(self.id, list(self.occupants), self.host.profile)