Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / profile_p.cpp
blob0c504c6735fbe5877e646f9b6095e576b331d11b
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Eugene Shalygin <eugene.shalygin@gmail.com>
4 * Copyright (C) 2012 Christophe Dumez
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "profile_p.h"
32 #include <QCoreApplication>
34 Private::Profile::Profile(const QString &configurationName)
35 : m_configurationSuffix {configurationName.isEmpty() ? QString() : QLatin1Char('_') + configurationName}
39 QString Private::Profile::configurationSuffix() const
41 return m_configurationSuffix;
44 QString Private::Profile::profileName() const
46 return QCoreApplication::applicationName() + configurationSuffix();
49 Private::DefaultProfile::DefaultProfile(const QString &configurationName)
50 : Profile(configurationName)
54 QString Private::DefaultProfile::baseDirectory() const
56 return QDir::homePath();
59 QString Private::DefaultProfile::cacheLocation() const
61 return locationWithConfigurationName(QStandardPaths::CacheLocation);
64 QString Private::DefaultProfile::configLocation() const
66 #if defined(Q_OS_WIN)
67 // On Windows QSettings stores files in FOLDERID_RoamingAppData\AppName
68 return locationWithConfigurationName(QStandardPaths::AppDataLocation);
69 #else
70 return locationWithConfigurationName(QStandardPaths::AppConfigLocation);
71 #endif
74 QString Private::DefaultProfile::dataLocation() const
76 #if defined(Q_OS_WIN) || defined (Q_OS_MACOS)
77 return locationWithConfigurationName(QStandardPaths::AppLocalDataLocation);
78 #else
79 // On Linux keep using the legacy directory ~/.local/share/data/ if it exists
80 const QString legacyDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
81 + QLatin1String("/data/") + profileName() + QLatin1Char('/');
83 const QString dataDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
84 + QLatin1Char('/') + profileName() + QLatin1Char('/');
86 if (!QDir(dataDir).exists() && QDir(legacyDir).exists())
88 qWarning("The legacy data directory '%s' is used. It is recommended to move its content to '%s'",
89 qUtf8Printable(legacyDir), qUtf8Printable(dataDir));
91 return legacyDir;
94 return dataDir;
95 #endif
98 QString Private::DefaultProfile::downloadLocation() const
100 return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
103 SettingsPtr Private::DefaultProfile::applicationSettings(const QString &name) const
105 #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
106 return SettingsPtr(new QSettings(QSettings::IniFormat, QSettings::UserScope, profileName(), name));
107 #else
108 return SettingsPtr(new QSettings(profileName(), name));
109 #endif
112 QString Private::DefaultProfile::locationWithConfigurationName(const QStandardPaths::StandardLocation location) const
114 return QStandardPaths::writableLocation(location) + configurationSuffix();
117 Private::CustomProfile::CustomProfile(const QString &rootPath, const QString &configurationName)
118 : Profile {configurationName}
119 , m_rootDirectory {QDir(rootPath).absoluteFilePath(this->profileName())}
123 QString Private::CustomProfile::baseDirectory() const
125 return m_rootDirectory.canonicalPath();
128 QString Private::CustomProfile::cacheLocation() const
130 return m_rootDirectory.absoluteFilePath(QLatin1String(cacheDirName));
133 QString Private::CustomProfile::configLocation() const
135 return m_rootDirectory.absoluteFilePath(QLatin1String(configDirName));
138 QString Private::CustomProfile::dataLocation() const
140 return m_rootDirectory.absoluteFilePath(QLatin1String(dataDirName));
143 QString Private::CustomProfile::downloadLocation() const
145 return m_rootDirectory.absoluteFilePath(QLatin1String(downloadsDirName));
148 SettingsPtr Private::CustomProfile::applicationSettings(const QString &name) const
150 // here we force QSettings::IniFormat format always because we need it to be portable across platforms
151 #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
152 constexpr const char *CONF_FILE_EXTENSION = ".ini";
153 #else
154 constexpr const char *CONF_FILE_EXTENSION = ".conf";
155 #endif
156 const QString settingsFileName {QDir(configLocation()).absoluteFilePath(name + QLatin1String(CONF_FILE_EXTENSION))};
157 return SettingsPtr(new QSettings(settingsFileName, QSettings::IniFormat));
160 QString Private::NoConvertConverter::fromPortablePath(const QString &portablePath) const
162 return portablePath;
165 QString Private::NoConvertConverter::toPortablePath(const QString &path) const
167 return path;
170 Private::Converter::Converter(const QString &basePath)
171 : m_baseDir {basePath}
173 m_baseDir.makeAbsolute();
176 QString Private::Converter::toPortablePath(const QString &path) const
178 if (path.isEmpty() || m_baseDir.path().isEmpty())
179 return path;
181 #ifdef Q_OS_WIN
182 if (QDir::isAbsolutePath(path))
184 const QChar driveLeter = path[0].toUpper();
185 const QChar baseDriveLetter = m_baseDir.path()[0].toUpper();
186 const bool onSameDrive = (driveLeter.category() == QChar::Letter_Uppercase) && (driveLeter == baseDriveLetter);
187 if (!onSameDrive)
188 return path;
190 #endif
191 return m_baseDir.relativeFilePath(path);
194 QString Private::Converter::fromPortablePath(const QString &portablePath) const
196 if (portablePath.isEmpty() || QDir::isAbsolutePath(portablePath))
197 return portablePath;
199 return QDir::cleanPath(m_baseDir.absoluteFilePath(portablePath));