summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/config/leapsettings.py
blob: d7590c249a1e76f37b756bc5d32f3feef5aebae7 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# -*- coding: utf-8 -*-
# leapsettings.py
# Copyright (C) 2013, 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/>.
"""
QSettings abstraction.
"""
import os

from PySide import QtCore

from leap.common.check import leap_assert, leap_assert_type
from leap.bitmask.logs.utils import get_logger
from leap.bitmask.util import get_path_prefix

logger = get_logger()


def to_bool(val):
    """
    Returns the boolean value corresponding to val. Will return False
    in case val is not a string or something that behaves like one.

    :param val: value to cast
    :type val: either bool already or str

    :rtype: bool
    """
    if isinstance(val, bool):
        return val

    bool_val = False
    try:
        bool_val = val.lower() == "true"
    except:
        pass

    return bool_val


class LeapSettings(object):
    """
    Leap client QSettings wrapper
    """

    CONFIG_NAME = "leap.conf"

    # keys
    GEOMETRY_KEY = "Geometry"
    WINDOWSTATE_KEY = "WindowState"
    USER_KEY = "User"
    PROVIDER_KEY = "Provider"
    REMEMBER_KEY = "RememberUserAndPass"
    DEFAULTPROVIDER_KEY = "DefaultProvider"
    AUTOSTARTEIP_KEY = "AutoStartEIP"
    ALERTMISSING_KEY = "AlertMissingScripts"
    GATEWAY_KEY = "Gateway"
    PINNED_KEY = "Pinned"
    SKIPFIRSTRUN_KEY = "SkipFirstRun"
    UUIDFORUSER_KEY = "%s/%s_uuid"
    PIXELMAIL_KEY = "Pixmail"

    # values
    GATEWAY_AUTOMATIC = "Automatic"

    def __init__(self):
        settings_path = os.path.join(get_path_prefix(),
                                     "leap", self.CONFIG_NAME)

        self._settings = QtCore.QSettings(settings_path,
                                          QtCore.QSettings.IniFormat)

    def get_geometry(self):
        """
        Returns the saved geometry or None if it wasn't saved

        :rtype: bytearray or None
        """
        return self._settings.value(self.GEOMETRY_KEY, None)

    def set_geometry(self, geometry):
        """
        Saves the geometry to the settings

        :param geometry: bytearray representing the geometry
        :type geometry: bytearray
        """
        leap_assert(geometry, "We need a geometry")
        self._settings.setValue(self.GEOMETRY_KEY, geometry)

    def get_windowstate(self):
        """
        Returns the window state or None if it wasn't saved

        :rtype: bytearray or None
        """
        return self._settings.value(self.WINDOWSTATE_KEY, None)

    def set_windowstate(self, windowstate):
        """
        Saves the window state to the settings

        :param windowstate: bytearray representing the window state
        :type windowstate: bytearray
        """
        leap_assert(windowstate, "We need a window state")
        self._settings.setValue(self.WINDOWSTATE_KEY, windowstate)

    def get_configured_providers(self):
        """
        Returns the configured providers based on the file structure in the
        settings directory.

        :rtype: list of str
        """
        # TODO: check which providers have a valid certificate among
        # other things, not just the directories
        providers = []
        try:
            providers_path = os.path.join(get_path_prefix(),
                                          "leap", "providers")
            providers = os.listdir(providers_path)
        except Exception as e:
            logger.debug("Error listing providers, assume there are none. %r"
                         % (e,))

        return providers

    def is_pinned_provider(self, domain):
        """
        Returns True if the domain 'domain' is pinned with the application.
                False otherwise.

        :param provider: provider domain
        :type provider: str

        :rtype: bool
        """
        leap_assert(len(domain) > 0, "We need a nonempty domain.")
        pinned_key = "{0}/{1}".format(domain, self.PINNED_KEY)
        result = to_bool(self._settings.value(pinned_key, False))

        return result

    def get_selected_gateway(self, provider):
        """
        Returns the configured gateway for the given provider.

        :param provider: provider domain
        :type provider: str

        :rtype: str
        """
        leap_assert(len(provider) > 0, "We need a nonempty provider")
        gateway_key = "{0}/{1}".format(provider, self.GATEWAY_KEY)
        gateway = self._settings.value(gateway_key, self.GATEWAY_AUTOMATIC)

        return gateway

    def set_selected_gateway(self, provider, gateway):
        """
        Saves the configured gateway for the given provider

        :param provider: provider domain
        :type provider: str

        :param gateway: gateway to use as default
        :type gateway: str
        """

        leap_assert(len(provider) > 0, "We need a nonempty provider")
        leap_assert_type(gateway, (str, unicode))

        gateway_key = "{0}/{1}".format(provider, self.GATEWAY_KEY)
        self._settings.setValue(gateway_key, gateway)

    def get_enabled_services(self, provider):
        """
        Returns a list of enabled services for the given provider

        :param provider: provider domain
        :type provider: str

        :rtype: list of str
        """

        leap_assert(len(provider) > 0, "We need a nonempty provider")
        enabled_services = self._settings.value("%s/Services" % (provider,),
                                                [])
        if isinstance(enabled_services, (str, unicode)):
            enabled_services = enabled_services.split(",")

        return enabled_services

    def set_enabled_services(self, provider, services):
        """
        Saves the list of enabled services for the given provider

        :param provider: provider domain
        :type provider: str

        :param services: list of services to save
        :type services: list of str
        """

        leap_assert(len(provider) > 0, "We need a nonempty provider")
        leap_assert_type(services, list)

        key = "{0}/Services".format(provider)
        if not services:
            # if there are no enabled services we don't need that key
            self._settings.remove(key)
        else:
            self._settings.setValue(key, services)

    def get_user(self):
        """
        Returns the configured user to remember, None if there isn't one

        :rtype: str or None
        """
        return self._settings.value(self.USER_KEY, None)

    def set_user(self, user):
        """
        Saves the user to remember

        :param user: user name to remember
        :type user: str
        """
        leap_assert(len(user) > 0, "We cannot save an empty user")
        self._settings.setValue(self.USER_KEY, user)

    def get_provider(self):
        """
        Returns the configured provider to remember, None if there isn't one

        :rtype: str or None
        """
        return self._settings.value(self.PROVIDER_KEY, None)

    def set_provider(self, provider):
        """
        Saves the provider to remember

        :param provider: provider name to remember
        :type provider: str
        """
        leap_assert(len(provider) > 0, "We cannot save an empty provider")
        self._settings.setValue(self.PROVIDER_KEY, provider)

    def get_remember(self):
        """
        Returns the value of the remember selection.

        :rtype: bool
        """
        return to_bool(self._settings.value(self.REMEMBER_KEY, False))

    def set_remember(self, remember):
        """
        Sets wheter the app should remember username and password

        :param remember: True if the app should remember username and
            password, False otherwise
        :rtype: bool
        """
        leap_assert_type(remember, bool)
        self._settings.setValue(self.REMEMBER_KEY, remember)

    def get_defaultprovider(self):
        """
        Returns the default provider to be used for autostarting EIP

        :rtype: str or None
        """
        return self._settings.value(self.DEFAULTPROVIDER_KEY, None)

    def set_defaultprovider(self, provider):
        """
        Sets the default provider to be used for autostarting EIP

        :param provider: provider to use
        :type provider: str or None
        """
        if provider is None:
            self._settings.remove(self.DEFAULTPROVIDER_KEY)
        else:
            self._settings.setValue(self.DEFAULTPROVIDER_KEY, provider)

    def get_autostart_eip(self):
        """
        Gets whether the app should autostart EIP.

        :rtype: bool
        """
        return to_bool(self._settings.value(self.AUTOSTARTEIP_KEY, False))

    def set_autostart_eip(self, autostart):
        """
        Sets whether the app should autostart EIP.

        :param autostart: True if we should try to autostart EIP.
        :type autostart: bool
        """
        leap_assert_type(autostart, bool)
        self._settings.setValue(self.AUTOSTARTEIP_KEY, autostart)

    def get_alert_missing_scripts(self):
        """
        Returns the setting for alerting of missing up/down scripts.

        :rtype: bool
        """
        return to_bool(self._settings.value(self.ALERTMISSING_KEY, True))

    def set_alert_missing_scripts(self, value):
        """
        Sets the setting for alerting of missing up/down scripts.

        :param value: the value to set
        :type value: bool
        """
        leap_assert_type(value, bool)
        self._settings.setValue(self.ALERTMISSING_KEY, value)

    def get_skip_first_run(self):
        """
        Gets the setting for skip running the first run wizard.

        :returns: if the first run wizard should be skipped or not
        :rtype: bool
        """
        return to_bool(self._settings.value(self.SKIPFIRSTRUN_KEY, False))

    def set_skip_first_run(self, skip):
        """
        Gets the setting for skip the first run wizard.

        :param skip: if the first run wizard should be skipped or not
        :type skip: bool
        """
        leap_assert_type(skip, bool)
        self._settings.setValue(self.SKIPFIRSTRUN_KEY, skip)

    def get_pixelmail_enabled(self):
        return to_bool(self._settings.value(self.PIXELMAIL_KEY, False))

    def set_pixelmail_enabled(self, enabled):
        leap_assert_type(enabled, bool)
        self._settings.setvalue(self.PIXELMAIL_KEY, enabled)