Allow to set qBittorrent as default program

Register qBittorrent as possible default program for .torrent files and
magnet links during install.

PR #19446.
This commit is contained in:
Vladimir Golovnev
2023-10-24 13:31:01 +03:00
committed by GitHub
parent 9de32a78f1
commit 6860c0d60d
10 changed files with 90 additions and 256 deletions

View File

@@ -885,23 +885,6 @@ int Application::exec()
#endif
m_window = new MainWindow(this, windowState);
delete m_startupProgressDialog;
#ifdef Q_OS_WIN
auto *pref = Preferences::instance();
if (!pref->neverCheckFileAssoc() && (!Utils::OS::isTorrentFileAssocSet() || !Utils::OS::isMagnetLinkAssocSet()))
{
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?")
, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
{
Utils::OS::setTorrentFileAssoc(true);
Utils::OS::setMagnetLinkAssoc(true);
}
else
{
pref->setNeverCheckFileAssoc();
}
}
#endif // Q_OS_WIN
#endif // DISABLE_GUI
#ifndef DISABLE_WEBUI

View File

@@ -1293,21 +1293,6 @@ void Preferences::setRecursiveDownloadEnabled(const bool enable)
setValue(u"Preferences/Advanced/DisableRecursiveDownload"_s, !enable);
}
#ifdef Q_OS_WIN
bool Preferences::neverCheckFileAssoc() const
{
return value(u"Preferences/Win32/NeverCheckFileAssocation"_s, false);
}
void Preferences::setNeverCheckFileAssoc(const bool check)
{
if (check == neverCheckFileAssoc())
return;
setValue(u"Preferences/Win32/NeverCheckFileAssocation"_s, check);
}
#endif // Q_OS_WIN
int Preferences::getTrackerPort() const
{
return value<int>(u"Preferences/Advanced/trackerPort"_s, 9000);

View File

@@ -287,11 +287,6 @@ public:
#endif
bool isRecursiveDownloadEnabled() const;
void setRecursiveDownloadEnabled(bool enable);
#ifdef Q_OS_WIN
bool neverCheckFileAssoc() const;
void setNeverCheckFileAssoc(bool check = true);
#endif
int getTrackerPort() const;
void setTrackerPort(int port);
bool isTrackerPortForwardingEnabled() const;

View File

@@ -34,18 +34,8 @@
#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"
@@ -114,81 +104,3 @@ void Utils::OS::setMagnetLinkAssoc()
::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

View File

@@ -40,11 +40,4 @@ namespace Utils::OS
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
}

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@@ -55,6 +56,7 @@
#include "base/utils/io.h"
#include "base/utils/misc.h"
#include "base/utils/net.h"
#include "base/utils/os.h"
#include "base/utils/password.h"
#include "base/utils/random.h"
#include "addnewtorrentdialog.h"
@@ -287,8 +289,6 @@ void OptionsDialog::loadBehaviorTabOptions()
#ifdef Q_OS_WIN
m_ui->checkStartup->setChecked(pref->WinStartup());
m_ui->checkAssociateTorrents->setChecked(Utils::OS::isTorrentFileAssocSet());
m_ui->checkAssociateMagnetLinks->setChecked(Utils::OS::isMagnetLinkAssocSet());
#endif
#ifdef Q_OS_MACOS
@@ -369,12 +369,23 @@ void OptionsDialog::loadBehaviorTabOptions()
connect(m_ui->checkPreventFromSuspendWhenDownloading, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkPreventFromSuspendWhenSeeding, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
#if defined(Q_OS_MACOS)
connect(m_ui->checkAssociateTorrents, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkAssociateMagnetLinks, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
#endif
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
connect(m_ui->checkProgramUpdates, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
#endif
#ifdef Q_OS_WIN
m_ui->assocPanel->hide();
#endif
#ifdef Q_OS_MAC
m_ui->defaultProgramPanel->hide();
#endif
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && !defined(QBT_USES_DBUS)
m_ui->checkPreventFromSuspendWhenDownloading->setDisabled(true);
m_ui->checkPreventFromSuspendWhenSeeding->setDisabled(true);
@@ -435,9 +446,6 @@ void OptionsDialog::saveBehaviorTabOptions() const
#ifdef Q_OS_WIN
pref->setWinStartup(WinStartup());
Utils::OS::setTorrentFileAssoc(m_ui->checkAssociateTorrents->isChecked());
Utils::OS::setMagnetLinkAssoc(m_ui->checkAssociateMagnetLinks->isChecked());
#endif
#ifndef Q_OS_MACOS

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or

View File

@@ -569,17 +569,60 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_28">
<item>
<widget class="QCheckBox" name="checkAssociateTorrents">
<property name="text">
<string>Use qBittorrent for .torrent files</string>
</property>
<widget class="QWidget" name="assocPanel" native="true">
<layout class="QVBoxLayout" name="assocPanelLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="checkAssociateTorrents">
<property name="text">
<string>Use qBittorrent for .torrent files</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkAssociateMagnetLinks">
<property name="text">
<string>Use qBittorrent for magnet links</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkAssociateMagnetLinks">
<property name="text">
<string>Use qBittorrent for magnet links</string>
</property>
<widget class="QWidget" name="defaultProgramPanel" native="true">
<layout class="QVBoxLayout" name="defaultProgramPanelLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="labelSetAssoc">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;To set qBittorrent as default program for .torrent files and/or Magnet links&lt;br/&gt;you can use &lt;span style=&quot; font-weight:600;&quot;&gt;Default Programs&lt;/span&gt; dialog from &lt;span style=&quot; font-weight:600;&quot;&gt;Control Panel&lt;/span&gt;.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>