From e487b7f0a9633bdaf96176a9331e0620d614303a Mon Sep 17 00:00:00 2001 From: deeplow Date: Tue, 11 Oct 2022 00:13:36 +0300 Subject: [PATCH] Instantiate documents with a filename Avoid setting document's filename via document.filename and instead do it via object instantiation where possible. Incidentally this has to change some window logic. When select_document() is called it no longer checks if there is already an open window with no document selected yet. The user can open as many windows with unselected documents as they want. --- dangerzone/cli.py | 3 +-- dangerzone/document.py | 6 ++++-- dangerzone/gui/__init__.py | 16 ++++++---------- dangerzone/gui/main_window.py | 8 ++++++-- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/dangerzone/cli.py b/dangerzone/cli.py index 96cbf48..9b16af0 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -28,7 +28,6 @@ def cli_main( ) -> None: setup_logging() global_common = GlobalCommon() - document = Document() display_banner() @@ -44,7 +43,7 @@ def cli_main( click.echo("Invalid filename") exit(1) - document.input_filename = os.path.abspath(filename) + document = Document(os.path.abspath(filename)) # Validate safe PDF output filename if output_filename: diff --git a/dangerzone/document.py b/dangerzone/document.py index d82c40b..c3f4b27 100644 --- a/dangerzone/document.py +++ b/dangerzone/document.py @@ -14,11 +14,13 @@ class Document: document, and validating its info. """ - def __init__(self) -> None: - # Name of input and out files + def __init__(self, input_filename: str = None) -> None: self._input_filename: Optional[str] = None self._output_filename: Optional[str] = None + if input_filename: + self.input_filename = input_filename + @property def input_filename(self) -> str: if self._input_filename is None: diff --git a/dangerzone/gui/__init__.py b/dangerzone/gui/__init__.py index 4277de9..4ee12d7 100644 --- a/dangerzone/gui/__init__.py +++ b/dangerzone/gui/__init__.py @@ -10,6 +10,7 @@ import click import colorama from PySide2 import QtCore, QtGui, QtWidgets +from ..document import Document from ..global_common import GlobalCommon from .common import GuiCommon from .main_window import MainWindow @@ -86,16 +87,11 @@ def gui_main(filename: Optional[str]) -> bool: # Open a document in a window def select_document(filename: Optional[str] = None) -> bool: - if ( - len(windows) == 1 - and windows[list(windows.keys())[0]].document.input_filename == None - ): - window = windows[list(windows.keys())[0]] - else: - window_id = uuid.uuid4().hex - window = MainWindow(global_common, gui_common, window_id) - window.delete_window.connect(delete_window) - windows[window_id] = window + document = Document(filename) + window_id = uuid.uuid4().hex + window = MainWindow(global_common, gui_common, window_id) + window.delete_window.connect(delete_window) + windows[window_id] = window if filename: # Validate filename diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 02ffd92..436bac6 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -24,13 +24,17 @@ class MainWindow(QtWidgets.QMainWindow): delete_window = QtCore.Signal(str) def __init__( - self, global_common: GlobalCommon, gui_common: GuiCommon, window_id: str + self, + global_common: GlobalCommon, + gui_common: GuiCommon, + window_id: str, + document: Document, ) -> None: super(MainWindow, self).__init__() self.global_common = global_common self.gui_common = gui_common self.window_id = window_id - self.document = Document() + self.document = document self.setWindowTitle("Dangerzone") self.setWindowIcon(self.gui_common.get_window_icon())