mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-29 02:12:36 +02:00
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:
parent
65d0b7a0d0
commit
06797ab626
4 changed files with 28 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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."""
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue