summaryrefslogtreecommitdiffstats
path: root/config/chroot_local-includes/usr/lib/python3/dist-packages/tailsgreeter/ui/additional_settings.py
blob: 08b774c4acb2c13907445f6237786e4ba7d4b61c (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
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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import gi

from tailsgreeter import TRANSLATION_DOMAIN
import tailsgreeter.config
import tailsgreeter.utils
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.network import NETCONF_DIRECT, NETCONF_DISABLED, NETCONF_OBSTACLE
from tailsgreeter.ui import _
from tailsgreeter.ui.setting import GreeterSetting
from tailsgreeter.ui.popover import Popover
from typing import TYPE_CHECKING

gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk, Gtk

if TYPE_CHECKING:
    from tailsgreeter.settings.admin import AdminSetting
    from tailsgreeter.settings.macspoof import MacSpoofSetting
    from tailsgreeter.settings.network import NetworkSetting

ADDITIONAL_SETTINGS_UI_FILE = "additional_settings.ui"


class AdditionalSetting(GreeterSetting):
    def __init__(self):
        super().__init__()
        self.dialog = None

        self.builder = Gtk.Builder()
        self.builder.set_translation_domain(TRANSLATION_DOMAIN)
        self.builder.add_from_file(tailsgreeter.config.data_path + ADDITIONAL_SETTINGS_UI_FILE)
        self.box = self.builder.get_object("box_{}_popover".format(self.id))

    def build_popover(self):
        self.popover = Popover(self.listboxrow, self.box)

    def close_window(self, response: Gtk.ResponseType):
        if self.has_popover() and self.popover.is_open():
            self.popover.close(response)
        else:
            self.dialog.response(response)

    def on_opened_in_dialog(self):
        pass

    def load(self) -> bool:
        pass


class AdminSettingUI(AdditionalSetting):
    @property
    def id(self) -> str:
        return "admin"

    @property
    def title(self) -> str:
        return _("_Administration Password")

    @property
    def icon_name(self) -> str:
        return "tails-admin"

    @property
    def value_for_display(self) -> str:
        return get_on_off_string(self.password, default=None)

    def update_check_icon(self):
        password = self.password_entry.get_text()
        password_verify = self.password_verify_entry.get_text()
        if not password_verify:
            icon = None
        elif password_verify == password:
            icon = 'emblem-ok-symbolic'
        else:
            icon = 'dialog-warning-symbolic'
        self.password_verify_entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, icon)

    def on_opened_in_dialog(self):
        self.update_check_icon()
        self.dialog.button_add.set_sensitive(self.passwords_match())
        self.password_entry.grab_focus()

    def __init__(self, admin_setting: "AdminSetting"):
        self._admin_setting = admin_setting
        self.password = None
        super().__init__()
        self.accel_key = Gdk.KEY_a

        self.password_entry = self.builder.get_object('entry_admin_password')
        self.password_entry.connect('changed', self.cb_entry_admin_changed)
        self.password_entry.connect('activate', self.cb_entry_admin_activate)
        self.password_verify_entry = self.builder.get_object('entry_admin_verify')
        self.password_verify_entry.connect('changed', self.cb_entry_admin_changed)
        self.password_verify_entry.connect('activate', self.cb_entry_admin_activate)
        self.box_admin_password = self.builder.get_object('box_admin_password')
        self.box_admin_verify = self.builder.get_object('box_admin_verify')
        self.button_admin_disable = self.builder.get_object('button_admin_disable')
        self.button_admin_disable.connect('clicked', self.cb_button_admin_disable_clicked)

    def build_popover(self):
        super().build_popover()
        self.popover.opened_cb = self.cb_popover_opened

    def cb_popover_opened(self, popover, user_data=None):
        self.password_verify_entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, None)
        password_already_set = bool(self.password)
        self.box_admin_password.set_visible(not password_already_set)
        self.box_admin_verify.set_visible(not password_already_set)
        self.button_admin_disable.set_visible(password_already_set)

    def passwords_match(self) -> bool:
        password = self.password_entry.get_text()
        # Don't accept an empty password
        if not password:
            return False
        return password == self.password_verify_entry.get_text()

    def apply(self):
        if self.password is True:
            # This should only be the case if the persistent storage was
            # unlocked and a persistent password settings file was found.
            # In that case, we just want to keep the existing settings file.
            pass
        elif self.password:
            # Write the password to a file from which it will be set
            # as the amnesia password when the greeter is closed.
            self._admin_setting.save(self.password)
        else:
            self._admin_setting.delete()
        super().apply()

    def load(self) -> bool:
        try:
            self._admin_setting.load()
        except SettingNotFoundError:
            raise
        self.password = True
        return True

    def cb_entry_admin_changed(self, editable, user_data=None):
        self.update_check_icon()
        passwords_match = self.passwords_match()
        if passwords_match:
            self.password = self.password_entry.get_text()
        if self.dialog:
            self.dialog.button_add.set_sensitive(passwords_match)
        return False

    def cb_entry_admin_activate(self, widget, user_data=None):
        if not self.passwords_match():
            if self.dialog:
                self.dialog.button_add.set_sensitive(False)
            self.password_verify_entry.grab_focus()
            return False

        self.password = self.password_entry.get_text()
        self.close_window(Gtk.ResponseType.YES)
        return False

    def cb_button_admin_disable_clicked(self, widget, user_data=None):
        self.password = None
        self.password_entry.set_text("")
        self.password_verify_entry.set_text("")

        if self.has_popover() and self.popover.is_open():
            self.popover.close(Gtk.ResponseType.YES)


class MACSpoofSettingUI(AdditionalSetting):
    @property
    def id(self) -> str:
        return "macspoof"

    @property
    def title(self) -> str:
        return _("_MAC Address Spoofing")

    @property
    def icon_name(self) -> str:
        return "tails-macspoof"

    @property
    def value_for_display(self) -> str:
        return get_on_off_string(self.spoofing_enabled, default=True)

    def __init__(self, macspoof_setting: "MacSpoofSetting"):
        self._macspoof_setting = macspoof_setting
        self.spoofing_enabled = True
        super().__init__()
        self.accel_key = Gdk.KEY_m

        self.image_macspoof_on = self.builder.get_object('image_macspoof_on')
        self.image_macspoof_off = self.builder.get_object('image_macspoof_off')
        self.listbox_macspoof_controls = self.builder.get_object('listbox_macspoof_controls')
        self.listbox_macspoof_controls.connect('row-activated', self.cb_listbox_macspoof_row_activated)
        self.listbox_macspoof_controls.connect('button-press-event', self.cb_listbox_macspoof_button_press)
        self.listboxrow_macspoof_on = self.builder.get_object('listboxrow_macspoof_on')
        self.listboxrow_macspoof_off = self.builder.get_object('listboxrow_macspoof_off')

    def apply(self):
        self._macspoof_setting.save(self.spoofing_enabled)
        super().apply()

    def load(self) -> bool:
        try:
            value = self._macspoof_setting.load()
        except SettingNotFoundError:
            raise

        # Select the correct listboxrow (used in the popover)
        if value:
            self.listbox_macspoof_controls.select_row(self.listboxrow_macspoof_on)
        else:
            self.listbox_macspoof_controls.select_row(self.listboxrow_macspoof_off)

        if self.spoofing_enabled == value:
            return False

        self.spoofing_enabled = value
        return True

    def cb_listbox_macspoof_row_activated(self, listbox, row, user_data=None):
        self.spoofing_enabled = row == self.listboxrow_macspoof_on
        self.image_macspoof_on.set_visible(self.spoofing_enabled)
        self.image_macspoof_off.set_visible(not self.spoofing_enabled)

        if self.has_popover() and self.popover.is_open():
            self.popover.close(Gtk.ResponseType.YES)
        return False

    def cb_listbox_macspoof_button_press(self, widget, event, user_data=None):
        # On double-click: Close the window and apply chosen setting
        if event.type == Gdk.EventType._2BUTTON_PRESS:
            self.close_window(Gtk.ResponseType.YES)
        return False


class NetworkSettingUI(AdditionalSetting):
    @property
    def id(self) -> str:
        return "network"

    @property
    def title(self) -> str:
        return _("_Network Connection")

    @property
    def icon_name(self) -> str:
        return "tails-network"

    @property
    def value_for_display(self) -> str:
        if self.value == NETCONF_DIRECT:
            return _("Direct (default)")
        if self.value == NETCONF_OBSTACLE:
            return _("Bridge & Proxy")
        if self.value == NETCONF_DISABLED:
            return _("Offline")

    def __init__(self, network_setting: "NetworkSetting"):
        self._network_setting = network_setting
        self.value = NETCONF_DIRECT
        super().__init__()
        self.accel_key = Gdk.KEY_n
        self.icon_network_clear_chosen = self.builder.get_object('image_network_clear')
        self.icon_network_specific_chosen = self.builder.get_object('image_network_specific')
        self.icon_network_off_chosen = self.builder.get_object('image_network_off')
        self.listbox_network_controls = self.builder.get_object('listbox_network_controls')
        self.listbox_network_controls.connect('button-press-event', self.cb_listbox_network_button_press)
        self.listbox_network_controls.connect('row-activated', self.cb_listbox_network_row_activated)
        self.listboxrow_network_clear = self.builder.get_object('listboxrow_network_clear')
        self.listboxrow_network_specific = self.builder.get_object('listboxrow_network_specific')
        self.listboxrow_network_off = self.builder.get_object('listboxrow_network_off')

    def apply(self):
        self._network_setting.save(self.value)
        is_bridge = self.value == NETCONF_OBSTACLE
        self.main_window.set_bridge_infobar_visibility(is_bridge)
        super().apply()

    def load(self):
        try:
            value = self._network_setting.load()
        except SettingNotFoundError:
            raise

        # Select the correct listboxrow (used in the popover)
        if value == NETCONF_DIRECT:
            self.listbox_network_controls.select_row(self.listboxrow_network_clear)
        elif value == NETCONF_OBSTACLE:
            self.listbox_network_controls.select_row(self.listboxrow_network_specific)
        elif value == NETCONF_DISABLED:
            self.listbox_network_controls.select_row(self.listboxrow_network_off)

        if self.value == value:
            return False

        self.value = value
        return True

    def cb_listbox_network_button_press(self, widget, event, user_data=None):
        # On double-click: Close the window and apply chosen setting
        if event.type == Gdk.EventType._2BUTTON_PRESS:
            self.close_window(Gtk.ResponseType.YES)
        return False

    def cb_listbox_network_row_activated(self, listbox, row, user_data=None):
        self.icon_network_clear_chosen.set_visible(False)
        self.icon_network_specific_chosen.set_visible(False)
        self.icon_network_off_chosen.set_visible(False)

        if row == self.listboxrow_network_clear:
            self.value = NETCONF_DIRECT
            self.icon_network_clear_chosen.set_visible(True)
        elif row == self.listboxrow_network_specific:
            self.value = NETCONF_OBSTACLE
            self.icon_network_specific_chosen.set_visible(True)
        elif row == self.listboxrow_network_off:
            self.value = NETCONF_DISABLED
            self.icon_network_off_chosen.set_visible(True)

        if self.has_popover() and self.popover.is_open():
            self.popover.close(Gtk.ResponseType.YES)
        return False


def get_on_off_string(value, default=None) -> str:
    """Return "On", "Off", "On (default)", or "Off (default)"""
    if value and default:
        return _("On (default)")
    if value and not default:
        return _("On")
    if not value and default:
        return _("Off")
    if not value and not default:
        return _("Off (default)")