Browse Source
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.adaptive-webui-19844
Eugene Shalygin
9 years ago
20 changed files with 900 additions and 397 deletions
@ -0,0 +1,192 @@ |
|||||||
|
/*
|
||||||
|
* Bittorrent Client using Qt and libtorrent. |
||||||
|
* Copyright (C) 2016 Eugene Shalygin <eugene.shalygin@gmail.com> |
||||||
|
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru> |
||||||
|
* Copyright (C) 2006 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. |
||||||
|
* |
||||||
|
* Contact : chris@qbittorrent.org |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "options.h" |
||||||
|
|
||||||
|
#include <iostream> |
||||||
|
#include <QFileInfo> |
||||||
|
|
||||||
|
#ifdef Q_OS_WIN |
||||||
|
#include <QMessageBox> |
||||||
|
#endif |
||||||
|
|
||||||
|
#include "base/utils/misc.h" |
||||||
|
|
||||||
|
QBtCommandLineParameters::QBtCommandLineParameters() |
||||||
|
: showHelp(false) |
||||||
|
#ifndef Q_OS_WIN |
||||||
|
, showVersion(false) |
||||||
|
#endif |
||||||
|
#ifndef DISABLE_GUI |
||||||
|
, noSplash(false) |
||||||
|
#else |
||||||
|
, shouldDaemonize(false) |
||||||
|
#endif |
||||||
|
, webUiPort(-1) |
||||||
|
, profileDir() |
||||||
|
, portableMode(false) |
||||||
|
, configurationName() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
QBtCommandLineParameters parseCommandLine(const QStringList &args) |
||||||
|
{ |
||||||
|
QBtCommandLineParameters result; |
||||||
|
|
||||||
|
for (int i = 1; i < args.count(); ++i) { |
||||||
|
const QString &arg = args[i]; |
||||||
|
|
||||||
|
if ((arg.startsWith("--") && !arg.endsWith(".torrent")) |
||||||
|
|| (arg.startsWith("-") && (arg.size() == 2))) { |
||||||
|
// Parse known parameters
|
||||||
|
if ((arg == QLatin1String("-h")) || (arg == QLatin1String("--help"))) { |
||||||
|
result.showHelp = true; |
||||||
|
} |
||||||
|
#ifndef Q_OS_WIN |
||||||
|
else if ((arg == QLatin1String("-v")) || (arg == QLatin1String("--version"))) { |
||||||
|
result.showVersion = true; |
||||||
|
} |
||||||
|
#endif |
||||||
|
else if (arg.startsWith(QLatin1String("--webui-port="))) { |
||||||
|
QStringList parts = arg.split(QLatin1Char('=')); |
||||||
|
if (parts.size() == 2) { |
||||||
|
bool ok = false; |
||||||
|
result.webUiPort = parts.last().toInt(&ok); |
||||||
|
if (!ok || (result.webUiPort < 1) || (result.webUiPort > 65535)) |
||||||
|
throw CommandLineParameterError(QObject::tr("%1 must specify the correct port (1 to 65535).") |
||||||
|
.arg(QLatin1String("--webui-port"))); |
||||||
|
} |
||||||
|
} |
||||||
|
#ifndef DISABLE_GUI |
||||||
|
else if (arg == QLatin1String("--no-splash")) { |
||||||
|
result.noSplash = true; |
||||||
|
} |
||||||
|
#else |
||||||
|
else if ((arg == QLatin1String("-d")) || (arg == QLatin1String("--daemon"))) { |
||||||
|
result.shouldDaemonize = true; |
||||||
|
} |
||||||
|
#endif |
||||||
|
else if (arg == QLatin1String("--profile")) { |
||||||
|
QStringList parts = arg.split(QLatin1Char('=')); |
||||||
|
if (parts.size() == 2) |
||||||
|
result.profileDir = parts.last(); |
||||||
|
} |
||||||
|
else if (arg == QLatin1String("--portable")) { |
||||||
|
result.portableMode = true; |
||||||
|
} |
||||||
|
else if (arg == QLatin1String("--configuration")) { |
||||||
|
QStringList parts = arg.split(QLatin1Char('=')); |
||||||
|
if (parts.size() == 2) |
||||||
|
result.configurationName = parts.last(); |
||||||
|
} |
||||||
|
else { |
||||||
|
// Unknown argument
|
||||||
|
result.unknownParameter = arg; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
else { |
||||||
|
QFileInfo torrentPath; |
||||||
|
torrentPath.setFile(arg); |
||||||
|
|
||||||
|
if (torrentPath.exists()) |
||||||
|
result.torrents += torrentPath.absoluteFilePath(); |
||||||
|
else |
||||||
|
result.torrents += arg; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
CommandLineParameterError::CommandLineParameterError(const QString &messageForUser) |
||||||
|
: std::runtime_error(messageForUser.toLocal8Bit().data()) |
||||||
|
, m_messageForUser(messageForUser) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
const QString& CommandLineParameterError::messageForUser() const |
||||||
|
{ |
||||||
|
return m_messageForUser; |
||||||
|
} |
||||||
|
|
||||||
|
QString makeUsage(const QString &prgName) |
||||||
|
{ |
||||||
|
QString text; |
||||||
|
|
||||||
|
text += QObject::tr("Usage:") + QLatin1Char('\n'); |
||||||
|
#ifndef Q_OS_WIN |
||||||
|
text += QLatin1Char('\t') + prgName + QLatin1String(" (-v | --version)") + QLatin1Char('\n'); |
||||||
|
#endif |
||||||
|
text += QLatin1Char('\t') + prgName + QLatin1String(" (-h | --help)") + QLatin1Char('\n'); |
||||||
|
text += QLatin1Char('\t') + prgName |
||||||
|
+ QLatin1String(" [--webui-port=<port>]") |
||||||
|
#ifndef DISABLE_GUI |
||||||
|
+ QLatin1String(" [--no-splash]") |
||||||
|
#else |
||||||
|
+ QLatin1String(" [-d | --daemon]") |
||||||
|
#endif |
||||||
|
+ QLatin1String("[(<filename> | <url>)...]") + QLatin1Char('\n'); |
||||||
|
text += QObject::tr("Options:") + QLatin1Char('\n'); |
||||||
|
#ifndef Q_OS_WIN |
||||||
|
text += QLatin1String("\t-v | --version\t\t") + QObject::tr("Displays program version") + QLatin1Char('\n'); |
||||||
|
#endif |
||||||
|
text += QLatin1String("\t-h | --help\t\t") + QObject::tr("Displays this help message") + QLatin1Char('\n'); |
||||||
|
text += QLatin1String("\t--webui-port=<port>\t") |
||||||
|
+ QObject::tr("Changes the Web UI port") |
||||||
|
+ QLatin1Char('\n'); |
||||||
|
#ifndef DISABLE_GUI |
||||||
|
text += QLatin1String("\t--no-splash\t\t") + QObject::tr("Disable splash screen") + QLatin1Char('\n'); |
||||||
|
#else |
||||||
|
text += QLatin1String("\t-d | --daemon\t\t") + QObject::tr("Run in daemon-mode (background)") + QLatin1Char('\n'); |
||||||
|
#endif |
||||||
|
text += QLatin1String("\t--profile=<dir>\t\t") + QObject::tr("Store configuration files in <dir>") + QLatin1Char('\n'); |
||||||
|
text += QLatin1String("\t--portable\t\t") + QObject::tr("Shortcut for --profile=<exe dir>/profile") + QLatin1Char('\n'); |
||||||
|
text += QLatin1String("\t--configuration=<name>\t\t") + QObject::tr("Store configuration files in directories qBittorrent_<name>") |
||||||
|
+ QLatin1Char('\n'); |
||||||
|
text += QLatin1String("\tfiles or urls\t\t") + QObject::tr("Downloads the torrents passed by the user"); |
||||||
|
|
||||||
|
return text; |
||||||
|
} |
||||||
|
|
||||||
|
void displayUsage(const QString& prgName) |
||||||
|
{ |
||||||
|
#ifndef Q_OS_WIN |
||||||
|
std::cout << qPrintable(makeUsage(prgName)) << std::endl; |
||||||
|
#else |
||||||
|
QMessageBox msgBox(QMessageBox::Information, QObject::tr("Help"), makeUsage(prgName), QMessageBox::Ok); |
||||||
|
msgBox.show(); // Need to be shown or to moveToCenter does not work
|
||||||
|
msgBox.move(Utils::Misc::screenCenter(&msgBox)); |
||||||
|
msgBox.exec(); |
||||||
|
#endif |
||||||
|
} |
||||||
|
|
@ -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)); |
||||||
|
} |
@ -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
|
@ -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)); |
||||||
|
} |
@ -0,0 +1,80 @@ |
|||||||
|
/*
|
||||||
|
* 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_H |
||||||
|
#define QBT_PROFILE_H |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
|
||||||
|
#include <QString> |
||||||
|
#include <QScopedPointer> |
||||||
|
#include <QSettings> |
||||||
|
|
||||||
|
class Application; |
||||||
|
|
||||||
|
namespace Private |
||||||
|
{ |
||||||
|
class Profile; |
||||||
|
} |
||||||
|
|
||||||
|
using SettingsPtr = std::unique_ptr<QSettings>; |
||||||
|
|
||||||
|
enum class SpecialFolder |
||||||
|
{ |
||||||
|
Cache, |
||||||
|
Config, |
||||||
|
Data, |
||||||
|
Downloads |
||||||
|
}; |
||||||
|
|
||||||
|
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
|
Loading…
Reference in new issue