2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Mike Tzou (Chocobo1)
4 * Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com>
5 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
34 #include <CoreServices/CoreServices.h>
44 #include <QCoreApplication>
45 #include <QRegularExpression>
49 #include "base/global.h"
50 #include "base/path.h"
55 const CFStringRef torrentExtension
= CFSTR("torrent");
56 const CFStringRef magnetUrlScheme
= CFSTR("magnet");
59 bool Utils::OS::isTorrentFileAssocSet()
62 const CFStringRef torrentId
= ::UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension
, torrentExtension
, NULL
);
63 if (torrentId
!= NULL
)
65 const CFStringRef defaultHandlerId
= ::LSCopyDefaultRoleHandlerForContentType(torrentId
, kLSRolesViewer
);
66 if (defaultHandlerId
!= NULL
)
68 const CFStringRef myBundleId
= ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
69 if (myBundleId
!= NULL
)
70 isSet
= ::CFStringCompare(myBundleId
, defaultHandlerId
, 0) == kCFCompareEqualTo
;
71 ::CFRelease(defaultHandlerId
);
73 ::CFRelease(torrentId
);
78 void Utils::OS::setTorrentFileAssoc()
80 if (isTorrentFileAssocSet())
83 const CFStringRef torrentId
= ::UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension
, torrentExtension
, NULL
);
84 if (torrentId
!= NULL
)
86 const CFStringRef myBundleId
= ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
87 if (myBundleId
!= NULL
)
88 ::LSSetDefaultRoleHandlerForContentType(torrentId
, kLSRolesViewer
, myBundleId
);
89 ::CFRelease(torrentId
);
93 bool Utils::OS::isMagnetLinkAssocSet()
96 const CFStringRef defaultHandlerId
= ::LSCopyDefaultHandlerForURLScheme(magnetUrlScheme
);
97 if (defaultHandlerId
!= NULL
)
99 const CFStringRef myBundleId
= ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
100 if (myBundleId
!= NULL
)
101 isSet
= ::CFStringCompare(myBundleId
, defaultHandlerId
, 0) == kCFCompareEqualTo
;
102 ::CFRelease(defaultHandlerId
);
107 void Utils::OS::setMagnetLinkAssoc()
109 if (isMagnetLinkAssocSet())
112 const CFStringRef myBundleId
= ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
113 if (myBundleId
!= NULL
)
114 ::LSSetDefaultHandlerForURLScheme(magnetUrlScheme
, myBundleId
);
119 bool Utils::OS::isTorrentFileAssocSet()
121 const QSettings
settings(u
"HKEY_CURRENT_USER\\Software\\Classes"_s
, QSettings::NativeFormat
);
122 return settings
.value(u
".torrent/Default"_s
).toString() == u
"qBittorrent";
125 void Utils::OS::setTorrentFileAssoc(const bool set
)
127 if (set
== isTorrentFileAssocSet())
130 QSettings
settings(u
"HKEY_CURRENT_USER\\Software\\Classes"_s
, QSettings::NativeFormat
);
134 const QString oldProgId
= settings
.value(u
".torrent/Default"_s
).toString();
135 if (!oldProgId
.isEmpty() && (oldProgId
!= u
"qBittorrent"))
136 settings
.setValue((u
".torrent/OpenWithProgids/" + oldProgId
), QString());
138 settings
.setValue(u
".torrent/Default"_s
, u
"qBittorrent"_s
);
139 settings
.setValue(u
".torrent/Content Type"_s
, u
"application/x-bittorrent"_s
);
143 settings
.setValue(u
".torrent/Default"_s
, QString());
146 ::SHChangeNotify(SHCNE_ASSOCCHANGED
, SHCNF_IDLIST
, nullptr, nullptr);
149 bool Utils::OS::isMagnetLinkAssocSet()
151 const QSettings
settings(u
"HKEY_CURRENT_USER\\Software\\Classes"_s
, QSettings::NativeFormat
);
152 const QString shellCommand
= settings
.value(u
"magnet/shell/open/command/Default"_s
).toString();
154 const QRegularExpressionMatch exeRegMatch
= QRegularExpression(u
"\"([^\"]+)\".*"_s
).match(shellCommand
);
155 if (!exeRegMatch
.hasMatch())
158 const Path assocExe
{exeRegMatch
.captured(1)};
159 if (assocExe
!= Path(qApp
->applicationFilePath()))
165 void Utils::OS::setMagnetLinkAssoc(const bool set
)
167 if (set
== isMagnetLinkAssocSet())
170 QSettings
settings(u
"HKEY_CURRENT_USER\\Software\\Classes"_s
, QSettings::NativeFormat
);
174 const QString applicationFilePath
= Path(qApp
->applicationFilePath()).toString();
175 const QString commandStr
= u
'"' + applicationFilePath
+ u
"\" \"%1\"";
176 const QString iconStr
= u
'"' + applicationFilePath
+ u
"\",1";
178 settings
.setValue(u
"magnet/Default"_s
, u
"URL:Magnet link"_s
);
179 settings
.setValue(u
"magnet/Content Type"_s
, u
"application/x-magnet"_s
);
180 settings
.setValue(u
"magnet/DefaultIcon/Default"_s
, iconStr
);
181 settings
.setValue(u
"magnet/shell/Default"_s
, u
"open"_s
);
182 settings
.setValue(u
"magnet/shell/open/command/Default"_s
, commandStr
);
183 settings
.setValue(u
"magnet/URL Protocol"_s
, QString());
187 // only wipe values that are specific to qbt
188 settings
.setValue(u
"magnet/DefaultIcon/Default"_s
, QString());
189 settings
.setValue(u
"magnet/shell/open/command/Default"_s
, QString());
192 ::SHChangeNotify(SHCNE_ASSOCCHANGED
, SHCNF_IDLIST
, nullptr, nullptr);