From 06797ab6261e18e929c4703a8af1d8664a40b180 Mon Sep 17 00:00:00 2001 From: deeplow Date: Tue, 29 Nov 2022 10:59:08 +0000 Subject: [PATCH] Prevent adding duplicate documents It was possible that users would add duplicate documents via 'open with Dangerzone'. This would lead to unexpected situations and preventing it both in the CLI and the GUI solves those issues. --- dangerzone/document.py | 5 +++++ dangerzone/errors.py | 7 +++++++ dangerzone/gui/main_window.py | 16 +++++++++++++--- dangerzone/logic.py | 4 +++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/dangerzone/document.py b/dangerzone/document.py index aa78de3..36145a9 100644 --- a/dangerzone/document.py +++ b/dangerzone/document.py @@ -195,3 +195,8 @@ class Document: def mark_as_safe(self) -> None: log.debug(f"Marking doc {self.id} as 'safe'") self.state = Document.STATE_SAFE + + def __eq__(self, other: object) -> bool: + if not isinstance(other, Document): + return False + return self.input_filename == other.input_filename diff --git a/dangerzone/errors.py b/dangerzone/errors.py index 5e38015..0f8a557 100644 --- a/dangerzone/errors.py +++ b/dangerzone/errors.py @@ -14,6 +14,13 @@ class DocumentFilenameException(Exception): """Exception for document-related filename errors.""" +class AddedDuplicateDocumentException(DocumentFilenameException): + """Exception for a document is added twice.""" + + def __init__(self) -> None: + super().__init__("A document was added twice") + + class InputFileNotFoundException(DocumentFilenameException): """Exception for when an input file does not exist.""" diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index a7e29aa..11d36ea 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -240,12 +240,22 @@ class ContentWidget(QtWidgets.QWidget): def documents_selected(self, new_docs: List[Document]) -> None: if not self.conversion_started: - for doc in new_docs: - self.dangerzone.add_document(doc) + for doc in new_docs.copy(): + try: + self.dangerzone.add_document(doc) + except errors.AddedDuplicateDocumentException: + new_docs.remove(doc) + Alert( + self.dangerzone, + message=f"Document '{doc.input_filename}' has already been added for conversion.", + has_cancel=False, + ).exec_() self.doc_selection_widget.hide() self.settings_widget.show() - self.documents_added.emit(new_docs) + + if len(new_docs) > 0: + self.documents_added.emit(new_docs) else: Alert( diff --git a/dangerzone/logic.py b/dangerzone/logic.py index 1fbe2cf..97cda84 100644 --- a/dangerzone/logic.py +++ b/dangerzone/logic.py @@ -12,7 +12,7 @@ from typing import Callable, List, Optional import appdirs import colorama -from . import container +from . import container, errors from .document import Document from .settings import Settings from .util import get_resource_path @@ -51,6 +51,8 @@ class DangerzoneCore(object): self.add_document(doc) def add_document(self, doc: Document) -> None: + if doc in self.documents: + raise errors.AddedDuplicateDocumentException() self.documents.append(doc) def convert_documents(