Debian Codegraph @

snap-standard-paths/SnapStandardPaths.cpp

raw symbols SCIP
1 /* Copyright (C) 2017 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu devices
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (at your option) version 3
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "SnapStandardPaths.h"
19 #include <QDir>
20 #include <QFileInfo>
21 #include <QHash>
22 #include <QStandardPaths>
23 #include <QCoreApplication>
24
25 #define SNAP "SNAP"
26 #define SNAP_DATA "SNAP_DATA"
27 #define SNAP_COMMON "SNAP_COMMON"
28 #define SNAP_USER_DATA "SNAP_USER_DATA"
29 #define SNAP_USER_COMMON "SNAP_USER_COMMON"
30
31
32 static bool ssp_testMode = false;
33
34 static QString getPath(const QByteArray &type)
35 {
36 return QString::fromLatin1(qgetenv(type));
37 }
38
39 static bool hasPath(const QByteArray &type)
40 {
41 return !getPath(type).isEmpty();
42 }
43
44
45 QString SnapStandardPaths::writableLocation(SnapStandardPaths::StandardLocation location)
46 {
47 const QString appName = QStringLiteral("/%1").arg(QCoreApplication::applicationName());
48 const QString dataLoc = QStringLiteral("/.local/share");
49 const QString cacheLoc = QStringLiteral("/.cache");
50 const QString configLoc = QStringLiteral("/.config");
51 switch (location) {
52 case HomeLocation:
53 if (hasPath(SNAP_USER_DATA))
54 return getPath(SNAP_USER_DATA);
55 else
56 return QDir::homePath();
57 case CommonHomeLocation:
58 if (hasPath(SNAP_USER_COMMON))
59 return getPath(SNAP_USER_COMMON);
60 else
61 return QDir::homePath();
62 case CacheLocation:
63 if (hasPath(SNAP_USER_DATA))
64 return getPath(SNAP_USER_DATA) + cacheLoc;
65 else
66 return QDir::homePath() + cacheLoc;
67 case CommonCacheLocation:
68 if (hasPath(SNAP_USER_COMMON))
69 return getPath(SNAP_USER_COMMON) + cacheLoc;
70 else
71 return QDir::homePath() + cacheLoc;
72 case AppCacheLocation:
73 return SnapStandardPaths::writableLocation(CacheLocation) + appName;
74 case CommonAppCacheLocation:
75 return SnapStandardPaths::writableLocation(CommonCacheLocation) + appName;
76 case DataLocation:
77 if (hasPath(SNAP_USER_DATA))
78 return getPath(SNAP_USER_DATA) + dataLoc;
79 else
80 return QDir::homePath() + dataLoc;
81 case CommonDataLocation:
82 if (hasPath(SNAP_USER_COMMON))
83 return getPath(SNAP_USER_COMMON) + dataLoc;
84 else
85 return QDir::homePath() + dataLoc;
86 case AppDataLocation:
87 return SnapStandardPaths::writableLocation(DataLocation) + appName;
88 case CommonAppDataLocation:
89 return SnapStandardPaths::writableLocation(CommonDataLocation) + appName;
90 case ConfigLocation:
91 if (hasPath(SNAP_USER_DATA))
92 return getPath(SNAP_USER_DATA) + configLoc;
93 else
94 return QDir::homePath() + configLoc;
95 case CommonConfigLocation:
96 if (hasPath(SNAP_USER_COMMON))
97 return getPath(SNAP_USER_COMMON) + configLoc;
98 else
99 return QDir::homePath() + configLoc;
100 case AppConfigLocation:
101 return SnapStandardPaths::writableLocation(DataLocation) + appName;
102 case CommonAppConfigLocation:
103 return SnapStandardPaths::writableLocation(DataLocation) + appName;
104 default:
105 break;
106 }
107 return QString();
108 }
109
110 QStringList SnapStandardPaths::standardLocations(SnapStandardPaths::StandardLocation location)
111 {
112 QStringList dirs;
113 switch (location) {
114 case HomeLocation:
115 if (hasPath(SNAP_USER_DATA)) {
116 dirs << getPath(SNAP_USER_DATA);
117 } else {
118 dirs << QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
119 }
120 break;
121 case CommonHomeLocation:
122 if (hasPath(SNAP_USER_COMMON)) {
123 dirs << getPath(SNAP_USER_COMMON);
124 } else {
125 dirs << QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
126 }
127 break;
128 case CacheLocation:
129 if (hasPath(SNAP_USER_DATA)) {
130 dirs << getPath(SNAP_USER_DATA) + QStringLiteral("/.cache");
131 } else {
132 dirs << QStandardPaths::standardLocations(QStandardPaths::CacheLocation);
133 }
134 break;
135 case CommonCacheLocation:
136 if (hasPath(SNAP_USER_COMMON)) {
137 dirs << getPath(SNAP_USER_COMMON) + QStringLiteral("/.cache");
138 } else {
139 dirs << QStandardPaths::standardLocations(QStandardPaths::CacheLocation);
140 }
141 break;
142 case AppCacheLocation:
143 if (hasPath(SNAP_USER_DATA)) {
144 dirs << getPath(SNAP_USER_DATA) + QStringLiteral("/.cache/%1").arg(QCoreApplication::applicationName());
145 } else {
146 dirs << QStandardPaths::standardLocations(QStandardPaths::CacheLocation);
147 }
148 break;
149 case CommonAppCacheLocation:
150 if (hasPath(SNAP_USER_COMMON)) {
151 dirs << getPath(SNAP_USER_COMMON) + QStringLiteral("/.cache/%1").arg(QCoreApplication::applicationName());
152 } else {
153 dirs << QStandardPaths::standardLocations(QStandardPaths::CacheLocation);
154 }
155 break;
156 case DataLocation:
157 if (hasPath(SNAP_USER_DATA) && hasPath(SNAP_DATA)) {
158 dirs << getPath(SNAP_USER_DATA) + QStringLiteral("/.local/share");
159 dirs << getPath(SNAP_DATA);
160 dirs << getPath(SNAP) + QStringLiteral("/usr/share");
161 dirs << getPath(SNAP) + QStringLiteral("/usr/local/share");
162 } else {
163 dirs << QStandardPaths::standardLocations(QStandardPaths::DataLocation);
164 }
165 break;
166 case CommonDataLocation:
167 if (hasPath(SNAP_USER_COMMON) && hasPath(SNAP_COMMON)) {
168 dirs << getPath(SNAP_USER_COMMON) + QStringLiteral("/.local/share");
169 dirs << getPath(SNAP_COMMON);
170 } else {
171 dirs << QStandardPaths::standardLocations(QStandardPaths::DataLocation);
172 }
173 break;
174 case AppDataLocation:
175 if (hasPath(SNAP_USER_DATA) && hasPath(SNAP_DATA)) {
176 dirs << getPath(SNAP_USER_DATA) + QStringLiteral("/.local/share/%1").arg(QCoreApplication::applicationName());
177 dirs << getPath(SNAP_DATA) + QStringLiteral("/%1").arg(QCoreApplication::applicationName());
178 dirs << getPath(SNAP) + QStringLiteral("/usr/share/%1").arg(QCoreApplication::applicationName());
179 dirs << getPath(SNAP) + QStringLiteral("/usr/local/share/%1").arg(QCoreApplication::applicationName());
180 } else {
181 dirs << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
182 }
183 break;
184 case CommonAppDataLocation:
185 if (hasPath(SNAP_USER_COMMON) && hasPath(SNAP_COMMON)) {
186 dirs << getPath(SNAP_USER_COMMON) + QStringLiteral("/.local/share/%1").arg(QCoreApplication::applicationName());
187 dirs << getPath(SNAP_COMMON) + QStringLiteral("/%1").arg(QCoreApplication::applicationName());
188 } else {
189 dirs << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
190 }
191 break;
192 case ConfigLocation:
193 if (hasPath(SNAP_USER_DATA)) {
194 dirs << getPath(SNAP_USER_DATA) + QStringLiteral("/.config");
195 } else {
196 dirs << QStandardPaths::standardLocations(QStandardPaths::ConfigLocation);
197 }
198 break;
199 case CommonConfigLocation:
200 if (hasPath(SNAP_USER_COMMON)) {
201 dirs << getPath(SNAP_USER_COMMON) + QStringLiteral("/.config");
202 } else {
203 dirs << QStandardPaths::standardLocations(QStandardPaths::ConfigLocation);
204 }
205 break;
206 case AppConfigLocation:
207 if (hasPath(SNAP_USER_DATA)) {
208 dirs << getPath(SNAP_USER_DATA) + QStringLiteral("/.config/%1").arg(QCoreApplication::applicationName());
209 } else {
210 #if QT_VERSION >= 0x050500
211 dirs << QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);
212 #else
213 dirs << QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QCoreApplication::applicationName();
214 #endif
215 }
216 break;
217 case CommonAppConfigLocation:
218 if (hasPath(SNAP_USER_COMMON)) {
219 dirs << getPath(SNAP_USER_COMMON) + QStringLiteral("/.config/%1").arg(QCoreApplication::applicationName());
220 } else {
221 #if QT_VERSION >= 0x050500
222 dirs << QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);
223 #else
224 dirs << QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/" + QCoreApplication::applicationName();
225 #endif
226
227 }
228 break;
229 }
230 return dirs;
231 }
232
233 void SnapStandardPaths::setTestModeEnabled(bool testMode)
234 {
235 ssp_testMode = testMode;
236 }
237
238 bool SnapStandardPaths::isTestModeEnabled()
239 {
240 return ssp_testMode;
241 }
242