diff --git a/dangerzone/__init__.py b/dangerzone/__init__.py index 3fa4774..fd7fcdc 100644 --- a/dangerzone/__init__.py +++ b/dangerzone/__init__.py @@ -5,6 +5,7 @@ import signal import platform import click import time +import uuid from .global_common import GlobalCommon from .main_window import MainWindow @@ -20,6 +21,7 @@ dangerzone_version = "0.0.3" class Application(QtWidgets.QApplication): document_selected = QtCore.pyqtSignal(str) + application_activated = QtCore.pyqtSignal() def __init__(self): QtWidgets.QApplication.__init__(self, sys.argv) @@ -29,6 +31,9 @@ class Application(QtWidgets.QApplication): if event.type() == QtCore.QEvent.FileOpen: self.document_selected.emit(event.file()) return True + elif event.type() == QtCore.QEvent.ApplicationActivate: + self.application_activated.emit() + return True return QtWidgets.QApplication.event(self, event) @@ -66,15 +71,25 @@ def main(filename): docker_installer.start() return - windows = [] + closed_windows = {} + windows = {} + + def delete_window(window_id): + closed_windows[window_id] = windows[window_id] + del windows[window_id] # Open a document in a window def select_document(filename=None): - if len(windows) == 1 and windows[0].common.document_filename == None: - window = windows[0] + if ( + len(windows) == 1 + and windows[list(windows.keys())[0]].common.document_filename == None + ): + window = windows[list(windows.keys())[0]] else: - window = MainWindow(global_common) - windows.append(window) + window_id = uuid.uuid4().hex + window = MainWindow(global_common, window_id) + window.delete_window.connect(delete_window) + windows[window_id] = window if filename: # Validate filename @@ -100,7 +115,15 @@ def main(filename): if not select_document(filename): return True + # Open a new window, if all windows are closed + def application_activated(): + if len(windows) == 0: + select_document() + # If we get a file open event, open it app.document_selected.connect(select_document) + # If the application is activated and all windows are closed, open a new one + app.application_activated.connect(application_activated) + sys.exit(app.exec_()) diff --git a/dangerzone/main_window.py b/dangerzone/main_window.py index 5becd69..dff53d0 100644 --- a/dangerzone/main_window.py +++ b/dangerzone/main_window.py @@ -10,9 +10,12 @@ from .common import Common class MainWindow(QtWidgets.QMainWindow): - def __init__(self, global_common): + delete_window = QtCore.pyqtSignal(str) + + def __init__(self, global_common, window_id): super(MainWindow, self).__init__() self.global_common = global_common + self.window_id = window_id self.common = Common() self.setWindowTitle("dangerzone") @@ -83,5 +86,7 @@ class MainWindow(QtWidgets.QMainWindow): def closeEvent(self, e): e.accept() + self.delete_window.emit(self.window_id) + if platform.system() != "Darwin": self.global_common.app.quit()