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)
|
2021-09-11 15:48:29 +03:00
|
|
|
: m_configurationName {configurationName}
|
2016-05-03 22:45:06 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-11 15:48:29 +03:00
|
|
|
QString Private::Profile::configurationName() const
|
|
|
|
{
|
|
|
|
return m_configurationName;
|
|
|
|
}
|
|
|
|
|
2017-04-18 16:29:10 +03:00
|
|
|
QString Private::Profile::configurationSuffix() const
|
2016-05-03 22:45:06 +03:00
|
|
|
{
|
2021-09-11 15:48:29 +03:00
|
|
|
return (m_configurationName.isEmpty() ? QString() : QLatin1Char('_') + m_configurationName);
|
2017-04-18 16:29:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::Profile::profileName() const
|
|
|
|
{
|
|
|
|
return QCoreApplication::applicationName() + configurationSuffix();
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Private::DefaultProfile::DefaultProfile(const QString &configurationName)
|
2021-09-11 15:48:29 +03:00
|
|
|
: Profile {configurationName}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::DefaultProfile::rootPath() const
|
2016-05-03 22:45:06 +03:00
|
|
|
{
|
2021-09-11 15:48:29 +03:00
|
|
|
return {};
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
2021-09-11 15:48:29 +03:00
|
|
|
QString Private::DefaultProfile::basePath() const
|
2016-05-03 22:45:06 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2019-09-05 15:11:33 +03:00
|
|
|
#if defined(Q_OS_WIN) || defined (Q_OS_MACOS)
|
2017-04-18 16:29:10 +03:00
|
|
|
return locationWithConfigurationName(QStandardPaths::AppLocalDataLocation);
|
2016-05-03 22:45:06 +03:00
|
|
|
#else
|
2019-12-12 17:56:16 +03:00
|
|
|
// On Linux keep using the legacy directory ~/.local/share/data/ if it exists
|
|
|
|
const QString legacyDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
|
2022-01-03 19:38:14 +03:00
|
|
|
+ QLatin1String("/data/") + profileName();
|
2019-12-12 17:56:16 +03:00
|
|
|
|
|
|
|
const QString dataDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
|
2022-01-03 19:38:14 +03:00
|
|
|
+ QLatin1Char('/') + profileName();
|
2019-12-12 17:56:16 +03:00
|
|
|
|
2021-01-07 14:12:01 +03:00
|
|
|
if (!QDir(dataDir).exists() && QDir(legacyDir).exists())
|
2020-11-16 10:02:11 +03:00
|
|
|
{
|
2019-12-12 17:56:16 +03:00
|
|
|
qWarning("The legacy data directory '%s' is used. It is recommended to move its content to '%s'",
|
|
|
|
qUtf8Printable(legacyDir), qUtf8Printable(dataDir));
|
|
|
|
|
|
|
|
return legacyDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataDir;
|
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
|
|
|
|
{
|
2019-09-05 15:11:33 +03:00
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
|
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}
|
2021-09-11 15:48:29 +03:00
|
|
|
, m_rootDir {rootPath}
|
|
|
|
, m_baseDir {m_rootDir.absoluteFilePath(profileName())}
|
|
|
|
, m_cacheLocation {m_baseDir.absoluteFilePath(QLatin1String("cache"))}
|
|
|
|
, m_configLocation {m_baseDir.absoluteFilePath(QLatin1String("config"))}
|
|
|
|
, m_dataLocation {m_baseDir.absoluteFilePath(QLatin1String("data"))}
|
|
|
|
, m_downloadLocation {m_baseDir.absoluteFilePath(QLatin1String("downloads"))}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::CustomProfile::rootPath() const
|
2016-05-03 22:45:06 +03:00
|
|
|
{
|
2021-09-11 15:48:29 +03:00
|
|
|
return m_rootDir.absolutePath();
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
2021-09-11 15:48:29 +03:00
|
|
|
QString Private::CustomProfile::basePath() const
|
2016-05-03 22:45:06 +03:00
|
|
|
{
|
2021-09-11 15:48:29 +03:00
|
|
|
return m_baseDir.absolutePath();
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::CustomProfile::cacheLocation() const
|
|
|
|
{
|
2021-09-11 15:48:29 +03:00
|
|
|
return m_cacheLocation;
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::CustomProfile::configLocation() const
|
|
|
|
{
|
2021-09-11 15:48:29 +03:00
|
|
|
return m_configLocation;
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::CustomProfile::dataLocation() const
|
|
|
|
{
|
2021-09-11 15:48:29 +03:00
|
|
|
return m_dataLocation;
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::CustomProfile::downloadLocation() const
|
|
|
|
{
|
2021-09-11 15:48:29 +03:00
|
|
|
return m_downloadLocation;
|
2016-05-03 22:45:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SettingsPtr Private::CustomProfile::applicationSettings(const QString &name) const
|
|
|
|
{
|
|
|
|
// here we force QSettings::IniFormat format always because we need it to be portable across platforms
|
2019-09-05 15:11:33 +03:00
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
|
2021-10-28 09:15:23 +03:00
|
|
|
const char CONF_FILE_EXTENSION[] = ".ini";
|
2016-05-03 22:45:06 +03:00
|
|
|
#else
|
2021-10-28 09:15:23 +03:00
|
|
|
const char CONF_FILE_EXTENSION[] = ".conf";
|
2016-05-03 22:45:06 +03:00
|
|
|
#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
|
2020-11-16 10:02:11 +03:00
|
|
|
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
|
|
|
|
{
|
2019-12-29 23:01:02 +03:00
|
|
|
if (portablePath.isEmpty() || QDir::isAbsolutePath(portablePath))
|
2016-05-13 21:32:47 +03:00
|
|
|
return portablePath;
|
|
|
|
|
|
|
|
return QDir::cleanPath(m_baseDir.absoluteFilePath(portablePath));
|
|
|
|
}
|