Sync Changelog entries between branches
[qBittorrent.git] / src / base / profile_p.cpp
bloba7c3ec5ae807f0f7abc120d9681d2d7e0e0a1e03
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_configurationName {configurationName}
39 QString Private::Profile::configurationName() const
41 return m_configurationName;
44 QString Private::Profile::configurationSuffix() const
46 return (m_configurationName.isEmpty() ? QString() : QLatin1Char('_') + m_configurationName);
49 QString Private::Profile::profileName() const
51 return QCoreApplication::applicationName() + configurationSuffix();
54 Private::DefaultProfile::DefaultProfile(const QString &configurationName)
55 : Profile {configurationName}
59 QString Private::DefaultProfile::rootPath() const
61 return {};
64 QString Private::DefaultProfile::basePath() const
66 return QDir::homePath();
69 QString Private::DefaultProfile::cacheLocation() const
71 return locationWithConfigurationName(QStandardPaths::CacheLocation);
74 QString Private::DefaultProfile::configLocation() const
76 #if defined(Q_OS_WIN)
77 // On Windows QSettings stores files in FOLDERID_RoamingAppData\AppName
78 return locationWithConfigurationName(QStandardPaths::AppDataLocation);
79 #else
80 return locationWithConfigurationName(QStandardPaths::AppConfigLocation);
81 #endif
84 QString Private::DefaultProfile::dataLocation() const
86 #if defined(Q_OS_WIN) || defined (Q_OS_MACOS)
87 return locationWithConfigurationName(QStandardPaths::AppLocalDataLocation);
88 #else
89 // On Linux keep using the legacy directory ~/.local/share/data/ if it exists
90 const QString legacyDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
91 + QLatin1String("/data/") + profileName() + QLatin1Char('/');
93 const QString dataDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
94 + QLatin1Char('/') + profileName() + QLatin1Char('/');
96 if (!QDir(dataDir).exists() && QDir(legacyDir).exists())
98 qWarning("The legacy data directory '%s' is used. It is recommended to move its content to '%s'",
99 qUtf8Printable(legacyDir), qUtf8Printable(dataDir));
101 return legacyDir;
104 return dataDir;
105 #endif
108 QString Private::DefaultProfile::downloadLocation() const
110 return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
113 SettingsPtr Private::DefaultProfile::applicationSettings(const QString &name) const
115 #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
116 return SettingsPtr(new QSettings(QSettings::IniFormat, QSettings::UserScope, profileName(), name));
117 #else
118 return SettingsPtr(new QSettings(profileName(), name));
119 #endif
122 QString Private::DefaultProfile::locationWithConfigurationName(const QStandardPaths::StandardLocation location) const
124 return QStandardPaths::writableLocation(location) + configurationSuffix();
127 Private::CustomProfile::CustomProfile(const QString &rootPath, const QString &configurationName)
128 : Profile {configurationName}
129 , m_rootDir {rootPath}
130 , m_baseDir {m_rootDir.absoluteFilePath(profileName())}
131 , m_cacheLocation {m_baseDir.absoluteFilePath(QLatin1String("cache"))}
132 , m_configLocation {m_baseDir.absoluteFilePath(QLatin1String("config"))}
133 , m_dataLocation {m_baseDir.absoluteFilePath(QLatin1String("data"))}
134 , m_downloadLocation {m_baseDir.absoluteFilePath(QLatin1String("downloads"))}
138 QString Private::CustomProfile::rootPath() const
140 return m_rootDir.absolutePath();
143 QString Private::CustomProfile::basePath() const
145 return m_baseDir.absolutePath();
148 QString Private::CustomProfile::cacheLocation() const
150 return m_cacheLocation;
153 QString Private::CustomProfile::configLocation() const
155 return m_configLocation;
158 QString Private::CustomProfile::dataLocation() const
160 return m_dataLocation;
163 QString Private::CustomProfile::downloadLocation() const
165 return m_downloadLocation;
168 SettingsPtr Private::CustomProfile::applicationSettings(const QString &name) const
170 // here we force QSettings::IniFormat format always because we need it to be portable across platforms
171 #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
172 const char CONF_FILE_EXTENSION[] = ".ini";
173 #else
174 const char CONF_FILE_EXTENSION[] = ".conf";
175 #endif
176 const QString settingsFileName {QDir(configLocation()).absoluteFilePath(name + QLatin1String(CONF_FILE_EXTENSION))};
177 return SettingsPtr(new QSettings(settingsFileName, QSettings::IniFormat));
180 QString Private::NoConvertConverter::fromPortablePath(const QString &portablePath) const
182 return portablePath;
185 QString Private::NoConvertConverter::toPortablePath(const QString &path) const
187 return path;
190 Private::Converter::Converter(const QString &basePath)
191 : m_baseDir {basePath}
193 m_baseDir.makeAbsolute();
196 QString Private::Converter::toPortablePath(const QString &path) const
198 if (path.isEmpty() || m_baseDir.path().isEmpty())
199 return path;
201 #ifdef Q_OS_WIN
202 if (QDir::isAbsolutePath(path))
204 const QChar driveLeter = path[0].toUpper();
205 const QChar baseDriveLetter = m_baseDir.path()[0].toUpper();
206 const bool onSameDrive = (driveLeter.category() == QChar::Letter_Uppercase) && (driveLeter == baseDriveLetter);
207 if (!onSameDrive)
208 return path;
210 #endif
211 return m_baseDir.relativeFilePath(path);
214 QString Private::Converter::fromPortablePath(const QString &portablePath) const
216 if (portablePath.isEmpty() || QDir::isAbsolutePath(portablePath))
217 return portablePath;
219 return QDir::cleanPath(m_baseDir.absoluteFilePath(portablePath));