2014-09-02 20:30:01 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-02 15:10:42 -04:00
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2017-01-06 21:58:15 -05:00
|
|
|
Copyright (C) 2017 Micah Lee <micah@micahflee.com>
|
2014-09-02 15:10:42 -04:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
"""
|
2014-08-27 22:07:15 -04:00
|
|
|
import platform
|
2016-02-12 18:12:27 -05:00
|
|
|
from PyQt5 import QtCore, QtWidgets, QtGui
|
2014-08-27 19:11:43 -04:00
|
|
|
|
2017-05-16 14:05:48 -04:00
|
|
|
from onionshare import strings, common
|
2014-08-27 19:11:43 -04:00
|
|
|
|
2016-02-12 18:12:27 -05:00
|
|
|
class ServerStatus(QtWidgets.QVBoxLayout):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
The server status chunk of the GUI.
|
|
|
|
"""
|
2014-08-27 20:24:44 -04:00
|
|
|
server_started = QtCore.pyqtSignal()
|
|
|
|
server_stopped = QtCore.pyqtSignal()
|
2014-08-29 18:43:07 -04:00
|
|
|
url_copied = QtCore.pyqtSignal()
|
2016-12-22 20:47:05 -05:00
|
|
|
hidservauth_copied = QtCore.pyqtSignal()
|
2014-08-27 20:24:44 -04:00
|
|
|
|
|
|
|
STATUS_STOPPED = 0
|
|
|
|
STATUS_WORKING = 1
|
|
|
|
STATUS_STARTED = 2
|
|
|
|
|
2014-08-27 22:07:15 -04:00
|
|
|
def __init__(self, qtapp, app, web, file_selection):
|
2014-08-27 19:11:43 -04:00
|
|
|
super(ServerStatus, self).__init__()
|
2014-08-27 20:24:44 -04:00
|
|
|
self.status = self.STATUS_STOPPED
|
2014-08-27 19:11:43 -04:00
|
|
|
|
2014-08-27 22:07:15 -04:00
|
|
|
self.qtapp = qtapp
|
|
|
|
self.app = app
|
|
|
|
self.web = web
|
2014-08-27 20:24:44 -04:00
|
|
|
self.file_selection = file_selection
|
|
|
|
|
2017-11-08 04:25:59 -05:00
|
|
|
# shutdown timeout layout
|
|
|
|
self.server_shutdown_timeout_checkbox = QtWidgets.QCheckBox()
|
|
|
|
self.server_shutdown_timeout_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
|
|
|
self.server_shutdown_timeout_checkbox.toggled.connect(self.shutdown_timeout_toggled)
|
|
|
|
self.server_shutdown_timeout_checkbox.setText(strings._("gui_settings_shutdown_timeout_choice", True))
|
|
|
|
self.server_shutdown_timeout_label = QtWidgets.QLabel(strings._('gui_settings_shutdown_timeout', True))
|
2017-11-08 19:29:55 -05:00
|
|
|
self.server_shutdown_timeout = QtWidgets.QDateTimeEdit()
|
2017-11-08 19:56:02 -05:00
|
|
|
self.server_shutdown_timeout.setDateTime(QtCore.QDateTime.currentDateTime().addSecs(300))
|
2017-11-08 19:34:59 -05:00
|
|
|
self.server_shutdown_timeout.setMinimumDateTime(QtCore.QDateTime.currentDateTime())
|
2017-11-08 19:29:55 -05:00
|
|
|
self.server_shutdown_timeout.setCurrentSectionIndex(4)
|
2017-11-08 04:25:59 -05:00
|
|
|
self.server_shutdown_timeout_label.hide()
|
|
|
|
self.server_shutdown_timeout.hide()
|
|
|
|
shutdown_timeout_layout_group = QtWidgets.QHBoxLayout()
|
|
|
|
shutdown_timeout_layout_group.addWidget(self.server_shutdown_timeout_checkbox)
|
|
|
|
shutdown_timeout_layout_group.addWidget(self.server_shutdown_timeout_label)
|
|
|
|
shutdown_timeout_layout_group.addWidget(self.server_shutdown_timeout)
|
2014-08-27 19:11:43 -04:00
|
|
|
# server layout
|
2017-05-16 14:05:48 -04:00
|
|
|
self.status_image_stopped = QtGui.QImage(common.get_resource_path('images/server_stopped.png'))
|
|
|
|
self.status_image_working = QtGui.QImage(common.get_resource_path('images/server_working.png'))
|
|
|
|
self.status_image_started = QtGui.QImage(common.get_resource_path('images/server_started.png'))
|
2016-02-12 18:12:27 -05:00
|
|
|
self.status_image_label = QtWidgets.QLabel()
|
2014-08-27 20:24:44 -04:00
|
|
|
self.status_image_label.setFixedWidth(30)
|
2016-02-12 18:12:27 -05:00
|
|
|
self.server_button = QtWidgets.QPushButton()
|
2014-09-17 20:37:19 -04:00
|
|
|
self.server_button.clicked.connect(self.server_button_clicked)
|
2016-02-12 18:12:27 -05:00
|
|
|
server_layout = QtWidgets.QHBoxLayout()
|
2014-08-27 20:24:44 -04:00
|
|
|
server_layout.addWidget(self.status_image_label)
|
2014-09-17 20:37:19 -04:00
|
|
|
server_layout.addWidget(self.server_button)
|
2014-08-27 19:11:43 -04:00
|
|
|
|
|
|
|
# url layout
|
2014-08-27 19:43:18 -04:00
|
|
|
url_font = QtGui.QFont()
|
2016-02-12 18:12:27 -05:00
|
|
|
self.url_label = QtWidgets.QLabel()
|
2014-08-27 19:43:18 -04:00
|
|
|
self.url_label.setFont(url_font)
|
2016-04-10 16:33:16 -04:00
|
|
|
self.url_label.setWordWrap(False)
|
2014-08-27 19:43:18 -04:00
|
|
|
self.url_label.setAlignment(QtCore.Qt.AlignCenter)
|
2016-02-12 18:12:27 -05:00
|
|
|
self.copy_url_button = QtWidgets.QPushButton(strings._('gui_copy_url', True))
|
2014-08-27 19:11:43 -04:00
|
|
|
self.copy_url_button.clicked.connect(self.copy_url)
|
2016-12-22 20:47:05 -05:00
|
|
|
self.copy_hidservauth_button = QtWidgets.QPushButton(strings._('gui_copy_hidservauth', True))
|
|
|
|
self.copy_hidservauth_button.clicked.connect(self.copy_hidservauth)
|
2016-02-12 18:12:27 -05:00
|
|
|
url_layout = QtWidgets.QHBoxLayout()
|
2014-08-27 19:11:43 -04:00
|
|
|
url_layout.addWidget(self.url_label)
|
|
|
|
url_layout.addWidget(self.copy_url_button)
|
2016-12-22 20:47:05 -05:00
|
|
|
url_layout.addWidget(self.copy_hidservauth_button)
|
2014-08-27 19:11:43 -04:00
|
|
|
|
|
|
|
# add the widgets
|
2017-11-08 04:25:59 -05:00
|
|
|
self.addLayout(shutdown_timeout_layout_group)
|
2014-08-27 19:11:43 -04:00
|
|
|
self.addLayout(server_layout)
|
|
|
|
self.addLayout(url_layout)
|
|
|
|
|
2014-08-27 20:24:44 -04:00
|
|
|
self.update()
|
|
|
|
|
2017-11-08 04:25:59 -05:00
|
|
|
def shutdown_timeout_toggled(self, checked):
|
|
|
|
"""
|
|
|
|
Shutdown timer option was toggled. If checked, hide the option and show the timer settings.
|
|
|
|
"""
|
|
|
|
if checked:
|
|
|
|
self.server_shutdown_timeout_checkbox.hide()
|
|
|
|
self.server_shutdown_timeout_label.show()
|
|
|
|
self.server_shutdown_timeout.show()
|
|
|
|
else:
|
|
|
|
self.server_shutdown_timeout_checkbox.show()
|
|
|
|
self.server_shutdown_timeout_label.hide()
|
|
|
|
self.server_shutdown_timeout.hide()
|
|
|
|
|
2014-08-27 20:24:44 -04:00
|
|
|
def update(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Update the GUI elements based on the current state.
|
|
|
|
"""
|
2014-08-27 20:24:44 -04:00
|
|
|
# set the status image
|
|
|
|
if self.status == self.STATUS_STOPPED:
|
|
|
|
self.status_image_label.setPixmap(QtGui.QPixmap.fromImage(self.status_image_stopped))
|
|
|
|
elif self.status == self.STATUS_WORKING:
|
|
|
|
self.status_image_label.setPixmap(QtGui.QPixmap.fromImage(self.status_image_working))
|
|
|
|
elif self.status == self.STATUS_STARTED:
|
|
|
|
self.status_image_label.setPixmap(QtGui.QPixmap.fromImage(self.status_image_started))
|
|
|
|
|
2014-08-27 22:07:15 -04:00
|
|
|
# set the URL fields
|
|
|
|
if self.status == self.STATUS_STARTED:
|
2016-04-10 16:33:16 -04:00
|
|
|
self.url_label.setText('http://{0:s}/{1:s}'.format(self.app.onion_host, self.web.slug))
|
2014-08-27 22:07:15 -04:00
|
|
|
self.url_label.show()
|
|
|
|
self.copy_url_button.show()
|
2015-05-18 14:05:33 -04:00
|
|
|
|
2016-12-22 20:47:05 -05:00
|
|
|
if self.app.stealth:
|
|
|
|
self.copy_hidservauth_button.show()
|
|
|
|
else:
|
|
|
|
self.copy_hidservauth_button.hide()
|
|
|
|
|
2015-05-18 14:05:33 -04:00
|
|
|
# resize parent widget
|
|
|
|
p = self.parentWidget()
|
|
|
|
p.resize(p.sizeHint())
|
2014-08-27 22:07:15 -04:00
|
|
|
else:
|
|
|
|
self.url_label.hide()
|
|
|
|
self.copy_url_button.hide()
|
2016-12-22 20:47:05 -05:00
|
|
|
self.copy_hidservauth_button.hide()
|
2014-08-27 22:07:15 -04:00
|
|
|
|
2014-09-17 20:37:19 -04:00
|
|
|
# button
|
2014-08-27 20:24:44 -04:00
|
|
|
if self.file_selection.get_num_files() == 0:
|
2014-09-17 20:37:19 -04:00
|
|
|
self.server_button.setEnabled(False)
|
|
|
|
self.server_button.setText(strings._('gui_start_server', True))
|
2014-08-27 20:24:44 -04:00
|
|
|
else:
|
|
|
|
if self.status == self.STATUS_STOPPED:
|
2014-09-17 20:37:19 -04:00
|
|
|
self.server_button.setEnabled(True)
|
|
|
|
self.server_button.setText(strings._('gui_start_server', True))
|
2017-11-08 04:25:59 -05:00
|
|
|
self.server_shutdown_timeout.setEnabled(True)
|
2017-11-08 19:49:01 -05:00
|
|
|
self.server_shutdown_timeout_checkbox.show()
|
2017-11-08 04:25:59 -05:00
|
|
|
self.server_shutdown_timeout_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
2014-08-29 18:50:19 -04:00
|
|
|
elif self.status == self.STATUS_STARTED:
|
2014-09-17 20:37:19 -04:00
|
|
|
self.server_button.setEnabled(True)
|
|
|
|
self.server_button.setText(strings._('gui_stop_server', True))
|
2017-11-08 04:25:59 -05:00
|
|
|
self.server_shutdown_timeout.setEnabled(False)
|
2017-11-08 19:46:26 -05:00
|
|
|
self.server_shutdown_timeout_checkbox.hide()
|
|
|
|
elif self.status == self.STATUS_WORKING:
|
|
|
|
self.server_button.setEnabled(False)
|
|
|
|
self.server_button.setText(strings._('gui_please_wait'))
|
|
|
|
self.server_shutdown_timeout.setEnabled(False)
|
|
|
|
self.server_shutdown_timeout_checkbox.hide()
|
2014-08-29 18:50:19 -04:00
|
|
|
else:
|
2014-09-17 20:37:19 -04:00
|
|
|
self.server_button.setEnabled(False)
|
|
|
|
self.server_button.setText(strings._('gui_please_wait'))
|
2017-11-08 04:25:59 -05:00
|
|
|
self.server_shutdown_timeout.setEnabled(False)
|
2014-09-17 20:37:19 -04:00
|
|
|
|
|
|
|
def server_button_clicked(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Toggle starting or stopping the server.
|
|
|
|
"""
|
2014-09-17 20:37:19 -04:00
|
|
|
if self.status == self.STATUS_STOPPED:
|
|
|
|
self.start_server()
|
|
|
|
elif self.status == self.STATUS_STARTED:
|
|
|
|
self.stop_server()
|
2014-08-27 20:24:44 -04:00
|
|
|
|
2014-08-27 19:11:43 -04:00
|
|
|
def start_server(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Start the server.
|
|
|
|
"""
|
2014-08-27 20:24:44 -04:00
|
|
|
self.status = self.STATUS_WORKING
|
|
|
|
self.update()
|
|
|
|
self.server_started.emit()
|
2014-08-27 19:11:43 -04:00
|
|
|
|
2014-08-27 22:07:15 -04:00
|
|
|
def start_server_finished(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
The server has finished starting.
|
|
|
|
"""
|
2014-08-27 22:07:15 -04:00
|
|
|
self.status = self.STATUS_STARTED
|
2017-11-08 19:46:26 -05:00
|
|
|
if self.server_shutdown_timeout_checkbox.isChecked():
|
|
|
|
# Convert the date value to seconds between now and then
|
|
|
|
now = QtCore.QDateTime.currentDateTime()
|
|
|
|
self.timeout = now.secsTo(self.server_shutdown_timeout.dateTime())
|
|
|
|
# Set the shutdown timeout value
|
|
|
|
if self.timeout > 0:
|
|
|
|
self.app.shutdown_timer = common.close_after_seconds(self.timeout)
|
|
|
|
self.app.shutdown_timer.start()
|
2014-08-28 02:52:56 -04:00
|
|
|
self.copy_url()
|
2014-08-27 22:07:15 -04:00
|
|
|
self.update()
|
|
|
|
|
2014-08-27 19:11:43 -04:00
|
|
|
def stop_server(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Stop the server.
|
|
|
|
"""
|
2014-08-27 22:07:15 -04:00
|
|
|
self.status = self.STATUS_WORKING
|
2014-08-27 20:24:44 -04:00
|
|
|
self.update()
|
|
|
|
self.server_stopped.emit()
|
2014-08-27 19:11:43 -04:00
|
|
|
|
2014-08-27 22:07:15 -04:00
|
|
|
def stop_server_finished(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
The server has finished stopping.
|
|
|
|
"""
|
2014-08-27 22:07:15 -04:00
|
|
|
self.status = self.STATUS_STOPPED
|
|
|
|
self.update()
|
|
|
|
|
2014-08-27 19:11:43 -04:00
|
|
|
def copy_url(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Copy the onionshare URL to the clipboard.
|
|
|
|
"""
|
2015-05-15 15:26:58 -04:00
|
|
|
url = 'http://{0:s}/{1:s}'.format(self.app.onion_host, self.web.slug)
|
2014-08-27 22:07:15 -04:00
|
|
|
|
2016-02-15 16:06:12 -05:00
|
|
|
clipboard = self.qtapp.clipboard()
|
|
|
|
clipboard.setText(url)
|
2014-08-27 22:07:15 -04:00
|
|
|
|
2014-08-29 18:43:07 -04:00
|
|
|
self.url_copied.emit()
|
2016-12-22 20:47:05 -05:00
|
|
|
|
|
|
|
def copy_hidservauth(self):
|
|
|
|
"""
|
|
|
|
Copy the HidServAuth line to the clipboard.
|
|
|
|
"""
|
|
|
|
clipboard = self.qtapp.clipboard()
|
|
|
|
clipboard.setText(self.app.auth_string)
|
|
|
|
|
|
|
|
self.hidservauth_copied.emit()
|