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 json
|
||||
import click
|
||||
import colorama
|
||||
from colorama import Fore, Style # type: ignore
|
||||
|
||||
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.argument("filename", required=True)
|
||||
def cli_main(output_filename, ocr_lang, filename):
|
||||
global_common = GlobalCommon()
|
||||
colorama.init(autoreset=True)
|
||||
common = Common()
|
||||
|
||||
GlobalCommon.display_banner()
|
||||
|
||||
# Validate filename
|
||||
|
@ -90,7 +90,7 @@ def cli_main(output_filename, ocr_lang, filename):
|
|||
# Convert the document
|
||||
print_header("Converting document to safe PDF")
|
||||
|
||||
def stdout_callback(line):
|
||||
def stdout_callback(line: str) -> None:
|
||||
try:
|
||||
status = json.loads(line)
|
||||
s = Style.BRIGHT + Fore.CYAN + f"{status['percentage']}% "
|
||||
|
|
|
@ -1,25 +1,16 @@
|
|||
import subprocess
|
||||
import gzip
|
||||
import colorama
|
||||
from colorama import Fore, Back, Style
|
||||
|
||||
import dangerzone
|
||||
from dangerzone.settings import Settings
|
||||
import dangerzone.util as dzutil
|
||||
|
||||
|
||||
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
|
||||
def display_banner():
|
||||
"""
|
||||
|
|
|
@ -7,11 +7,12 @@ from typing import Optional
|
|||
import click
|
||||
import uuid
|
||||
|
||||
import colorama
|
||||
|
||||
from .application import Application
|
||||
from .common import GuiCommon
|
||||
from .main_window import MainWindow
|
||||
from .systray import SysTray
|
||||
from ..global_common import GlobalCommon
|
||||
|
||||
|
||||
@click.command()
|
||||
|
@ -44,9 +45,11 @@ def gui_main(filename):
|
|||
# Create the Qt app
|
||||
app = Application()
|
||||
|
||||
# Initialize colorama
|
||||
colorama.init(autoreset=True)
|
||||
|
||||
# Common objects
|
||||
global_common = GlobalCommon()
|
||||
gui_common = GuiCommon(app, global_common)
|
||||
gui_common = GuiCommon(app)
|
||||
|
||||
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
|
||||
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||
|
@ -71,7 +74,7 @@ def gui_main(filename):
|
|||
window: MainWindow = windows[list(windows.keys())[0]]
|
||||
else:
|
||||
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)
|
||||
windows[window_id] = window
|
||||
|
||||
|
|
|
@ -3,13 +3,11 @@ import platform
|
|||
import subprocess
|
||||
import shlex
|
||||
import pipes
|
||||
from PySide6 import QtCore, QtGui, QtWidgets
|
||||
from PySide6.QtGui import QIcon
|
||||
from PySide6 import QtGui
|
||||
from colorama import Fore
|
||||
|
||||
from . import Application
|
||||
from ..global_common import GlobalCommon
|
||||
import dangerzone.util as dzutil
|
||||
from .settings import Settings
|
||||
|
||||
if platform.system() == "Linux":
|
||||
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
|
||||
"""
|
||||
|
||||
def __init__(self, app: Application, global_common: GlobalCommon):
|
||||
def __init__(self, app: Application):
|
||||
# Qt app
|
||||
self.app = app
|
||||
|
||||
# Global common singleton
|
||||
self.global_common = global_common
|
||||
|
||||
# Preload font
|
||||
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)
|
||||
self.is_waiting_finished = False
|
||||
|
||||
self.settings = Settings()
|
||||
|
||||
def open_pdf_viewer(self, filename: str):
|
||||
if platform.system() == "Darwin":
|
||||
# Open in Preview
|
||||
|
@ -49,7 +46,7 @@ class GuiCommon(object):
|
|||
elif platform.system() == "Linux":
|
||||
# Get the PDF reader command
|
||||
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
|
||||
for i in range(len(args)):
|
||||
|
|
|
@ -18,9 +18,8 @@ from ..global_common import GlobalCommon
|
|||
class MainWindow(QtWidgets.QMainWindow):
|
||||
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__()
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
self.window_id = window_id
|
||||
self.common = Common()
|
||||
|
@ -49,12 +48,12 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||
header_layout.addStretch()
|
||||
|
||||
# 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)
|
||||
|
||||
# Content widget, contains all the window content except waiting widget
|
||||
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)
|
||||
|
||||
|
@ -94,12 +93,11 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||
class InstallContainerThread(QtCore.QThread):
|
||||
finished = QtCore.Signal()
|
||||
|
||||
def __init__(self, global_common: GlobalCommon):
|
||||
def __init__(self):
|
||||
super(InstallContainerThread, self).__init__()
|
||||
self.global_common = global_common
|
||||
|
||||
def run(self):
|
||||
self.global_common.install_container()
|
||||
GlobalCommon.install_container()
|
||||
self.finished.emit()
|
||||
|
||||
|
||||
|
@ -115,9 +113,8 @@ class WaitingWidget(QtWidgets.QWidget):
|
|||
# - "install_container"
|
||||
finished = QtCore.Signal()
|
||||
|
||||
def __init__(self, global_common: GlobalCommon, gui_common: GuiCommon):
|
||||
def __init__(self, gui_common: GuiCommon):
|
||||
super(WaitingWidget, self).__init__()
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
|
||||
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..."
|
||||
)
|
||||
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.start()
|
||||
|
||||
|
@ -204,10 +201,8 @@ class WaitingWidget(QtWidgets.QWidget):
|
|||
class ContentWidget(QtWidgets.QWidget):
|
||||
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__()
|
||||
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
self.common = common
|
||||
|
||||
|
@ -217,7 +212,7 @@ class ContentWidget(QtWidgets.QWidget):
|
|||
|
||||
# Settings
|
||||
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.settings_widget.document_selected
|
||||
|
@ -228,7 +223,7 @@ class ContentWidget(QtWidgets.QWidget):
|
|||
|
||||
# Convert
|
||||
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.doc_selection_widget.document_selected.connect(
|
||||
|
@ -302,9 +297,8 @@ class SettingsWidget(QtWidgets.QWidget):
|
|||
start_clicked = 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__()
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
self.common = common
|
||||
|
||||
|
@ -395,31 +389,31 @@ class SettingsWidget(QtWidgets.QWidget):
|
|||
self.setLayout(layout)
|
||||
|
||||
# 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)
|
||||
else:
|
||||
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)
|
||||
else:
|
||||
self.ocr_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
||||
|
||||
index = self.ocr_combobox.findText(
|
||||
self.global_common.settings.get("ocr_language")
|
||||
self.gui_common.settings.get("ocr_language")
|
||||
)
|
||||
if index != -1:
|
||||
self.ocr_combobox.setCurrentIndex(index)
|
||||
|
||||
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)
|
||||
else:
|
||||
self.open_checkbox.setCheckState(QtCore.Qt.Unchecked)
|
||||
|
||||
if platform.system() == "Linux":
|
||||
index = self.open_combobox.findText(
|
||||
self.global_common.settings.get("open_app")
|
||||
self.gui_common.settings.get("open_app")
|
||||
)
|
||||
if index != -1:
|
||||
self.open_combobox.setCurrentIndex(index)
|
||||
|
@ -468,22 +462,22 @@ class SettingsWidget(QtWidgets.QWidget):
|
|||
self.common.output_filename = tmp[1]
|
||||
|
||||
# Update settings
|
||||
self.global_common.settings.set(
|
||||
self.gui_common.settings.set(
|
||||
"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
|
||||
)
|
||||
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":
|
||||
self.global_common.settings.set(
|
||||
self.gui_common.settings.set(
|
||||
"open", self.open_checkbox.checkState() == QtCore.Qt.Checked
|
||||
)
|
||||
if platform.system() == "Linux":
|
||||
self.global_common.settings.set(
|
||||
self.gui_common.settings.set(
|
||||
"open_app", self.open_combobox.currentText()
|
||||
)
|
||||
self.global_common.settings.save()
|
||||
self.gui_common.settings.save()
|
||||
|
||||
# Start!
|
||||
self.start_clicked.emit()
|
||||
|
@ -493,16 +487,16 @@ class ConvertThread(QtCore.QThread):
|
|||
is_finished = QtCore.Signal(bool)
|
||||
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__()
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
self.common = common
|
||||
self.error = False
|
||||
|
||||
def run(self):
|
||||
if self.global_common.settings.get("ocr"):
|
||||
if self.gui_common.settings.get("ocr"):
|
||||
ocr_lang = dzutil.OCR_LANGUAGES[
|
||||
self.global_common.settings.get("ocr_language")
|
||||
self.gui_common.settings.get("ocr_language")
|
||||
]
|
||||
else:
|
||||
ocr_lang = None
|
||||
|
@ -540,9 +534,8 @@ class ConvertThread(QtCore.QThread):
|
|||
class ConvertWidget(QtWidgets.QWidget):
|
||||
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__()
|
||||
self.global_common = global_common
|
||||
self.gui_common = gui_common
|
||||
self.common = common
|
||||
|
||||
|
@ -594,7 +587,7 @@ class ConvertWidget(QtWidgets.QWidget):
|
|||
)
|
||||
|
||||
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.finished.connect(self.all_done)
|
||||
self.convert_t.start()
|
||||
|
@ -622,7 +615,7 @@ class ConvertWidget(QtWidgets.QWidget):
|
|||
)
|
||||
|
||||
# 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)
|
||||
|
||||
# Quit
|
||||
|
|
Loading…
Reference in a new issue