From d7a2d227e9c684a3faec08e1e00b54ddc4c96638 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Sat, 19 Sep 2015 15:41:09 +0200 Subject: [PATCH] Enable dynamic preferences ( #139 ) --- res/ui/preferences.ui | 45 ++------------------------------ src/cutecoin/core/app.py | 31 +++++++++++++++------- src/cutecoin/core/net/network.py | 4 +-- src/cutecoin/gui/preferences.py | 2 -- 4 files changed, 26 insertions(+), 56 deletions(-) diff --git a/res/ui/preferences.ui b/res/ui/preferences.ui index cd29ddda..942d581d 100644 --- a/res/ui/preferences.ui +++ b/res/ui/preferences.ui @@ -96,7 +96,7 @@ <item> <widget class="QStackedWidget" name="stackedWidget"> <property name="currentIndex"> - <number>1</number> + <number>0</number> </property> <widget class="QWidget" name="page"> <layout class="QVBoxLayout" name="verticalLayout_7"> @@ -333,51 +333,10 @@ <item> <widget class="QCheckBox" name="checkbox_proxy"> <property name="text"> - <string>Use a proxy server</string> + <string>Use a http proxy server</string> </property> </widget> </item> - <item> - <layout class="QHBoxLayout" name="horizontalLayout_11"> - <property name="topMargin"> - <number>6</number> - </property> - <item> - <widget class="QLabel" name="label_8"> - <property name="text"> - <string>Proxy type : </string> - </property> - </widget> - </item> - <item> - <widget class="QComboBox" name="combox_proxytype"> - <item> - <property name="text"> - <string>HTTP</string> - </property> - </item> - <item> - <property name="text"> - <string>SOCKS5</string> - </property> - </item> - </widget> - </item> - <item> - <spacer name="horizontalSpacer_4"> - <property name="orientation"> - <enum>Qt::Horizontal</enum> - </property> - <property name="sizeHint" stdset="0"> - <size> - <width>40</width> - <height>20</height> - </size> - </property> - </spacer> - </item> - </layout> - </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_9"> <item> diff --git a/src/cutecoin/core/app.py b/src/cutecoin/core/app.py index 4ae3e38a..fe4c27a9 100644 --- a/src/cutecoin/core/app.py +++ b/src/cutecoin/core/app.py @@ -23,6 +23,7 @@ from .registry.identities import IdentitiesRegistry from .. import __version__ from ..tools.exceptions import NameAlreadyExists, BadAccountFile from ..tools.decorators import asyncify +import i18n_rc class Application(QObject): @@ -52,6 +53,7 @@ class Application(QObject): self.available_version = (True, __version__, "") + self._translator = QTranslator(self.qapp) self._identities_registry = identities_registry self.preferences = {'account': "", 'lang': 'en_GB', @@ -74,10 +76,7 @@ class Application(QObject): app = cls(qapp, loop, identities_registry) app.load() app.switch_language() - if app.preferences['enable_proxy'] is True: - API.aiohttp_connector = ProxyConnector("http://{0}:{1}".format( - app.preferences['proxy_address'], - app.preferences['proxy_port'])) + app.set_proxy() if app.preferences["account"] != "": account = app.get_account(app.preferences["account"]) app.change_current_account(account) @@ -96,16 +95,28 @@ class Application(QObject): return app + def set_proxy(self): + if self.preferences['enable_proxy'] is True: + API.aiohttp_connector = ProxyConnector("http://{0}:{1}".format( + self.preferences['proxy_address'], + self.preferences['proxy_port'])) + else: + API.aiohttp_connector = None + def switch_language(self): - translator = QTranslator(self.qapp) logging.debug("Loading translations") locale = self.preferences['lang'] QLocale.setDefault(QLocale(locale)) - if translator.load(":/i18n/{0}".format(locale)): - if QCoreApplication.installTranslator(translator): - logging.debug("Loaded i18n/{0}".format(locale)) + if locale == "en_GB": + QCoreApplication.removeTranslator(self._translator) else: - logging.debug("Couldn't load translation") + QCoreApplication.removeTranslator(self._translator) + self._translator = QTranslator(self.qapp) + if self._translator.load(":/i18n/{0}".format(locale)): + if QCoreApplication.installTranslator(self._translator): + logging.debug("Loaded i18n/{0}".format(locale)) + else: + logging.debug("Couldn't load translation") def get_account(self, name): """ @@ -310,6 +321,8 @@ class Application(QObject): with open(preferences_path, 'w') as outfile: json.dump(preferences, outfile, indent=4) + self.set_proxy() + def save(self, account): """ Save an account diff --git a/src/cutecoin/core/net/network.py b/src/cutecoin/core/net/network.py index f7bfeb96..f9d7dc6e 100644 --- a/src/cutecoin/core/net/network.py +++ b/src/cutecoin/core/net/network.py @@ -328,12 +328,12 @@ class Network(QObject): node = self.sender() if node.state in (Node.ONLINE, Node.DESYNCED): self.check_nodes_sync() - self.nodes_changed.emit() else: if node.last_change + 3600 < time.time(): node.disconnect() self.nodes.remove(node) - self.nodes_changed.emit() + + self.nodes_changed.emit() logging.debug("{0} -> {1}".format(self._block_found[:10], self.latest_block_hash[:10])) if self._block_found != self.latest_block_hash and node.state == Node.ONLINE: diff --git a/src/cutecoin/gui/preferences.py b/src/cutecoin/gui/preferences.py index 919d50de..32d541e4 100644 --- a/src/cutecoin/gui/preferences.py +++ b/src/cutecoin/gui/preferences.py @@ -80,8 +80,6 @@ class PreferencesDialog(QDialog, Ui_PreferencesDialog): self.app.save_preferences(pref) # change UI translation self.app.switch_language() - toast.display(self.tr("Preferences"), - self.tr("A restart is needed to apply your new preferences.")) super().accept() def reject(self): -- GitLab