mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2024-12-27 18:28:18 +03:00
Improve .torrent associate handling
* Use correct pointer type in NSIS scripts * Only remove qbt specific registry keys when uninstalling or disassociating * Set .torrent Content Type when associating .torrent format * Move ".torrent association" functions to Utils::OS class Original PR #19709.
This commit is contained in:
parent
4beee27701
commit
ec8802203d
11 changed files with 278 additions and 185 deletions
4
dist/windows/installer.nsi
vendored
4
dist/windows/installer.nsi
vendored
|
@ -109,7 +109,7 @@ Section $(inst_torrent) ;"Open .torrent files with qBittorrent"
|
||||||
|
|
||||||
!insertmacro UAC_AsUser_Call Function inst_torrent_user ${UAC_SYNCREGISTERS}|${UAC_SYNCOUTDIR}|${UAC_SYNCINSTDIR}
|
!insertmacro UAC_AsUser_Call Function inst_torrent_user ${UAC_SYNCREGISTERS}|${UAC_SYNCOUTDIR}|${UAC_SYNCINSTDIR}
|
||||||
|
|
||||||
System::Call 'Shell32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED}, i ${SHCNF_IDLIST}, i 0, i 0)'
|
System::Call 'Shell32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED}, i ${SHCNF_IDLIST}, p 0, p 0)'
|
||||||
|
|
||||||
SectionEnd
|
SectionEnd
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ Section $(inst_magnet) ;"Open magnet links with qBittorrent"
|
||||||
|
|
||||||
!insertmacro UAC_AsUser_Call Function inst_magnet_user ${UAC_SYNCREGISTERS}|${UAC_SYNCOUTDIR}|${UAC_SYNCINSTDIR}
|
!insertmacro UAC_AsUser_Call Function inst_magnet_user ${UAC_SYNCREGISTERS}|${UAC_SYNCOUTDIR}|${UAC_SYNCINSTDIR}
|
||||||
|
|
||||||
System::Call 'Shell32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED}, i ${SHCNF_IDLIST}, i 0, i 0)'
|
System::Call 'Shell32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED}, i ${SHCNF_IDLIST}, p 0, p 0)'
|
||||||
|
|
||||||
SectionEnd
|
SectionEnd
|
||||||
|
|
||||||
|
|
11
dist/windows/uninstaller.nsi
vendored
11
dist/windows/uninstaller.nsi
vendored
|
@ -26,17 +26,17 @@ Section "un.$(remove_associations)" ;"un.Remove file associations"
|
||||||
DetailPrint "$(uninst_tor_warn) $0"
|
DetailPrint "$(uninst_tor_warn) $0"
|
||||||
DeleteRegValue HKLM "Software\Classes\.torrent" ""
|
DeleteRegValue HKLM "Software\Classes\.torrent" ""
|
||||||
DeleteRegKey /ifempty HKLM "Software\Classes\.torrent"
|
DeleteRegKey /ifempty HKLM "Software\Classes\.torrent"
|
||||||
|
|
||||||
torrent_end:
|
torrent_end:
|
||||||
|
|
||||||
ReadRegStr $0 HKLM "Software\Classes\magnet\shell\open\command" ""
|
ReadRegStr $0 HKLM "Software\Classes\magnet\shell\open\command" ""
|
||||||
StrCmp $0 '"$INSTDIR\qbittorrent.exe" "%1"' 0 magnet_end
|
StrCmp $0 '"$INSTDIR\qbittorrent.exe" "%1"' 0 magnet_end
|
||||||
DetailPrint "$(uninst_mag_warn) $0"
|
DetailPrint "$(uninst_mag_warn) $0"
|
||||||
DeleteRegKey HKLM "Software\Classes\magnet"
|
DeleteRegKey HKLM "Software\Classes\magnet"
|
||||||
|
|
||||||
magnet_end:
|
magnet_end:
|
||||||
|
|
||||||
!insertmacro UAC_AsUser_Call Function un.remove_associations_user ${UAC_SYNCREGISTERS}|${UAC_SYNCOUTDIR}|${UAC_SYNCINSTDIR}
|
!insertmacro UAC_AsUser_Call Function un.remove_associations_user ${UAC_SYNCREGISTERS}|${UAC_SYNCOUTDIR}|${UAC_SYNCINSTDIR}
|
||||||
|
|
||||||
System::Call 'Shell32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED}, i ${SHCNF_IDLIST}, i 0, i 0)'
|
System::Call 'Shell32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED}, i ${SHCNF_IDLIST}, p 0, p 0)'
|
||||||
SectionEnd
|
SectionEnd
|
||||||
|
|
||||||
Function un.remove_associations_user
|
Function un.remove_associations_user
|
||||||
|
@ -45,13 +45,12 @@ Function un.remove_associations_user
|
||||||
DetailPrint "$(uninst_tor_warn) $0"
|
DetailPrint "$(uninst_tor_warn) $0"
|
||||||
DeleteRegValue HKCU "Software\Classes\.torrent" ""
|
DeleteRegValue HKCU "Software\Classes\.torrent" ""
|
||||||
DeleteRegKey /ifempty HKCU "Software\Classes\.torrent"
|
DeleteRegKey /ifempty HKCU "Software\Classes\.torrent"
|
||||||
|
|
||||||
torrent_end:
|
torrent_end:
|
||||||
|
|
||||||
ReadRegStr $0 HKCU "Software\Classes\magnet\shell\open\command" ""
|
ReadRegStr $0 HKCU "Software\Classes\magnet\shell\open\command" ""
|
||||||
StrCmp $0 '"$INSTDIR\qbittorrent.exe" "%1"' 0 magnet_end
|
StrCmp $0 '"$INSTDIR\qbittorrent.exe" "%1"' 0 magnet_end
|
||||||
DetailPrint "$(uninst_mag_warn) $0"
|
DetailPrint "$(uninst_mag_warn) $0"
|
||||||
DeleteRegKey HKCU "Software\Classes\magnet"
|
DeleteRegKey HKCU "Software\Classes\magnet"
|
||||||
|
|
||||||
magnet_end:
|
magnet_end:
|
||||||
FunctionEnd
|
FunctionEnd
|
||||||
|
|
||||||
|
@ -62,7 +61,7 @@ Section "un.$(remove_registry)" ;"un.Remove registry keys"
|
||||||
DeleteRegKey HKLM "Software\qBittorrent"
|
DeleteRegKey HKLM "Software\qBittorrent"
|
||||||
DeleteRegKey HKLM "Software\Classes\qBittorrent"
|
DeleteRegKey HKLM "Software\Classes\qBittorrent"
|
||||||
|
|
||||||
System::Call 'Shell32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED}, i ${SHCNF_IDLIST}, i 0, i 0)'
|
System::Call 'Shell32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED}, i ${SHCNF_IDLIST}, p 0, p 0)'
|
||||||
SectionEnd
|
SectionEnd
|
||||||
|
|
||||||
Section "un.$(remove_firewall)" ;
|
Section "un.$(remove_firewall)" ;
|
||||||
|
|
|
@ -98,6 +98,10 @@
|
||||||
#include "gui/uithememanager.h"
|
#include "gui/uithememanager.h"
|
||||||
#include "gui/utils.h"
|
#include "gui/utils.h"
|
||||||
#include "gui/windowstate.h"
|
#include "gui/windowstate.h"
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
#include "base/utils/os.h"
|
||||||
|
#endif // Q_OS_WIN
|
||||||
#endif // DISABLE_GUI
|
#endif // DISABLE_GUI
|
||||||
|
|
||||||
#ifndef DISABLE_WEBUI
|
#ifndef DISABLE_WEBUI
|
||||||
|
@ -877,14 +881,14 @@ int Application::exec()
|
||||||
delete m_startupProgressDialog;
|
delete m_startupProgressDialog;
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
auto *pref = Preferences::instance();
|
auto *pref = Preferences::instance();
|
||||||
if (!pref->neverCheckFileAssoc() && (!Preferences::isTorrentFileAssocSet() || !Preferences::isMagnetLinkAssocSet()))
|
if (!pref->neverCheckFileAssoc() && (!Utils::OS::isTorrentFileAssocSet() || !Utils::OS::isMagnetLinkAssocSet()))
|
||||||
{
|
{
|
||||||
if (QMessageBox::question(m_window, tr("Torrent file association")
|
if (QMessageBox::question(m_window, tr("Torrent file association")
|
||||||
, tr("qBittorrent is not the default application for opening torrent files or Magnet links.\nDo you want to make qBittorrent the default application for these?")
|
, tr("qBittorrent is not the default application for opening torrent files or Magnet links.\nDo you want to make qBittorrent the default application for these?")
|
||||||
, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
|
, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
|
||||||
{
|
{
|
||||||
pref->setTorrentFileAssoc(true);
|
Utils::OS::setTorrentFileAssoc(true);
|
||||||
pref->setMagnetLinkAssoc(true);
|
Utils::OS::setMagnetLinkAssoc(true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -99,6 +99,7 @@ add_library(qbt_base STATIC
|
||||||
utils/io.h
|
utils/io.h
|
||||||
utils/misc.h
|
utils/misc.h
|
||||||
utils/net.h
|
utils/net.h
|
||||||
|
utils/os.h
|
||||||
utils/password.h
|
utils/password.h
|
||||||
utils/random.h
|
utils/random.h
|
||||||
utils/string.h
|
utils/string.h
|
||||||
|
@ -184,6 +185,7 @@ add_library(qbt_base STATIC
|
||||||
utils/io.cpp
|
utils/io.cpp
|
||||||
utils/misc.cpp
|
utils/misc.cpp
|
||||||
utils/net.cpp
|
utils/net.cpp
|
||||||
|
utils/os.cpp
|
||||||
utils/password.cpp
|
utils/password.cpp
|
||||||
utils/random.cpp
|
utils/random.cpp
|
||||||
utils/string.cpp
|
utils/string.cpp
|
||||||
|
|
|
@ -99,6 +99,7 @@ HEADERS += \
|
||||||
$$PWD/utils/io.h \
|
$$PWD/utils/io.h \
|
||||||
$$PWD/utils/misc.h \
|
$$PWD/utils/misc.h \
|
||||||
$$PWD/utils/net.h \
|
$$PWD/utils/net.h \
|
||||||
|
$$PWD/utils/os.h \
|
||||||
$$PWD/utils/password.h \
|
$$PWD/utils/password.h \
|
||||||
$$PWD/utils/random.h \
|
$$PWD/utils/random.h \
|
||||||
$$PWD/utils/string.h \
|
$$PWD/utils/string.h \
|
||||||
|
@ -184,6 +185,7 @@ SOURCES += \
|
||||||
$$PWD/utils/io.cpp \
|
$$PWD/utils/io.cpp \
|
||||||
$$PWD/utils/misc.cpp \
|
$$PWD/utils/misc.cpp \
|
||||||
$$PWD/utils/net.cpp \
|
$$PWD/utils/net.cpp \
|
||||||
|
$$PWD/utils/os.cpp \
|
||||||
$$PWD/utils/password.cpp \
|
$$PWD/utils/password.cpp \
|
||||||
$$PWD/utils/random.cpp \
|
$$PWD/utils/random.cpp \
|
||||||
$$PWD/utils/string.cpp \
|
$$PWD/utils/string.cpp \
|
||||||
|
|
|
@ -31,13 +31,6 @@
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
#ifdef Q_OS_MACOS
|
|
||||||
#include <CoreServices/CoreServices.h>
|
|
||||||
#endif
|
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
#include <shlobj.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
@ -47,10 +40,6 @@
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QTime>
|
#include <QTime>
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
#include <QRegularExpression>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "algorithm.h"
|
#include "algorithm.h"
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
|
@ -1316,150 +1305,8 @@ void Preferences::setNeverCheckFileAssoc(const bool check)
|
||||||
|
|
||||||
setValue(u"Preferences/Win32/NeverCheckFileAssocation"_s, check);
|
setValue(u"Preferences/Win32/NeverCheckFileAssocation"_s, check);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Preferences::isTorrentFileAssocSet()
|
|
||||||
{
|
|
||||||
const QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
|
|
||||||
if (settings.value(u".torrent/Default"_s).toString() != u"qBittorrent")
|
|
||||||
{
|
|
||||||
qDebug(".torrent != qBittorrent");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Preferences::setTorrentFileAssoc(const bool set)
|
|
||||||
{
|
|
||||||
QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
|
|
||||||
|
|
||||||
// .Torrent association
|
|
||||||
if (set)
|
|
||||||
{
|
|
||||||
const QString oldProgId = settings.value(u".torrent/Default"_s).toString();
|
|
||||||
if (!oldProgId.isEmpty() && (oldProgId != u"qBittorrent"))
|
|
||||||
settings.setValue((u".torrent/OpenWithProgids/" + oldProgId), QString());
|
|
||||||
settings.setValue(u".torrent/Default"_s, u"qBittorrent"_s);
|
|
||||||
}
|
|
||||||
else if (isTorrentFileAssocSet())
|
|
||||||
{
|
|
||||||
settings.setValue(u".torrent/Default"_s, QString());
|
|
||||||
}
|
|
||||||
|
|
||||||
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Preferences::isMagnetLinkAssocSet()
|
|
||||||
{
|
|
||||||
const QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
|
|
||||||
|
|
||||||
// Check magnet link assoc
|
|
||||||
const QString shellCommand = settings.value(u"magnet/shell/open/command/Default"_s, QString()).toString();
|
|
||||||
|
|
||||||
const QRegularExpressionMatch exeRegMatch = QRegularExpression(u"\"([^\"]+)\".*"_s).match(shellCommand);
|
|
||||||
if (!exeRegMatch.hasMatch())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const Path assocExe {exeRegMatch.captured(1)};
|
|
||||||
if (assocExe != Path(qApp->applicationFilePath()))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Preferences::setMagnetLinkAssoc(const bool set)
|
|
||||||
{
|
|
||||||
QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
|
|
||||||
|
|
||||||
// Magnet association
|
|
||||||
if (set)
|
|
||||||
{
|
|
||||||
const QString applicationFilePath = Path(qApp->applicationFilePath()).toString();
|
|
||||||
const QString commandStr = u'"' + applicationFilePath + u"\" \"%1\"";
|
|
||||||
const QString iconStr = u'"' + applicationFilePath + u"\",1";
|
|
||||||
|
|
||||||
settings.setValue(u"magnet/Default"_s, u"URL:Magnet link"_s);
|
|
||||||
settings.setValue(u"magnet/Content Type"_s, u"application/x-magnet"_s);
|
|
||||||
settings.setValue(u"magnet/URL Protocol"_s, QString());
|
|
||||||
settings.setValue(u"magnet/DefaultIcon/Default"_s, iconStr);
|
|
||||||
settings.setValue(u"magnet/shell/Default"_s, u"open"_s);
|
|
||||||
settings.setValue(u"magnet/shell/open/command/Default"_s, commandStr);
|
|
||||||
}
|
|
||||||
else if (isMagnetLinkAssocSet())
|
|
||||||
{
|
|
||||||
settings.remove(u"magnet"_s);
|
|
||||||
}
|
|
||||||
|
|
||||||
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
|
|
||||||
}
|
|
||||||
#endif // Q_OS_WIN
|
#endif // Q_OS_WIN
|
||||||
|
|
||||||
#ifdef Q_OS_MACOS
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
const CFStringRef torrentExtension = CFSTR("torrent");
|
|
||||||
const CFStringRef magnetUrlScheme = CFSTR("magnet");
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Preferences::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 Preferences::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 Preferences::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 Preferences::setMagnetLinkAssoc()
|
|
||||||
{
|
|
||||||
if (isMagnetLinkAssocSet())
|
|
||||||
return;
|
|
||||||
|
|
||||||
const CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle());
|
|
||||||
if (myBundleId != NULL)
|
|
||||||
LSSetDefaultHandlerForURLScheme(magnetUrlScheme, myBundleId);
|
|
||||||
}
|
|
||||||
#endif // Q_OS_MACOS
|
|
||||||
|
|
||||||
int Preferences::getTrackerPort() const
|
int Preferences::getTrackerPort() const
|
||||||
{
|
{
|
||||||
return value<int>(u"Preferences/Advanced/trackerPort"_s, 9000);
|
return value<int>(u"Preferences/Advanced/trackerPort"_s, 9000);
|
||||||
|
|
|
@ -290,17 +290,8 @@ public:
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
bool neverCheckFileAssoc() const;
|
bool neverCheckFileAssoc() const;
|
||||||
void setNeverCheckFileAssoc(bool check = true);
|
void setNeverCheckFileAssoc(bool check = true);
|
||||||
static bool isTorrentFileAssocSet();
|
|
||||||
static void setTorrentFileAssoc(bool set);
|
|
||||||
static bool isMagnetLinkAssocSet();
|
|
||||||
static void setMagnetLinkAssoc(bool set);
|
|
||||||
#endif
|
|
||||||
#ifdef Q_OS_MACOS
|
|
||||||
static bool isTorrentFileAssocSet();
|
|
||||||
static void setTorrentFileAssoc();
|
|
||||||
static bool isMagnetLinkAssocSet();
|
|
||||||
static void setMagnetLinkAssoc();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int getTrackerPort() const;
|
int getTrackerPort() const;
|
||||||
void setTrackerPort(int port);
|
void setTrackerPort(int port);
|
||||||
bool isTrackerPortForwardingEnabled() const;
|
bool isTrackerPortForwardingEnabled() const;
|
||||||
|
|
|
@ -611,4 +611,4 @@ Path Utils::Misc::windowsSystemPath()
|
||||||
}();
|
}();
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
#endif
|
#endif // Q_OS_WIN
|
||||||
|
|
194
src/base/utils/os.cpp
Normal file
194
src/base/utils/os.cpp
Normal file
|
@ -0,0 +1,194 @@
|
||||||
|
/*
|
||||||
|
* Bittorrent Client using Qt and libtorrent.
|
||||||
|
* Copyright (C) 2023 Mike Tzou (Chocobo1)
|
||||||
|
* Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com>
|
||||||
|
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
* In addition, as a special exception, the copyright holders give permission to
|
||||||
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
||||||
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
||||||
|
* and distribute the linked executables. You must obey the GNU General Public
|
||||||
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
||||||
|
* modify file(s), you may extend this exception to your version of the file(s),
|
||||||
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
||||||
|
* exception statement from your version.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "os.h"
|
||||||
|
|
||||||
|
#ifdef Q_OS_MACOS
|
||||||
|
#include <CoreServices/CoreServices.h>
|
||||||
|
#endif // Q_OS_MACOS
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
#include <shlobj.h>
|
||||||
|
#endif // Q_OS_WIN
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QSettings>
|
||||||
|
#endif // Q_OS_WIN
|
||||||
|
|
||||||
|
#include "base/global.h"
|
||||||
|
#include "base/path.h"
|
||||||
|
|
||||||
|
#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
|
||||||
|
bool Utils::OS::isTorrentFileAssocSet()
|
||||||
|
{
|
||||||
|
const QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
|
||||||
|
return settings.value(u".torrent/Default"_s).toString() == u"qBittorrent";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Utils::OS::setTorrentFileAssoc(const bool set)
|
||||||
|
{
|
||||||
|
if (set == isTorrentFileAssocSet())
|
||||||
|
return;
|
||||||
|
|
||||||
|
QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
|
||||||
|
|
||||||
|
if (set)
|
||||||
|
{
|
||||||
|
const QString oldProgId = settings.value(u".torrent/Default"_s).toString();
|
||||||
|
if (!oldProgId.isEmpty() && (oldProgId != u"qBittorrent"))
|
||||||
|
settings.setValue((u".torrent/OpenWithProgids/" + oldProgId), QString());
|
||||||
|
|
||||||
|
settings.setValue(u".torrent/Default"_s, u"qBittorrent"_s);
|
||||||
|
settings.setValue(u".torrent/Content Type"_s, u"application/x-bittorrent"_s);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
settings.setValue(u".torrent/Default"_s, QString());
|
||||||
|
}
|
||||||
|
|
||||||
|
::SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Utils::OS::isMagnetLinkAssocSet()
|
||||||
|
{
|
||||||
|
const QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
|
||||||
|
const QString shellCommand = settings.value(u"magnet/shell/open/command/Default"_s).toString();
|
||||||
|
|
||||||
|
const QRegularExpressionMatch exeRegMatch = QRegularExpression(u"\"([^\"]+)\".*"_s).match(shellCommand);
|
||||||
|
if (!exeRegMatch.hasMatch())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const Path assocExe {exeRegMatch.captured(1)};
|
||||||
|
if (assocExe != Path(qApp->applicationFilePath()))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Utils::OS::setMagnetLinkAssoc(const bool set)
|
||||||
|
{
|
||||||
|
if (set == isMagnetLinkAssocSet())
|
||||||
|
return;
|
||||||
|
|
||||||
|
QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
|
||||||
|
|
||||||
|
if (set)
|
||||||
|
{
|
||||||
|
const QString applicationFilePath = Path(qApp->applicationFilePath()).toString();
|
||||||
|
const QString commandStr = u'"' + applicationFilePath + u"\" \"%1\"";
|
||||||
|
const QString iconStr = u'"' + applicationFilePath + u"\",1";
|
||||||
|
|
||||||
|
settings.setValue(u"magnet/Default"_s, u"URL:Magnet link"_s);
|
||||||
|
settings.setValue(u"magnet/Content Type"_s, u"application/x-magnet"_s);
|
||||||
|
settings.setValue(u"magnet/DefaultIcon/Default"_s, iconStr);
|
||||||
|
settings.setValue(u"magnet/shell/Default"_s, u"open"_s);
|
||||||
|
settings.setValue(u"magnet/shell/open/command/Default"_s, commandStr);
|
||||||
|
settings.setValue(u"magnet/URL Protocol"_s, QString());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// only wipe values that are specific to qbt
|
||||||
|
settings.setValue(u"magnet/DefaultIcon/Default"_s, QString());
|
||||||
|
settings.setValue(u"magnet/shell/open/command/Default"_s, QString());
|
||||||
|
}
|
||||||
|
|
||||||
|
::SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
#endif // Q_OS_WIN
|
50
src/base/utils/os.h
Normal file
50
src/base/utils/os.h
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Bittorrent Client using Qt and libtorrent.
|
||||||
|
* Copyright (C) 2023 Mike Tzou (Chocobo1)
|
||||||
|
* Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com>
|
||||||
|
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
* In addition, as a special exception, the copyright holders give permission to
|
||||||
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
||||||
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
||||||
|
* and distribute the linked executables. You must obey the GNU General Public
|
||||||
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
||||||
|
* modify file(s), you may extend this exception to your version of the file(s),
|
||||||
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
||||||
|
* exception statement from your version.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QtGlobal>
|
||||||
|
|
||||||
|
namespace Utils::OS
|
||||||
|
{
|
||||||
|
#ifdef Q_OS_MACOS
|
||||||
|
bool isTorrentFileAssocSet();
|
||||||
|
void setTorrentFileAssoc();
|
||||||
|
bool isMagnetLinkAssocSet();
|
||||||
|
void setMagnetLinkAssoc();
|
||||||
|
#endif // Q_OS_MACOS
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
bool isTorrentFileAssocSet();
|
||||||
|
void setTorrentFileAssoc(bool set);
|
||||||
|
bool isMagnetLinkAssocSet();
|
||||||
|
void setMagnetLinkAssoc(bool set);
|
||||||
|
#endif // Q_OS_WIN
|
||||||
|
}
|
|
@ -74,6 +74,10 @@
|
||||||
#include "base/net/dnsupdater.h"
|
#include "base/net/dnsupdater.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined Q_OS_MACOS || defined Q_OS_WIN
|
||||||
|
#include "base/utils/os.h"
|
||||||
|
#endif // defined Q_OS_MACOS || defined Q_OS_WIN
|
||||||
|
|
||||||
#define SETTINGS_KEY(name) u"OptionsDialog/" name
|
#define SETTINGS_KEY(name) u"OptionsDialog/" name
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
|
@ -284,15 +288,15 @@ void OptionsDialog::loadBehaviorTabOptions()
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
m_ui->checkStartup->setChecked(pref->WinStartup());
|
m_ui->checkStartup->setChecked(pref->WinStartup());
|
||||||
m_ui->checkAssociateTorrents->setChecked(Preferences::isTorrentFileAssocSet());
|
m_ui->checkAssociateTorrents->setChecked(Utils::OS::isTorrentFileAssocSet());
|
||||||
m_ui->checkAssociateMagnetLinks->setChecked(Preferences::isMagnetLinkAssocSet());
|
m_ui->checkAssociateMagnetLinks->setChecked(Utils::OS::isMagnetLinkAssocSet());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef Q_OS_MACOS
|
#ifdef Q_OS_MACOS
|
||||||
m_ui->checkShowSystray->setVisible(false);
|
m_ui->checkShowSystray->setVisible(false);
|
||||||
m_ui->checkAssociateTorrents->setChecked(Preferences::isTorrentFileAssocSet());
|
m_ui->checkAssociateTorrents->setChecked(Utils::OS::isTorrentFileAssocSet());
|
||||||
m_ui->checkAssociateTorrents->setEnabled(!m_ui->checkAssociateTorrents->isChecked());
|
m_ui->checkAssociateTorrents->setEnabled(!m_ui->checkAssociateTorrents->isChecked());
|
||||||
m_ui->checkAssociateMagnetLinks->setChecked(Preferences::isMagnetLinkAssocSet());
|
m_ui->checkAssociateMagnetLinks->setChecked(Utils::OS::isMagnetLinkAssocSet());
|
||||||
m_ui->checkAssociateMagnetLinks->setEnabled(!m_ui->checkAssociateMagnetLinks->isChecked());
|
m_ui->checkAssociateMagnetLinks->setEnabled(!m_ui->checkAssociateMagnetLinks->isChecked());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -433,8 +437,8 @@ void OptionsDialog::saveBehaviorTabOptions() const
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
pref->setWinStartup(WinStartup());
|
pref->setWinStartup(WinStartup());
|
||||||
|
|
||||||
Preferences::setTorrentFileAssoc(m_ui->checkAssociateTorrents->isChecked());
|
Utils::OS::setTorrentFileAssoc(m_ui->checkAssociateTorrents->isChecked());
|
||||||
Preferences::setMagnetLinkAssoc(m_ui->checkAssociateMagnetLinks->isChecked());
|
Utils::OS::setMagnetLinkAssoc(m_ui->checkAssociateMagnetLinks->isChecked());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef Q_OS_MACOS
|
#ifndef Q_OS_MACOS
|
||||||
|
@ -447,14 +451,14 @@ void OptionsDialog::saveBehaviorTabOptions() const
|
||||||
#ifdef Q_OS_MACOS
|
#ifdef Q_OS_MACOS
|
||||||
if (m_ui->checkAssociateTorrents->isChecked())
|
if (m_ui->checkAssociateTorrents->isChecked())
|
||||||
{
|
{
|
||||||
Preferences::setTorrentFileAssoc();
|
Utils::OS::setTorrentFileAssoc();
|
||||||
m_ui->checkAssociateTorrents->setChecked(Preferences::isTorrentFileAssocSet());
|
m_ui->checkAssociateTorrents->setChecked(Utils::OS::isTorrentFileAssocSet());
|
||||||
m_ui->checkAssociateTorrents->setEnabled(!m_ui->checkAssociateTorrents->isChecked());
|
m_ui->checkAssociateTorrents->setEnabled(!m_ui->checkAssociateTorrents->isChecked());
|
||||||
}
|
}
|
||||||
if (m_ui->checkAssociateMagnetLinks->isChecked())
|
if (m_ui->checkAssociateMagnetLinks->isChecked())
|
||||||
{
|
{
|
||||||
Preferences::setMagnetLinkAssoc();
|
Utils::OS::setMagnetLinkAssoc();
|
||||||
m_ui->checkAssociateMagnetLinks->setChecked(Preferences::isMagnetLinkAssocSet());
|
m_ui->checkAssociateMagnetLinks->setChecked(Utils::OS::isMagnetLinkAssocSet());
|
||||||
m_ui->checkAssociateMagnetLinks->setEnabled(!m_ui->checkAssociateMagnetLinks->isChecked());
|
m_ui->checkAssociateMagnetLinks->setEnabled(!m_ui->checkAssociateMagnetLinks->isChecked());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue