chat_control_base.py 61.1 KB
Newer Older
Philipp Hörist's avatar
Philipp Hörist committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
# Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
# Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
#                         Nikos Kouremenos <kourem AT gmail.com>
#                         Travis Shirk <travis AT pobox.com>
# Copyright (C) 2007 Lukas Petrovicky <lukas AT petrovicky.net>
#                    Julien Pivotto <roidelapluie AT gmail.com>
# Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com>
#                         Stephan Erb <steve-e AT h3c.de>
# 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/>.
25

26
import os
27
import sys
28
import time
29
import uuid
30
import tempfile
31

32
33
34
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib
Philipp Hörist's avatar
Philipp Hörist committed
35
from gi.repository import Gio
Philipp Hörist's avatar
Philipp Hörist committed
36

André's avatar
André committed
37
from gajim.common import events
38
from gajim.common import app
André's avatar
André committed
39
40
from gajim.common import helpers
from gajim.common import ged
41
from gajim.common import i18n
42
from gajim.common.i18n import _
Philipp Hörist's avatar
Philipp Hörist committed
43
from gajim.common.nec import EventHelper
44
from gajim.common.helpers import AdditionalDataDict
45
from gajim.common.helpers import event_filter
André's avatar
André committed
46
from gajim.common.contacts import GC_Contact
47
from gajim.common.const import Chatstate
Philipp Hörist's avatar
Philipp Hörist committed
48
from gajim.common.structs import OutgoingMessage
49

50
51
52
from gajim import gtkgui_helpers

from gajim.conversation_textview import ConversationTextview
53

Philipp Hörist's avatar
Philipp Hörist committed
54
55
56
57
58
59
60
61
62
63
64
65
from gajim.gui.dialogs import DialogButton
from gajim.gui.dialogs import ConfirmationDialog
from gajim.gui.dialogs import PastePreviewDialog
from gajim.gui.message_input import MessageInputTextView
from gajim.gui.util import at_the_end
from gajim.gui.util import get_show_in_roster
from gajim.gui.util import get_show_in_systray
from gajim.gui.util import get_hardware_key_codes
from gajim.gui.util import get_builder
from gajim.gui.util import generate_account_badge
from gajim.gui.const import ControlType  # pylint: disable=unused-import
from gajim.gui.emoji_chooser import emoji_chooser
66

André's avatar
André committed
67
68
from gajim.command_system.implementation.middleware import ChatCommandProcessor
from gajim.command_system.implementation.middleware import CommandTools
69

70
71
72
73
# The members of these modules are not referenced directly anywhere in this
# module, but still they need to be kept around. Importing them automatically
# registers the contained CommandContainers with the command system, thereby
# populating the list of available commands.
74
# pylint: disable=unused-import
André's avatar
André committed
75
76
from gajim.command_system.implementation import standard
from gajim.command_system.implementation import execute
77
# pylint: enable=unused-import
78

79
if app.is_installed('GSPELL'):
80
    from gi.repository import Gspell  # pylint: disable=ungrouped-imports
Philipp Hörist's avatar
Philipp Hörist committed
81

82
83
84
85
86
87
# This is needed so copying text from the conversation textview
# works with different language layouts. Pressing the key c on a russian
# layout yields another keyval than with the english layout.
# So we match hardware keycodes instead of keyvals.
# Multiple hardware keycodes can trigger a keyval like Gdk.KEY_c.
KEYCODES_KEY_C = get_hardware_key_codes(Gdk.KEY_c)
88

89
90
91
92
93
94
95
96
if sys.platform == 'darwin':
    COPY_MODIFIER = Gdk.ModifierType.META_MASK
    COPY_MODIFIER_KEYS = (Gdk.KEY_Meta_L, Gdk.KEY_Meta_R)
else:
    COPY_MODIFIER = Gdk.ModifierType.CONTROL_MASK
    COPY_MODIFIER_KEYS = (Gdk.KEY_Control_L, Gdk.KEY_Control_R)


97
################################################################################
98
class ChatControlBase(ChatCommandProcessor, CommandTools, EventHelper):
99
    """
100
    A base class containing a banner, ConversationTextview, MessageInputTextView
101
102
    """

103
    _type = None  # type: ControlType
104

105
    def __init__(self, parent_win, widget_name, contact, acct,
106
                 resource=None):
Philipp Hörist's avatar
Philipp Hörist committed
107
        EventHelper.__init__(self)
108
109
110
111
112
113
114
        # Undo needs this variable to know if space has been pressed.
        # Initialize it to True so empty textview is saved in undo list
        self.space_pressed = True

        if resource is None:
            # We very likely got a contact with a random resource.
            # This is bad, we need the highest for caps etc.
115
116
117
118
            _contact = app.contacts.get_contact_with_highest_priority(
                acct, contact.jid)
            if _contact and not isinstance(_contact, GC_Contact):
                contact = _contact
119

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        self.handlers = {}
        self.parent_win = parent_win
        self.contact = contact
        self.account = acct
        self.resource = resource

        # 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())
        self.session = None

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

        self.xml = get_builder('%s.ui' % widget_name)
        self.xml.connect_signals(self)
        self.widget = self.xml.get_object('%s_hbox' % widget_name)
136

137
138
        self._accounts = app.get_enabled_accounts_with_labels()
        if len(self._accounts) > 1:
139
140
141
142
143
            account_badge = generate_account_badge(self.account)
            account_badge.set_tooltip_text(
                _('Account: %s') % app.get_account_label(self.account))
            self.xml.account_badge.add(account_badge)
            account_badge.show()
144

145
146
147
        # Drag and drop
        self.xml.overlay.add_overlay(self.xml.drop_area)
        self.xml.drop_area.hide()
Daniel Brötzmann's avatar
Daniel Brötzmann committed
148
149
150
151
        self.xml.overlay.connect(
            'drag-data-received', self._on_drag_data_received)
        self.xml.overlay.connect('drag-motion', self._on_drag_motion)
        self.xml.overlay.connect('drag-leave', self._on_drag_leave)
152

153
        self.TARGET_TYPE_URI_LIST = 80
154
155
156
157
158
159
        uri_entry = Gtk.TargetEntry.new(
            'text/uri-list',
            Gtk.TargetFlags.OTHER_APP,
            self.TARGET_TYPE_URI_LIST)
        dst_targets = Gtk.TargetList.new([uri_entry])
        dst_targets.add_text_targets(0)
160
161
162
163
164
165
166
        self._dnd_list = [uri_entry,
                          Gtk.TargetEntry.new(
                              'MY_TREE_MODEL_ROW',
                              Gtk.TargetFlags.SAME_APP,
                              0)]

        self.xml.overlay.drag_dest_set(
167
            Gtk.DestDefaults.ALL,
168
            self._dnd_list,
169
            Gdk.DragAction.COPY | Gdk.DragAction.MOVE)
170
        self.xml.overlay.drag_dest_set_target_list(dst_targets)
171
172
173

        # Create textviews and connect signals
        self.conv_textview = ConversationTextview(self.account)
174

175
        id_ = self.conv_textview.connect('quote', self.on_quote)
176
        self.handlers[id_] = self.conv_textview
177

178
179
        self.conv_textview.tv.connect('key-press-event',
                                      self._on_conv_textview_key_press_event)
180

181
182
183
184
185
        # This is a workaround: as soon as a line break occurs in Gtk.TextView
        # with word-char wrapping enabled, a hyphen character is automatically
        # inserted before the line break. This triggers the hscrollbar to show,
        # see: https://gitlab.gnome.org/GNOME/gtk/-/issues/2384
        # Using set_hscroll_policy(Gtk.Scrollable.Policy.NEVER) would cause bad
186
        # performance during resize, and prevent the window from being shrunk
187
188
189
190
        # horizontally under certain conditions (applies to GroupchatControl)
        hscrollbar = self.xml.conversation_scrolledwindow.get_hscrollbar()
        hscrollbar.hide()

191
192
193
        self.xml.conversation_scrolledwindow.add(self.conv_textview.tv)
        widget = self.xml.conversation_scrolledwindow.get_vadjustment()
        widget.connect('changed', self.on_conversation_vadjustment_changed)
194

195
196
197
        vscrollbar = self.xml.conversation_scrolledwindow.get_vscrollbar()
        vscrollbar.connect('button-release-event',
                           self._on_scrollbar_button_release)
198

199
        self.msg_textview = MessageInputTextView()
200
201
202
203
204
205
        self.msg_textview.connect('paste-clipboard',
                                  self._on_message_textview_paste_event)
        self.msg_textview.connect('key-press-event',
                                  self._on_message_textview_key_press_event)
        self.msg_textview.connect('populate-popup',
                                  self.on_msg_textview_populate_popup)
206
207
        self.msg_textview.get_buffer().connect(
            'changed', self._on_message_tv_buffer_changed)
208

209
210
211
212
213
214
215
216
217
218
        # Send message button
        self.xml.send_message_button.set_action_name(
            'win.send-message-%s' % self.control_id)
        self.xml.send_message_button.set_visible(
            app.settings.get('show_send_message_button'))
        app.settings.bind_signal(
            'show_send_message_button',
            self.xml.send_message_button,
            'set_visible')

219
        self.msg_scrolledwindow = ScrolledWindow()
220
221
        self.msg_scrolledwindow.set_margin_start(3)
        self.msg_scrolledwindow.set_margin_end(3)
222
223
        self.msg_scrolledwindow.get_style_context().add_class(
            'message-input-border')
224
        self.msg_scrolledwindow.add(self.msg_textview)
225

226
        self.xml.hbox.pack_start(self.msg_scrolledwindow, True, True, 0)
227

228
229
230
231
232
233
234
        # the following vars are used to keep history of user's messages
        self.sent_history = []
        self.sent_history_pos = 0
        self.received_history = []
        self.received_history_pos = 0
        self.orig_msg = None

235
236
237
        # For XEP-0333
        self.last_msg_id = None

238
239
240
        self.correcting = False
        self.last_sent_msg = None

Philipp Hörist's avatar
Philipp Hörist committed
241
        self.set_emoticon_popover()
242
243

        # Attach speller
Philipp Hörist's avatar
Philipp Hörist committed
244
        self.set_speller()
245
246
247
248
249
250
251
252
        self.conv_textview.tv.show()

        # For XEP-0172
        self.user_nick = None

        self.command_hits = []
        self.last_key_tabs = False

253
254
        self.sendmessage = True

255
        con = app.connections[self.account]
256
        con.get_module('Chatstate').set_active(self.contact)
257

258
259
        if parent_win is not None:
            id_ = parent_win.window.connect('motion-notify-event',
260
                                            self._on_window_motion_notify)
261
262
            self.handlers[id_] = parent_win.window

263
        self.encryption = self.get_encryption_state()
264
        self.conv_textview.encryption_enabled = self.encryption is not None
Philipp Hörist's avatar
Philipp Hörist committed
265

266
267
        # PluginSystem: adding GUI extension point for ChatControlBase
        # instance object (also subclasses, eg. ChatControl or GroupchatControl)
268
        app.plugin_manager.gui_extension_point('chat_control_base', self)
269

270
        # pylint: disable=line-too-long
Philipp Hörist's avatar
Philipp Hörist committed
271
        self.register_events([
272
273
274
275
276
277
            ('our-show', ged.GUI1, self._nec_our_status),
            ('ping-sent', ged.GUI1, self._nec_ping),
            ('ping-reply', ged.GUI1, self._nec_ping),
            ('ping-error', ged.GUI1, self._nec_ping),
            ('sec-catalog-received', ged.GUI1, self._sec_labels_received),
            ('style-changed', ged.GUI1, self._style_changed),
Philipp Hörist's avatar
Philipp Hörist committed
278
        ])
279
280
        # pylint: enable=line-too-long

Alexander Krotov's avatar
Alexander Krotov committed
281
        # This is basically a very nasty hack to surpass the inability
282
283
284
        # to properly use the super, because of the old code.
        CommandTools.__init__(self)

285
    def _on_conv_textview_key_press_event(self, textview, event):
286
287
288
289
        if event.get_state() & Gdk.ModifierType.SHIFT_MASK:
            if event.keyval in (Gdk.KEY_Page_Down, Gdk.KEY_Page_Up):
                return Gdk.EVENT_PROPAGATE

290
291
292
293
294
295
296
297
298
299
        if event.keyval in COPY_MODIFIER_KEYS:
            # Don’t route modifier keys for copy action to the Message Input
            # otherwise pressing CTRL/META + c (the next event after that)
            # will not reach the textview (because the Message Input would get
            # focused).
            return Gdk.EVENT_PROPAGATE

        if event.get_state() & COPY_MODIFIER:
            # Don’t reroute the event if it is META + c and the
            # textview has a selection
300
            if event.hardware_keycode in KEYCODES_KEY_C:
301
302
303
                if textview.get_buffer().props.has_selection:
                    return Gdk.EVENT_PROPAGATE

304
305
306
307
308
309
        if not self.msg_textview.get_sensitive():
            # If the input textview is not sensitive it can’t get the focus.
            # In that case propagate_key_event() would send the event again
            # to the conversation textview. This would mean a recursion.
            return Gdk.EVENT_PROPAGATE

Philipp Hörist's avatar
Philipp Hörist committed
310
        # Focus the Message Input and resend the event
311
312
313
314
        self.msg_textview.grab_focus()
        self.msg_textview.get_toplevel().propagate_key_event(event)
        return Gdk.EVENT_STOP

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    @property
    def type(self):
        return self._type

    @property
    def is_chat(self):
        return self._type.is_chat

    @property
    def is_privatechat(self):
        return self._type.is_privatechat

    @property
    def is_groupchat(self):
        return self._type.is_groupchat

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
    def get_full_jid(self):
        fjid = self.contact.jid
        if self.resource:
            fjid += '/' + self.resource
        return fjid

    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 losing 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)

366
367
368
    def focus(self):
        raise NotImplementedError

369
370
371
372
    def get_nb_unread(self):
        jid = self.contact.jid
        if self.resource:
            jid += '/' + self.resource
373
374
375
376
        return len(app.events.get_events(
            self.account,
            jid,
            ['printed_%s' % self._type, str(self._type)]))
377
378
379

    def draw_banner(self):
        """
380
381
        Draw the fat line at the top of the window
        that houses the icon, jid, etc
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

        Derived types MAY implement this.
        """
        self.draw_banner_text()

    def update_toolbar(self):
        """
        update state of buttons in toolbar
        """
        self._update_toolbar()
        app.plugin_manager.gui_extension_point(
            'chat_control_base_update_toolbar', self)

    def draw_banner_text(self):
        """
        Derived types SHOULD implement this
        """

    def update_ui(self):
        """
        Derived types SHOULD implement this
        """
        self.draw_banner()

    def repaint_themed_widgets(self):
        """
        Derived types MAY implement this
        """
        self.draw_banner()

    def _update_toolbar(self):
        """
        Derived types MAY implement this
        """

417
418
419
420
421
422
423
424
425
426
427
    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 tuple like:
        # return (label_str, None)

Philipp Hörist's avatar
Philipp Hörist committed
428
    def get_tab_image(self):
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
        # Return a suitable tab image for display.
        return None

    def prepare_context_menu(self, hide_buttonbar_items=False):
        """
        Derived classes SHOULD implement this
        """
        return None

    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 session and oldsession:
            oldsession.control = None

    def remove_session(self, session):
        if session != self.session:
            return
        self.session.control = None
        self.session = None

460
461
462
    @event_filter(['account'])
    def _nec_our_status(self, event):
        if event.show == 'connecting':
463
            return
Philipp Hörist's avatar
Philipp Hörist committed
464

465
        if event.show == 'offline':
466
467
            self.got_disconnected()
        else:
Philipp Hörist's avatar
Philipp Hörist committed
468
            self.got_connected()
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
        if self.parent_win:
            self.parent_win.redraw_tab(self)

    def _nec_ping(self, obj):
        raise NotImplementedError

    def setup_seclabel(self):
        self.xml.label_selector.hide()
        self.xml.label_selector.set_no_show_all(True)
        lb = Gtk.ListStore(str)
        self.xml.label_selector.set_model(lb)
        cell = Gtk.CellRendererText()
        cell.set_property('xpad', 5)  # padding for status text
        self.xml.label_selector.pack_start(cell, True)
        # text to show is in in first column of liststore
        self.xml.label_selector.add_attribute(cell, 'text', 0)
        con = app.connections[self.account]
        jid = self.contact.jid
487
        if self._type.is_privatechat:
488
489
490
491
492
493
494
495
496
            jid = self.gc_contact.room_jid
        if con.get_module('SecLabels').supported:
            con.get_module('SecLabels').request_catalog(jid)

    def _sec_labels_received(self, event):
        if event.account != self.account:
            return

        jid = self.contact.jid
497
        if self._type.is_privatechat:
498
499
500
501
502
503
504
505
            jid = self.gc_contact.room_jid

        if event.jid != jid:
            return
        model = self.xml.label_selector.get_model()
        model.clear()

        sel = 0
506
507
        labellist = event.catalog.get_label_names()
        default = event.catalog.default
508
509
510
511
512
513
514
515
516
        for index, label in enumerate(labellist):
            model.append([label])
            if label == default:
                sel = index

        self.xml.label_selector.set_active(sel)
        self.xml.label_selector.set_no_show_all(False)
        self.xml.label_selector.show_all()

517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
    def delegate_action(self, action):
        if action == 'browse-history':
            dict_ = {'jid': GLib.Variant('s', self.contact.jid),
                     'account': GLib.Variant('s', self.account)}
            variant = GLib.Variant('a{sv}', dict_)
            app.app.activate_action('browse-history', variant)
            return Gdk.EVENT_STOP

        if action == 'clear-chat':
            self.conv_textview.clear()
            return Gdk.EVENT_STOP

        if action == 'delete-line':
            self.clear(self.msg_textview)
            return Gdk.EVENT_STOP

        if action == 'show-emoji-chooser':
534
535
536
537
            if sys.platform in ('win32', 'darwin'):
                self.xml.emoticons_button.get_popover().show()
                return Gdk.EVENT_STOP
            self.msg_textview.emit('insert-emoji')
538
539
540
541
            return Gdk.EVENT_STOP

        return Gdk.EVENT_PROPAGATE

542
    def add_actions(self):
Philipp Hörist's avatar
Philipp Hörist committed
543
        action = Gio.SimpleAction.new_stateful(
544
545
546
547
            'set-encryption-%s' % self.control_id,
            GLib.VariantType.new('s'),
            GLib.Variant('s', self.encryption or 'disabled'))
        action.connect('change-state', self.change_encryption)
Philipp Hörist's avatar
Philipp Hörist committed
548
549
        self.parent_win.window.add_action(action)

550
        actions = {
551
            'send-message-%s': self._on_send_message,
552
553
554
555
            'send-file-%s': self._on_send_file,
            'send-file-httpupload-%s': self._on_send_file,
            'send-file-jingle-%s': self._on_send_file,
        }
556

557
558
559
560
561
        for name, func in actions.items():
            action = Gio.SimpleAction.new(name % self.control_id, None)
            action.connect('activate', func)
            action.set_enabled(False)
            self.parent_win.window.add_action(action)
Philipp Hörist's avatar
Philipp Hörist committed
562

563
564
    def remove_actions(self):
        actions = [
565
            'send-message-',
566
567
568
569
570
571
572
573
574
            'set-encryption-',
            'send-file-',
            'send-file-httpupload-',
            'send-file-jingle-',
        ]

        for action in actions:
            self.parent_win.window.remove_action(f'{action}{self.control_id}')

Philipp Hörist's avatar
Philipp Hörist committed
575
    def change_encryption(self, action, param):
Philipp Hörist's avatar
Philipp Hörist committed
576
        encryption = param.get_string()
577
578
579
        if encryption == 'disabled':
            encryption = None

Philipp Hörist's avatar
Philipp Hörist committed
580
581
582
        if self.encryption == encryption:
            return

583
        if encryption:
584
            plugin = app.plugin_manager.encryption_plugins[encryption]
Philipp Hörist's avatar
Philipp Hörist committed
585
586
            if not plugin.activate_encryption(self):
                return
Philipp Hörist's avatar
Philipp Hörist committed
587

Philipp Hörist's avatar
Philipp Hörist committed
588
        action.set_state(param)
589
        self.set_encryption_state(encryption)
590
        self.set_encryption_menu_icon()
Philipp Hörist's avatar
Philipp Hörist committed
591
592
        self.set_lock_image()

593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
    def set_lock_image(self):
        encryption_state = {'visible': self.encryption is not None,
                            'enc_type': self.encryption,
                            'authenticated': False}

        if self.encryption:
            app.plugin_manager.extension_point(
                'encryption_state' + self.encryption, self, encryption_state)

        visible, enc_type, authenticated = encryption_state.values()

        if authenticated:
            authenticated_string = _('and authenticated')
            self.xml.lock_image.set_from_icon_name(
                'security-high-symbolic', Gtk.IconSize.MENU)
        else:
            authenticated_string = _('and NOT authenticated')
            self.xml.lock_image.set_from_icon_name(
                'security-low-symbolic', Gtk.IconSize.MENU)

        tooltip = _('%(type)s encryption is active %(authenticated)s.') % {
            'type': enc_type, 'authenticated': authenticated_string}

        self.xml.authentication_button.set_tooltip_text(tooltip)
        self.xml.authentication_button.set_visible(visible)
        self.xml.lock_image.set_sensitive(visible)

    def _on_authentication_button_clicked(self, _button):
        app.plugin_manager.extension_point(
            'encryption_dialog' + self.encryption, self)

624
625
    def set_encryption_state(self, encryption):
        self.encryption = encryption
626
        self.conv_textview.encryption_enabled = encryption is not None
627
        self.contact.settings.set('encryption', self.encryption or '')
628
629

    def get_encryption_state(self):
630
        state = self.contact.settings.get('encryption')
631
632
        if not state:
            return None
633
        if state not in app.plugin_manager.encryption_plugins:
634
635
636
            self.set_encryption_state(None)
            return None
        return state
Philipp Hörist's avatar
Philipp Hörist committed
637

638
    def set_encryption_menu_icon(self):
639
        image = self.xml.encryption_menu.get_image()
Philipp Hörist's avatar
Philipp Hörist committed
640
641
        if image is None:
            image = Gtk.Image()
642
            self.xml.encryption_menu.set_image(image)
643
        if not self.encryption:
Philipp Hörist's avatar
Philipp Hörist committed
644
645
            image.set_from_icon_name('channel-insecure-symbolic',
                                     Gtk.IconSize.MENU)
646
        else:
Philipp Hörist's avatar
Philipp Hörist committed
647
648
            image.set_from_icon_name('channel-secure-symbolic',
                                     Gtk.IconSize.MENU)
649

650
    def set_speller(self):
Philipp Hörist's avatar
Philipp Hörist committed
651
        if not app.is_installed('GSPELL') or not app.settings.get('use_speller'):
Philipp Hörist's avatar
Philipp Hörist committed
652
653
            return

Philipp Hörist's avatar
Philipp Hörist committed
654
        gspell_lang = self.get_speller_language()
655
        spell_checker = Gspell.Checker.new(gspell_lang)
Philipp Hörist's avatar
Philipp Hörist committed
656
657
        spell_buffer = Gspell.TextBuffer.get_from_gtk_text_buffer(
            self.msg_textview.get_buffer())
658
        spell_buffer.set_spell_checker(spell_checker)
Philipp Hörist's avatar
Philipp Hörist committed
659
660
661
        spell_view = Gspell.TextView.get_from_gtk_text_view(self.msg_textview)
        spell_view.set_inline_spell_checking(False)
        spell_view.set_enable_language_menu(True)
Philipp Hörist's avatar
Philipp Hörist committed
662

663
        spell_checker.connect('notify::language', self.on_language_changed)
Philipp Hörist's avatar
Philipp Hörist committed
664
665

    def get_speller_language(self):
666
        lang = self.contact.settings.get('speller_language')
667
668
        if not lang:
            # use the default one
Philipp Hörist's avatar
Philipp Hörist committed
669
            lang = app.settings.get('speller_language')
670
            if not lang:
671
                lang = i18n.LANG
Philipp Hörist's avatar
Philipp Hörist committed
672
673
674
675
        gspell_lang = Gspell.language_lookup(lang)
        if gspell_lang is None:
            gspell_lang = Gspell.language_get_default()
        return gspell_lang
676

677
    def on_language_changed(self, checker, _param):
Philipp Hörist's avatar
Philipp Hörist committed
678
        gspell_lang = checker.get_language()
679
        self.contact.settings.set('speller_language', gspell_lang.get_code())
680

681
    def on_banner_label_populate_popup(self, _label, menu):
682
        """
Alexander Krotov's avatar
Alexander Krotov committed
683
        Override the default context menu and add our own menuitems
684
685
686
687
        """
        item = Gtk.SeparatorMenuItem.new()
        menu.prepend(item)

688
        menu2 = self.prepare_context_menu()  # pylint: disable=assignment-from-none
689
690
691
692
693
694
695
696
697
        i = 0
        for item in menu2:
            menu2.remove(item)
            menu.prepend(item)
            menu.reorder_child(item, i)
            i += 1
        menu.show_all()

    def shutdown(self):
698
699
700
701
702
703
704
705
        # remove_gui_extension_point() is called on shutdown, but also when
        # a plugin is getting disabled. Plugins don’t know the difference.
        # Plugins might want to remove their widgets on
        # remove_gui_extension_point(), so delete the objects only afterwards.
        app.plugin_manager.remove_gui_extension_point('chat_control_base', self)
        app.plugin_manager.remove_gui_extension_point(
            'chat_control_base_update_toolbar', self)

706
707
708
        for i in list(self.handlers.keys()):
            if self.handlers[i].handler_is_connected(i):
                self.handlers[i].disconnect(i)
709
        self.handlers.clear()
710
711
712
713

        self.conv_textview.del_handlers()
        del self.conv_textview
        del self.msg_textview
714
715
716
717
718
719
        del self.msg_scrolledwindow

        self.widget.destroy()
        del self.widget

        del self.xml
720

Philipp Hörist's avatar
Philipp Hörist committed
721
        self.unregister_events()
722

723
    def on_msg_textview_populate_popup(self, _textview, menu):
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
        """
        Override the default context menu and we prepend an option to switch
        languages
        """
        item = Gtk.MenuItem.new_with_mnemonic(_('_Undo'))
        menu.prepend(item)
        id_ = item.connect('activate', self.msg_textview.undo)
        self.handlers[id_] = item

        item = Gtk.SeparatorMenuItem.new()
        menu.prepend(item)

        item = Gtk.MenuItem.new_with_mnemonic(_('_Clear'))
        menu.prepend(item)
        id_ = item.connect('activate', self.msg_textview.clear)
        self.handlers[id_] = item

741
742
743
744
745
        paste_item = Gtk.MenuItem.new_with_label(_('Paste as quote'))
        id_ = paste_item.connect('activate', self.paste_clipboard_as_quote)
        self.handlers[id_] = paste_item
        menu.append(paste_item)

746
747
        menu.show_all()

748
749
    def insert_as_quote(self, text: str) -> None:
        text = '> ' + text.replace('\n', '\n> ') + '\n'
750
751
752
        message_buffer = self.msg_textview.get_buffer()
        message_buffer.insert_at_cursor(text)

753
754
755
    def paste_clipboard_as_quote(self, _item: Gtk.MenuItem) -> None:
        clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        text = clipboard.wait_for_text()
756
757
        if text is None:
            return
758
759
        self.insert_as_quote(text)

760
    def on_quote(self, _widget, text):
761
762
        self.insert_as_quote(text)

763
    # moved from ChatControl
764
    def _on_banner_eventbox_button_press_event(self, _widget, event):
765
766
767
768
769
770
        """
        If right-clicked, show popup
        """
        if event.button == 3:  # right click
            self.parent_win.popup_menu(event)

771
    def _on_message_textview_paste_event(self, _texview):
772
773
774
        clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        image = clipboard.wait_for_image()
        if image is not None:
Philipp Hörist's avatar
Philipp Hörist committed
775
            if not app.settings.get('confirm_paste_image'):
776
                self._paste_event_confirmed(True, image)
777
                return
778
            PastePreviewDialog(
779
                _('Paste Image'),
780
781
                _('You are trying to paste an image'),
                _('Are you sure you want to paste your '
782
783
                  'clipboard\'s image into the chat window?'),
                _('_Do not ask me again'),
784
                image,
785
                [DialogButton.make('Cancel'),
786
                 DialogButton.make('Accept',
787
788
789
                                   text=_('_Paste'),
                                   callback=self._paste_event_confirmed,
                                   args=[image])]).show()
790

791
792
    def _paste_event_confirmed(self, is_checked, image):
        if is_checked:
Philipp Hörist's avatar
Philipp Hörist committed
793
            app.settings.set('confirm_paste_image', False)
794

795
796
        dir_ = tempfile.gettempdir()
        path = os.path.join(dir_, '%s.png' % str(uuid.uuid4()))
797
798
799
        image.savev(path, 'png', [], [])

        self._start_filetransfer(path)
800

801
    def _get_pref_ft_method(self):
802
803
        ft_pref = app.settings.get_account_setting(self.account,
                                                   'filetransfer_preference')
804
        httpupload = self.parent_win.window.lookup_action(
805
            'send-file-httpupload-%s' % self.control_id)
806
807
        jingle = self.parent_win.window.lookup_action(
            'send-file-jingle-%s' % self.control_id)
808

809
        if self._type.is_groupchat:
810
            if httpupload.get_enabled():
811
812
                return 'httpupload'
            return None
813

814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
        if httpupload.get_enabled() and jingle.get_enabled():
            return ft_pref

        if httpupload.get_enabled():
            return 'httpupload'

        if jingle.get_enabled():
            return 'jingle'
        return None

    def _start_filetransfer(self, path):
        method = self._get_pref_ft_method()
        if method is None:
            return

        if method == 'httpupload':
830
            app.interface.send_httpupload(self, path)
831
832
833
834

        else:
            ft = app.interface.instances['file_transfers']
            ft.send_file(self.account, self.contact, path)
835

836
    def _on_message_textview_key_press_event(self, textview, event):
837
838
839
840
841
842
        if event.keyval == Gdk.KEY_space:
            self.space_pressed = True

        elif (self.space_pressed or self.msg_textview.undo_pressed) and \
        event.keyval not in (Gdk.KEY_Control_L, Gdk.KEY_Control_R) and \
        not (event.keyval == Gdk.KEY_z and event.get_state() & Gdk.ModifierType.CONTROL_MASK):
Alexander Krotov's avatar
Alexander Krotov committed
843
844
845
            # If the space key has been pressed and now it hasn't,
            # we save the buffer into the undo list. But be careful we're not
            # pressing Control again (as in ctrl+z)
846
            _buffer = textview.get_buffer()
847
            start_iter, end_iter = _buffer.get_bounds()
848
849
850
            self.msg_textview.save_undo(_buffer.get_text(start_iter,
                                                         end_iter,
                                                         True))
851
852
853
            self.space_pressed = False

        # Ctrl [+ Shift] + Tab are not forwarded to notebook. We handle it here
854
        if self._type.is_groupchat:
855
856
            if event.keyval not in (Gdk.KEY_ISO_Left_Tab, Gdk.KEY_Tab):
                self.last_key_tabs = False
857

858
859
860
861
862
        if event.get_state() & Gdk.ModifierType.SHIFT_MASK:
            if event.get_state() & Gdk.ModifierType.CONTROL_MASK and \
                            event.keyval == Gdk.KEY_ISO_Left_Tab:
                self.parent_win.move_to_next_unread_tab(False)
                return True
863
864

            if event.keyval in (Gdk.KEY_Page_Down, Gdk.KEY_Page_Up):
865
                self.conv_textview.tv.event(event)
866
                self._on_scroll(None, event.keyval)
867
                return True
868

869
        if event.get_state() & Gdk.ModifierType.CONTROL_MASK:
870
            if event.keyval == Gdk.KEY_Tab:
871
872
873
874
875
876
877
878
879
880
                self.parent_win.move_to_next_unread_tab(True)
                return True

        message_buffer = self.msg_textview.get_buffer()
        event_state = event.get_state()
        if event.keyval == Gdk.KEY_Tab:
            start, end = message_buffer.get_bounds()
            position = message_buffer.get_insert()
            end = message_buffer.get_iter_at_mark(position)
            text = message_buffer.get_text(start, end, False)
Emmanuel Gil Peyrot's avatar
Emmanuel Gil Peyrot committed
881
            split = text.split()
882
883
            if (text.startswith(self.COMMAND_PREFIX) and
                    not text.startswith(self.COMMAND_PREFIX * 2) and
Emmanuel Gil Peyrot's avatar
Emmanuel Gil Peyrot committed
884
885
                    len(split) == 1):
                text = split[0]
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
                bare = text.lstrip(self.COMMAND_PREFIX)
                if len(text) == 1:
                    self.command_hits = []
                    for command in self.list_commands():
                        for name in command.names:
                            self.command_hits.append(name)
                else:
                    if (self.last_key_tabs and self.command_hits and
                            self.command_hits[0].startswith(bare)):
                        self.command_hits.append(self.command_hits.pop(0))
                    else:
                        self.command_hits = []
                        for command in self.list_commands():
                            for name in command.names:
                                if name.startswith(bare):
                                    self.command_hits.append(name)

                if self.command_hits:
                    message_buffer.delete(start, end)
                    message_buffer.insert_at_cursor(self.COMMAND_PREFIX + \
                        self.command_hits[0] + ' ')
                    self.last_key_tabs = True
                return True
909
            if not self._type.is_groupchat:
910
911
912
913
914
915
916
                self.last_key_tabs = False
        if event.keyval == Gdk.KEY_Up:
            if event_state & Gdk.ModifierType.CONTROL_MASK:
                if event_state & Gdk.ModifierType.SHIFT_MASK: # Ctrl+Shift+UP
                    self.scroll_messages('up', message_buffer, 'received')
                else:  # Ctrl+UP
                    self.scroll_messages('up', message_buffer, 'sent')
917
                return True
918
919
920
921
922
923
        elif event.keyval == Gdk.KEY_Down:
            if event_state & Gdk.ModifierType.CONTROL_MASK:
                if event_state & Gdk.ModifierType.SHIFT_MASK: # Ctrl+Shift+Down
                    self.scroll_messages('down', message_buffer, 'received')
                else:  # Ctrl+Down
                    self.scroll_messages('down', message_buffer, 'sent')
924
                return True
925
926
        elif (event.keyval == Gdk.KEY_Return or
              event.keyval == Gdk.KEY_KP_Enter):  # ENTER
927

928
            if event_state & Gdk.ModifierType.SHIFT_MASK:
929
930
931
932
933
934
935
                textview.insert_newline()
                return True

            if event_state & Gdk.ModifierType.CONTROL_MASK:
                if not app.settings.get('send_on_ctrl_enter'):
                    textview.insert_newline()
                    return True
936
937
938
939
            else:
                if app.settings.get('send_on_ctrl_enter'):
                    textview.insert_newline()
                    return True
940

941
            if not app.account_is_available(self.account):
942
                # we are not connected
943
                app.interface.raise_dialog('not-connected-while-sending')
944
                return True
945

946
            self._on_send_message()
947
            return True
948

949
950
951
952
953
954
955
956
        elif event.keyval == Gdk.KEY_z: # CTRL+z
            if event_state & Gdk.ModifierType.CONTROL_MASK:
                self.msg_textview.undo()
                return True

        return False

    def _on_drag_data_received(self, widget, context, x, y, selection,
957
                               target_type, timestamp):
958
959
960
961
        """
        Derived types SHOULD implement this
        """

962
    def _on_drag_leave(self, *args):
963
964
        self.xml.drop_area.set_no_show_all(True)
        self.xml.drop_area.hide()
965

966
    def _on_drag_motion(self, *args):
967
968
        self.xml.drop_area.set_no_show_all(False)
        self.xml.drop_area.show_all()
969

970
    def drag_data_file_transfer(self, selection: Gtk.SelectionData) -> None:
971
972
973
974
975
        # we may have more than one file dropped
        uri_splitted = selection.get_uris()
        for uri in uri_splitted:
            path = helpers.get_file_path_from_dnd_dropped_uri(uri)
            if not os.path.isfile(path):  # is it a file?
976
                self.add_info_message(_("The following file could not be accessed and was not uploaded: ") + path)
977
                continue
978
979

            self._start_filetransfer(path)
980

981
    def get_seclabel(self):
982
        idx = self.xml.label_selector.get_active()
Philipp Hörist's avatar
Philipp Hörist committed
983
        if idx == -1:
984
            return None
Philipp Hörist's avatar
Philipp Hörist committed
985
986

        con = app.connections[self.account]
987
        jid = self.contact.jid
988
        if self._type.is_privatechat:
989
990
            jid = self.gc_contact.room_jid
        catalog = con.get_module('SecLabels').get_catalog(jid)
991
        labels, label_list = catalog.labels, catalog.get_label_names()
Philipp Hörist's avatar
Philipp Hörist committed
992
993
        lname = label_list[idx]
        label = labels[lname]
994
995
        return label

996
997
998
999
1000
    def _on_send_message(self, *args):
        self.msg_textview.replace_emojis()
        message = self.msg_textview.get_text()
        xhtml = self.msg_textview.get_xhtml()
        self.send_message(message, xhtml=xhtml)