message_control.py 7.59 KB
Newer Older
roidelapluie's avatar
roidelapluie committed
1
# -*- coding:utf-8 -*-
roidelapluie's avatar
roidelapluie committed
2
## src/message_control.py
3
##
roidelapluie's avatar
roidelapluie committed
4 5 6 7
## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
##                    Nikos Kouremenos <kourem AT gmail.com>
## Copyright (C) 2006-2007 Jean-Marie Traissard <jim AT lapin.org>
##                         Travis Shirk <travis AT pobox.com>
Yann Leboulanger's avatar
Yann Leboulanger committed
8
## Copyright (C) 2006-2010 Yann Leboulanger <asterix AT lagaule.org>
roidelapluie's avatar
roidelapluie committed
9 10 11 12
## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
##                    Stephan Erb <steve-e AT h3c.de>
## Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com>
## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
13
##
14 15 16
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
17
## it under the terms of the GNU General Public License as published
18
## by the Free Software Foundation; version 3 only.
19
##
20
## Gajim is distributed in the hope that it will be useful,
21
## but WITHOUT ANY WARRANTY; without even the implied warranty of
roidelapluie's avatar
roidelapluie committed
22
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 24
## GNU General Public License for more details.
##
25
## You should have received a copy of the GNU General Public License
roidelapluie's avatar
roidelapluie committed
26
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
27
##
roidelapluie's avatar
roidelapluie committed
28

29
import gtkgui_helpers
30 31

from common import gajim
32
from common import helpers
33
from common import ged
34
from common.stanza_session import EncryptedStanzaSession, ArchivingStanzaSession
35 36 37 38 39 40 41 42

# Derived types MUST register their type IDs here if custom behavor is required
TYPE_CHAT = 'chat'
TYPE_GC = 'gc'
TYPE_PM = 'pm'

####################

43
class MessageControl(object):
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    """
    An abstract base widget that can embed in the gtk.Notebook of a
    MessageWindow
    """

    def __init__(self, type_id, parent_win, widget_name, contact, account, resource = None):
        # dict { cb id : widget}
        # keep all registered callbacks of widgets, created by self.xml
        self.handlers = {}
        self.type_id = type_id
        self.parent_win = parent_win
        self.widget_name = widget_name
        self.contact = contact
        self.account = account
        self.hide_chat_buttons = False
        self.resource = resource

        self.session = None

        gajim.last_message_time[self.account][self.get_full_jid()] = 0

        self.xml = gtkgui_helpers.get_gtk_builder('%s.ui' % widget_name)
66
        self.widget = self.xml.get_object('%s_hbox' % widget_name)
67

68 69 70
        gajim.ged.register_event_handler('message-outgoing', ged.OUT_GUI1,
            self._nec_message_outgoing)

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    def get_full_jid(self):
        fjid = self.contact.jid
        if self.resource:
            fjid += '/' + self.resource
        return fjid

    def set_control_active(self, state):
        """
        Called when the control becomes active (state is True) or inactive (state
        is False)
        """
        pass  # Derived classes MUST implement this method

    def minimizable(self):
        """
        Called to check if control can be minimized

        Derived classes MAY implement this.
        """
        return False

    def safe_shutdown(self):
        """
        Called to check if control can be closed without loosing data.
        returns True if control can be closed safely else False

        Derived classes MAY implement this.
        """
        return True

    def allow_shutdown(self, method, on_response_yes, on_response_no,
                    on_response_minimize):
        """
        Called to check is a control is allowed to shutdown.
        If a control is not in a suitable shutdown state this method
        should call on_response_no, else on_response_yes or
        on_response_minimize

        Derived classes MAY implement this.
        """
        on_response_yes(self)

    def shutdown(self):
        """
        Derived classes MUST implement this
        """
117 118
        gajim.ged.remove_event_handler('message-outgoing', ged.OUT_GUI1,
            self._nec_message_outgoing)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

    def repaint_themed_widgets(self):
        """
        Derived classes SHOULD implement this
        """
        pass

    def update_ui(self):
        """
        Derived classes SHOULD implement this
        """
        pass

    def toggle_emoticons(self):
        """
        Derived classes MAY implement this
        """
        pass

    def update_font(self):
        """
        Derived classes SHOULD implement this
        """
        pass

    def update_tags(self):
        """
        Derived classes SHOULD implement this
        """
        pass

    def get_tab_label(self, chatstate):
        """
        Return a suitable tab label string. Returns a tuple such as: (label_str,
        color) either of which can be None if chatstate is given that means we
        have HE SENT US a chatstate and we want it displayed

        Derivded classes MUST implement this.
        """
        # Return a markup'd label and optional gtk.Color in a tupple like:
        # return (label_str, None)
        pass

    def get_tab_image(self, count_unread=True):
        # Return a suitable tab image for display.
        # None clears any current label.
        return None

    def prepare_context_menu(self):
        """
        Derived classes SHOULD implement this
        """
        return None

    def chat_buttons_set_visible(self, state):
        """
        Derived classes MAY implement this
        """
        self.hide_chat_buttons = state

    def got_connected(self):
        pass

    def got_disconnected(self):
        pass

    def get_specific_unread(self):
        return len(gajim.events.get_events(self.account,
                self.contact.jid))

    def set_session(self, session):
        oldsession = None
        if hasattr(self, 'session'):
            oldsession = self.session

        if oldsession and session == oldsession:
            return

        self.session = session

        if session:
            session.control = self

        if oldsession:
            oldsession.control = None

            jid = self.contact.jid
            if self.resource:
                jid += '/' + self.resource

209 210
        crypto_changed = bool(session and isinstance(session,
                EncryptedStanzaSession) and session.enable_encryption) != \
Yann Leboulanger's avatar
Yann Leboulanger committed
211 212
                bool(oldsession and isinstance(oldsession,
                EncryptedStanzaSession) and oldsession.enable_encryption)
213

214
        archiving_changed = bool(session and isinstance(session,
215 216 217
            ArchivingStanzaSession) and session.archiving) != \
            bool(oldsession and isinstance(oldsession,
            ArchivingStanzaSession) and oldsession.archiving)
218 219 220

        if crypto_changed or archiving_changed:
            self.print_session_details()
221

222
    def _nec_message_outgoing(self, obj):
223 224
        # Send the given message to the active tab.
        # Doesn't return None if error
225 226
        if obj.account != self.account:
            return
227

228 229 230
        obj.jid = self.contact.jid
        obj.message = helpers.remove_invalid_xml_chars(obj.message)
        obj.original_message = obj.message
231 232 233 234

        conn = gajim.connections[self.account]

        if not self.session:
235
            if not obj.resource:
236
                if self.resource:
237
                    obj.resource = self.resource
238
                else:
239 240
                    obj.resource = self.contact.resource
            sess = conn.find_controlless_session(obj.jid, resource=obj.resource)
241 242

            if self.resource:
243
                obj.jid += '/' + self.resource
244 245 246

            if not sess:
                if self.type_id == TYPE_PM:
247
                    sess = conn.make_new_session(obj.jid, type_='pm')
248
                else:
249
                    sess = conn.make_new_session(obj.jid)
250 251 252

            self.set_session(sess)

253
        obj.session = self.session