From a564b96ccfbd89f83a4f64480c941e43b4716f92 Mon Sep 17 00:00:00 2001 From: Dmitry Victorov Date: Sun, 27 Dec 2015 07:02:31 +0300 Subject: [PATCH] Set qBittorrent as default torrent app in Mac OS --- src/base/preferences.cpp | 60 ++++++++++++++++++++++++++++++++++++++++ src/base/preferences.h | 6 ++++ src/gui/options_imp.cpp | 23 ++++++++++++++- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/base/preferences.cpp b/src/base/preferences.cpp index 590039380..3e3fdaef2 100644 --- a/src/base/preferences.cpp +++ b/src/base/preferences.cpp @@ -51,6 +51,10 @@ #include #endif +#ifdef Q_OS_MAC +#include +#endif + #include #include "base/utils/fs.h" #include "base/utils/misc.h" @@ -1910,6 +1914,62 @@ void Preferences::setMagnetLinkAssoc(bool set) } #endif +#ifdef Q_OS_MAC +namespace +{ + CFStringRef torrentExtension = CFSTR("torrent"); + CFStringRef magnetUrlScheme = CFSTR("magnet"); +} + +bool Preferences::isTorrentFileAssocSet() +{ + bool isSet = false; + CFStringRef torrentId = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, torrentExtension, NULL); + if (torrentId != NULL) { + CFStringRef defaultHandlerId = LSCopyDefaultRoleHandlerForContentType(torrentId, kLSRolesViewer); + if (defaultHandlerId != NULL) { + CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle()); + isSet = CFStringCompare(myBundleId, defaultHandlerId, 0) == kCFCompareEqualTo; + CFRelease(defaultHandlerId); + } + CFRelease(torrentId); + } + return isSet; +} + +bool Preferences::isMagnetLinkAssocSet() +{ + bool isSet = false; + CFStringRef defaultHandlerId = LSCopyDefaultHandlerForURLScheme(magnetUrlScheme); + if (defaultHandlerId != NULL) { + CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle()); + isSet = CFStringCompare(myBundleId, defaultHandlerId, 0) == kCFCompareEqualTo; + CFRelease(defaultHandlerId); + } + return isSet; +} + +void Preferences::setTorrentFileAssoc() +{ + if (isTorrentFileAssocSet()) + return; + CFStringRef torrentId = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, torrentExtension, NULL); + if (torrentId != NULL) { + CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle()); + LSSetDefaultRoleHandlerForContentType(torrentId, kLSRolesViewer, myBundleId); + CFRelease(torrentId); + } +} + +void Preferences::setMagnetLinkAssoc() +{ + if (isMagnetLinkAssocSet()) + return; + CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle()); + LSSetDefaultHandlerForURLScheme(magnetUrlScheme, myBundleId); +} +#endif + bool Preferences::isTrackerEnabled() const { return value("Preferences/Advanced/trackerEnabled", false).toBool(); diff --git a/src/base/preferences.h b/src/base/preferences.h index 4767a1d59..ce5f1f644 100644 --- a/src/base/preferences.h +++ b/src/base/preferences.h @@ -421,6 +421,12 @@ public: static bool isMagnetLinkAssocSet(); static void setTorrentFileAssoc(bool set); static void setMagnetLinkAssoc(bool set); +#endif +#ifdef Q_OS_MAC + static bool isTorrentFileAssocSet(); + static bool isMagnetLinkAssocSet(); + static void setTorrentFileAssoc(); + static void setMagnetLinkAssoc(); #endif bool isTrackerEnabled() const; void setTrackerEnabled(bool enabled); diff --git a/src/gui/options_imp.cpp b/src/gui/options_imp.cpp index 6ef0283a3..0dcab7b0c 100644 --- a/src/gui/options_imp.cpp +++ b/src/gui/options_imp.cpp @@ -123,6 +123,9 @@ options_imp::options_imp(QWidget *parent) #ifndef Q_OS_WIN checkStartup->setVisible(false); +#endif + +#if !(defined(Q_OS_WIN) || defined(Q_OS_MAC)) groupFileAssociation->setVisible(false); #endif @@ -149,7 +152,7 @@ options_imp::options_imp(QWidget *parent) #if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) && !defined(QT_DBUS_LIB) checkPreventFromSuspend->setDisabled(true); #endif -#ifdef Q_OS_WIN +#if defined(Q_OS_WIN) || defined(Q_OS_MAC) connect(checkAssociateTorrents, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton())); connect(checkAssociateMagnetLinks, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton())); #endif @@ -402,6 +405,18 @@ void options_imp::saveOptions() // Windows: file association settings Preferences::setTorrentFileAssoc(checkAssociateTorrents->isChecked()); Preferences::setMagnetLinkAssoc(checkAssociateMagnetLinks->isChecked()); +#endif +#ifdef Q_OS_MAC + if (checkAssociateTorrents->isChecked()) { + Preferences::setTorrentFileAssoc(); + checkAssociateTorrents->setChecked(Preferences::isTorrentFileAssocSet()); + checkAssociateTorrents->setEnabled(!checkAssociateTorrents->isChecked()); + } + if (checkAssociateMagnetLinks->isChecked()) { + Preferences::setMagnetLinkAssoc(); + checkAssociateMagnetLinks->setChecked(Preferences::isMagnetLinkAssocSet()); + checkAssociateMagnetLinks->setEnabled(!checkAssociateMagnetLinks->isChecked()); + } #endif // End General preferences @@ -573,6 +588,12 @@ void options_imp::loadOptions() checkStartup->setChecked(pref->WinStartup()); checkAssociateTorrents->setChecked(Preferences::isTorrentFileAssocSet()); checkAssociateMagnetLinks->setChecked(Preferences::isMagnetLinkAssocSet()); +#endif +#ifdef Q_OS_MAC + checkAssociateTorrents->setChecked(Preferences::isTorrentFileAssocSet()); + checkAssociateTorrents->setEnabled(!checkAssociateTorrents->isChecked()); + checkAssociateMagnetLinks->setChecked(Preferences::isMagnetLinkAssocSet()); + checkAssociateMagnetLinks->setEnabled(!checkAssociateMagnetLinks->isChecked()); #endif // End General preferences