2014-09-02 20:30:01 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-02 15:10:42 -04:00
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2018-04-24 13:07:59 -04:00
|
|
|
Copyright (C) 2014-2018 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/>.
|
|
|
|
"""
|
2015-12-23 00:57:46 -05:00
|
|
|
import time
|
2018-02-27 01:59:46 -05:00
|
|
|
from PyQt5 import QtCore, QtWidgets, QtGui
|
2014-08-27 19:43:18 -04:00
|
|
|
|
2018-03-08 13:18:31 -05:00
|
|
|
from onionshare import strings
|
2014-08-27 19:43:18 -04:00
|
|
|
|
2015-12-23 00:57:46 -05:00
|
|
|
class Download(object):
|
|
|
|
|
2018-03-08 13:18:31 -05:00
|
|
|
def __init__(self, common, download_id, total_bytes):
|
|
|
|
self.common = common
|
|
|
|
|
2015-12-23 00:57:46 -05:00
|
|
|
self.download_id = download_id
|
|
|
|
self.started = time.time()
|
|
|
|
self.total_bytes = total_bytes
|
|
|
|
self.downloaded_bytes = 0
|
|
|
|
|
|
|
|
# make a new progress bar
|
2017-05-27 05:53:30 -04:00
|
|
|
cssStyleData ="""
|
|
|
|
QProgressBar {
|
2018-02-06 20:10:42 -05:00
|
|
|
border: 1px solid #4e064f;
|
|
|
|
background-color: #ffffff !important;
|
2017-05-27 05:53:30 -04:00
|
|
|
text-align: center;
|
2018-02-06 20:10:42 -05:00
|
|
|
color: #9b9b9b;
|
|
|
|
font-size: 12px;
|
2017-05-27 05:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QProgressBar::chunk {
|
2018-02-06 20:10:42 -05:00
|
|
|
background-color: #4e064f;
|
2017-05-27 05:53:30 -04:00
|
|
|
width: 10px;
|
|
|
|
}"""
|
2016-02-12 18:12:27 -05:00
|
|
|
self.progress_bar = QtWidgets.QProgressBar()
|
2015-12-23 00:57:46 -05:00
|
|
|
self.progress_bar.setTextVisible(True)
|
2017-05-31 03:35:16 -04:00
|
|
|
self.progress_bar.setAttribute(QtCore.Qt.WA_DeleteOnClose)
|
2015-12-23 00:57:46 -05:00
|
|
|
self.progress_bar.setAlignment(QtCore.Qt.AlignHCenter)
|
|
|
|
self.progress_bar.setMinimum(0)
|
|
|
|
self.progress_bar.setMaximum(total_bytes)
|
|
|
|
self.progress_bar.setValue(0)
|
2017-05-27 05:53:30 -04:00
|
|
|
self.progress_bar.setStyleSheet(cssStyleData)
|
2015-12-23 00:57:46 -05:00
|
|
|
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:
|
2016-02-12 11:58:29 -05:00
|
|
|
pb_fmt = strings._('gui_download_progress_complete').format(
|
2018-03-08 13:18:31 -05:00
|
|
|
self.common.format_seconds(time.time() - self.started))
|
2015-12-23 00:57:46 -05:00
|
|
|
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
|
2015-12-23 00:57:46 -05:00
|
|
|
# the beginning of the download.
|
2016-02-12 11:58:29 -05:00
|
|
|
pb_fmt = strings._('gui_download_progress_starting').format(
|
2018-03-08 13:18:31 -05:00
|
|
|
self.common.human_readable_filesize(downloaded_bytes))
|
2015-12-23 00:57:46 -05:00
|
|
|
else:
|
2016-02-12 11:58:29 -05:00
|
|
|
pb_fmt = strings._('gui_download_progress_eta').format(
|
2018-03-08 13:18:31 -05:00
|
|
|
self.common.human_readable_filesize(downloaded_bytes),
|
2015-12-23 00:57:46 -05:00
|
|
|
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):
|
2018-03-08 13:18:31 -05:00
|
|
|
return self.common.estimated_time_remaining(self.downloaded_bytes,
|
2015-12-23 00:57:46 -05:00
|
|
|
self.total_bytes,
|
|
|
|
self.started)
|
|
|
|
|
|
|
|
|
2016-12-22 18:15:37 -05:00
|
|
|
class Downloads(QtWidgets.QWidget):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
The downloads chunk of the GUI. This lists all of the active download
|
|
|
|
progress bars.
|
|
|
|
"""
|
2018-03-08 13:18:31 -05:00
|
|
|
def __init__(self, common):
|
2014-08-27 19:43:18 -04:00
|
|
|
super(Downloads, self).__init__()
|
2018-03-08 13:18:31 -05:00
|
|
|
|
|
|
|
self.common = common
|
|
|
|
|
2015-12-23 00:57:46 -05:00
|
|
|
self.downloads = {}
|
2018-02-27 17:48:23 -05:00
|
|
|
|
|
|
|
self.downloads_container = QtWidgets.QScrollArea()
|
|
|
|
self.downloads_container.setWidget(self)
|
|
|
|
self.downloads_container.setWindowTitle(strings._('gui_downloads', True))
|
|
|
|
self.downloads_container.setWidgetResizable(True)
|
|
|
|
self.downloads_container.setMaximumHeight(600)
|
|
|
|
self.downloads_container.setMinimumHeight(150)
|
|
|
|
self.downloads_container.setMinimumWidth(350)
|
|
|
|
self.downloads_container.setWindowIcon(QtGui.QIcon(common.get_resource_path('images/logo.png')))
|
|
|
|
self.downloads_container.setWindowFlags(QtCore.Qt.Sheet | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.CustomizeWindowHint)
|
|
|
|
self.downloads_container.vbar = self.downloads_container.verticalScrollBar()
|
|
|
|
|
2018-02-27 01:59:46 -05:00
|
|
|
self.downloads_label = QtWidgets.QLabel(strings._('gui_downloads', True))
|
|
|
|
self.downloads_label.setStyleSheet('QLabel { font-weight: bold; font-size 14px; text-align: center; }')
|
|
|
|
self.no_downloads_label = QtWidgets.QLabel(strings._('gui_no_downloads', True))
|
2018-02-27 17:48:23 -05:00
|
|
|
|
2018-04-22 18:25:28 -04:00
|
|
|
self.downloads_layout = QtWidgets.QVBoxLayout()
|
|
|
|
|
2016-12-22 18:15:37 -05:00
|
|
|
self.layout = QtWidgets.QVBoxLayout()
|
2018-02-27 01:59:46 -05:00
|
|
|
self.layout.addWidget(self.downloads_label)
|
|
|
|
self.layout.addWidget(self.no_downloads_label)
|
2018-04-22 18:25:28 -04:00
|
|
|
self.layout.addLayout(self.downloads_layout)
|
|
|
|
self.layout.addStretch()
|
2016-12-22 18:15:37 -05:00
|
|
|
self.setLayout(self.layout)
|
2014-08-28 02:52:56 -04:00
|
|
|
|
|
|
|
def add_download(self, download_id, total_bytes):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Add a new download progress bar.
|
|
|
|
"""
|
2014-08-28 02:52:56 -04:00
|
|
|
# add it to the list
|
2018-03-08 13:18:31 -05:00
|
|
|
download = Download(self.common, download_id, total_bytes)
|
2015-12-23 00:57:46 -05:00
|
|
|
self.downloads[download_id] = download
|
2018-04-22 18:25:28 -04:00
|
|
|
self.downloads_layout.addWidget(download.progress_bar)
|
2014-08-28 02:52:56 -04:00
|
|
|
|
2015-12-23 00:57:46 -05:00
|
|
|
def update_download(self, download_id, downloaded_bytes):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Update the progress of a download progress bar.
|
|
|
|
"""
|
2015-12-23 00:57:46 -05:00
|
|
|
self.downloads[download_id].update(downloaded_bytes)
|
2014-08-27 19:43:18 -04:00
|
|
|
|
2014-09-22 16:22:30 -04:00
|
|
|
def cancel_download(self, download_id):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Update a download progress bar to show that it has been canceled.
|
|
|
|
"""
|
2015-12-23 00:57:46 -05:00
|
|
|
self.downloads[download_id].cancel()
|
2017-05-31 03:35:16 -04:00
|
|
|
|
|
|
|
def reset_downloads(self):
|
|
|
|
"""
|
|
|
|
Reset the downloads back to zero
|
|
|
|
"""
|
|
|
|
for download in self.downloads.values():
|
2018-04-22 18:25:28 -04:00
|
|
|
self.downloads_layout.removeWidget(download.progress_bar)
|
2017-05-31 05:44:53 -04:00
|
|
|
download.progress_bar.close()
|
2017-05-31 03:35:16 -04:00
|
|
|
self.downloads = {}
|