summaryrefslogtreecommitdiff
path: root/qutebrowser/browser/signalfilter.py
blob: 88ac4a65daa7ae80f39914deca3273846668c270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2014-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser 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.
#
# qutebrowser 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 qutebrowser.  If not, see <https://www.gnu.org/licenses/>.

"""A filter for signals which either filters or passes them."""

import functools

from PyQt5.QtCore import QObject

from qutebrowser.utils import debug, log, objreg


class SignalFilter(QObject):

    """A filter for signals.

    Signals are only passed to the parent TabbedBrowser if they originated in
    the currently shown widget.

    Attributes:
        _win_id: The window ID this SignalFilter is associated with.

    Class attributes:
        BLACKLIST: List of signal names which should not be logged.
    """

    BLACKLIST = {'cur_scroll_perc_changed', 'cur_progress', 'cur_link_hovered'}

    def __init__(self, win_id, parent=None):
        super().__init__(parent)
        self._win_id = win_id

    def create(self, signal, tab):
        """Factory for partial _filter_signals functions.

        Args:
            signal: The pyqtBoundSignal to filter.
            tab: The WebView to create filters for.

        Return:
            A partial function calling _filter_signals with a signal.
        """
        log_signal = debug.signal_name(signal) not in self.BLACKLIST
        return functools.partial(
            self._filter_signals, signal=signal,
            log_signal=log_signal, tab=tab)

    def _filter_signals(self, *args, signal, log_signal, tab):
        """Filter signals and trigger TabbedBrowser signals if needed.

        Triggers signal if the original signal was sent from the _current_ tab
        and not from any other one.

        The original signal does not matter, since we get the new signal and
        all args.

        Args:
            signal: The signal to emit if the sender was the current widget.
            tab: The WebView which the filter belongs to.
            *args: The args to pass to the signal.
        """
        tabbed_browser = objreg.get('tabbed-browser', scope='window',
                                    window=self._win_id)
        try:
            tabidx = tabbed_browser.widget.indexOf(tab)
        except RuntimeError:
            # The tab has been deleted already
            return
        if tabidx == tabbed_browser.widget.currentIndex():
            if log_signal:
                log.signals.debug("emitting: {} (tab {})".format(
                    debug.dbg_signal(signal, args), tabidx))
            signal.emit(*args)
        else:
            # pylint: disable=else-if-used
            if log_signal:
                log.signals.debug("ignoring: {} (tab {})".format(
                    debug.dbg_signal(signal, args), tabidx))