Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / preferences.cpp
blob1af6cd76a183732b490a88ffe30394db2545d8b9
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "preferences.h"
32 #include <chrono>
34 #ifdef Q_OS_MACOS
35 #include <CoreServices/CoreServices.h>
36 #endif
37 #ifdef Q_OS_WIN
38 #include <shlobj.h>
39 #endif
41 #include <QCoreApplication>
42 #include <QDateTime>
43 #include <QDir>
44 #include <QList>
45 #include <QLocale>
46 #include <QNetworkCookie>
47 #include <QSettings>
48 #include <QSize>
49 #include <QTime>
50 #include <QVariant>
52 #ifdef Q_OS_WIN
53 #include <QRegularExpression>
54 #endif
56 #include "algorithm.h"
57 #include "global.h"
58 #include "settingsstorage.h"
59 #include "utils/fs.h"
61 Preferences *Preferences::m_instance = nullptr;
63 Preferences::Preferences() = default;
65 Preferences *Preferences::instance()
67 return m_instance;
70 void Preferences::initInstance()
72 if (!m_instance)
73 m_instance = new Preferences;
76 void Preferences::freeInstance()
78 delete m_instance;
79 m_instance = nullptr;
82 const QVariant Preferences::value(const QString &key, const QVariant &defaultValue) const
84 return SettingsStorage::instance()->loadValue(key, defaultValue);
87 void Preferences::setValue(const QString &key, const QVariant &value)
89 SettingsStorage::instance()->storeValue(key, value);
92 // General options
93 QString Preferences::getLocale() const
95 const QString localeName = value("Preferences/General/Locale").toString();
96 return (localeName.isEmpty() ? QLocale::system().name() : localeName);
99 void Preferences::setLocale(const QString &locale)
101 setValue("Preferences/General/Locale", locale);
104 bool Preferences::useCustomUITheme() const
106 return value("Preferences/General/UseCustomUITheme", false).toBool()
107 && !customUIThemePath().isEmpty();
110 void Preferences::setUseCustomUITheme(const bool use)
112 setValue("Preferences/General/UseCustomUITheme", use);
115 QString Preferences::customUIThemePath() const
117 return value("Preferences/General/CustomUIThemePath").toString();
120 void Preferences::setCustomUIThemePath(const QString &path)
122 setValue("Preferences/General/CustomUIThemePath", path);
125 bool Preferences::deleteTorrentFilesAsDefault() const
127 return value("Preferences/General/DeleteTorrentsFilesAsDefault", false).toBool();
130 void Preferences::setDeleteTorrentFilesAsDefault(const bool del)
132 setValue("Preferences/General/DeleteTorrentsFilesAsDefault", del);
135 bool Preferences::confirmOnExit() const
137 return value("Preferences/General/ExitConfirm", true).toBool();
140 void Preferences::setConfirmOnExit(const bool confirm)
142 setValue("Preferences/General/ExitConfirm", confirm);
145 bool Preferences::speedInTitleBar() const
147 return value("Preferences/General/SpeedInTitleBar", false).toBool();
150 void Preferences::showSpeedInTitleBar(const bool show)
152 setValue("Preferences/General/SpeedInTitleBar", show);
155 bool Preferences::useAlternatingRowColors() const
157 return value("Preferences/General/AlternatingRowColors", true).toBool();
160 void Preferences::setAlternatingRowColors(const bool b)
162 setValue("Preferences/General/AlternatingRowColors", b);
165 bool Preferences::getHideZeroValues() const
167 return value("Preferences/General/HideZeroValues", false).toBool();
170 void Preferences::setHideZeroValues(const bool b)
172 setValue("Preferences/General/HideZeroValues", b);
175 int Preferences::getHideZeroComboValues() const
177 return value("Preferences/General/HideZeroComboValues", 0).toInt();
180 void Preferences::setHideZeroComboValues(const int n)
182 setValue("Preferences/General/HideZeroComboValues", n);
185 // In Mac OS X the dock is sufficient for our needs so we disable the sys tray functionality.
186 // See extensive discussion in https://github.com/qbittorrent/qBittorrent/pull/3018
187 #ifndef Q_OS_MACOS
188 bool Preferences::systrayIntegration() const
190 return value("Preferences/General/SystrayEnabled", true).toBool();
193 void Preferences::setSystrayIntegration(const bool enabled)
195 setValue("Preferences/General/SystrayEnabled", enabled);
198 bool Preferences::minimizeToTray() const
200 return value("Preferences/General/MinimizeToTray", false).toBool();
203 void Preferences::setMinimizeToTray(const bool b)
205 setValue("Preferences/General/MinimizeToTray", b);
208 bool Preferences::minimizeToTrayNotified() const
210 return value("Preferences/General/MinimizeToTrayNotified", false).toBool();
213 void Preferences::setMinimizeToTrayNotified(const bool b)
215 setValue("Preferences/General/MinimizeToTrayNotified", b);
218 bool Preferences::closeToTray() const
220 return value("Preferences/General/CloseToTray", true).toBool();
223 void Preferences::setCloseToTray(const bool b)
225 setValue("Preferences/General/CloseToTray", b);
228 bool Preferences::closeToTrayNotified() const
230 return value("Preferences/General/CloseToTrayNotified", false).toBool();
233 void Preferences::setCloseToTrayNotified(const bool b)
235 setValue("Preferences/General/CloseToTrayNotified", b);
238 bool Preferences::iconsInMenusEnabled() const
240 return value("Preferences/Advanced/EnableIconsInMenus", true).toBool();
243 void Preferences::setIconsInMenusEnabled(const bool enable)
245 setValue("Preferences/Advanced/EnableIconsInMenus", enable);
247 #endif // Q_OS_MACOS
249 bool Preferences::isToolbarDisplayed() const
251 return value("Preferences/General/ToolbarDisplayed", true).toBool();
254 void Preferences::setToolbarDisplayed(const bool displayed)
256 setValue("Preferences/General/ToolbarDisplayed", displayed);
259 bool Preferences::isStatusbarDisplayed() const
261 return value("Preferences/General/StatusbarDisplayed", true).toBool();
264 void Preferences::setStatusbarDisplayed(const bool displayed)
266 setValue("Preferences/General/StatusbarDisplayed", displayed);
269 bool Preferences::startMinimized() const
271 return value("Preferences/General/StartMinimized", false).toBool();
274 void Preferences::setStartMinimized(const bool b)
276 setValue("Preferences/General/StartMinimized", b);
279 bool Preferences::isSplashScreenDisabled() const
281 return value("Preferences/General/NoSplashScreen", true).toBool();
284 void Preferences::setSplashScreenDisabled(const bool b)
286 setValue("Preferences/General/NoSplashScreen", b);
289 // Preventing from system suspend while active torrents are presented.
290 bool Preferences::preventFromSuspendWhenDownloading() const
292 return value("Preferences/General/PreventFromSuspendWhenDownloading", false).toBool();
295 void Preferences::setPreventFromSuspendWhenDownloading(const bool b)
297 setValue("Preferences/General/PreventFromSuspendWhenDownloading", b);
300 bool Preferences::preventFromSuspendWhenSeeding() const
302 return value("Preferences/General/PreventFromSuspendWhenSeeding", false).toBool();
305 void Preferences::setPreventFromSuspendWhenSeeding(const bool b)
307 setValue("Preferences/General/PreventFromSuspendWhenSeeding", b);
310 #ifdef Q_OS_WIN
311 bool Preferences::WinStartup() const
313 QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
314 return settings.contains("qBittorrent");
317 void Preferences::setWinStartup(const bool b)
319 QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
320 if (b)
322 const QString binPath = '"' + Utils::Fs::toNativePath(qApp->applicationFilePath()) + '"';
323 settings.setValue("qBittorrent", binPath);
325 else
327 settings.remove("qBittorrent");
330 #endif // Q_OS_WIN
332 // Downloads
333 QString Preferences::lastLocationPath() const
335 return Utils::Fs::toUniformPath(value("Preferences/Downloads/LastLocationPath").toString());
338 void Preferences::setLastLocationPath(const QString &path)
340 setValue("Preferences/Downloads/LastLocationPath", Utils::Fs::toUniformPath(path));
343 QString Preferences::getScanDirsLastPath() const
345 return Utils::Fs::toUniformPath(value("Preferences/Downloads/ScanDirsLastPath").toString());
348 void Preferences::setScanDirsLastPath(const QString &path)
350 setValue("Preferences/Downloads/ScanDirsLastPath", Utils::Fs::toUniformPath(path));
353 bool Preferences::isMailNotificationEnabled() const
355 return value("Preferences/MailNotification/enabled", false).toBool();
358 void Preferences::setMailNotificationEnabled(const bool enabled)
360 setValue("Preferences/MailNotification/enabled", enabled);
363 QString Preferences::getMailNotificationSender() const
365 return value("Preferences/MailNotification/sender", "qBittorrent_notification@example.com").toString();
368 void Preferences::setMailNotificationSender(const QString &mail)
370 setValue("Preferences/MailNotification/sender", mail);
373 QString Preferences::getMailNotificationEmail() const
375 return value("Preferences/MailNotification/email").toString();
378 void Preferences::setMailNotificationEmail(const QString &mail)
380 setValue("Preferences/MailNotification/email", mail);
383 QString Preferences::getMailNotificationSMTP() const
385 return value("Preferences/MailNotification/smtp_server", "smtp.changeme.com").toString();
388 void Preferences::setMailNotificationSMTP(const QString &smtp_server)
390 setValue("Preferences/MailNotification/smtp_server", smtp_server);
393 bool Preferences::getMailNotificationSMTPSSL() const
395 return value("Preferences/MailNotification/req_ssl", false).toBool();
398 void Preferences::setMailNotificationSMTPSSL(const bool use)
400 setValue("Preferences/MailNotification/req_ssl", use);
403 bool Preferences::getMailNotificationSMTPAuth() const
405 return value("Preferences/MailNotification/req_auth", false).toBool();
408 void Preferences::setMailNotificationSMTPAuth(const bool use)
410 setValue("Preferences/MailNotification/req_auth", use);
413 QString Preferences::getMailNotificationSMTPUsername() const
415 return value("Preferences/MailNotification/username").toString();
418 void Preferences::setMailNotificationSMTPUsername(const QString &username)
420 setValue("Preferences/MailNotification/username", username);
423 QString Preferences::getMailNotificationSMTPPassword() const
425 return value("Preferences/MailNotification/password").toString();
428 void Preferences::setMailNotificationSMTPPassword(const QString &password)
430 setValue("Preferences/MailNotification/password", password);
433 int Preferences::getActionOnDblClOnTorrentDl() const
435 return value("Preferences/Downloads/DblClOnTorDl", 0).toInt();
438 void Preferences::setActionOnDblClOnTorrentDl(const int act)
440 setValue("Preferences/Downloads/DblClOnTorDl", act);
443 int Preferences::getActionOnDblClOnTorrentFn() const
445 return value("Preferences/Downloads/DblClOnTorFn", 1).toInt();
448 void Preferences::setActionOnDblClOnTorrentFn(const int act)
450 setValue("Preferences/Downloads/DblClOnTorFn", act);
453 QTime Preferences::getSchedulerStartTime() const
455 return value("Preferences/Scheduler/start_time", QTime(8,0)).toTime();
458 void Preferences::setSchedulerStartTime(const QTime &time)
460 setValue("Preferences/Scheduler/start_time", time);
463 QTime Preferences::getSchedulerEndTime() const
465 return value("Preferences/Scheduler/end_time", QTime(20,0)).toTime();
468 void Preferences::setSchedulerEndTime(const QTime &time)
470 setValue("Preferences/Scheduler/end_time", time);
473 SchedulerDays Preferences::getSchedulerDays() const
475 return static_cast<SchedulerDays>(value("Preferences/Scheduler/days", EVERY_DAY).toInt());
478 void Preferences::setSchedulerDays(const SchedulerDays days)
480 setValue("Preferences/Scheduler/days", static_cast<int>(days));
483 // Search
484 bool Preferences::isSearchEnabled() const
486 return value("Preferences/Search/SearchEnabled", false).toBool();
489 void Preferences::setSearchEnabled(const bool enabled)
491 setValue("Preferences/Search/SearchEnabled", enabled);
494 bool Preferences::isWebUiEnabled() const
496 #ifdef DISABLE_GUI
497 return true;
498 #else
499 return value("Preferences/WebUI/Enabled", false).toBool();
500 #endif
503 void Preferences::setWebUiEnabled(const bool enabled)
505 setValue("Preferences/WebUI/Enabled", enabled);
508 bool Preferences::isWebUiLocalAuthEnabled() const
510 return value("Preferences/WebUI/LocalHostAuth", true).toBool();
513 void Preferences::setWebUiLocalAuthEnabled(const bool enabled)
515 setValue("Preferences/WebUI/LocalHostAuth", enabled);
518 bool Preferences::isWebUiAuthSubnetWhitelistEnabled() const
520 return value("Preferences/WebUI/AuthSubnetWhitelistEnabled", false).toBool();
523 void Preferences::setWebUiAuthSubnetWhitelistEnabled(const bool enabled)
525 setValue("Preferences/WebUI/AuthSubnetWhitelistEnabled", enabled);
528 QVector<Utils::Net::Subnet> Preferences::getWebUiAuthSubnetWhitelist() const
530 const QStringList subnets = value("Preferences/WebUI/AuthSubnetWhitelist").toStringList();
532 QVector<Utils::Net::Subnet> ret;
533 ret.reserve(subnets.size());
535 for (const QString &rawSubnet : subnets)
537 bool ok = false;
538 const Utils::Net::Subnet subnet = Utils::Net::parseSubnet(rawSubnet.trimmed(), &ok);
539 if (ok)
540 ret.append(subnet);
543 return ret;
546 void Preferences::setWebUiAuthSubnetWhitelist(QStringList subnets)
548 Algorithm::removeIf(subnets, [](const QString &subnet)
550 bool ok = false;
551 Utils::Net::parseSubnet(subnet.trimmed(), &ok);
552 return !ok;
555 setValue("Preferences/WebUI/AuthSubnetWhitelist", subnets);
558 QString Preferences::getServerDomains() const
560 return value("Preferences/WebUI/ServerDomains", QChar('*')).toString();
563 void Preferences::setServerDomains(const QString &str)
565 setValue("Preferences/WebUI/ServerDomains", str);
568 QString Preferences::getWebUiAddress() const
570 return value("Preferences/WebUI/Address", QChar('*')).toString().trimmed();
573 void Preferences::setWebUiAddress(const QString &addr)
575 setValue("Preferences/WebUI/Address", addr.trimmed());
578 quint16 Preferences::getWebUiPort() const
580 return value("Preferences/WebUI/Port", 8080).toInt();
583 void Preferences::setWebUiPort(const quint16 port)
585 setValue("Preferences/WebUI/Port", port);
588 bool Preferences::useUPnPForWebUIPort() const
590 #ifdef DISABLE_GUI
591 return value("Preferences/WebUI/UseUPnP", true).toBool();
592 #else
593 return value("Preferences/WebUI/UseUPnP", false).toBool();
594 #endif
597 void Preferences::setUPnPForWebUIPort(const bool enabled)
599 setValue("Preferences/WebUI/UseUPnP", enabled);
602 QString Preferences::getWebUiUsername() const
604 return value("Preferences/WebUI/Username", "admin").toString();
607 void Preferences::setWebUiUsername(const QString &username)
609 setValue("Preferences/WebUI/Username", username);
612 QByteArray Preferences::getWebUIPassword() const
614 // default: adminadmin
615 const QByteArray defaultValue = "ARQ77eY1NUZaQsuDHbIMCA==:0WMRkYTUWVT9wVvdDtHAjU9b3b7uB8NR1Gur2hmQCvCDpm39Q+PsJRJPaCU51dEiz+dTzh8qbPsL8WkFljQYFQ==";
616 return value("Preferences/WebUI/Password_PBKDF2", defaultValue).toByteArray();
619 void Preferences::setWebUIPassword(const QByteArray &password)
621 setValue("Preferences/WebUI/Password_PBKDF2", password);
624 int Preferences::getWebUIMaxAuthFailCount() const
626 return value("Preferences/WebUI/MaxAuthenticationFailCount", 5).toInt();
629 void Preferences::setWebUIMaxAuthFailCount(const int count)
631 setValue("Preferences/WebUI/MaxAuthenticationFailCount", count);
634 std::chrono::seconds Preferences::getWebUIBanDuration() const
636 return std::chrono::seconds {value("Preferences/WebUI/BanDuration", 3600).toInt()};
639 void Preferences::setWebUIBanDuration(const std::chrono::seconds duration)
641 setValue("Preferences/WebUI/BanDuration", static_cast<int>(duration.count()));
644 int Preferences::getWebUISessionTimeout() const
646 return value("Preferences/WebUI/SessionTimeout", 3600).toInt();
649 void Preferences::setWebUISessionTimeout(const int timeout)
651 setValue("Preferences/WebUI/SessionTimeout", timeout);
654 bool Preferences::isWebUiClickjackingProtectionEnabled() const
656 return value("Preferences/WebUI/ClickjackingProtection", true).toBool();
659 void Preferences::setWebUiClickjackingProtectionEnabled(const bool enabled)
661 setValue("Preferences/WebUI/ClickjackingProtection", enabled);
664 bool Preferences::isWebUiCSRFProtectionEnabled() const
666 return value("Preferences/WebUI/CSRFProtection", true).toBool();
669 void Preferences::setWebUiCSRFProtectionEnabled(const bool enabled)
671 setValue("Preferences/WebUI/CSRFProtection", enabled);
674 bool Preferences::isWebUiSecureCookieEnabled() const
676 return value("Preferences/WebUI/SecureCookie", true).toBool();
679 void Preferences::setWebUiSecureCookieEnabled(const bool enabled)
681 setValue("Preferences/WebUI/SecureCookie", enabled);
684 bool Preferences::isWebUIHostHeaderValidationEnabled() const
686 return value("Preferences/WebUI/HostHeaderValidation", true).toBool();
689 void Preferences::setWebUIHostHeaderValidationEnabled(const bool enabled)
691 setValue("Preferences/WebUI/HostHeaderValidation", enabled);
694 bool Preferences::isWebUiHttpsEnabled() const
696 return value("Preferences/WebUI/HTTPS/Enabled", false).toBool();
699 void Preferences::setWebUiHttpsEnabled(const bool enabled)
701 setValue("Preferences/WebUI/HTTPS/Enabled", enabled);
704 QString Preferences::getWebUIHttpsCertificatePath() const
706 return value("Preferences/WebUI/HTTPS/CertificatePath").toString();
709 void Preferences::setWebUIHttpsCertificatePath(const QString &path)
711 setValue("Preferences/WebUI/HTTPS/CertificatePath", path);
714 QString Preferences::getWebUIHttpsKeyPath() const
716 return value("Preferences/WebUI/HTTPS/KeyPath").toString();
719 void Preferences::setWebUIHttpsKeyPath(const QString &path)
721 setValue("Preferences/WebUI/HTTPS/KeyPath", path);
724 bool Preferences::isAltWebUiEnabled() const
726 return value("Preferences/WebUI/AlternativeUIEnabled", false).toBool();
729 void Preferences::setAltWebUiEnabled(const bool enabled)
731 setValue("Preferences/WebUI/AlternativeUIEnabled", enabled);
734 QString Preferences::getWebUiRootFolder() const
736 return value("Preferences/WebUI/RootFolder").toString();
739 void Preferences::setWebUiRootFolder(const QString &path)
741 setValue("Preferences/WebUI/RootFolder", path);
744 bool Preferences::isWebUICustomHTTPHeadersEnabled() const
746 return value("Preferences/WebUI/CustomHTTPHeadersEnabled", false).toBool();
749 void Preferences::setWebUICustomHTTPHeadersEnabled(const bool enabled)
751 setValue("Preferences/WebUI/CustomHTTPHeadersEnabled", enabled);
754 QString Preferences::getWebUICustomHTTPHeaders() const
756 return value("Preferences/WebUI/CustomHTTPHeaders").toString();
759 void Preferences::setWebUICustomHTTPHeaders(const QString &headers)
761 setValue("Preferences/WebUI/CustomHTTPHeaders", headers);
764 bool Preferences::isWebUIReverseProxySupportEnabled() const
766 return value("Preferences/WebUI/ReverseProxySupportEnabled", false).toBool();
769 void Preferences::setWebUIReverseProxySupportEnabled(const bool enabled)
771 setValue("Preferences/WebUI/ReverseProxySupportEnabled", enabled);
774 QString Preferences::getWebUITrustedReverseProxiesList() const
776 return value("Preferences/WebUI/TrustedReverseProxiesList").toString();
779 void Preferences::setWebUITrustedReverseProxiesList(const QString &addr)
781 setValue("Preferences/WebUI/TrustedReverseProxiesList", addr);
784 bool Preferences::isDynDNSEnabled() const
786 return value("Preferences/DynDNS/Enabled", false).toBool();
789 void Preferences::setDynDNSEnabled(const bool enabled)
791 setValue("Preferences/DynDNS/Enabled", enabled);
794 DNS::Service Preferences::getDynDNSService() const
796 return DNS::Service(value("Preferences/DynDNS/Service", DNS::DYNDNS).toInt());
799 void Preferences::setDynDNSService(const int service)
801 setValue("Preferences/DynDNS/Service", service);
804 QString Preferences::getDynDomainName() const
806 return value("Preferences/DynDNS/DomainName", "changeme.dyndns.org").toString();
809 void Preferences::setDynDomainName(const QString &name)
811 setValue("Preferences/DynDNS/DomainName", name);
814 QString Preferences::getDynDNSUsername() const
816 return value("Preferences/DynDNS/Username").toString();
819 void Preferences::setDynDNSUsername(const QString &username)
821 setValue("Preferences/DynDNS/Username", username);
824 QString Preferences::getDynDNSPassword() const
826 return value("Preferences/DynDNS/Password").toString();
829 void Preferences::setDynDNSPassword(const QString &password)
831 setValue("Preferences/DynDNS/Password", password);
834 // Advanced settings
835 QByteArray Preferences::getUILockPassword() const
837 return value("Locking/password_PBKDF2").toByteArray();
840 void Preferences::setUILockPassword(const QByteArray &password)
842 setValue("Locking/password_PBKDF2", password);
845 bool Preferences::isUILocked() const
847 return value("Locking/locked", false).toBool();
850 void Preferences::setUILocked(const bool locked)
852 setValue("Locking/locked", locked);
855 bool Preferences::isAutoRunEnabled() const
857 return value("AutoRun/enabled", false).toBool();
860 void Preferences::setAutoRunEnabled(const bool enabled)
862 setValue("AutoRun/enabled", enabled);
865 QString Preferences::getAutoRunProgram() const
867 return value("AutoRun/program").toString();
870 void Preferences::setAutoRunProgram(const QString &program)
872 setValue("AutoRun/program", program);
875 #if defined(Q_OS_WIN)
876 bool Preferences::isAutoRunConsoleEnabled() const
878 return value("AutoRun/ConsoleEnabled", false).toBool();
881 void Preferences::setAutoRunConsoleEnabled(const bool enabled)
883 setValue("AutoRun/ConsoleEnabled", enabled);
885 #endif
887 bool Preferences::shutdownWhenDownloadsComplete() const
889 return value("Preferences/Downloads/AutoShutDownOnCompletion", false).toBool();
892 void Preferences::setShutdownWhenDownloadsComplete(const bool shutdown)
894 setValue("Preferences/Downloads/AutoShutDownOnCompletion", shutdown);
897 bool Preferences::suspendWhenDownloadsComplete() const
899 return value("Preferences/Downloads/AutoSuspendOnCompletion", false).toBool();
902 void Preferences::setSuspendWhenDownloadsComplete(const bool suspend)
904 setValue("Preferences/Downloads/AutoSuspendOnCompletion", suspend);
907 bool Preferences::hibernateWhenDownloadsComplete() const
909 return value("Preferences/Downloads/AutoHibernateOnCompletion", false).toBool();
912 void Preferences::setHibernateWhenDownloadsComplete(const bool hibernate)
914 setValue("Preferences/Downloads/AutoHibernateOnCompletion", hibernate);
917 bool Preferences::shutdownqBTWhenDownloadsComplete() const
919 return value("Preferences/Downloads/AutoShutDownqBTOnCompletion", false).toBool();
922 void Preferences::setShutdownqBTWhenDownloadsComplete(const bool shutdown)
924 setValue("Preferences/Downloads/AutoShutDownqBTOnCompletion", shutdown);
927 bool Preferences::dontConfirmAutoExit() const
929 return value("ShutdownConfirmDlg/DontConfirmAutoExit", false).toBool();
932 void Preferences::setDontConfirmAutoExit(const bool dontConfirmAutoExit)
934 setValue("ShutdownConfirmDlg/DontConfirmAutoExit", dontConfirmAutoExit);
937 bool Preferences::recheckTorrentsOnCompletion() const
939 return value("Preferences/Advanced/RecheckOnCompletion", false).toBool();
942 void Preferences::recheckTorrentsOnCompletion(const bool recheck)
944 setValue("Preferences/Advanced/RecheckOnCompletion", recheck);
947 bool Preferences::resolvePeerCountries() const
949 return value("Preferences/Connection/ResolvePeerCountries", true).toBool();
952 void Preferences::resolvePeerCountries(const bool resolve)
954 setValue("Preferences/Connection/ResolvePeerCountries", resolve);
957 bool Preferences::resolvePeerHostNames() const
959 return value("Preferences/Connection/ResolvePeerHostNames", false).toBool();
962 void Preferences::resolvePeerHostNames(const bool resolve)
964 setValue("Preferences/Connection/ResolvePeerHostNames", resolve);
967 #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
968 bool Preferences::useSystemIconTheme() const
970 return value("Preferences/Advanced/useSystemIconTheme", true).toBool();
973 void Preferences::useSystemIconTheme(const bool enabled)
975 setValue("Preferences/Advanced/useSystemIconTheme", enabled);
977 #endif
979 bool Preferences::recursiveDownloadDisabled() const
981 return value("Preferences/Advanced/DisableRecursiveDownload", false).toBool();
984 void Preferences::disableRecursiveDownload(const bool disable)
986 setValue("Preferences/Advanced/DisableRecursiveDownload", disable);
989 #ifdef Q_OS_WIN
990 bool Preferences::neverCheckFileAssoc() const
992 return value("Preferences/Win32/NeverCheckFileAssocation", false).toBool();
995 void Preferences::setNeverCheckFileAssoc(const bool check)
997 setValue("Preferences/Win32/NeverCheckFileAssocation", check);
1000 bool Preferences::isTorrentFileAssocSet()
1002 const QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat);
1003 if (settings.value(".torrent/Default").toString() != "qBittorrent")
1005 qDebug(".torrent != qBittorrent");
1006 return false;
1009 return true;
1012 bool Preferences::isMagnetLinkAssocSet()
1014 const QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat);
1016 // Check magnet link assoc
1017 const QString shellCommand = Utils::Fs::toNativePath(settings.value("magnet/shell/open/command/Default", "").toString());
1019 const QRegularExpressionMatch exeRegMatch = QRegularExpression("\"([^\"]+)\".*").match(shellCommand);
1020 if (!exeRegMatch.hasMatch())
1021 return false;
1023 const QString assocExe = exeRegMatch.captured(1);
1024 if (assocExe.compare(Utils::Fs::toNativePath(qApp->applicationFilePath()), Qt::CaseInsensitive) != 0)
1025 return false;
1027 return true;
1030 void Preferences::setTorrentFileAssoc(const bool set)
1032 QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat);
1034 // .Torrent association
1035 if (set)
1037 const QString oldProgId = settings.value(".torrent/Default").toString();
1038 if (!oldProgId.isEmpty() && (oldProgId != "qBittorrent"))
1039 settings.setValue(".torrent/OpenWithProgids/" + oldProgId, "");
1040 settings.setValue(".torrent/Default", "qBittorrent");
1042 else if (isTorrentFileAssocSet())
1044 settings.setValue(".torrent/Default", "");
1047 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
1050 void Preferences::setMagnetLinkAssoc(const bool set)
1052 QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat);
1054 // Magnet association
1055 if (set)
1057 const QString commandStr = '"' + qApp->applicationFilePath() + "\" \"%1\"";
1058 const QString iconStr = '"' + qApp->applicationFilePath() + "\",1";
1060 settings.setValue("magnet/Default", "URL:Magnet link");
1061 settings.setValue("magnet/Content Type", "application/x-magnet");
1062 settings.setValue("magnet/URL Protocol", "");
1063 settings.setValue("magnet/DefaultIcon/Default", Utils::Fs::toNativePath(iconStr));
1064 settings.setValue("magnet/shell/Default", "open");
1065 settings.setValue("magnet/shell/open/command/Default", Utils::Fs::toNativePath(commandStr));
1067 else if (isMagnetLinkAssocSet())
1069 settings.remove("magnet");
1072 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
1074 #endif // Q_OS_WIN
1076 #ifdef Q_OS_MACOS
1077 namespace
1079 const CFStringRef torrentExtension = CFSTR("torrent");
1080 const CFStringRef magnetUrlScheme = CFSTR("magnet");
1083 bool Preferences::isTorrentFileAssocSet()
1085 bool isSet = false;
1086 const CFStringRef torrentId = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, torrentExtension, NULL);
1087 if (torrentId != NULL)
1089 const CFStringRef defaultHandlerId = LSCopyDefaultRoleHandlerForContentType(torrentId, kLSRolesViewer);
1090 if (defaultHandlerId != NULL)
1092 const CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle());
1093 isSet = CFStringCompare(myBundleId, defaultHandlerId, 0) == kCFCompareEqualTo;
1094 CFRelease(defaultHandlerId);
1096 CFRelease(torrentId);
1098 return isSet;
1101 bool Preferences::isMagnetLinkAssocSet()
1103 bool isSet = false;
1104 const CFStringRef defaultHandlerId = LSCopyDefaultHandlerForURLScheme(magnetUrlScheme);
1105 if (defaultHandlerId != NULL)
1107 const CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle());
1108 isSet = CFStringCompare(myBundleId, defaultHandlerId, 0) == kCFCompareEqualTo;
1109 CFRelease(defaultHandlerId);
1111 return isSet;
1114 void Preferences::setTorrentFileAssoc()
1116 if (isTorrentFileAssocSet())
1117 return;
1118 const CFStringRef torrentId = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, torrentExtension, NULL);
1119 if (torrentId != NULL)
1121 const CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle());
1122 LSSetDefaultRoleHandlerForContentType(torrentId, kLSRolesViewer, myBundleId);
1123 CFRelease(torrentId);
1127 void Preferences::setMagnetLinkAssoc()
1129 if (isMagnetLinkAssocSet())
1130 return;
1131 const CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle());
1132 LSSetDefaultHandlerForURLScheme(magnetUrlScheme, myBundleId);
1134 #endif // Q_OS_MACOS
1136 int Preferences::getTrackerPort() const
1138 return value("Preferences/Advanced/trackerPort", 9000).toInt();
1141 void Preferences::setTrackerPort(const int port)
1143 setValue("Preferences/Advanced/trackerPort", port);
1146 #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
1147 bool Preferences::isUpdateCheckEnabled() const
1149 return value("Preferences/Advanced/updateCheck", true).toBool();
1152 void Preferences::setUpdateCheckEnabled(const bool enabled)
1154 setValue("Preferences/Advanced/updateCheck", enabled);
1156 #endif
1158 bool Preferences::confirmTorrentDeletion() const
1160 return value("Preferences/Advanced/confirmTorrentDeletion", true).toBool();
1163 void Preferences::setConfirmTorrentDeletion(const bool enabled)
1165 setValue("Preferences/Advanced/confirmTorrentDeletion", enabled);
1168 bool Preferences::confirmTorrentRecheck() const
1170 return value("Preferences/Advanced/confirmTorrentRecheck", true).toBool();
1173 void Preferences::setConfirmTorrentRecheck(const bool enabled)
1175 setValue("Preferences/Advanced/confirmTorrentRecheck", enabled);
1178 bool Preferences::confirmRemoveAllTags() const
1180 return value("Preferences/Advanced/confirmRemoveAllTags", true).toBool();
1183 void Preferences::setConfirmRemoveAllTags(const bool enabled)
1185 setValue("Preferences/Advanced/confirmRemoveAllTags", enabled);
1188 #ifndef Q_OS_MACOS
1189 TrayIcon::Style Preferences::trayIconStyle() const
1191 return TrayIcon::Style(value("Preferences/Advanced/TrayIconStyle", TrayIcon::NORMAL).toInt());
1194 void Preferences::setTrayIconStyle(const TrayIcon::Style style)
1196 setValue("Preferences/Advanced/TrayIconStyle", style);
1198 #endif
1200 // Stuff that don't appear in the Options GUI but are saved
1201 // in the same file.
1203 QDateTime Preferences::getDNSLastUpd() const
1205 return value("DNSUpdater/lastUpdateTime").toDateTime();
1208 void Preferences::setDNSLastUpd(const QDateTime &date)
1210 setValue("DNSUpdater/lastUpdateTime", date);
1213 QString Preferences::getDNSLastIP() const
1215 return value("DNSUpdater/lastIP").toString();
1218 void Preferences::setDNSLastIP(const QString &ip)
1220 setValue("DNSUpdater/lastIP", ip);
1223 bool Preferences::getAcceptedLegal() const
1225 return value("LegalNotice/Accepted", false).toBool();
1228 void Preferences::setAcceptedLegal(const bool accepted)
1230 setValue("LegalNotice/Accepted", accepted);
1233 QByteArray Preferences::getMainGeometry() const
1235 return value("MainWindow/geometry").toByteArray();
1238 void Preferences::setMainGeometry(const QByteArray &geometry)
1240 setValue("MainWindow/geometry", geometry);
1243 QByteArray Preferences::getMainVSplitterState() const
1245 return value("MainWindow/qt5/vsplitterState").toByteArray();
1248 void Preferences::setMainVSplitterState(const QByteArray &state)
1250 setValue("MainWindow/qt5/vsplitterState", state);
1253 QString Preferences::getMainLastDir() const
1255 return value("MainWindowLastDir", QDir::homePath()).toString();
1258 void Preferences::setMainLastDir(const QString &path)
1260 setValue("MainWindowLastDir", path);
1263 QByteArray Preferences::getPeerListState() const
1265 return value("TorrentProperties/Peers/qt5/PeerListState").toByteArray();
1268 void Preferences::setPeerListState(const QByteArray &state)
1270 setValue("TorrentProperties/Peers/qt5/PeerListState", state);
1273 QString Preferences::getPropSplitterSizes() const
1275 return value("TorrentProperties/SplitterSizes").toString();
1278 void Preferences::setPropSplitterSizes(const QString &sizes)
1280 setValue("TorrentProperties/SplitterSizes", sizes);
1283 QByteArray Preferences::getPropFileListState() const
1285 return value("TorrentProperties/qt5/FilesListState").toByteArray();
1288 void Preferences::setPropFileListState(const QByteArray &state)
1290 setValue("TorrentProperties/qt5/FilesListState", state);
1293 int Preferences::getPropCurTab() const
1295 return value("TorrentProperties/CurrentTab", -1).toInt();
1298 void Preferences::setPropCurTab(const int tab)
1300 setValue("TorrentProperties/CurrentTab", tab);
1303 bool Preferences::getPropVisible() const
1305 return value("TorrentProperties/Visible", false).toBool();
1308 void Preferences::setPropVisible(const bool visible)
1310 setValue("TorrentProperties/Visible", visible);
1313 QByteArray Preferences::getPropTrackerListState() const
1315 return value("TorrentProperties/Trackers/qt5/TrackerListState").toByteArray();
1318 void Preferences::setPropTrackerListState(const QByteArray &state)
1320 setValue("TorrentProperties/Trackers/qt5/TrackerListState", state);
1323 QSize Preferences::getRssGeometrySize() const
1325 return value("RssFeedDownloader/geometrySize").toSize();
1328 void Preferences::setRssGeometrySize(const QSize &geometry)
1330 setValue("RssFeedDownloader/geometrySize", geometry);
1333 QByteArray Preferences::getRssHSplitterSizes() const
1335 return value("RssFeedDownloader/qt5/hsplitterSizes").toByteArray();
1338 void Preferences::setRssHSplitterSizes(const QByteArray &sizes)
1340 setValue("RssFeedDownloader/qt5/hsplitterSizes", sizes);
1343 QStringList Preferences::getRssOpenFolders() const
1345 return value("GUI/RSSWidget/OpenedFolders").toStringList();
1348 void Preferences::setRssOpenFolders(const QStringList &folders)
1350 setValue("GUI/RSSWidget/OpenedFolders", folders);
1353 QByteArray Preferences::getRssSideSplitterState() const
1355 return value("GUI/RSSWidget/qt5/splitter_h").toByteArray();
1358 void Preferences::setRssSideSplitterState(const QByteArray &state)
1360 setValue("GUI/RSSWidget/qt5/splitter_h", state);
1363 QByteArray Preferences::getRssMainSplitterState() const
1365 return value("GUI/RSSWidget/qt5/splitterMain").toByteArray();
1368 void Preferences::setRssMainSplitterState(const QByteArray &state)
1370 setValue("GUI/RSSWidget/qt5/splitterMain", state);
1373 QByteArray Preferences::getSearchTabHeaderState() const
1375 return value("SearchTab/qt5/HeaderState").toByteArray();
1378 void Preferences::setSearchTabHeaderState(const QByteArray &state)
1380 setValue("SearchTab/qt5/HeaderState", state);
1383 bool Preferences::getRegexAsFilteringPatternForSearchJob() const
1385 return value("SearchTab/UseRegexAsFilteringPattern", false).toBool();
1388 void Preferences::setRegexAsFilteringPatternForSearchJob(const bool checked)
1390 setValue("SearchTab/UseRegexAsFilteringPattern", checked);
1393 QStringList Preferences::getSearchEngDisabled() const
1395 return value("SearchEngines/disabledEngines").toStringList();
1398 void Preferences::setSearchEngDisabled(const QStringList &engines)
1400 setValue("SearchEngines/disabledEngines", engines);
1403 QString Preferences::getTorImportLastContentDir() const
1405 return value("TorrentImport/LastContentDir", QDir::homePath()).toString();
1408 void Preferences::setTorImportLastContentDir(const QString &path)
1410 setValue("TorrentImport/LastContentDir", path);
1413 QByteArray Preferences::getTorImportGeometry() const
1415 return value("TorrentImportDlg/dimensions").toByteArray();
1418 void Preferences::setTorImportGeometry(const QByteArray &geometry)
1420 setValue("TorrentImportDlg/dimensions", geometry);
1423 bool Preferences::getStatusFilterState() const
1425 return value("TransferListFilters/statusFilterState", true).toBool();
1428 void Preferences::setStatusFilterState(const bool checked)
1430 setValue("TransferListFilters/statusFilterState", checked);
1433 bool Preferences::getCategoryFilterState() const
1435 return value("TransferListFilters/CategoryFilterState", true).toBool();
1438 void Preferences::setCategoryFilterState(const bool checked)
1440 setValue("TransferListFilters/CategoryFilterState", checked);
1443 bool Preferences::getTagFilterState() const
1445 return value("TransferListFilters/TagFilterState", true).toBool();
1448 void Preferences::setTagFilterState(const bool checked)
1450 setValue("TransferListFilters/TagFilterState", checked);
1453 bool Preferences::getTrackerFilterState() const
1455 return value("TransferListFilters/trackerFilterState", true).toBool();
1458 void Preferences::setTrackerFilterState(const bool checked)
1460 setValue("TransferListFilters/trackerFilterState", checked);
1463 int Preferences::getTransSelFilter() const
1465 return value("TransferListFilters/selectedFilterIndex", 0).toInt();
1468 void Preferences::setTransSelFilter(const int index)
1470 setValue("TransferListFilters/selectedFilterIndex", index);
1473 QByteArray Preferences::getTransHeaderState() const
1475 return value("TransferList/qt5/HeaderState").toByteArray();
1478 void Preferences::setTransHeaderState(const QByteArray &state)
1480 setValue("TransferList/qt5/HeaderState", state);
1483 bool Preferences::getRegexAsFilteringPatternForTransferList() const
1485 return value("TransferList/UseRegexAsFilteringPattern", false).toBool();
1488 void Preferences::setRegexAsFilteringPatternForTransferList(const bool checked)
1490 setValue("TransferList/UseRegexAsFilteringPattern", checked);
1493 // From old RssSettings class
1494 bool Preferences::isRSSWidgetEnabled() const
1496 return value("GUI/RSSWidget/Enabled", false).toBool();
1499 void Preferences::setRSSWidgetVisible(const bool enabled)
1501 setValue("GUI/RSSWidget/Enabled", enabled);
1504 int Preferences::getToolbarTextPosition() const
1506 return value("Toolbar/textPosition", -1).toInt();
1509 void Preferences::setToolbarTextPosition(const int position)
1511 setValue("Toolbar/textPosition", position);
1514 QList<QNetworkCookie> Preferences::getNetworkCookies() const
1516 QList<QNetworkCookie> cookies;
1517 const QStringList rawCookies = value("Network/Cookies").toStringList();
1518 for (const QString &rawCookie : rawCookies)
1519 cookies << QNetworkCookie::parseCookies(rawCookie.toUtf8());
1521 return cookies;
1524 void Preferences::setNetworkCookies(const QList<QNetworkCookie> &cookies)
1526 QStringList rawCookies;
1527 for (const QNetworkCookie &cookie : cookies)
1528 rawCookies << cookie.toRawForm();
1530 setValue("Network/Cookies", rawCookies);
1533 bool Preferences::isSpeedWidgetEnabled() const
1535 return value("SpeedWidget/Enabled", true).toBool();
1538 void Preferences::setSpeedWidgetEnabled(const bool enabled)
1540 setValue("SpeedWidget/Enabled", enabled);
1543 int Preferences::getSpeedWidgetPeriod() const
1545 return value("SpeedWidget/period", 1).toInt();
1548 void Preferences::setSpeedWidgetPeriod(const int period)
1550 setValue("SpeedWidget/period", period);
1553 bool Preferences::getSpeedWidgetGraphEnable(const int id) const
1555 // UP and DOWN graphs enabled by default
1556 return value("SpeedWidget/graph_enable_" + QString::number(id), (id == 0 || id == 1)).toBool();
1559 void Preferences::setSpeedWidgetGraphEnable(const int id, const bool enable)
1561 setValue("SpeedWidget/graph_enable_" + QString::number(id), enable);
1564 void Preferences::apply()
1566 if (SettingsStorage::instance()->save())
1567 emit changed();