2014-09-02 20:30:01 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-02 15:10:42 -04:00
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2015-09-08 00:48:49 -04:00
|
|
|
Copyright (C) 2015 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
|
2014-08-27 19:11:43 -04:00
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
|
|
|
import common
|
|
|
|
from onionshare import strings, helpers
|
|
|
|
|
2014-11-18 12:29:32 -05:00
|
|
|
|
2014-08-27 19:11:43 -04:00
|
|
|
class ServerStatus(QtGui.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()
|
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
|
|
|
|
|
2014-08-27 19:11:43 -04:00
|
|
|
# server layout
|
2014-09-03 20:50:06 -04:00
|
|
|
self.status_image_stopped = QtGui.QImage(common.get_image_path('server_stopped.png'))
|
|
|
|
self.status_image_working = QtGui.QImage(common.get_image_path('server_working.png'))
|
|
|
|
self.status_image_started = QtGui.QImage(common.get_image_path('server_started.png'))
|
2014-08-27 20:24:44 -04:00
|
|
|
self.status_image_label = QtGui.QLabel()
|
|
|
|
self.status_image_label.setFixedWidth(30)
|
2014-09-17 20:37:19 -04:00
|
|
|
self.server_button = QtGui.QPushButton()
|
|
|
|
self.server_button.clicked.connect(self.server_button_clicked)
|
2014-08-27 19:11:43 -04:00
|
|
|
server_layout = QtGui.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()
|
2014-08-27 20:24:44 -04:00
|
|
|
self.url_label = QtGui.QLabel()
|
2014-08-27 19:43:18 -04:00
|
|
|
self.url_label.setFont(url_font)
|
|
|
|
self.url_label.setWordWrap(True)
|
|
|
|
self.url_label.setAlignment(QtCore.Qt.AlignCenter)
|
2014-09-15 22:09:37 -04:00
|
|
|
self.copy_url_button = QtGui.QPushButton(strings._('gui_copy_url', True))
|
2014-08-27 19:11:43 -04:00
|
|
|
self.copy_url_button.clicked.connect(self.copy_url)
|
|
|
|
url_layout = QtGui.QHBoxLayout()
|
|
|
|
url_layout.addWidget(self.url_label)
|
|
|
|
url_layout.addWidget(self.copy_url_button)
|
|
|
|
|
|
|
|
# add the widgets
|
|
|
|
self.addLayout(server_layout)
|
|
|
|
self.addLayout(url_layout)
|
|
|
|
|
2014-08-27 20:24:44 -04:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
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:
|
2015-05-15 15:26:58 -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
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
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))
|
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))
|
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'))
|
|
|
|
|
|
|
|
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
|
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
|
|
|
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
# Qt's QClipboard isn't working in Windows
|
|
|
|
# https://github.com/micahflee/onionshare/issues/46
|
|
|
|
import ctypes
|
|
|
|
GMEM_DDESHARE = 0x2000
|
|
|
|
ctypes.windll.user32.OpenClipboard(None)
|
|
|
|
ctypes.windll.user32.EmptyClipboard()
|
2015-06-26 13:38:22 -04:00
|
|
|
hcd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(url)) + 1)
|
2014-08-27 22:07:15 -04:00
|
|
|
pch_data = ctypes.windll.kernel32.GlobalLock(hcd)
|
|
|
|
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pch_data), bytes(url))
|
|
|
|
ctypes.windll.kernel32.GlobalUnlock(hcd)
|
|
|
|
ctypes.windll.user32.SetClipboardData(1, hcd)
|
|
|
|
ctypes.windll.user32.CloseClipboard()
|
|
|
|
else:
|
|
|
|
clipboard = self.qtapp.clipboard()
|
|
|
|
clipboard.setText(url)
|
|
|
|
|
2014-08-29 18:43:07 -04:00
|
|
|
self.url_copied.emit()
|