RabbitCommon v2.3.3
Loading...
Searching...
No Matches
RabbitCommonDir.cpp
1// Copyright Copyright (c) Kang Lin studio, All Rights Reserved
2// Author Kang Lin <kl222@126.com>
3
4#include "Log/Log.h"
5#include "RabbitCommonDir.h"
6#include <QStandardPaths>
7#include <QDir>
8#include <QCoreApplication>
9#include <QLoggingCategory>
10
11namespace RabbitCommon {
12
13static Q_LOGGING_CATEGORY(log, "RabbitCommon.Dir")
14
15CDir::CDir()
16{
17 //注意这个必须的在最前
18 m_szDocumentPath = QStandardPaths::writableLocation(
19 QStandardPaths::DocumentsLocation)
20 + QDir::separator() + "Rabbit"
21 + QDir::separator() + QCoreApplication::applicationName();
22 qInfo(log) << "Document path:" << m_szDocumentPath;
23 QDir d;
24 if(!d.exists(m_szDocumentPath))
25 if(!d.mkpath(m_szDocumentPath))
26 qCritical(log) << "mkdir documents dir fail:" << m_szDocumentPath;
27
28 m_szApplicationDir = QCoreApplication::applicationDirPath();
29 qInfo(log) << "Application dir:" << m_szApplicationDir;
30#if defined (Q_OS_ANDROID)
31 m_szApplicationInstallRootDir = "assets:";
32#else
33 if(m_szApplicationDir.isEmpty())
34 m_szApplicationInstallRootDir = "..";
35 else
36 m_szApplicationInstallRootDir = m_szApplicationDir + QDir::separator() + "..";
37 QDir r(m_szApplicationInstallRootDir);
38 m_szApplicationInstallRootDir = r.absolutePath();
39#endif
40 qInfo(log) << "Application root dir:" << m_szApplicationInstallRootDir;
41}
42
43CDir* CDir::Instance()
44{
45 static CDir* p = nullptr;
46 if(!p)
47 p = new CDir;
48 return p;
49}
50
51QString CDir::GetDirApplication()
52{
53 //qDebug(log) << "GetDirApplication:" << qApp->applicationDirPath();
54 return m_szApplicationDir;
55}
56
57int CDir::SetDirApplication(const QString &szPath)
58{
59 m_szApplicationDir = szPath;
60 return 0;
61}
62
63QString CDir::GetDirApplicationInstallRoot()
64{
65 return m_szApplicationInstallRootDir;
66}
67
68int CDir::SetDirApplicationInstallRoot(const QString &szPath)
69{
70 m_szApplicationInstallRootDir = szPath;
71 return 0;
72}
73
74QString CDir::GetDirConfig(bool bReadOnly)
75{
76 Q_UNUSED(bReadOnly)
77 QString szPath;
78#if defined (Q_OS_ANDROID)
79 if(bReadOnly)
80 return "assets:/etc";
81
82 szPath = GetDirUserDocument() + QDir::separator() + "root" + QDir::separator() + "etc";
83 QDir d;
84 if(!d.exists(szPath))
85 {
86 d.mkpath(szPath);
87 //Todo: Copy assets:/etc to here.
88 //CopyDirectory("assets:/etc", szPath);
89 }
90#else
91 szPath = GetDirApplicationInstallRoot() + QDir::separator() + "etc";
92 QDir d;
93 if(!d.exists(szPath))
94 d.mkpath(szPath);
95#endif
96 return szPath;
97}
98
99QString CDir::GetDirLog()
100{
101 QString szPath;
102 szPath = QStandardPaths::writableLocation(QStandardPaths::TempLocation)
103 + QDir::separator() + "log"
104 + QDir::separator() + "Rabbit"
105 + QDir::separator() + QCoreApplication::applicationName();
106/*#if defined (Q_OS_ANDROID)
107 szPath = GetDirUserDocument() + QDir::separator() + "root" + QDir::separator() + "log";
108#elif defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
109 szPath = "/var/log/" + QCoreApplication::applicationName();
110#else
111 szPath = GetDirApplicationInstallRoot() + QDir::separator() + "log";
112#endif*/
113 QDir d;
114 if(!d.exists(szPath))
115 if(!d.mkpath(szPath))
116 qCritical(log) << "Make path fail:" << szPath;
117 return szPath;
118}
119
120QString CDir::GetDirData(bool bReadOnly)
121{
122 QString szPath;
123 Q_UNUSED(bReadOnly);
124#if defined (Q_OS_ANDROID)
125 if(bReadOnly)
126 return "assets:/share";
127 szPath = GetDirUserDocument() + QDir::separator() + "root" + QDir::separator() + "share";
128 QDir d;
129 if(!d.exists(szPath))
130 {
131 d.mkpath(szPath);
132 //TODO: Copy assets:/etc to here.
133 //CopyDirectory("assets:/share", szPath);
134 }
135#else
136 szPath = GetDirApplicationInstallRoot() + QDir::separator() + "share";
137 QDir d;
138 if(!d.exists(szPath))
139 d.mkpath(szPath);
140#endif
141 return szPath;
142}
143
144QString CDir::GetDirDocument(QString szProjectName, bool bReadOnly)
145{
146 QString szPath = GetDirData(bReadOnly) + QDir::separator() + "doc";
147 if(!szProjectName.isEmpty())
148 szPath += QDir::separator() + szProjectName;
149 return szPath;
150}
151
152QString CDir::GetDirDatabase(bool bReadOnly)
153{
154 QString szPath = GetDirData(bReadOnly) + QDir::separator() + "db";
155 QDir d;
156 if(!d.exists(szPath))
157 d.mkpath(szPath);
158 return szPath;
159}
160
161QString CDir::GetDirDatabaseFile(const QString &szFile, bool bReadOnly)
162{
163 if(szFile.isEmpty())
164 return GetDirDatabase(bReadOnly) + QDir::separator() + "database.db";
165 return GetDirDatabase(bReadOnly) + QDir::separator() + szFile;
166}
167
168QString CDir::GetDirApplicationXml(bool bReadOnly)
169{
170 QString szPath = GetDirConfig(bReadOnly) + QDir::separator() + "xml";
171 QDir d;
172 if(!d.exists(szPath))
173 d.mkpath(szPath);
174 return szPath;
175}
176
177QString CDir::GetDirPlugins(const QString &szDir)
178{
179 QString szPath;
180#if defined (Q_OS_ANDROID)
181 szPath = GetDirApplication();
182#else
183 szPath = GetDirApplicationInstallRoot() + QDir::separator() + "plugins";
184#endif
185 if(!szDir.isEmpty())
186 szPath += QDir::separator() + szDir;
187 return szPath;
188}
189
190QString CDir::GetDirUserDocument()
191{
192 return m_szDocumentPath;
193}
194
195int CDir::SetDirUserDocument(QString szPath)
196{
197 m_szDocumentPath = szPath;
198 if(m_szDocumentPath.isEmpty())
199 {
200 m_szDocumentPath = QStandardPaths::writableLocation(
201 QStandardPaths::DocumentsLocation)
202 + QDir::separator() + "Rabbit"
203 + QDir::separator() + QCoreApplication::applicationName();
204 }
205
206 QDir d;
207 if(!d.exists(szPath))
208 d.mkpath(szPath);
209 return 0;
210}
211
212QString CDir::GetDirUserConfig()
213{
214 QString szPath = GetDirUserDocument() + QDir::separator() + "etc";
215
216 QDir d;
217 if(!d.exists(szPath))
218 {
219 d.mkpath(szPath);
220 }
221 return szPath;
222}
223
224QString CDir::GetDirUserData()
225{
226 QString szPath = GetDirUserDocument() + QDir::separator() + "share";
227
228 QDir d;
229 if(!d.exists(szPath))
230 {
231 d.mkpath(szPath);
232 }
233 return szPath;
234}
235
236QString CDir::GetDirUserDatabase()
237{
238 QString szPath = GetDirUserData() + QDir::separator() + "db";
239 QDir d;
240 if(!d.exists(szPath))
241 d.mkpath(szPath);
242 return szPath;
243}
244
245QString CDir::GetDirUserDatabaseFile(const QString &szFile)
246{
247 if(szFile.isEmpty())
248 return GetDirUserDatabase() + QDir::separator() + "database.db";
249 return GetDirUserDatabase() + QDir::separator() + szFile;
250}
251
252QString CDir::GetDirUserXml()
253{
254 QString szPath = GetDirUserData() + QDir::separator() + "xml";
255 QDir d;
256 if(!d.exists(szPath))
257 d.mkpath(szPath);
258 return szPath;
259}
260
261QString CDir::GetDirUserImage()
262{
263 QString szPath = GetDirUserData() + QDir::separator() + "image";
264 QDir d;
265 if(!d.exists(szPath))
266 d.mkpath(szPath);
267 return szPath;
268}
269
270QString CDir::GetDirTranslations(QString szPrefix)
271{
272#if defined(_DEBUG) || defined(DEBUG)
273 return ":/share/translations";
274#endif
275 QString szPath(szPrefix);
276 if(szPath.isEmpty())
277 {
278 szPath = GetDirData(true);
279 }
280 return szPath + QDir::separator() + "translations";
281}
282
283QString CDir::GetDirIcons(bool bReadOnly)
284{
285 return GetDirData(bReadOnly) + QDir::separator() + "icons";
286}
287
288QString CDir::GetDirPluginsTranslation(QString szDir)
289{
290#if defined(_DEBUG) || defined(DEBUG)
291 return ":/" + szDir + "/translations";
292#elif defined (Q_OS_ANDROID)
293 return "assets:/" + szDir + "/translations";
294#endif
295
296 return GetDirApplicationInstallRoot() + QDir::separator()
297 + szDir + QDir::separator() + "translations";
298}
299
300QString CDir::GetFileApplicationConfigure(bool bReadOnly)
301{
302 return GetDirConfig(bReadOnly) + QDir::separator()
303 + QCoreApplication::applicationName() + ".conf";
304}
305
306QString CDir::GetFileUserConfigure()
307{
308 QString szName = GetDirUserDocument() + QDir::separator()
309 + QCoreApplication::applicationName() + ".conf";
310 return szName;
311}
312
313int CDir::CopyDirectory(const QString &fromDir, const QString &toDir, bool bCoverIfFileExists)
314{
315 QDir formDir_(fromDir);
316 QDir toDir_(toDir);
317
318 if(!toDir_.exists())
319 {
320 if(!toDir_.mkpath(toDir_.absolutePath()))
321 return false;
322 }
323
324 QFileInfoList fileInfoList = formDir_.entryInfoList();
325 foreach(QFileInfo fileInfo, fileInfoList)
326 {
327 if(fileInfo.fileName() == "."|| fileInfo.fileName() == "..")
328 continue;
329
330 if(fileInfo.isDir())
331 {
332 if(!CopyDirectory(fileInfo.filePath(),
333 toDir_.filePath(fileInfo.fileName()),
334 true))
335 return false;
336 }
337 else
338 {
339 if(bCoverIfFileExists && toDir_.exists(fileInfo.fileName()))
340 {
341 toDir_.remove(fileInfo.fileName());
342 }
343 if(!QFile::copy(fileInfo.filePath(), toDir_.filePath(fileInfo.fileName())))
344 {
345 return false;
346 }
347 }
348 }
349 return true;
350}
351
352} //namespace RabbitCommon
QString GetDirData(bool bReadOnly=false)
Get data directory.
QString GetDirDocument(QString szProjectName=QString(), bool bReadOnly=false)
GetDirDocument.
int SetDirUserDocument(QString szPath=QString())
Set user document directory.
QString GetDirTranslations(QString szPrefix=QString())