aboutsummaryrefslogtreecommitdiff
path: root/desktop/onionshare/tab/mode/file_selection.py
blob: 1addba22bb0a99598c98560f9bc7360dae2f7030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/

Copyright (C) 2014-2021 Micah Lee, et al. <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 PySide2 import QtCore, QtWidgets, QtGui

from ... import strings
from ...widgets import Alert, AddFileDialog
from ...gui_common import GuiCommon


class DropHereWidget(QtWidgets.QWidget):
    """
    When there are no files or folders in the FileList yet, display the
    'drop files here' message and graphic.
    """

    def __init__(self, common, image_filename, header_text, w, h, parent):
        super(DropHereWidget, self).__init__(parent)
        self.common = common
        self.setAcceptDrops(True)

        self.image_label = QtWidgets.QLabel(parent=self)
        self.image_label.setPixmap(
            QtGui.QPixmap.fromImage(
                QtGui.QImage(GuiCommon.get_resource_path(image_filename))
            )
        )
        self.image_label.setAlignment(QtCore.Qt.AlignCenter)
        self.image_label.show()

        self.header_label = QtWidgets.QLabel(parent=self)
        self.header_label.setText(header_text)
        self.header_label.setStyleSheet(
            self.common.gui.css["share_file_selection_drop_here_header_label"]
        )
        self.header_label.setAlignment(QtCore.Qt.AlignCenter)
        self.header_label.show()

        self.text_label = QtWidgets.QLabel(parent=self)
        self.text_label.setText(strings._("gui_drag_and_drop"))
        self.text_label.setStyleSheet(
            self.common.gui.css["share_file_selection_drop_here_label"]
        )
        self.text_label.setAlignment(QtCore.Qt.AlignCenter)
        self.text_label.show()

        self.resize(w, h)
        self.hide()

    def dragEnterEvent(self, event):
        self.hide()
        event.accept()

    def resize(self, w, h):
        self.setGeometry(0, 0, w, h)
        self.image_label.setGeometry(0, 0, w, h - 100)
        self.header_label.setGeometry(0, 290, w, h - 360)
        self.text_label.setGeometry(0, 340, w, h - 380)


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"))
        self.setStyleSheet(self.common.gui.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.Signal()
    files_updated = QtCore.Signal()

    def __init__(self, common, background_image_filename, header_text, parent=None):
        super(FileList, self).__init__(parent)
        self.common = common
        self.setAcceptDrops(True)

        self.setIconSize(QtCore.QSize(32, 32))
        self.setSortingEnabled(True)
        self.setMinimumHeight(160)
        self.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.drop_here = DropHereWidget(
            self.common,
            background_image_filename,
            header_text,
            self.width(),
            self.height(),
            self,
        )
        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.show()
        else:
            self.drop_here.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.
        """
        self.drop_here.resize(self.width(), self.height())

        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.
        """
        # Drag and drop doesn't work in Flatpak, because of the sandbox
        if self.common.is_flatpak():
            Alert(self.common, strings._("gui_dragdrop_sandbox_flatpak").format())
            event.ignore()
            return

        if event.mimeData().hasUrls:
            self.setStyleSheet(self.common.gui.css["share_file_list_drag_enter"])
            count = len(event.mimeData().urls())
            self.drop_count.setText(f"+{count}")

            size_hint = self.drop_count.sizeHint()
            self.drop_count.setGeometry(
                self.width() - size_hint.width() - 30,
                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.
        """
        # Drag and drop doesn't work in Flatpak, because of the sandbox
        if self.common.is_flatpak():
            event.ignore()
            return

        self.setStyleSheet(self.common.gui.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.
        """
        # Drag and drop doesn't work in Flatpak, because of the sandbox
        if self.common.is_flatpak():
            event.ignore()
            return

        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.
        """
        # Drag and drop doesn't work in Flatpak, because of the sandbox
        if self.common.is_flatpak():
            event.ignore()
            return

        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.gui.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").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.gui.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(GuiCommon.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.setContentsMargins(0, 0, 0, 0)
            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, background_image_filename, header_text, parent):
        super(FileSelection, self).__init__()

        self.common = common
        self.parent = parent

        self.server_on = False

        # File list
        self.file_list = FileList(self.common, background_image_filename, header_text)
        self.file_list.itemSelectionChanged.connect(self.update)
        self.file_list.files_dropped.connect(self.update)
        self.file_list.files_updated.connect(self.update)

        # Sandboxes (for masOS, Flatpak, etc.) need separate add files and folders buttons, in
        # order to use native file selection dialogs
        if self.common.platform == "Darwin" or self.common.is_flatpak():
            self.sandbox = True
        else:
            self.sandbox = False

        # Buttons
        if self.sandbox:
            # The macOS sandbox makes it so the Mac version needs separate add files
            # and folders buttons, in order to use native file selection dialogs
            self.add_files_button = QtWidgets.QPushButton(strings._("gui_add_files"))
            self.add_files_button.clicked.connect(self.add_files)
            self.add_folder_button = QtWidgets.QPushButton(strings._("gui_add_folder"))
            self.add_folder_button.clicked.connect(self.add_folder)
        else:
            self.add_button = QtWidgets.QPushButton(strings._("gui_add"))
            self.add_button.clicked.connect(self.add)
        self.remove_button = QtWidgets.QPushButton(strings._("gui_remove"))
        self.remove_button.clicked.connect(self.delete)
        button_layout = QtWidgets.QHBoxLayout()
        button_layout.addStretch()
        if self.sandbox:
            button_layout.addWidget(self.add_files_button)
            button_layout.addWidget(self.add_folder_button)
        else:
            button_layout.addWidget(self.add_button)
        button_layout.addWidget(self.remove_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:
            if self.sandbox:
                self.add_files_button.hide()
                self.add_folder_button.hide()
            else:
                self.add_button.hide()
            self.remove_button.hide()
        else:
            if self.sandbox:
                self.add_files_button.show()
                self.add_folder_button.show()
            else:
                self.add_button.show()

            # Delete button should be hidden if item isn't selected
            if len(self.file_list.selectedItems()) == 0:
                self.remove_button.hide()
            else:
                self.remove_button.show()

        # Update the file list
        self.file_list.update()

        # Save the latest file list to mode settings
        self.save_filenames()

    def add(self):
        """
        Add button clicked.
        """
        file_dialog = AddFileDialog(self.common, caption=strings._("gui_choose_items"))
        if file_dialog.exec_() == QtWidgets.QDialog.Accepted:
            self.common.log("FileSelection", "add", file_dialog.selectedFiles())
            for filename in file_dialog.selectedFiles():
                self.file_list.add_file(filename)

        self.file_list.setCurrentItem(None)
        self.update()

    def add_files(self):
        """
        Add Files button clicked.
        """
        files = QtWidgets.QFileDialog.getOpenFileNames(
            self.parent, caption=strings._("gui_choose_items")
        )
        self.common.log("FileSelection", "add_files", files)

        filenames = files[0]
        for filename in filenames:
            self.file_list.add_file(filename)

        self.file_list.setCurrentItem(None)
        self.update()

    def add_folder(self):
        """
        Add Folder button clicked.
        """
        filename = QtWidgets.QFileDialog.getExistingDirectory(
            self.parent,
            caption=strings._("gui_choose_items"),
            options=QtWidgets.QFileDialog.ShowDirsOnly,
        )
        self.common.log("FileSelection", "add_folder", filename)
        if filename:
            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 get_filenames(self):
        """
        Return the list of file and folder names
        """
        filenames = []
        for index in range(self.file_list.count()):
            filenames.append(self.file_list.item(index).filename)
        return filenames

    def save_filenames(self):
        """
        Save the filenames to mode settings
        """
        filenames = self.get_filenames()
        if self.parent.tab.mode == self.common.gui.MODE_SHARE:
            self.parent.settings.set("share", "filenames", filenames)
        elif self.parent.tab.mode == self.common.gui.MODE_WEBSITE:
            self.parent.settings.set("website", "filenames", filenames)

    def setFocus(self):
        """
        Set the Qt app focus on the file selection box.
        """
        self.file_list.setFocus()