summaryrefslogtreecommitdiff
path: root/src/leap/baseapp/network.py
blob: a33265e5aeda202cbd9dfde6179c7f23eaba8f17 (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
from __future__ import print_function

import logging

logger = logging.getLogger(name=__name__)

from PyQt4 import QtCore

from leap.baseapp.dialogs import ErrorDialog
from leap.base.network import NetworkCheckerThread

from leap.util.misc import null_check


class NetworkCheckerAppMixin(object):
    """
    initialize an instance of the Network Checker,
    which gathers error and passes them on.
    """
    def __init__(self, *args, **kwargs):
        provider = kwargs.pop('provider', None)
        if provider:
            self.init_network_checker(provider)

    def init_network_checker(self, provider):
        null_check(provider, "provider_domain")
        if not hasattr(self, 'network_checker'):
            self.network_checker = NetworkCheckerThread(
                error_cb=self.networkError.emit,
                debug=self.debugmode,
                provider=provider)
        self.network_checker.start()

    @QtCore.pyqtSlot(object)
    def runNetworkChecks(self):
        self.network_checker.run_checks()

    @QtCore.pyqtSlot(object)
    def onNetworkError(self, exc):
        """
        slot that receives a network exceptions
        and raises a user error message
        """
        logger.debug('handling network exception')
        logger.error(exc.message)
        dialog = ErrorDialog(parent=self)

        if exc.critical:
            dialog.criticalMessage(exc.usermessage, "network error")
        else:
            dialog.warningMessage(exc.usermessage, "network error")