blocking.py 4.16 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
# 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/>.

import logging

Philipp Hörist's avatar
Philipp Hörist committed
17
from nbxmpp.errors import StanzaError
Philipp Hörist's avatar
Philipp Hörist committed
18
19
from gi.repository import Gtk
from gi.repository import Gdk
20
from gi.repository import GLib
Philipp Hörist's avatar
Philipp Hörist committed
21
22
23

from gajim.common import app
from gajim.common.i18n import _
24
from gajim.common.helpers import to_user_string
Philipp Hörist's avatar
Philipp Hörist committed
25

Philipp Hörist's avatar
Philipp Hörist committed
26
27
from .util import get_builder
from .dialogs import HigDialog
Philipp Hörist's avatar
Philipp Hörist committed
28

Philipp Hörist's avatar
Philipp Hörist committed
29
log = logging.getLogger('gajim.gui.blocking_list')
Philipp Hörist's avatar
Philipp Hörist committed
30
31
32
33
34
35
36
37
38
39


class BlockingList(Gtk.ApplicationWindow):
    def __init__(self, account):
        Gtk.ApplicationWindow.__init__(self)
        self.set_application(app.app)
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_show_menubar(False)
        self.set_title(_('Blocking List for %s') % account)

40
        self.connect_after('key-press-event', self._on_key_press)
Philipp Hörist's avatar
Philipp Hörist committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

        self.account = account
        self._con = app.connections[account]
        self._prev_blocked_jids = set()

        self._ui = get_builder('blocking_list.ui')
        self.add(self._ui.blocking_grid)

        self._spinner = Gtk.Spinner()
        self._ui.overlay.add_overlay(self._spinner)

        self._set_grid_state(False)
        self._ui.connect_signals(self)
        self.show_all()

        self._activate_spinner()

Philipp Hörist's avatar
Philipp Hörist committed
58
        self._con.get_module('Blocking').request_blocking_list(
Philipp Hörist's avatar
Philipp Hörist committed
59
60
61
62
63
64
            callback=self._on_blocking_list_received)

    def _show_error(self, error):
        dialog = HigDialog(
            self, Gtk.MessageType.INFO, Gtk.ButtonsType.OK,
            _('Error!'),
65
            GLib.markup_escape_text(error))
Philipp Hörist's avatar
Philipp Hörist committed
66
67
        dialog.popup()

Philipp Hörist's avatar
Philipp Hörist committed
68
69
70
71
72
73
74
    def _on_blocking_list_received(self, task):
        try:
            blocking_list = task.finish()
        except StanzaError as error:
            self._set_grid_state(False)
            self._show_error(to_user_string(error))
            return
Philipp Hörist's avatar
Philipp Hörist committed
75

Philipp Hörist's avatar
Philipp Hörist committed
76
77
78
        self._prev_blocked_jids = set(blocking_list)
        self._ui.blocking_store.clear()
        for item in blocking_list:
79
            self._ui.blocking_store.append((str(item),))
Philipp Hörist's avatar
Philipp Hörist committed
80

Philipp Hörist's avatar
Philipp Hörist committed
81
82
        self._set_grid_state(True)
        self._disable_spinner()
Philipp Hörist's avatar
Philipp Hörist committed
83

Philipp Hörist's avatar
Philipp Hörist committed
84
85
86
87
88
89
90
91
    def _on_save_result(self, task):
        try:
            _successful = task.finish()
        except StanzaError as error:
            self._show_error(to_user_string(error))
            self._disable_spinner()
            self._set_grid_state(True)
            return
Philipp Hörist's avatar
Philipp Hörist committed
92

Philipp Hörist's avatar
Philipp Hörist committed
93
        self.destroy()
Philipp Hörist's avatar
Philipp Hörist committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

    def _set_grid_state(self, state):
        self._ui.blocking_grid.set_sensitive(state)

    def _jid_edited(self, _renderer, path, new_text):
        iter_ = self._ui.blocking_store.get_iter(path)
        self._ui.blocking_store.set_value(iter_, 0, new_text)

    def _on_add(self, _button):
        self._ui.blocking_store.append([''])

    def _on_remove(self, _button):
        mod, paths = self._ui.block_view.get_selection().get_selected_rows()
        for path in paths:
            iter_ = mod.get_iter(path)
            self._ui.blocking_store.remove(iter_)

    def _on_save(self, _button):
        self._activate_spinner()
        self._set_grid_state(False)

        blocked_jids = set()
        for item in self._ui.blocking_store:
            blocked_jids.add(item[0].lower())

        unblock_jids = self._prev_blocked_jids - blocked_jids
        block_jids = blocked_jids - self._prev_blocked_jids
Philipp Hörist's avatar
Philipp Hörist committed
121
122
123

        self._con.get_module('Blocking').update_blocking_list(
            block_jids, unblock_jids, callback=self._on_save_result)
Philipp Hörist's avatar
Philipp Hörist committed
124
125
126
127
128
129
130
131
132
133
134
135

    def _activate_spinner(self):
        self._spinner.show()
        self._spinner.start()

    def _disable_spinner(self):
        self._spinner.hide()
        self._spinner.stop()

    def _on_key_press(self, _widget, event):
        if event.keyval == Gdk.KEY_Escape:
            self.destroy()