-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
profile_p.cpp
223 lines (188 loc) · 6.68 KB
/
profile_p.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* 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>
#include "base/global.h"
#include "base/utils/fs.h"
Private::Profile::Profile(const QString &configurationName)
: m_configurationName {configurationName}
{
}
QString Private::Profile::configurationName() const
{
return m_configurationName;
}
QString Private::Profile::configurationSuffix() const
{
return (m_configurationName.isEmpty() ? QString() : (u'_' + m_configurationName));
}
QString Private::Profile::profileName() const
{
return QCoreApplication::applicationName() + configurationSuffix();
}
Private::DefaultProfile::DefaultProfile(const QString &configurationName)
: Profile {configurationName}
{
}
Path Private::DefaultProfile::rootPath() const
{
return {};
}
Path Private::DefaultProfile::basePath() const
{
return Utils::Fs::homePath();
}
Path Private::DefaultProfile::cacheLocation() const
{
return locationWithConfigurationName(QStandardPaths::CacheLocation);
}
Path Private::DefaultProfile::configLocation() const
{
#if defined(Q_OS_WIN)
// On Windows QSettings stores files in FOLDERID_RoamingAppData\AppName
return locationWithConfigurationName(QStandardPaths::AppDataLocation);
#else
return locationWithConfigurationName(QStandardPaths::AppConfigLocation);
#endif
}
Path Private::DefaultProfile::dataLocation() const
{
#if defined(Q_OS_WIN) || defined (Q_OS_MACOS)
return locationWithConfigurationName(QStandardPaths::AppLocalDataLocation);
#else
// On Linux keep using the legacy directory ~/.local/share/data/ if it exists
const Path genericDataPath {QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)};
const Path profilePath {profileName()};
const Path legacyDir = genericDataPath / Path(u"data"_s) / profilePath;
const Path dataDir = genericDataPath / profilePath;
if (!dataDir.exists() && legacyDir.exists())
{
qWarning("The legacy data directory '%s' is used. It is recommended to move its content to '%s'",
qUtf8Printable(legacyDir.toString()), qUtf8Printable(dataDir.toString()));
return legacyDir;
}
return dataDir;
#endif
}
Path Private::DefaultProfile::downloadLocation() const
{
return Path(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
}
std::unique_ptr<QSettings> Private::DefaultProfile::applicationSettings(const QString &name) const
{
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
return std::make_unique<QSettings>(QSettings::IniFormat, QSettings::UserScope, profileName(), name);
#else
return std::make_unique<QSettings>(profileName(), name);
#endif
}
Path Private::DefaultProfile::locationWithConfigurationName(const QStandardPaths::StandardLocation location) const
{
return Path(QStandardPaths::writableLocation(location) + configurationSuffix());
}
Private::CustomProfile::CustomProfile(const Path &rootPath, const QString &configurationName)
: Profile {configurationName}
, m_rootPath {rootPath}
, m_basePath {m_rootPath / Path(profileName())}
, m_cacheLocation {m_basePath / Path(u"cache"_s)}
, m_configLocation {m_basePath / Path(u"config"_s)}
, m_dataLocation {m_basePath / Path(u"data"_s)}
, m_downloadLocation {m_basePath / Path(u"downloads"_s)}
{
}
Path Private::CustomProfile::rootPath() const
{
return m_rootPath;
}
Path Private::CustomProfile::basePath() const
{
return m_basePath;
}
Path Private::CustomProfile::cacheLocation() const
{
return m_cacheLocation;
}
Path Private::CustomProfile::configLocation() const
{
return m_configLocation;
}
Path Private::CustomProfile::dataLocation() const
{
return m_dataLocation;
}
Path Private::CustomProfile::downloadLocation() const
{
return m_downloadLocation;
}
std::unique_ptr<QSettings> 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_MACOS)
const auto CONF_FILE_EXTENSION = u".ini"_s;
#else
const auto CONF_FILE_EXTENSION = u".conf"_s;
#endif
const Path settingsFilePath = configLocation() / Path(name + CONF_FILE_EXTENSION);
return std::make_unique<QSettings>(settingsFilePath.data(), QSettings::IniFormat);
}
Path Private::NoConvertConverter::fromPortablePath(const Path &portablePath) const
{
return portablePath;
}
Path Private::NoConvertConverter::toPortablePath(const Path &path) const
{
return path;
}
Private::Converter::Converter(const Path &basePath)
: m_basePath {basePath}
{
Q_ASSERT(basePath.isAbsolute());
}
Path Private::Converter::toPortablePath(const Path &path) const
{
if (path.isEmpty())
return path;
#ifdef Q_OS_WIN
if (path.isAbsolute())
{
const QChar driveLetter = path.data()[0].toUpper();
const QChar baseDriveLetter = m_basePath.data()[0].toUpper();
const bool onSameDrive = (driveLetter.category() == QChar::Letter_Uppercase) && (driveLetter == baseDriveLetter);
if (!onSameDrive)
return path;
}
#endif
return m_basePath.relativePathOf(path);
}
Path Private::Converter::fromPortablePath(const Path &portablePath) const
{
if (portablePath.isEmpty() || portablePath.isAbsolute())
return portablePath;
return m_basePath / portablePath;
}