mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-29 10:12:38 +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:
|
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
|
||||||
|
|
|
@ -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."""
|
||||||
|
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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(
|
||||||
|
|
Loading…
Reference in a new issue