mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-12 16:23:07 -07:00
parent
a96cabfe73
commit
d05d5a85a5
7 changed files with 320 additions and 189 deletions
|
@ -30,15 +30,9 @@
|
|||
* Contact : hammered999@gmail.com
|
||||
*/
|
||||
|
||||
#include "preferences.h"
|
||||
#include "qinisettings.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QPair>
|
||||
#include <QDir>
|
||||
#include <QReadLocker>
|
||||
#include <QWriteLocker>
|
||||
|
||||
#ifndef DISABLE_GUI
|
||||
#include <QApplication>
|
||||
|
@ -56,74 +50,19 @@
|
|||
#endif
|
||||
|
||||
#include <cstdlib>
|
||||
#include "base/utils/fs.h"
|
||||
#include "base/utils/misc.h"
|
||||
|
||||
#include "utils/fs.h"
|
||||
#include "utils/misc.h"
|
||||
#include "settingsstorage.h"
|
||||
#include "logger.h"
|
||||
#include "preferences.h"
|
||||
|
||||
Preferences* Preferences::m_instance = 0;
|
||||
|
||||
Preferences::Preferences()
|
||||
: m_randomPort(rand() % 64512 + 1024)
|
||||
, dirty(false)
|
||||
, lock(QReadWriteLock::Recursive)
|
||||
{
|
||||
qRegisterMetaTypeStreamOperators<MaxRatioAction>("MaxRatioAction");
|
||||
|
||||
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()) {
|
||||
Logger::instance()->addMessage(tr("Detected unclean program exit. Using fallback file to restore settings."), Log::WARNING);
|
||||
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
|
||||
QString new_path = settings_new->fileName();
|
||||
delete settings_new;
|
||||
Utils::Fs::forceRemove(new_path);
|
||||
|
||||
if (use_new)
|
||||
save();
|
||||
#endif
|
||||
|
||||
timer.setSingleShot(true);
|
||||
timer.setInterval(5 * 1000);
|
||||
connect(&timer, SIGNAL(timeout()), SLOT(save()));
|
||||
}
|
||||
|
||||
Preferences::~Preferences()
|
||||
{
|
||||
save();
|
||||
}
|
||||
|
||||
Preferences *Preferences::instance()
|
||||
|
@ -145,71 +84,14 @@ void Preferences::freeInstance()
|
|||
}
|
||||
}
|
||||
|
||||
bool Preferences::save()
|
||||
{
|
||||
QWriteLocker locker(&lock);
|
||||
|
||||
if (!dirty) return false;
|
||||
|
||||
#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<QString, QVariant>::const_iterator i = m_data.begin(), e = m_data.end(); i != e; ++i)
|
||||
settings->setValue(i.key(), i.value());
|
||||
|
||||
dirty = false;
|
||||
locker.unlock();
|
||||
|
||||
#ifndef Q_OS_MAC
|
||||
settings->sync(); // Important to get error status
|
||||
QString new_path = settings->fileName();
|
||||
QSettings::Status status = settings->status();
|
||||
|
||||
if (status != QSettings::NoError) {
|
||||
if (status == QSettings::AccessError)
|
||||
Logger::instance()->addMessage(tr("An access error occurred while trying to write the configuration file."), Log::CRITICAL);
|
||||
else
|
||||
Logger::instance()->addMessage(tr("A format error occurred while trying to write the configuration file."), Log::CRITICAL);
|
||||
|
||||
delete settings;
|
||||
Utils::Fs::forceRemove(new_path);
|
||||
return false;
|
||||
}
|
||||
delete settings;
|
||||
QString final_path = new_path;
|
||||
int index = final_path.lastIndexOf("_new", -1, Qt::CaseInsensitive);
|
||||
final_path.remove(index, 4);
|
||||
Utils::Fs::forceRemove(final_path);
|
||||
QFile::rename(new_path, final_path);
|
||||
#else
|
||||
delete settings;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const QVariant Preferences::value(const QString &key, const QVariant &defaultValue) const
|
||||
{
|
||||
QReadLocker locker(&lock);
|
||||
return m_data.value(key, defaultValue);
|
||||
return SettingsStorage::instance()->loadValue(key, defaultValue);
|
||||
}
|
||||
|
||||
void Preferences::setValue(const QString &key, const QVariant &value)
|
||||
{
|
||||
QWriteLocker locker(&lock);
|
||||
if (m_data.value(key) == value)
|
||||
return;
|
||||
dirty = true;
|
||||
timer.start();
|
||||
m_data.insert(key, value);
|
||||
SettingsStorage::instance()->storeValue(key, value);
|
||||
}
|
||||
|
||||
// General options
|
||||
|
@ -643,7 +525,6 @@ void Preferences::setActionOnDblClOnTorrentFn(int act)
|
|||
// Connection options
|
||||
int Preferences::getSessionPort() const
|
||||
{
|
||||
QReadLocker locker(&lock);
|
||||
if (useRandomPort())
|
||||
return m_randomPort;
|
||||
return value("Preferences/Connection/PortRangeMin", 8999).toInt();
|
||||
|
@ -2487,22 +2368,6 @@ void Preferences::setTransHeaderState(const QByteArray &state)
|
|||
#endif
|
||||
}
|
||||
|
||||
// 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
|
||||
{
|
||||
|
@ -2574,31 +2439,6 @@ void Preferences::setToolbarTextPosition(const int position)
|
|||
setValue("Toolbar/textPosition", position);
|
||||
}
|
||||
|
||||
void Preferences::moveRSSCookies()
|
||||
{
|
||||
QList<QNetworkCookie> cookies = getNetworkCookies();
|
||||
QVariantMap hostsTable = value("Rss/hosts_cookies").toMap();
|
||||
foreach (const QString &key, hostsTable.keys()) {
|
||||
QVariant value = hostsTable[key];
|
||||
QList<QByteArray> rawCookies = value.toByteArray().split(':');
|
||||
foreach (const QByteArray &rawCookie, rawCookies) {
|
||||
foreach (QNetworkCookie cookie, QNetworkCookie::parseCookies(rawCookie)) {
|
||||
cookie.setDomain(key);
|
||||
cookie.setPath("/");
|
||||
cookie.setExpirationDate(QDateTime::currentDateTime().addYears(10));
|
||||
cookies << cookie;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setNetworkCookies(cookies);
|
||||
|
||||
QWriteLocker locker(&lock);
|
||||
dirty = true;
|
||||
timer.start();
|
||||
m_data.remove("Rss/hosts_cookies");
|
||||
}
|
||||
|
||||
QList<QNetworkCookie> Preferences::getNetworkCookies() const
|
||||
{
|
||||
QList<QNetworkCookie> cookies;
|
||||
|
@ -2639,8 +2479,31 @@ void Preferences::setSpeedWidgetGraphEnable(int id, const bool enable)
|
|||
setValue("SpeedWidget/graph_enable_" + QString::number(id), enable);
|
||||
}
|
||||
|
||||
void Preferences::upgrade()
|
||||
{
|
||||
// Move RSS cookies to global storage
|
||||
QList<QNetworkCookie> cookies = getNetworkCookies();
|
||||
QVariantMap hostsTable = value("Rss/hosts_cookies").toMap();
|
||||
foreach (const QString &key, hostsTable.keys()) {
|
||||
QVariant value = hostsTable[key];
|
||||
QList<QByteArray> rawCookies = value.toByteArray().split(':');
|
||||
foreach (const QByteArray &rawCookie, rawCookies) {
|
||||
foreach (QNetworkCookie cookie, QNetworkCookie::parseCookies(rawCookie)) {
|
||||
cookie.setDomain(key);
|
||||
cookie.setPath("/");
|
||||
cookie.setExpirationDate(QDateTime::currentDateTime().addYears(10));
|
||||
cookies << cookie;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setNetworkCookies(cookies);
|
||||
|
||||
SettingsStorage::instance()->removeValue("Rss/hosts_cookies");
|
||||
}
|
||||
|
||||
void Preferences::apply()
|
||||
{
|
||||
if (save())
|
||||
if (SettingsStorage::instance()->save())
|
||||
emit changed();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue