qBittorrent
profile_p.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2016 Eugene Shalygin <eugene.shalygin@gmail.com>
4  * Copyright (C) 2012 Christophe Dumez
5  *
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.
10  *
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.
15  *
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.
19  *
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.
28  */
29 
30 #include "profile_p.h"
31 
32 #include <QCoreApplication>
33 
34 Private::Profile::Profile(const QString &configurationName)
35  : m_configurationName {configurationName}
36 {
37 }
38 
40 {
41  return m_configurationName;
42 }
43 
45 {
46  return (m_configurationName.isEmpty() ? QString() : QLatin1Char('_') + m_configurationName);
47 }
48 
50 {
51  return QCoreApplication::applicationName() + configurationSuffix();
52 }
53 
54 Private::DefaultProfile::DefaultProfile(const QString &configurationName)
55  : Profile {configurationName}
56 {
57 }
58 
60 {
61  return {};
62 }
63 
65 {
66  return QDir::homePath();
67 }
68 
70 {
71  return locationWithConfigurationName(QStandardPaths::CacheLocation);
72 }
73 
75 {
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
82 }
83 
85 {
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();
92 
93  const QString dataDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
94  + QLatin1Char('/') + profileName();
95 
96  if (!QDir(dataDir).exists() && QDir(legacyDir).exists())
97  {
98  qWarning("The legacy data directory '%s' is used. It is recommended to move its content to '%s'",
99  qUtf8Printable(legacyDir), qUtf8Printable(dataDir));
100 
101  return legacyDir;
102  }
103 
104  return dataDir;
105 #endif
106 }
107 
109 {
110  return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
111 }
112 
114 {
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
120 }
121 
122 QString Private::DefaultProfile::locationWithConfigurationName(const QStandardPaths::StandardLocation location) const
123 {
124  return QStandardPaths::writableLocation(location) + configurationSuffix();
125 }
126 
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"))}
135 {
136 }
137 
139 {
140  return m_rootDir.absolutePath();
141 }
142 
144 {
145  return m_baseDir.absolutePath();
146 }
147 
149 {
150  return m_cacheLocation;
151 }
152 
154 {
155  return m_configLocation;
156 }
157 
159 {
160  return m_dataLocation;
161 }
162 
164 {
165  return m_downloadLocation;
166 }
167 
169 {
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));
178 }
179 
180 QString Private::NoConvertConverter::fromPortablePath(const QString &portablePath) const
181 {
182  return portablePath;
183 }
184 
185 QString Private::NoConvertConverter::toPortablePath(const QString &path) const
186 {
187  return path;
188 }
189 
190 Private::Converter::Converter(const QString &basePath)
191  : m_baseDir {basePath}
192 {
193  m_baseDir.makeAbsolute();
194 }
195 
196 QString Private::Converter::toPortablePath(const QString &path) const
197 {
198  if (path.isEmpty() || m_baseDir.path().isEmpty())
199  return path;
200 
201 #ifdef Q_OS_WIN
202  if (QDir::isAbsolutePath(path))
203  {
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;
209  }
210 #endif
211  return m_baseDir.relativeFilePath(path);
212 }
213 
214 QString Private::Converter::fromPortablePath(const QString &portablePath) const
215 {
216  if (portablePath.isEmpty() || QDir::isAbsolutePath(portablePath))
217  return portablePath;
218 
219  return QDir::cleanPath(m_baseDir.absoluteFilePath(portablePath));
220 }
Converter(const QString &basePath)
Definition: profile_p.cpp:190
QString fromPortablePath(const QString &portablePath) const override
Definition: profile_p.cpp:214
QString toPortablePath(const QString &path) const override
Definition: profile_p.cpp:196
QString cacheLocation() const override
Definition: profile_p.cpp:148
SettingsPtr applicationSettings(const QString &name) const override
Definition: profile_p.cpp:168
QString dataLocation() const override
Definition: profile_p.cpp:158
QString rootPath() const override
Definition: profile_p.cpp:138
QString downloadLocation() const override
Definition: profile_p.cpp:163
QString configLocation() const override
Definition: profile_p.cpp:153
QString basePath() const override
The base path against to which portable (relative) paths are resolved.
Definition: profile_p.cpp:143
CustomProfile(const QString &rootPath, const QString &configurationName)
Definition: profile_p.cpp:127
SettingsPtr applicationSettings(const QString &name) const override
Definition: profile_p.cpp:113
QString configLocation() const override
Definition: profile_p.cpp:74
QString downloadLocation() const override
Definition: profile_p.cpp:108
QString basePath() const override
The base path against to which portable (relative) paths are resolved.
Definition: profile_p.cpp:64
DefaultProfile(const QString &configurationName)
Definition: profile_p.cpp:54
QString locationWithConfigurationName(QStandardPaths::StandardLocation location) const
Standard path writable location for profile files.
Definition: profile_p.cpp:122
QString cacheLocation() const override
Definition: profile_p.cpp:69
QString dataLocation() const override
Definition: profile_p.cpp:84
QString rootPath() const override
Definition: profile_p.cpp:59
QString fromPortablePath(const QString &portablePath) const override
Definition: profile_p.cpp:180
QString toPortablePath(const QString &path) const override
Definition: profile_p.cpp:185
QString profileName() const
QCoreApplication::applicationName() with optional configuration name appended.
Definition: profile_p.cpp:49
QString configurationName() const
Definition: profile_p.cpp:39
Profile(const QString &configurationName)
Definition: profile_p.cpp:34
QString configurationSuffix() const
Definition: profile_p.cpp:44
std::unique_ptr< QSettings > SettingsPtr
Definition: profile.h:44