FileSelection and ServerStatus interact together, disabling buttons when appropriate

This commit is contained in:
Micah Lee 2014-08-27 17:24:44 -07:00
parent db6600cb71
commit 49eac35196
3 changed files with 87 additions and 21 deletions

View File

@ -6,6 +6,7 @@ from onionshare import strings, helpers
class FileList(QtGui.QListWidget):
files_dropped = QtCore.pyqtSignal()
files_updated = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(FileList, self).__init__(parent)
@ -74,6 +75,8 @@ class FileList(QtGui.QListWidget):
item.setIcon(icon)
self.addItem(item)
self.files_updated.emit()
def human_readable_filesize(self, b):
thresh = 1024.0
if b < thresh:
@ -89,6 +92,7 @@ class FileList(QtGui.QListWidget):
class FileSelection(QtGui.QVBoxLayout):
def __init__(self):
super(FileSelection, self).__init__()
self.server_on = False
# file list
self.file_list = FileList()
@ -114,6 +118,15 @@ class FileSelection(QtGui.QVBoxLayout):
self.update()
def update(self):
# all buttons should be disabled if the server is on
if self.server_on:
self.add_files_button.setEnabled(False)
self.add_dir_button.setEnabled(False)
self.delete_button.setEnabled(False)
else:
self.add_files_button.setEnabled(True)
self.add_dir_button.setEnabled(True)
# delete button should be disabled if item isn't selected
current_item = self.file_list.currentItem()
if not current_item:
@ -143,3 +156,16 @@ class FileSelection(QtGui.QVBoxLayout):
self.file_list.takeItem(current_row)
self.update()
def server_started(self):
self.server_on = True
self.file_list.setAcceptDrops(False)
self.update()
def server_stopped(self):
self.server_on = False
self.file_list.setAcceptDrops(True)
self.update()
def get_num_files(self):
return len(self.file_list.filenames)

View File

@ -24,23 +24,25 @@ class Application(QtGui.QApplication):
QtGui.QApplication.__init__(self, sys.argv)
class OnionShareGui(QtGui.QWidget):
def __init__(self, app, filenames=None):
def __init__(self, app):
super(OnionShareGui, self).__init__()
self.app = app
self.filenames = filenames
self.setWindowTitle('OnionShare')
self.setWindowIcon(window_icon)
def start_send(self):
def start_send(self, filenames=None):
# file selection
file_selection = FileSelection()
if self.filenames:
for filename in self.filenames:
if filenames:
for filename in filenames:
file_selection.file_list.add_file(filename)
# server status
server_status = ServerStatus()
server_status = ServerStatus(file_selection)
server_status.server_started.connect(file_selection.server_started)
server_status.server_stopped.connect(file_selection.server_stopped)
file_selection.file_list.files_updated.connect(server_status.update)
# downloads
downloads = Downloads()
@ -366,8 +368,8 @@ def main():
qtapp.connect(qtapp, QtCore.SIGNAL("aboutToQuit()"), shutdown)
# launch the gui
gui = OnionShareGui(app, filenames)
gui.start_send()
gui = OnionShareGui(app)
gui.start_send(filenames)
# all done
sys.exit(qtapp.exec_())

View File

@ -4,28 +4,39 @@ import common
from onionshare import strings, helpers
class ServerStatus(QtGui.QVBoxLayout):
def __init__(self):
server_started = QtCore.pyqtSignal()
server_stopped = QtCore.pyqtSignal()
STATUS_STOPPED = 0
STATUS_WORKING = 1
STATUS_STARTED = 2
def __init__(self, file_selection):
super(ServerStatus, self).__init__()
self.status = self.STATUS_STOPPED
self.addSpacing(10)
self.file_selection = file_selection
# server layout
self.status_image = QtGui.QImage('{0}/server_stopped.png'.format(common.onionshare_gui_dir))
status_image_label = QtGui.QLabel()
status_image_label.setPixmap(QtGui.QPixmap.fromImage(self.status_image))
status_image_label.setFixedWidth(30)
self.status_image_stopped = QtGui.QImage('{0}/server_stopped.png'.format(common.onionshare_gui_dir))
self.status_image_working = QtGui.QImage('{0}/server_working.png'.format(common.onionshare_gui_dir))
self.status_image_started = QtGui.QImage('{0}/server_started.png'.format(common.onionshare_gui_dir))
self.status_image_label = QtGui.QLabel()
self.status_image_label.setFixedWidth(30)
self.start_server_button = QtGui.QPushButton(strings._('gui_start_server'))
self.start_server_button.clicked.connect(self.start_server)
self.stop_server_button = QtGui.QPushButton(strings._('gui_stop_server'))
self.stop_server_button.clicked.connect(self.stop_server)
server_layout = QtGui.QHBoxLayout()
server_layout.addWidget(status_image_label)
server_layout.addWidget(self.status_image_label)
server_layout.addWidget(self.start_server_button)
server_layout.addWidget(self.stop_server_button)
# url layout
url_font = QtGui.QFont()
url_font.setPointSize(8)
self.url_label = QtGui.QLabel('http://mry2aqolyzxwfxpt.onion/ x6justoparr5ayreqj6zyf6w2e')
self.url_label = QtGui.QLabel()
self.url_label.setFont(url_font)
self.url_label.setWordWrap(True)
self.url_label.setAlignment(QtCore.Qt.AlignCenter)
@ -43,11 +54,38 @@ class ServerStatus(QtGui.QVBoxLayout):
self.addLayout(server_layout)
self.addLayout(url_layout)
self.update()
def update(self):
# 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))
# buttons enabled
if self.file_selection.get_num_files() == 0:
self.start_server_button.setEnabled(False)
self.stop_server_button.setEnabled(False)
else:
if self.status == self.STATUS_STOPPED:
self.start_server_button.setEnabled(True)
self.stop_server_button.setEnabled(False)
else:
self.start_server_button.setEnabled(False)
self.stop_server_button.setEnabled(True)
def start_server(self):
pass
self.status = self.STATUS_WORKING
self.update()
self.server_started.emit()
def stop_server(self):
pass
self.status = self.STATUS_STOPPED
self.update()
self.server_stopped.emit()
def copy_url(self):
pass