author | sheepluva |
Thu, 20 Oct 2011 13:10:24 +0200 | |
changeset 6159 | c780b8cf4d75 |
child 6160 | 863d3edf5690 |
permissions | -rw-r--r-- |
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
1 |
/* |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
3 |
* Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com> |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
4 |
* Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com> |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
5 |
* |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
6 |
* This program is free software; you can redistribute it and/or modify |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
7 |
* it under the terms of the GNU General Public License as published by |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
8 |
* the Free Software Foundation; version 2 of the License |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
9 |
* |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
13 |
* GNU General Public License for more details. |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
14 |
* |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
18 |
*/ |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
19 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
20 |
#include <QStringList> |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
21 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
22 |
#include "hwconsts.h" |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
23 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
24 |
#include "HWDataManager.h" |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
25 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
26 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
27 |
HWDataManager::HWDataManager() |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
28 |
{ |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
29 |
userData = new QDir(cfgdir->absolutePath()); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
30 |
if (!userData->cd("Data")) |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
31 |
userData = NULL; |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
32 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
33 |
defaultData = new QDir(datadir->absolutePath()); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
34 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
35 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
36 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
37 |
HWDataManager & HWDataManager::instance() |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
38 |
{ |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
39 |
static HWDataManager instance; |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
40 |
return instance; |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
41 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
42 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
43 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
44 |
QStringList HWDataManager::entryList( |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
45 |
const QString & subDirectory, |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
46 |
QDir::Filters filters, |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
47 |
const QStringList & nameFilters |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
48 |
) const |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
49 |
{ |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
50 |
QStringList result; |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
51 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
52 |
if (userData != NULL) |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
53 |
{ |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
54 |
QDir tmpDir(*userData); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
55 |
if (tmpDir.cd(subDirectory)) |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
56 |
result.append(tmpDir.entryList(nameFilters, filters)); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
57 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
58 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
59 |
QDir tmpDir(*defaultData); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
60 |
if (tmpDir.cd(subDirectory)) |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
61 |
result.append(tmpDir.entryList(nameFilters, filters)); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
62 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
63 |
result.sort(); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
64 |
result.removeDuplicates(); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
65 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
66 |
return result; |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
67 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
68 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
69 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
70 |
QFile * HWDataManager::findFileForRead( |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
71 |
const QString & relativeDataFilePath) const |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
72 |
{ |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
73 |
QFile * file = |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
74 |
new QFile(userData->absolutePath()+"/"+relativeDataFilePath); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
75 |
if (!file->exists()) |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
76 |
{ |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
77 |
delete file; |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
78 |
file = new QFile(defaultData->absolutePath()+"/"+relativeDataFilePath); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
79 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
80 |
return file; |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
81 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
82 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
83 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
84 |
QFile * HWDataManager::findFileForWrite( |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
85 |
const QString & relativeDataFilePath) const |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
86 |
{ |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
87 |
return new QFile(userData->absolutePath()+"/"+relativeDataFilePath); |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
88 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
89 |