Skip to content

Commit 5f81ba7

Browse files
committed
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 qt/qtbase@664c7ff. 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).
1 parent d4c5a91 commit 5f81ba7

File tree

4 files changed

+74
-21
lines changed

4 files changed

+74
-21
lines changed

frontends/qt/BitBox.pro

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ unix:!macx {
5959

6060
SOURCES += \
6161
main.cpp \
62-
filedialog.cpp
62+
filedialog.cpp \
63+
urlhandler.cpp
6364

64-
HEADERS += libserver.h webclass.h filedialog.h
65+
HEADERS += libserver.h webclass.h filedialog.h urlhandler.h
6566

6667
unix:macx {
6768
CONFIG += sdk_no_version_check

frontends/qt/main.cpp

+8-19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <QSystemTrayIcon>
3939
#include <QMessageBox>
4040
#include <QtGlobal>
41+
#include <QtSystemDetection>
4142
#if defined(_WIN32)
4243
#if QT_VERSION_MAJOR >= 6
4344
#include <private/qguiapplication_p.h>
@@ -55,6 +56,7 @@
5556
#include "filedialog.h"
5657
#include "libserver.h"
5758
#include "webclass.h"
59+
#include "urlhandler.h"
5860

5961
#define APPNAME "BitBoxApp"
6062

@@ -85,25 +87,6 @@ class BitBoxApp : public SingleApplication
8587
Mode::User | Mode::SecondaryNotification | Mode::ExcludeAppVersion | Mode::ExcludeAppPath)
8688
{
8789
}
88-
89-
#if defined(Q_OS_MACOS)
90-
bool event(QEvent *event) override
91-
{
92-
if (event->type() == QEvent::FileOpen) {
93-
QFileOpenEvent* openEvent = static_cast<QFileOpenEvent*>(event);
94-
if (!openEvent->url().isEmpty()) {
95-
// This is only supported on macOS and is used to handle URIs that are opened with
96-
// the BitBoxApp, such as "aopp:..." links. The event is received and handled both
97-
// if the BitBoxApp is launched and also when it is already running, in which case
98-
// it is brought to the foreground automatically.
99-
100-
handleURI(openEvent->url().toString().toUtf8().constData());
101-
}
102-
}
103-
104-
return QApplication::event(event);
105-
}
106-
#endif
10790
};
10891

10992
class WebEnginePage : public QWebEnginePage {
@@ -318,6 +301,12 @@ int main(int argc, char *argv[])
318301
}
319302

320303
BitBoxApp a(argc, argv);
304+
// The URI scheme handler for aopp is handled via OS events on macOS. The other platforms invoke
305+
// the process with the uri as a command line param.
306+
#if defined(Q_OS_MACOS)
307+
UrlHandler url_handler;
308+
url_handler.setup();
309+
#endif
321310
// These three are part of the SingleApplication instance ID - if changed, the user should close
322311
// th existing app before launching the new one.
323312
// See https://github.com/BitBoxSwiss/SingleApplication/blob/c557da5d0cb63b8002c1ba99ec18f257620009b1/singleapplication_p.cpp#L135-L137

frontends/qt/urlhandler.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2025 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "urlhandler.h"
16+
#include "libserver.h"
17+
18+
#include <iostream>
19+
#include <QDesktopServices>
20+
21+
UrlHandler::UrlHandler(QObject *parent) : QObject(parent) {
22+
}
23+
24+
void UrlHandler::setup() {
25+
QDesktopServices::setUrlHandler("aopp", this, "handleUrlSlot");
26+
}
27+
28+
void UrlHandler::handleUrlSlot(const QUrl &url) {
29+
handleURI(url.toString().toUtf8().constData());
30+
}

frontends/qt/urlhandler.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2025 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef URLHANDLER_H
16+
#define URLHANDLER_H
17+
18+
#include <QObject>
19+
#include <QUrl>
20+
21+
class UrlHandler : public QObject {
22+
Q_OBJECT
23+
24+
public:
25+
UrlHandler(QObject *parent = nullptr);
26+
27+
void setup();
28+
29+
public slots:
30+
void handleUrlSlot(const QUrl &url);
31+
};
32+
33+
#endif // URLHANDLER_H

0 commit comments

Comments
 (0)