]> git.lizzy.rs Git - torbrowser-launcher.git/blob - torbrowser_launcher/settings.py
Update copyright to 2021
[torbrowser-launcher.git] / torbrowser_launcher / settings.py
1 """
2 Tor Browser Launcher
3 https://github.com/micahflee/torbrowser-launcher/
4
5 Copyright (c) 2013-2021 Micah Lee <micah@micahflee.com>
6
7 Permission is hereby granted, free of charge, to any person
8 obtaining a copy of this software and associated documentation
9 files (the "Software"), to deal in the Software without
10 restriction, including without limitation the rights to use,
11 copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following
14 conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 OTHER DEALINGS IN THE SOFTWARE.
27 """
28
29 import subprocess
30 import shutil
31
32 from PyQt5 import QtCore, QtWidgets, QtGui
33
34
35 class Settings(QtWidgets.QMainWindow):
36     """
37     Settings window.
38     """
39
40     def __init__(self, common, app):
41         super(Settings, self).__init__()
42
43         self.common = common
44         self.app = app
45
46         # Set up the window
47         self.setWindowTitle(_("Tor Browser Launcher Settings"))
48         self.setWindowIcon(QtGui.QIcon(self.common.paths["icon_file"]))
49
50         # Download over system tor
51         self.tor_download_checkbox = QtWidgets.QCheckBox(_("Download over system Tor"))
52         if self.common.settings["download_over_tor"]:
53             self.tor_download_checkbox.setCheckState(QtCore.Qt.Checked)
54         else:
55             self.tor_download_checkbox.setCheckState(QtCore.Qt.Unchecked)
56
57         # Force en-US, only display if language isn't already en-US
58         self.force_en_checkbox = QtWidgets.QCheckBox(
59             _("Force downloading English version of Tor Browser")
60         )
61         if self.common.settings["force_en-US"]:
62             self.force_en_checkbox.setCheckState(QtCore.Qt.Checked)
63         else:
64             self.force_en_checkbox.setCheckState(QtCore.Qt.Unchecked)
65         if self.common.language == "en-US":
66             self.force_en_checkbox.hide()
67
68         # Tor SOCKS address
69         tor_addr_label = QtWidgets.QLabel(_("Tor server"))
70         self.tor_addr = QtWidgets.QLineEdit()
71         self.tor_addr.setText(self.common.settings["tor_socks_address"])
72         tor_addr_layout = QtWidgets.QHBoxLayout()
73         tor_addr_layout.addWidget(tor_addr_label)
74         tor_addr_layout.addWidget(self.tor_addr)
75
76         # Settings layout
77         settings_layout = QtWidgets.QVBoxLayout()
78         settings_layout.addWidget(self.tor_download_checkbox)
79         settings_layout.addWidget(self.force_en_checkbox)
80         settings_layout.addLayout(tor_addr_layout)
81
82         # Status
83         status_label = QtWidgets.QLabel()
84         if self.common.settings["installed"]:
85             status_label.setText(_("Status: Installed"))
86         else:
87             status_label.setText(_("Status: Not Installed"))
88
89         # Install button
90         install_button = QtWidgets.QPushButton(_("Install Tor Browser"))
91         install_button.setIcon(
92             self.style().standardIcon(QtWidgets.QStyle.SP_DialogApplyButton)
93         )
94         install_button.clicked.connect(self.install)
95
96         # Reinstall buttons
97         reinstall_button = QtWidgets.QPushButton(_("Reinstall Tor Browser"))
98         reinstall_button.setIcon(
99             self.style().standardIcon(QtWidgets.QStyle.SP_DialogApplyButton)
100         )
101         reinstall_button.clicked.connect(self.reinstall)
102
103         if self.common.settings["installed"]:
104             install_button.hide()
105             reinstall_button.show()
106         else:
107             install_button.show()
108             reinstall_button.hide()
109
110         # Status layout
111         status_layout = QtWidgets.QVBoxLayout()
112         status_layout.addWidget(status_label)
113         status_layout.addWidget(install_button)
114         status_layout.addWidget(reinstall_button)
115
116         # Top layout
117         top_layout = QtWidgets.QHBoxLayout()
118         top_layout.addLayout(settings_layout)
119         top_layout.addLayout(status_layout)
120
121         # Mirror
122         mirror_label = QtWidgets.QLabel(_("Mirror"))
123
124         self.mirror = QtWidgets.QComboBox()
125         for mirror in self.common.mirrors:
126             self.mirror.addItem(mirror)
127
128         if self.common.settings["mirror"] in self.common.mirrors:
129             self.mirror.setCurrentIndex(
130                 self.mirror.findText(self.common.settings["mirror"])
131             )
132         else:
133             self.mirror.setCurrentIndex(0)
134
135         mirror_layout = QtWidgets.QHBoxLayout()
136         mirror_layout.addWidget(mirror_label)
137         mirror_layout.addWidget(self.mirror)
138
139         # Save & Exit button
140         self.save_exit_button = QtWidgets.QPushButton(_("Save && Exit"))
141         self.save_exit_button.setIcon(
142             self.style().standardIcon(QtWidgets.QStyle.SP_DialogApplyButton)
143         )
144         self.save_exit_button.clicked.connect(self.save_exit)
145
146         # Cancel button
147         self.cancel_button = QtWidgets.QPushButton(_("Cancel"))
148         self.cancel_button.setIcon(
149             self.style().standardIcon(QtWidgets.QStyle.SP_DialogCancelButton)
150         )
151         self.cancel_button.clicked.connect(self.close)
152
153         # Buttons layout
154         buttons_layout = QtWidgets.QHBoxLayout()
155         buttons_layout.addWidget(self.save_exit_button)
156         buttons_layout.addWidget(self.cancel_button)
157
158         # Main layout
159         layout = QtWidgets.QVBoxLayout()
160         layout.addLayout(top_layout)
161         layout.addLayout(mirror_layout)
162         layout.addLayout(buttons_layout)
163
164         central_widget = QtWidgets.QWidget()
165         central_widget.setLayout(layout)
166         self.setCentralWidget(central_widget)
167
168     # Install
169     def install(self):
170         self.save()
171         subprocess.Popen([self.common.paths["tbl_bin"]])
172         self.close()
173
174     # Reinstall
175     def reinstall(self):
176         self.save()
177         shutil.rmtree(self.common.paths["tbb"]["dir"])
178         subprocess.Popen([self.common.paths["tbl_bin"]])
179         self.close()
180
181     # Save & Exit
182     def save_exit(self):
183         self.save()
184         self.close()
185
186     # Save settings
187     def save(self):
188         # Checkbox options
189         self.common.settings[
190             "download_over_tor"
191         ] = self.tor_download_checkbox.isChecked()
192         self.common.settings["force_en-US"] = self.force_en_checkbox.isChecked()
193         self.common.settings["tor_socks_address"] = self.tor_addr.text()
194
195         # Figure out the selected mirror
196         self.common.settings["mirror"] = self.mirror.currentText()
197
198         # Save them
199         self.common.save_settings()