Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / utils / os.cpp
blob8ee374e3de473c1fdda27e11f2dda9b8d02c13e8
1 /*
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.
31 #include "os.h"
33 #ifdef Q_OS_MACOS
34 #include <CoreServices/CoreServices.h>
35 #endif // Q_OS_MACOS
37 #ifdef Q_OS_WIN
38 #include <shlobj.h>
39 #endif // Q_OS_WIN
41 #include <QString>
43 #ifdef Q_OS_WIN
44 #include <QCoreApplication>
45 #include <QRegularExpression>
46 #include <QSettings>
47 #endif // Q_OS_WIN
49 #include "base/global.h"
50 #include "base/path.h"
52 #ifdef Q_OS_MACOS
53 namespace
55 const CFStringRef torrentExtension = CFSTR("torrent");
56 const CFStringRef magnetUrlScheme = CFSTR("magnet");
59 bool Utils::OS::isTorrentFileAssocSet()
61 bool isSet = false;
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);
75 return isSet;
78 void Utils::OS::setTorrentFileAssoc()
80 if (isTorrentFileAssocSet())
81 return;
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()
95 bool isSet = false;
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);
104 return isSet;
107 void Utils::OS::setMagnetLinkAssoc()
109 if (isMagnetLinkAssocSet())
110 return;
112 const CFStringRef myBundleId = ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
113 if (myBundleId != NULL)
114 ::LSSetDefaultHandlerForURLScheme(magnetUrlScheme, myBundleId);
116 #endif // Q_OS_MACOS
118 #ifdef Q_OS_WIN
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())
128 return;
130 QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
132 if (set)
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);
141 else
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())
156 return false;
158 const Path assocExe {exeRegMatch.captured(1)};
159 if (assocExe != Path(qApp->applicationFilePath()))
160 return false;
162 return true;
165 void Utils::OS::setMagnetLinkAssoc(const bool set)
167 if (set == isMagnetLinkAssocSet())
168 return;
170 QSettings settings(u"HKEY_CURRENT_USER\\Software\\Classes"_s, QSettings::NativeFormat);
172 if (set)
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());
185 else
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);
194 #endif // Q_OS_WIN