room.py 4.48 KB
Newer Older
1
# Copyright 2010 Le Coz Florent <louiz@louiz.org>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# This file is part of Poezio.
#
# Poezio 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, version 3 of the License.
#
# Poezio 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 Poezio.  If not, see <http://www.gnu.org/licenses/>.

17
from text_buffer import TextBuffer
18 19 20
from datetime import datetime
from random import randrange
from config import config
21
from logger import logger
22
from message import Message
23

24
import common
25
import theme
26

27
class Room(TextBuffer):
28 29
    """
    """
30 31
    def __init__(self, name, nick):
        TextBuffer.__init__(self)
32 33
        self.name = name
        self.own_nick = nick
34
        self.color_state = theme.COLOR_TAB_NORMAL   # color used in RoomInfo
35
        self.joined = False     # false until self presence is receied
36
        self.users = []         # User objects
37 38 39
        self.topic = ''

    def disconnect(self):
40 41 42 43
        """
        Set the state of the room as not joined, so
        we can know if we can join it, send messages to it, etc
        """
44 45
        self.joined = False

46
    def log_message(self, txt, time, nickname):
47
        """
48 49
        Log the messages in the archives, if it needs
        to be
50
        """
51 52
        if time == None and self.joined:        # don't log the history messages
            logger.message(self.name, nickname, txt)
53 54 55 56 57

    def do_highlight(self, txt, time, nickname):
        """
        Set the tab color and returns the txt color
        """
58
        color = theme.COLOR_NORMAL_TEXT
59
        if not time and nickname and nickname != self.own_nick and self.joined: # do the highlight
60
            try:
61
                if self.own_nick in txt:
62 63
                    self.set_color_state(theme.COLOR_TAB_HIGHLIGHT)
                    color = theme.COLOR_HIGHLIGHT_TEXT
64 65 66
            except UnicodeDecodeError:
                try:
                    if self.own_nick in txt:
67 68
                        self.set_color_state(theme.COLOR_TAB_HIGHLIGHT)
                        color = theme.COLOR_HIGHLIGHT_TEXT
69 70
                except:
                    pass
71 72 73 74
            else:
                highlight_words = config.get('highlight_on', '').split(':')
                for word in highlight_words:
                    if word.lower() in txt.lower() and word != '':
75 76
                        self.set_color_state(theme.COLOR_TAB_HIGHLIGHT)
                        color = theme.COLOR_HIGHLIGHT_TEXT
77
                        break
78 79
        return color

80 81 82 83 84 85 86 87 88 89 90 91 92
    def get_user_by_name(self, nick):
        for user in self.users:
            if user.nick == nick:
                return user
        return None

    def set_color_state(self, color):
        """
        Set the color that will be used to display the room's
        number in the RoomInfo window
        """
        self.color_state = color

93
    def add_message(self, txt, time=None, nickname=None, colorized=False):
94 95 96 97 98 99 100 101 102
        """
        Note that user can be None even if nickname is not None. It happens
        when we receive an history message said by someone who is not
        in the room anymore
        """
        self.log_message(txt, time, nickname)
        user = self.get_user_by_name(nickname) if nickname is not None else None
        if user:
            user.set_last_talked(datetime.now())
103
        color = theme.COLOR_NORMAL_TEXT
104
        if not time and nickname and\
105
                nickname != self.own_nick and\
106
                self.color_state != theme.COLOR_TAB_CURRENT:
107
            if self.color_state != theme.COLOR_TAB_HIGHLIGHT:
108
                self.set_color_state(theme.COLOR_TAB_NEW_MESSAGE)
109 110
            # elif self.jid:
            #     self.set_color_state(theme.COLOR_TAB_PRIVATE)
111
        if not nickname:
112
            color = theme.COLOR_INFORMATION_TEXT
113 114
        else:
            color = self.do_highlight(txt, time, nickname)
115
        if time:                # History messages are colored to be distinguished
116
            color = theme.COLOR_INFORMATION_TEXT
117
        time = time if time is not None else datetime.now()
118 119
        if self.pos:            # avoid scrolling of one line when one line is received
            self.pos += 1
120
        self.messages.append(Message(txt, time, nickname, user, color, colorized))