macOS: handle file open event requested by Finder

This commit is contained in:
Le Tan 2021-08-15 10:10:05 +08:00
parent a9005bef23
commit 280144f4eb
4 changed files with 54 additions and 2 deletions

23
src/application.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "application.h"
#include <QFileOpenEvent>
#include <QDebug>
using namespace vnotex;
Application::Application(int &p_argc, char **p_argv)
: QApplication(p_argc, p_argv)
{
}
bool Application::event(QEvent *p_event)
{
// On macOS, we need this to open file from Finder.
if (p_event->type() == QEvent::FileOpen) {
QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(p_event);
qDebug() << "request to open file" << openEvent->file();
emit openFileRequested(openEvent->file());
}
return QApplication::event(p_event);
}

22
src/application.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef APPLICATION_H
#define APPLICATION_H
#include <QApplication>
namespace vnotex
{
class Application : public QApplication
{
Q_OBJECT
public:
Application(int &p_argc, char **p_argv);
signals:
void openFileRequested(const QString &p_filePath);
protected:
bool event(QEvent *p_event) Q_DECL_OVERRIDE;
};
}
#endif // APPLICATION_H

View File

@ -1,4 +1,3 @@
#include <QApplication>
#include <QDebug>
#include <QTextCodec>
#include <QSslSocket>
@ -22,6 +21,7 @@
#include <core/exception.h>
#include <widgets/messageboxhelper.h>
#include "commandlineoptions.h"
#include "application.h"
using namespace vnotex;
@ -68,7 +68,7 @@ int main(int argc, char *argv[])
}
#endif
QApplication app(argc, argv);
Application app(argc, argv);
initWebEngineSettings();
@ -162,6 +162,11 @@ int main(int argc, char *argv[])
QObject::connect(&guard, &SingleInstanceGuard::openFilesRequested,
&window, &MainWindow::openFiles);
QObject::connect(&app, &Application::openFileRequested,
&window, [&window](const QString &p_filePath) {
window.openFiles(QStringList() << p_filePath);
});
window.kickOffOnStart(cmdOptions.m_pathsToOpen);
int ret = app.exec();

View File

@ -32,6 +32,7 @@ TRANSLATIONS += \
data/core/translations/vnote_ja.ts
SOURCES += \
application.cpp \
commandlineoptions.cpp \
main.cpp
@ -148,4 +149,5 @@ unix:!macx {
}
HEADERS += \
application.h \
commandlineoptions.h