mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Merge pull request #23 from firstlookmedia/9_macos_open_with
Handle macOS file open events
This commit is contained in:
commit
db4a63e95b
1 changed files with 28 additions and 4 deletions
|
@ -18,6 +18,21 @@ from .docker_installer import (
|
||||||
dangerzone_version = "0.1.0"
|
dangerzone_version = "0.1.0"
|
||||||
|
|
||||||
|
|
||||||
|
class Application(QtWidgets.QApplication):
|
||||||
|
document_selected = QtCore.pyqtSignal(str)
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
QtWidgets.QApplication.__init__(self, sys.argv)
|
||||||
|
|
||||||
|
def event(self, event):
|
||||||
|
# In macOS, handle the file open event
|
||||||
|
if event.type() == QtCore.QEvent.FileOpen:
|
||||||
|
self.document_selected.emit(event.file())
|
||||||
|
return True
|
||||||
|
|
||||||
|
return QtWidgets.QApplication.event(self, event)
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.argument("filename", required=False)
|
@click.argument("filename", required=False)
|
||||||
def main(filename):
|
def main(filename):
|
||||||
|
@ -27,7 +42,7 @@ def main(filename):
|
||||||
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||||
|
|
||||||
# Create the Qt app
|
# Create the Qt app
|
||||||
app = QtWidgets.QApplication(sys.argv)
|
app = Application()
|
||||||
app.setQuitOnLastWindowClosed(False)
|
app.setQuitOnLastWindowClosed(False)
|
||||||
|
|
||||||
# Common object
|
# Common object
|
||||||
|
@ -81,18 +96,27 @@ def main(filename):
|
||||||
# Main window
|
# Main window
|
||||||
main_window = MainWindow(common)
|
main_window = MainWindow(common)
|
||||||
|
|
||||||
if filename is not None:
|
def select_document(filename):
|
||||||
# Validate filename
|
# Validate filename
|
||||||
filename = os.path.abspath(os.path.expanduser(filename))
|
filename = os.path.abspath(os.path.expanduser(filename))
|
||||||
try:
|
try:
|
||||||
open(filename, "rb")
|
open(filename, "rb")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("File not found")
|
print("File not found")
|
||||||
return
|
return False
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
print("Permission denied")
|
print("Permission denied")
|
||||||
return
|
return False
|
||||||
common.set_document_filename(filename)
|
common.set_document_filename(filename)
|
||||||
main_window.doc_selection_widget.document_selected.emit()
|
main_window.doc_selection_widget.document_selected.emit()
|
||||||
|
return True
|
||||||
|
|
||||||
|
# If filename is passed as an argument, open it
|
||||||
|
if filename is not None:
|
||||||
|
if not select_document(filename):
|
||||||
|
return False
|
||||||
|
|
||||||
|
# If we get a file open event, open it
|
||||||
|
app.document_selected.connect(select_document)
|
||||||
|
|
||||||
sys.exit(app.exec_())
|
sys.exit(app.exec_())
|
||||||
|
|
Loading…
Reference in a new issue