From d8d1dc800df8b836269ecdf09ebb688c97a2d9dc Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 19 May 2018 22:58:55 -0700 Subject: [PATCH] Allow file uploads to finish, and improve uploads styling --- onionshare/web.py | 10 +++- onionshare_gui/mode.py | 6 +++ onionshare_gui/onionshare_gui.py | 3 ++ onionshare_gui/receive_mode/__init__.py | 6 +++ onionshare_gui/receive_mode/uploads.py | 67 +++++++++++++++++-------- share/locale/en.json | 3 +- 6 files changed, 70 insertions(+), 25 deletions(-) diff --git a/onionshare/web.py b/onionshare/web.py index 4f521bf5..d3e9f3c7 100644 --- a/onionshare/web.py +++ b/onionshare/web.py @@ -54,8 +54,9 @@ class Web(object): REQUEST_CLOSE_SERVER = 6 REQUEST_UPLOAD_NEW_FILE_STARTED = 7 REQUEST_UPLOAD_FILE_RENAMED = 8 - REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE = 9 - REQUEST_ERROR_DOWNLOADS_DIR_NOT_WRITABLE = 10 + REQUEST_UPLOAD_FINISHED = 9 + REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE = 10 + REQUEST_ERROR_DOWNLOADS_DIR_NOT_WRITABLE = 11 def __init__(self, common, gui_mode, receive_mode=False): self.common = common @@ -748,6 +749,11 @@ class ReceiveModeRequest(Request): """ super(ReceiveModeRequest, self).close() if self.upload_request: + # Inform the GUI that the upload has finished + self.web.add_request(Web.REQUEST_UPLOAD_FINISHED, self.path, { + 'id': self.upload_id + }) + if len(self.progress) > 0: print('') diff --git a/onionshare_gui/mode.py b/onionshare_gui/mode.py index edcedca3..c81d9895 100644 --- a/onionshare_gui/mode.py +++ b/onionshare_gui/mode.py @@ -335,3 +335,9 @@ class Mode(QtWidgets.QWidget): Handle REQUEST_UPLOAD_FILE_RENAMED event. """ pass + + def handle_request_upload_finished(self, event): + """ + Handle REQUEST_UPLOAD_FINISHED event. + """ + pass diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 10fa62b6..c2bfa749 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -393,6 +393,9 @@ class OnionShareGui(QtWidgets.QMainWindow): elif event["type"] == Web.REQUEST_UPLOAD_FILE_RENAMED: mode.handle_request_upload_file_renamed(event) + elif event["type"] == Web.REQUEST_UPLOAD_FINISHED: + mode.handle_request_upload_finished(event) + if event["type"] == Web.REQUEST_ERROR_DOWNLOADS_DIR_CANNOT_CREATE: Alert(self.common, strings._('error_cannot_create_downloads_dir').format(self.common.settings.get('downloads_dir'))) diff --git a/onionshare_gui/receive_mode/__init__.py b/onionshare_gui/receive_mode/__init__.py index 0d2ef61b..65d3905a 100644 --- a/onionshare_gui/receive_mode/__init__.py +++ b/onionshare_gui/receive_mode/__init__.py @@ -168,6 +168,12 @@ class ReceiveMode(Mode): """ pass + def handle_request_upload_finished(self, event): + """ + Handle REQUEST_UPLOAD_FINISHED event. + """ + self.uploads.finished(event["data"]["id"]) + def reset_info_counters(self): """ Set the info counters back to zero. diff --git a/onionshare_gui/receive_mode/uploads.py b/onionshare_gui/receive_mode/uploads.py index 2a999f86..38d127c3 100644 --- a/onionshare_gui/receive_mode/uploads.py +++ b/onionshare_gui/receive_mode/uploads.py @@ -111,28 +111,22 @@ class Upload(QtWidgets.QWidget): for filename in progress: total_uploaded_bytes += progress[filename]['uploaded_bytes'] - if total_uploaded_bytes == self.content_length: - # Hide the progress bar, show the folder button - self.progress_bar.hide() - self.folder_button.show() + # Update the progress bar + self.progress_bar.setMaximum(self.content_length) + self.progress_bar.setValue(total_uploaded_bytes) + elapsed = datetime.now() - self.started + if elapsed.seconds < 10: + pb_fmt = strings._('gui_download_upload_progress_starting').format( + self.common.human_readable_filesize(total_uploaded_bytes)) else: - # Update the progress bar - self.progress_bar.setMaximum(self.content_length) - self.progress_bar.setValue(total_uploaded_bytes) - - elapsed = datetime.now() - self.started - if elapsed.seconds < 10: - pb_fmt = strings._('gui_download_upload_progress_starting').format( - self.common.human_readable_filesize(total_uploaded_bytes)) - else: - estimated_time_remaining = self.common.estimated_time_remaining( - total_uploaded_bytes, - self.content_length, - started.timestamp()) - pb_fmt = strings._('gui_download_upload_progress_eta').format( - self.common.human_readable_filesize(total_uploaded_bytes), - estimated_time_remaining) + estimated_time_remaining = self.common.estimated_time_remaining( + total_uploaded_bytes, + self.content_length, + self.started.timestamp()) + pb_fmt = strings._('gui_download_upload_progress_eta').format( + self.common.human_readable_filesize(total_uploaded_bytes), + estimated_time_remaining) for filename in progress: # Add a new file if needed @@ -143,6 +137,29 @@ class Upload(QtWidgets.QWidget): # Update the file self.files[filename].update(progress[filename]['uploaded_bytes'], progress[filename]['complete']) + def finished(self): + # Hide the progress bar + self.progress_bar.hide() + + # Change the label + self.ended = self.started = datetime.now() + if self.started.year == self.ended.year and self.started.month == self.ended.month and self.started.day == self.ended.day: + if self.started.hour == self.ended.hour and self.started.minute == self.ended.minute: + text = strings._('gui_upload_finished', True).format( + self.started.strftime("%b %d, %I:%M%p") + ) + else: + text = strings._('gui_upload_finished_range', True).format( + self.started.strftime("%b %d, %I:%M%p"), + self.ended.strftime("%I:%M%p") + ) + else: + text = strings._('gui_upload_finished_range', True).format( + self.started.strftime("%b %d, %I:%M%p"), + self.ended.strftime("%b %d, %I:%M%p") + ) + self.label.setText(text) + class Uploads(QtWidgets.QScrollArea): """ @@ -198,10 +215,16 @@ class Uploads(QtWidgets.QScrollArea): def update(self, upload_id, progress): """ - Update the progress of an upload progress bar. + Update the progress of an upload. """ self.uploads[upload_id].update(progress) - self.adjustSize() + #self.adjustSize() + + def finished(self, upload_id): + """ + An upload has finished. + """ + self.uploads[upload_id].finished() def cancel(self, upload_id): """ diff --git a/share/locale/en.json b/share/locale/en.json index 9874d710..bd0f939d 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -192,5 +192,6 @@ "gui_uploads_window_tooltip": "Show/hide uploads", "gui_no_uploads": "No uploads yet.", "gui_upload_in_progress": "Upload Started {}", - "gui_upload_finished": "Uploaded {} to {}" + "gui_upload_finished_range": "Uploaded {} to {}", + "gui_upload_finished": "Uploaded {}" }