summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/gui/preferences_account_page.py
blob: da9da14d24507c01309cfb84cc8a2394754f3c56 (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
# -*- coding: utf-8 -*-
# Copyright (C) 2014 LEAP
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Widget for "account" preferences
"""
from functools import partial

from PySide import QtCore, QtGui

from leap.bitmask.logs.utils import get_logger
from leap.bitmask.gui import ui_preferences_account_page as ui_pref
from leap.bitmask.gui.passwordwindow import PasswordWindow
from leap.bitmask.services import get_service_display_name
from leap.bitmask._components import HAS_EIP

logger = get_logger()


class PreferencesAccountPage(QtGui.QWidget):

    def __init__(self, parent, account, app):
        """
        :param parent: parent object of the PreferencesWindow.
        :parent type: QWidget

        :param account: user account (user + provider or just provider)
        :type account: Account

        :param app: the current App object
        :type app: App
        """
        QtGui.QWidget.__init__(self, parent)
        self.ui = ui_pref.Ui_PreferencesAccountPage()
        self.ui.setupUi(self)

        self.account = account
        self.app = app

        self._selected_services = set()
        self.ui.change_password_label.setVisible(False)
        self.ui.provider_services_label.setVisible(False)

        self.ui.change_password_button.clicked.connect(
            self._show_change_password)
        app.signaler.prov_get_supported_services.connect(self._load_services)
        app.backend.provider_get_supported_services(domain=account.domain)

        if account.username is None:
            self.ui.change_password_label.setText(
                self.tr('You must be logged in to change your password.'))
            self.ui.change_password_label.setVisible(True)
            self.ui.change_password_button.setEnabled(False)

    def _service_selection_changed(self, service, state):
        """
        TRIGGERS:
            service_checkbox.stateChanged

        Adds the service to the state if the state is checked, removes
        it otherwise

        :param service: service to handle
        :type service: str
        :param state: state of the checkbox
        :type state: int
        """
        if state == QtCore.Qt.Checked:
            self._selected_services = \
                self._selected_services.union(set([service]))
        else:
            self._selected_services = \
                self._selected_services.difference(set([service]))
        services = list(self._selected_services)

        # We hide the maybe-visible status label after a change
        self.ui.provider_services_label.setVisible(False)

        # write to config
        self.app.settings.set_enabled_services(self.account.domain, services)

        # emit signal alerting change
        self.app.service_selection_changed.emit(self.account, services)

    def _load_services(self, services):
        """
        TRIGGERS:
            prov_get_supported_services

        Loads the services that the provider provides into the UI for
        the user to enable or disable.

        :param services: list of supported service names
        :type services: list of str
        """
        services_conf = self.account.services()

        self._selected_services = set()

        # Remove existing checkboxes
        # (the new widget is deleted when its parent is deleted.
        #  We need to loop backwards because removing things from the
        #  beginning shifts items and changes the order of items in the layout.
        #  Using `QObject.deleteLater` doesn't seem to work.)
        layout = self.ui.provider_services_layout
        for i in reversed(range(layout.count())):
            layout.itemAt(i).widget().setParent(None)

        # add one checkbox per service and set the current value
        # from what is saved in settings.
        for service in services:
            if not HAS_EIP and service == "openvpn":
                continue
            try:
                checkbox = QtGui.QCheckBox(
                    get_service_display_name(service), self)
                self.ui.provider_services_layout.addWidget(checkbox)
                checkbox.stateChanged.connect(
                    partial(self._service_selection_changed, service))
                checkbox.setChecked(service in services_conf)
            except ValueError:
                logger.error("Something went wrong while trying to "
                             "load service %s" % (service,))

    def _show_change_password(self):
        change_password_window = PasswordWindow(self, self.account, self.app)
        change_password_window.show()