From 09ac74a1be1842397dc74b7444ec569359b9dec4 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Mon, 6 Dec 2021 20:01:28 -0800 Subject: [PATCH] Add country dropdown --- cli/onionshare_cli/common.py | 1 - desktop/src/onionshare/connection_tab.py | 32 ++++++++++++++++--- desktop/src/onionshare/gui_common.py | 23 ++++++++++--- .../src/onionshare/resources/locale/en.json | 2 +- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/cli/onionshare_cli/common.py b/cli/onionshare_cli/common.py index a8e32411..87ecff93 100644 --- a/cli/onionshare_cli/common.py +++ b/cli/onionshare_cli/common.py @@ -312,7 +312,6 @@ class Common: """ Returns the absolute path of a resource """ - self.log("Common", "get_resource_path", f"filename={filename}") path = resource_filename("onionshare_cli", os.path.join("resources", filename)) self.log("Common", "get_resource_path", f"filename={filename}, path={path}") return path diff --git a/desktop/src/onionshare/connection_tab.py b/desktop/src/onionshare/connection_tab.py index 9b0c188d..b7381926 100644 --- a/desktop/src/onionshare/connection_tab.py +++ b/desktop/src/onionshare/connection_tab.py @@ -18,6 +18,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ +import json +import os from PySide2 import QtCore, QtWidgets, QtGui from onionshare_cli.settings import Settings @@ -57,7 +59,9 @@ class AutoConnectTab(QtWidgets.QWidget): QtGui.QPixmap.fromImage( QtGui.QImage( GuiCommon.get_resource_path( - "images/{}_logo_text_bg.png".format(common.gui.color_mode) + os.path.join( + "images", f"{common.gui.color_mode}_logo_text_bg.png" + ) ) ) ) @@ -291,7 +295,22 @@ class AutoConnectUseBridgeWidget(QtWidgets.QWidget): detect_layout.addWidget(self.detect_automatic_radio) detect_layout.addWidget(self.detect_manual_radio) - # World map + # Country list + locale = self.common.settings.get("locale") + if not locale: + locale = "en" + + with open( + GuiCommon.get_resource_path(os.path.join("countries", f"{locale}.json")) + ) as f: + countries = json.loads(f.read()) + + self.country_combobox = QtWidgets.QComboBox() + self.country_combobox.setStyleSheet( + common.gui.css["autoconnect_countries_combobox"] + ) + for country_code in countries: + self.country_combobox.addItem(countries[country_code], country_code) # Buttons self.connect_button = QtWidgets.QPushButton( @@ -326,9 +345,12 @@ class AutoConnectUseBridgeWidget(QtWidgets.QWidget): layout = QtWidgets.QVBoxLayout() layout.addWidget(description_label) layout.addLayout(detect_layout) + layout.addWidget(self.country_combobox) layout.addWidget(cta_widget) self.setLayout(layout) + self.detect_automatic_radio.setChecked(True) + def hide_buttons(self): self.connect_button.hide() self.configure_button.hide() @@ -338,10 +360,12 @@ class AutoConnectUseBridgeWidget(QtWidgets.QWidget): self.configure_button.show() def _detect_automatic_toggled(self): - pass + self.country_combobox.setEnabled(False) + self.country_combobox.hide() def _detect_manual_toggled(self): - pass + self.country_combobox.setEnabled(True) + self.country_combobox.show() def _connect_clicked(self): self.connect_clicked.emit() diff --git a/desktop/src/onionshare/gui_common.py b/desktop/src/onionshare/gui_common.py index dc9c74b6..a9bc9795 100644 --- a/desktop/src/onionshare/gui_common.py +++ b/desktop/src/onionshare/gui_common.py @@ -176,6 +176,15 @@ class GuiCommon: width: 0; height: 0; }""", + "autoconnect_countries_combobox": """ + QComboBox { + padding: 10px; + font-size: 16px; + } + QComboBox:disabled { + color: #666666; + } + """, # Common styles between modes and their child widgets "mode_settings_toggle_advanced": """ QPushButton { @@ -562,9 +571,13 @@ class ToggleCheckbox(QtWidgets.QCheckBox): self.initStyleOption(opt) s = self.style() s.drawControl(QtWidgets.QStyle.CE_CheckBox, opt, painter, self) - - rect = QtCore.QRect(s.subElementRect(QtWidgets.QStyle.SE_CheckBoxContents, opt, self)) - x = rect.width() - rect.x() - self.w + 20 # 20 is the padding between text and toggle + + rect = QtCore.QRect( + s.subElementRect(QtWidgets.QStyle.SE_CheckBoxContents, opt, self) + ) + x = ( + rect.width() - rect.x() - self.w + 20 + ) # 20 is the padding between text and toggle y = self.height() / 2 - self.h / 2 + self.y() / 2 self.toggleRect = QtCore.QRect(x, y, self.w, self.h) painter.setBrush(QtGui.QColor(self.bg_color)) @@ -574,6 +587,8 @@ class ToggleCheckbox(QtWidgets.QCheckBox): painter.drawEllipse(x, y - 3, self.h + 6, self.h + 6) else: painter.setBrush(QtGui.QColor(self.active_color)) - painter.drawEllipse(x + self.w - (self.h + 6), y - 3, self.h + 6, self.h + 6) + painter.drawEllipse( + x + self.w - (self.h + 6), y - 3, self.h + 6, self.h + 6 + ) painter.end() diff --git a/desktop/src/onionshare/resources/locale/en.json b/desktop/src/onionshare/resources/locale/en.json index 48fc16f2..e1a543e1 100644 --- a/desktop/src/onionshare/resources/locale/en.json +++ b/desktop/src/onionshare/resources/locale/en.json @@ -44,7 +44,7 @@ "gui_tor_settings_window_title": "Tor Settings", "gui_autoconnect_description": "OnionShare relies on the Tor Network, run by thousands of volunteers around the world.", "gui_enable_autoconnect_checkbox": "Enable automatically connecting to Tor", - "gui_autoconnect_bridge_description": "Failed connecting to Tor. This could be because your internet is being censored, and you might be able to bypass this censorship by using a bridge.", + "gui_autoconnect_bridge_description": "Failed connecting to Tor. This could be because your internet is being censored. You might be able to bypass this censorship by using a bridge.", "gui_autoconnect_bridge_detect_automatic": "Automatically determine my location to bypass country-specific censorship", "gui_autoconnect_bridge_detect_manual": "Manually select my country", "gui_autoconnect_start": "Connect to Tor",