From 02d72179feb11f8dd2f894d3dfa16da13fe6a2f0 Mon Sep 17 00:00:00 2001 From: Ryu481 <142620516+Ryu481@users.noreply.github.com> Date: Sat, 9 Aug 2025 12:13:18 +0200 Subject: [PATCH] Migrate away from deprecated methods for setting the standard application on macOS The methods for checking if qBittorrent is set as the standard application for opening torrent files and magnet links and setting qBittorrent as the standard application for those is using deprecated methods now. For example `LSCopyDefaultHandlerForURLScheme` and `kUTTagClassFilenameExtension`. The new methods have been moved to `macutilities.mm` because in `os.cpp` cocoa couldn't be imported. PR #23059. --- src/base/utils/os.cpp | 66 --------------------------------------- src/base/utils/os.h | 7 ----- src/gui/macutilities.h | 5 +++ src/gui/macutilities.mm | 40 ++++++++++++++++++++++++ src/gui/optionsdialog.cpp | 18 +++++------ 5 files changed, 54 insertions(+), 82 deletions(-) diff --git a/src/base/utils/os.cpp b/src/base/utils/os.cpp index c946cb698..f0a78b132 100644 --- a/src/base/utils/os.cpp +++ b/src/base/utils/os.cpp @@ -193,72 +193,6 @@ void Utils::OS::shutdownComputer([[maybe_unused]] const ShutdownDialogAction &ac #endif } -#ifdef Q_OS_MACOS -namespace -{ - const CFStringRef torrentExtension = CFSTR("torrent"); - const CFStringRef magnetUrlScheme = CFSTR("magnet"); -} - -bool Utils::OS::isTorrentFileAssocSet() -{ - bool isSet = false; - const CFStringRef torrentId = ::UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, torrentExtension, NULL); - if (torrentId != NULL) - { - const CFStringRef defaultHandlerId = ::LSCopyDefaultRoleHandlerForContentType(torrentId, kLSRolesViewer); - if (defaultHandlerId != NULL) - { - const CFStringRef myBundleId = ::CFBundleGetIdentifier(::CFBundleGetMainBundle()); - if (myBundleId != NULL) - isSet = ::CFStringCompare(myBundleId, defaultHandlerId, 0) == kCFCompareEqualTo; - ::CFRelease(defaultHandlerId); - } - ::CFRelease(torrentId); - } - return isSet; -} - -void Utils::OS::setTorrentFileAssoc() -{ - if (isTorrentFileAssocSet()) - return; - - const CFStringRef torrentId = ::UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, torrentExtension, NULL); - if (torrentId != NULL) - { - const CFStringRef myBundleId = ::CFBundleGetIdentifier(::CFBundleGetMainBundle()); - if (myBundleId != NULL) - ::LSSetDefaultRoleHandlerForContentType(torrentId, kLSRolesViewer, myBundleId); - ::CFRelease(torrentId); - } -} - -bool Utils::OS::isMagnetLinkAssocSet() -{ - bool isSet = false; - const CFStringRef defaultHandlerId = ::LSCopyDefaultHandlerForURLScheme(magnetUrlScheme); - if (defaultHandlerId != NULL) - { - const CFStringRef myBundleId = ::CFBundleGetIdentifier(::CFBundleGetMainBundle()); - if (myBundleId != NULL) - isSet = ::CFStringCompare(myBundleId, defaultHandlerId, 0) == kCFCompareEqualTo; - ::CFRelease(defaultHandlerId); - } - return isSet; -} - -void Utils::OS::setMagnetLinkAssoc() -{ - if (isMagnetLinkAssocSet()) - return; - - const CFStringRef myBundleId = ::CFBundleGetIdentifier(::CFBundleGetMainBundle()); - if (myBundleId != NULL) - ::LSSetDefaultHandlerForURLScheme(magnetUrlScheme, myBundleId); -} -#endif // Q_OS_MACOS - #ifdef Q_OS_WIN Path Utils::OS::windowsSystemPath() { diff --git a/src/base/utils/os.h b/src/base/utils/os.h index 9dc4df021..7354cd4d3 100644 --- a/src/base/utils/os.h +++ b/src/base/utils/os.h @@ -48,13 +48,6 @@ namespace Utils::OS { void shutdownComputer(const ShutdownDialogAction &action); -#ifdef Q_OS_MACOS - bool isTorrentFileAssocSet(); - void setTorrentFileAssoc(); - bool isMagnetLinkAssocSet(); - void setMagnetLinkAssoc(); -#endif // Q_OS_MACOS - #ifdef Q_OS_WIN Path windowsSystemPath(); diff --git a/src/gui/macutilities.h b/src/gui/macutilities.h index b27eeb177..46b732db0 100644 --- a/src/gui/macutilities.h +++ b/src/gui/macutilities.h @@ -44,6 +44,11 @@ namespace MacUtils void displayNotification(const QString &title, const QString &message); void openFiles(const PathList &pathList); + bool isMagnetLinkAssocSet(); + void setMagnetLinkAssoc(); + bool isTorrentFileAssocSet(); + void setTorrentFileAssoc(); + QString badgeLabelText(); void setBadgeLabelText(const QString &text); } diff --git a/src/gui/macutilities.mm b/src/gui/macutilities.mm index d4c643ce5..fc2d40b50 100644 --- a/src/gui/macutilities.mm +++ b/src/gui/macutilities.mm @@ -143,6 +143,46 @@ namespace MacUtils } } + bool isMagnetLinkAssocSet() + { + @autoreleasepool + { + const NSURL *magnetStandardURL = [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL:[NSURL URLWithString:@"magnet:"]]; + const NSURL *qbtURL = [[NSBundle mainBundle] bundleURL]; + return [magnetStandardURL isEqual:qbtURL]; + } + } + + void setMagnetLinkAssoc() + { + @autoreleasepool + { + [[NSWorkspace sharedWorkspace] setDefaultApplicationAtURL:[[NSBundle mainBundle] bundleURL] + toOpenURLsWithScheme:@"magnet" completionHandler:nil]; + } + } + + bool isTorrentFileAssocSet() + { + @autoreleasepool + { + const NSURL *torrentStandardURL = [[NSWorkspace sharedWorkspace] + URLForApplicationToOpenContentType:[UTType typeWithFilenameExtension:@"torrent"]]; + const NSURL *qbtURL = [[NSBundle mainBundle] bundleURL]; + return [torrentStandardURL isEqual:qbtURL]; + } + } + + void setTorrentFileAssoc() + { + @autoreleasepool + { + [[NSWorkspace sharedWorkspace] setDefaultApplicationAtURL:[[NSBundle mainBundle] bundleURL] + toOpenContentType:[UTType typeWithFilenameExtension:@"torrent"] + completionHandler:nil]; + } + } + QString badgeLabelText() { return QString::fromNSString(NSApp.dockTile.badgeLabel); diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index 557c5569c..ad2eb348b 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -88,9 +88,9 @@ #include "base/net/dnsupdater.h" #endif -#if defined Q_OS_MACOS || defined Q_OS_WIN -#include "base/utils/os.h" -#endif // defined Q_OS_MACOS || defined Q_OS_WIN +#ifdef Q_OS_MACOS +#include "macutilities.h" +#endif #define SETTINGS_KEY(name) u"OptionsDialog/" name @@ -329,9 +329,9 @@ void OptionsDialog::loadBehaviorTabOptions() #ifdef Q_OS_MACOS m_ui->checkShowSystray->setVisible(false); - m_ui->checkAssociateTorrents->setChecked(Utils::OS::isTorrentFileAssocSet()); + m_ui->checkAssociateTorrents->setChecked(MacUtils::isTorrentFileAssocSet()); m_ui->checkAssociateTorrents->setEnabled(!m_ui->checkAssociateTorrents->isChecked()); - m_ui->checkAssociateMagnetLinks->setChecked(Utils::OS::isMagnetLinkAssocSet()); + m_ui->checkAssociateMagnetLinks->setChecked(MacUtils::isMagnetLinkAssocSet()); m_ui->checkAssociateMagnetLinks->setEnabled(!m_ui->checkAssociateMagnetLinks->isChecked()); #endif @@ -517,14 +517,14 @@ void OptionsDialog::saveBehaviorTabOptions() const #ifdef Q_OS_MACOS if (m_ui->checkAssociateTorrents->isChecked()) { - Utils::OS::setTorrentFileAssoc(); - m_ui->checkAssociateTorrents->setChecked(Utils::OS::isTorrentFileAssocSet()); + MacUtils::setTorrentFileAssoc(); + m_ui->checkAssociateTorrents->setChecked(MacUtils::isTorrentFileAssocSet()); m_ui->checkAssociateTorrents->setEnabled(!m_ui->checkAssociateTorrents->isChecked()); } if (m_ui->checkAssociateMagnetLinks->isChecked()) { - Utils::OS::setMagnetLinkAssoc(); - m_ui->checkAssociateMagnetLinks->setChecked(Utils::OS::isMagnetLinkAssocSet()); + MacUtils::setMagnetLinkAssoc(); + m_ui->checkAssociateMagnetLinks->setChecked(MacUtils::isMagnetLinkAssocSet()); m_ui->checkAssociateMagnetLinks->setEnabled(!m_ui->checkAssociateMagnetLinks->isChecked()); } #endif