2014-09-02 20:30:01 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-02 15:10:42 -04:00
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2017-01-06 21:58:15 -05:00
|
|
|
Copyright (C) 2017 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/>.
|
|
|
|
"""
|
2014-08-27 17:21:08 -04:00
|
|
|
import os
|
2016-02-12 18:12:27 -05:00
|
|
|
from PyQt5 import QtCore, QtWidgets, QtGui
|
2017-05-18 04:09:49 -04:00
|
|
|
from .alert import Alert
|
2014-08-27 17:21:08 -04:00
|
|
|
|
2017-05-16 14:05:48 -04:00
|
|
|
from onionshare import strings, common
|
2014-08-27 17:21:08 -04:00
|
|
|
|
2016-02-12 18:12:27 -05:00
|
|
|
class FileList(QtWidgets.QListWidget):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
The list of files and folders in the GUI.
|
|
|
|
"""
|
2014-08-27 17:21:08 -04:00
|
|
|
files_dropped = QtCore.pyqtSignal()
|
2014-08-27 20:24:44 -04:00
|
|
|
files_updated = QtCore.pyqtSignal()
|
2014-08-27 17:21:08 -04:00
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super(FileList, self).__init__(parent)
|
|
|
|
self.setAcceptDrops(True)
|
|
|
|
self.setIconSize(QtCore.QSize(32, 32))
|
2014-08-28 19:26:56 -04:00
|
|
|
self.setSortingEnabled(True)
|
2014-08-27 17:21:08 -04:00
|
|
|
|
2016-02-12 18:12:27 -05:00
|
|
|
class DropHereLabel(QtWidgets.QLabel):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
When there are no files or folders in the FileList yet, display the
|
|
|
|
'drop files here' message and graphic.
|
|
|
|
"""
|
2014-09-17 18:48:19 -04:00
|
|
|
def __init__(self, parent, image=False):
|
|
|
|
self.parent = parent
|
|
|
|
super(DropHereLabel, self).__init__(parent=parent)
|
|
|
|
self.setAcceptDrops(True)
|
|
|
|
self.setAlignment(QtCore.Qt.AlignCenter)
|
|
|
|
|
|
|
|
if image:
|
2017-05-16 14:05:48 -04:00
|
|
|
self.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(common.get_resource_path('images/drop_files.png'))))
|
2014-09-17 18:48:19 -04:00
|
|
|
else:
|
2016-02-12 18:12:27 -05:00
|
|
|
self.setText(strings._('gui_drag_and_drop', True))
|
2014-09-17 18:48:19 -04:00
|
|
|
self.setStyleSheet('color: #999999;')
|
|
|
|
|
|
|
|
self.hide()
|
|
|
|
|
|
|
|
def dragEnterEvent(self, event):
|
|
|
|
self.parent.drop_here_image.hide()
|
|
|
|
self.parent.drop_here_text.hide()
|
|
|
|
event.ignore()
|
|
|
|
|
|
|
|
self.drop_here_image = DropHereLabel(self, True)
|
|
|
|
self.drop_here_text = DropHereLabel(self, False)
|
2014-08-27 17:21:08 -04:00
|
|
|
|
|
|
|
self.filenames = []
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Update the GUI elements based on the current state.
|
|
|
|
"""
|
2014-08-27 17:21:08 -04:00
|
|
|
# file list should have a background image if empty
|
|
|
|
if len(self.filenames) == 0:
|
2014-09-17 18:48:19 -04:00
|
|
|
self.drop_here_image.show()
|
|
|
|
self.drop_here_text.show()
|
2014-08-27 17:21:08 -04:00
|
|
|
else:
|
2014-09-17 18:48:19 -04:00
|
|
|
self.drop_here_image.hide()
|
|
|
|
self.drop_here_text.hide()
|
2014-08-27 17:21:08 -04:00
|
|
|
|
|
|
|
def resizeEvent(self, event):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
When the widget is resized, resize the drop files image and text.
|
|
|
|
"""
|
2014-09-17 18:48:19 -04:00
|
|
|
self.drop_here_image.setGeometry(0, 0, self.width(), self.height())
|
|
|
|
self.drop_here_text.setGeometry(0, 0, self.width(), self.height())
|
2014-08-27 17:21:08 -04:00
|
|
|
|
|
|
|
def dragEnterEvent(self, event):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
dragEnterEvent for dragging files and directories into the widget.
|
|
|
|
"""
|
2014-08-27 17:21:08 -04:00
|
|
|
if event.mimeData().hasUrls:
|
|
|
|
event.accept()
|
|
|
|
else:
|
|
|
|
event.ignore()
|
|
|
|
|
2014-09-17 18:48:19 -04:00
|
|
|
def dragLeaveEvent(self, event):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
dragLeaveEvent for dragging files and directories into the widget.
|
|
|
|
"""
|
2014-09-17 18:48:19 -04:00
|
|
|
event.accept()
|
|
|
|
self.update()
|
|
|
|
|
2014-08-27 17:21:08 -04:00
|
|
|
def dragMoveEvent(self, event):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
dragMoveEvent for dragging files and directories into the widget.
|
|
|
|
"""
|
2014-08-27 17:21:08 -04:00
|
|
|
if event.mimeData().hasUrls:
|
|
|
|
event.setDropAction(QtCore.Qt.CopyAction)
|
|
|
|
event.accept()
|
|
|
|
else:
|
|
|
|
event.ignore()
|
|
|
|
|
|
|
|
def dropEvent(self, event):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
dropEvent for dragging files and directories into the widget.
|
|
|
|
"""
|
2014-08-27 17:21:08 -04:00
|
|
|
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.files_dropped.emit()
|
|
|
|
|
|
|
|
def add_file(self, filename):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Add a file or directory to this widget.
|
|
|
|
"""
|
2014-08-27 17:21:08 -04:00
|
|
|
if filename not in self.filenames:
|
2017-05-18 13:55:10 -04:00
|
|
|
if not os.access(filename, os.R_OK):
|
|
|
|
Alert(strings._("not_a_readable_file", True).format(filename))
|
|
|
|
return
|
|
|
|
|
2014-08-27 17:21:08 -04:00
|
|
|
self.filenames.append(filename)
|
|
|
|
|
|
|
|
fileinfo = QtCore.QFileInfo(filename)
|
2014-09-22 15:03:50 -04:00
|
|
|
basename = os.path.basename(filename.rstrip('/'))
|
2016-02-12 18:12:27 -05:00
|
|
|
ip = QtWidgets.QFileIconProvider()
|
2014-08-27 17:21:08 -04:00
|
|
|
icon = ip.icon(fileinfo)
|
|
|
|
|
|
|
|
if os.path.isfile(filename):
|
2017-05-16 14:05:48 -04:00
|
|
|
size = common.human_readable_filesize(fileinfo.size())
|
2014-08-27 17:21:08 -04:00
|
|
|
else:
|
2017-05-16 14:05:48 -04:00
|
|
|
size = common.human_readable_filesize(common.dir_size(filename))
|
2016-02-12 18:12:27 -05:00
|
|
|
item_name = '{0:s} ({1:s})'.format(basename, size)
|
|
|
|
item = QtWidgets.QListWidgetItem(item_name)
|
|
|
|
item.setToolTip(size)
|
2014-09-15 20:22:14 -04:00
|
|
|
|
2014-08-27 17:21:08 -04:00
|
|
|
item.setIcon(icon)
|
|
|
|
self.addItem(item)
|
|
|
|
|
2014-08-27 20:24:44 -04:00
|
|
|
self.files_updated.emit()
|
|
|
|
|
2014-11-18 12:29:32 -05:00
|
|
|
|
2016-02-12 18:12:27 -05:00
|
|
|
class FileSelection(QtWidgets.QVBoxLayout):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
The list of files and folders in the GUI, as well as buttons to add and
|
|
|
|
delete the files and folders.
|
|
|
|
"""
|
2014-08-27 17:21:08 -04:00
|
|
|
def __init__(self):
|
|
|
|
super(FileSelection, self).__init__()
|
2014-08-27 20:24:44 -04:00
|
|
|
self.server_on = False
|
2014-08-27 17:21:08 -04:00
|
|
|
|
|
|
|
# file list
|
|
|
|
self.file_list = FileList()
|
|
|
|
self.file_list.currentItemChanged.connect(self.update)
|
|
|
|
self.file_list.files_dropped.connect(self.update)
|
|
|
|
|
|
|
|
# buttons
|
2016-02-12 18:12:27 -05:00
|
|
|
self.add_files_button = QtWidgets.QPushButton(strings._('gui_add_files', True))
|
2014-08-27 17:21:08 -04:00
|
|
|
self.add_files_button.clicked.connect(self.add_files)
|
2016-02-12 18:12:27 -05:00
|
|
|
self.add_dir_button = QtWidgets.QPushButton(strings._('gui_add_folder', True))
|
2014-08-27 17:21:08 -04:00
|
|
|
self.add_dir_button.clicked.connect(self.add_dir)
|
2016-02-12 18:12:27 -05:00
|
|
|
self.delete_button = QtWidgets.QPushButton(strings._('gui_delete', True))
|
2014-08-27 17:21:08 -04:00
|
|
|
self.delete_button.clicked.connect(self.delete_file)
|
2016-02-12 18:12:27 -05:00
|
|
|
button_layout = QtWidgets.QHBoxLayout()
|
2014-08-27 17:21:08 -04:00
|
|
|
button_layout.addWidget(self.add_files_button)
|
|
|
|
button_layout.addWidget(self.add_dir_button)
|
|
|
|
button_layout.addWidget(self.delete_button)
|
|
|
|
|
|
|
|
# add the widgets
|
|
|
|
self.addWidget(self.file_list)
|
|
|
|
self.addLayout(button_layout)
|
|
|
|
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Update the GUI elements based on the current state.
|
|
|
|
"""
|
2014-08-27 20:24:44 -04:00
|
|
|
# all buttons should be disabled if the server is on
|
|
|
|
if self.server_on:
|
|
|
|
self.add_files_button.setEnabled(False)
|
|
|
|
self.add_dir_button.setEnabled(False)
|
2014-08-27 17:21:08 -04:00
|
|
|
self.delete_button.setEnabled(False)
|
|
|
|
else:
|
2014-08-27 20:24:44 -04:00
|
|
|
self.add_files_button.setEnabled(True)
|
|
|
|
self.add_dir_button.setEnabled(True)
|
|
|
|
|
|
|
|
# delete button should be disabled if item isn't selected
|
|
|
|
current_item = self.file_list.currentItem()
|
|
|
|
if not current_item:
|
|
|
|
self.delete_button.setEnabled(False)
|
|
|
|
else:
|
|
|
|
self.delete_button.setEnabled(True)
|
2014-08-27 17:21:08 -04:00
|
|
|
|
|
|
|
# update the file list
|
|
|
|
self.file_list.update()
|
|
|
|
|
|
|
|
def add_files(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Add files button clicked.
|
|
|
|
"""
|
2017-05-22 19:10:55 -04:00
|
|
|
file_dialog = QtWidgets.QFileDialog(caption=strings._('gui_choose_files', True))
|
|
|
|
file_dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
|
|
|
|
file_dialog.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, True)
|
|
|
|
file_dialog.setOption(QtWidgets.QFileDialog.ReadOnly, True)
|
|
|
|
tree_view = file_dialog.findChild(QtWidgets.QTreeView)
|
|
|
|
tree_view.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
|
|
|
|
list_view = file_dialog.findChild(QtWidgets.QListView, "listView")
|
|
|
|
list_view.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
|
|
|
|
|
|
|
|
if file_dialog.exec_() == QtWidgets.QDialog.Accepted:
|
|
|
|
for filename in file_dialog.selectedFiles():
|
|
|
|
self.file_list.add_file(filename)
|
|
|
|
|
2014-08-27 17:21:08 -04:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def add_dir(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Add folder button clicked.
|
|
|
|
"""
|
2017-05-21 02:14:32 -04:00
|
|
|
file_dialog = QtWidgets.QFileDialog(caption=strings._('gui_choose_folder', True))
|
|
|
|
file_dialog.setFileMode(QtWidgets.QFileDialog.Directory)
|
|
|
|
file_dialog.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, True)
|
|
|
|
file_dialog.setOption(QtWidgets.QFileDialog.ReadOnly, True)
|
|
|
|
file_dialog.setOption(QtWidgets.QFileDialog.ShowDirsOnly, True)
|
|
|
|
tree_view = file_dialog.findChild(QtWidgets.QTreeView)
|
|
|
|
tree_view.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
|
|
|
|
list_view = file_dialog.findChild(QtWidgets.QListView, "listView")
|
|
|
|
list_view.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
|
|
|
|
|
|
|
|
if file_dialog.exec_() == QtWidgets.QDialog.Accepted:
|
|
|
|
for filename in file_dialog.selectedFiles():
|
|
|
|
self.file_list.add_file(filename)
|
|
|
|
|
2014-08-27 17:21:08 -04:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def delete_file(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Delete button clicked
|
|
|
|
"""
|
2014-08-27 17:21:08 -04:00
|
|
|
current_row = self.file_list.currentRow()
|
|
|
|
self.file_list.filenames.pop(current_row)
|
|
|
|
self.file_list.takeItem(current_row)
|
|
|
|
self.update()
|
|
|
|
|
2014-08-27 20:24:44 -04:00
|
|
|
def server_started(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Gets called when the server starts.
|
|
|
|
"""
|
2014-08-27 20:24:44 -04:00
|
|
|
self.server_on = True
|
|
|
|
self.file_list.setAcceptDrops(False)
|
|
|
|
self.update()
|
2014-09-15 20:22:14 -04:00
|
|
|
|
2014-08-27 20:24:44 -04:00
|
|
|
def server_stopped(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Gets called when the server stops.
|
|
|
|
"""
|
2014-08-27 20:24:44 -04:00
|
|
|
self.server_on = False
|
|
|
|
self.file_list.setAcceptDrops(True)
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def get_num_files(self):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
|
|
|
Returns the total number of files and folders in the list.
|
|
|
|
"""
|
2014-08-27 20:24:44 -04:00
|
|
|
return len(self.file_list.filenames)
|
2017-05-16 18:24:14 -04:00
|
|
|
|
|
|
|
def setFocus(self):
|
|
|
|
"""
|
|
|
|
Set the Qt app focus on the file selection box.
|
|
|
|
"""
|
|
|
|
self.file_list.setFocus()
|