onionshare/onionshare_gui/share_mode/downloads.py

227 lines
7.8 KiB
Python
Raw Normal View History

2014-09-02 20:30:01 -04:00
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/
2018-04-24 13:07:59 -04:00
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
2014-08-27 19:43:18 -04:00
from onionshare import strings
2014-08-27 19:43:18 -04:00
class Download(object):
def __init__(self, common, download_id, total_bytes):
self.common = common
self.download_id = download_id
self.started = time.time()
self.total_bytes = total_bytes
self.downloaded_bytes = 0
# Progress bar
2018-05-07 18:44:04 -04:00
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
# 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.
2016-08-02 12:43:40 -04:00
# 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.QListWidget):
"""
List of download progess bars.
"""
def __init__(self, common):
super(DownloadList, self).__init__()
self.common = common
self.downloads = {}
self.setMinimumHeight(205)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
def add(self, download_id, total_bytes):
"""
Add a new download progress bar.
"""
download = Download(self.common, download_id, total_bytes)
self.downloads[download_id] = download
item = QtWidgets.QListWidgetItem()
self.addItem(item)
self.setItemWidget(item, download.progress_bar)
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
"""
# Remove all items from list
while True:
item = self.takeItem(0)
if not item:
break
# Close all progress bars
for download in self.downloads.values():
download.progress_bar.close()
self.downloads = {}
2018-09-28 21:30:32 -04:00
class Downloads(QtWidgets.QWidget):
"""
The downloads chunk of the GUI. This lists all of the active download
progress bars.
"""
def __init__(self, common):
2014-08-27 19:43:18 -04:00
super(Downloads, self).__init__()
self.common = common
self.setMinimumWidth(350)
2018-09-28 20:01:48 -04:00
# 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()
2018-09-28 21:30:32 -04:00
self.empty.setStyleSheet(self.common.css['downloads_uploads_empty'])
2018-09-28 20:01:48 -04:00
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'])
2018-09-28 20:01:48 -04:00
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
2018-09-28 21:30:32 -04:00
not_empty_layout = QtWidgets.QVBoxLayout()
not_empty_layout.addLayout(download_header)
not_empty_layout.addWidget(self.download_list)
2018-09-28 21:30:32 -04:00
self.not_empty = QtWidgets.QWidget()
self.not_empty.setLayout(not_empty_layout)
2018-09-28 20:01:48 -04:00
# Layout
layout = QtWidgets.QVBoxLayout()
2018-09-28 20:01:48 -04:00
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.empty)
layout.addWidget(self.not_empty)
2018-09-28 21:30:32 -04:00
self.setLayout(layout)
2018-09-28 20:01:48 -04:00
# Reset once at the beginning
self.reset()
def add(self, download_id, total_bytes):
"""
Add a new download progress bar.
"""
self.common.log('Downloads', 'add', 'download_id: {}, content_length: {}'.format(download_id, content_length))
2018-09-28 20:01:48 -04:00
# Hide empty, show not empty
self.empty.hide()
self.not_empty.show()
# Add it to the list
self.download_list.add(download_id, total_bytes)
def update(self, download_id, downloaded_bytes):
"""
Update the progress of a download progress bar.
"""
self.download_list.update(download_id, downloaded_bytes)
2014-08-27 19:43:18 -04:00
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()
2018-09-28 20:01:48 -04:00
# Hide not empty, show empty
self.not_empty.hide()
self.empty.show()