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.
This commit is contained in:
deeplow 2022-11-29 10:59:08 +00:00
parent 65d0b7a0d0
commit 06797ab626
No known key found for this signature in database
GPG key ID: 577982871529A52A
4 changed files with 28 additions and 4 deletions

View file

@ -195,3 +195,8 @@ class Document:
def mark_as_safe(self) -> None: def mark_as_safe(self) -> None:
log.debug(f"Marking doc {self.id} as 'safe'") log.debug(f"Marking doc {self.id} as 'safe'")
self.state = Document.STATE_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

View file

@ -14,6 +14,13 @@ class DocumentFilenameException(Exception):
"""Exception for document-related filename errors.""" """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): class InputFileNotFoundException(DocumentFilenameException):
"""Exception for when an input file does not exist.""" """Exception for when an input file does not exist."""

View file

@ -240,11 +240,21 @@ class ContentWidget(QtWidgets.QWidget):
def documents_selected(self, new_docs: List[Document]) -> None: def documents_selected(self, new_docs: List[Document]) -> None:
if not self.conversion_started: if not self.conversion_started:
for doc in new_docs: for doc in new_docs.copy():
try:
self.dangerzone.add_document(doc) 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.doc_selection_widget.hide()
self.settings_widget.show() self.settings_widget.show()
if len(new_docs) > 0:
self.documents_added.emit(new_docs) self.documents_added.emit(new_docs)
else: else:

View file

@ -12,7 +12,7 @@ from typing import Callable, List, Optional
import appdirs import appdirs
import colorama import colorama
from . import container from . import container, errors
from .document import Document from .document import Document
from .settings import Settings from .settings import Settings
from .util import get_resource_path from .util import get_resource_path
@ -51,6 +51,8 @@ class DangerzoneCore(object):
self.add_document(doc) self.add_document(doc)
def add_document(self, doc: Document) -> None: def add_document(self, doc: Document) -> None:
if doc in self.documents:
raise errors.AddedDuplicateDocumentException()
self.documents.append(doc) self.documents.append(doc)
def convert_documents( def convert_documents(