Fix a bug in "Change Selection"

Fix a bug in the "Change Selection" action, whereby changing your
selection and picking files from another directory results in:

    "Dangerzone does not support adding documents from multiple
    locations. The newly added documents were ignored."

To fix this, change the output directory when we change selection as
well.
This commit is contained in:
Alex Pyrgiotis 2023-10-09 00:43:27 +03:00
parent edfba0c783
commit ba5adb33c0
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA
2 changed files with 16 additions and 14 deletions

View file

@ -488,24 +488,20 @@ class ContentWidget(QtWidgets.QWidget):
).exec_()
return
# Get previously selected documents
self.dangerzone.clear_documents()
self.documents_list.clear()
# Assumed all files in batch are in the same directory
first_doc = docs[0]
output_dir = os.path.dirname(first_doc.input_filename)
if not self.dangerzone.output_dir:
self.dangerzone.output_dir = output_dir
elif self.dangerzone.output_dir != output_dir:
# Ensure all files in batch are in the same directory
dirnames = {os.path.dirname(doc.input_filename) for doc in docs}
if len(dirnames) > 1:
Alert(
self.dangerzone,
message="Dangerzone does not support adding documents from multiple locations.\n\n The newly added documents were ignored.",
has_cancel=False,
).exec_()
return
else:
self.dangerzone.output_dir = output_dir
# Clear previously selected documents
self.dangerzone.clear_documents()
self.documents_list.clear()
self.dangerzone.output_dir = list(dirnames)[0]
for doc in docs:
self.dangerzone.add_document(doc)

View file

@ -1,3 +1,6 @@
import os
import pathlib
import shutil
import time
import typing
@ -335,6 +338,7 @@ def test_change_document_button(
mocker: MockerFixture,
sample_pdf: str,
sample_doc: str,
tmp_path: pathlib.Path,
) -> None:
# Setup first doc selection
file_dialog_mock = mocker.MagicMock()
@ -350,7 +354,9 @@ def test_change_document_button(
file_dialog_mock.accept()
# Setup doc change
file_dialog_mock.selectedFiles.return_value = (sample_doc,)
shutil.copy(sample_doc, tmp_path)
tmp_sample_doc = tmp_path / os.path.basename(sample_doc)
file_dialog_mock.selectedFiles.return_value = (tmp_sample_doc,)
# When clicking on "select docs" button
with qtbot.waitSignal(content_widget.documents_added):
@ -370,4 +376,4 @@ def test_change_document_button(
for doc in content_widget.dangerzone.get_unconverted_documents()
]
assert len(docs) is 1
assert docs[0] == sample_doc
assert docs[0] == str(tmp_sample_doc)