mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2026-01-01 05:08:05 -06:00
Add support for different configurations. Partially closes #465
It may be useful to have different configurations either for portable
versions or for debugging purposes. To implement this we add two
options, avaliable via command line switches
1. An option to change configuration name ("--configuration"). The name
supplied via this option is appended to
QCoreApplication::applicationName() to form "qBittorrent_<conf_name>"
name for the configuration files.
2. An option to provide a path do directory where all the settings are
stored (kind of profile directory). There is a shortcut "--portable"
which means "use directory 'profile' near the executable location".
In order to implement that we have to perform initialisation of the
profile directories before the SettingStorage and Preferences singletones
are initialised. Thus, options parsing shall be performed without defaults
read from preferences.
This commit is contained in:
@@ -33,6 +33,7 @@ net/private/geoipdatabase.h
|
||||
net/proxyconfigurationmanager.h
|
||||
net/reverseresolution.h
|
||||
net/smtp.h
|
||||
private/profile_p.h
|
||||
rss/private/rssparser.h
|
||||
rss/rssarticle.h
|
||||
rss/rssdownloadrule.h
|
||||
@@ -52,7 +53,7 @@ iconprovider.h
|
||||
indexrange.h
|
||||
logger.h
|
||||
preferences.h
|
||||
qinisettings.h
|
||||
profile.h
|
||||
scanfoldersmodel.h
|
||||
searchengine.h
|
||||
settingsstorage.h
|
||||
@@ -94,6 +95,7 @@ net/private/geoipdatabase.cpp
|
||||
net/proxyconfigurationmanager.cpp
|
||||
net/reverseresolution.cpp
|
||||
net/smtp.cpp
|
||||
private/profile_p.cpp
|
||||
rss/private/rssparser.cpp
|
||||
rss/rssarticle.cpp
|
||||
rss/rssdownloadrule.cpp
|
||||
@@ -112,6 +114,7 @@ filesystemwatcher.cpp
|
||||
iconprovider.cpp
|
||||
logger.cpp
|
||||
preferences.cpp
|
||||
profile.cpp
|
||||
scanfoldersmodel.cpp
|
||||
searchengine.cpp
|
||||
settingsstorage.cpp
|
||||
|
||||
@@ -2,7 +2,6 @@ HEADERS += \
|
||||
$$PWD/types.h \
|
||||
$$PWD/tristatebool.h \
|
||||
$$PWD/filesystemwatcher.h \
|
||||
$$PWD/qinisettings.h \
|
||||
$$PWD/logger.h \
|
||||
$$PWD/settingsstorage.h \
|
||||
$$PWD/settingvalue.h \
|
||||
@@ -55,6 +54,8 @@ HEADERS += \
|
||||
$$PWD/utils/net.h \
|
||||
$$PWD/utils/random.h \
|
||||
$$PWD/utils/string.h \
|
||||
$$PWD/profile.h \
|
||||
$$PWD/private/profile_p.h \
|
||||
$$PWD/unicodestrings.h \
|
||||
$$PWD/torrentfileguard.h \
|
||||
$$PWD/torrentfilter.h \
|
||||
@@ -112,6 +113,8 @@ SOURCES += \
|
||||
$$PWD/utils/net.cpp \
|
||||
$$PWD/utils/random.cpp \
|
||||
$$PWD/utils/string.cpp \
|
||||
$$PWD/profile.cpp \
|
||||
$$PWD/private/profile_p.cpp \
|
||||
$$PWD/torrentfileguard.cpp \
|
||||
$$PWD/torrentfilter.cpp \
|
||||
$$PWD/scanfoldersmodel.cpp \
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#include "statistics.h"
|
||||
|
||||
#include <QDateTime>
|
||||
|
||||
#include <libtorrent/session.hpp>
|
||||
|
||||
#include "base/qinisettings.h"
|
||||
#include "base/bittorrent/sessionstatus.h"
|
||||
#include "base/profile.h"
|
||||
#include "base/bittorrent/session.h"
|
||||
#include "statistics.h"
|
||||
|
||||
static const qint64 SAVE_INTERVAL = 15 * 60 * 1000;
|
||||
|
||||
@@ -64,19 +65,19 @@ void Statistics::save() const
|
||||
if (!m_dirty || ((now - m_lastWrite) < SAVE_INTERVAL))
|
||||
return;
|
||||
|
||||
QIniSettings s("qBittorrent", "qBittorrent-data");
|
||||
SettingsPtr s = Profile::instance().applicationSettings(QLatin1String("qBittorrent-data"));
|
||||
QVariantHash v;
|
||||
v.insert("AlltimeDL", m_alltimeDL + m_sessionDL);
|
||||
v.insert("AlltimeUL", m_alltimeUL + m_sessionUL);
|
||||
s.setValue("Stats/AllStats", v);
|
||||
s->setValue("Stats/AllStats", v);
|
||||
m_dirty = false;
|
||||
m_lastWrite = now;
|
||||
}
|
||||
|
||||
void Statistics::load()
|
||||
{
|
||||
QIniSettings s("qBittorrent", "qBittorrent-data");
|
||||
QVariantHash v = s.value("Stats/AllStats").toHash();
|
||||
SettingsPtr s = Profile::instance().applicationSettings(QLatin1String("qBittorrent-data"));
|
||||
QVariantHash v = s->value("Stats/AllStats").toHash();
|
||||
|
||||
m_alltimeDL = v["AlltimeDL"].toULongLong();
|
||||
m_alltimeUL = v["AlltimeUL"].toULongLong();
|
||||
|
||||
209
src/base/private/profile_p.cpp
Normal file
209
src/base/private/profile_p.cpp
Normal file
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2016 Eugene Shalygin <eugene.shalygin@gmail.com>
|
||||
* Copyright (C) 2012 Christophe Dumez
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "profile_p.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include <QStandardPaths>
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
#include <CoreServices/CoreServices.h>
|
||||
#include <Carbon/Carbon.h>
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include <shlobj.h>
|
||||
#endif
|
||||
|
||||
#include "base/utils/fs.h"
|
||||
|
||||
Private::Profile::Profile(const QString &configurationName)
|
||||
: m_configurationName {configurationName.isEmpty()
|
||||
? QCoreApplication::applicationName()
|
||||
: QCoreApplication::applicationName() + QLatin1Char('_') + configurationName}
|
||||
{
|
||||
}
|
||||
|
||||
QString Private::Profile::configurationName() const
|
||||
{
|
||||
return m_configurationName;
|
||||
}
|
||||
|
||||
Private::DefaultProfile::DefaultProfile(const QString &configurationName)
|
||||
: Profile(configurationName)
|
||||
{
|
||||
}
|
||||
|
||||
QString Private::DefaultProfile::baseDirectory() const
|
||||
{
|
||||
return QDir::homePath();
|
||||
}
|
||||
|
||||
QString Private::DefaultProfile::cacheLocation() const
|
||||
{
|
||||
QString result;
|
||||
#if defined(Q_OS_WIN) || defined(Q_OS_OS2)
|
||||
result = dataLocation() + QLatin1String("cache");
|
||||
#else
|
||||
#ifdef Q_OS_MAC
|
||||
// http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
|
||||
FSRef ref;
|
||||
OSErr err = FSFindFolder(kUserDomain, kCachedDataFolderType, false, &ref);
|
||||
if (err)
|
||||
return QString();
|
||||
QByteArray ba(2048, 0);
|
||||
if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
|
||||
result = QString::fromUtf8(ba).normalized(QString::NormalizationForm_C);
|
||||
result += QLatin1Char('/') + configurationName();
|
||||
#else
|
||||
QString xdgCacheHome = QLatin1String(qgetenv("XDG_CACHE_HOME"));
|
||||
if (xdgCacheHome.isEmpty())
|
||||
xdgCacheHome = QDir::homePath() + QLatin1String("/.cache");
|
||||
xdgCacheHome += QLatin1Char('/') + configurationName();
|
||||
result = xdgCacheHome;
|
||||
#endif
|
||||
#endif
|
||||
if (!result.endsWith("/"))
|
||||
result += "/";
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Private::DefaultProfile::configLocation() const
|
||||
{
|
||||
QString result;
|
||||
#if defined(Q_OS_WIN) || defined(Q_OS_OS2)
|
||||
result = dataLocation() + QLatin1String("config");
|
||||
#else
|
||||
#ifdef Q_OS_MAC
|
||||
result = QDir::homePath() + QLatin1String("/Library/Preferences/") + configurationName();
|
||||
#else
|
||||
QString xdgConfigHome = QLatin1String(qgetenv("XDG_CONFIG_HOME"));
|
||||
if (xdgConfigHome.isEmpty())
|
||||
xdgConfigHome = QDir::homePath() + QLatin1String("/.config");
|
||||
xdgConfigHome += QLatin1Char('/') + configurationName();
|
||||
result = xdgConfigHome;
|
||||
#endif
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Private::DefaultProfile::dataLocation() const
|
||||
{
|
||||
QString result;
|
||||
#if defined(Q_OS_WIN)
|
||||
wchar_t path[MAX_PATH + 1] = {L'\0'};
|
||||
if (SHGetSpecialFolderPathW(0, path, CSIDL_LOCAL_APPDATA, FALSE))
|
||||
result = Utils::Fs::fromNativePath(QString::fromWCharArray(path));
|
||||
if (!QCoreApplication::applicationName().isEmpty())
|
||||
result += QLatin1String("/") + qApp->applicationName();
|
||||
#elif defined(Q_OS_MAC)
|
||||
FSRef ref;
|
||||
OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref);
|
||||
if (err)
|
||||
return QString();
|
||||
QByteArray ba(2048, 0);
|
||||
if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
|
||||
result = QString::fromUtf8(ba).normalized(QString::NormalizationForm_C);
|
||||
result += QLatin1Char('/') + qApp->applicationName();
|
||||
#else
|
||||
QString xdgDataHome = QLatin1String(qgetenv("XDG_DATA_HOME"));
|
||||
if (xdgDataHome.isEmpty())
|
||||
xdgDataHome = QDir::homePath() + QLatin1String("/.local/share");
|
||||
xdgDataHome += QLatin1String("/data/")
|
||||
+ qApp->applicationName();
|
||||
result = xdgDataHome;
|
||||
#endif
|
||||
if (!result.endsWith("/"))
|
||||
result += "/";
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Private::DefaultProfile::downloadLocation() const
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
if (QSysInfo::windowsVersion() <= QSysInfo::WV_XP) // Windows XP
|
||||
return QDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).absoluteFilePath(
|
||||
QCoreApplication::translate("fsutils", "Downloads"));
|
||||
#endif
|
||||
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
||||
}
|
||||
|
||||
SettingsPtr Private::DefaultProfile::applicationSettings(const QString &name) const
|
||||
{
|
||||
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
||||
return SettingsPtr(new QSettings(QSettings::IniFormat, QSettings::UserScope, configurationName(), name));
|
||||
#else
|
||||
return SettingsPtr(new QSettings(configurationName(), name));
|
||||
#endif
|
||||
}
|
||||
|
||||
Private::CustomProfile::CustomProfile(const QString &rootPath, const QString &configurationName)
|
||||
: Profile {configurationName}
|
||||
, m_rootDirectory {QDir(rootPath).absoluteFilePath(this->configurationName())}
|
||||
{
|
||||
}
|
||||
|
||||
QString Private::CustomProfile::baseDirectory() const
|
||||
{
|
||||
return m_rootDirectory.canonicalPath();
|
||||
}
|
||||
|
||||
QString Private::CustomProfile::cacheLocation() const
|
||||
{
|
||||
return m_rootDirectory.absoluteFilePath(QLatin1String(cacheDirName));
|
||||
}
|
||||
|
||||
QString Private::CustomProfile::configLocation() const
|
||||
{
|
||||
return m_rootDirectory.absoluteFilePath(QLatin1String(configDirName));
|
||||
}
|
||||
|
||||
QString Private::CustomProfile::dataLocation() const
|
||||
{
|
||||
return m_rootDirectory.absoluteFilePath(QLatin1String(dataDirName));
|
||||
}
|
||||
|
||||
QString Private::CustomProfile::downloadLocation() const
|
||||
{
|
||||
return m_rootDirectory.absoluteFilePath(QLatin1String(downloadsDirName));
|
||||
}
|
||||
|
||||
SettingsPtr Private::CustomProfile::applicationSettings(const QString &name) const
|
||||
{
|
||||
// here we force QSettings::IniFormat format always because we need it to be portable across platforms
|
||||
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
||||
constexpr const char *CONF_FILE_EXTENSION = ".ini";
|
||||
#else
|
||||
constexpr const char *CONF_FILE_EXTENSION = ".conf";
|
||||
#endif
|
||||
const QString settingsFileName {QDir(configLocation()).absoluteFilePath(name + QLatin1String(CONF_FILE_EXTENSION))};
|
||||
return SettingsPtr(new QSettings(settingsFileName, QSettings::IniFormat));
|
||||
}
|
||||
95
src/base/private/profile_p.h
Normal file
95
src/base/private/profile_p.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2016 Eugene Shalygin <eugene.shalygin@gmail.com>
|
||||
* Copyright (C) 2012 Christophe Dumez
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QBT_PROFILE_P_H
|
||||
#define QBT_PROFILE_P_H
|
||||
|
||||
#include <QDir>
|
||||
#include "base/profile.h"
|
||||
|
||||
namespace Private
|
||||
{
|
||||
class Profile
|
||||
{
|
||||
public:
|
||||
virtual QString baseDirectory() const = 0;
|
||||
virtual QString cacheLocation() const = 0;
|
||||
virtual QString configLocation() const = 0;
|
||||
virtual QString dataLocation() const = 0;
|
||||
virtual QString downloadLocation() const = 0;
|
||||
virtual SettingsPtr applicationSettings(const QString &name) const = 0;
|
||||
|
||||
virtual ~Profile() = default;
|
||||
|
||||
QString configurationName() const;
|
||||
|
||||
protected:
|
||||
Profile(const QString &configurationName);
|
||||
|
||||
private:
|
||||
QString m_configurationName;
|
||||
};
|
||||
|
||||
/// Default implementation. Takes paths from system
|
||||
class DefaultProfile: public Profile
|
||||
{
|
||||
public:
|
||||
DefaultProfile(const QString &configurationName);
|
||||
|
||||
QString baseDirectory() const override;
|
||||
QString cacheLocation() const override;
|
||||
QString configLocation() const override;
|
||||
QString dataLocation() const override;
|
||||
QString downloadLocation() const override;
|
||||
SettingsPtr applicationSettings(const QString &name) const override;
|
||||
};
|
||||
|
||||
/// Custom tree: creates directories under the specified root directory
|
||||
class CustomProfile: public Profile
|
||||
{
|
||||
public:
|
||||
CustomProfile(const QString &rootPath, const QString &configurationName);
|
||||
|
||||
QString baseDirectory() const override;
|
||||
QString cacheLocation() const override;
|
||||
QString configLocation() const override;
|
||||
QString dataLocation() const override;
|
||||
QString downloadLocation() const override;
|
||||
SettingsPtr applicationSettings(const QString &name) const override;
|
||||
|
||||
private:
|
||||
QDir m_rootDirectory;
|
||||
static constexpr const char *cacheDirName = "cache";
|
||||
static constexpr const char *configDirName = "config";
|
||||
static constexpr const char *dataDirName = "data";
|
||||
static constexpr const char *downloadsDirName = "downloads";
|
||||
};
|
||||
}
|
||||
#endif // QBT_PROFILE_P_H
|
||||
101
src/base/profile.cpp
Normal file
101
src/base/profile.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2016 Eugene Shalygin <eugene.shalygin@gmail.com>
|
||||
* Copyright (C) 2012 Christophe Dumez
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "profile.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include "private/profile_p.h"
|
||||
|
||||
Profile *Profile::m_instance = nullptr;
|
||||
|
||||
Profile::Profile(Private::Profile *impl)
|
||||
: m_impl(impl)
|
||||
{
|
||||
ensureDirectoryExists(SpecialFolder::Cache);
|
||||
ensureDirectoryExists(SpecialFolder::Config);
|
||||
ensureDirectoryExists(SpecialFolder::Data);
|
||||
ensureDirectoryExists(SpecialFolder::Downloads);
|
||||
}
|
||||
|
||||
// to generate correct call to ProfilePrivate::~ProfileImpl()
|
||||
Profile::~Profile() = default;
|
||||
|
||||
void Profile::initialize(const QString &rootProfilePath, const QString &configurationName)
|
||||
{
|
||||
m_instance = new Profile(rootProfilePath.isEmpty()
|
||||
? static_cast<Private::Profile *>(new Private::DefaultProfile(configurationName))
|
||||
: static_cast<Private::Profile *>(new Private::CustomProfile(rootProfilePath, configurationName)));
|
||||
}
|
||||
|
||||
const Profile &Profile::instance()
|
||||
{
|
||||
return *m_instance;
|
||||
}
|
||||
|
||||
QString Profile::location(SpecialFolder folder) const
|
||||
{
|
||||
QString result;
|
||||
switch (folder) {
|
||||
case SpecialFolder::Cache:
|
||||
result = m_impl->cacheLocation();
|
||||
break;
|
||||
case SpecialFolder::Config:
|
||||
result = m_impl->configLocation();
|
||||
break;
|
||||
case SpecialFolder::Data:
|
||||
result = m_impl->dataLocation();
|
||||
break;
|
||||
case SpecialFolder::Downloads:
|
||||
result = m_impl->downloadLocation();
|
||||
break;
|
||||
}
|
||||
|
||||
if (!result.endsWith(QLatin1Char('/')))
|
||||
result += QLatin1Char('/');
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Profile::configurationName() const
|
||||
{
|
||||
return m_impl->configurationName();
|
||||
}
|
||||
|
||||
SettingsPtr Profile::applicationSettings(const QString &name) const
|
||||
{
|
||||
return m_impl->applicationSettings(name);
|
||||
}
|
||||
|
||||
void Profile::ensureDirectoryExists(SpecialFolder folder)
|
||||
{
|
||||
QString locationPath = location(folder);
|
||||
if (!locationPath.isEmpty() && !QDir().mkpath(locationPath))
|
||||
qFatal("Could not create required directory '%s'", qPrintable(locationPath));
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt4 and libtorrent.
|
||||
* Copyright (C) 2006 Christophe Dumez
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2016 Eugene Shalygin <eugene.shalygin@gmail.com>
|
||||
* Copyright (C) 2012 Christophe Dumez
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
@@ -25,49 +26,55 @@
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef QINISETTINGS_H
|
||||
#define QINISETTINGS_H
|
||||
#ifndef QBT_PROFILE_H
|
||||
#define QBT_PROFILE_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <QString>
|
||||
#include <QScopedPointer>
|
||||
#include <QSettings>
|
||||
|
||||
class QIniSettings : public QSettings {
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY (QIniSettings)
|
||||
class Application;
|
||||
|
||||
public:
|
||||
QIniSettings(const QString &organization = "qBittorrent", const QString &application = "qBittorrent", QObject *parent = 0 ):
|
||||
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
||||
QSettings(QSettings::IniFormat, QSettings::UserScope, organization, application, parent)
|
||||
#else
|
||||
QSettings(organization, application, parent)
|
||||
#endif
|
||||
{
|
||||
namespace Private
|
||||
{
|
||||
class Profile;
|
||||
}
|
||||
|
||||
}
|
||||
using SettingsPtr = std::unique_ptr<QSettings>;
|
||||
|
||||
QIniSettings(const QString &fileName, Format format, QObject *parent = 0 ) : QSettings(fileName, format, parent) {
|
||||
|
||||
}
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
QVariant value(const QString & key, const QVariant &defaultValue = QVariant()) const {
|
||||
QString key_tmp(key);
|
||||
QVariant ret = QSettings::value(key_tmp);
|
||||
if (ret.isNull())
|
||||
return defaultValue;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void setValue(const QString &key, const QVariant &val) {
|
||||
QString key_tmp(key);
|
||||
if (format() == QSettings::NativeFormat) // Using registry, don't touch replace here
|
||||
key_tmp.replace("\\", "/");
|
||||
QSettings::setValue(key_tmp, val);
|
||||
}
|
||||
#endif
|
||||
enum class SpecialFolder
|
||||
{
|
||||
Cache,
|
||||
Config,
|
||||
Data,
|
||||
Downloads
|
||||
};
|
||||
|
||||
#endif // QINISETTINGS_H
|
||||
class Profile
|
||||
{
|
||||
public:
|
||||
QString location(SpecialFolder folder) const;
|
||||
SettingsPtr applicationSettings(const QString &name) const;
|
||||
|
||||
/// Returns either default name for configuration file (QCoreApplication::applicationName())
|
||||
/// or the value, supplied via parameters
|
||||
QString configurationName() const;
|
||||
|
||||
static const Profile &instance();
|
||||
|
||||
private:
|
||||
Profile(Private::Profile *impl);
|
||||
~Profile();
|
||||
|
||||
friend class ::Application;
|
||||
static void initialize(const QString &rootProfilePath, const QString &configurationName);
|
||||
void ensureDirectoryExists(SpecialFolder folder);
|
||||
|
||||
QScopedPointer<Private::Profile> m_impl;
|
||||
static Profile *m_instance;
|
||||
};
|
||||
#endif // QBT_PROFILE_H
|
||||
@@ -28,13 +28,14 @@
|
||||
* Contact : chris@qbittorrent.org
|
||||
*/
|
||||
|
||||
#include "rssdownloadrulelist.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QDataStream>
|
||||
#include <QDebug>
|
||||
|
||||
#include "base/preferences.h"
|
||||
#include "base/qinisettings.h"
|
||||
#include "rssdownloadrulelist.h"
|
||||
#include "base/profile.h"
|
||||
|
||||
using namespace Rss;
|
||||
|
||||
@@ -66,14 +67,14 @@ void DownloadRuleList::replace(DownloadRuleList *other)
|
||||
|
||||
void DownloadRuleList::saveRulesToStorage()
|
||||
{
|
||||
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
|
||||
qBTRSS.setValue("download_rules", toVariantHash());
|
||||
SettingsPtr qBTRSS = Profile::instance().applicationSettings(QLatin1String("qBittorrent-rss"));
|
||||
qBTRSS->setValue("download_rules", toVariantHash());
|
||||
}
|
||||
|
||||
void DownloadRuleList::loadRulesFromStorage()
|
||||
{
|
||||
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
|
||||
loadRulesFromVariantHash(qBTRSS.value("download_rules").toHash());
|
||||
SettingsPtr qBTRSS = Profile::instance().applicationSettings(QLatin1String("qBittorrent-rss"));
|
||||
loadRulesFromVariantHash(qBTRSS->value("download_rules").toHash());
|
||||
}
|
||||
|
||||
QVariantHash DownloadRuleList::toVariantHash() const
|
||||
|
||||
@@ -30,11 +30,13 @@
|
||||
* Contact: chris@qbittorrent.org, arnaud@qbittorrent.org
|
||||
*/
|
||||
|
||||
#include "rssfeed.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "base/preferences.h"
|
||||
#include "base/qinisettings.h"
|
||||
#include "base/logger.h"
|
||||
#include "base/profile.h"
|
||||
#include "base/bittorrent/session.h"
|
||||
#include "base/bittorrent/magneturi.h"
|
||||
#include "base/utils/misc.h"
|
||||
@@ -46,7 +48,6 @@
|
||||
#include "rssarticle.h"
|
||||
#include "rssfolder.h"
|
||||
#include "rssmanager.h"
|
||||
#include "rssfeed.h"
|
||||
|
||||
namespace Rss
|
||||
{
|
||||
@@ -99,7 +100,7 @@ void Feed::saveItemsToDisk()
|
||||
|
||||
m_dirty = false;
|
||||
|
||||
QIniSettings qBTRSSFeeds("qBittorrent", "qBittorrent-rss-feeds");
|
||||
SettingsPtr qBTRSSFeeds = Profile::instance().applicationSettings(QLatin1String("qBittorrent-rss-feeds"));
|
||||
QVariantList oldItems;
|
||||
|
||||
ArticleHash::ConstIterator it = m_articles.begin();
|
||||
@@ -107,16 +108,15 @@ void Feed::saveItemsToDisk()
|
||||
for (; it != itend; ++it)
|
||||
oldItems << it.value()->toHash();
|
||||
qDebug("Saving %d old items for feed %s", oldItems.size(), qPrintable(displayName()));
|
||||
QHash<QString, QVariant> allOldItems = qBTRSSFeeds.value("old_items", QHash<QString, QVariant>()).toHash();
|
||||
QHash<QString, QVariant> allOldItems = qBTRSSFeeds->value("old_items", QHash<QString, QVariant>()).toHash();
|
||||
allOldItems[m_url] = oldItems;
|
||||
qBTRSSFeeds.setValue("old_items", allOldItems);
|
||||
qBTRSSFeeds->setValue("old_items", allOldItems);
|
||||
}
|
||||
|
||||
void Feed::loadItemsFromDisk()
|
||||
{
|
||||
QIniSettings qBTRSSFeeds("qBittorrent", "qBittorrent-rss-feeds");
|
||||
QHash<QString, QVariant> allOldItems = qBTRSSFeeds.value("old_items", QHash<QString, QVariant>()).toHash();
|
||||
|
||||
SettingsPtr qBTRSSFeeds = Profile::instance().applicationSettings(QLatin1String("qBittorrent-rss-feeds"));
|
||||
QHash<QString, QVariant> allOldItems = qBTRSSFeeds->value("old_items", QHash<QString, QVariant>()).toHash();
|
||||
const QVariantList oldItems = allOldItems.value(m_url, QVariantList()).toList();
|
||||
qDebug("Loading %d old items for feed %s", oldItems.size(), qPrintable(displayName()));
|
||||
|
||||
@@ -193,22 +193,22 @@ QString Feed::id() const
|
||||
void Feed::removeAllSettings()
|
||||
{
|
||||
qDebug() << "Removing all settings / history for feed: " << m_url;
|
||||
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
|
||||
QVariantHash feedsWDownloader = qBTRSS.value("downloader_on", QVariantHash()).toHash();
|
||||
SettingsPtr qBTRSS = Profile::instance().applicationSettings(QLatin1String("qBittorrent-rss"));
|
||||
QVariantHash feedsWDownloader = qBTRSS->value("downloader_on", QVariantHash()).toHash();
|
||||
if (feedsWDownloader.contains(m_url)) {
|
||||
feedsWDownloader.remove(m_url);
|
||||
qBTRSS.setValue("downloader_on", feedsWDownloader);
|
||||
qBTRSS->setValue("downloader_on", feedsWDownloader);
|
||||
}
|
||||
QVariantHash allFeedsFilters = qBTRSS.value("feed_filters", QVariantHash()).toHash();
|
||||
QVariantHash allFeedsFilters = qBTRSS->value("feed_filters", QVariantHash()).toHash();
|
||||
if (allFeedsFilters.contains(m_url)) {
|
||||
allFeedsFilters.remove(m_url);
|
||||
qBTRSS.setValue("feed_filters", allFeedsFilters);
|
||||
qBTRSS->setValue("feed_filters", allFeedsFilters);
|
||||
}
|
||||
QIniSettings qBTRSSFeeds("qBittorrent", "qBittorrent-rss-feeds");
|
||||
QVariantHash allOldItems = qBTRSSFeeds.value("old_items", QVariantHash()).toHash();
|
||||
SettingsPtr qBTRSSFeeds = Profile::instance().applicationSettings(QLatin1String("qBittorrent-rss-feeds"));
|
||||
QVariantHash allOldItems = qBTRSSFeeds->value("old_items", QVariantHash()).toHash();
|
||||
if (allOldItems.contains(m_url)) {
|
||||
allOldItems.remove(m_url);
|
||||
qBTRSSFeeds.setValue("old_items", allOldItems);
|
||||
qBTRSSFeeds->setValue("old_items", allOldItems);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,9 +33,9 @@
|
||||
#include <QFile>
|
||||
#include <QHash>
|
||||
#include <QStringList>
|
||||
#include <QSettings>
|
||||
|
||||
#include "logger.h"
|
||||
#include "profile.h"
|
||||
#include "utils/fs.h"
|
||||
|
||||
namespace
|
||||
@@ -62,16 +62,6 @@ namespace
|
||||
QString deserialize(const QString &name, QVariantHash &data);
|
||||
QString serialize(const QString &name, const QVariantHash &data);
|
||||
|
||||
using SettingsPtr = std::unique_ptr<QSettings>;
|
||||
SettingsPtr createSettings(const QString &name)
|
||||
{
|
||||
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
||||
return SettingsPtr(new QSettings(QSettings::IniFormat, QSettings::UserScope, "qBittorrent", name));
|
||||
#else
|
||||
return SettingsPtr(new QSettings("qBittorrent", name));
|
||||
#endif
|
||||
}
|
||||
|
||||
QString m_name;
|
||||
};
|
||||
|
||||
@@ -290,7 +280,7 @@ bool TransactionalSettings::write(const QVariantHash &data)
|
||||
|
||||
QString TransactionalSettings::deserialize(const QString &name, QVariantHash &data)
|
||||
{
|
||||
SettingsPtr settings = createSettings(name);
|
||||
SettingsPtr settings = Profile::instance().applicationSettings(name);
|
||||
|
||||
if (settings->allKeys().isEmpty())
|
||||
return QString();
|
||||
@@ -306,7 +296,7 @@ QString TransactionalSettings::deserialize(const QString &name, QVariantHash &da
|
||||
|
||||
QString TransactionalSettings::serialize(const QString &name, const QVariantHash &data)
|
||||
{
|
||||
SettingsPtr settings = createSettings(name);
|
||||
SettingsPtr settings = Profile::instance().applicationSettings(name);
|
||||
for (auto i = data.begin(); i != data.end(); ++i)
|
||||
settings->setValue(i.key(), i.value());
|
||||
|
||||
|
||||
@@ -28,19 +28,15 @@
|
||||
* Contact : chris@qbittorrent.org
|
||||
*/
|
||||
|
||||
#include "fs.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QSettings>
|
||||
#include <QDirIterator>
|
||||
#include <QCoreApplication>
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
#include <CoreServices/CoreServices.h>
|
||||
#include <Carbon/Carbon.h>
|
||||
#endif
|
||||
|
||||
#ifndef Q_OS_WIN
|
||||
#if defined(Q_OS_MAC) || defined(Q_OS_FREEBSD)
|
||||
#include <sys/param.h>
|
||||
@@ -51,15 +47,10 @@
|
||||
#include <sys/vfs.h>
|
||||
#endif
|
||||
#else
|
||||
#include <shlobj.h>
|
||||
#include <winbase.h>
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
#include <QStandardPaths>
|
||||
|
||||
|
||||
#include "misc.h"
|
||||
#include "fs.h"
|
||||
#include "base/profile.h"
|
||||
|
||||
/**
|
||||
* Converts a path to a string suitable for display.
|
||||
@@ -328,72 +319,17 @@ QString Utils::Fs::expandPathAbs(const QString& path)
|
||||
|
||||
QString Utils::Fs::QDesktopServicesDataLocation()
|
||||
{
|
||||
QString result;
|
||||
#if defined(Q_OS_WIN)
|
||||
wchar_t path[MAX_PATH + 1] = {L'\0'};
|
||||
if (SHGetSpecialFolderPathW(0, path, CSIDL_LOCAL_APPDATA, FALSE))
|
||||
result = fromNativePath(QString::fromWCharArray(path));
|
||||
if (!QCoreApplication::applicationName().isEmpty())
|
||||
result += QLatin1String("/") + qApp->applicationName();
|
||||
#elif defined(Q_OS_MAC)
|
||||
FSRef ref;
|
||||
OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref);
|
||||
if (err)
|
||||
return QString();
|
||||
QByteArray ba(2048, 0);
|
||||
if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
|
||||
result = QString::fromUtf8(ba).normalized(QString::NormalizationForm_C);
|
||||
result += QLatin1Char('/') + qApp->applicationName();
|
||||
#else
|
||||
QString xdgDataHome = QLatin1String(qgetenv("XDG_DATA_HOME"));
|
||||
if (xdgDataHome.isEmpty())
|
||||
xdgDataHome = QDir::homePath() + QLatin1String("/.local/share");
|
||||
xdgDataHome += QLatin1String("/data/")
|
||||
+ qApp->applicationName();
|
||||
result = xdgDataHome;
|
||||
#endif
|
||||
if (!result.endsWith("/"))
|
||||
result += "/";
|
||||
return result;
|
||||
return Profile::instance().location(SpecialFolder::Data);
|
||||
}
|
||||
|
||||
QString Utils::Fs::QDesktopServicesCacheLocation()
|
||||
{
|
||||
QString result;
|
||||
#if defined(Q_OS_WIN) || defined(Q_OS_OS2)
|
||||
result = QDesktopServicesDataLocation() + QLatin1String("cache");
|
||||
#else
|
||||
#ifdef Q_OS_MAC
|
||||
// http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
|
||||
FSRef ref;
|
||||
OSErr err = FSFindFolder(kUserDomain, kCachedDataFolderType, false, &ref);
|
||||
if (err)
|
||||
return QString();
|
||||
QByteArray ba(2048, 0);
|
||||
if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
|
||||
result = QString::fromUtf8(ba).normalized(QString::NormalizationForm_C);
|
||||
result += QLatin1Char('/') + qApp->applicationName();
|
||||
#else
|
||||
QString xdgCacheHome = QLatin1String(qgetenv("XDG_CACHE_HOME"));
|
||||
if (xdgCacheHome.isEmpty())
|
||||
xdgCacheHome = QDir::homePath() + QLatin1String("/.cache");
|
||||
xdgCacheHome += QLatin1Char('/') + QCoreApplication::applicationName();
|
||||
result = xdgCacheHome;
|
||||
#endif
|
||||
#endif
|
||||
if (!result.endsWith("/"))
|
||||
result += "/";
|
||||
return result;
|
||||
return Profile::instance().location(SpecialFolder::Cache);
|
||||
}
|
||||
|
||||
QString Utils::Fs::QDesktopServicesDownloadLocation()
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
if (QSysInfo::windowsVersion() <= QSysInfo::WV_XP) // Windows XP
|
||||
return QDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).absoluteFilePath(
|
||||
QCoreApplication::translate("fsutils", "Downloads"));
|
||||
#endif
|
||||
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
||||
return Profile::instance().location(SpecialFolder::Downloads);
|
||||
}
|
||||
|
||||
QString Utils::Fs::cacheLocation()
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
#include <QByteArray>
|
||||
#include <QDebug>
|
||||
#include <QProcess>
|
||||
#include <QSettings>
|
||||
#include <QThread>
|
||||
#include <QSysInfo>
|
||||
#include <boost/version.hpp>
|
||||
|
||||
Reference in New Issue
Block a user