From da6ce859c0b8bc2677e5a5eea3a80946e95b95b6 Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Sat, 28 Jun 2014 16:50:56 +0300 Subject: [PATCH] Cache preferences in memory. Closes #1272, #1523, #1694. --- src/preferences/preferences.cpp | 1818 +++++++++++++++++++++++++++++++ src/preferences/preferences.h | 1623 +++++++-------------------- src/preferences/preferences.pri | 2 + 3 files changed, 2213 insertions(+), 1230 deletions(-) create mode 100644 src/preferences/preferences.cpp diff --git a/src/preferences/preferences.cpp b/src/preferences/preferences.cpp new file mode 100644 index 000000000..a1e7a68f1 --- /dev/null +++ b/src/preferences/preferences.cpp @@ -0,0 +1,1818 @@ +/* + * Bittorrent Client using Qt4 and libtorrent. + * Copyright (C) 2006 Christophe Dumez + * Copyright (C) 2014 sledgehammer999 + * + * 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. + * + * Contact : chris@qbittorrent.org + * Contact : hammered999@gmail.com + */ + +#include "preferences.h" +#include "qinisettings.h" + +#include +#include +#include +#include +#include + +#ifndef DISABLE_GUI +#include +#else +#include +#endif + +#ifdef Q_OS_WIN +#include +#endif + +#include "misc.h" +#include "fs_utils.h" + + +Preferences* Preferences::m_instance = 0; + + +Preferences::Preferences() : dirty(false), lock(QReadWriteLock::Recursive) { + QIniSettings *settings = new QIniSettings; +#ifndef Q_OS_MAC + QIniSettings *settings_new = new QIniSettings("qBittorrent", "qBittorrent_new"); + QStringList keys = settings_new->allKeys(); + bool use_new = false; + + + // This means that the PC closed either due to power outage + // or because the disk was full. In any case the settings weren't transfered + // in their final position. So assume that qbittorrent_new.ini/qbittorrent_new.conf + // contains the most recent settings. + if (!keys.isEmpty()) { + use_new = true; + dirty = true; + } + else { + keys = settings->allKeys(); + } +#else + QStringList keys = settings.allKeys(); +#endif + + // Copy everything into memory. This means even keys inserted in the file manually + // or that we don't touch directly in this code(eg disabled by ifdef). This ensures + // that they will be copied over when save our settings to disk. + for (QStringList::const_iterator i = keys.begin(), e = keys.end(); i != e; ++i) { +#ifndef Q_OS_MAC + if (!use_new) + m_data[*i] = settings->value(*i); + else + m_data[*i] = settings_new->value(*i); +#else + m_data[*i] = settings->value(*i); +#endif + } + + //Ensures sync to disk before we attempt to manipulate the files from save(). + delete settings; +#ifndef Q_OS_MAC + delete settings_new; + + if (use_new) + save(); +#endif + + timer.setSingleShot(true); + timer.setInterval(15*60*1000); + connect(&timer, SIGNAL(timeout()), SLOT(save())); +} + +Preferences::~Preferences() { + save(); +} + +Preferences * Preferences::instance() { + if (!m_instance) + m_instance = new Preferences; + + return m_instance; +} + +void Preferences::drop() +{ + if (m_instance) { + delete m_instance; + m_instance = 0; + } +} + +void Preferences::save() { + QReadLocker locker(&lock); + + if (!dirty) + return; + +#ifndef Q_OS_MAC + // QSettings delete the file before writing it out. This can result in problems + // if the disk is full or a power outage occurs. Those events might occur + // between deleting the file and recreating it. This is a safety measure. + // Write everything to qBittorrent_new.ini/qBittorrent_new.conf and if it succeeds + // replace qBittorrent_new.ini/qBittorrent.conf with it. + QIniSettings *settings = new QIniSettings("qBittorrent", "qBittorrent_new"); +#else + QIniSettings *settings = new QIniSettings; +#endif + + for (QHash::const_iterator i = m_data.begin(), e = m_data.end(); i != e; ++i) { + settings->setValue(i.key(), i.value()); + } + + dirty = false; + +#ifndef Q_OS_MAC + settings->sync(); // Important to get error status + if (settings->status() == QSettings::AccessError) { + delete settings; + return; + } + QString new_path = settings->fileName(); + delete settings; + QString final_path = new_path; + int index = final_path.lastIndexOf("_new", -1, Qt::CaseInsensitive); + final_path.remove(index, 4); + fsutils::forceRemove(final_path); + QFile::rename(new_path, final_path); +#else + delete settings; +#endif +} + +const QVariant Preferences::value(const QString &key, const QVariant &defaultValue) const { + QReadLocker locker(&lock); + return m_data.value(key, defaultValue); +} + +void Preferences::setValue(const QString &key, const QVariant &value) { + QWriteLocker locker(&lock); + if (m_data.value(key) == value) + return; + dirty = true; + if (!timer.isActive()) + timer.start(); + m_data.insert(key, value); +} + +// General options +QString Preferences::getLocale() const { + return value("Preferences/General/Locale").toString(); +} + +void Preferences::setLocale(const QString &locale) { + setValue("Preferences/General/Locale", locale); +} + +bool Preferences::useProgramNotification() const { + return value("Preferences/General/ProgramNotification", true).toBool(); +} + +void Preferences::useProgramNotification(bool use) { + setValue("Preferences/General/ProgramNotification", use); +} + +bool Preferences::deleteTorrentFilesAsDefault() const { + return value("Preferences/General/DeleteTorrentsFilesAsDefault", false).toBool(); +} + +void Preferences::setDeleteTorrentFilesAsDefault(bool del) { + setValue("Preferences/General/DeleteTorrentsFilesAsDefault", del); +} + +bool Preferences::confirmOnExit() const { + return value("Preferences/General/ExitConfirm", true).toBool(); +} + +void Preferences::setConfirmOnExit(bool confirm) { + setValue("Preferences/General/ExitConfirm", confirm); +} + +bool Preferences::speedInTitleBar() const { + return value("Preferences/General/SpeedInTitleBar", false).toBool(); +} + +void Preferences::showSpeedInTitleBar(bool show) { + setValue("Preferences/General/SpeedInTitleBar", show); +} + +bool Preferences::useAlternatingRowColors() const { + return value("Preferences/General/AlternatingRowColors", true).toBool(); +} + +void Preferences::setAlternatingRowColors(bool b) { + setValue("Preferences/General/AlternatingRowColors", b); +} + +bool Preferences::useRandomPort() const { + return value("Preferences/General/UseRandomPort", false).toBool(); +} + +void Preferences::setRandomPort(bool b) { + setValue("Preferences/General/UseRandomPort", b); +} + +bool Preferences::systrayIntegration() const { + return value("Preferences/General/SystrayEnabled", true).toBool(); +} + +void Preferences::setSystrayIntegration(bool enabled) { + setValue("Preferences/General/SystrayEnabled", enabled); +} + +bool Preferences::isToolbarDisplayed() const { + return value("Preferences/General/ToolbarDisplayed", true).toBool(); +} + +void Preferences::setToolbarDisplayed(bool displayed) { + setValue("Preferences/General/ToolbarDisplayed", displayed); +} + +bool Preferences::minimizeToTray() const { + return value("Preferences/General/MinimizeToTray", false).toBool(); +} + +void Preferences::setMinimizeToTray(bool b) { + setValue("Preferences/General/MinimizeToTray", b); +} + +bool Preferences::closeToTray() const { + return value("Preferences/General/CloseToTray", false).toBool(); +} + +void Preferences::setCloseToTray(bool b) { + setValue("Preferences/General/CloseToTray", b); +} + +bool Preferences::startMinimized() const { + return value("Preferences/General/StartMinimized", false).toBool(); +} + +void Preferences::setStartMinimized(bool b) { + setValue("Preferences/General/StartMinimized", b); +} + +bool Preferences::isSlashScreenDisabled() const { + return value("Preferences/General/NoSplashScreen", false).toBool(); +} + +void Preferences::setSplashScreenDisabled(bool b) { + setValue("Preferences/General/NoSplashScreen", b); +} + +// Preventing from system suspend while active torrents are presented. +bool Preferences::preventFromSuspend() const { + return value("Preferences/General/PreventFromSuspend", false).toBool(); +} + +void Preferences::setPreventFromSuspend(bool b) { + setValue("Preferences/General/PreventFromSuspend", b); +} + +#ifdef Q_OS_WIN +bool Preferences::WinStartup() const { + QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); + return settings.contains("qBittorrent"); +} + +void Preferences::setWinStartup(bool b) { + QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); + if (b) { + const QString bin_path = "\"" + fsutils::toNativePath(qApp->applicationFilePath()) + "\""; + settings.setValue("qBittorrent", bin_path); + } + else { + settings.remove("qBittorrent"); + } +} +#endif + +// Downloads +QString Preferences::getSavePath() const { + QString save_path = value("Preferences/Downloads/SavePath").toString(); + if (!save_path.isEmpty()) + return fsutils::fromNativePath(save_path); + return fsutils::QDesktopServicesDownloadLocation(); +} + +void Preferences::setSavePath(const QString &save_path) { + setValue("Preferences/Downloads/SavePath", fsutils::fromNativePath(save_path)); +} + +bool Preferences::isTempPathEnabled() const { + return value("Preferences/Downloads/TempPathEnabled", false).toBool(); +} + +void Preferences::setTempPathEnabled(bool enabled) { + setValue("Preferences/Downloads/TempPathEnabled", enabled); +} + +QString Preferences::getTempPath() const { + const QString temp = QDir(getSavePath()).absoluteFilePath("temp"); + return fsutils::fromNativePath(value("Preferences/Downloads/TempPath", temp).toString()); +} + +void Preferences::setTempPath(const QString &path) { + setValue("Preferences/Downloads/TempPath", fsutils::fromNativePath(path)); +} + +bool Preferences::useIncompleteFilesExtension() const { + return value("Preferences/Downloads/UseIncompleteExtension", false).toBool(); +} + +void Preferences::useIncompleteFilesExtension(bool enabled) { + setValue("Preferences/Downloads/UseIncompleteExtension", enabled); +} + +bool Preferences::appendTorrentLabel() const { + return value("Preferences/Downloads/AppendLabel", false).toBool(); +} + +void Preferences::setAppendTorrentLabel(bool b) { + setValue("Preferences/Downloads/AppendLabel", b); +} + +QString Preferences::lastLocationPath() const { + return fsutils::fromNativePath(value("Preferences/Downloads/LastLocationPath").toString()); +} + +void Preferences::setLastLocationPath(const QString &path) { + setValue("Preferences/Downloads/LastLocationPath", fsutils::fromNativePath(path)); +} + +bool Preferences::preAllocateAllFiles() const { + return value("Preferences/Downloads/PreAllocation", false).toBool(); +} + +void Preferences::preAllocateAllFiles(bool enabled) { + return setValue("Preferences/Downloads/PreAllocation", enabled); +} + +bool Preferences::useAdditionDialog() const { + return value("Preferences/Downloads/NewAdditionDialog", true).toBool(); +} + +void Preferences::useAdditionDialog(bool b) { + setValue("Preferences/Downloads/NewAdditionDialog", b); +} + +bool Preferences::additionDialogFront() const { + return value("Preferences/Downloads/NewAdditionDialogFront", true).toBool(); +} + +void Preferences::additionDialogFront(bool b) { + setValue("Preferences/Downloads/NewAdditionDialogFront", b); +} + +bool Preferences::addTorrentsInPause() const { + return value("Preferences/Downloads/StartInPause", false).toBool(); +} + +void Preferences::addTorrentsInPause(bool b) { + setValue("Preferences/Downloads/StartInPause", b); +} + +QStringList Preferences::getScanDirs() const { + QStringList originalList = value("Preferences/Downloads/ScanDirs").toStringList(); + if (originalList.isEmpty()) + return originalList; + + QStringList newList; + foreach (const QString& s, originalList) { + newList << fsutils::fromNativePath(s); + } + return newList; +} + +// This must be called somewhere with data from the model +void Preferences::setScanDirs(const QStringList &dirs) { + QStringList newList; + if (!dirs.isEmpty()) { + foreach (const QString& s, dirs) { + newList << fsutils::fromNativePath(s); + } + } + setValue("Preferences/Downloads/ScanDirs", newList); +} + +QList Preferences::getDownloadInScanDirs() const { + return misc::boolListfromStringList(value("Preferences/Downloads/DownloadInScanDirs").toStringList()); +} + +void Preferences::setDownloadInScanDirs(const QList &list) { + setValue("Preferences/Downloads/DownloadInScanDirs", misc::toStringList(list)); +} + +QString Preferences::getScanDirsLastPath() const { + return fsutils::fromNativePath(value("Preferences/Downloads/ScanDirsLastPath").toString()); +} + +void Preferences::setScanDirsLastPath(const QString &path) { + setValue("Preferences/Downloads/ScanDirsLastPath", fsutils::fromNativePath(path)); +} + +bool Preferences::isTorrentExportEnabled() const { + return !value("Preferences/Downloads/TorrentExportDir").toString().isEmpty(); +} + +QString Preferences::getTorrentExportDir() const { + return fsutils::fromNativePath(value("Preferences/Downloads/TorrentExportDir").toString()); +} + +void Preferences::setTorrentExportDir(QString path) { + setValue("Preferences/Downloads/TorrentExportDir", fsutils::fromNativePath(path.trimmed())); +} + +bool Preferences::isFinishedTorrentExportEnabled() const { + return !value("Preferences/Downloads/FinishedTorrentExportDir").toString().isEmpty(); +} + +QString Preferences::getFinishedTorrentExportDir() const { + return fsutils::fromNativePath(value("Preferences/Downloads/FinishedTorrentExportDir").toString()); +} + +void Preferences::setFinishedTorrentExportDir(QString path) { + setValue("Preferences/Downloads/FinishedTorrentExportDir", fsutils::fromNativePath(path.trimmed())); +} + +bool Preferences::isMailNotificationEnabled() const { + return value("Preferences/MailNotification/enabled", false).toBool(); +} + +void Preferences::setMailNotificationEnabled(bool enabled) { + setValue("Preferences/MailNotification/enabled", enabled); +} + +QString Preferences::getMailNotificationEmail() const { + return value("Preferences/MailNotification/email").toString(); +} + +void Preferences::setMailNotificationEmail(const QString &mail) { + setValue("Preferences/MailNotification/email", mail); +} + +QString Preferences::getMailNotificationSMTP() const { + return value("Preferences/MailNotification/smtp_server", "smtp.changeme.com").toString(); +} + +void Preferences::setMailNotificationSMTP(const QString &smtp_server) { + setValue("Preferences/MailNotification/smtp_server", smtp_server); +} + +bool Preferences::getMailNotificationSMTPSSL() const { + return value("Preferences/MailNotification/req_ssl", false).toBool(); +} + +void Preferences::setMailNotificationSMTPSSL(bool use) { + setValue("Preferences/MailNotification/req_ssl", use); +} + +bool Preferences::getMailNotificationSMTPAuth() const { + return value("Preferences/MailNotification/req_auth", false).toBool(); +} + +void Preferences::setMailNotificationSMTPAuth(bool use) { + setValue("Preferences/MailNotification/req_auth", use); +} + +QString Preferences::getMailNotificationSMTPUsername() const { + return value("Preferences/MailNotification/username").toString(); +} + +void Preferences::setMailNotificationSMTPUsername(const QString &username) { + setValue("Preferences/MailNotification/username", username); +} + +QString Preferences::getMailNotificationSMTPPassword() const { + return value("Preferences/MailNotification/password").toString(); +} + +void Preferences::setMailNotificationSMTPPassword(const QString &password) { + setValue("Preferences/MailNotification/password", password); +} + +int Preferences::getActionOnDblClOnTorrentDl() const { + return value("Preferences/Downloads/DblClOnTorDl", 0).toInt(); +} + +void Preferences::setActionOnDblClOnTorrentDl(int act) { + setValue("Preferences/Downloads/DblClOnTorDl", act); +} + +int Preferences::getActionOnDblClOnTorrentFn() const { + return value("Preferences/Downloads/DblClOnTorFn", 1).toInt(); +} + +void Preferences::setActionOnDblClOnTorrentFn(int act) { + setValue("Preferences/Downloads/DblClOnTorFn", act); +} + +// Connection options +int Preferences::getSessionPort() const { + return value("Preferences/Connection/PortRangeMin", 6881).toInt(); +} + +void Preferences::setSessionPort(int port) { + setValue("Preferences/Connection/PortRangeMin", port); +} + +bool Preferences::isUPnPEnabled() const { + return value("Preferences/Connection/UPnP", true).toBool(); +} + +void Preferences::setUPnPEnabled(bool enabled) { + setValue("Preferences/Connection/UPnP", enabled); +} + +int Preferences::getGlobalDownloadLimit() const { + return value("Preferences/Connection/GlobalDLLimit", -1).toInt(); +} + +void Preferences::setGlobalDownloadLimit(int limit) { + if (limit <= 0) + limit = -1; + setValue("Preferences/Connection/GlobalDLLimit", limit); +} + +int Preferences::getGlobalUploadLimit() const { + return value("Preferences/Connection/GlobalUPLimit", 50).toInt(); +} + +void Preferences::setGlobalUploadLimit(int limit) { + if (limit <= 0) + limit = -1; + setValue("Preferences/Connection/GlobalUPLimit", limit); +} + +int Preferences::getAltGlobalDownloadLimit() const { + int ret = value("Preferences/Connection/GlobalDLLimitAlt", 10).toInt(); + if (ret <= 0) + ret = 10; + return ret; +} + +void Preferences::setAltGlobalDownloadLimit(int limit) { + if (limit <= 0) + limit = -1; + setValue("Preferences/Connection/GlobalDLLimitAlt", limit); +} + +int Preferences::getAltGlobalUploadLimit() const { + int ret = value("Preferences/Connection/GlobalUPLimitAlt", 10).toInt(); + if (ret <= 0) + ret = 10; + return ret; +} + +void Preferences::setAltGlobalUploadLimit(int limit) { + if (limit <= 0) + limit = -1; + setValue("Preferences/Connection/GlobalUPLimitAlt", limit); +} + +bool Preferences::isAltBandwidthEnabled() const { + return value("Preferences/Connection/alt_speeds_on", false).toBool(); +} + +void Preferences::setAltBandwidthEnabled(bool enabled) { + setValue("Preferences/Connection/alt_speeds_on", enabled); +} + +bool Preferences::isSchedulerEnabled() const { + return value("Preferences/Scheduler/Enabled", false).toBool(); +} + +void Preferences::setSchedulerEnabled(bool enabled) { + setValue("Preferences/Scheduler/Enabled", enabled); +} + +QTime Preferences::getSchedulerStartTime() const { + return value("Preferences/Scheduler/start_time", QTime(8,0)).toTime(); +} + +void Preferences::setSchedulerStartTime(const QTime &time) { + setValue("Preferences/Scheduler/start_time", time); +} + +QTime Preferences::getSchedulerEndTime() const { + return value("Preferences/Scheduler/end_time", QTime(20,0)).toTime(); +} + +void Preferences::setSchedulerEndTime(const QTime &time) { + setValue("Preferences/Scheduler/end_time", time); +} + +scheduler_days Preferences::getSchedulerDays() const { + return (scheduler_days)value("Preferences/Scheduler/days", EVERY_DAY).toInt(); +} + +void Preferences::setSchedulerDays(scheduler_days days) { + setValue("Preferences/Scheduler/days", (int)days); +} + +// Proxy options +bool Preferences::isProxyEnabled() const { + return getProxyType() > 0; +} + +bool Preferences::isProxyAuthEnabled() const { + return value("Preferences/Connection/Proxy/Authentication", false).toBool(); +} + +void Preferences::setProxyAuthEnabled(bool enabled) { + setValue("Preferences/Connection/Proxy/Authentication", enabled); +} + +QString Preferences::getProxyIp() const { + return value("Preferences/Connection/Proxy/IP", "0.0.0.0").toString(); +} + +void Preferences::setProxyIp(const QString &ip) { + setValue("Preferences/Connection/Proxy/IP", ip); +} + +unsigned short Preferences::getProxyPort() const { + return value("Preferences/Connection/Proxy/Port", 8080).toInt(); +} + +void Preferences::setProxyPort(unsigned short port) { + setValue("Preferences/Connection/Proxy/Port", port); +} + +QString Preferences::getProxyUsername() const { + return value("Preferences/Connection/Proxy/Username").toString(); +} + +void Preferences::setProxyUsername(const QString &username) { + setValue("Preferences/Connection/Proxy/Username", username); +} + +QString Preferences::getProxyPassword() const { + return value("Preferences/Connection/Proxy/Password").toString(); +} + +void Preferences::setProxyPassword(const QString &password) { + setValue("Preferences/Connection/Proxy/Password", password); +} + +int Preferences::getProxyType() const { + return value("Preferences/Connection/ProxyType", 0).toInt(); +} + +void Preferences::setProxyType(int type) { + setValue("Preferences/Connection/ProxyType", type); +} + +bool Preferences::proxyPeerConnections() const { + return value("Preferences/Connection/ProxyPeerConnections", false).toBool(); +} + +void Preferences::setProxyPeerConnections(bool enabled) { + setValue("Preferences/Connection/ProxyPeerConnections", enabled); +} + +// Bittorrent options +int Preferences::getMaxConnecs() const { + return value("Preferences/Bittorrent/MaxConnecs", 500).toInt(); +} + +void Preferences::setMaxConnecs(int val) { + if (val <= 0) + val = -1; + setValue("Preferences/Bittorrent/MaxConnecs", val); +} + +int Preferences::getMaxConnecsPerTorrent() const { + return value("Preferences/Bittorrent/MaxConnecsPerTorrent", 100).toInt(); +} + +void Preferences::setMaxConnecsPerTorrent(int val) { + if (val <= 0) + val = -1; + setValue("Preferences/Bittorrent/MaxConnecsPerTorrent", val); +} + +int Preferences::getMaxUploads() const { + return value("Preferences/Bittorrent/MaxUploads", 8).toInt(); +} + +void Preferences::setMaxUploads(int val) { + if (val <= 0) + val = -1; + setValue("Preferences/Bittorrent/MaxUploads", val); +} + +int Preferences::getMaxUploadsPerTorrent() const { + return value("Preferences/Bittorrent/MaxUploadsPerTorrent", 4).toInt(); +} + +void Preferences::setMaxUploadsPerTorrent(int val) { + if (val <= 0) + val = -1; + setValue("Preferences/Bittorrent/MaxUploadsPerTorrent", val); +} + +bool Preferences::isuTPEnabled() const { + return value("Preferences/Bittorrent/uTP", true).toBool(); +} + +void Preferences::setuTPEnabled(bool enabled) { + setValue("Preferences/Bittorrent/uTP", enabled); +} + +bool Preferences::isuTPRateLimited() const { + return value("Preferences/Bittorrent/uTP_rate_limited", true).toBool(); +} + +void Preferences::setuTPRateLimited(bool enabled) { + setValue("Preferences/Bittorrent/uTP_rate_limited", enabled); +} + +bool Preferences::isDHTEnabled() const { + return value("Preferences/Bittorrent/DHT", true).toBool(); +} + +void Preferences::setDHTEnabled(bool enabled) { + setValue("Preferences/Bittorrent/DHT", enabled); +} + +bool Preferences::isPeXEnabled() const { + return value("Preferences/Bittorrent/PeX", true).toBool(); +} + +void Preferences::setPeXEnabled(bool enabled) { + setValue("Preferences/Bittorrent/PeX", enabled); +} + +bool Preferences::isLSDEnabled() const { + return value("Preferences/Bittorrent/LSD", true).toBool(); +} + +void Preferences::setLSDEnabled(bool enabled) { + setValue("Preferences/Bittorrent/LSD", enabled); +} + +int Preferences::getEncryptionSetting() const { + return value("Preferences/Bittorrent/Encryption", 0).toInt(); +} + +void Preferences::setEncryptionSetting(int val) { + setValue("Preferences/Bittorrent/Encryption", val); +} + +qreal Preferences::getGlobalMaxRatio() const { + return value("Preferences/Bittorrent/MaxRatio", -1).toDouble(); +} + +void Preferences::setGlobalMaxRatio(qreal ratio) { + setValue("Preferences/Bittorrent/MaxRatio", ratio); +} + +int Preferences::getMaxRatioAction() const { + return value("Preferences/Bittorrent/MaxRatioAction", PAUSE_ACTION).toInt(); +} + +void Preferences::setMaxRatioAction(int act) { + setValue("Preferences/Bittorrent/MaxRatioAction", act); +} + +// IP Filter +bool Preferences::isFilteringEnabled() const { + return value("Preferences/IPFilter/Enabled", false).toBool(); +} + +void Preferences::setFilteringEnabled(bool enabled) { + setValue("Preferences/IPFilter/Enabled", enabled); +} + +QString Preferences::getFilter() const { + return fsutils::fromNativePath(value("Preferences/IPFilter/File").toString()); +} + +void Preferences::setFilter(const QString &path) { + setValue("Preferences/IPFilter/File", fsutils::fromNativePath(path)); +} + +QStringList Preferences::bannedIPs() const { + return value("Preferences/IPFilter/BannedIPs").toStringList(); +} + +void Preferences::banIP(const QString &ip) { + QStringList banned_ips = value("Preferences/IPFilter/BannedIPs").toStringList(); + if (!banned_ips.contains(ip)) { + banned_ips << ip; + setValue("Preferences/IPFilter/BannedIPs", banned_ips); + } +} + +// Search +bool Preferences::isSearchEnabled() const { + return value("Preferences/Search/SearchEnabled", true).toBool(); +} + +void Preferences::setSearchEnabled(bool enabled) { + setValue("Preferences/Search/SearchEnabled", enabled); +} + +// Execution Log +bool Preferences::isExecutionLogEnabled() const { + return value("Preferences/ExecutionLog/enabled", false).toBool(); +} + +void Preferences::setExecutionLogEnabled(bool b) { + setValue("Preferences/ExecutionLog/enabled", b); +} + +// Queueing system +bool Preferences::isQueueingSystemEnabled() const { + return value("Preferences/Queueing/QueueingEnabled", false).toBool(); +} + +void Preferences::setQueueingSystemEnabled(bool enabled) { + setValue("Preferences/Queueing/QueueingEnabled", enabled); +} + +int Preferences::getMaxActiveDownloads() const { + return value("Preferences/Queueing/MaxActiveDownloads", 3).toInt(); +} + +void Preferences::setMaxActiveDownloads(int val) { + if (val < 0) + val = -1; + setValue("Preferences/Queueing/MaxActiveDownloads", val); +} + +int Preferences::getMaxActiveUploads() const { + return value("Preferences/Queueing/MaxActiveUploads", 3).toInt(); +} + +void Preferences::setMaxActiveUploads(int val) { + if (val < 0) + val = -1; + setValue("Preferences/Queueing/MaxActiveUploads", val); +} + +int Preferences::getMaxActiveTorrents() const { + return value("Preferences/Queueing/MaxActiveTorrents", 5).toInt(); +} + +void Preferences::setMaxActiveTorrents(int val) { + if (val < 0) + val = -1; + setValue("Preferences/Queueing/MaxActiveTorrents", val); +} + +bool Preferences::ignoreSlowTorrentsForQueueing() const { + return value("Preferences/Queueing/IgnoreSlowTorrents", false).toBool(); +} + +void Preferences::setIgnoreSlowTorrentsForQueueing(bool ignore) { + setValue("Preferences/Queueing/IgnoreSlowTorrents", ignore); +} + +bool Preferences::isWebUiEnabled() const { + return value("Preferences/WebUI/Enabled", false).toBool(); +} + +void Preferences::setWebUiEnabled(bool enabled) { + setValue("Preferences/WebUI/Enabled", enabled); +} + +bool Preferences::isWebUiLocalAuthEnabled() const { + return value("Preferences/WebUI/LocalHostAuth", true).toBool(); +} + +void Preferences::setWebUiLocalAuthEnabled(bool enabled) { + setValue("Preferences/WebUI/LocalHostAuth", enabled); +} + +quint16 Preferences::getWebUiPort() const { + return value("Preferences/WebUI/Port", 8080).toInt(); +} + +void Preferences::setWebUiPort(quint16 port) { + setValue("Preferences/WebUI/Port", port); +} + +bool Preferences::useUPnPForWebUIPort() const { + return value("Preferences/WebUI/UseUPnP", true).toBool(); +} + +void Preferences::setUPnPForWebUIPort(bool enabled) { + setValue("Preferences/WebUI/UseUPnP", enabled); +} + +QString Preferences::getWebUiUsername() const { + return value("Preferences/WebUI/Username", "admin").toString(); +} + +void Preferences::setWebUiUsername(const QString &username) { + setValue("Preferences/WebUI/Username", username); +} + +QString Preferences::getWebUiPassword() const { + QString pass_ha1 = value("Preferences/WebUI/Password_ha1").toString(); + if (pass_ha1.isEmpty()) { + QCryptographicHash md5(QCryptographicHash::Md5); + md5.addData(getWebUiUsername().toLocal8Bit()+":"+QBT_REALM+":"); + md5.addData("adminadmin"); + pass_ha1 = md5.result().toHex(); + } + return pass_ha1; +} + +void Preferences::setWebUiPassword(const QString &new_password) { + // Get current password md5 + QString current_pass_md5 = getWebUiPassword(); + // Check if password did not change + if (current_pass_md5 == new_password) return; + // Encode to md5 and save + QCryptographicHash md5(QCryptographicHash::Md5); + md5.addData(getWebUiUsername().toLocal8Bit()+":"+QBT_REALM+":"); + md5.addData(new_password.toLocal8Bit()); + + setValue("Preferences/WebUI/Password_ha1", md5.result().toHex()); +} + +bool Preferences::isWebUiHttpsEnabled() const { + return value("Preferences/WebUI/HTTPS/Enabled", false).toBool(); +} + +void Preferences::setWebUiHttpsEnabled(bool enabled) { + setValue("Preferences/WebUI/HTTPS/Enabled", enabled); +} + +QByteArray Preferences::getWebUiHttpsCertificate() const { + return value("Preferences/WebUI/HTTPS/Certificate").toByteArray(); +} + +void Preferences::setWebUiHttpsCertificate(const QByteArray &data) { + setValue("Preferences/WebUI/HTTPS/Certificate", data); +} + +QByteArray Preferences::getWebUiHttpsKey() const { + return value("Preferences/WebUI/HTTPS/Key").toByteArray(); +} + +void Preferences::setWebUiHttpsKey(const QByteArray &data) { + setValue("Preferences/WebUI/HTTPS/Key", data); +} + +bool Preferences::isDynDNSEnabled() const { + return value("Preferences/DynDNS/Enabled", false).toBool(); +} + +void Preferences::setDynDNSEnabled(bool enabled) { + setValue("Preferences/DynDNS/Enabled", enabled); +} + +DNS::Service Preferences::getDynDNSService() const { + return DNS::Service(value("Preferences/DynDNS/Service", DNS::DYNDNS).toInt()); +} + +void Preferences::setDynDNSService(int service) { + setValue("Preferences/DynDNS/Service", service); +} + +QString Preferences::getDynDomainName() const { + return value("Preferences/DynDNS/DomainName", "changeme.dyndns.org").toString(); +} + +void Preferences::setDynDomainName(const QString &name) { + setValue("Preferences/DynDNS/DomainName", name); +} + +QString Preferences::getDynDNSUsername() const { + return value("Preferences/DynDNS/Username").toString(); +} + +void Preferences::setDynDNSUsername(const QString &username) { + setValue("Preferences/DynDNS/Username", username); +} + +QString Preferences::getDynDNSPassword() const { + return value("Preferences/DynDNS/Password").toString(); +} + +void Preferences::setDynDNSPassword(const QString &password) { + setValue("Preferences/DynDNS/Password", password); +} + +// Advanced settings +void Preferences::clearUILockPassword() { + setValue("Locking/password", QString()); +} + +QString Preferences::getUILockPasswordMD5() const { + return value("Locking/password").toString(); +} + +void Preferences::setUILockPassword(const QString &clear_password) { + QCryptographicHash md5(QCryptographicHash::Md5); + md5.addData(clear_password.toLocal8Bit()); + QString md5_password = md5.result().toHex(); + setValue("Locking/password", md5_password); +} + +bool Preferences::isUILocked() const { + return value("Locking/locked", false).toBool(); +} + +void Preferences::setUILocked(bool locked) { + return setValue("Locking/locked", locked); +} + +bool Preferences::isAutoRunEnabled() const { + return value("AutoRun/enabled", false).toBool(); +} + +void Preferences::setAutoRunEnabled(bool enabled) { + return setValue("AutoRun/enabled", enabled); +} + +QString Preferences::getAutoRunProgram() const { + return fsutils::fromNativePath(value("AutoRun/program").toString()); +} + +void Preferences::setAutoRunProgram(const QString &program) { + setValue("AutoRun/program", fsutils::fromNativePath(program)); +} + +bool Preferences::shutdownWhenDownloadsComplete() const { + return value("Preferences/Downloads/AutoShutDownOnCompletion", false).toBool(); +} + +void Preferences::setShutdownWhenDownloadsComplete(bool shutdown) { + setValue("Preferences/Downloads/AutoShutDownOnCompletion", shutdown); +} + +bool Preferences::suspendWhenDownloadsComplete() const { + return value("Preferences/Downloads/AutoSuspendOnCompletion", false).toBool(); +} + +void Preferences::setSuspendWhenDownloadsComplete(bool suspend) { + setValue("Preferences/Downloads/AutoSuspendOnCompletion", suspend); +} + +bool Preferences::hibernateWhenDownloadsComplete() const { + return value("Preferences/Downloads/AutoHibernateOnCompletion", false).toBool(); +} + +void Preferences::setHibernateWhenDownloadsComplete(bool hibernate) { + setValue("Preferences/Downloads/AutoHibernateOnCompletion", hibernate); +} + +bool Preferences::shutdownqBTWhenDownloadsComplete() const { + return value("Preferences/Downloads/AutoShutDownqBTOnCompletion", false).toBool(); +} + +void Preferences::setShutdownqBTWhenDownloadsComplete(bool shutdown) { + setValue("Preferences/Downloads/AutoShutDownqBTOnCompletion", shutdown); +} + +uint Preferences::diskCacheSize() const { + return value("Preferences/Downloads/DiskWriteCacheSize", 0).toUInt(); +} + +void Preferences::setDiskCacheSize(uint size) { + setValue("Preferences/Downloads/DiskWriteCacheSize", size); +} + +uint Preferences::diskCacheTTL() const { + return value("Preferences/Downloads/DiskWriteCacheTTL", 60).toUInt(); +} + +void Preferences::setDiskCacheTTL(uint ttl) { + setValue("Preferences/Downloads/DiskWriteCacheTTL", ttl); +} + +uint Preferences::outgoingPortsMin() const { + return value("Preferences/Advanced/OutgoingPortsMin", 0).toUInt(); +} + +void Preferences::setOutgoingPortsMin(uint val) { + setValue("Preferences/Advanced/OutgoingPortsMin", val); +} + +uint Preferences::outgoingPortsMax() const { + return value("Preferences/Advanced/OutgoingPortsMax", 0).toUInt(); +} + +void Preferences::setOutgoingPortsMax(uint val) { + setValue("Preferences/Advanced/OutgoingPortsMax", val); +} + +bool Preferences::ignoreLimitsOnLAN() const { + return value("Preferences/Advanced/IgnoreLimitsLAN", true).toBool(); +} + +void Preferences::ignoreLimitsOnLAN(bool ignore) { + setValue("Preferences/Advanced/IgnoreLimitsLAN", ignore); +} + +bool Preferences::includeOverheadInLimits() const { + return value("Preferences/Advanced/IncludeOverhead", false).toBool(); +} + +void Preferences::includeOverheadInLimits(bool include) { + setValue("Preferences/Advanced/IncludeOverhead", include); +} + +bool Preferences::trackerExchangeEnabled() const { + return value("Preferences/Advanced/LtTrackerExchange", false).toBool(); +} + +void Preferences::setTrackerExchangeEnabled(bool enable) { + setValue("Preferences/Advanced/LtTrackerExchange", enable); +} + +bool Preferences::recheckTorrentsOnCompletion() const { + return value("Preferences/Advanced/RecheckOnCompletion", false).toBool(); +} + +void Preferences::recheckTorrentsOnCompletion(bool recheck) { + setValue("Preferences/Advanced/RecheckOnCompletion", recheck); +} + +unsigned int Preferences::getRefreshInterval() const { + return value("Preferences/General/RefreshInterval", 1500).toUInt(); +} + +void Preferences::setRefreshInterval(uint interval) { + setValue("Preferences/General/RefreshInterval", interval); +} + +bool Preferences::resolvePeerCountries() const { + return value("Preferences/Connection/ResolvePeerCountries", true).toBool(); +} + +void Preferences::resolvePeerCountries(bool resolve) { + setValue("Preferences/Connection/ResolvePeerCountries", resolve); +} + +bool Preferences::resolvePeerHostNames() const { + return value("Preferences/Connection/ResolvePeerHostNames", false).toBool(); +} + +void Preferences::resolvePeerHostNames(bool resolve) { + setValue("Preferences/Connection/ResolvePeerHostNames", resolve); +} + +int Preferences::getMaxHalfOpenConnections() const { + const int val = value("Preferences/Connection/MaxHalfOpenConnec", 50).toInt(); + if (val <= 0) + return -1; + return val; +} + +void Preferences::setMaxHalfOpenConnections(int value) { + if (value <= 0) + value = -1; + setValue("Preferences/Connection/MaxHalfOpenConnec", value); +} + +QString Preferences::getNetworkInterface() const { + return value("Preferences/Connection/Interface").toString(); +} + +void Preferences::setNetworkInterface(const QString& iface) { + setValue("Preferences/Connection/Interface", iface); +} + +QString Preferences::getNetworkInterfaceName() const { + return value("Preferences/Connection/InterfaceName").toString(); +} + +void Preferences::setNetworkInterfaceName(const QString& iface) { + setValue("Preferences/Connection/InterfaceName", iface); +} + +QString Preferences::getNetworkAddress() const { + return value("Preferences/Connection/InetAddress").toString(); +} + +void Preferences::setNetworkAddress(const QString& addr) { + setValue("Preferences/Connection/InetAddress", addr); +} + +bool Preferences::isAnonymousModeEnabled() const { + return value("Preferences/Advanced/AnonymousMode", false).toBool(); +} + +void Preferences::enableAnonymousMode(bool enabled) { + setValue("Preferences/Advanced/AnonymousMode", enabled); +} + +bool Preferences::isSuperSeedingEnabled() const { + return value("Preferences/Advanced/SuperSeeding", false).toBool(); +} + +void Preferences::enableSuperSeeding(bool enabled) { + setValue("Preferences/Advanced/SuperSeeding", enabled); +} + +bool Preferences::announceToAllTrackers() const { + return value("Preferences/Advanced/AnnounceToAllTrackers", false).toBool(); +} + +void Preferences::setAnnounceToAllTrackers(bool enabled) { + setValue("Preferences/Advanced/AnnounceToAllTrackers", enabled); +} + +#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) +bool Preferences::useSystemIconTheme() const { + return value("Preferences/Advanced/useSystemIconTheme", true).toBool(); +} + +void Preferences::useSystemIconTheme(bool enabled) { + setValue("Preferences/Advanced/useSystemIconTheme", enabled); +} +#endif + +QStringList Preferences::getTorrentLabels() const { + return value("TransferListFilters/customLabels").toStringList(); +} + +void Preferences::setTorrentLabels(const QStringList& labels) { + setValue("TransferListFilters/customLabels", labels); +} + +void Preferences::addTorrentLabel(const QString& label) { + QStringList labels = value("TransferListFilters/customLabels").toStringList(); + if (!labels.contains(label)) + labels << label; + setValue("TransferListFilters/customLabels", labels); +} + +void Preferences::removeTorrentLabel(const QString& label) { + QStringList labels = value("TransferListFilters/customLabels").toStringList(); + if (labels.contains(label)) + labels.removeOne(label); + setValue("TransferListFilters/customLabels", labels); +} + +bool Preferences::recursiveDownloadDisabled() const { + return value("Preferences/Advanced/DisableRecursiveDownload", false).toBool(); +} + +void Preferences::disableRecursiveDownload(bool disable) { + setValue("Preferences/Advanced/DisableRecursiveDownload", disable); +} + +#ifdef Q_OS_WIN +QString Preferences::getPythonPath() { + QSettings reg_python("HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore", QIniSettings::NativeFormat); + QStringList versions = reg_python.childGroups(); + qDebug("Python versions nb: %d", versions.size()); + //versions = versions.filter(QRegExp("2\\..*")); + versions.sort(); + while(!versions.empty()) { + const QString version = versions.takeLast(); + qDebug("Detected possible Python v%s location", qPrintable(version)); + QString path = reg_python.value(version+"/InstallPath/Default", "").toString(); + if (!path.isEmpty() && QDir(path).exists("python.exe")) { + qDebug("Found python.exe at %s", qPrintable(path)); + return path; + } + } + // Fallback: Detect python from default locations + QStringList supported_versions; + supported_versions << "32" << "31" << "30" << "27" << "26" << "25"; + foreach (const QString &v, supported_versions) { + if (QFile::exists("C:/Python"+v+"/python.exe")) { + reg_python.setValue(v[0]+"."+v[1]+"/InstallPath/Default", QString("C:/Python"+v)); + return "C:/Python"+v; + } + } + return QString::null; +} + +bool Preferences::neverCheckFileAssoc() const { + return value("Preferences/Win32/NeverCheckFileAssocation", false).toBool(); +} + +void Preferences::setNeverCheckFileAssoc(bool check) { + setValue("Preferences/Win32/NeverCheckFileAssocation", check); +} + +bool Preferences::isTorrentFileAssocSet() { + QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat); + if (settings.value(".torrent/Default").toString() != "qBittorrent") { + qDebug(".torrent != qBittorrent"); + return false; + } + + return true; +} + +bool Preferences::isMagnetLinkAssocSet() { + QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat); + + // Check magnet link assoc + QRegExp exe_reg("\"([^\"]+)\".*"); + QString shell_command = fsutils::toNativePath(settings.value("magnet/shell/open/command/Default", "").toString()); + if (exe_reg.indexIn(shell_command) < 0) + return false; + QString assoc_exe = exe_reg.cap(1); + qDebug("exe: %s", qPrintable(assoc_exe)); + if (assoc_exe.compare(fsutils::toNativePath(qApp->applicationFilePath()), Qt::CaseInsensitive) != 0) + return false; + + return true; +} + +void Preferences::setTorrentFileAssoc(bool set) { + QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat); + + // .Torrent association + if (set) { + QString old_progid = settings.value(".torrent/Default").toString(); + if (!old_progid.isEmpty() && (old_progid != "qBittorrent")) + settings.setValue(".torrent/OpenWithProgids/" + old_progid, ""); + settings.setValue(".torrent/Default", "qBittorrent"); + } + else if (isTorrentFileAssocSet()) { + settings.setValue(".torrent/Default", ""); + } + + SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0); +} + +void Preferences::setMagnetLinkAssoc(bool set) { + QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat); + + // Magnet association + if (set) { + const QString command_str = "\""+qApp->applicationFilePath()+"\" \"%1\""; + const QString icon_str = "\""+qApp->applicationFilePath()+"\",1"; + + settings.setValue("magnet/Default", "URL:Magnet link"); + settings.setValue("magnet/Content Type", "application/x-magnet"); + settings.setValue("magnet/URL Protocol", ""); + settings.setValue("magnet/DefaultIcon/Default", fsutils::toNativePath(icon_str)); + settings.setValue("magnet/shell/Default", "open"); + settings.setValue("magnet/shell/open/command/Default", fsutils::toNativePath(command_str)); + } + else if (isMagnetLinkAssocSet()) { + settings.remove("magnet"); + } + + SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0); +} +#endif + +bool Preferences::isTrackerEnabled() const { + return value("Preferences/Advanced/trackerEnabled", false).toBool(); +} + +void Preferences::setTrackerEnabled(bool enabled) { + setValue("Preferences/Advanced/trackerEnabled", enabled); +} + +int Preferences::getTrackerPort() const { + return value("Preferences/Advanced/trackerPort", 9000).toInt(); +} + +void Preferences::setTrackerPort(int port) { + setValue("Preferences/Advanced/trackerPort", port); +} + +#if defined(Q_OS_WIN) || defined(Q_OS_MAC) +bool Preferences::isUpdateCheckEnabled() const { + return value("Preferences/Advanced/updateCheck", true).toBool(); +} + +void Preferences::setUpdateCheckEnabled(bool enabled) { + setValue("Preferences/Advanced/updateCheck", enabled); +} +#endif + +bool Preferences::confirmTorrentDeletion() const { + return value("Preferences/Advanced/confirmTorrentDeletion", true).toBool(); +} + +void Preferences::setConfirmTorrentDeletion(bool enabled) { + setValue("Preferences/Advanced/confirmTorrentDeletion", enabled); +} + +TrayIcon::Style Preferences::trayIconStyle() const { + return TrayIcon::Style(value("Preferences/Advanced/TrayIconStyle", TrayIcon::NORMAL).toInt()); +} + +void Preferences::setTrayIconStyle(TrayIcon::Style style) { + setValue("Preferences/Advanced/TrayIconStyle", style); +} + +// Stuff that don't appear in the Options GUI but are saved +// in the same file. +QByteArray Preferences::getAddNewTorrentDialogState() const { + return value("AddNewTorrentDialog/treeHeaderState").toByteArray(); +} + +void Preferences::setAddNewTorrentDialogState(const QByteArray &state) { + setValue("AddNewTorrentDialog/treeHeaderState", state); +} + +int Preferences::getAddNewTorrentDialogPos() const { + return value("AddNewTorrentDialog/y", -1).toInt(); +} + +void Preferences::setAddNewTorrentDialogPos(const int &pos) { + setValue("AddNewTorrentDialog/y", pos); +} + +int Preferences::getAddNewTorrentDialogWidth() const { + return value("AddNewTorrentDialog/width", -1).toInt(); +} + +void Preferences::setAddNewTorrentDialogWidth(const int &width) { + setValue("AddNewTorrentDialog/width", width); +} + +bool Preferences::getAddNewTorrentDialogExpanded() const { + return value("AddNewTorrentDialog/expanded", false).toBool(); +} + +void Preferences::setAddNewTorrentDialogExpanded(const bool expanded) { + setValue("AddNewTorrentDialog/expanded", expanded); +} + +QStringList Preferences::getAddNewTorrentDialogPathHistory() const { + return value("TorrentAdditionDlg/save_path_history").toStringList(); +} + +void Preferences::setAddNewTorrentDialogPathHistory(const QStringList &history) { + setValue("TorrentAdditionDlg/save_path_history", history); +} + +QDateTime Preferences::getDNSLastUpd() const { + return value("DNSUpdater/lastUpdateTime").toDateTime(); +} + +void Preferences::setDNSLastUpd(const QDateTime &date) { + setValue("DNSUpdater/lastUpdateTime", date); +} + +QString Preferences::getDNSLastIP() const { + return value("DNSUpdater/lastIP").toString(); +} + +void Preferences::setDNSLastIP(const QString &ip) { + setValue("DNSUpdater/lastIP", ip); +} + +bool Preferences::getAcceptedLegal() const { + return value("LegalNotice/Accepted", false).toBool(); +} + +void Preferences::setAcceptedLegal(const bool accepted) { + setValue("LegalNotice/Accepted", accepted); +} + +QByteArray Preferences::getMainGeometry() const { + return value("MainWindow/geometry").toByteArray(); +} + +void Preferences::setMainGeometry(const QByteArray &geometry) { + setValue("MainWindow/geometry", geometry); +} + +QByteArray Preferences::getMainVSplitterState() const { + return value("MainWindow/vsplitterState").toByteArray(); +} + +void Preferences::setMainVSplitterState(const QByteArray &state) { + setValue("MainWindow/vsplitterState", state); +} + +QString Preferences::getMainLastDir() const { + return value("MainWindowLastDir", QDir::homePath()).toString(); +} + +void Preferences::setMainLastDir(const QString &path) { + setValue("MainWindowLastDir", path); +} + +#ifndef DISABLE_GUI +QSize Preferences::getPrefSize(const QSize& defaultSize) const { + return value("Preferences/State/size", defaultSize).toSize(); +} + +void Preferences::setPrefSize(const QSize &size) { + setValue("Preferences/State/size", size); +} +#endif + +QPoint Preferences::getPrefPos() const { + return value("Preferences/State/pos").toPoint(); +} + +void Preferences::setPrefPos(const QPoint &pos) { + setValue("Preferences/State/pos", pos); +} + +QStringList Preferences::getPrefHSplitterSizes() const { + return value("Preferences/State/hSplitterSizes").toStringList(); +} + +void Preferences::setPrefHSplitterSizes(const QStringList &sizes) { + setValue("Preferences/State/hSplitterSizes", sizes); +} + +QByteArray Preferences::getPeerListState() const { + return value("TorrentProperties/Peers/PeerListState").toByteArray(); +} + +void Preferences::setPeerListState(const QByteArray &state) { + setValue("TorrentProperties/Peers/PeerListState", state); +} + +QString Preferences::getPropSplitterSizes() const { + return value("TorrentProperties/SplitterSizes").toString(); +} + +void Preferences::setPropSplitterSizes(const QString &sizes) { + setValue("TorrentProperties/SplitterSizes", sizes); +} + +QByteArray Preferences::getPropFileListState() const { + return value("TorrentProperties/FilesListState").toByteArray(); +} + +void Preferences::setPropFileListState(const QByteArray &state) { + setValue("TorrentProperties/FilesListState", state); +} + +int Preferences::getPropCurTab() const { + return value("TorrentProperties/CurrentTab", -1).toInt(); +} + +void Preferences::setPropCurTab(const int &tab) { + setValue("TorrentProperties/CurrentTab", tab); +} + +bool Preferences::getPropVisible() const { + return value("TorrentProperties/Visible", false).toBool(); +} + +void Preferences::setPropVisible(const bool visible) { + setValue("TorrentProperties/Visible", visible); +} + +QByteArray Preferences::getPropTrackerListState() const { + return value("TorrentProperties/Trackers/TrackerListState").toByteArray(); +} + +void Preferences::setPropTrackerListState(const QByteArray &state) { + setValue("TorrentProperties/Trackers/TrackerListState", state); +} + +QByteArray Preferences::getRssGeometry() const { + return value("RssFeedDownloader/geometry").toByteArray(); +} + +void Preferences::setRssGeometry(const QByteArray &geometry) { + setValue("RssFeedDownloader/geometry", geometry); +} + +QByteArray Preferences::getRssHSplitterSizes() const { + return value("RssFeedDownloader/hsplitterSizes").toByteArray(); +} + +void Preferences::setRssHSplitterSizes(const QByteArray &sizes) { + setValue("RssFeedDownloader/hsplitterSizes", sizes); +} + +QStringList Preferences::getRssOpenFolders() const { + return value("Rss/open_folders").toStringList(); +} + +void Preferences::setRssOpenFolders(const QStringList &folders) { + setValue("Rss/open_folders", folders); +} + +QByteArray Preferences::getRssHSplitterState() const { + return value("rss/splitter_h").toByteArray(); +} + +void Preferences::setRssHSplitterState(const QByteArray &state) { + setValue("rss/splitter_h", state); +} + +QByteArray Preferences::getRssVSplitterState() const { + return value("rss/splitter_v").toByteArray(); +} + +void Preferences::setRssVSplitterState(const QByteArray &state) { + setValue("rss/splitter_v", state); +} + +QString Preferences::getSearchColsWidth() const { + return value("SearchResultsColsWidth").toString(); +} + +void Preferences::setSearchColsWidth(const QString &width) { + setValue("SearchResultsColsWidth", width); +} + +QStringList Preferences::getSearchEngDisabled() const { + return value("SearchEngines/disabledEngines").toStringList(); +} + +void Preferences::setSearchEngDisabled(const QStringList &engines) { + setValue("SearchEngines/disabledEngines", engines); +} + +QString Preferences::getCreateTorLastAddPath() const { + return value("CreateTorrent/last_add_path", QDir::homePath()).toString(); +} + +void Preferences::setCreateTorLastAddPath(const QString &path) { + setValue("CreateTorrent/last_add_path", path); +} + +QString Preferences::getCreateTorLastSavePath() const { + return value("CreateTorrent/last_save_path", QDir::homePath()).toString(); +} + +void Preferences::setCreateTorLastSavePath(const QString &path) { + setValue("CreateTorrent/last_save_path", path); +} + +QString Preferences::getCreateTorTrackers() const { + return value("CreateTorrent/TrackerList", QDir::homePath()).toString(); +} + +void Preferences::setCreateTorTrackers(const QString &path) { + setValue("CreateTorrent/TrackerList", path); +} + +QByteArray Preferences::getCreateTorGeometry() const { + return value("CreateTorrent/dimensions").toByteArray(); +} + +void Preferences::setCreateTorGeometry(const QByteArray &geometry) { + setValue("CreateTorrent/dimensions", geometry); +} + +bool Preferences::getCreateTorIgnoreRatio() const { + return value("CreateTorrent/IgnoreRatio").toBool(); +} + +void Preferences::setCreateTorIgnoreRatio(const bool ignore) { + setValue("CreateTorrent/IgnoreRatio", ignore); +} + +QString Preferences::getTorImportLastContentDir() const { + return value("TorrentImport/LastContentDir", QDir::homePath()).toString(); +} + +void Preferences::setTorImportLastContentDir(const QString &path) { + setValue("TorrentImport/LastContentDir", path); +} + +QByteArray Preferences::getTorImportGeometry() const { + return value("TorrentImportDlg/dimensions").toByteArray(); +} + +void Preferences::setTorImportGeometry(const QByteArray &geometry) { + setValue("TorrentImportDlg/dimensions", geometry); +} + +int Preferences::getTransSelFilter() const { + return value("TransferListFilters/selectedFilterIndex", 0).toInt(); +} + +void Preferences::setTransSelFilter(const int &index) { + setValue("TransferListFilters/selectedFilterIndex", index); +} + +QByteArray Preferences::getTransHeaderState() const { + return value("TransferList/HeaderState").toByteArray(); +} + +void Preferences::setTransHeaderState(const QByteArray &state) { + setValue("TransferList/HeaderState", state); +} + +// Temp code. +// See TorrentStatistics::loadStats() for details. +QVariantHash Preferences::getStats() const { + return value("Stats/AllStats").toHash(); +} + +void Preferences::removeStats() { + QWriteLocker locker(&lock); + dirty = true; + if (!timer.isActive()) + timer.start(); + m_data.remove("Stats/AllStats"); +} + +//From old RssSettings class +bool Preferences::isRSSEnabled() const { + return value("Preferences/RSS/RSSEnabled", false).toBool(); +} + +void Preferences::setRSSEnabled(const bool enabled) { + setValue("Preferences/RSS/RSSEnabled", enabled); +} + +uint Preferences::getRSSRefreshInterval() const { + return value("Preferences/RSS/RSSRefresh", 5).toUInt(); +} + +void Preferences::setRSSRefreshInterval(const uint &interval) { + setValue("Preferences/RSS/RSSRefresh", interval); +} + +int Preferences::getRSSMaxArticlesPerFeed() const { + return value("Preferences/RSS/RSSMaxArticlesPerFeed", 50).toInt(); +} + +void Preferences::setRSSMaxArticlesPerFeed(const int &nb) { + setValue("Preferences/RSS/RSSMaxArticlesPerFeed", nb); +} + +bool Preferences::isRssDownloadingEnabled() const { + return value("Preferences/RSS/RssDownloading", true).toBool(); +} + +void Preferences::setRssDownloadingEnabled(const bool b) { + setValue("Preferences/RSS/RssDownloading", b); +} + +QStringList Preferences::getRssFeedsUrls() const { + return value("Rss/streamList").toStringList(); +} + +void Preferences::setRssFeedsUrls(const QStringList &rssFeeds) { + setValue("Rss/streamList", rssFeeds); +} + +QStringList Preferences::getRssFeedsAliases() const { + return value("Rss/streamAlias").toStringList(); +} + +void Preferences::setRssFeedsAliases(const QStringList &rssAliases) { + setValue("Rss/streamAlias", rssAliases); +} + +QList Preferences::getHostNameCookies(const QString &host_name) const { + QMap hosts_table = value("Rss/hosts_cookies").toMap(); + if (!hosts_table.contains(host_name)) return QList(); + QByteArray raw_cookies = hosts_table.value(host_name).toByteArray(); + return raw_cookies.split(':'); +} + +QList Preferences::getHostNameQNetworkCookies(const QString& host_name) const { + QList cookies; + const QList raw_cookies = getHostNameCookies(host_name); + foreach (const QByteArray& raw_cookie, raw_cookies) { + QList cookie_parts = raw_cookie.split('='); + if (cookie_parts.size() == 2) { + qDebug("Loading cookie: %s = %s", cookie_parts.first().constData(), cookie_parts.last().constData()); + cookies << QNetworkCookie(cookie_parts.first(), cookie_parts.last()); + } + } + return cookies; +} + +void Preferences::setHostNameCookies(const QString &host_name, const QList &cookies) { + QMap hosts_table = value("Rss/hosts_cookies").toMap(); + QByteArray raw_cookies = ""; + foreach (const QByteArray& cookie, cookies) { + raw_cookies += cookie + ":"; + } + if (raw_cookies.endsWith(":")) + raw_cookies.chop(1); + hosts_table.insert(host_name, raw_cookies); + setValue("Rss/hosts_cookies", hosts_table); +} diff --git a/src/preferences/preferences.h b/src/preferences/preferences.h index 32b1bd712..5e9308bb7 100755 --- a/src/preferences/preferences.h +++ b/src/preferences/preferences.h @@ -1,6 +1,7 @@ /* * Bittorrent Client using Qt4 and libtorrent. * Copyright (C) 2006 Christophe Dumez + * Copyright (C) 2014 sledgehammer999 * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -26,32 +27,20 @@ * exception statement from your version. * * Contact : chris@qbittorrent.org + * Contact : hammered999@gmail.com */ #ifndef PREFERENCES_H #define PREFERENCES_H -#include -#include -#include + #include +#include #include -#include -#include - -#ifndef DISABLE_GUI -#include -#else -#include -#endif - -#ifdef Q_OS_WIN -#include -#endif - -#include "misc.h" -#include "fs_utils.h" -#include "qinisettings.h" +#include +#include +#include +#include #define QBT_REALM "Web UI Access" enum scheduler_days { EVERY_DAY, WEEK_DAYS, WEEK_ENDS, MON, TUE, WED, THU, FRI, SAT, SUN }; @@ -66,1248 +55,422 @@ namespace DNS { enum Service { DYNDNS, NOIP, NONE = -1 }; } -class Preferences : private QIniSettings { +class Preferences : public QObject { + Q_OBJECT Q_DISABLE_COPY(Preferences) +private: + explicit Preferences(); + static Preferences* m_instance; + QHash m_data; + bool dirty; + QTimer timer; + mutable QReadWriteLock lock; + const QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const; + void setValue(const QString &key, const QVariant &value); + +public slots: + void save(); + public: - Preferences() - : QIniSettings("qBittorrent", "qBittorrent") - { - qDebug() << "Preferences constructor"; - } - - void sync() - { - QIniSettings::sync(); - } - - // General options - QString getLocale() const { - return value(QString::fromUtf8("Preferences/General/Locale")).toString(); - } - - void setLocale(const QString &locale) { - setValue(QString::fromUtf8("Preferences/General/Locale"), locale); - } - - bool useProgramNotification() const { - return value(QString::fromUtf8("Preferences/General/ProgramNotification"), true).toBool(); - } - - void useProgramNotification(bool use) { - setValue(QString::fromUtf8("Preferences/General/ProgramNotification"), use); - } - - bool deleteTorrentFilesAsDefault() const { - return value(QString::fromUtf8("Preferences/General/DeleteTorrentsFilesAsDefault"), false).toBool(); - } - - void setDeleteTorrentFilesAsDefault(bool del) { - setValue(QString::fromUtf8("Preferences/General/DeleteTorrentsFilesAsDefault"), del); - } - - void setConfirmOnExit(bool confirm) { - setValue(QString::fromUtf8("Preferences/General/ExitConfirm"), confirm); - } - - bool confirmOnExit() const { - return value(QString::fromUtf8("Preferences/General/ExitConfirm"), true).toBool(); - } - - void showSpeedInTitleBar(bool show) { - setValue(QString::fromUtf8("Preferences/General/SpeedInTitleBar"), show); - } - - bool speedInTitleBar() const { - return value(QString::fromUtf8("Preferences/General/SpeedInTitleBar"), false).toBool(); - } - - bool useAlternatingRowColors() const { - return value(QString::fromUtf8("Preferences/General/AlternatingRowColors"), true).toBool(); - } - - void setAlternatingRowColors(bool b) { - setValue("Preferences/General/AlternatingRowColors", b); - } - - bool useRandomPort() const { - return value(QString::fromUtf8("Preferences/General/UseRandomPort"), false).toBool(); - } - - void setRandomPort(bool b) { - setValue("Preferences/General/UseRandomPort", b); - } - - bool systrayIntegration() const { - return value(QString::fromUtf8("Preferences/General/SystrayEnabled"), true).toBool(); - } - - void setSystrayIntegration(bool enabled) { - setValue(QString::fromUtf8("Preferences/General/SystrayEnabled"), enabled); - } - - void setToolbarDisplayed(bool displayed) { - setValue(QString::fromUtf8("Preferences/General/ToolbarDisplayed"), displayed); - } - - bool isToolbarDisplayed() const { - return value(QString::fromUtf8("Preferences/General/ToolbarDisplayed"), true).toBool(); - } - - bool minimizeToTray() const { - return value(QString::fromUtf8("Preferences/General/MinimizeToTray"), false).toBool(); - } - - void setMinimizeToTray(bool b) { - setValue("Preferences/General/MinimizeToTray", b); - } - - bool closeToTray() const { - return value(QString::fromUtf8("Preferences/General/CloseToTray"), false).toBool(); - } - - void setCloseToTray(bool b) { - setValue("Preferences/General/CloseToTray", b); - } - - bool startMinimized() const { - return value(QString::fromUtf8("Preferences/General/StartMinimized"), false).toBool(); - } - - void setStartMinimized(bool b) { - setValue("Preferences/General/StartMinimized", b); - } - - bool isSlashScreenDisabled() const { - return value(QString::fromUtf8("Preferences/General/NoSplashScreen"), false).toBool(); - } - - void setSplashScreenDisabled(bool b) { - setValue("Preferences/General/NoSplashScreen", b); - } - - // Preventing from system suspend while active torrents are presented. - bool preventFromSuspend() const { - return value(QString::fromUtf8("Preferences/General/PreventFromSuspend"), false).toBool(); - } - - void setPreventFromSuspend(bool b) { - setValue("Preferences/General/PreventFromSuspend", b); - } + static Preferences* instance(); + static void drop(); + ~Preferences(); + // General options + QString getLocale() const; + void setLocale(const QString &locale); + bool useProgramNotification() const; + void useProgramNotification(bool use); + bool deleteTorrentFilesAsDefault() const; + void setDeleteTorrentFilesAsDefault(bool del); + bool confirmOnExit() const; + void setConfirmOnExit(bool confirm); + bool speedInTitleBar() const; + void showSpeedInTitleBar(bool show); + bool useAlternatingRowColors() const; + void setAlternatingRowColors(bool b); + bool useRandomPort() const; + void setRandomPort(bool b); + bool systrayIntegration() const; + void setSystrayIntegration(bool enabled); + bool isToolbarDisplayed() const; + void setToolbarDisplayed(bool displayed); + bool minimizeToTray() const; + void setMinimizeToTray(bool b); + bool closeToTray() const; + void setCloseToTray(bool b); + bool startMinimized() const; + void setStartMinimized(bool b); + bool isSlashScreenDisabled() const; + void setSplashScreenDisabled(bool b); + bool preventFromSuspend() const; + void setPreventFromSuspend(bool b); #ifdef Q_OS_WIN - bool Startup() const { - QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); - return settings.contains("qBittorrent"); - } - - void setStartup(bool b) { - QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); - if (b) { - const QString bin_path = "\"" + fsutils::toNativePath(qApp->applicationFilePath()) + "\""; - settings.setValue("qBittorrent", bin_path); - } - else { - settings.remove("qBittorrent"); - } - } + bool WinStartup() const; + void setWinStartup(bool b); #endif // Downloads - QString getSavePath() const { - QString save_path = value(QString::fromUtf8("Preferences/Downloads/SavePath")).toString(); - if (!save_path.isEmpty()) - return fsutils::fromNativePath(save_path); - return fsutils::QDesktopServicesDownloadLocation(); - } - - void setSavePath(const QString &save_path) { - setValue(QString::fromUtf8("Preferences/Downloads/SavePath"), fsutils::fromNativePath(save_path)); - } - - bool isTempPathEnabled() const { - return value(QString::fromUtf8("Preferences/Downloads/TempPathEnabled"), false).toBool(); - } - - void setTempPathEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Downloads/TempPathEnabled"), enabled); - } - - QString getTempPath() const { - const QString temp = QDir(getSavePath()).absoluteFilePath("temp"); - return fsutils::fromNativePath(value(QString::fromUtf8("Preferences/Downloads/TempPath"), temp).toString()); - } - - void setTempPath(const QString &path) { - setValue(QString::fromUtf8("Preferences/Downloads/TempPath"), fsutils::fromNativePath(path)); - } - - bool useIncompleteFilesExtension() const { - return value(QString::fromUtf8("Preferences/Downloads/UseIncompleteExtension"), false).toBool(); - } - - void useIncompleteFilesExtension(bool enabled) { - setValue(QString::fromUtf8("Preferences/Downloads/UseIncompleteExtension"), enabled); - } - - bool appendTorrentLabel() const { - return value(QString::fromUtf8("Preferences/Downloads/AppendLabel"), false).toBool(); - } - - void setAppendTorrentLabel(bool b) { - setValue("Preferences/Downloads/AppendLabel", b); - } - - QString lastLocationPath() const { - return fsutils::fromNativePath(value(QString::fromUtf8("Preferences/Downloads/LastLocationPath"), QString()).toString()); - } - - void setLastLocationPath(const QString &path) { - setValue(QString::fromUtf8("Preferences/Downloads/LastLocationPath"), fsutils::fromNativePath(path)); - } - - bool preAllocateAllFiles() const { - return value(QString::fromUtf8("Preferences/Downloads/PreAllocation"), false).toBool(); - } - - void preAllocateAllFiles(bool enabled) { - return setValue(QString::fromUtf8("Preferences/Downloads/PreAllocation"), enabled); - } - - bool useAdditionDialog() const { - return value(QString::fromUtf8("Preferences/Downloads/NewAdditionDialog"), true).toBool(); - } - - void useAdditionDialog(bool b) { - setValue("Preferences/Downloads/NewAdditionDialog", b); - } - - bool AdditionDialogFront() const { - return value(QString::fromUtf8("Preferences/Downloads/NewAdditionDialogFront"), true).toBool(); - } - - void AdditionDialogFront(bool b) { - setValue("Preferences/Downloads/NewAdditionDialogFront", b); - } - - bool addTorrentsInPause() const { - return value(QString::fromUtf8("Preferences/Downloads/StartInPause"), false).toBool(); - } - - void addTorrentsInPause(bool b) { - setValue("Preferences/Downloads/StartInPause", b); - } - - QStringList getScanDirs() const { - QStringList originalList = value(QString::fromUtf8("Preferences/Downloads/ScanDirs"), QStringList()).toStringList(); - if (originalList.isEmpty()) - return originalList; - - QStringList newList; - foreach (const QString& s, originalList) { - newList << fsutils::fromNativePath(s); - } - return newList; - } - - // This must be called somewhere with data from the model - void setScanDirs(const QStringList &dirs) { - QStringList newList; - if (!dirs.isEmpty()) { - foreach (const QString& s, dirs) { - newList << fsutils::fromNativePath(s); - } - } - setValue(QString::fromUtf8("Preferences/Downloads/ScanDirs"), newList); - } - - QList getDownloadInScanDirs() const { - return misc::boolListfromStringList(value(QString::fromUtf8("Preferences/Downloads/DownloadInScanDirs")).toStringList()); - } - - void setDownloadInScanDirs(const QList &list) { - setValue(QString::fromUtf8("Preferences/Downloads/DownloadInScanDirs"), misc::toStringList(list)); - } - - QString getScanDirsLastPath() const { - return fsutils::fromNativePath(value(QString::fromUtf8("Preferences/Downloads/ScanDirsLastPath"), QString()).toString()); - } - - void setScanDirsLastPath(const QString &path) { - setValue(QString::fromUtf8("Preferences/Downloads/ScanDirsLastPath"), fsutils::fromNativePath(path)); - } - - bool isTorrentExportEnabled() const { - return !value(QString::fromUtf8("Preferences/Downloads/TorrentExportDir"), QString()).toString().isEmpty(); - } - - QString getTorrentExportDir() const { - return fsutils::fromNativePath(value(QString::fromUtf8("Preferences/Downloads/TorrentExportDir"), QString()).toString()); - } - - void setTorrentExportDir(QString path) { - setValue(QString::fromUtf8("Preferences/Downloads/TorrentExportDir"), fsutils::fromNativePath(path.trimmed())); - } - - bool isFinishedTorrentExportEnabled() const { - return !value(QString::fromUtf8("Preferences/Downloads/FinishedTorrentExportDir"), QString()).toString().isEmpty(); - } - - QString getFinishedTorrentExportDir() const { - return fsutils::fromNativePath(value(QString::fromUtf8("Preferences/Downloads/FinishedTorrentExportDir"), QString()).toString()); - } - - void setFinishedTorrentExportDir(QString path) { - setValue(QString::fromUtf8("Preferences/Downloads/FinishedTorrentExportDir"), fsutils::fromNativePath(path.trimmed())); - } - - bool isMailNotificationEnabled() const { - return value(QString::fromUtf8("Preferences/MailNotification/enabled"), false).toBool(); - } - - void setMailNotificationEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/MailNotification/enabled"), enabled); - } - - QString getMailNotificationEmail() const { - return value(QString::fromUtf8("Preferences/MailNotification/email"), "").toString(); - } - - void setMailNotificationEmail(const QString &mail) { - setValue(QString::fromUtf8("Preferences/MailNotification/email"), mail); - } - - QString getMailNotificationSMTP() const { - return value(QString::fromUtf8("Preferences/MailNotification/smtp_server"), "smtp.changeme.com").toString(); - } - - void setMailNotificationSMTP(const QString &smtp_server) { - setValue(QString::fromUtf8("Preferences/MailNotification/smtp_server"), smtp_server); - } - - bool getMailNotificationSMTPSSL() const { - return value(QString::fromUtf8("Preferences/MailNotification/req_ssl"), false).toBool(); - } - - void setMailNotificationSMTPSSL(bool use) { - setValue(QString::fromUtf8("Preferences/MailNotification/req_ssl"), use); - } - - bool getMailNotificationSMTPAuth() const { - return value(QString::fromUtf8("Preferences/MailNotification/req_auth"), false).toBool(); - } - - void setMailNotificationSMTPAuth(bool use) { - setValue(QString::fromUtf8("Preferences/MailNotification/req_auth"), use); - } - - QString getMailNotificationSMTPUsername() const { - return value(QString::fromUtf8("Preferences/MailNotification/username")).toString(); - } - - void setMailNotificationSMTPUsername(const QString &username) { - setValue(QString::fromUtf8("Preferences/MailNotification/username"), username); - } - - QString getMailNotificationSMTPPassword() const { - return value(QString::fromUtf8("Preferences/MailNotification/password")).toString(); - } - - void setMailNotificationSMTPPassword(const QString &password) { - setValue(QString::fromUtf8("Preferences/MailNotification/password"), password); - } - - int getActionOnDblClOnTorrentDl() const { - return value(QString::fromUtf8("Preferences/Downloads/DblClOnTorDl"), 0).toInt(); - } - - void setActionOnDblClOnTorrentDl(int act) { - setValue("Preferences/Downloads/DblClOnTorDl", act); - } - - int getActionOnDblClOnTorrentFn() const { - return value(QString::fromUtf8("Preferences/Downloads/DblClOnTorFn"), 1).toInt(); - } - - void setActionOnDblClOnTorrentFn(int act) { - setValue("Preferences/Downloads/DblClOnTorFn", act); - } + QString getSavePath() const; + void setSavePath(const QString &save_path); + bool isTempPathEnabled() const; + void setTempPathEnabled(bool enabled); + QString getTempPath() const; + void setTempPath(const QString &path); + bool useIncompleteFilesExtension() const; + void useIncompleteFilesExtension(bool enabled); + bool appendTorrentLabel() const; + void setAppendTorrentLabel(bool b); + QString lastLocationPath() const; + void setLastLocationPath(const QString &path); + bool preAllocateAllFiles() const; + void preAllocateAllFiles(bool enabled); + bool useAdditionDialog() const; + void useAdditionDialog(bool b); + bool additionDialogFront() const; + void additionDialogFront(bool b); + bool addTorrentsInPause() const; + void addTorrentsInPause(bool b); + QStringList getScanDirs() const; + void setScanDirs(const QStringList &dirs); + QList getDownloadInScanDirs() const; + void setDownloadInScanDirs(const QList &list); + QString getScanDirsLastPath() const; + void setScanDirsLastPath(const QString &path); + bool isTorrentExportEnabled() const; + QString getTorrentExportDir() const; + void setTorrentExportDir(QString path); + bool isFinishedTorrentExportEnabled() const; + QString getFinishedTorrentExportDir() const; + void setFinishedTorrentExportDir(QString path); + bool isMailNotificationEnabled() const; + void setMailNotificationEnabled(bool enabled); + QString getMailNotificationEmail() const; + void setMailNotificationEmail(const QString &mail); + QString getMailNotificationSMTP() const; + void setMailNotificationSMTP(const QString &smtp_server); + bool getMailNotificationSMTPSSL() const; + void setMailNotificationSMTPSSL(bool use); + bool getMailNotificationSMTPAuth() const; + void setMailNotificationSMTPAuth(bool use); + QString getMailNotificationSMTPUsername() const; + void setMailNotificationSMTPUsername(const QString &username); + QString getMailNotificationSMTPPassword() const; + void setMailNotificationSMTPPassword(const QString &password); + int getActionOnDblClOnTorrentDl() const; + void setActionOnDblClOnTorrentDl(int act); + int getActionOnDblClOnTorrentFn() const; + void setActionOnDblClOnTorrentFn(int act); // Connection options - int getSessionPort() const { - return value(QString::fromUtf8("Preferences/Connection/PortRangeMin"), 6881).toInt(); - } - - void setSessionPort(int port) { - setValue(QString::fromUtf8("Preferences/Connection/PortRangeMin"), port); - } - - bool isUPnPEnabled() const { - return value(QString::fromUtf8("Preferences/Connection/UPnP"), true).toBool(); - } - - void setUPnPEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Connection/UPnP"), enabled); - } - - int getGlobalDownloadLimit() const { - return value("Preferences/Connection/GlobalDLLimit", -1).toInt(); - } - - void setGlobalDownloadLimit(int limit) { - if (limit <= 0) limit = -1; - setValue("Preferences/Connection/GlobalDLLimit", limit); - } - - int getGlobalUploadLimit() const { - return value("Preferences/Connection/GlobalUPLimit", 50).toInt(); - } - - void setGlobalUploadLimit(int limit) { - if (limit <= 0) limit = -1; - setValue("Preferences/Connection/GlobalUPLimit", limit); - } - - int getAltGlobalDownloadLimit() const { - int ret = value(QString::fromUtf8("Preferences/Connection/GlobalDLLimitAlt"), 10).toInt(); - if (ret <= 0) - ret = 10; - return ret; - } - - void setAltGlobalDownloadLimit(int limit) { - if (limit <= 0) limit = -1; - setValue("Preferences/Connection/GlobalDLLimitAlt", limit); - } - - int getAltGlobalUploadLimit() const { - int ret = value(QString::fromUtf8("Preferences/Connection/GlobalUPLimitAlt"), 10).toInt(); - if (ret <= 0) - ret = 10; - return ret; - } - - void setAltGlobalUploadLimit(int limit) { - if (limit <= 0) limit = -1; - setValue("Preferences/Connection/GlobalUPLimitAlt", limit); - } - - bool isAltBandwidthEnabled() const { - return value("Preferences/Connection/alt_speeds_on", false).toBool(); - } - - void setAltBandwidthEnabled(bool enabled) { - setValue("Preferences/Connection/alt_speeds_on", enabled); - } - - void setSchedulerEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Scheduler/Enabled"), enabled); - } - - bool isSchedulerEnabled() const { - return value(QString::fromUtf8("Preferences/Scheduler/Enabled"), false).toBool(); - } - - QTime getSchedulerStartTime() const { - return value(QString::fromUtf8("Preferences/Scheduler/start_time"), QTime(8,0)).toTime(); - } - - void setSchedulerStartTime(const QTime &time) { - setValue(QString::fromUtf8("Preferences/Scheduler/start_time"), time); - } - - QTime getSchedulerEndTime() const { - return value(QString::fromUtf8("Preferences/Scheduler/end_time"), QTime(20,0)).toTime(); - } - - void setSchedulerEndTime(const QTime &time) { - setValue(QString::fromUtf8("Preferences/Scheduler/end_time"), time); - } - - scheduler_days getSchedulerDays() const { - return (scheduler_days)value(QString::fromUtf8("Preferences/Scheduler/days"), EVERY_DAY).toInt(); - } - - void setSchedulerDays(scheduler_days days) { - setValue(QString::fromUtf8("Preferences/Scheduler/days"), (int)days); - } + int getSessionPort() const; + void setSessionPort(int port); + bool isUPnPEnabled() const; + void setUPnPEnabled(bool enabled); + int getGlobalDownloadLimit() const; + void setGlobalDownloadLimit(int limit); + int getGlobalUploadLimit() const; + void setGlobalUploadLimit(int limit); + int getAltGlobalDownloadLimit() const; + void setAltGlobalDownloadLimit(int limit); + int getAltGlobalUploadLimit() const; + void setAltGlobalUploadLimit(int limit); + bool isAltBandwidthEnabled() const; + void setAltBandwidthEnabled(bool enabled); + bool isSchedulerEnabled() const; + void setSchedulerEnabled(bool enabled); + QTime getSchedulerStartTime() const; + void setSchedulerStartTime(const QTime &time); + QTime getSchedulerEndTime() const; + void setSchedulerEndTime(const QTime &time); + scheduler_days getSchedulerDays() const; + void setSchedulerDays(scheduler_days days); // Proxy options - bool isProxyEnabled() const { - return value(QString::fromUtf8("Preferences/Connection/ProxyType"), 0).toInt() > 0; - } - - bool isProxyAuthEnabled() const { - return value(QString::fromUtf8("Preferences/Connection/Proxy/Authentication"), false).toBool(); - } - - void setProxyAuthEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Connection/Proxy/Authentication"), enabled); - } - - QString getProxyIp() const { - return value(QString::fromUtf8("Preferences/Connection/Proxy/IP"), "0.0.0.0").toString(); - } - - void setProxyIp(const QString &ip) { - setValue(QString::fromUtf8("Preferences/Connection/Proxy/IP"), ip); - } - - unsigned short getProxyPort() const { - return value(QString::fromUtf8("Preferences/Connection/Proxy/Port"), 8080).toInt(); - } - - void setProxyPort(unsigned short port) { - setValue(QString::fromUtf8("Preferences/Connection/Proxy/Port"), port); - } - - QString getProxyUsername() const { - return value(QString::fromUtf8("Preferences/Connection/Proxy/Username"), QString()).toString(); - } - - void setProxyUsername(const QString &username) { - setValue(QString::fromUtf8("Preferences/Connection/Proxy/Username"), username); - } - - QString getProxyPassword() const { - return value(QString::fromUtf8("Preferences/Connection/Proxy/Password"), QString()).toString(); - } - - void setProxyPassword(const QString &password) { - setValue(QString::fromUtf8("Preferences/Connection/Proxy/Password"), password); - } - - int getProxyType() const { - return value(QString::fromUtf8("Preferences/Connection/ProxyType"), 0).toInt(); - } - - void setProxyType(int type) { - setValue(QString::fromUtf8("Preferences/Connection/ProxyType"), type); - } - - void setProxyPeerConnections(bool enabled) { - setValue(QString::fromUtf8("Preferences/Connection/ProxyPeerConnections"), enabled); - } - - bool proxyPeerConnections() const { - return value(QString::fromUtf8("Preferences/Connection/ProxyPeerConnections"), false).toBool(); - } + bool isProxyEnabled() const; + bool isProxyAuthEnabled() const; + void setProxyAuthEnabled(bool enabled); + QString getProxyIp() const; + void setProxyIp(const QString &ip); + unsigned short getProxyPort() const; + void setProxyPort(unsigned short port); + QString getProxyUsername() const; + void setProxyUsername(const QString &username); + QString getProxyPassword() const; + void setProxyPassword(const QString &password); + int getProxyType() const; + void setProxyType(int type); + bool proxyPeerConnections() const; + void setProxyPeerConnections(bool enabled); // Bittorrent options - int getMaxConnecs() const { - return value(QString::fromUtf8("Preferences/Bittorrent/MaxConnecs"), 500).toInt(); - } - - void setMaxConnecs(int val) { - if (val <= 0) val = -1; - setValue(QString::fromUtf8("Preferences/Bittorrent/MaxConnecs"), val); - } - - int getMaxConnecsPerTorrent() const { - return value(QString::fromUtf8("Preferences/Bittorrent/MaxConnecsPerTorrent"), 100).toInt(); - } - - void setMaxConnecsPerTorrent(int val) { - if (val <= 0) val = -1; - setValue(QString::fromUtf8("Preferences/Bittorrent/MaxConnecsPerTorrent"), val); - } - - int getMaxUploads() const { - return value(QString::fromUtf8("Preferences/Bittorrent/MaxUploads"), 8).toInt(); - } - - void setMaxUploads(int val) { - if (val <= 0) val = -1; - setValue(QString::fromUtf8("Preferences/Bittorrent/MaxUploads"), val); - } - - int getMaxUploadsPerTorrent() const { - return value(QString::fromUtf8("Preferences/Bittorrent/MaxUploadsPerTorrent"), 4).toInt(); - } - - void setMaxUploadsPerTorrent(int val) { - if (val <= 0) val = -1; - setValue(QString::fromUtf8("Preferences/Bittorrent/MaxUploadsPerTorrent"), val); - } - - bool isuTPEnabled() const { - return value(QString::fromUtf8("Preferences/Bittorrent/uTP"), true).toBool(); - } - - void setuTPEnabled(bool enabled) { - setValue("Preferences/Bittorrent/uTP", enabled); - } - - bool isuTPRateLimited() const { - return value(QString::fromUtf8("Preferences/Bittorrent/uTP_rate_limited"), true).toBool(); - } - - void setuTPRateLimited(bool enabled) { - setValue("Preferences/Bittorrent/uTP_rate_limited", enabled); - } - - bool isDHTEnabled() const { - return value(QString::fromUtf8("Preferences/Bittorrent/DHT"), true).toBool(); - } - - void setDHTEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Bittorrent/DHT"), enabled); - } - - bool isPeXEnabled() const { - return value(QString::fromUtf8("Preferences/Bittorrent/PeX"), true).toBool(); - } - - void setPeXEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Bittorrent/PeX"), enabled); - } - - bool isLSDEnabled() const { - return value(QString::fromUtf8("Preferences/Bittorrent/LSD"), true).toBool(); - } - - void setLSDEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Bittorrent/LSD"), enabled); - } - - int getEncryptionSetting() const { - return value(QString::fromUtf8("Preferences/Bittorrent/Encryption"), 0).toInt(); - } - - void setEncryptionSetting(int val) { - setValue(QString::fromUtf8("Preferences/Bittorrent/Encryption"), val); - } - - qreal getGlobalMaxRatio() const { - return value(QString::fromUtf8("Preferences/Bittorrent/MaxRatio"), -1).toDouble(); - } - - void setGlobalMaxRatio(qreal ratio) { - setValue(QString::fromUtf8("Preferences/Bittorrent/MaxRatio"), ratio); - } - - void setMaxRatioAction(int act) { - setValue(QString::fromUtf8("Preferences/Bittorrent/MaxRatioAction"), act); - } - - int getMaxRatioAction() const { - return value(QString::fromUtf8("Preferences/Bittorrent/MaxRatioAction"), PAUSE_ACTION).toInt(); - } + int getMaxConnecs() const; + void setMaxConnecs(int val); + int getMaxConnecsPerTorrent() const; + void setMaxConnecsPerTorrent(int val); + int getMaxUploads() const; + void setMaxUploads(int val); + int getMaxUploadsPerTorrent() const; + void setMaxUploadsPerTorrent(int val); + bool isuTPEnabled() const; + void setuTPEnabled(bool enabled); + bool isuTPRateLimited() const; + void setuTPRateLimited(bool enabled); + bool isDHTEnabled() const; + void setDHTEnabled(bool enabled); + bool isPeXEnabled() const; + void setPeXEnabled(bool enabled); + bool isLSDEnabled() const; + void setLSDEnabled(bool enabled); + int getEncryptionSetting() const; + void setEncryptionSetting(int val); + qreal getGlobalMaxRatio() const; + void setGlobalMaxRatio(qreal ratio); + int getMaxRatioAction() const; + void setMaxRatioAction(int act); // IP Filter - bool isFilteringEnabled() const { - return value(QString::fromUtf8("Preferences/IPFilter/Enabled"), false).toBool(); - } - - void setFilteringEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/IPFilter/Enabled"), enabled); - } - - QString getFilter() const { - return fsutils::fromNativePath(value(QString::fromUtf8("Preferences/IPFilter/File"), QString()).toString()); - } - - void setFilter(const QString &path) { - setValue(QString::fromUtf8("Preferences/IPFilter/File"), fsutils::fromNativePath(path)); - } - - void banIP(const QString &ip) { - QStringList banned_ips = value(QString::fromUtf8("Preferences/IPFilter/BannedIPs"), QStringList()).toStringList(); - if (!banned_ips.contains(ip)) { - banned_ips << ip; - setValue("Preferences/IPFilter/BannedIPs", banned_ips); - } - } - - QStringList bannedIPs() const { - return value(QString::fromUtf8("Preferences/IPFilter/BannedIPs"), QStringList()).toStringList(); - } + bool isFilteringEnabled() const; + void setFilteringEnabled(bool enabled); + QString getFilter() const; + void setFilter(const QString &path); + QStringList bannedIPs() const; + void banIP(const QString &ip); // Search - bool isSearchEnabled() const { - return value(QString::fromUtf8("Preferences/Search/SearchEnabled"), true).toBool(); - } - - void setSearchEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Search/SearchEnabled"), enabled); - } + bool isSearchEnabled() const; + void setSearchEnabled(bool enabled); // Execution Log - - bool isExecutionLogEnabled() const { - return value(QString::fromUtf8("Preferences/ExecutionLog/enabled"), false).toBool(); - } - - void setExecutionLogEnabled(bool b) { - setValue(QString::fromUtf8("Preferences/ExecutionLog/enabled"), b); - } + bool isExecutionLogEnabled() const; + void setExecutionLogEnabled(bool b); // Queueing system - bool isQueueingSystemEnabled() const { - return value("Preferences/Queueing/QueueingEnabled", false).toBool(); - } - - void setQueueingSystemEnabled(bool enabled) { - setValue("Preferences/Queueing/QueueingEnabled", enabled); - } - - int getMaxActiveDownloads() const { - return value(QString::fromUtf8("Preferences/Queueing/MaxActiveDownloads"), 3).toInt(); - } - - void setMaxActiveDownloads(int val) { - if (val < 0) val = -1; - setValue(QString::fromUtf8("Preferences/Queueing/MaxActiveDownloads"), val); - } - - int getMaxActiveUploads() const { - return value(QString::fromUtf8("Preferences/Queueing/MaxActiveUploads"), 3).toInt(); - } - - void setMaxActiveUploads(int val) { - if (val < 0) val = -1; - setValue(QString::fromUtf8("Preferences/Queueing/MaxActiveUploads"), val); - } - - int getMaxActiveTorrents() const { - return value(QString::fromUtf8("Preferences/Queueing/MaxActiveTorrents"), 5).toInt(); - } - - void setMaxActiveTorrents(int val) { - if (val < 0) val = -1; - setValue(QString::fromUtf8("Preferences/Queueing/MaxActiveTorrents"), val); - } - - void setIgnoreSlowTorrentsForQueueing(bool ignore) { - setValue("Preferences/Queueing/IgnoreSlowTorrents", ignore); - } - - bool ignoreSlowTorrentsForQueueing() const { - return value("Preferences/Queueing/IgnoreSlowTorrents", false).toBool(); - } - - bool isWebUiEnabled() const { - return value("Preferences/WebUI/Enabled", false).toBool(); - } - - void setWebUiEnabled(bool enabled) { - setValue("Preferences/WebUI/Enabled", enabled); - } - - void setWebUiLocalAuthEnabled(bool enabled) { - setValue("Preferences/WebUI/LocalHostAuth", enabled); - } - - bool isWebUiLocalAuthEnabled() const { - return value("Preferences/WebUI/LocalHostAuth", true).toBool(); - } - - quint16 getWebUiPort() const { - return value("Preferences/WebUI/Port", 8080).toInt(); - } - - void setWebUiPort(quint16 port) { - setValue("Preferences/WebUI/Port", port); - } - - bool useUPnPForWebUIPort() const { - return value("Preferences/WebUI/UseUPnP", true).toBool(); - } - - void setUPnPForWebUIPort(bool enabled) { - setValue("Preferences/WebUI/UseUPnP", enabled); - } - - QString getWebUiUsername() const { - return value("Preferences/WebUI/Username", "admin").toString(); - } - - void setWebUiUsername(const QString &username) { - setValue("Preferences/WebUI/Username", username); - } - - void setWebUiPassword(const QString &new_password) { - // Get current password md5 - QString current_pass_md5 = getWebUiPassword(); - // Check if password did not change - if (current_pass_md5 == new_password) return; - // Encode to md5 and save - QCryptographicHash md5(QCryptographicHash::Md5); - md5.addData(getWebUiUsername().toLocal8Bit()+":"+QBT_REALM+":"); - md5.addData(new_password.toLocal8Bit()); - - setValue("Preferences/WebUI/Password_ha1", md5.result().toHex()); - } - - QString getWebUiPassword() const { - QString pass_ha1 = value("Preferences/WebUI/Password_ha1", "").toString(); - if (pass_ha1.isEmpty()) { - QCryptographicHash md5(QCryptographicHash::Md5); - md5.addData(getWebUiUsername().toLocal8Bit()+":"+QBT_REALM+":"); - md5.addData("adminadmin"); - pass_ha1 = md5.result().toHex(); - } - return pass_ha1; - } - - bool isWebUiHttpsEnabled() const { - return value("Preferences/WebUI/HTTPS/Enabled", false).toBool(); - } - - void setWebUiHttpsEnabled(bool enabled) { - setValue("Preferences/WebUI/HTTPS/Enabled", enabled); - } - - QByteArray getWebUiHttpsCertificate() const { - return value("Preferences/WebUI/HTTPS/Certificate").toByteArray(); - } - - void setWebUiHttpsCertificate(const QByteArray &data) { - setValue("Preferences/WebUI/HTTPS/Certificate", data); - } - - QByteArray getWebUiHttpsKey() const { - return value("Preferences/WebUI/HTTPS/Key").toByteArray(); - } - - void setWebUiHttpsKey(const QByteArray &data) { - setValue("Preferences/WebUI/HTTPS/Key", data); - } - - bool isDynDNSEnabled() const { - return value("Preferences/DynDNS/Enabled", false).toBool(); - } - - void setDynDNSEnabled(bool enabled) { - setValue("Preferences/DynDNS/Enabled", enabled); - } - - DNS::Service getDynDNSService() const { - return DNS::Service(value("Preferences/DynDNS/Service", DNS::DYNDNS).toInt()); - } - - void setDynDNSService(int service) { - setValue("Preferences/DynDNS/Service", service); - } - - QString getDynDomainName() const { - return value("Preferences/DynDNS/DomainName", "changeme.dyndns.org").toString(); - } - - void setDynDomainName(const QString &name) { - setValue("Preferences/DynDNS/DomainName", name); - } - - QString getDynDNSUsername() const { - return value("Preferences/DynDNS/Username").toString(); - } - - void setDynDNSUsername(const QString &username) { - setValue("Preferences/DynDNS/Username", username); - } - - QString getDynDNSPassword() const { - return value("Preferences/DynDNS/Password").toString(); - } - - void setDynDNSPassword(const QString &password) { - setValue("Preferences/DynDNS/Password", password); - } + bool isQueueingSystemEnabled() const; + void setQueueingSystemEnabled(bool enabled); + int getMaxActiveDownloads() const; + void setMaxActiveDownloads(int val); + int getMaxActiveUploads() const; + void setMaxActiveUploads(int val); + int getMaxActiveTorrents() const; + void setMaxActiveTorrents(int val); + bool ignoreSlowTorrentsForQueueing() const; + void setIgnoreSlowTorrentsForQueueing(bool ignore); + bool isWebUiEnabled() const; + void setWebUiEnabled(bool enabled); + bool isWebUiLocalAuthEnabled() const; + void setWebUiLocalAuthEnabled(bool enabled); + quint16 getWebUiPort() const; + void setWebUiPort(quint16 port); + bool useUPnPForWebUIPort() const; + void setUPnPForWebUIPort(bool enabled); + QString getWebUiUsername() const; + void setWebUiUsername(const QString &username); + QString getWebUiPassword() const; + void setWebUiPassword(const QString &new_password); + bool isWebUiHttpsEnabled() const; + void setWebUiHttpsEnabled(bool enabled); + QByteArray getWebUiHttpsCertificate() const; + void setWebUiHttpsCertificate(const QByteArray &data); + QByteArray getWebUiHttpsKey() const; + void setWebUiHttpsKey(const QByteArray &data); + bool isDynDNSEnabled() const; + void setDynDNSEnabled(bool enabled); + DNS::Service getDynDNSService() const; + void setDynDNSService(int service); + QString getDynDomainName() const; + void setDynDomainName(const QString &name); + QString getDynDNSUsername() const; + void setDynDNSUsername(const QString &username); + QString getDynDNSPassword() const; + void setDynDNSPassword(const QString &password); // Advanced settings - - void setUILockPassword(const QString &clear_password) { - QCryptographicHash md5(QCryptographicHash::Md5); - md5.addData(clear_password.toLocal8Bit()); - QString md5_password = md5.result().toHex(); - setValue("Locking/password", md5_password); - } - - void clearUILockPassword() { - remove("Locking/password"); - } - - QString getUILockPasswordMD5() const { - return value("Locking/password", QString()).toString(); - } - - bool isUILocked() const { - return value("Locking/locked", false).toBool(); - } - - void setUILocked(bool locked) { - return setValue("Locking/locked", locked); - } - - bool isAutoRunEnabled() const { - return value("AutoRun/enabled", false).toBool(); - } - - void setAutoRunEnabled(bool enabled) { - return setValue("AutoRun/enabled", enabled); - } - - void setAutoRunProgram(const QString &program) { - setValue("AutoRun/program", fsutils::fromNativePath(program)); - } - - QString getAutoRunProgram() const { - return fsutils::fromNativePath(value("AutoRun/program", QString()).toString()); - } - - bool shutdownWhenDownloadsComplete() const { - return value(QString::fromUtf8("Preferences/Downloads/AutoShutDownOnCompletion"), false).toBool(); - } - - void setShutdownWhenDownloadsComplete(bool shutdown) { - setValue(QString::fromUtf8("Preferences/Downloads/AutoShutDownOnCompletion"), shutdown); - } - - bool suspendWhenDownloadsComplete() const { - return value(QString::fromUtf8("Preferences/Downloads/AutoSuspendOnCompletion"), false).toBool(); - } - - void setSuspendWhenDownloadsComplete(bool suspend) { - setValue(QString::fromUtf8("Preferences/Downloads/AutoSuspendOnCompletion"), suspend); - } - - bool hibernateWhenDownloadsComplete() const { - return value(QString::fromUtf8("Preferences/Downloads/AutoHibernateOnCompletion"), false).toBool(); - } - - void setHibernateWhenDownloadsComplete(bool hibernate) { - setValue(QString::fromUtf8("Preferences/Downloads/AutoHibernateOnCompletion"), hibernate); - } - - bool shutdownqBTWhenDownloadsComplete() const { - return value(QString::fromUtf8("Preferences/Downloads/AutoShutDownqBTOnCompletion"), false).toBool(); - } - - void setShutdownqBTWhenDownloadsComplete(bool shutdown) { - setValue(QString::fromUtf8("Preferences/Downloads/AutoShutDownqBTOnCompletion"), shutdown); - } - - uint diskCacheSize() const { - return value(QString::fromUtf8("Preferences/Downloads/DiskWriteCacheSize"), 0).toUInt(); - } - - void setDiskCacheSize(uint size) { - setValue(QString::fromUtf8("Preferences/Downloads/DiskWriteCacheSize"), size); - } - - uint diskCacheTTL() const { - return value(QString::fromUtf8("Preferences/Downloads/DiskWriteCacheTTL"), 60).toUInt(); - } - - void setDiskCacheTTL(uint ttl) { - setValue(QString::fromUtf8("Preferences/Downloads/DiskWriteCacheTTL"), ttl); - } - - uint outgoingPortsMin() const { - return value(QString::fromUtf8("Preferences/Advanced/OutgoingPortsMin"), 0).toUInt(); - } - - void setOutgoingPortsMin(uint val) { - setValue(QString::fromUtf8("Preferences/Advanced/OutgoingPortsMin"), val); - } - - uint outgoingPortsMax() const { - return value(QString::fromUtf8("Preferences/Advanced/OutgoingPortsMax"), 0).toUInt(); - } - - void setOutgoingPortsMax(uint val) { - setValue(QString::fromUtf8("Preferences/Advanced/OutgoingPortsMax"), val); - } - - bool ignoreLimitsOnLAN() const { - return value(QString::fromUtf8("Preferences/Advanced/IgnoreLimitsLAN"), true).toBool(); - } - - void ignoreLimitsOnLAN(bool ignore) { - setValue(QString::fromUtf8("Preferences/Advanced/IgnoreLimitsLAN"), ignore); - } - - bool includeOverheadInLimits() const { - return value(QString::fromUtf8("Preferences/Advanced/IncludeOverhead"), false).toBool(); - } - - void includeOverheadInLimits(bool include) { - setValue(QString::fromUtf8("Preferences/Advanced/IncludeOverhead"), include); - } - - bool trackerExchangeEnabled() const { - return value(QString::fromUtf8("Preferences/Advanced/LtTrackerExchange"), false).toBool(); - } - - void setTrackerExchangeEnabled(bool enable) { - setValue(QString::fromUtf8("Preferences/Advanced/LtTrackerExchange"), enable); - } - - bool recheckTorrentsOnCompletion() const { - return value(QString::fromUtf8("Preferences/Advanced/RecheckOnCompletion"), false).toBool(); - } - - void recheckTorrentsOnCompletion(bool recheck) { - setValue(QString::fromUtf8("Preferences/Advanced/RecheckOnCompletion"), recheck); - } - - unsigned int getRefreshInterval() const { - return value(QString::fromUtf8("Preferences/General/RefreshInterval"), 1500).toUInt(); - } - - void setRefreshInterval(uint interval) { - setValue(QString::fromUtf8("Preferences/General/RefreshInterval"), interval); - } - - bool resolvePeerCountries() const { - return value(QString::fromUtf8("Preferences/Connection/ResolvePeerCountries"), true).toBool(); - } - - void resolvePeerCountries(bool resolve) { - setValue(QString::fromUtf8("Preferences/Connection/ResolvePeerCountries"), resolve); - } - - bool resolvePeerHostNames() const { - return value(QString::fromUtf8("Preferences/Connection/ResolvePeerHostNames"), false).toBool(); - } - - void resolvePeerHostNames(bool resolve) { - setValue(QString::fromUtf8("Preferences/Connection/ResolvePeerHostNames"), resolve); - } - - int getMaxHalfOpenConnections() const { - const int val = value(QString::fromUtf8("Preferences/Connection/MaxHalfOpenConnec"), 50).toInt(); - if (val <= 0) return -1; - return val; - } - - void setMaxHalfOpenConnections(int value) { - if (value <= 0) value = -1; - setValue(QString::fromUtf8("Preferences/Connection/MaxHalfOpenConnec"), value); - } - - - void setNetworkInterface(const QString& iface) { - setValue(QString::fromUtf8("Preferences/Connection/Interface"), iface); - } - - QString getNetworkInterface() const { - return value(QString::fromUtf8("Preferences/Connection/Interface"), QString()).toString(); - } - - void setNetworkInterfaceName(const QString& iface) { - setValue(QString::fromUtf8("Preferences/Connection/InterfaceName"), iface); - } - - QString getNetworkInterfaceName() const { - return value(QString::fromUtf8("Preferences/Connection/InterfaceName"), QString()).toString(); - } - - void setNetworkAddress(const QString& addr) { - setValue(QString::fromUtf8("Preferences/Connection/InetAddress"), addr); - } - - QString getNetworkAddress() const { - return value(QString::fromUtf8("Preferences/Connection/InetAddress"), QString()).toString(); - } - - bool isAnonymousModeEnabled() const { - return value(QString::fromUtf8("Preferences/Advanced/AnonymousMode"), false).toBool(); - } - - void enableAnonymousMode(bool enabled) { - setValue(QString::fromUtf8("Preferences/Advanced/AnonymousMode"), enabled); - } - - bool isSuperSeedingEnabled() const { - return value(QString::fromUtf8("Preferences/Advanced/SuperSeeding"), false).toBool(); - } - - void enableSuperSeeding(bool enabled) { - setValue(QString::fromUtf8("Preferences/Advanced/SuperSeeding"), enabled); - } - - bool announceToAllTrackers() const { - return value(QString::fromUtf8("Preferences/Advanced/AnnounceToAllTrackers"), false).toBool(); - } - - void setAnnounceToAllTrackers(bool enabled) { - setValue(QString::fromUtf8("Preferences/Advanced/AnnounceToAllTrackers"), enabled); - } - + void setUILockPassword(const QString &clear_password); + void clearUILockPassword(); + QString getUILockPasswordMD5() const; + bool isUILocked() const; + void setUILocked(bool locked); + bool isAutoRunEnabled() const; + void setAutoRunEnabled(bool enabled); + QString getAutoRunProgram() const; + void setAutoRunProgram(const QString &program); + bool shutdownWhenDownloadsComplete() const; + void setShutdownWhenDownloadsComplete(bool shutdown); + bool suspendWhenDownloadsComplete() const; + void setSuspendWhenDownloadsComplete(bool suspend); + bool hibernateWhenDownloadsComplete() const; + void setHibernateWhenDownloadsComplete(bool hibernate); + bool shutdownqBTWhenDownloadsComplete() const; + void setShutdownqBTWhenDownloadsComplete(bool shutdown); + uint diskCacheSize() const; + void setDiskCacheSize(uint size); + uint diskCacheTTL() const; + void setDiskCacheTTL(uint ttl); + uint outgoingPortsMin() const; + void setOutgoingPortsMin(uint val); + uint outgoingPortsMax() const; + void setOutgoingPortsMax(uint val); + bool ignoreLimitsOnLAN() const; + void ignoreLimitsOnLAN(bool ignore); + bool includeOverheadInLimits() const; + void includeOverheadInLimits(bool include); + bool trackerExchangeEnabled() const; + void setTrackerExchangeEnabled(bool enable); + bool recheckTorrentsOnCompletion() const; + void recheckTorrentsOnCompletion(bool recheck); + unsigned int getRefreshInterval() const; + void setRefreshInterval(uint interval); + bool resolvePeerCountries() const; + void resolvePeerCountries(bool resolve); + bool resolvePeerHostNames() const; + void resolvePeerHostNames(bool resolve); + int getMaxHalfOpenConnections() const; + void setMaxHalfOpenConnections(int value); + QString getNetworkInterface() const; + void setNetworkInterface(const QString& iface); + QString getNetworkInterfaceName() const; + void setNetworkInterfaceName(const QString& iface); + QString getNetworkAddress() const; + void setNetworkAddress(const QString& addr); + bool isAnonymousModeEnabled() const; + void enableAnonymousMode(bool enabled); + bool isSuperSeedingEnabled() const; + void enableSuperSeeding(bool enabled); + bool announceToAllTrackers() const; + void setAnnounceToAllTrackers(bool enabled); #if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) - bool useSystemIconTheme() const { - return value(QString::fromUtf8("Preferences/Advanced/useSystemIconTheme"), true).toBool(); - } - - void useSystemIconTheme(bool enabled) { - setValue(QString::fromUtf8("Preferences/Advanced/useSystemIconTheme"), enabled); - } + bool useSystemIconTheme() const; + void useSystemIconTheme(bool enabled); #endif - - QStringList getTorrentLabels() const { - return value("TransferListFilters/customLabels").toStringList(); - } - - void addTorrentLabel(const QString& label) { - QStringList labels = value("TransferListFilters/customLabels").toStringList(); - if (!labels.contains(label)) - labels << label; - setValue("TransferListFilters/customLabels", labels); - } - - void removeTorrentLabel(const QString& label) { - QStringList labels = value("TransferListFilters/customLabels").toStringList(); - if (labels.contains(label)) - labels.removeOne(label); - setValue("TransferListFilters/customLabels", labels); - } - - bool recursiveDownloadDisabled() const { - return value(QString::fromUtf8("Preferences/Advanced/DisableRecursiveDownload"), false).toBool(); - } - - void disableRecursiveDownload(bool disable=true) { - setValue(QString::fromUtf8("Preferences/Advanced/DisableRecursiveDownload"), disable); - } - + QStringList getTorrentLabels() const; + void setTorrentLabels(const QStringList& labels); + void addTorrentLabel(const QString& label); + void removeTorrentLabel(const QString& label); + bool recursiveDownloadDisabled() const; + void disableRecursiveDownload(bool disable=true); #ifdef Q_OS_WIN - static QString getPythonPath() { - QSettings reg_python("HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore", QIniSettings::NativeFormat); - QStringList versions = reg_python.childGroups(); - qDebug("Python versions nb: %d", versions.size()); - //versions = versions.filter(QRegExp("2\\..*")); - versions.sort(); - while(!versions.empty()) { - const QString version = versions.takeLast(); - qDebug("Detected possible Python v%s location", qPrintable(version)); - QString path = reg_python.value(version+"/InstallPath/Default", "").toString(); - if (!path.isEmpty() && QDir(path).exists("python.exe")) { - qDebug("Found python.exe at %s", qPrintable(path)); - return path; - } - } - // Fallback: Detect python from default locations - QStringList supported_versions; - supported_versions << "32" << "31" << "30" << "27" << "26" << "25"; - foreach (const QString &v, supported_versions) { - if (QFile::exists("C:/Python"+v+"/python.exe")) { - reg_python.setValue(v[0]+"."+v[1]+"/InstallPath/Default", QString("C:/Python"+v)); - return "C:/Python"+v; - } - } - return QString::null; - } - - bool neverCheckFileAssoc() const { - return value(QString::fromUtf8("Preferences/Win32/NeverCheckFileAssocation"), false).toBool(); - } - - void setNeverCheckFileAssoc(bool check = true) { - setValue(QString::fromUtf8("Preferences/Win32/NeverCheckFileAssocation"), check); - } - - static bool isTorrentFileAssocSet() { - QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat); - if (settings.value(".torrent/Default").toString() != "qBittorrent") { - qDebug(".torrent != qBittorrent"); - return false; - } - - return true; - } - - static bool isMagnetLinkAssocSet() { - QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat); - - // Check magnet link assoc - QRegExp exe_reg("\"([^\"]+)\".*"); - QString shell_command = fsutils::toNativePath(settings.value("magnet/shell/open/command/Default", "").toString()); - if (exe_reg.indexIn(shell_command) < 0) - return false; - QString assoc_exe = exe_reg.cap(1); - qDebug("exe: %s", qPrintable(assoc_exe)); - if (assoc_exe.compare(fsutils::toNativePath(qApp->applicationFilePath()), Qt::CaseInsensitive) != 0) - return false; - return true; - } - - static void setTorrentFileAssoc(bool set) { - QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat); - - // .Torrent association - if (set) { - QString old_progid = settings.value(".torrent/Default").toString(); - if (!old_progid.isEmpty() && (old_progid != "qBittorrent")) - settings.setValue(".torrent/OpenWithProgids/" + old_progid, ""); - settings.setValue(".torrent/Default", "qBittorrent"); - } else if (isTorrentFileAssocSet()) { - settings.setValue(".torrent/Default", ""); - } - - SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0); - } - - static void setMagnetLinkAssoc(bool set) { - QSettings settings("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat); - - // Magnet association - if (set) { - const QString command_str = "\""+qApp->applicationFilePath()+"\" \"%1\""; - const QString icon_str = "\""+qApp->applicationFilePath()+"\",1"; - - settings.setValue("magnet/Default", "URL:Magnet link"); - settings.setValue("magnet/Content Type", "application/x-magnet"); - settings.setValue("magnet/URL Protocol", ""); - settings.setValue("magnet/DefaultIcon/Default", fsutils::toNativePath(icon_str)); - settings.setValue("magnet/shell/Default", "open"); - settings.setValue("magnet/shell/open/command/Default", fsutils::toNativePath(command_str)); - } else if (isMagnetLinkAssocSet()) { - settings.remove("magnet"); - } - - SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0); - } + static QString getPythonPath(); + bool neverCheckFileAssoc() const; + void setNeverCheckFileAssoc(bool check = true); + static bool isTorrentFileAssocSet(); + static bool isMagnetLinkAssocSet(); + static void setTorrentFileAssoc(bool set); + static void setMagnetLinkAssoc(bool set); #endif - - bool isTrackerEnabled() const { - return value(QString::fromUtf8("Preferences/Advanced/trackerEnabled"), false).toBool(); - } - - void setTrackerEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Advanced/trackerEnabled"), enabled); - } - - int getTrackerPort() const { - return value(QString::fromUtf8("Preferences/Advanced/trackerPort"), 9000).toInt(); - } - - void setTrackerPort(int port) { - setValue(QString::fromUtf8("Preferences/Advanced/trackerPort"), port); - } - + bool isTrackerEnabled() const; + void setTrackerEnabled(bool enabled); + int getTrackerPort() const; + void setTrackerPort(int port); #if defined(Q_OS_WIN) || defined(Q_OS_MAC) - bool isUpdateCheckEnabled() const { - return value(QString::fromUtf8("Preferences/Advanced/updateCheck"), true).toBool(); - } - - void setUpdateCheckEnabled(bool enabled) { - setValue(QString::fromUtf8("Preferences/Advanced/updateCheck"), enabled); - } + bool isUpdateCheckEnabled() const; + void setUpdateCheckEnabled(bool enabled); #endif - bool confirmTorrentDeletion() const { - return value(QString::fromUtf8("Preferences/Advanced/confirmTorrentDeletion"), true).toBool(); - } - void setConfirmTorrentDeletion(bool enabled) { - setValue(QString::fromUtf8("Preferences/Advanced/confirmTorrentDeletion"), enabled); - } + bool confirmTorrentDeletion() const; + void setConfirmTorrentDeletion(bool enabled); + TrayIcon::Style trayIconStyle() const; + void setTrayIconStyle(TrayIcon::Style style); - TrayIcon::Style trayIconStyle() const { - return TrayIcon::Style(value(QString::fromUtf8("Preferences/Advanced/TrayIconStyle"), TrayIcon::NORMAL).toInt()); - } - void setTrayIconStyle(TrayIcon::Style style) { - setValue(QString::fromUtf8("Preferences/Advanced/TrayIconStyle"), style); - } + + // Stuff that don't appear in the Options GUI but are saved + // in the same file. + QByteArray getAddNewTorrentDialogState() const; + void setAddNewTorrentDialogState(const QByteArray &state); + int getAddNewTorrentDialogPos() const; + void setAddNewTorrentDialogPos(const int &pos); + int getAddNewTorrentDialogWidth() const; + void setAddNewTorrentDialogWidth(const int &width); + bool getAddNewTorrentDialogExpanded() const; + void setAddNewTorrentDialogExpanded(const bool expanded); + QStringList getAddNewTorrentDialogPathHistory() const; + void setAddNewTorrentDialogPathHistory(const QStringList &history); + QDateTime getDNSLastUpd() const; + void setDNSLastUpd(const QDateTime &date); + QString getDNSLastIP() const; + void setDNSLastIP(const QString &ip); + bool getAcceptedLegal() const; + void setAcceptedLegal(const bool accepted); + QByteArray getMainGeometry() const; + void setMainGeometry(const QByteArray &geometry); + QByteArray getMainVSplitterState() const; + void setMainVSplitterState(const QByteArray &state); + QString getMainLastDir() const; + void setMainLastDir(const QString &path); +#ifndef DISABLE_GUI + QSize getPrefSize(const QSize &defaultSize) const; + void setPrefSize(const QSize &size); +#endif + QPoint getPrefPos() const; + void setPrefPos(const QPoint &pos); + QStringList getPrefHSplitterSizes() const; + void setPrefHSplitterSizes(const QStringList &sizes); + QByteArray getPeerListState() const; + void setPeerListState(const QByteArray &state); + QString getPropSplitterSizes() const; + void setPropSplitterSizes(const QString &sizes); + QByteArray getPropFileListState() const; + void setPropFileListState(const QByteArray &state); + int getPropCurTab() const; + void setPropCurTab(const int &tab); + bool getPropVisible() const; + void setPropVisible(const bool visible); + QByteArray getPropTrackerListState() const; + void setPropTrackerListState(const QByteArray &state); + QByteArray getRssGeometry() const; + void setRssGeometry(const QByteArray &geometry); + QByteArray getRssHSplitterSizes() const; + void setRssHSplitterSizes(const QByteArray &sizes); + QStringList getRssOpenFolders() const; + void setRssOpenFolders(const QStringList &folders); + QByteArray getRssHSplitterState() const; + void setRssHSplitterState(const QByteArray &state); + QByteArray getRssVSplitterState() const; + void setRssVSplitterState(const QByteArray &state); + QString getSearchColsWidth() const; + void setSearchColsWidth(const QString &width); + QStringList getSearchEngDisabled() const; + void setSearchEngDisabled(const QStringList &engines); + QString getCreateTorLastAddPath() const; + void setCreateTorLastAddPath(const QString &path); + QString getCreateTorLastSavePath() const; + void setCreateTorLastSavePath(const QString &path); + QString getCreateTorTrackers() const; + void setCreateTorTrackers(const QString &path); + QByteArray getCreateTorGeometry() const; + void setCreateTorGeometry(const QByteArray &geometry); + bool getCreateTorIgnoreRatio() const; + void setCreateTorIgnoreRatio(const bool ignore); + QString getTorImportLastContentDir() const; + void setTorImportLastContentDir(const QString &path); + QByteArray getTorImportGeometry() const; + void setTorImportGeometry(const QByteArray &geometry); + int getTransSelFilter() const; + void setTransSelFilter(const int &index); + QByteArray getTransHeaderState() const; + void setTransHeaderState(const QByteArray &state); + + // Temp code. + // See TorrentStatistics::loadStats() for details. + QVariantHash getStats() const; + void removeStats(); + + //From old RssSettings class + bool isRSSEnabled() const; + void setRSSEnabled(const bool enabled); + uint getRSSRefreshInterval() const; + void setRSSRefreshInterval(const uint &interval); + int getRSSMaxArticlesPerFeed() const; + void setRSSMaxArticlesPerFeed(const int &nb); + bool isRssDownloadingEnabled() const; + void setRssDownloadingEnabled(const bool b); + QStringList getRssFeedsUrls() const; + void setRssFeedsUrls(const QStringList &rssFeeds); + QStringList getRssFeedsAliases() const; + void setRssFeedsAliases(const QStringList &rssAliases); + QList getHostNameCookies(const QString &host_name) const; + QList getHostNameQNetworkCookies(const QString& host_name) const; + void setHostNameCookies(const QString &host_name, const QList &cookies); }; #endif // PREFERENCES_H diff --git a/src/preferences/preferences.pri b/src/preferences/preferences.pri index 0c4d80335..7b9c3e777 100644 --- a/src/preferences/preferences.pri +++ b/src/preferences/preferences.pri @@ -11,3 +11,5 @@ INCLUDEPATH += $$PWD } HEADERS += $$PWD/preferences.h + +SOURCES += $$PWD/preferences.cpp