From 54be8b5b4f0035df6e681fbd2a33059bff877349 Mon Sep 17 00:00:00 2001 From: Marko Bencun Date: Tue, 4 Mar 2025 10:10:42 +0100 Subject: [PATCH] qt: fix uri scheme handling on macOS In Qt 6.8, the way custom uri schemes are handled on macOS changed. The commit that changed it is https://github.com/qt/qtbase/commit/664c7ffb212eb898ed03f7b19c883400fa027b6b. Before, the URL would be emitted as a FileOpen event with a QFileOpenEvent object. After this change, it is dispatched to QDesktopServices. Before, QDesktopServices would only be used to register custom schemes while the app was running, but now it also is used when the app is launched with by a URI click (provided the Info.plist entry for it is there of course). --- frontends/qt/BitBox.pro | 5 +++-- frontends/qt/main.cpp | 27 ++++++++------------------- frontends/qt/urlhandler.cpp | 34 ++++++++++++++++++++++++++++++++++ frontends/qt/urlhandler.h | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 frontends/qt/urlhandler.cpp create mode 100644 frontends/qt/urlhandler.h diff --git a/frontends/qt/BitBox.pro b/frontends/qt/BitBox.pro index 854ead8d21..3aec005331 100644 --- a/frontends/qt/BitBox.pro +++ b/frontends/qt/BitBox.pro @@ -59,9 +59,10 @@ unix:!macx { SOURCES += \ main.cpp \ - filedialog.cpp + filedialog.cpp \ + urlhandler.cpp -HEADERS += libserver.h webclass.h filedialog.h +HEADERS += libserver.h webclass.h filedialog.h urlhandler.h unix:macx { CONFIG += sdk_no_version_check diff --git a/frontends/qt/main.cpp b/frontends/qt/main.cpp index 33a01c2cee..36bf0e0eaa 100644 --- a/frontends/qt/main.cpp +++ b/frontends/qt/main.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #if defined(_WIN32) #if QT_VERSION_MAJOR >= 6 #include @@ -55,6 +56,7 @@ #include "filedialog.h" #include "libserver.h" #include "webclass.h" +#include "urlhandler.h" #define APPNAME "BitBoxApp" @@ -85,25 +87,6 @@ class BitBoxApp : public SingleApplication Mode::User | Mode::SecondaryNotification | Mode::ExcludeAppVersion | Mode::ExcludeAppPath) { } - -#if defined(Q_OS_MACOS) - bool event(QEvent *event) override - { - if (event->type() == QEvent::FileOpen) { - QFileOpenEvent* openEvent = static_cast(event); - if (!openEvent->url().isEmpty()) { - // This is only supported on macOS and is used to handle URIs that are opened with - // the BitBoxApp, such as "aopp:..." links. The event is received and handled both - // if the BitBoxApp is launched and also when it is already running, in which case - // it is brought to the foreground automatically. - - handleURI(openEvent->url().toString().toUtf8().constData()); - } - } - - return QApplication::event(event); - } -#endif }; class WebEnginePage : public QWebEnginePage { @@ -318,6 +301,12 @@ int main(int argc, char *argv[]) } BitBoxApp a(argc, argv); + // The URI scheme handler for aopp is handled via OS events on macOS. The other platforms invoke + // the process with the uri as a command line param. +#if defined(Q_OS_MACOS) + UrlHandler url_handler; + url_handler.setup(); +#endif // These three are part of the SingleApplication instance ID - if changed, the user should close // th existing app before launching the new one. // See https://github.com/BitBoxSwiss/SingleApplication/blob/c557da5d0cb63b8002c1ba99ec18f257620009b1/singleapplication_p.cpp#L135-L137 diff --git a/frontends/qt/urlhandler.cpp b/frontends/qt/urlhandler.cpp new file mode 100644 index 0000000000..6cfee9bbaf --- /dev/null +++ b/frontends/qt/urlhandler.cpp @@ -0,0 +1,34 @@ +// Copyright 2025 Shift Crypto AG +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "urlhandler.h" +#include "libserver.h" + +#include +#include + +UrlHandler::UrlHandler(QObject *parent) : QObject(parent) { +} + +void UrlHandler::setup() { + // This is only supported on macOS and is used to handle URIs that are opened with + // the BitBoxApp using "aopp:..." links. The event is received and handled both + // if the BitBoxApp is launched and also when it is already running, in which case + // it is brought to the foreground automatically. + QDesktopServices::setUrlHandler("aopp", this, "handleUrlSlot"); +} + +void UrlHandler::handleUrlSlot(const QUrl &url) { + handleURI(url.toString().toUtf8().constData()); +} diff --git a/frontends/qt/urlhandler.h b/frontends/qt/urlhandler.h new file mode 100644 index 0000000000..f5b10fe4b5 --- /dev/null +++ b/frontends/qt/urlhandler.h @@ -0,0 +1,33 @@ +// Copyright 2025 Shift Crypto AG +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef URLHANDLER_H +#define URLHANDLER_H + +#include +#include + +class UrlHandler : public QObject { + Q_OBJECT + +public: + UrlHandler(QObject *parent = nullptr); + + void setup(); + +public slots: + void handleUrlSlot(const QUrl &url); +}; + +#endif // URLHANDLER_H