2018-09-05 02:59:34 +02:00
|
|
|
# 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>
|
|
|
|
# Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
# 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>
|
|
|
|
#
|
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim 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 only.
|
|
|
|
#
|
|
|
|
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2008-08-15 05:20:23 +02:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim import gtkgui_helpers
|
2017-08-18 18:52:04 +02:00
|
|
|
import uuid
|
2006-01-02 03:12:34 +01:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import helpers
|
|
|
|
from gajim.common import ged
|
2006-01-02 03:12:34 +01:00
|
|
|
|
|
|
|
# Derived types MUST register their type IDs here if custom behavor is required
|
|
|
|
TYPE_CHAT = 'chat'
|
|
|
|
TYPE_GC = 'gc'
|
|
|
|
TYPE_PM = 'pm'
|
|
|
|
|
|
|
|
####################
|
|
|
|
|
2008-08-03 15:29:11 +02:00
|
|
|
class MessageControl(object):
|
2010-04-08 01:20:17 +02:00
|
|
|
"""
|
2012-12-23 16:23:43 +01:00
|
|
|
An abstract base widget that can embed in the Gtk.Notebook of a
|
2010-04-08 01:20:17 +02:00
|
|
|
MessageWindow
|
|
|
|
"""
|
|
|
|
|
2011-08-21 09:51:57 +02:00
|
|
|
def __init__(self, type_id, parent_win, widget_name, contact, account,
|
|
|
|
resource=None):
|
2010-04-08 01:20:17 +02:00
|
|
|
# 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.resource = resource
|
2017-08-18 18:52:04 +02:00
|
|
|
# control_id is a unique id for the control,
|
|
|
|
# its used as action name for actions that belong to a control
|
|
|
|
self.control_id = str(uuid.uuid4())
|
2010-04-08 01:20:17 +02:00
|
|
|
self.session = None
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.last_message_time[self.account][self.get_full_jid()] = 0
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
self.xml = gtkgui_helpers.get_gtk_builder('%s.ui' % widget_name)
|
2016-12-23 19:01:32 +01:00
|
|
|
self.xml.connect_signals(self)
|
2010-10-27 11:39:45 +02:00
|
|
|
self.widget = self.xml.get_object('%s_hbox' % widget_name)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('message-outgoing', ged.OUT_GUI1,
|
2011-05-18 19:44:43 +02:00
|
|
|
self._nec_message_outgoing)
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
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):
|
|
|
|
"""
|
2018-06-27 00:52:14 +02:00
|
|
|
Called to check if control can be closed without losing data.
|
2010-04-08 01:20:17 +02:00
|
|
|
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
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('message-outgoing', ged.OUT_GUI1,
|
2011-05-18 19:44:43 +02:00
|
|
|
self._nec_message_outgoing)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2017-03-06 20:38:29 +01:00
|
|
|
# Return a markup'd label and optional Gtk.Color in a tuple like:
|
2010-04-08 01:20:17 +02:00
|
|
|
# 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
|
|
|
|
|
2017-08-23 23:26:26 +02:00
|
|
|
def prepare_context_menu(self, hide_buttonbar_items=False):
|
2010-04-08 01:20:17 +02:00
|
|
|
"""
|
|
|
|
Derived classes SHOULD implement this
|
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
|
|
|
def got_connected(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def got_disconnected(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_specific_unread(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
return len(app.events.get_events(self.account,
|
2010-04-08 01:20:17 +02:00
|
|
|
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
|
|
|
|
|
2016-01-17 20:54:20 +01:00
|
|
|
if session and oldsession:
|
2010-04-08 01:20:17 +02:00
|
|
|
oldsession.control = None
|
|
|
|
|
2011-08-21 09:51:57 +02:00
|
|
|
def remove_session(self, session):
|
|
|
|
if session != self.session:
|
|
|
|
return
|
2012-12-16 20:28:57 +01:00
|
|
|
self.session.control = None
|
2012-08-20 14:58:15 +02:00
|
|
|
self.session = None
|
2011-08-21 09:51:57 +02:00
|
|
|
|
2011-05-18 19:44:43 +02:00
|
|
|
def _nec_message_outgoing(self, obj):
|
2010-04-08 01:20:17 +02:00
|
|
|
# Send the given message to the active tab.
|
|
|
|
# Doesn't return None if error
|
2011-08-21 09:51:57 +02:00
|
|
|
if obj.control != self:
|
2011-05-20 22:36:10 +02:00
|
|
|
return
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2011-05-18 19:44:43 +02:00
|
|
|
obj.message = helpers.remove_invalid_xml_chars(obj.message)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
conn = app.connections[self.account]
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
if not self.session:
|
2017-03-19 19:34:30 +01:00
|
|
|
if (not obj.resource and
|
2017-08-13 13:18:56 +02:00
|
|
|
obj.jid != app.get_jid_from_account(self.account)):
|
2010-04-08 01:20:17 +02:00
|
|
|
if self.resource:
|
2011-05-18 19:44:43 +02:00
|
|
|
obj.resource = self.resource
|
2010-04-08 01:20:17 +02:00
|
|
|
else:
|
2011-05-18 19:44:43 +02:00
|
|
|
obj.resource = self.contact.resource
|
|
|
|
sess = conn.find_controlless_session(obj.jid, resource=obj.resource)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
if self.resource:
|
2011-05-18 19:44:43 +02:00
|
|
|
obj.jid += '/' + self.resource
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
if not sess:
|
|
|
|
if self.type_id == TYPE_PM:
|
2011-05-18 19:44:43 +02:00
|
|
|
sess = conn.make_new_session(obj.jid, type_='pm')
|
2010-04-08 01:20:17 +02:00
|
|
|
else:
|
2011-05-18 19:44:43 +02:00
|
|
|
sess = conn.make_new_session(obj.jid)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
self.set_session(sess)
|
|
|
|
|
2011-05-18 19:44:43 +02:00
|
|
|
obj.session = self.session
|