mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-05-04 12:41:50 +02:00
Remove all instantiations of GlobalCommon, move Settings to GuiCommon
This commit is contained in:
parent
2f48e8aad9
commit
519b8fd1b8
6 changed files with 46 additions and 62 deletions
|
@ -2,6 +2,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import click
|
import click
|
||||||
|
import colorama
|
||||||
from colorama import Fore, Style # type: ignore
|
from colorama import Fore, Style # type: ignore
|
||||||
|
|
||||||
from .global_common import GlobalCommon
|
from .global_common import GlobalCommon
|
||||||
|
@ -20,9 +21,8 @@ def print_header(s):
|
||||||
@click.option("--ocr-lang", help="Language to OCR, defaults to none")
|
@click.option("--ocr-lang", help="Language to OCR, defaults to none")
|
||||||
@click.argument("filename", required=True)
|
@click.argument("filename", required=True)
|
||||||
def cli_main(output_filename, ocr_lang, filename):
|
def cli_main(output_filename, ocr_lang, filename):
|
||||||
global_common = GlobalCommon()
|
colorama.init(autoreset=True)
|
||||||
common = Common()
|
common = Common()
|
||||||
|
|
||||||
GlobalCommon.display_banner()
|
GlobalCommon.display_banner()
|
||||||
|
|
||||||
# Validate filename
|
# Validate filename
|
||||||
|
@ -90,7 +90,7 @@ def cli_main(output_filename, ocr_lang, filename):
|
||||||
# Convert the document
|
# Convert the document
|
||||||
print_header("Converting document to safe PDF")
|
print_header("Converting document to safe PDF")
|
||||||
|
|
||||||
def stdout_callback(line):
|
def stdout_callback(line: str) -> None:
|
||||||
try:
|
try:
|
||||||
status = json.loads(line)
|
status = json.loads(line)
|
||||||
s = Style.BRIGHT + Fore.CYAN + f"{status['percentage']}% "
|
s = Style.BRIGHT + Fore.CYAN + f"{status['percentage']}% "
|
||||||
|
|
|
@ -1,25 +1,16 @@
|
||||||
import subprocess
|
import subprocess
|
||||||
import gzip
|
import gzip
|
||||||
import colorama
|
|
||||||
from colorama import Fore, Back, Style
|
from colorama import Fore, Back, Style
|
||||||
|
|
||||||
import dangerzone
|
import dangerzone
|
||||||
from dangerzone.settings import Settings
|
|
||||||
import dangerzone.util as dzutil
|
import dangerzone.util as dzutil
|
||||||
|
|
||||||
|
|
||||||
class GlobalCommon(object):
|
class GlobalCommon(object):
|
||||||
"""
|
"""
|
||||||
The GlobalCommon class is a singleton of shared functionality throughout the app
|
The GlobalCommon class contains functionality shared throughout the app
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
# Initialize terminal colors
|
|
||||||
colorama.init(autoreset=True)
|
|
||||||
|
|
||||||
# Load settings
|
|
||||||
self.settings = Settings()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def display_banner():
|
def display_banner():
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -7,11 +7,12 @@ from typing import Optional
|
||||||
import click
|
import click
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import colorama
|
||||||
|
|
||||||
from .application import Application
|
from .application import Application
|
||||||
from .common import GuiCommon
|
from .common import GuiCommon
|
||||||
from .main_window import MainWindow
|
from .main_window import MainWindow
|
||||||
from .systray import SysTray
|
from .systray import SysTray
|
||||||
from ..global_common import GlobalCommon
|
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
|
@ -44,9 +45,11 @@ def gui_main(filename):
|
||||||
# Create the Qt app
|
# Create the Qt app
|
||||||
app = Application()
|
app = Application()
|
||||||
|
|
||||||
|
# Initialize colorama
|
||||||
|
colorama.init(autoreset=True)
|
||||||
|
|
||||||
# Common objects
|
# Common objects
|
||||||
global_common = GlobalCommon()
|
gui_common = GuiCommon(app)
|
||||||
gui_common = GuiCommon(app, global_common)
|
|
||||||
|
|
||||||
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
|
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
|
||||||
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||||
|
@ -71,7 +74,7 @@ def gui_main(filename):
|
||||||
window: MainWindow = windows[list(windows.keys())[0]]
|
window: MainWindow = windows[list(windows.keys())[0]]
|
||||||
else:
|
else:
|
||||||
window_id = uuid.uuid4().hex
|
window_id = uuid.uuid4().hex
|
||||||
window = MainWindow(global_common, gui_common, window_id)
|
window = MainWindow(gui_common, window_id)
|
||||||
window.delete_window.connect(delete_window)
|
window.delete_window.connect(delete_window)
|
||||||
windows[window_id] = window
|
windows[window_id] = window
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,11 @@ import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
import shlex
|
import shlex
|
||||||
import pipes
|
import pipes
|
||||||
from PySide6 import QtCore, QtGui, QtWidgets
|
from PySide6 import QtGui
|
||||||
from PySide6.QtGui import QIcon
|
|
||||||
from colorama import Fore
|
from colorama import Fore
|
||||||
|
|
||||||
from . import Application
|
from . import Application
|
||||||
from ..global_common import GlobalCommon
|
from .settings import Settings
|
||||||
import dangerzone.util as dzutil
|
|
||||||
|
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux":
|
||||||
from xdg.DesktopEntry import DesktopEntry # type: ignore
|
from xdg.DesktopEntry import DesktopEntry # type: ignore
|
||||||
|
@ -20,13 +18,10 @@ class GuiCommon(object):
|
||||||
The GuiCommon class is a singleton of shared functionality for the GUI
|
The GuiCommon class is a singleton of shared functionality for the GUI
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, app: Application, global_common: GlobalCommon):
|
def __init__(self, app: Application):
|
||||||
# Qt app
|
# Qt app
|
||||||
self.app = app
|
self.app = app
|
||||||
|
|
||||||
# Global common singleton
|
|
||||||
self.global_common = global_common
|
|
||||||
|
|
||||||
# Preload font
|
# Preload font
|
||||||
self.fixed_font = QtGui.QFontDatabase.systemFont(QtGui.QFontDatabase.FixedFont)
|
self.fixed_font = QtGui.QFontDatabase.systemFont(QtGui.QFontDatabase.FixedFont)
|
||||||
|
|
||||||
|
@ -36,6 +31,8 @@ class GuiCommon(object):
|
||||||
# Are we done waiting (for Docker Desktop to be installed, or for container to install)
|
# Are we done waiting (for Docker Desktop to be installed, or for container to install)
|
||||||
self.is_waiting_finished = False
|
self.is_waiting_finished = False
|
||||||
|
|
||||||
|
self.settings = Settings()
|
||||||
|
|
||||||
def open_pdf_viewer(self, filename: str):
|
def open_pdf_viewer(self, filename: str):
|
||||||
if platform.system() == "Darwin":
|
if platform.system() == "Darwin":
|
||||||
# Open in Preview
|
# Open in Preview
|
||||||
|
@ -49,7 +46,7 @@ class GuiCommon(object):
|
||||||
elif platform.system() == "Linux":
|
elif platform.system() == "Linux":
|
||||||
# Get the PDF reader command
|
# Get the PDF reader command
|
||||||
args = shlex.split(
|
args = shlex.split(
|
||||||
self.pdf_viewers[self.global_common.settings.get("open_app")]
|
self.pdf_viewers[self.settings.get("open_app")]
|
||||||
)
|
)
|
||||||
# %f, %F, %u, and %U are filenames or URLS -- so replace with the file to open
|
# %f, %F, %u, and %U are filenames or URLS -- so replace with the file to open
|
||||||
for i in range(len(args)):
|
for i in range(len(args)):
|
||||||
|
|
|
@ -18,9 +18,8 @@ from ..global_common import GlobalCommon
|
||||||
class MainWindow(QtWidgets.QMainWindow):
|
class MainWindow(QtWidgets.QMainWindow):
|
||||||
delete_window = QtCore.Signal(str)
|
delete_window = QtCore.Signal(str)
|
||||||
|
|
||||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon, window_id: str):
|
def __init__(self, gui_common: GuiCommon, window_id: str):
|
||||||
super(MainWindow, self).__init__()
|
super(MainWindow, self).__init__()
|
||||||
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.common = Common()
|
||||||
|
@ -49,12 +48,12 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||||
header_layout.addStretch()
|
header_layout.addStretch()
|
||||||
|
|
||||||
# Waiting widget, replaces content widget while container runtime isn't available
|
# Waiting widget, replaces content widget while container runtime isn't available
|
||||||
self.waiting_widget = WaitingWidget(self.global_common, self.gui_common)
|
self.waiting_widget = WaitingWidget(self.gui_common)
|
||||||
self.waiting_widget.finished.connect(self.waiting_finished)
|
self.waiting_widget.finished.connect(self.waiting_finished)
|
||||||
|
|
||||||
# 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.gui_common, self.common
|
||||||
)
|
)
|
||||||
self.content_widget.close_window.connect(self.close)
|
self.content_widget.close_window.connect(self.close)
|
||||||
|
|
||||||
|
@ -94,12 +93,11 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||||
class InstallContainerThread(QtCore.QThread):
|
class InstallContainerThread(QtCore.QThread):
|
||||||
finished = QtCore.Signal()
|
finished = QtCore.Signal()
|
||||||
|
|
||||||
def __init__(self, global_common: GlobalCommon):
|
def __init__(self):
|
||||||
super(InstallContainerThread, self).__init__()
|
super(InstallContainerThread, self).__init__()
|
||||||
self.global_common = global_common
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.global_common.install_container()
|
GlobalCommon.install_container()
|
||||||
self.finished.emit()
|
self.finished.emit()
|
||||||
|
|
||||||
|
|
||||||
|
@ -115,9 +113,8 @@ class WaitingWidget(QtWidgets.QWidget):
|
||||||
# - "install_container"
|
# - "install_container"
|
||||||
finished = QtCore.Signal()
|
finished = QtCore.Signal()
|
||||||
|
|
||||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon):
|
def __init__(self, gui_common: GuiCommon):
|
||||||
super(WaitingWidget, self).__init__()
|
super(WaitingWidget, self).__init__()
|
||||||
self.global_common = global_common
|
|
||||||
self.gui_common = gui_common
|
self.gui_common = gui_common
|
||||||
|
|
||||||
self.label = QtWidgets.QLabel()
|
self.label = QtWidgets.QLabel()
|
||||||
|
@ -196,7 +193,7 @@ class WaitingWidget(QtWidgets.QWidget):
|
||||||
"Installing the Dangerzone container image.<br><br>This might take a few minutes..."
|
"Installing the Dangerzone container image.<br><br>This might take a few minutes..."
|
||||||
)
|
)
|
||||||
self.buttons.hide()
|
self.buttons.hide()
|
||||||
self.install_container_t = InstallContainerThread(self.global_common)
|
self.install_container_t = InstallContainerThread()
|
||||||
self.install_container_t.finished.connect(self.finished)
|
self.install_container_t.finished.connect(self.finished)
|
||||||
self.install_container_t.start()
|
self.install_container_t.start()
|
||||||
|
|
||||||
|
@ -204,10 +201,8 @@ class WaitingWidget(QtWidgets.QWidget):
|
||||||
class ContentWidget(QtWidgets.QWidget):
|
class ContentWidget(QtWidgets.QWidget):
|
||||||
close_window = QtCore.Signal()
|
close_window = QtCore.Signal()
|
||||||
|
|
||||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon, common: Common):
|
def __init__(self, gui_common: GuiCommon, common: Common):
|
||||||
super(ContentWidget, self).__init__()
|
super(ContentWidget, self).__init__()
|
||||||
|
|
||||||
self.global_common = global_common
|
|
||||||
self.gui_common = gui_common
|
self.gui_common = gui_common
|
||||||
self.common = common
|
self.common = common
|
||||||
|
|
||||||
|
@ -217,7 +212,7 @@ class ContentWidget(QtWidgets.QWidget):
|
||||||
|
|
||||||
# Settings
|
# Settings
|
||||||
self.settings_widget = SettingsWidget(
|
self.settings_widget = SettingsWidget(
|
||||||
self.global_common, self.gui_common, self.common
|
self.gui_common, self.common
|
||||||
)
|
)
|
||||||
self.doc_selection_widget.document_selected.connect(
|
self.doc_selection_widget.document_selected.connect(
|
||||||
self.settings_widget.document_selected
|
self.settings_widget.document_selected
|
||||||
|
@ -228,7 +223,7 @@ class ContentWidget(QtWidgets.QWidget):
|
||||||
|
|
||||||
# Convert
|
# Convert
|
||||||
self.convert_widget = ConvertWidget(
|
self.convert_widget = ConvertWidget(
|
||||||
self.global_common, self.gui_common, self.common
|
self.gui_common, self.common
|
||||||
)
|
)
|
||||||
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(
|
||||||
|
@ -302,9 +297,8 @@ class SettingsWidget(QtWidgets.QWidget):
|
||||||
start_clicked = QtCore.Signal()
|
start_clicked = QtCore.Signal()
|
||||||
close_window = QtCore.Signal()
|
close_window = QtCore.Signal()
|
||||||
|
|
||||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon, common: Common):
|
def __init__(self, gui_common: GuiCommon, common: Common):
|
||||||
super(SettingsWidget, self).__init__()
|
super(SettingsWidget, self).__init__()
|
||||||
self.global_common = global_common
|
|
||||||
self.gui_common = gui_common
|
self.gui_common = gui_common
|
||||||
self.common = common
|
self.common = common
|
||||||
|
|
||||||
|
@ -395,31 +389,31 @@ class SettingsWidget(QtWidgets.QWidget):
|
||||||
self.setLayout(layout)
|
self.setLayout(layout)
|
||||||
|
|
||||||
# Load values from settings
|
# Load values from settings
|
||||||
if self.global_common.settings.get("save"):
|
if self.gui_common.settings.get("save"):
|
||||||
self.save_checkbox.setCheckState(QtCore.Qt.Checked)
|
self.save_checkbox.setCheckState(QtCore.Qt.Checked)
|
||||||
else:
|
else:
|
||||||
self.save_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
self.save_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
||||||
|
|
||||||
if self.global_common.settings.get("ocr"):
|
if self.gui_common.settings.get("ocr"):
|
||||||
self.ocr_checkbox.setCheckState(QtCore.Qt.Checked)
|
self.ocr_checkbox.setCheckState(QtCore.Qt.Checked)
|
||||||
else:
|
else:
|
||||||
self.ocr_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
self.ocr_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
||||||
|
|
||||||
index = self.ocr_combobox.findText(
|
index = self.ocr_combobox.findText(
|
||||||
self.global_common.settings.get("ocr_language")
|
self.gui_common.settings.get("ocr_language")
|
||||||
)
|
)
|
||||||
if index != -1:
|
if index != -1:
|
||||||
self.ocr_combobox.setCurrentIndex(index)
|
self.ocr_combobox.setCurrentIndex(index)
|
||||||
|
|
||||||
if platform.system() == "Darwin" or platform.system() == "Linux":
|
if platform.system() == "Darwin" or platform.system() == "Linux":
|
||||||
if self.global_common.settings.get("open"):
|
if self.gui_common.settings.get("open"):
|
||||||
self.open_checkbox.setCheckState(QtCore.Qt.Checked)
|
self.open_checkbox.setCheckState(QtCore.Qt.Checked)
|
||||||
else:
|
else:
|
||||||
self.open_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
self.open_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
||||||
|
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux":
|
||||||
index = self.open_combobox.findText(
|
index = self.open_combobox.findText(
|
||||||
self.global_common.settings.get("open_app")
|
self.gui_common.settings.get("open_app")
|
||||||
)
|
)
|
||||||
if index != -1:
|
if index != -1:
|
||||||
self.open_combobox.setCurrentIndex(index)
|
self.open_combobox.setCurrentIndex(index)
|
||||||
|
@ -468,22 +462,22 @@ class SettingsWidget(QtWidgets.QWidget):
|
||||||
self.common.output_filename = tmp[1]
|
self.common.output_filename = tmp[1]
|
||||||
|
|
||||||
# Update settings
|
# Update settings
|
||||||
self.global_common.settings.set(
|
self.gui_common.settings.set(
|
||||||
"save", self.save_checkbox.checkState() == QtCore.Qt.Checked
|
"save", self.save_checkbox.checkState() == QtCore.Qt.Checked
|
||||||
)
|
)
|
||||||
self.global_common.settings.set(
|
self.gui_common.settings.set(
|
||||||
"ocr", self.ocr_checkbox.checkState() == QtCore.Qt.Checked
|
"ocr", self.ocr_checkbox.checkState() == QtCore.Qt.Checked
|
||||||
)
|
)
|
||||||
self.global_common.settings.set("ocr_language", self.ocr_combobox.currentText())
|
self.gui_common.settings.set("ocr_language", self.ocr_combobox.currentText())
|
||||||
if platform.system() == "Darwin" or platform.system() == "Linux":
|
if platform.system() == "Darwin" or platform.system() == "Linux":
|
||||||
self.global_common.settings.set(
|
self.gui_common.settings.set(
|
||||||
"open", self.open_checkbox.checkState() == QtCore.Qt.Checked
|
"open", self.open_checkbox.checkState() == QtCore.Qt.Checked
|
||||||
)
|
)
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux":
|
||||||
self.global_common.settings.set(
|
self.gui_common.settings.set(
|
||||||
"open_app", self.open_combobox.currentText()
|
"open_app", self.open_combobox.currentText()
|
||||||
)
|
)
|
||||||
self.global_common.settings.save()
|
self.gui_common.settings.save()
|
||||||
|
|
||||||
# Start!
|
# Start!
|
||||||
self.start_clicked.emit()
|
self.start_clicked.emit()
|
||||||
|
@ -493,16 +487,16 @@ class ConvertThread(QtCore.QThread):
|
||||||
is_finished = QtCore.Signal(bool)
|
is_finished = QtCore.Signal(bool)
|
||||||
update = QtCore.Signal(bool, str, int)
|
update = QtCore.Signal(bool, str, int)
|
||||||
|
|
||||||
def __init__(self, global_common: GlobalCommon, common: Common):
|
def __init__(self, gui_common: GuiCommon, common: Common):
|
||||||
super(ConvertThread, self).__init__()
|
super(ConvertThread, self).__init__()
|
||||||
self.global_common = global_common
|
self.gui_common = gui_common
|
||||||
self.common = common
|
self.common = common
|
||||||
self.error = False
|
self.error = False
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.global_common.settings.get("ocr"):
|
if self.gui_common.settings.get("ocr"):
|
||||||
ocr_lang = dzutil.OCR_LANGUAGES[
|
ocr_lang = dzutil.OCR_LANGUAGES[
|
||||||
self.global_common.settings.get("ocr_language")
|
self.gui_common.settings.get("ocr_language")
|
||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
ocr_lang = None
|
ocr_lang = None
|
||||||
|
@ -540,9 +534,8 @@ class ConvertThread(QtCore.QThread):
|
||||||
class ConvertWidget(QtWidgets.QWidget):
|
class ConvertWidget(QtWidgets.QWidget):
|
||||||
close_window = QtCore.Signal()
|
close_window = QtCore.Signal()
|
||||||
|
|
||||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon, common: Common):
|
def __init__(self, gui_common: GuiCommon, common: Common):
|
||||||
super(ConvertWidget, self).__init__()
|
super(ConvertWidget, self).__init__()
|
||||||
self.global_common = global_common
|
|
||||||
self.gui_common = gui_common
|
self.gui_common = gui_common
|
||||||
self.common = common
|
self.common = common
|
||||||
|
|
||||||
|
@ -594,7 +587,7 @@ class ConvertWidget(QtWidgets.QWidget):
|
||||||
)
|
)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.convert_t = ConvertThread(self.global_common, self.common)
|
self.convert_t = ConvertThread(self.gui_common, self.common)
|
||||||
self.convert_t.update.connect(self.update)
|
self.convert_t.update.connect(self.update)
|
||||||
self.convert_t.finished.connect(self.all_done)
|
self.convert_t.finished.connect(self.all_done)
|
||||||
self.convert_t.start()
|
self.convert_t.start()
|
||||||
|
@ -622,7 +615,7 @@ class ConvertWidget(QtWidgets.QWidget):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Open
|
# Open
|
||||||
if self.global_common.settings.get("open"):
|
if self.gui_common.settings.get("open"):
|
||||||
self.gui_common.open_pdf_viewer(self.common.output_filename)
|
self.gui_common.open_pdf_viewer(self.common.output_filename)
|
||||||
|
|
||||||
# Quit
|
# Quit
|
||||||
|
|
Loading…
Reference in a new issue