]> git.lizzy.rs Git - torbrowser-launcher.git/blob - torbrowser_launcher/settings.py
545fab505dda4c17a44d8589aa42f673b4b73953
[torbrowser-launcher.git] / torbrowser_launcher / settings.py
1 """
2 Tor Browser Launcher
3 https://github.com/micahflee/torbrowser-launcher/
4
5 Copyright (c) 2013-2017 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     def __init__(self, common, app):
40         super(Settings, self).__init__()
41
42         self.common = common
43         self.app = app
44
45         # Set up the window
46         self.setWindowTitle(_("Tor Browser Launcher Settings"))
47         self.setWindowIcon(QtGui.QIcon(self.common.paths['icon_file']))
48
49         # Download over system tor
50         self.tor_download_checkbox = QtWidgets.QCheckBox(_("Download over system Tor"))
51         if self.common.settings['download_over_tor']:
52             self.tor_download_checkbox.setCheckState(QtCore.Qt.Checked)
53         else:
54             self.tor_download_checkbox.setCheckState(QtCore.Qt.Unchecked)
55
56         # Force en-US, only display if language isn't already en-US
57         self.force_en_checkbox = QtWidgets.QCheckBox(_("Force downloading English version of Tor Browser"))
58         if self.common.settings['force_en-US']:
59             self.force_en_checkbox.setCheckState(QtCore.Qt.Checked)
60         else:
61             self.force_en_checkbox.setCheckState(QtCore.Qt.Unchecked)
62         if self.common.language == 'en-US':
63             self.force_en_checkbox.hide()
64
65         # Tor SOCKS address
66         tor_addr_label = QtWidgets.QLabel(_('Tor server'))
67         self.tor_addr = QtWidgets.QLineEdit()
68         self.tor_addr.setText(self.common.settings['tor_socks_address'])
69         tor_addr_layout = QtWidgets.QHBoxLayout()
70         tor_addr_layout.addWidget(tor_addr_label)
71         tor_addr_layout.addWidget(self.tor_addr)
72
73         # Settings layout
74         settings_layout = QtWidgets.QVBoxLayout()
75         settings_layout.addWidget(self.tor_download_checkbox)
76         settings_layout.addWidget(self.force_en_checkbox)
77         settings_layout.addLayout(tor_addr_layout)
78
79         # Status
80         status_label = QtWidgets.QLabel()
81         if(self.common.settings['installed']):
82             status_label.setText(_('Status: Installed'))
83         else:
84             status_label.setText(_('Status: Not Installed'))
85
86         # Install button
87         install_button = QtWidgets.QPushButton(_("Install Tor Browser"))
88         install_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_DialogApplyButton))
89         install_button.clicked.connect(self.install)
90
91         # Reinstall buttons
92         reinstall_button = QtWidgets.QPushButton(_("Reinstall Tor Browser"))
93         reinstall_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_DialogApplyButton))
94         reinstall_button.clicked.connect(self.reinstall)
95
96         if(self.common.settings['installed']):
97             install_button.hide()
98             reinstall_button.show()
99         else:
100             install_button.show()
101             reinstall_button.hide()
102
103         # Status layout
104         status_layout = QtWidgets.QVBoxLayout()
105         status_layout.addWidget(status_label)
106         status_layout.addWidget(install_button)
107         status_layout.addWidget(reinstall_button)
108
109         # Top layout
110         top_layout = QtWidgets.QHBoxLayout()
111         top_layout.addLayout(settings_layout)
112         top_layout.addLayout(status_layout)
113
114         # Mirror
115         mirror_label = QtWidgets.QLabel(_('Mirror'))
116
117         self.mirror = QtWidgets.QComboBox()
118         for mirror in self.common.mirrors:
119             self.mirror.addItem(mirror)
120
121         if self.common.settings['mirror'] in self.common.mirrors:
122             self.mirror.setCurrentIndex(self.mirror.findText(self.common.settings['mirror']))
123         else:
124             self.mirror.setCurrentIndex(0)
125
126         mirror_layout = QtWidgets.QHBoxLayout()
127         mirror_layout.addWidget(mirror_label)
128         mirror_layout.addWidget(self.mirror)
129
130         # Save & Exit button
131         self.save_exit_button = QtWidgets.QPushButton(_("Save && Exit"))
132         self.save_exit_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_DialogApplyButton))
133         self.save_exit_button.clicked.connect(self.save_exit)
134
135         # Cancel button
136         self.cancel_button = QtWidgets.QPushButton(_("Cancel"))
137         self.cancel_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_DialogCancelButton))
138         self.cancel_button.clicked.connect(self.close)
139
140         # Buttons layout
141         buttons_layout = QtWidgets.QHBoxLayout()
142         buttons_layout.addWidget(self.save_exit_button)
143         buttons_layout.addWidget(self.cancel_button)
144
145         # Main layout
146         layout = QtWidgets.QVBoxLayout()
147         layout.addLayout(top_layout)
148         layout.addLayout(mirror_layout)
149         layout.addLayout(buttons_layout)
150
151         central_widget = QtWidgets.QWidget()
152         central_widget.setLayout(layout)
153         self.setCentralWidget(central_widget)
154
155     # Install
156     def install(self):
157         self.save()
158         subprocess.Popen([self.common.paths['tbl_bin']])
159         self.close()
160
161     # Reinstall
162     def reinstall(self):
163         self.save()
164         shutil.rmtree(self.common.paths['tbb']['dir'])
165         subprocess.Popen([self.common.paths['tbl_bin']])
166         self.close()
167
168     # Save & Exit
169     def save_exit(self):
170         self.save()
171         self.close()
172
173     # Save settings
174     def save(self):
175         # Checkbox options
176         self.common.settings['download_over_tor'] = self.tor_download_checkbox.isChecked()
177         self.common.settings['force_en-US'] = self.force_en_checkbox.isChecked()
178         self.common.settings['tor_socks_address'] = self.tor_addr.text()
179
180         # Figure out the selected mirror
181         self.common.settings['mirror'] = self.mirror.currentText()
182
183         # Save them
184         self.common.save_settings()