mirror of
https://github.com/onionshare/onionshare.git
synced 2025-08-03 20:04:14 -04:00
Move ShareMode and ReceiveMode into Mode module
This commit is contained in:
parent
62718d1c8b
commit
b0b5b6c79e
10 changed files with 10 additions and 9 deletions
|
@ -1,378 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
OnionShare | https://onionshare.org/
|
||||
|
||||
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
|
||||
|
||||
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/>.
|
||||
"""
|
||||
import os
|
||||
from PyQt5 import QtCore, QtWidgets, QtGui
|
||||
|
||||
from onionshare import strings
|
||||
from onionshare.onion import *
|
||||
from onionshare.common import Common
|
||||
from onionshare.web import Web
|
||||
|
||||
from .file_selection import FileSelection
|
||||
from .downloads import Downloads
|
||||
from .threads import CompressThread
|
||||
from .info import ShareModeInfo
|
||||
from ..mode import Mode
|
||||
from ..widgets import Alert
|
||||
|
||||
class ShareMode(Mode):
|
||||
"""
|
||||
Parts of the main window UI for sharing files.
|
||||
"""
|
||||
def init(self):
|
||||
"""
|
||||
Custom initialization for ReceiveMode.
|
||||
"""
|
||||
# Threads start out as None
|
||||
self.compress_thread = None
|
||||
|
||||
# Create the Web object
|
||||
self.web = Web(self.common, True, 'share')
|
||||
|
||||
# File selection
|
||||
self.file_selection = FileSelection(self.common)
|
||||
if self.filenames:
|
||||
for filename in self.filenames:
|
||||
self.file_selection.file_list.add_file(filename)
|
||||
|
||||
# Server status
|
||||
self.server_status.set_mode('share', self.file_selection)
|
||||
self.server_status.server_started.connect(self.file_selection.server_started)
|
||||
self.server_status.server_stopped.connect(self.file_selection.server_stopped)
|
||||
self.server_status.server_stopped.connect(self.update_primary_action)
|
||||
self.server_status.server_canceled.connect(self.file_selection.server_stopped)
|
||||
self.server_status.server_canceled.connect(self.update_primary_action)
|
||||
self.file_selection.file_list.files_updated.connect(self.server_status.update)
|
||||
self.file_selection.file_list.files_updated.connect(self.update_primary_action)
|
||||
# Tell server_status about web, then update
|
||||
self.server_status.web = self.web
|
||||
self.server_status.update()
|
||||
|
||||
# Filesize warning
|
||||
self.filesize_warning = QtWidgets.QLabel()
|
||||
self.filesize_warning.setWordWrap(True)
|
||||
self.filesize_warning.setStyleSheet(self.common.css['share_filesize_warning'])
|
||||
self.filesize_warning.hide()
|
||||
|
||||
# Downloads
|
||||
self.downloads = Downloads(self.common)
|
||||
self.downloads.hide()
|
||||
self.downloads_in_progress = 0
|
||||
self.downloads_completed = 0
|
||||
|
||||
# Information about share, and show downloads button
|
||||
self.info = ShareModeInfo(self.common, self)
|
||||
|
||||
# Primary action layout
|
||||
self.primary_action_layout.addWidget(self.filesize_warning)
|
||||
self.primary_action.hide()
|
||||
self.update_primary_action()
|
||||
|
||||
# Status bar, zip progress bar
|
||||
self._zip_progress_bar = None
|
||||
|
||||
# Main layout
|
||||
self.main_layout = QtWidgets.QVBoxLayout()
|
||||
self.main_layout.addWidget(self.info)
|
||||
self.main_layout.addLayout(self.file_selection)
|
||||
self.main_layout.addWidget(self.primary_action)
|
||||
self.main_layout.addWidget(self.min_width_widget)
|
||||
|
||||
# Wrapper layout
|
||||
self.wrapper_layout = QtWidgets.QHBoxLayout()
|
||||
self.wrapper_layout.addLayout(self.main_layout)
|
||||
self.wrapper_layout.addWidget(self.downloads)
|
||||
self.setLayout(self.wrapper_layout)
|
||||
|
||||
# Always start with focus on file selection
|
||||
self.file_selection.setFocus()
|
||||
|
||||
def get_stop_server_shutdown_timeout_text(self):
|
||||
"""
|
||||
Return the string to put on the stop server button, if there's a shutdown timeout
|
||||
"""
|
||||
return strings._('gui_share_stop_server_shutdown_timeout', True)
|
||||
|
||||
def timeout_finished_should_stop_server(self):
|
||||
"""
|
||||
The shutdown timer expired, should we stop the server? Returns a bool
|
||||
"""
|
||||
# If there were no attempts to download the share, or all downloads are done, we can stop
|
||||
if self.web.share_mode.download_count == 0 or self.web.done:
|
||||
self.server_status.stop_server()
|
||||
self.server_status_label.setText(strings._('close_on_timeout', True))
|
||||
return True
|
||||
# A download is probably still running - hold off on stopping the share
|
||||
else:
|
||||
self.server_status_label.setText(strings._('timeout_download_still_running', True))
|
||||
return False
|
||||
|
||||
def start_server_custom(self):
|
||||
"""
|
||||
Starting the server.
|
||||
"""
|
||||
# Reset web counters
|
||||
self.web.share_mode.download_count = 0
|
||||
self.web.error404_count = 0
|
||||
|
||||
# Hide and reset the downloads if we have previously shared
|
||||
self.reset_info_counters()
|
||||
|
||||
def start_server_step2_custom(self):
|
||||
"""
|
||||
Step 2 in starting the server. Zipping up files.
|
||||
"""
|
||||
# Add progress bar to the status bar, indicating the compressing of files.
|
||||
self._zip_progress_bar = ZipProgressBar(self.common, 0)
|
||||
self.filenames = []
|
||||
for index in range(self.file_selection.file_list.count()):
|
||||
self.filenames.append(self.file_selection.file_list.item(index).filename)
|
||||
|
||||
self._zip_progress_bar.total_files_size = ShareMode._compute_total_size(self.filenames)
|
||||
self.status_bar.insertWidget(0, self._zip_progress_bar)
|
||||
|
||||
# prepare the files for sending in a new thread
|
||||
self.compress_thread = CompressThread(self)
|
||||
self.compress_thread.success.connect(self.starting_server_step3.emit)
|
||||
self.compress_thread.success.connect(self.start_server_finished.emit)
|
||||
self.compress_thread.error.connect(self.starting_server_error.emit)
|
||||
self.server_status.server_canceled.connect(self.compress_thread.cancel)
|
||||
self.compress_thread.start()
|
||||
|
||||
def start_server_step3_custom(self):
|
||||
"""
|
||||
Step 3 in starting the server. Remove zip progess bar, and display large filesize
|
||||
warning, if applicable.
|
||||
"""
|
||||
# Remove zip progress bar
|
||||
if self._zip_progress_bar is not None:
|
||||
self.status_bar.removeWidget(self._zip_progress_bar)
|
||||
self._zip_progress_bar = None
|
||||
|
||||
# Warn about sending large files over Tor
|
||||
if self.web.share_mode.download_filesize >= 157286400: # 150mb
|
||||
self.filesize_warning.setText(strings._("large_filesize", True))
|
||||
self.filesize_warning.show()
|
||||
|
||||
def start_server_error_custom(self):
|
||||
"""
|
||||
Start server error.
|
||||
"""
|
||||
if self._zip_progress_bar is not None:
|
||||
self.status_bar.removeWidget(self._zip_progress_bar)
|
||||
self._zip_progress_bar = None
|
||||
|
||||
def stop_server_custom(self):
|
||||
"""
|
||||
Stop server.
|
||||
"""
|
||||
# Remove the progress bar
|
||||
if self._zip_progress_bar is not None:
|
||||
self.status_bar.removeWidget(self._zip_progress_bar)
|
||||
self._zip_progress_bar = None
|
||||
|
||||
self.filesize_warning.hide()
|
||||
self.downloads_in_progress = 0
|
||||
self.downloads_completed = 0
|
||||
self.info.update_downloads_in_progress()
|
||||
self.file_selection.file_list.adjustSize()
|
||||
|
||||
def cancel_server_custom(self):
|
||||
"""
|
||||
Stop the compression thread on cancel
|
||||
"""
|
||||
if self.compress_thread:
|
||||
self.common.log('ShareMode', 'cancel_server: quitting compress thread')
|
||||
self.compress_thread.quit()
|
||||
|
||||
def handle_tor_broke_custom(self):
|
||||
"""
|
||||
Connection to Tor broke.
|
||||
"""
|
||||
self.primary_action.hide()
|
||||
self.info.show_less()
|
||||
|
||||
def handle_request_load(self, event):
|
||||
"""
|
||||
Handle REQUEST_LOAD event.
|
||||
"""
|
||||
self.system_tray.showMessage(strings._('systray_page_loaded_title', True), strings._('systray_download_page_loaded_message', True))
|
||||
|
||||
def handle_request_started(self, event):
|
||||
"""
|
||||
Handle REQUEST_STARTED event.
|
||||
"""
|
||||
if event["data"]["use_gzip"]:
|
||||
filesize = self.web.share_mode.gzip_filesize
|
||||
else:
|
||||
filesize = self.web.share_mode.download_filesize
|
||||
self.downloads.add(event["data"]["id"], filesize)
|
||||
self.info.update_indicator(True)
|
||||
self.downloads_in_progress += 1
|
||||
self.info.update_downloads_in_progress()
|
||||
|
||||
self.system_tray.showMessage(strings._('systray_download_started_title', True), strings._('systray_download_started_message', True))
|
||||
|
||||
def handle_request_progress(self, event):
|
||||
"""
|
||||
Handle REQUEST_PROGRESS event.
|
||||
"""
|
||||
self.downloads.update(event["data"]["id"], event["data"]["bytes"])
|
||||
|
||||
# Is the download complete?
|
||||
if event["data"]["bytes"] == self.web.share_mode.filesize:
|
||||
self.system_tray.showMessage(strings._('systray_download_completed_title', True), strings._('systray_download_completed_message', True))
|
||||
|
||||
# Update the total 'completed downloads' info
|
||||
self.downloads_completed += 1
|
||||
self.info.update_downloads_completed()
|
||||
# Update the 'in progress downloads' info
|
||||
self.downloads_in_progress -= 1
|
||||
self.info.update_downloads_in_progress()
|
||||
|
||||
# Close on finish?
|
||||
if self.common.settings.get('close_after_first_download'):
|
||||
self.server_status.stop_server()
|
||||
self.status_bar.clearMessage()
|
||||
self.server_status_label.setText(strings._('closing_automatically', True))
|
||||
else:
|
||||
if self.server_status.status == self.server_status.STATUS_STOPPED:
|
||||
self.downloads.cancel(event["data"]["id"])
|
||||
self.downloads_in_progress = 0
|
||||
self.info.update_downloads_in_progress()
|
||||
|
||||
def handle_request_canceled(self, event):
|
||||
"""
|
||||
Handle REQUEST_CANCELED event.
|
||||
"""
|
||||
self.downloads.cancel(event["data"]["id"])
|
||||
|
||||
# Update the 'in progress downloads' info
|
||||
self.downloads_in_progress -= 1
|
||||
self.info.update_downloads_in_progress()
|
||||
self.system_tray.showMessage(strings._('systray_download_canceled_title', True), strings._('systray_download_canceled_message', True))
|
||||
|
||||
def on_reload_settings(self):
|
||||
"""
|
||||
If there were some files listed for sharing, we should be ok to re-enable
|
||||
the 'Start Sharing' button now.
|
||||
"""
|
||||
if self.server_status.file_selection.get_num_files() > 0:
|
||||
self.primary_action.show()
|
||||
self.info.show_more()
|
||||
|
||||
def update_primary_action(self):
|
||||
self.common.log('ShareMode', 'update_primary_action')
|
||||
|
||||
# Show or hide primary action layout
|
||||
file_count = self.file_selection.file_list.count()
|
||||
if file_count > 0:
|
||||
self.primary_action.show()
|
||||
self.info.show_more()
|
||||
|
||||
# Update the file count in the info label
|
||||
total_size_bytes = 0
|
||||
for index in range(self.file_selection.file_list.count()):
|
||||
item = self.file_selection.file_list.item(index)
|
||||
total_size_bytes += item.size_bytes
|
||||
total_size_readable = self.common.human_readable_filesize(total_size_bytes)
|
||||
|
||||
if file_count > 1:
|
||||
self.info.update_label(strings._('gui_file_info', True).format(file_count, total_size_readable))
|
||||
else:
|
||||
self.info.update_label(strings._('gui_file_info_single', True).format(file_count, total_size_readable))
|
||||
|
||||
else:
|
||||
self.primary_action.hide()
|
||||
self.info.show_less()
|
||||
|
||||
# Resize window
|
||||
self.resize_window()
|
||||
|
||||
def reset_info_counters(self):
|
||||
"""
|
||||
Set the info counters back to zero.
|
||||
"""
|
||||
self.downloads_completed = 0
|
||||
self.downloads_in_progress = 0
|
||||
self.info.update_downloads_completed()
|
||||
self.info.update_downloads_in_progress()
|
||||
self.downloads.reset()
|
||||
|
||||
def resize_window(self):
|
||||
min_width = self.common.min_window_width
|
||||
if self.downloads.isVisible():
|
||||
min_width += 300
|
||||
self.adjust_size.emit(min_width)
|
||||
|
||||
@staticmethod
|
||||
def _compute_total_size(filenames):
|
||||
total_size = 0
|
||||
for filename in filenames:
|
||||
if os.path.isfile(filename):
|
||||
total_size += os.path.getsize(filename)
|
||||
if os.path.isdir(filename):
|
||||
total_size += Common.dir_size(filename)
|
||||
return total_size
|
||||
|
||||
|
||||
class ZipProgressBar(QtWidgets.QProgressBar):
|
||||
update_processed_size_signal = QtCore.pyqtSignal(int)
|
||||
|
||||
def __init__(self, common, total_files_size):
|
||||
super(ZipProgressBar, self).__init__()
|
||||
self.common = common
|
||||
|
||||
self.setMaximumHeight(20)
|
||||
self.setMinimumWidth(200)
|
||||
self.setValue(0)
|
||||
self.setFormat(strings._('zip_progress_bar_format'))
|
||||
self.setStyleSheet(self.common.css['share_zip_progess_bar'])
|
||||
|
||||
self._total_files_size = total_files_size
|
||||
self._processed_size = 0
|
||||
|
||||
self.update_processed_size_signal.connect(self.update_processed_size)
|
||||
|
||||
@property
|
||||
def total_files_size(self):
|
||||
return self._total_files_size
|
||||
|
||||
@total_files_size.setter
|
||||
def total_files_size(self, val):
|
||||
self._total_files_size = val
|
||||
|
||||
@property
|
||||
def processed_size(self):
|
||||
return self._processed_size
|
||||
|
||||
@processed_size.setter
|
||||
def processed_size(self, val):
|
||||
self.update_processed_size(val)
|
||||
|
||||
def update_processed_size(self, val):
|
||||
self._processed_size = val
|
||||
|
||||
if self.processed_size < self.total_files_size:
|
||||
self.setValue(int((self.processed_size * 100) / self.total_files_size))
|
||||
elif self.total_files_size != 0:
|
||||
self.setValue(100)
|
||||
else:
|
||||
self.setValue(0)
|
|
@ -1,248 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
OnionShare | https://onionshare.org/
|
||||
|
||||
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
|
||||
|
||||
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/>.
|
||||
"""
|
||||
import time
|
||||
from PyQt5 import QtCore, QtWidgets, QtGui
|
||||
|
||||
from onionshare import strings
|
||||
|
||||
|
||||
class Download(QtWidgets.QWidget):
|
||||
def __init__(self, common, download_id, total_bytes):
|
||||
super(Download, self).__init__()
|
||||
self.common = common
|
||||
|
||||
self.download_id = download_id
|
||||
self.started = time.time()
|
||||
self.total_bytes = total_bytes
|
||||
self.downloaded_bytes = 0
|
||||
|
||||
self.setStyleSheet('QWidget { border: 1px solid red; }')
|
||||
|
||||
# Progress bar
|
||||
self.progress_bar = QtWidgets.QProgressBar()
|
||||
self.progress_bar.setTextVisible(True)
|
||||
self.progress_bar.setAttribute(QtCore.Qt.WA_DeleteOnClose)
|
||||
self.progress_bar.setAlignment(QtCore.Qt.AlignHCenter)
|
||||
self.progress_bar.setMinimum(0)
|
||||
self.progress_bar.setMaximum(total_bytes)
|
||||
self.progress_bar.setValue(0)
|
||||
self.progress_bar.setStyleSheet(self.common.css['downloads_uploads_progress_bar'])
|
||||
self.progress_bar.total_bytes = total_bytes
|
||||
|
||||
# Layout
|
||||
layout = QtWidgets.QVBoxLayout()
|
||||
layout.addWidget(self.progress_bar)
|
||||
self.setLayout(layout)
|
||||
|
||||
# Start at 0
|
||||
self.update(0)
|
||||
|
||||
def update(self, downloaded_bytes):
|
||||
self.downloaded_bytes = downloaded_bytes
|
||||
|
||||
self.progress_bar.setValue(downloaded_bytes)
|
||||
if downloaded_bytes == self.progress_bar.total_bytes:
|
||||
pb_fmt = strings._('gui_download_upload_progress_complete').format(
|
||||
self.common.format_seconds(time.time() - self.started))
|
||||
else:
|
||||
elapsed = time.time() - self.started
|
||||
if elapsed < 10:
|
||||
# Wait a couple of seconds for the download rate to stabilize.
|
||||
# This prevents a "Windows copy dialog"-esque experience at
|
||||
# the beginning of the download.
|
||||
pb_fmt = strings._('gui_download_upload_progress_starting').format(
|
||||
self.common.human_readable_filesize(downloaded_bytes))
|
||||
else:
|
||||
pb_fmt = strings._('gui_download_upload_progress_eta').format(
|
||||
self.common.human_readable_filesize(downloaded_bytes),
|
||||
self.estimated_time_remaining)
|
||||
|
||||
self.progress_bar.setFormat(pb_fmt)
|
||||
|
||||
def cancel(self):
|
||||
self.progress_bar.setFormat(strings._('gui_canceled'))
|
||||
|
||||
@property
|
||||
def estimated_time_remaining(self):
|
||||
return self.common.estimated_time_remaining(self.downloaded_bytes,
|
||||
self.total_bytes,
|
||||
self.started)
|
||||
|
||||
|
||||
class DownloadList(QtWidgets.QScrollArea):
|
||||
"""
|
||||
List of download progress bars.
|
||||
"""
|
||||
def __init__(self, common):
|
||||
super(DownloadList, self).__init__()
|
||||
self.common = common
|
||||
|
||||
self.downloads = {}
|
||||
|
||||
# The layout that holds all of the downloads
|
||||
self.downloads_layout = QtWidgets.QVBoxLayout()
|
||||
self.downloads_layout.setContentsMargins(0, 0, 0, 0)
|
||||
self.downloads_layout.setSizeConstraint(QtWidgets.QLayout.SetMinAndMaxSize)
|
||||
|
||||
# Wrapper layout that also contains a stretch
|
||||
wrapper_layout = QtWidgets.QVBoxLayout()
|
||||
wrapper_layout.setSizeConstraint(QtWidgets.QLayout.SetMinAndMaxSize)
|
||||
wrapper_layout.addLayout(self.downloads_layout)
|
||||
wrapper_layout.addStretch()
|
||||
|
||||
# The internal widget of the scroll area
|
||||
widget = QtWidgets.QWidget()
|
||||
widget.setLayout(wrapper_layout)
|
||||
self.setWidget(widget)
|
||||
self.setWidgetResizable(True)
|
||||
|
||||
# Other scroll area settings
|
||||
self.setBackgroundRole(QtGui.QPalette.Light)
|
||||
self.verticalScrollBar().rangeChanged.connect(self.resizeScroll)
|
||||
|
||||
def resizeScroll(self, minimum, maximum):
|
||||
"""
|
||||
Scroll to the bottom of the window when the range changes.
|
||||
"""
|
||||
self.verticalScrollBar().setValue(maximum)
|
||||
|
||||
def add(self, download_id, content_length):
|
||||
"""
|
||||
Add a new download progress bar.
|
||||
"""
|
||||
download = Download(self.common, download_id, content_length)
|
||||
self.downloads[download_id] = download
|
||||
self.downloads_layout.addWidget(download)
|
||||
|
||||
def update(self, download_id, downloaded_bytes):
|
||||
"""
|
||||
Update the progress of a download progress bar.
|
||||
"""
|
||||
self.downloads[download_id].update(downloaded_bytes)
|
||||
|
||||
def cancel(self, download_id):
|
||||
"""
|
||||
Update a download progress bar to show that it has been canceled.
|
||||
"""
|
||||
self.downloads[download_id].cancel()
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Reset the downloads back to zero
|
||||
"""
|
||||
for download in self.downloads.values():
|
||||
self.downloads_layout.removeWidget(download)
|
||||
download.progress_bar.close()
|
||||
self.downloads = {}
|
||||
|
||||
|
||||
class Downloads(QtWidgets.QWidget):
|
||||
"""
|
||||
The downloads chunk of the GUI. This lists all of the active download
|
||||
progress bars.
|
||||
"""
|
||||
def __init__(self, common):
|
||||
super(Downloads, self).__init__()
|
||||
self.common = common
|
||||
|
||||
self.setMinimumWidth(350)
|
||||
|
||||
# When there are no downloads
|
||||
empty_image = QtWidgets.QLabel()
|
||||
empty_image.setAlignment(QtCore.Qt.AlignCenter)
|
||||
empty_image.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(self.common.get_resource_path('images/downloads_transparent.png'))))
|
||||
empty_text = QtWidgets.QLabel(strings._('gui_no_downloads', True))
|
||||
empty_text.setAlignment(QtCore.Qt.AlignCenter)
|
||||
empty_text.setStyleSheet(self.common.css['downloads_uploads_empty_text'])
|
||||
empty_layout = QtWidgets.QVBoxLayout()
|
||||
empty_layout.addStretch()
|
||||
empty_layout.addWidget(empty_image)
|
||||
empty_layout.addWidget(empty_text)
|
||||
empty_layout.addStretch()
|
||||
self.empty = QtWidgets.QWidget()
|
||||
self.empty.setStyleSheet(self.common.css['downloads_uploads_empty'])
|
||||
self.empty.setLayout(empty_layout)
|
||||
|
||||
# When there are downloads
|
||||
self.download_list = DownloadList(self.common)
|
||||
|
||||
# Download header
|
||||
downloads_label = QtWidgets.QLabel(strings._('gui_downloads', True))
|
||||
downloads_label.setStyleSheet(self.common.css['downloads_uploads_label'])
|
||||
clear_button = QtWidgets.QPushButton(strings._('gui_clear_history', True))
|
||||
clear_button.setStyleSheet(self.common.css['downloads_uploads_clear'])
|
||||
clear_button.setFlat(True)
|
||||
clear_button.clicked.connect(self.reset)
|
||||
download_header = QtWidgets.QHBoxLayout()
|
||||
download_header.addWidget(downloads_label)
|
||||
download_header.addStretch()
|
||||
download_header.addWidget(clear_button)
|
||||
|
||||
# Download layout
|
||||
not_empty_layout = QtWidgets.QVBoxLayout()
|
||||
not_empty_layout.addLayout(download_header)
|
||||
not_empty_layout.addWidget(self.download_list)
|
||||
self.not_empty = QtWidgets.QWidget()
|
||||
self.not_empty.setLayout(not_empty_layout)
|
||||
|
||||
# Layout
|
||||
layout = QtWidgets.QVBoxLayout()
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
layout.addWidget(self.empty)
|
||||
layout.addWidget(self.not_empty)
|
||||
self.setLayout(layout)
|
||||
|
||||
# Reset once at the beginning
|
||||
self.reset()
|
||||
|
||||
def add(self, download_id, content_length):
|
||||
"""
|
||||
Add a new download progress bar.
|
||||
"""
|
||||
self.common.log('Downloads', 'add', 'download_id: {}, content_length: {}'.format(download_id, content_length))
|
||||
|
||||
# Hide empty, show not empty
|
||||
self.empty.hide()
|
||||
self.not_empty.show()
|
||||
|
||||
# Add it to the list
|
||||
self.download_list.add(download_id, content_length)
|
||||
|
||||
def update(self, download_id, downloaded_bytes):
|
||||
"""
|
||||
Update the progress of a download progress bar.
|
||||
"""
|
||||
self.download_list.update(download_id, downloaded_bytes)
|
||||
|
||||
def cancel(self, download_id):
|
||||
"""
|
||||
Update a download progress bar to show that it has been canceled.
|
||||
"""
|
||||
self.download_list.cancel(download_id)
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Reset the downloads back to zero
|
||||
"""
|
||||
self.download_list.reset()
|
||||
|
||||
# Hide not empty, show empty
|
||||
self.not_empty.hide()
|
||||
self.empty.show()
|
|
@ -1,390 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
OnionShare | https://onionshare.org/
|
||||
|
||||
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
|
||||
|
||||
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/>.
|
||||
"""
|
||||
import os
|
||||
from PyQt5 import QtCore, QtWidgets, QtGui
|
||||
|
||||
from onionshare import strings
|
||||
|
||||
from ..widgets import Alert, AddFileDialog
|
||||
|
||||
class DropHereLabel(QtWidgets.QLabel):
|
||||
"""
|
||||
When there are no files or folders in the FileList yet, display the
|
||||
'drop files here' message and graphic.
|
||||
"""
|
||||
def __init__(self, common, parent, image=False):
|
||||
self.parent = parent
|
||||
super(DropHereLabel, self).__init__(parent=parent)
|
||||
|
||||
self.common = common
|
||||
|
||||
self.setAcceptDrops(True)
|
||||
self.setAlignment(QtCore.Qt.AlignCenter)
|
||||
|
||||
if image:
|
||||
self.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(self.common.get_resource_path('images/logo_transparent.png'))))
|
||||
else:
|
||||
self.setText(strings._('gui_drag_and_drop', True))
|
||||
self.setStyleSheet(self.common.css['share_file_selection_drop_here_label'])
|
||||
|
||||
self.hide()
|
||||
|
||||
def dragEnterEvent(self, event):
|
||||
self.parent.drop_here_image.hide()
|
||||
self.parent.drop_here_text.hide()
|
||||
event.accept()
|
||||
|
||||
|
||||
class DropCountLabel(QtWidgets.QLabel):
|
||||
"""
|
||||
While dragging files over the FileList, this counter displays the
|
||||
number of files you're dragging.
|
||||
"""
|
||||
def __init__(self, common, parent):
|
||||
self.parent = parent
|
||||
super(DropCountLabel, self).__init__(parent=parent)
|
||||
|
||||
self.common = common
|
||||
|
||||
self.setAcceptDrops(True)
|
||||
self.setAlignment(QtCore.Qt.AlignCenter)
|
||||
self.setText(strings._('gui_drag_and_drop', True))
|
||||
self.setStyleSheet(self.common.css['share_file_selection_drop_count_label'])
|
||||
self.hide()
|
||||
|
||||
def dragEnterEvent(self, event):
|
||||
self.hide()
|
||||
event.accept()
|
||||
|
||||
|
||||
class FileList(QtWidgets.QListWidget):
|
||||
"""
|
||||
The list of files and folders in the GUI.
|
||||
"""
|
||||
files_dropped = QtCore.pyqtSignal()
|
||||
files_updated = QtCore.pyqtSignal()
|
||||
|
||||
def __init__(self, common, parent=None):
|
||||
super(FileList, self).__init__(parent)
|
||||
|
||||
self.common = common
|
||||
|
||||
self.setAcceptDrops(True)
|
||||
self.setIconSize(QtCore.QSize(32, 32))
|
||||
self.setSortingEnabled(True)
|
||||
self.setMinimumHeight(205)
|
||||
self.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
|
||||
self.drop_here_image = DropHereLabel(self.common, self, True)
|
||||
self.drop_here_text = DropHereLabel(self.common, self, False)
|
||||
self.drop_count = DropCountLabel(self.common, self)
|
||||
self.resizeEvent(None)
|
||||
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
|
||||
|
||||
def update(self):
|
||||
"""
|
||||
Update the GUI elements based on the current state.
|
||||
"""
|
||||
# file list should have a background image if empty
|
||||
if self.count() == 0:
|
||||
self.drop_here_image.show()
|
||||
self.drop_here_text.show()
|
||||
else:
|
||||
self.drop_here_image.hide()
|
||||
self.drop_here_text.hide()
|
||||
|
||||
def server_started(self):
|
||||
"""
|
||||
Update the GUI when the server starts, by hiding delete buttons.
|
||||
"""
|
||||
self.setAcceptDrops(False)
|
||||
self.setCurrentItem(None)
|
||||
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
|
||||
for index in range(self.count()):
|
||||
self.item(index).item_button.hide()
|
||||
|
||||
def server_stopped(self):
|
||||
"""
|
||||
Update the GUI when the server stops, by showing delete buttons.
|
||||
"""
|
||||
self.setAcceptDrops(True)
|
||||
self.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
|
||||
for index in range(self.count()):
|
||||
self.item(index).item_button.show()
|
||||
|
||||
def resizeEvent(self, event):
|
||||
"""
|
||||
When the widget is resized, resize the drop files image and text.
|
||||
"""
|
||||
offset = 70
|
||||
self.drop_here_image.setGeometry(0, 0, self.width(), self.height() - offset)
|
||||
self.drop_here_text.setGeometry(0, offset, self.width(), self.height() - offset)
|
||||
|
||||
if self.count() > 0:
|
||||
# Add and delete an empty item, to force all items to get redrawn
|
||||
# This is ugly, but the only way I could figure out how to proceed
|
||||
item = QtWidgets.QListWidgetItem('fake item')
|
||||
self.addItem(item)
|
||||
self.takeItem(self.row(item))
|
||||
self.update()
|
||||
|
||||
# Extend any filenames that were truncated to fit the window
|
||||
# We use 200 as a rough guess at how wide the 'file size + delete button' widget is
|
||||
# and extend based on the overall width minus that amount.
|
||||
for index in range(self.count()):
|
||||
metrics = QtGui.QFontMetrics(self.item(index).font())
|
||||
elided = metrics.elidedText(self.item(index).basename, QtCore.Qt.ElideRight, self.width() - 200)
|
||||
self.item(index).setText(elided)
|
||||
|
||||
|
||||
def dragEnterEvent(self, event):
|
||||
"""
|
||||
dragEnterEvent for dragging files and directories into the widget.
|
||||
"""
|
||||
if event.mimeData().hasUrls:
|
||||
self.setStyleSheet(self.common.css['share_file_list_drag_enter'])
|
||||
count = len(event.mimeData().urls())
|
||||
self.drop_count.setText('+{}'.format(count))
|
||||
|
||||
size_hint = self.drop_count.sizeHint()
|
||||
self.drop_count.setGeometry(self.width() - size_hint.width() - 10, self.height() - size_hint.height() - 10, size_hint.width(), size_hint.height())
|
||||
self.drop_count.show()
|
||||
event.accept()
|
||||
else:
|
||||
event.ignore()
|
||||
|
||||
def dragLeaveEvent(self, event):
|
||||
"""
|
||||
dragLeaveEvent for dragging files and directories into the widget.
|
||||
"""
|
||||
self.setStyleSheet(self.common.css['share_file_list_drag_leave'])
|
||||
self.drop_count.hide()
|
||||
event.accept()
|
||||
self.update()
|
||||
|
||||
def dragMoveEvent(self, event):
|
||||
"""
|
||||
dragMoveEvent for dragging files and directories into the widget.
|
||||
"""
|
||||
if event.mimeData().hasUrls:
|
||||
event.setDropAction(QtCore.Qt.CopyAction)
|
||||
event.accept()
|
||||
else:
|
||||
event.ignore()
|
||||
|
||||
def dropEvent(self, event):
|
||||
"""
|
||||
dropEvent for dragging files and directories into the widget.
|
||||
"""
|
||||
if event.mimeData().hasUrls:
|
||||
event.setDropAction(QtCore.Qt.CopyAction)
|
||||
event.accept()
|
||||
for url in event.mimeData().urls():
|
||||
filename = str(url.toLocalFile())
|
||||
self.add_file(filename)
|
||||
else:
|
||||
event.ignore()
|
||||
|
||||
self.setStyleSheet(self.common.css['share_file_list_drag_leave'])
|
||||
self.drop_count.hide()
|
||||
|
||||
self.files_dropped.emit()
|
||||
|
||||
def add_file(self, filename):
|
||||
"""
|
||||
Add a file or directory to this widget.
|
||||
"""
|
||||
filenames = []
|
||||
for index in range(self.count()):
|
||||
filenames.append(self.item(index).filename)
|
||||
|
||||
if filename not in filenames:
|
||||
if not os.access(filename, os.R_OK):
|
||||
Alert(self.common, strings._("not_a_readable_file", True).format(filename))
|
||||
return
|
||||
|
||||
fileinfo = QtCore.QFileInfo(filename)
|
||||
ip = QtWidgets.QFileIconProvider()
|
||||
icon = ip.icon(fileinfo)
|
||||
|
||||
if os.path.isfile(filename):
|
||||
size_bytes = fileinfo.size()
|
||||
size_readable = self.common.human_readable_filesize(size_bytes)
|
||||
else:
|
||||
size_bytes = self.common.dir_size(filename)
|
||||
size_readable = self.common.human_readable_filesize(size_bytes)
|
||||
|
||||
# Create a new item
|
||||
item = QtWidgets.QListWidgetItem()
|
||||
item.setIcon(icon)
|
||||
item.size_bytes = size_bytes
|
||||
|
||||
# Item's filename attribute and size labels
|
||||
item.filename = filename
|
||||
item_size = QtWidgets.QLabel(size_readable)
|
||||
item_size.setStyleSheet(self.common.css['share_file_list_item_size'])
|
||||
|
||||
item.basename = os.path.basename(filename.rstrip('/'))
|
||||
# Use the basename as the method with which to sort the list
|
||||
metrics = QtGui.QFontMetrics(item.font())
|
||||
elided = metrics.elidedText(item.basename, QtCore.Qt.ElideRight, self.sizeHint().width())
|
||||
item.setData(QtCore.Qt.DisplayRole, elided)
|
||||
|
||||
# Item's delete button
|
||||
def delete_item():
|
||||
itemrow = self.row(item)
|
||||
self.takeItem(itemrow)
|
||||
self.files_updated.emit()
|
||||
|
||||
item.item_button = QtWidgets.QPushButton()
|
||||
item.item_button.setDefault(False)
|
||||
item.item_button.setFlat(True)
|
||||
item.item_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/file_delete.png')) )
|
||||
item.item_button.clicked.connect(delete_item)
|
||||
item.item_button.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
|
||||
|
||||
# Item info widget, with a white background
|
||||
item_info_layout = QtWidgets.QHBoxLayout()
|
||||
item_info_layout.addWidget(item_size)
|
||||
item_info_layout.addWidget(item.item_button)
|
||||
item_info = QtWidgets.QWidget()
|
||||
item_info.setObjectName('item-info')
|
||||
item_info.setLayout(item_info_layout)
|
||||
|
||||
# Create the item's widget and layouts
|
||||
item_hlayout = QtWidgets.QHBoxLayout()
|
||||
item_hlayout.addStretch()
|
||||
item_hlayout.addWidget(item_info)
|
||||
widget = QtWidgets.QWidget()
|
||||
widget.setLayout(item_hlayout)
|
||||
|
||||
item.setSizeHint(widget.sizeHint())
|
||||
|
||||
self.addItem(item)
|
||||
self.setItemWidget(item, widget)
|
||||
|
||||
self.files_updated.emit()
|
||||
|
||||
|
||||
class FileSelection(QtWidgets.QVBoxLayout):
|
||||
"""
|
||||
The list of files and folders in the GUI, as well as buttons to add and
|
||||
delete the files and folders.
|
||||
"""
|
||||
def __init__(self, common):
|
||||
super(FileSelection, self).__init__()
|
||||
|
||||
self.common = common
|
||||
|
||||
self.server_on = False
|
||||
|
||||
# File list
|
||||
self.file_list = FileList(self.common)
|
||||
self.file_list.itemSelectionChanged.connect(self.update)
|
||||
self.file_list.files_dropped.connect(self.update)
|
||||
self.file_list.files_updated.connect(self.update)
|
||||
|
||||
# Buttons
|
||||
self.add_button = QtWidgets.QPushButton(strings._('gui_add', True))
|
||||
self.add_button.clicked.connect(self.add)
|
||||
self.delete_button = QtWidgets.QPushButton(strings._('gui_delete', True))
|
||||
self.delete_button.clicked.connect(self.delete)
|
||||
button_layout = QtWidgets.QHBoxLayout()
|
||||
button_layout.addStretch()
|
||||
button_layout.addWidget(self.add_button)
|
||||
button_layout.addWidget(self.delete_button)
|
||||
|
||||
# Add the widgets
|
||||
self.addWidget(self.file_list)
|
||||
self.addLayout(button_layout)
|
||||
|
||||
self.update()
|
||||
|
||||
def update(self):
|
||||
"""
|
||||
Update the GUI elements based on the current state.
|
||||
"""
|
||||
# All buttons should be hidden if the server is on
|
||||
if self.server_on:
|
||||
self.add_button.hide()
|
||||
self.delete_button.hide()
|
||||
else:
|
||||
self.add_button.show()
|
||||
|
||||
# Delete button should be hidden if item isn't selected
|
||||
if len(self.file_list.selectedItems()) == 0:
|
||||
self.delete_button.hide()
|
||||
else:
|
||||
self.delete_button.show()
|
||||
|
||||
# Update the file list
|
||||
self.file_list.update()
|
||||
|
||||
def add(self):
|
||||
"""
|
||||
Add button clicked.
|
||||
"""
|
||||
file_dialog = AddFileDialog(self.common, caption=strings._('gui_choose_items', True))
|
||||
if file_dialog.exec_() == QtWidgets.QDialog.Accepted:
|
||||
for filename in file_dialog.selectedFiles():
|
||||
self.file_list.add_file(filename)
|
||||
|
||||
self.file_list.setCurrentItem(None)
|
||||
self.update()
|
||||
|
||||
def delete(self):
|
||||
"""
|
||||
Delete button clicked
|
||||
"""
|
||||
selected = self.file_list.selectedItems()
|
||||
for item in selected:
|
||||
itemrow = self.file_list.row(item)
|
||||
self.file_list.takeItem(itemrow)
|
||||
self.file_list.files_updated.emit()
|
||||
|
||||
self.file_list.setCurrentItem(None)
|
||||
self.update()
|
||||
|
||||
def server_started(self):
|
||||
"""
|
||||
Gets called when the server starts.
|
||||
"""
|
||||
self.server_on = True
|
||||
self.file_list.server_started()
|
||||
self.update()
|
||||
|
||||
def server_stopped(self):
|
||||
"""
|
||||
Gets called when the server stops.
|
||||
"""
|
||||
self.server_on = False
|
||||
self.file_list.server_stopped()
|
||||
self.update()
|
||||
|
||||
def get_num_files(self):
|
||||
"""
|
||||
Returns the total number of files and folders in the list.
|
||||
"""
|
||||
return len(range(self.file_list.count()))
|
||||
|
||||
def setFocus(self):
|
||||
"""
|
||||
Set the Qt app focus on the file selection box.
|
||||
"""
|
||||
self.file_list.setFocus()
|
|
@ -1,149 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
OnionShare | https://onionshare.org/
|
||||
|
||||
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
|
||||
|
||||
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/>.
|
||||
"""
|
||||
from PyQt5 import QtCore, QtWidgets, QtGui
|
||||
|
||||
from onionshare import strings
|
||||
|
||||
|
||||
class ShareModeInfo(QtWidgets.QWidget):
|
||||
"""
|
||||
Share mode information widget
|
||||
"""
|
||||
def __init__(self, common, share_mode):
|
||||
super(ShareModeInfo, self).__init__()
|
||||
self.common = common
|
||||
self.share_mode = share_mode
|
||||
|
||||
# Label
|
||||
self.label_text = ""
|
||||
self.label = QtWidgets.QLabel()
|
||||
self.label.setStyleSheet(self.common.css['mode_info_label'])
|
||||
|
||||
# In progress and completed labels
|
||||
self.in_progress_downloads_count = QtWidgets.QLabel()
|
||||
self.in_progress_downloads_count.setStyleSheet(self.common.css['mode_info_label'])
|
||||
self.completed_downloads_count = QtWidgets.QLabel()
|
||||
self.completed_downloads_count.setStyleSheet(self.common.css['mode_info_label'])
|
||||
|
||||
# Toggle button
|
||||
self.toggle_button = QtWidgets.QPushButton()
|
||||
self.toggle_button.setDefault(False)
|
||||
self.toggle_button.setFixedWidth(35)
|
||||
self.toggle_button.setFixedHeight(30)
|
||||
self.toggle_button.setFlat(True)
|
||||
self.toggle_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/downloads_toggle.png')) )
|
||||
self.toggle_button.clicked.connect(self.toggle_downloads)
|
||||
|
||||
# Keep track of indicator
|
||||
self.indicator_count = 0
|
||||
self.indicator_label = QtWidgets.QLabel(parent=self.toggle_button)
|
||||
self.indicator_label.setStyleSheet(self.common.css['download_uploads_indicator'])
|
||||
self.update_indicator()
|
||||
|
||||
# Layout
|
||||
layout = QtWidgets.QHBoxLayout()
|
||||
layout.addWidget(self.label)
|
||||
layout.addStretch()
|
||||
layout.addWidget(self.in_progress_downloads_count)
|
||||
layout.addWidget(self.completed_downloads_count)
|
||||
layout.addWidget(self.toggle_button)
|
||||
self.setLayout(layout)
|
||||
|
||||
self.update_downloads_completed()
|
||||
self.update_downloads_in_progress()
|
||||
|
||||
def update_label(self, s):
|
||||
"""
|
||||
Updates the text of the label.
|
||||
"""
|
||||
self.label_text = s
|
||||
self.label.setText(self.label_text)
|
||||
|
||||
def update_indicator(self, increment=False):
|
||||
"""
|
||||
Update the display of the indicator count. If increment is True, then
|
||||
only increment the counter if Downloads is hidden.
|
||||
"""
|
||||
if increment and not self.share_mode.downloads.isVisible():
|
||||
self.indicator_count += 1
|
||||
|
||||
self.indicator_label.setText("{}".format(self.indicator_count))
|
||||
|
||||
if self.indicator_count == 0:
|
||||
self.indicator_label.hide()
|
||||
else:
|
||||
size = self.indicator_label.sizeHint()
|
||||
self.indicator_label.setGeometry(35-size.width(), 0, size.width(), size.height())
|
||||
self.indicator_label.show()
|
||||
|
||||
def update_downloads_completed(self):
|
||||
"""
|
||||
Update the 'Downloads completed' info widget.
|
||||
"""
|
||||
if self.share_mode.downloads_completed == 0:
|
||||
image = self.common.get_resource_path('images/share_completed_none.png')
|
||||
else:
|
||||
image = self.common.get_resource_path('images/share_completed.png')
|
||||
self.completed_downloads_count.setText('<img src="{0:s}" /> {1:d}'.format(image, self.share_mode.downloads_completed))
|
||||
self.completed_downloads_count.setToolTip(strings._('info_completed_downloads_tooltip', True).format(self.share_mode.downloads_completed))
|
||||
|
||||
def update_downloads_in_progress(self):
|
||||
"""
|
||||
Update the 'Downloads in progress' info widget.
|
||||
"""
|
||||
if self.share_mode.downloads_in_progress == 0:
|
||||
image = self.common.get_resource_path('images/share_in_progress_none.png')
|
||||
else:
|
||||
image = self.common.get_resource_path('images/share_in_progress.png')
|
||||
self.in_progress_downloads_count.setText('<img src="{0:s}" /> {1:d}'.format(image, self.share_mode.downloads_in_progress))
|
||||
self.in_progress_downloads_count.setToolTip(strings._('info_in_progress_downloads_tooltip', True).format(self.share_mode.downloads_in_progress))
|
||||
|
||||
def toggle_downloads(self):
|
||||
"""
|
||||
Toggle showing and hiding the Downloads widget
|
||||
"""
|
||||
self.common.log('ShareModeInfo', 'toggle_downloads')
|
||||
|
||||
if self.share_mode.downloads.isVisible():
|
||||
self.share_mode.downloads.hide()
|
||||
self.toggle_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/downloads_toggle.png')) )
|
||||
self.toggle_button.setFlat(True)
|
||||
else:
|
||||
self.share_mode.downloads.show()
|
||||
self.toggle_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/downloads_toggle_selected.png')) )
|
||||
self.toggle_button.setFlat(False)
|
||||
|
||||
# Reset the indicator count
|
||||
self.indicator_count = 0
|
||||
self.update_indicator()
|
||||
|
||||
self.share_mode.resize_window()
|
||||
|
||||
def show_less(self):
|
||||
"""
|
||||
Remove clutter widgets that aren't necessary.
|
||||
"""
|
||||
self.label.setText("")
|
||||
|
||||
def show_more(self):
|
||||
"""
|
||||
Show all widgets.
|
||||
"""
|
||||
self.label.setText(self.label_text)
|
|
@ -1,60 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
OnionShare | https://onionshare.org/
|
||||
|
||||
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
|
||||
|
||||
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/>.
|
||||
"""
|
||||
from PyQt5 import QtCore
|
||||
|
||||
|
||||
class CompressThread(QtCore.QThread):
|
||||
"""
|
||||
Compresses files to be shared
|
||||
"""
|
||||
success = QtCore.pyqtSignal()
|
||||
error = QtCore.pyqtSignal(str)
|
||||
|
||||
def __init__(self, mode):
|
||||
super(CompressThread, self).__init__()
|
||||
self.mode = mode
|
||||
self.mode.common.log('CompressThread', '__init__')
|
||||
|
||||
# prepare files to share
|
||||
def set_processed_size(self, x):
|
||||
if self.mode._zip_progress_bar != None:
|
||||
self.mode._zip_progress_bar.update_processed_size_signal.emit(x)
|
||||
|
||||
def run(self):
|
||||
self.mode.common.log('CompressThread', 'run')
|
||||
|
||||
try:
|
||||
if self.mode.web.share_mode.set_file_info(self.mode.filenames, processed_size_callback=self.set_processed_size):
|
||||
self.success.emit()
|
||||
else:
|
||||
# Cancelled
|
||||
pass
|
||||
|
||||
self.mode.app.cleanup_filenames += self.mode.web.share_mode.cleanup_filenames
|
||||
except OSError as e:
|
||||
self.error.emit(e.strerror)
|
||||
|
||||
def cancel(self):
|
||||
self.mode.common.log('CompressThread', 'cancel')
|
||||
|
||||
# Let the Web and ZipWriter objects know that we're canceling compression early
|
||||
self.mode.web.cancel_compression = True
|
||||
if self.mode.web.zip_writer:
|
||||
self.mode.web.zip_writer.cancel_compression = True
|
Loading…
Add table
Add a link
Reference in a new issue