Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qt: fix uri scheme handling on macOS #3212

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions frontends/qt/BitBox.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 8 additions & 19 deletions frontends/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <QSystemTrayIcon>
#include <QMessageBox>
#include <QtGlobal>
#include <QtSystemDetection>
#if defined(_WIN32)
#if QT_VERSION_MAJOR >= 6
#include <private/qguiapplication_p.h>
Expand All @@ -55,6 +56,7 @@
#include "filedialog.h"
#include "libserver.h"
#include "webclass.h"
#include "urlhandler.h"

#define APPNAME "BitBoxApp"

Expand Down Expand Up @@ -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<QFileOpenEvent*>(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 {
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions frontends/qt/urlhandler.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <QDesktopServices>

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());
}
33 changes: 33 additions & 0 deletions frontends/qt/urlhandler.h
Original file line number Diff line number Diff line change
@@ -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 <QObject>
#include <QUrl>

class UrlHandler : public QObject {
Q_OBJECT

public:
UrlHandler(QObject *parent = nullptr);

void setup();

public slots:
void handleUrlSlot(const QUrl &url);
};

#endif // URLHANDLER_H