2018-05-04 19:26:54 -04:00
|
|
|
# -*- 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/>.
|
|
|
|
"""
|
2018-05-20 17:40:27 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
2018-09-18 20:57:12 -04:00
|
|
|
import textwrap
|
2018-05-08 02:07:11 -04:00
|
|
|
from datetime import datetime
|
2018-05-04 19:26:54 -04:00
|
|
|
from PyQt5 import QtCore, QtWidgets, QtGui
|
|
|
|
|
|
|
|
from onionshare import strings
|
2018-05-20 17:40:27 -04:00
|
|
|
from ..widgets import Alert
|
2018-05-04 19:26:54 -04:00
|
|
|
|
2018-05-07 18:44:04 -04:00
|
|
|
|
2018-05-08 17:28:02 -04:00
|
|
|
class File(QtWidgets.QWidget):
|
|
|
|
def __init__(self, common, filename):
|
|
|
|
super(File, self).__init__()
|
|
|
|
self.common = common
|
|
|
|
|
2018-05-20 17:40:27 -04:00
|
|
|
self.common.log('File', '__init__', 'filename: {}'.format(filename))
|
|
|
|
|
|
|
|
self.filename = filename
|
2018-10-26 00:38:20 -04:00
|
|
|
self.dir = None
|
2018-05-08 17:28:02 -04:00
|
|
|
self.started = datetime.now()
|
|
|
|
|
|
|
|
# Filename label
|
2018-05-20 01:36:08 -04:00
|
|
|
self.filename_label = QtWidgets.QLabel(self.filename)
|
2018-08-31 19:20:50 -04:00
|
|
|
self.filename_label_width = self.filename_label.width()
|
2018-05-20 01:36:08 -04:00
|
|
|
|
|
|
|
# File size label
|
|
|
|
self.filesize_label = QtWidgets.QLabel()
|
|
|
|
self.filesize_label.setStyleSheet(self.common.css['receive_file_size'])
|
|
|
|
self.filesize_label.hide()
|
|
|
|
|
|
|
|
# Folder button
|
|
|
|
folder_pixmap = QtGui.QPixmap.fromImage(QtGui.QImage(self.common.get_resource_path('images/open_folder.png')))
|
|
|
|
folder_icon = QtGui.QIcon(folder_pixmap)
|
|
|
|
self.folder_button = QtWidgets.QPushButton()
|
2018-05-20 17:40:27 -04:00
|
|
|
self.folder_button.clicked.connect(self.open_folder)
|
2018-05-20 01:36:08 -04:00
|
|
|
self.folder_button.setIcon(folder_icon)
|
|
|
|
self.folder_button.setIconSize(folder_pixmap.rect().size())
|
|
|
|
self.folder_button.setFlat(True)
|
|
|
|
self.folder_button.hide()
|
|
|
|
|
|
|
|
# Layouts
|
|
|
|
layout = QtWidgets.QHBoxLayout()
|
|
|
|
layout.addWidget(self.filename_label)
|
|
|
|
layout.addWidget(self.filesize_label)
|
|
|
|
layout.addStretch()
|
|
|
|
layout.addWidget(self.folder_button)
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
|
|
|
def update(self, uploaded_bytes, complete):
|
|
|
|
self.filesize_label.setText(self.common.human_readable_filesize(uploaded_bytes))
|
|
|
|
self.filesize_label.show()
|
|
|
|
|
|
|
|
if complete:
|
|
|
|
self.folder_button.show()
|
|
|
|
|
2018-10-26 00:38:20 -04:00
|
|
|
def set_dir(self, dir):
|
|
|
|
self.dir = dir
|
|
|
|
|
2018-05-20 17:12:53 -04:00
|
|
|
def rename(self, new_filename):
|
|
|
|
self.filename = new_filename
|
|
|
|
self.filename_label.setText(self.filename)
|
|
|
|
|
2018-05-20 17:40:27 -04:00
|
|
|
def open_folder(self):
|
|
|
|
"""
|
|
|
|
Open the downloads folder, with the file selected, in a cross-platform manner
|
|
|
|
"""
|
|
|
|
self.common.log('File', 'open_folder')
|
|
|
|
|
2018-10-26 00:38:20 -04:00
|
|
|
if not self.dir:
|
|
|
|
return
|
|
|
|
|
|
|
|
abs_filename = os.path.join(self.dir, self.filename)
|
2018-05-20 17:40:27 -04:00
|
|
|
|
|
|
|
# Linux
|
|
|
|
if self.common.platform == 'Linux' or self.common.platform == 'BSD':
|
|
|
|
try:
|
|
|
|
# If nautilus is available, open it
|
|
|
|
subprocess.Popen(['nautilus', abs_filename])
|
|
|
|
except:
|
|
|
|
Alert(self.common, strings._('gui_open_folder_error_nautilus').format(abs_filename))
|
|
|
|
|
|
|
|
# macOS
|
|
|
|
elif self.common.platform == 'Darwin':
|
|
|
|
# TODO: Implement opening folder with file selected in macOS
|
|
|
|
# This seems helpful: https://stackoverflow.com/questions/3520493/python-show-in-finder
|
|
|
|
self.common.log('File', 'open_folder', 'not implemented for Darwin yet')
|
|
|
|
|
|
|
|
# Windows
|
|
|
|
elif self.common.platform == 'Windows':
|
|
|
|
# TODO: Implement opening folder with file selected in Windows
|
|
|
|
# This seems helpful: https://stackoverflow.com/questions/6631299/python-opening-a-folder-in-explorer-nautilus-mac-thingie
|
|
|
|
self.common.log('File', 'open_folder', 'not implemented for Windows yet')
|
|
|
|
|
2018-05-20 01:36:08 -04:00
|
|
|
|
|
|
|
class Upload(QtWidgets.QWidget):
|
|
|
|
def __init__(self, common, upload_id, content_length):
|
|
|
|
super(Upload, self).__init__()
|
|
|
|
self.common = common
|
|
|
|
self.upload_id = upload_id
|
|
|
|
self.content_length = content_length
|
|
|
|
self.started = datetime.now()
|
|
|
|
|
|
|
|
# Label
|
2018-09-30 20:47:10 -04:00
|
|
|
self.label = QtWidgets.QLabel(strings._('gui_upload_in_progress').format(self.started.strftime("%b %d, %I:%M%p")))
|
2018-05-08 17:28:02 -04:00
|
|
|
|
|
|
|
# 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.setValue(0)
|
|
|
|
self.progress_bar.setStyleSheet(self.common.css['downloads_uploads_progress_bar'])
|
|
|
|
|
2018-05-20 01:36:08 -04:00
|
|
|
# This layout contains file widgets
|
|
|
|
self.files_layout = QtWidgets.QVBoxLayout()
|
|
|
|
self.files_layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
files_widget = QtWidgets.QWidget()
|
|
|
|
files_widget.setStyleSheet(self.common.css['receive_file'])
|
|
|
|
files_widget.setLayout(self.files_layout)
|
2018-05-08 17:28:02 -04:00
|
|
|
|
2018-05-20 01:36:08 -04:00
|
|
|
# Layout
|
|
|
|
layout = QtWidgets.QVBoxLayout()
|
|
|
|
layout.addWidget(self.label)
|
|
|
|
layout.addWidget(self.progress_bar)
|
|
|
|
layout.addWidget(files_widget)
|
|
|
|
layout.addStretch()
|
2018-05-08 17:28:02 -04:00
|
|
|
self.setLayout(layout)
|
|
|
|
|
2018-05-20 01:36:08 -04:00
|
|
|
# We're also making a dictionary of file widgets, to make them easier to access
|
|
|
|
self.files = {}
|
|
|
|
|
|
|
|
def update(self, progress):
|
|
|
|
"""
|
|
|
|
Using the progress from Web, update the progress bar and file size labels
|
|
|
|
for each file
|
|
|
|
"""
|
|
|
|
total_uploaded_bytes = 0
|
|
|
|
for filename in progress:
|
|
|
|
total_uploaded_bytes += progress[filename]['uploaded_bytes']
|
|
|
|
|
2018-05-20 01:58:55 -04:00
|
|
|
# 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))
|
2018-05-08 17:28:02 -04:00
|
|
|
else:
|
2018-05-20 01:58:55 -04:00
|
|
|
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)
|
2018-05-08 17:28:02 -04:00
|
|
|
|
2018-05-20 17:05:34 -04:00
|
|
|
# Using list(progress) to avoid "RuntimeError: dictionary changed size during iteration"
|
|
|
|
for filename in list(progress):
|
2018-05-08 17:28:02 -04:00
|
|
|
# Add a new file if needed
|
|
|
|
if filename not in self.files:
|
|
|
|
self.files[filename] = File(self.common, filename)
|
2018-05-20 01:36:08 -04:00
|
|
|
self.files_layout.addWidget(self.files[filename])
|
2018-05-08 17:28:02 -04:00
|
|
|
|
|
|
|
# Update the file
|
2018-05-20 01:36:08 -04:00
|
|
|
self.files[filename].update(progress[filename]['uploaded_bytes'], progress[filename]['complete'])
|
2018-05-07 19:38:29 -04:00
|
|
|
|
2018-05-20 17:12:53 -04:00
|
|
|
def rename(self, old_filename, new_filename):
|
|
|
|
self.files[old_filename].rename(new_filename)
|
|
|
|
self.files[new_filename] = self.files.pop(old_filename)
|
|
|
|
|
2018-10-26 00:38:20 -04:00
|
|
|
def set_dir(self, filename, dir):
|
|
|
|
self.files[filename].set_dir(dir)
|
|
|
|
|
2018-05-20 01:58:55 -04:00
|
|
|
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:
|
2018-09-30 20:47:10 -04:00
|
|
|
text = strings._('gui_upload_finished').format(
|
2018-05-20 01:58:55 -04:00
|
|
|
self.started.strftime("%b %d, %I:%M%p")
|
|
|
|
)
|
|
|
|
else:
|
2018-09-30 20:47:10 -04:00
|
|
|
text = strings._('gui_upload_finished_range').format(
|
2018-05-20 01:58:55 -04:00
|
|
|
self.started.strftime("%b %d, %I:%M%p"),
|
|
|
|
self.ended.strftime("%I:%M%p")
|
|
|
|
)
|
|
|
|
else:
|
2018-09-30 20:47:10 -04:00
|
|
|
text = strings._('gui_upload_finished_range').format(
|
2018-05-20 01:58:55 -04:00
|
|
|
self.started.strftime("%b %d, %I:%M%p"),
|
|
|
|
self.ended.strftime("%b %d, %I:%M%p")
|
|
|
|
)
|
|
|
|
self.label.setText(text)
|
|
|
|
|
2018-05-07 19:38:29 -04:00
|
|
|
|
2018-05-04 20:57:30 -04:00
|
|
|
class Uploads(QtWidgets.QScrollArea):
|
2018-05-04 19:26:54 -04:00
|
|
|
"""
|
|
|
|
The uploads chunk of the GUI. This lists all of the active upload
|
|
|
|
progress bars, as well as information about each upload.
|
|
|
|
"""
|
|
|
|
def __init__(self, common):
|
|
|
|
super(Uploads, self).__init__()
|
|
|
|
self.common = common
|
2018-05-08 17:28:02 -04:00
|
|
|
self.common.log('Uploads', '__init__')
|
2018-05-04 19:26:54 -04:00
|
|
|
|
2018-08-31 19:20:50 -04:00
|
|
|
self.resizeEvent = None
|
|
|
|
|
2018-05-04 20:57:30 -04:00
|
|
|
self.uploads = {}
|
|
|
|
|
2018-09-30 20:47:10 -04:00
|
|
|
self.setWindowTitle(strings._('gui_uploads'))
|
2018-05-04 20:57:30 -04:00
|
|
|
self.setWidgetResizable(True)
|
|
|
|
self.setMinimumHeight(150)
|
|
|
|
self.setMinimumWidth(350)
|
|
|
|
self.setWindowIcon(QtGui.QIcon(common.get_resource_path('images/logo.png')))
|
|
|
|
self.setWindowFlags(QtCore.Qt.Sheet | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.CustomizeWindowHint)
|
|
|
|
self.vbar = self.verticalScrollBar()
|
2018-09-17 22:59:01 -04:00
|
|
|
self.vbar.rangeChanged.connect(self.resizeScroll)
|
2018-05-04 20:57:30 -04:00
|
|
|
|
2018-09-30 20:47:10 -04:00
|
|
|
uploads_label = QtWidgets.QLabel(strings._('gui_uploads'))
|
2018-05-07 19:21:22 -04:00
|
|
|
uploads_label.setStyleSheet(self.common.css['downloads_uploads_label'])
|
2018-09-30 20:47:10 -04:00
|
|
|
self.no_uploads_label = QtWidgets.QLabel(strings._('gui_no_uploads'))
|
|
|
|
self.clear_history_button = QtWidgets.QPushButton(strings._('gui_clear_history'))
|
2018-09-19 00:37:07 -04:00
|
|
|
self.clear_history_button.clicked.connect(self.reset)
|
|
|
|
self.clear_history_button.hide()
|
|
|
|
|
2018-05-04 20:57:30 -04:00
|
|
|
|
|
|
|
self.uploads_layout = QtWidgets.QVBoxLayout()
|
|
|
|
|
|
|
|
widget = QtWidgets.QWidget()
|
|
|
|
layout = QtWidgets.QVBoxLayout()
|
|
|
|
layout.addWidget(uploads_label)
|
|
|
|
layout.addWidget(self.no_uploads_label)
|
2018-09-19 00:37:07 -04:00
|
|
|
layout.addWidget(self.clear_history_button)
|
2018-05-04 20:57:30 -04:00
|
|
|
layout.addLayout(self.uploads_layout)
|
|
|
|
layout.addStretch()
|
|
|
|
widget.setLayout(layout)
|
|
|
|
self.setWidget(widget)
|
2018-05-07 18:44:04 -04:00
|
|
|
|
2018-09-17 22:59:01 -04:00
|
|
|
def resizeScroll(self, minimum, maximum):
|
|
|
|
"""
|
|
|
|
Scroll to the bottom of the window when the range changes.
|
|
|
|
"""
|
|
|
|
self.vbar.setValue(maximum)
|
|
|
|
|
2018-05-20 01:36:08 -04:00
|
|
|
def add(self, upload_id, content_length):
|
2018-05-07 19:38:29 -04:00
|
|
|
"""
|
2018-05-08 02:07:11 -04:00
|
|
|
Add a new upload.
|
2018-05-07 19:38:29 -04:00
|
|
|
"""
|
2018-05-20 01:36:08 -04:00
|
|
|
self.common.log('Uploads', 'add', 'upload_id: {}, content_length: {}'.format(upload_id, content_length))
|
2018-05-07 19:38:29 -04:00
|
|
|
# Hide the no_uploads_label
|
|
|
|
self.no_uploads_label.hide()
|
2018-09-19 00:37:07 -04:00
|
|
|
# Show the clear_history_button
|
|
|
|
self.clear_history_button.show()
|
2018-05-07 19:38:29 -04:00
|
|
|
|
|
|
|
# Add it to the list
|
2018-05-20 01:36:08 -04:00
|
|
|
upload = Upload(self.common, upload_id, content_length)
|
2018-05-08 02:07:11 -04:00
|
|
|
self.uploads[upload_id] = upload
|
|
|
|
self.uploads_layout.addWidget(upload)
|
2018-05-07 19:38:29 -04:00
|
|
|
|
2018-05-08 17:28:02 -04:00
|
|
|
def update(self, upload_id, progress):
|
2018-05-07 19:38:29 -04:00
|
|
|
"""
|
2018-05-20 01:58:55 -04:00
|
|
|
Update the progress of an upload.
|
2018-05-07 19:38:29 -04:00
|
|
|
"""
|
2018-05-08 17:28:02 -04:00
|
|
|
self.uploads[upload_id].update(progress)
|
2018-05-20 17:12:53 -04:00
|
|
|
|
|
|
|
def rename(self, upload_id, old_filename, new_filename):
|
|
|
|
"""
|
|
|
|
Rename a file, which happens if the filename already exists in downloads_dir.
|
|
|
|
"""
|
|
|
|
self.uploads[upload_id].rename(old_filename, new_filename)
|
2018-05-20 01:58:55 -04:00
|
|
|
|
|
|
|
def finished(self, upload_id):
|
|
|
|
"""
|
|
|
|
An upload has finished.
|
|
|
|
"""
|
|
|
|
self.uploads[upload_id].finished()
|
2018-05-07 19:38:29 -04:00
|
|
|
|
|
|
|
def cancel(self, upload_id):
|
|
|
|
"""
|
|
|
|
Update an upload progress bar to show that it has been canceled.
|
|
|
|
"""
|
2018-05-08 17:28:02 -04:00
|
|
|
self.common.log('Uploads', 'cancel', 'upload_id: {}'.format(upload_id))
|
2018-05-07 19:38:29 -04:00
|
|
|
self.uploads[upload_id].cancel()
|
|
|
|
|
2018-05-07 18:44:04 -04:00
|
|
|
def reset(self):
|
|
|
|
"""
|
|
|
|
Reset the uploads back to zero
|
|
|
|
"""
|
2018-05-08 17:28:02 -04:00
|
|
|
self.common.log('Uploads', 'reset')
|
2018-05-07 19:38:29 -04:00
|
|
|
for upload in self.uploads.values():
|
2018-09-17 21:51:32 -04:00
|
|
|
upload.close()
|
2018-05-08 02:07:11 -04:00
|
|
|
self.uploads_layout.removeWidget(upload)
|
2018-05-07 19:38:29 -04:00
|
|
|
self.uploads = {}
|
|
|
|
|
|
|
|
self.no_uploads_label.show()
|
2018-09-19 00:37:07 -04:00
|
|
|
self.clear_history_button.hide()
|
2018-05-07 19:38:29 -04:00
|
|
|
self.resize(self.sizeHint())
|
2018-08-31 19:20:50 -04:00
|
|
|
|
|
|
|
def resizeEvent(self, event):
|
|
|
|
width = self.frameGeometry().width()
|
|
|
|
try:
|
|
|
|
for upload in self.uploads.values():
|
|
|
|
for item in upload.files.values():
|
2018-09-18 20:57:12 -04:00
|
|
|
item.filename_label.setText(textwrap.fill(item.filename, 30))
|
|
|
|
item.adjustSize()
|
2018-08-31 19:20:50 -04:00
|
|
|
except:
|
|
|
|
pass
|