2016-05-03 22:45:06 +03:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
Private::Profile::Profile(const QString &configurationName)
|
2017-04-18 16:29:10 +03:00
|
|
|
: m_configurationSuffix {configurationName.isEmpty() ? QString() : QLatin1Char('_') + configurationName}
|
2016-05-03 22:45:06 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-18 16:29:10 +03:00
|
|
|
QString Private::Profile::configurationSuffix() const
|
2016-05-03 22:45:06 +03:00
|
|
|
{
|
2017-04-18 16:29:10 +03:00
|
|
|
return m_configurationSuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::Profile::profileName() const
|
|
|
|
{
|
|
|
|
return QCoreApplication::applicationName() + configurationSuffix();
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Private::DefaultProfile::DefaultProfile(const QString &configurationName)
|
|
|
|
: Profile(configurationName)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::DefaultProfile::baseDirectory() const
|
|
|
|
{
|
|
|
|
return QDir::homePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::DefaultProfile::cacheLocation() const
|
|
|
|
{
|
2017-04-18 16:29:10 +03:00
|
|
|
return locationWithConfigurationName(QStandardPaths::CacheLocation);
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::DefaultProfile::configLocation() const
|
|
|
|
{
|
2017-04-18 16:29:10 +03:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
// On Windows QSettings stores files in FOLDERID_RoamingAppData\AppName
|
|
|
|
return locationWithConfigurationName(QStandardPaths::AppDataLocation);
|
2016-05-03 22:45:06 +03:00
|
|
|
#else
|
2017-04-18 16:29:10 +03:00
|
|
|
return locationWithConfigurationName(QStandardPaths::AppConfigLocation);
|
2016-05-03 22:45:06 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::DefaultProfile::dataLocation() const
|
|
|
|
{
|
2017-04-18 16:29:10 +03:00
|
|
|
#if defined(Q_OS_WIN) || defined (Q_OS_MAC)
|
|
|
|
return locationWithConfigurationName(QStandardPaths::AppLocalDataLocation);
|
2016-05-03 22:45:06 +03:00
|
|
|
#else
|
2017-04-18 16:29:10 +03:00
|
|
|
// on Linux gods know why qBittorrent creates 'data' subdirectory in ~/.local/share/
|
|
|
|
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
|
|
|
|
+ QLatin1String("/data/") + profileName() + QLatin1Char('/');
|
2016-05-03 22:45:06 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::DefaultProfile::downloadLocation() const
|
|
|
|
{
|
|
|
|
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsPtr Private::DefaultProfile::applicationSettings(const QString &name) const
|
|
|
|
{
|
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
2017-04-18 16:29:10 +03:00
|
|
|
return SettingsPtr(new QSettings(QSettings::IniFormat, QSettings::UserScope, profileName(), name));
|
2016-05-03 22:45:06 +03:00
|
|
|
#else
|
2017-04-18 16:29:10 +03:00
|
|
|
return SettingsPtr(new QSettings(profileName(), name));
|
2016-05-03 22:45:06 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:31:43 +03:00
|
|
|
QString Private::DefaultProfile::locationWithConfigurationName(const QStandardPaths::StandardLocation location) const
|
2017-04-18 16:29:10 +03:00
|
|
|
{
|
|
|
|
return QStandardPaths::writableLocation(location) + configurationSuffix();
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:45:06 +03:00
|
|
|
Private::CustomProfile::CustomProfile(const QString &rootPath, const QString &configurationName)
|
|
|
|
: Profile {configurationName}
|
2017-04-18 16:29:10 +03:00
|
|
|
, m_rootDirectory {QDir(rootPath).absoluteFilePath(this->profileName())}
|
2016-05-03 22:45:06 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2016-05-13 21:32:47 +03:00
|
|
|
|
|
|
|
QString Private::NoConvertConverter::fromPortablePath(const QString &portablePath) const
|
|
|
|
{
|
|
|
|
return portablePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::NoConvertConverter::toPortablePath(const QString &path) const
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
Private::Converter::Converter(const QString &basePath)
|
|
|
|
: m_baseDir {basePath}
|
|
|
|
{
|
|
|
|
m_baseDir.makeAbsolute();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::Converter::toPortablePath(const QString &path) const
|
|
|
|
{
|
|
|
|
if (path.isEmpty() || m_baseDir.path().isEmpty())
|
|
|
|
return path;
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (QDir::isAbsolutePath(path)) {
|
2019-02-22 00:31:43 +03:00
|
|
|
const QChar driveLeter = path[0].toUpper();
|
|
|
|
const QChar baseDriveLetter = m_baseDir.path()[0].toUpper();
|
|
|
|
const bool onSameDrive = (driveLeter.category() == QChar::Letter_Uppercase) && (driveLeter == baseDriveLetter);
|
2016-05-13 21:32:47 +03:00
|
|
|
if (!onSameDrive)
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return m_baseDir.relativeFilePath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::Converter::fromPortablePath(const QString &portablePath) const
|
|
|
|
{
|
|
|
|
if (QDir::isAbsolutePath(portablePath))
|
|
|
|
return portablePath;
|
|
|
|
|
|
|
|
return QDir::cleanPath(m_baseDir.absoluteFilePath(portablePath));
|
|
|
|
}
|