summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/backend
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2014-07-11 17:24:20 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2014-07-14 12:56:55 -0300
commit0aee7d6cbc3f2c0b764056966eeddc4057eafd08 (patch)
tree2b9d50cc39a0debc2b581b1e035e81f4f8b67b1e /src/leap/bitmask/backend
parent525433088d6fbe3392af90942272dfd5dd2511d6 (diff)
Use specific settings for backend.
This way we get rid of Qt on the backend side. The use of QSettings in the backend was breaking the app on OSX.
Diffstat (limited to 'src/leap/bitmask/backend')
-rw-r--r--src/leap/bitmask/backend/api.py1
-rw-r--r--src/leap/bitmask/backend/components.py8
-rw-r--r--src/leap/bitmask/backend/leapbackend.py14
-rw-r--r--src/leap/bitmask/backend/settings.py158
4 files changed, 177 insertions, 4 deletions
diff --git a/src/leap/bitmask/backend/api.py b/src/leap/bitmask/backend/api.py
index f8b8c699..b8533f36 100644
--- a/src/leap/bitmask/backend/api.py
+++ b/src/leap/bitmask/backend/api.py
@@ -45,6 +45,7 @@ API = (
"provider_get_pinned_providers",
"provider_get_supported_services",
"provider_setup",
+ "settings_set_selected_gateway",
"smtp_start_service",
"smtp_stop_service",
"soledad_bootstrap",
diff --git a/src/leap/bitmask/backend/components.py b/src/leap/bitmask/backend/components.py
index fe7b566a..8b471b14 100644
--- a/src/leap/bitmask/backend/components.py
+++ b/src/leap/bitmask/backend/components.py
@@ -31,7 +31,7 @@ from twisted.python import log
import zope.interface
import zope.proxy
-from leap.bitmask.config.leapsettings import LeapSettings
+from leap.bitmask.backend.settings import Settings, GATEWAY_AUTOMATIC
from leap.bitmask.config.providerconfig import ProviderConfig
from leap.bitmask.crypto.srpauth import SRPAuth
from leap.bitmask.crypto.srpregister import SRPRegister
@@ -602,7 +602,7 @@ class EIP(object):
eip_get_gateway_country_code -> str
eip_no_gateway
"""
- leap_settings = LeapSettings()
+ settings = Settings()
eip_config = eipconfig.EIPConfig()
provider_config = ProviderConfig.get_provider_config(domain)
@@ -612,9 +612,9 @@ class EIP(object):
eip_config.load(eipconfig.get_eipconfig_path(domain))
gateway_selector = eipconfig.VPNGatewaySelector(eip_config)
- gateway_conf = leap_settings.get_selected_gateway(domain)
+ gateway_conf = settings.get_selected_gateway(domain)
- if gateway_conf == leap_settings.GATEWAY_AUTOMATIC:
+ if gateway_conf == GATEWAY_AUTOMATIC:
gateways = gateway_selector.get_gateways()
else:
gateways = [gateway_conf]
diff --git a/src/leap/bitmask/backend/leapbackend.py b/src/leap/bitmask/backend/leapbackend.py
index 3bc7a513..d3c4fcda 100644
--- a/src/leap/bitmask/backend/leapbackend.py
+++ b/src/leap/bitmask/backend/leapbackend.py
@@ -24,6 +24,7 @@ import zope.proxy
from leap.bitmask.backend import components
from leap.bitmask.backend.backend import Backend
+from leap.bitmask.backend.settings import Settings
logger = logging.getLogger(__name__)
@@ -41,6 +42,8 @@ class LeapBackend(Backend):
"""
Backend.__init__(self)
+ self._settings = Settings()
+
# Objects needed by several components, so we make a proxy and pass
# them around
self._soledad_proxy = zope.proxy.ProxyBase(None)
@@ -515,3 +518,14 @@ class LeapBackend(Backend):
imap_stopped
"""
self._mail.stop_imap_service()
+
+ def settings_set_selected_gateway(self, provider, gateway):
+ """
+ Set the selected gateway for a given provider.
+
+ :param provider: provider domain
+ :type provider: str
+ :param gateway: gateway to use as default
+ :type gateway: str
+ """
+ self._settings.set_selected_gateway(provider, gateway)
diff --git a/src/leap/bitmask/backend/settings.py b/src/leap/bitmask/backend/settings.py
new file mode 100644
index 00000000..5cb4c616
--- /dev/null
+++ b/src/leap/bitmask/backend/settings.py
@@ -0,0 +1,158 @@
+# -*- coding: utf-8 -*-
+# settings.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/>.
+"""
+Backend settings
+"""
+import ConfigParser
+import logging
+import os
+
+from leap.bitmask.util import get_path_prefix
+from leap.common.check import leap_assert, leap_assert_type
+
+logger = logging.getLogger(__name__)
+
+# We need this one available for the default decorator
+GATEWAY_AUTOMATIC = "Automatic"
+GENERAL_SECTION = "General"
+
+
+class Settings(object):
+ """
+ Leap backend settings hanler.
+ """
+ CONFIG_NAME = "leap-backend.conf"
+
+ # keys
+ GATEWAY_KEY = "Gateway"
+
+ def __init__(self):
+ """
+ Create the ConfigParser object and read it.
+ """
+ self._settings_path = os.path.join(get_path_prefix(),
+ "leap", self.CONFIG_NAME)
+
+ self._settings = ConfigParser.ConfigParser()
+ self._settings.read(self._settings_path)
+
+ self._add_section(GENERAL_SECTION)
+
+ def _add_section(self, section):
+ """
+ Add `section` to the config file and don't fail if already exists.
+
+ :param section: the section to add.
+ :type section: str
+ """
+ self._settings.read(self._settings_path)
+ try:
+ self._settings.add_section(section)
+ except ConfigParser.DuplicateSectionError:
+ pass
+
+ def _save(self):
+ """
+ Save the current state to the config file.
+ """
+ with open(self._settings_path, 'wb') as f:
+ self._settings.write(f)
+
+ def _get_value(self, section, key, default):
+ """
+ Return the value for the fiven `key` in `section`.
+ If there's no such section/key, `default` is returned.
+
+ :param section: the section to get the value from.
+ :type section: str
+ :param key: the key which value we want to get.
+ :type key: str
+ :param default: the value to return if there is no section/key.
+ :type default: object
+
+ :rtype: object
+ """
+ try:
+ return self._settings.get(section, key)
+ except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+ return default
+
+ def get_selected_gateway(self, provider):
+ """
+ Return 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")
+ return self._get_value(provider, self.GATEWAY_KEY, GATEWAY_AUTOMATIC)
+
+ 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))
+
+ self._add_section(provider)
+
+ self._settings.set(provider, self.GATEWAY_KEY, gateway)
+ self._save()
+
+ def get_uuid(self, username):
+ """
+ Gets the uuid for a given username.
+
+ :param username: the full user identifier in the form user@provider
+ :type username: basestring
+ """
+ leap_assert("@" in username,
+ "Expected username in the form user@provider")
+ user, provider = username.split('@')
+
+ return self._get_value(provider, username, "")
+
+ def set_uuid(self, username, value):
+ """
+ Sets the uuid for a given username.
+
+ :param username: the full user identifier in the form user@provider
+ :type username: str or unicode
+ :param value: the uuid to save or None to remove it
+ :type value: str or unicode or None
+ """
+ leap_assert("@" in username,
+ "Expected username in the form user@provider")
+ user, provider = username.split('@')
+
+ if value is None:
+ self._settings.remove_option(provider, username)
+ else:
+ leap_assert(len(value) > 0, "We cannot save an empty uuid")
+ self._add_section(provider)
+ self._settings.set(provider, username, value)
+
+ self._save()