summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/gui/app.py
blob: 1011454e5812a3092d730dcf25e9237d28120140 (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
# -*- 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/>.
"""
A single App instances holds the signals that are shared among different
frontend UI components. The App also keeps a reference to the backend object
and the signaler get signals from the backend.
"""
from PySide import QtCore, QtGui

from leap.bitmask.gui.account import Account
from leap.bitmask.config.leapsettings import LeapSettings
from leap.bitmask.backend.backend_proxy import BackendProxy
from leap.bitmask.backend.leapsignaler import LeapSignaler
from leap.bitmask.logs.utils import get_logger

logger = get_logger()


class App(QtGui.QWidget):

    # the user has changed which services are enabled for a particular account
    # args: account (Account), active services (list of str)
    service_selection_changed = QtCore.Signal(object, list)

    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.settings = LeapSettings()
        self.backend = BackendProxy()
        self.backend.start()
        self.signaler = LeapSignaler()
        self.signaler.start()

        self.soledad_started = False
        self.service_tokens = {}
        self.login_state = None
        self.providers_widget = None

        # periodically check if the backend is alive
        self._backend_checker = QtCore.QTimer(self)
        self._backend_checker.timeout.connect(self._check_backend_status)
        self._backend_checker.start(2000)

        # store the service tokens for later use, once they are known.
        self.signaler.soledad_got_service_token.connect(
            self._set_service_tokens)

    def current_account(self):
        """
        Alas, the only definitive account information is buried in the memory
        of QT widgets.

        :returns: an object representing the current user account.
        :rtype: Account
        """
        if self.login_state is None or self.providers_widget is None:
            return None

        if self.login_state.full_logged_username is not None:
            username, domain = self.login_state.full_logged_username.split('@')
            return Account(username, domain)
        else:
            domain = self.providers_widget.get_selected_provider()
            return Account(None, domain)

    def _check_backend_status(self):
        """
        TRIGGERS:
            self._backend_checker.timeout

        Check that the backend is running. Otherwise show an error to the user.
        """
        if not self.backend.online:
            logger.critical("Backend is not online.")
            QtGui.QMessageBox.critical(
                self, self.tr("Application error"),
                self.tr("There is a problem contacting the backend, please "
                        "restart Bitmask."))
            self._backend_checker.stop()

    def _set_service_tokens(self, data):
        """
        Triggered by signal soledad_got_service_token.
        Saves the service tokens.
        """
        service, token = data
        self.service_tokens[service] = token