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