1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "custompaths.h" |
6 | #include "fileoperation.h" |
7 | #include "config.h" |
8 | |
9 | #include <QDir> |
10 | #include <QStandardPaths> |
11 | #include <QCoreApplication> |
12 | #include <QtConcurrent> |
13 | |
14 | /*! |
15 | * 程序运行有两种状态: |
16 | * 1. 程序运行于/usr/bin 则成为installed状态 |
17 | * (1). 在installed状态下运行程序时,分别对应加载全局配置文件、插件资源等(安装时的资源路径)。 |
18 | * (2). 还会加载用户目录下会存在相应副本,用于用户临时更改的任何配置文件、插件资源(用户使用生成,用户作用域)。 |
19 | * 这么做的原因是,区别于用户绑定的插件、或者配置项。即切换用户后在用户目录下存在不同的插件场景 |
20 | * 2. 程序运行于非/usr/bin 则统称为builded状态,该状态下默认指向源码构建。 |
21 | * |
22 | * 对于class CustomPaths接口user/global而言,两种组态外部调用者并不关心。 |
23 | * 所以内部将通过程序运行时分别两种组态以达到不同的组合运行效果 |
24 | */ |
25 | |
26 | using FO = FileOperation; |
27 | class PathMode |
28 | { |
29 | PathMode() = delete; |
30 | Q_DISABLE_COPY(PathMode) |
31 | public: |
32 | static QString installed(CustomPaths::Flags flags); |
33 | static QString builded(CustomPaths::Flags flags); |
34 | static bool isRunAppBuilded(); |
35 | static bool isInstalled(CustomPaths::Flags flags, const QString &path); |
36 | static QString userHome(); |
37 | static QString usreCachePath(); |
38 | static QString userConfigurePath(); |
39 | static QString userDataPath(); |
40 | }; |
41 | |
42 | QString formatString(QString str) |
43 | { |
44 | if (str.back() == QDir::separator()) { |
45 | str.chop(1); |
46 | } |
47 | return str; |
48 | } |
49 | |
50 | QString PathMode::installed(CustomPaths::Flags flags) |
51 | { |
52 | switch (flags) { |
53 | case CustomPaths::Applition: |
54 | return formatString(RUNTIME_INSTALL_RPEFIX); |
55 | case CustomPaths::DependLibs: |
56 | return formatString(LIBRARY_INSTALL_PREFIX); |
57 | case CustomPaths::Plugins: |
58 | return formatString(PLUGIN_INSTALL_RPEFIX); |
59 | case CustomPaths::Tools: |
60 | return formatString(LIBRARY_INSTALL_PREFIX) + QDir::separator() + "tools" ; |
61 | case CustomPaths::Packages: |
62 | return formatString(LIBRARY_INSTALL_PREFIX) + QDir::separator() + "packages" ; |
63 | case CustomPaths::Resources: |
64 | return formatString(SOURCES_INSTALL_RPEFIX) + QDir::separator() + "resource" ; |
65 | case CustomPaths::Configures: |
66 | return formatString(SOURCES_INSTALL_RPEFIX) + QDir::separator() + "configures" ; |
67 | case CustomPaths::Scripts: |
68 | return formatString(LIBRARY_INSTALL_PREFIX) + QDir::separator() + "scripts" ; |
69 | case CustomPaths::Translations: |
70 | return formatString(SOURCES_INSTALL_RPEFIX) + QDir::separator() + "translations" ; |
71 | case CustomPaths::Templates: |
72 | return formatString(SOURCES_INSTALL_RPEFIX) + QDir::separator() + "templates" ; |
73 | default: |
74 | return "" ; |
75 | } |
76 | } |
77 | |
78 | bool PathMode::isInstalled(CustomPaths::Flags flags, const QString &path) |
79 | { |
80 | switch (flags) { |
81 | case CustomPaths::Applition: |
82 | return RUNTIME_INSTALL_RPEFIX == path; |
83 | default: |
84 | return false; |
85 | } |
86 | } |
87 | |
88 | QString PathMode::userHome() |
89 | { |
90 | return QStandardPaths::writableLocation(QStandardPaths::HomeLocation); |
91 | } |
92 | |
93 | QString PathMode::builded(CustomPaths::Flags flags) |
94 | { |
95 | switch (flags) { |
96 | case CustomPaths::Applition: |
97 | return formatString(RUNTIME_BUILD_RPEFIX); |
98 | case CustomPaths::DependLibs: |
99 | return formatString(LIBRARY_BUILD_PREFIX); |
100 | case CustomPaths::Plugins: |
101 | return formatString(PLUGIN_BUILD_RPEFIX); |
102 | case CustomPaths::Tools: |
103 | return formatString(RUNTIME_BUILD_RPEFIX); |
104 | case CustomPaths::Resources: |
105 | return formatString(PROJECT_SOURCE_RPEFIX) + QDir::separator() + "resource" ; |
106 | case CustomPaths::Configures: |
107 | return formatString(PROJECT_SOURCE_RPEFIX) + QDir::separator() + "configures" ; |
108 | case CustomPaths::Scripts: |
109 | return formatString(PROJECT_SOURCE_RPEFIX) + QDir::separator() + "scripts" ; |
110 | case CustomPaths::Templates: |
111 | return formatString(ASSETS_SOURCE_RPEFIX) + QDir::separator() + "templates" ; |
112 | case CustomPaths::Translations: |
113 | return formatString(ASSETS_SOURCE_RPEFIX) + QDir::separator() + "translations" ; |
114 | default: |
115 | return "" ; |
116 | } |
117 | } |
118 | |
119 | QString PathMode::usreCachePath() |
120 | { |
121 | return FO::checkCreateDir(FO::checkCreateDir(userHome(), ".cache" ), "deepin-unioncode" ); |
122 | } |
123 | |
124 | QString PathMode::userConfigurePath() |
125 | { |
126 | return FO::checkCreateDir(FO::checkCreateDir(userHome(), ".config" ), "deepin-unioncode" ); |
127 | } |
128 | |
129 | QString PathMode::userDataPath() |
130 | { |
131 | return FO::checkCreateDir(FO::checkCreateDir(userHome(), ".data" ), "deepin-unioncode" ); |
132 | } |
133 | |
134 | QString CustomPaths::endSeparator(const QString &path) |
135 | { |
136 | if (!path.endsWith(QDir::separator())) |
137 | return path + QDir::separator(); |
138 | return path; |
139 | } |
140 | |
141 | QString CustomPaths::projectGeneratePath(const QString &path) |
142 | { |
143 | auto result = endSeparator(path) + "build" ; |
144 | if (!QDir(result).exists()) { |
145 | QDir().mkdir(result); |
146 | } |
147 | return result; |
148 | } |
149 | |
150 | QString CustomPaths::lspRuntimePath(const QString &language) |
151 | { |
152 | QString lspRuntimePath = FO::checkCreateDir(CustomPaths::user(CustomPaths::Tools), "lsp" ); |
153 | if (language.isEmpty()) { |
154 | return lspRuntimePath; |
155 | } else { |
156 | QString languageTemp = language; |
157 | languageTemp = languageTemp.replace(QDir::separator(), "_" ); |
158 | return FO::checkCreateDir(lspRuntimePath, languageTemp); |
159 | } |
160 | } |
161 | |
162 | bool CustomPaths::checkDir(const QString &path) |
163 | { |
164 | if (!QDir(path).exists()) { |
165 | return QDir().mkpath(path); |
166 | } |
167 | return false; |
168 | } |
169 | |
170 | QString CustomPaths::projectCachePath(const QString &projectPath) |
171 | { |
172 | return FO::checkCreateDir(projectPath, ".unioncode" ); |
173 | } |
174 | |
175 | QString CustomPaths::user(CustomPaths::Flags flag) |
176 | { |
177 | switch (flag) { |
178 | case Applition: |
179 | return qApp->applicationDirPath(); |
180 | case Plugins: |
181 | return FileOperation::checkCreateDir(PathMode::usreCachePath(), "plugins" ); |
182 | case Tools: |
183 | return FileOperation::checkCreateDir(PathMode::usreCachePath(), "tools" ); |
184 | case Extensions: |
185 | return FileOperation::checkCreateDir(PathMode::usreCachePath(), "extensions" ); |
186 | case Configures: |
187 | return FileOperation::checkCreateDir(PathMode::userConfigurePath(), "configures" ); |
188 | case Scripts: |
189 | return FileOperation::checkCreateDir(PathMode::userConfigurePath(), "Scripts" ); |
190 | case Templates: |
191 | return FileOperation::checkCreateDir(PathMode::userConfigurePath(), "templates" ); |
192 | default: |
193 | return "" ; |
194 | } |
195 | } |
196 | |
197 | QString CustomPaths::global(CustomPaths::Flags flags) |
198 | { |
199 | if (installed()) { |
200 | return PathMode::installed(flags); |
201 | } else { |
202 | return PathMode::builded(flags); |
203 | } |
204 | } |
205 | |
206 | bool CustomPaths::installed() |
207 | { |
208 | return PathMode::isInstalled(CustomPaths::Applition, QCoreApplication::applicationDirPath()); |
209 | } |
210 | |