mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-29 18:22:37 +02:00
Rename common.Common class to document.Document
Rename the `common` module and `common.Common` class to `document` and `document.Document` respectively. Also, rename the variables that hold instances of this class. This change reflects the fact that the class is responsible for tracking the state of the document. When we add bulk document conversion, allowing us to keep track of a document's state will be key. This name change is a step towards that.
This commit is contained in:
parent
03c3541bdc
commit
0493aca036
4 changed files with 50 additions and 46 deletions
|
@ -8,8 +8,8 @@ import click
|
||||||
from colorama import Back, Fore, Style
|
from colorama import Back, Fore, Style
|
||||||
|
|
||||||
from . import container
|
from . import container
|
||||||
from .common import Common
|
|
||||||
from .container import convert
|
from .container import convert
|
||||||
|
from .document import Document
|
||||||
from .global_common import GlobalCommon
|
from .global_common import GlobalCommon
|
||||||
from .util import get_version
|
from .util import get_version
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ def cli_main(
|
||||||
) -> None:
|
) -> None:
|
||||||
setup_logging()
|
setup_logging()
|
||||||
global_common = GlobalCommon()
|
global_common = GlobalCommon()
|
||||||
common = Common()
|
document = Document()
|
||||||
|
|
||||||
display_banner()
|
display_banner()
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ def cli_main(
|
||||||
click.echo("Invalid filename")
|
click.echo("Invalid filename")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
common.input_filename = os.path.abspath(filename)
|
document.input_filename = os.path.abspath(filename)
|
||||||
|
|
||||||
# Validate safe PDF output filename
|
# Validate safe PDF output filename
|
||||||
if output_filename:
|
if output_filename:
|
||||||
|
@ -63,18 +63,18 @@ def cli_main(
|
||||||
click.echo("Safe PDF filename is not writable")
|
click.echo("Safe PDF filename is not writable")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
common.output_filename = os.path.abspath(output_filename)
|
document.output_filename = os.path.abspath(output_filename)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
common.output_filename = (
|
document.output_filename = (
|
||||||
f"{os.path.splitext(common.input_filename)[0]}-safe.pdf"
|
f"{os.path.splitext(document.input_filename)[0]}-safe.pdf"
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
with open(common.output_filename, "wb"):
|
with open(document.output_filename, "wb"):
|
||||||
pass
|
pass
|
||||||
except:
|
except:
|
||||||
click.echo(
|
click.echo(
|
||||||
f"Output filename {common.output_filename} is not writable, use --output-filename"
|
f"Output filename {document.output_filename} is not writable, use --output-filename"
|
||||||
)
|
)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
@ -110,13 +110,13 @@ def cli_main(
|
||||||
click.echo(f"Invalid JSON returned from container: {line}")
|
click.echo(f"Invalid JSON returned from container: {line}")
|
||||||
|
|
||||||
if convert(
|
if convert(
|
||||||
common.input_filename,
|
document.input_filename,
|
||||||
common.output_filename,
|
document.output_filename,
|
||||||
ocr_lang,
|
ocr_lang,
|
||||||
stdout_callback,
|
stdout_callback,
|
||||||
):
|
):
|
||||||
print_header("Safe PDF created successfully")
|
print_header("Safe PDF created successfully")
|
||||||
click.echo(common.output_filename)
|
click.echo(document.output_filename)
|
||||||
exit(0)
|
exit(0)
|
||||||
else:
|
else:
|
||||||
print_header("Failed to convert document")
|
print_header("Failed to convert document")
|
||||||
|
|
|
@ -7,9 +7,11 @@ from typing import Optional
|
||||||
import appdirs
|
import appdirs
|
||||||
|
|
||||||
|
|
||||||
class Common(object):
|
class Document:
|
||||||
"""
|
"""Track the state of a single document.
|
||||||
The Common class is a singleton of shared functionality throughout an open dangerzone window
|
|
||||||
|
The Document class is responsible for holding the state of a single
|
||||||
|
document, and validating its info.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
|
@ -88,7 +88,7 @@ def gui_main(filename: Optional[str]) -> bool:
|
||||||
def select_document(filename: Optional[str] = None) -> bool:
|
def select_document(filename: Optional[str] = None) -> bool:
|
||||||
if (
|
if (
|
||||||
len(windows) == 1
|
len(windows) == 1
|
||||||
and windows[list(windows.keys())[0]].common.input_filename == None
|
and windows[list(windows.keys())[0]].document.input_filename == None
|
||||||
):
|
):
|
||||||
window = windows[list(windows.keys())[0]]
|
window = windows[list(windows.keys())[0]]
|
||||||
else:
|
else:
|
||||||
|
@ -108,7 +108,7 @@ def gui_main(filename: Optional[str]) -> bool:
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
click.echo("Permission denied")
|
click.echo("Permission denied")
|
||||||
return False
|
return False
|
||||||
window.common.input_filename = file_path
|
window.document.input_filename = file_path
|
||||||
window.content_widget.doc_selection_widget.document_selected.emit()
|
window.content_widget.doc_selection_widget.document_selected.emit()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -11,8 +11,8 @@ from colorama import Fore, Style
|
||||||
from PySide2 import QtCore, QtGui, QtWidgets
|
from PySide2 import QtCore, QtGui, QtWidgets
|
||||||
|
|
||||||
from .. import container
|
from .. import container
|
||||||
from ..common import Common
|
|
||||||
from ..container import convert
|
from ..container import convert
|
||||||
|
from ..document import Document
|
||||||
from ..global_common import GlobalCommon
|
from ..global_common import GlobalCommon
|
||||||
from ..util import get_resource_path, get_subprocess_startupinfo
|
from ..util import get_resource_path, get_subprocess_startupinfo
|
||||||
from .common import GuiCommon
|
from .common import GuiCommon
|
||||||
|
@ -30,7 +30,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||||
self.global_common = global_common
|
self.global_common = global_common
|
||||||
self.gui_common = gui_common
|
self.gui_common = gui_common
|
||||||
self.window_id = window_id
|
self.window_id = window_id
|
||||||
self.common = Common()
|
self.document = Document()
|
||||||
|
|
||||||
self.setWindowTitle("Dangerzone")
|
self.setWindowTitle("Dangerzone")
|
||||||
self.setWindowIcon(self.gui_common.get_window_icon())
|
self.setWindowIcon(self.gui_common.get_window_icon())
|
||||||
|
@ -59,7 +59,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||||
|
|
||||||
# Content widget, contains all the window content except waiting widget
|
# Content widget, contains all the window content except waiting widget
|
||||||
self.content_widget = ContentWidget(
|
self.content_widget = ContentWidget(
|
||||||
self.global_common, self.gui_common, self.common
|
self.global_common, self.gui_common, self.document
|
||||||
)
|
)
|
||||||
self.content_widget.close_window.connect(self.close)
|
self.content_widget.close_window.connect(self.close)
|
||||||
|
|
||||||
|
@ -206,21 +206,21 @@ class ContentWidget(QtWidgets.QWidget):
|
||||||
close_window = QtCore.Signal()
|
close_window = QtCore.Signal()
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, global_common: GlobalCommon, gui_common: GuiCommon, common: Common
|
self, global_common: GlobalCommon, gui_common: GuiCommon, document: Document
|
||||||
) -> None:
|
) -> None:
|
||||||
super(ContentWidget, self).__init__()
|
super(ContentWidget, self).__init__()
|
||||||
|
|
||||||
self.global_common = global_common
|
self.global_common = global_common
|
||||||
self.gui_common = gui_common
|
self.gui_common = gui_common
|
||||||
self.common = common
|
self.document = document
|
||||||
|
|
||||||
# Doc selection widget
|
# Doc selection widget
|
||||||
self.doc_selection_widget = DocSelectionWidget(self.common)
|
self.doc_selection_widget = DocSelectionWidget(self.document)
|
||||||
self.doc_selection_widget.document_selected.connect(self.document_selected)
|
self.doc_selection_widget.document_selected.connect(self.document_selected)
|
||||||
|
|
||||||
# Settings
|
# Settings
|
||||||
self.settings_widget = SettingsWidget(
|
self.settings_widget = SettingsWidget(
|
||||||
self.global_common, self.gui_common, self.common
|
self.global_common, self.gui_common, self.document
|
||||||
)
|
)
|
||||||
self.doc_selection_widget.document_selected.connect(
|
self.doc_selection_widget.document_selected.connect(
|
||||||
self.settings_widget.document_selected
|
self.settings_widget.document_selected
|
||||||
|
@ -231,7 +231,7 @@ class ContentWidget(QtWidgets.QWidget):
|
||||||
|
|
||||||
# Convert
|
# Convert
|
||||||
self.convert_widget = ConvertWidget(
|
self.convert_widget = ConvertWidget(
|
||||||
self.global_common, self.gui_common, self.common
|
self.global_common, self.gui_common, self.document
|
||||||
)
|
)
|
||||||
self.convert_widget.close_window.connect(self._close_window)
|
self.convert_widget.close_window.connect(self._close_window)
|
||||||
self.doc_selection_widget.document_selected.connect(
|
self.doc_selection_widget.document_selected.connect(
|
||||||
|
@ -262,9 +262,9 @@ class ContentWidget(QtWidgets.QWidget):
|
||||||
class DocSelectionWidget(QtWidgets.QWidget):
|
class DocSelectionWidget(QtWidgets.QWidget):
|
||||||
document_selected = QtCore.Signal()
|
document_selected = QtCore.Signal()
|
||||||
|
|
||||||
def __init__(self, common: Common) -> None:
|
def __init__(self, document: Document) -> None:
|
||||||
super(DocSelectionWidget, self).__init__()
|
super(DocSelectionWidget, self).__init__()
|
||||||
self.common = common
|
self.document = document
|
||||||
|
|
||||||
# Dangerous document selection
|
# Dangerous document selection
|
||||||
self.dangerous_doc_label = QtWidgets.QLabel()
|
self.dangerous_doc_label = QtWidgets.QLabel()
|
||||||
|
@ -296,7 +296,7 @@ class DocSelectionWidget(QtWidgets.QWidget):
|
||||||
filter="Documents (*.pdf *.docx *.doc *.docm *.xlsx *.xls *.pptx *.ppt *.odt *.odg *.odp *.ods *.jpg *.jpeg *.gif *.png *.tif *.tiff)",
|
filter="Documents (*.pdf *.docx *.doc *.docm *.xlsx *.xls *.pptx *.ppt *.odt *.odg *.odp *.ods *.jpg *.jpeg *.gif *.png *.tif *.tiff)",
|
||||||
)
|
)
|
||||||
if filename != "":
|
if filename != "":
|
||||||
self.common.input_filename = filename
|
self.document.input_filename = filename
|
||||||
self.document_selected.emit()
|
self.document_selected.emit()
|
||||||
|
|
||||||
|
|
||||||
|
@ -305,12 +305,12 @@ class SettingsWidget(QtWidgets.QWidget):
|
||||||
close_window = QtCore.Signal()
|
close_window = QtCore.Signal()
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, global_common: GlobalCommon, gui_common: GuiCommon, common: Common
|
self, global_common: GlobalCommon, gui_common: GuiCommon, document: Document
|
||||||
) -> None:
|
) -> None:
|
||||||
super(SettingsWidget, self).__init__()
|
super(SettingsWidget, self).__init__()
|
||||||
self.global_common = global_common
|
self.global_common = global_common
|
||||||
self.gui_common = gui_common
|
self.gui_common = gui_common
|
||||||
self.common = common
|
self.document = document
|
||||||
|
|
||||||
# Dangerous document label
|
# Dangerous document label
|
||||||
self.dangerous_doc_label = QtWidgets.QLabel()
|
self.dangerous_doc_label = QtWidgets.QLabel()
|
||||||
|
@ -446,30 +446,32 @@ class SettingsWidget(QtWidgets.QWidget):
|
||||||
def document_selected(self) -> None:
|
def document_selected(self) -> None:
|
||||||
# Update the danger doc label
|
# Update the danger doc label
|
||||||
self.dangerous_doc_label.setText(
|
self.dangerous_doc_label.setText(
|
||||||
f"Suspicious: {os.path.basename(self.common.input_filename)}"
|
f"Suspicious: {os.path.basename(self.document.input_filename)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update the save location
|
# Update the save location
|
||||||
output_filename = f"{os.path.splitext(self.common.input_filename)[0]}-safe.pdf"
|
output_filename = (
|
||||||
self.common.output_filename = output_filename
|
f"{os.path.splitext(self.document.input_filename)[0]}-safe.pdf"
|
||||||
|
)
|
||||||
|
self.document.output_filename = output_filename
|
||||||
self.save_lineedit.setText(os.path.basename(output_filename))
|
self.save_lineedit.setText(os.path.basename(output_filename))
|
||||||
|
|
||||||
def save_browse_button_clicked(self) -> None:
|
def save_browse_button_clicked(self) -> None:
|
||||||
filename = QtWidgets.QFileDialog.getSaveFileName(
|
filename = QtWidgets.QFileDialog.getSaveFileName(
|
||||||
self,
|
self,
|
||||||
"Save safe PDF as...",
|
"Save safe PDF as...",
|
||||||
self.common.output_filename,
|
self.document.output_filename,
|
||||||
filter="Documents (*.pdf)",
|
filter="Documents (*.pdf)",
|
||||||
)
|
)
|
||||||
if filename[0] != "":
|
if filename[0] != "":
|
||||||
self.common.output_filename = filename[0]
|
self.document.output_filename = filename[0]
|
||||||
self.save_lineedit.setText(os.path.basename(self.common.output_filename))
|
self.save_lineedit.setText(os.path.basename(self.document.output_filename))
|
||||||
|
|
||||||
def start_button_clicked(self) -> None:
|
def start_button_clicked(self) -> None:
|
||||||
if self.save_checkbox.checkState() == QtCore.Qt.Unchecked:
|
if self.save_checkbox.checkState() == QtCore.Qt.Unchecked:
|
||||||
# If not saving, then save it to a temp file instead
|
# If not saving, then save it to a temp file instead
|
||||||
tmp = tempfile.mkstemp(suffix=".pdf", prefix="dangerzone_")
|
tmp = tempfile.mkstemp(suffix=".pdf", prefix="dangerzone_")
|
||||||
self.common.output_filename = tmp[1]
|
self.document.output_filename = tmp[1]
|
||||||
|
|
||||||
# Update settings
|
# Update settings
|
||||||
self.global_common.settings.set(
|
self.global_common.settings.set(
|
||||||
|
@ -497,10 +499,10 @@ class ConvertThread(QtCore.QThread):
|
||||||
finished = QtCore.Signal(bool)
|
finished = QtCore.Signal(bool)
|
||||||
update = QtCore.Signal(bool, str, int)
|
update = QtCore.Signal(bool, str, int)
|
||||||
|
|
||||||
def __init__(self, global_common: GlobalCommon, common: Common) -> None:
|
def __init__(self, global_common: GlobalCommon, document: Document) -> None:
|
||||||
super(ConvertThread, self).__init__()
|
super(ConvertThread, self).__init__()
|
||||||
self.global_common = global_common
|
self.global_common = global_common
|
||||||
self.common = common
|
self.document = document
|
||||||
self.error = False
|
self.error = False
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
|
@ -512,8 +514,8 @@ class ConvertThread(QtCore.QThread):
|
||||||
ocr_lang = None
|
ocr_lang = None
|
||||||
|
|
||||||
if convert(
|
if convert(
|
||||||
self.common.input_filename,
|
self.document.input_filename,
|
||||||
self.common.output_filename,
|
self.document.output_filename,
|
||||||
ocr_lang,
|
ocr_lang,
|
||||||
self.stdout_callback,
|
self.stdout_callback,
|
||||||
):
|
):
|
||||||
|
@ -546,12 +548,12 @@ class ConvertWidget(QtWidgets.QWidget):
|
||||||
close_window = QtCore.Signal()
|
close_window = QtCore.Signal()
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, global_common: GlobalCommon, gui_common: GuiCommon, common: Common
|
self, global_common: GlobalCommon, gui_common: GuiCommon, document: Document
|
||||||
) -> None:
|
) -> None:
|
||||||
super(ConvertWidget, self).__init__()
|
super(ConvertWidget, self).__init__()
|
||||||
self.global_common = global_common
|
self.global_common = global_common
|
||||||
self.gui_common = gui_common
|
self.gui_common = gui_common
|
||||||
self.common = common
|
self.document = document
|
||||||
|
|
||||||
self.error = False
|
self.error = False
|
||||||
|
|
||||||
|
@ -595,11 +597,11 @@ class ConvertWidget(QtWidgets.QWidget):
|
||||||
def document_selected(self) -> None:
|
def document_selected(self) -> None:
|
||||||
# Update the danger doc label
|
# Update the danger doc label
|
||||||
self.dangerous_doc_label.setText(
|
self.dangerous_doc_label.setText(
|
||||||
f"Suspicious: {os.path.basename(self.common.input_filename)}"
|
f"Suspicious: {os.path.basename(self.document.input_filename)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
self.convert_t = ConvertThread(self.global_common, self.common)
|
self.convert_t = ConvertThread(self.global_common, self.document)
|
||||||
self.convert_t.update.connect(self.update_progress)
|
self.convert_t.update.connect(self.update_progress)
|
||||||
self.convert_t.finished.connect(self.all_done)
|
self.convert_t.finished.connect(self.all_done)
|
||||||
self.convert_t.start()
|
self.convert_t.start()
|
||||||
|
@ -619,7 +621,7 @@ class ConvertWidget(QtWidgets.QWidget):
|
||||||
|
|
||||||
# In Windows, open Explorer with the safe PDF in focus
|
# In Windows, open Explorer with the safe PDF in focus
|
||||||
if platform.system() == "Windows":
|
if platform.system() == "Windows":
|
||||||
dest_filename_windows = self.common.output_filename.replace("/", "\\")
|
dest_filename_windows = self.document.output_filename.replace("/", "\\")
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
f'explorer.exe /select,"{dest_filename_windows}"',
|
f'explorer.exe /select,"{dest_filename_windows}"',
|
||||||
shell=True,
|
shell=True,
|
||||||
|
@ -628,7 +630,7 @@ class ConvertWidget(QtWidgets.QWidget):
|
||||||
|
|
||||||
# Open
|
# Open
|
||||||
if self.global_common.settings.get("open"):
|
if self.global_common.settings.get("open"):
|
||||||
self.gui_common.open_pdf_viewer(self.common.output_filename)
|
self.gui_common.open_pdf_viewer(self.document.output_filename)
|
||||||
|
|
||||||
# Quit
|
# Quit
|
||||||
if platform.system() == "Darwin":
|
if platform.system() == "Darwin":
|
||||||
|
|
Loading…
Reference in a new issue