-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle QEvent::FileOpen events on macOS
- Loading branch information
Showing
5 changed files
with
179 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-FileCopyrightText: 2015-2023 Alexey Rochev | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#include "fileopeneventhandler.h" | ||
|
||
#include <QCoreApplication> | ||
#include <QFileOpenEvent> | ||
|
||
#include "log/log.h" | ||
|
||
namespace tremotesf { | ||
FileOpenEventHandler::FileOpenEventHandler(QObject* parent) : QObject(parent) { | ||
QCoreApplication::instance()->installEventFilter(this); | ||
} | ||
|
||
FileOpenEventHandler::~FileOpenEventHandler() { QCoreApplication::instance()->removeEventFilter(this); } | ||
|
||
bool FileOpenEventHandler::eventFilter(QObject* watched, QEvent* event) { | ||
// In case of multiple files / urls, Qt send separate QFileOpenEvent per file in a loop | ||
// Call processPendingEvents() in the next event loop iteration to process all events at once | ||
if (event->type() == QEvent::FileOpen) { | ||
auto* const e = static_cast<QFileOpenEvent*>(event); | ||
if (!e->file().isEmpty() || e->url().isValid()) { | ||
logInfo("Received QEvent::FileOpen"); | ||
auto pendingEvent = [&] { | ||
if (!e->file().isEmpty()) { | ||
logInfo("file = {}", e->file()); | ||
return PendingEvent{.fileOrUrl = e->file(), .isFile = true}; | ||
} | ||
auto url = e->url().toString(); | ||
logInfo("url = {}", url); | ||
return PendingEvent{.fileOrUrl = std::move(url), .isFile = false}; | ||
}(); | ||
if (mPendingEvents.empty()) { | ||
QMetaObject::invokeMethod(this, &FileOpenEventHandler::processPendingEvents, Qt::QueuedConnection); | ||
} | ||
mPendingEvents.push_back(std::move(pendingEvent)); | ||
} | ||
} | ||
return QObject::eventFilter(watched, event); | ||
} | ||
|
||
void FileOpenEventHandler::processPendingEvents() { | ||
QStringList files{}; | ||
QStringList urls{}; | ||
for (auto& event : mPendingEvents) { | ||
if (event.isFile) { | ||
files.push_back(std::move(event.fileOrUrl)); | ||
} else { | ||
urls.push_back(std::move(event.fileOrUrl)); | ||
} | ||
} | ||
mPendingEvents.clear(); | ||
emit filesOpeningRequested(files, urls); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-FileCopyrightText: 2015-2023 Alexey Rochev | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#ifndef TREMOTESF_FILEOPENEVENTHANDLER_H | ||
#define TREMOTESF_FILEOPENEVENTHANDLER_H | ||
|
||
#include <vector> | ||
#include <QObject> | ||
|
||
class QFileOpenEvent; | ||
|
||
namespace tremotesf { | ||
class FileOpenEventHandler : public QObject { | ||
Q_OBJECT | ||
public: | ||
explicit FileOpenEventHandler(QObject* parent = nullptr); | ||
~FileOpenEventHandler() override; | ||
Q_DISABLE_COPY_MOVE(FileOpenEventHandler) | ||
bool eventFilter(QObject* watched, QEvent* event) override; | ||
|
||
private: | ||
void processPendingEvents(); | ||
|
||
struct PendingEvent { | ||
QString fileOrUrl{}; | ||
bool isFile{}; | ||
}; | ||
std::vector<PendingEvent> mPendingEvents{}; | ||
|
||
signals: | ||
void filesOpeningRequested(const QStringList& files, const QStringList& urls); | ||
}; | ||
} | ||
|
||
#endif // TREMOTESF_FILEOPENEVENTHANDLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters