Debian Codegraph @

src/filesystem/dstandardpaths.cpp

raw
1 // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dstandardpaths.h"
6
7 #include <QProcessEnvironment>
8 #include <unistd.h>
9 #include <pwd.h>
10
11 DCORE_BEGIN_NAMESPACE
12
13 class DSnapStandardPathsPrivate
14 {
15 public:
16 inline static QString writableLocation(QStandardPaths::StandardLocation /*type*/)
17 {
18 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
19 return env.value("SNAP_USER_COMMON");
20 }
21
22 inline static QStringList standardLocations(QStandardPaths::StandardLocation type)
23 {
24 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
25
26 switch (type) {
27 case QStandardPaths::GenericDataLocation: {
28 QString snapRoot = env.value("SNAP");
29 QString genericDataDir = snapRoot + PREFIX"/share/";
30 return QStringList() << genericDataDir;
31 }
32 default:
33 break;
34 }
35
36 return QStringList() << env.value("SNAP_USER_COMMON");
37 }
38
39 private:
40 DSnapStandardPathsPrivate();
41 ~DSnapStandardPathsPrivate();
42 Q_DISABLE_COPY(DSnapStandardPathsPrivate)
43 };
44
45
46 /*!
47 \class Dtk::Core::DStandardPaths
48 \inmodule dtkcore
49 \brief DStandardPaths提供兼容Snap/Dtk标准的路径模式。DStandardPaths实现了Qt的QStandardPaths主要接口.
50 \sa QStandardPaths
51 */
52
53 /*!
54 \enum Dtk::Core::DStandardPaths::Mode
55 \brief DStandardPaths支持的路径产生模式。
56 \value Auto
57 \brief 和Qt标准的行为表现一致。
58 \value Snap
59 \brief 读取SNAP相关的环境变量,支持将配置存储在SNAP对应目录。
60 \value Test
61 \brief 和Qt标准的行为表现一致,但是会开启测试模式,参考QStandardPaths::setTestModeEnabled。
62 */
63
64
65 static DStandardPaths::Mode s_mode = DStandardPaths::Auto;
66
67 QString DStandardPaths::writableLocation(QStandardPaths::StandardLocation type)
68 {
69 switch (s_mode) {
70 case Auto:
71 case Test:
72 return QStandardPaths::writableLocation(type);
73 case Snap:
74 return DSnapStandardPathsPrivate::writableLocation(type);
75 }
76 return QStandardPaths::writableLocation(type);
77 }
78
79 QStringList DStandardPaths::standardLocations(QStandardPaths::StandardLocation type)
80 {
81 switch (s_mode) {
82 case Auto:
83 case Test:
84 return QStandardPaths::standardLocations(type);
85 case Snap:
86 return DSnapStandardPathsPrivate::standardLocations(type);
87 }
88 return QStandardPaths::standardLocations(type);
89 }
90
91 QString DStandardPaths::locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
92 {
93 return QStandardPaths::locate(type, fileName, options);
94 }
95
96 QStringList DStandardPaths::locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
97 {
98 return QStandardPaths::locateAll(type, fileName, options);
99 }
100
101 QString DStandardPaths::findExecutable(const QString &executableName, const QStringList &paths)
102 {
103 return QStandardPaths::findExecutable(executableName, paths);
104 }
105
106 void DStandardPaths::setMode(DStandardPaths::Mode mode)
107 {
108 s_mode = mode;
109 QStandardPaths::setTestModeEnabled(mode == Test);
110 }
111
112 // https://gitlabwh.uniontech.com/wuhan/se/deepin-specifications/-/issues/21
113
114 QString DStandardPaths::homePath()
115 {
116 const QByteArray &home = qgetenv("HOME");
117
118 if (!home.isEmpty())
119 return QString::fromLocal8Bit(home);
120
121 return homePath(getuid());
122 }
123
124 QString DStandardPaths::path(DStandardPaths::XDG type)
125 {
126 switch (type) {
127 case XDG::DataHome: {
128 const QByteArray &path = qgetenv("XDG_DATA_HOME");
129 if (!path.isEmpty())
130 return QString::fromLocal8Bit(path);
131 return homePath() + QStringLiteral("/.local/share");
132 }
133 case XDG::CacheHome: {
134 const QByteArray &path = qgetenv("XDG_CACHE_HOME");
135 if (!path.isEmpty())
136 return QString::fromLocal8Bit(path);
137 return homePath() + QStringLiteral("/.cache");
138 }
139 case XDG::ConfigHome: {
140 const QByteArray &path = qgetenv("XDG_CONFIG_HOME");
141 if (!path.isEmpty())
142 return QString::fromLocal8Bit(path);
143 return homePath() + QStringLiteral("/.config");
144 }
145 case XDG::RuntimeDir: {
146 const QByteArray &path = qgetenv("XDG_RUNTIME_DIR");
147 if (!path.isEmpty())
148 return QString::fromLocal8Bit(path);
149 return QStringLiteral("/run/user/") + QString::number(getuid());
150 }
151 case XDG::StateHome: {
152 const QByteArray &path = qgetenv("XDG_STATE_HOME");
153 if (!path.isEmpty())
154 return QString::fromLocal8Bit(path);
155 #ifdef Q_OS_LINUX
156 return homePath() + QStringLiteral("/.local/state");
157 #else
158 // TODO: handle it on mac
159 return QString();
160 #endif
161 }
162 }
163 return QString();
164 }
165
166 QString DStandardPaths::path(DStandardPaths::DSG type)
167 {
168 const auto list = paths(type);
169 return list.isEmpty() ? nullptr : list.first();
170 }
171
172 QStringList DStandardPaths::paths(DSG type)
173 {
174 QStringList paths;
175
176 if (type == DSG::DataDir) {
177 const QByteArray &path = qgetenv("DSG_DATA_DIRS");
178 if (path.isEmpty()) {
179 return {QLatin1String(PREFIX"/share/dsg")};
180 }
181 const auto list = path.split(':');
182 paths.reserve(list.size());
183 for (const auto &i : list)
184 paths.push_back(QString::fromLocal8Bit(i));
185 } else if (type == DSG::AppData) {
186 const QByteArray &path = qgetenv("DSG_APP_DATA");
187 //TODO 应用数据目录规范:`/persistent/appdata/{appid}`, now `appid` is not captured.
188 paths.push_back(QString::fromLocal8Bit(path));
189 }
190
191 return paths;
192 }
193
194 QString DStandardPaths::filePath(DStandardPaths::XDG type, QString fileName)
195 {
196 const QString &dir = path(type);
197
198 if (dir.isEmpty())
199 return QString();
200
201 return dir + QLatin1Char('/') + fileName;
202 }
203
204 QString DStandardPaths::filePath(DStandardPaths::DSG type, const QString fileName)
205 {
206 const QString &dir = path(type);
207
208 if (dir.isEmpty())
209 return QString();
210
211 return dir + QLatin1Char('/') + fileName;
212 }
213
214 QString DStandardPaths::homePath(const uint uid)
215 {
216 struct passwd *pw = getpwuid(uid);
217
218 if (!pw)
219 return QString();
220
221 const char *homedir = pw->pw_dir;
222 return QString::fromLocal8Bit(homedir);
223 }
224
225 DCORE_END_NAMESPACE