Add logic to handle documents removal

This implements the backend part of changing documents.
This commit is contained in:
deeplow 2023-06-06 14:31:20 +01:00
parent d0c86fbbe2
commit 2bd97a036a
No known key found for this signature in database
GPG key ID: 577982871529A52A
3 changed files with 71 additions and 47 deletions

View file

@ -199,4 +199,13 @@ class Document:
def __eq__(self, other: object) -> bool: def __eq__(self, other: object) -> bool:
if not isinstance(other, Document): if not isinstance(other, Document):
return False return False
return self.input_filename == other.input_filename return (
Path(self.input_filename).absolute()
== Path(other.input_filename).absolute()
)
def __hash__(self) -> int:
return hash(str(Path(self.input_filename).absolute()))
def __str__(self) -> str:
return self.input_filename

View file

@ -7,7 +7,7 @@ import subprocess
import tempfile import tempfile
import typing import typing
from multiprocessing.pool import ThreadPool from multiprocessing.pool import ThreadPool
from typing import List, Optional from typing import Dict, List, Optional
from colorama import Fore, Style from colorama import Fore, Style
@ -471,10 +471,21 @@ class ContentWidget(QtWidgets.QWidget):
layout.addWidget(self.doc_selection_widget, stretch=1) layout.addWidget(self.doc_selection_widget, stretch=1)
self.setLayout(layout) self.setLayout(layout)
def documents_selected(self, new_docs: List[Document]) -> None: def documents_selected(self, docs: List[Document]) -> None:
if not self.conversion_started: if self.conversion_started:
# assumed all files in batch are in the same directory Alert(
first_doc = new_docs[0] self.dangerzone,
message="Dangerzone does not support adding documents after the conversion has started.",
has_cancel=False,
).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) output_dir = os.path.dirname(first_doc.input_filename)
if not self.dangerzone.output_dir: if not self.dangerzone.output_dir:
self.dangerzone.output_dir = output_dir self.dangerzone.output_dir = output_dir
@ -488,29 +499,14 @@ class ContentWidget(QtWidgets.QWidget):
else: else:
self.dangerzone.output_dir = output_dir self.dangerzone.output_dir = output_dir
for doc in new_docs.copy(): for doc in docs:
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: if len(docs) > 0:
self.documents_added.emit(new_docs) self.documents_added.emit(docs)
else:
Alert(
self.dangerzone,
message="Dangerzone does not support adding documents after the conversion has started.",
has_cancel=False,
).exec_()
def start_clicked(self) -> None: def start_clicked(self) -> None:
self.conversion_started = True self.conversion_started = True
@ -797,14 +793,14 @@ class SettingsWidget(QtWidgets.QWidget):
else: else:
self.start_button.setDisabled(True) self.start_button.setDisabled(True)
def documents_added(self, new_docs: List[Document]) -> None: def documents_added(self, docs: List[Document]) -> None:
self.save_location.setText(os.path.basename(self.dangerzone.output_dir)) self.save_location.setText(os.path.basename(self.dangerzone.output_dir))
self.update_doc_n_labels() self.update_doc_n_labels()
self.update_ui() self.update_ui()
# validations # validations
self.check_writeable_archive_dir(new_docs) self.check_writeable_archive_dir(docs)
def update_doc_n_labels(self) -> None: def update_doc_n_labels(self) -> None:
"""Updates labels dependent on the number of present documents""" """Updates labels dependent on the number of present documents"""
@ -907,30 +903,38 @@ class DocumentsListWidget(QtWidgets.QListWidget):
def __init__(self, dangerzone: DangerzoneGui) -> None: def __init__(self, dangerzone: DangerzoneGui) -> None:
super().__init__() super().__init__()
self.dangerzone = dangerzone self.dangerzone = dangerzone
self.document_widgets: List[DocumentWidget] = [] self.docs_list: List[Document] = []
self.docs_list_widget_map: dict[Document, DocumentWidget] = {}
# Initialize thread_pool only on the first conversion # Initialize thread_pool only on the first conversion
# to ensure docker-daemon detection logic runs first # to ensure docker-daemon detection logic runs first
self.thread_pool_initized = False self.thread_pool_initized = False
def documents_added(self, new_docs: List[Document]) -> None: def clear(self) -> None:
for document in new_docs: self.docs_list = []
self.docs_list_widget_map = {}
super().clear()
def documents_added(self, docs: List[Document]) -> None:
for document in docs:
item = QtWidgets.QListWidgetItem() item = QtWidgets.QListWidgetItem()
item.setSizeHint(QtCore.QSize(500, 50)) item.setSizeHint(QtCore.QSize(500, 50))
widget = DocumentWidget(self.dangerzone, document) widget = DocumentWidget(self.dangerzone, document)
self.addItem(item) self.addItem(item)
self.setItemWidget(item, widget) self.setItemWidget(item, widget)
self.document_widgets.append(widget)
# Keep docs_list in sync with items list
self.docs_list.append(document)
self.docs_list_widget_map[document] = widget
def start_conversion(self) -> None: def start_conversion(self) -> None:
if not self.thread_pool_initized: if not self.thread_pool_initized:
max_jobs = self.dangerzone.isolation_provider.get_max_parallel_conversions() max_jobs = self.dangerzone.isolation_provider.get_max_parallel_conversions()
self.thread_pool = ThreadPool(max_jobs) self.thread_pool = ThreadPool(max_jobs)
for doc_widget in self.document_widgets: for doc in self.docs_list:
task = ConvertTask( task = ConvertTask(self.dangerzone, doc, self.get_ocr_lang())
self.dangerzone, doc_widget.document, self.get_ocr_lang() doc_widget = self.docs_list_widget_map[doc]
)
task.update.connect(doc_widget.update_progress) task.update.connect(doc_widget.update_progress)
task.finished.connect(doc_widget.all_done) task.finished.connect(doc_widget.all_done)
self.thread_pool.apply_async(task.convert_document) self.thread_pool.apply_async(task.convert_document)

View file

@ -58,6 +58,17 @@ class DangerzoneCore(object):
raise errors.AddedDuplicateDocumentException() raise errors.AddedDuplicateDocumentException()
self.documents.append(doc) self.documents.append(doc)
def remove_document(self, doc: Document) -> None:
if doc not in self.documents:
# Sanity check: should not have reached
return
log.debug(f"Removing document {doc.input_filename}")
self.documents.remove(doc)
def clear_documents(self) -> None:
log.debug("Removing all documents")
self.documents = []
def convert_documents( def convert_documents(
self, ocr_lang: Optional[str], stdout_callback: Optional[Callable] = None self, ocr_lang: Optional[str], stdout_callback: Optional[Callable] = None
) -> None: ) -> None: