|
1 /* |
|
2 * Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 */ |
|
18 |
|
19 /** |
|
20 * @file |
|
21 * @brief DataManager class implementation |
|
22 */ |
|
23 |
|
24 #include <QMap> |
|
25 #include <QStringList> |
|
26 |
|
27 #include <QFileInfo> |
|
28 |
|
29 #include "hwconsts.h" |
|
30 |
|
31 #include "DataManager.h" |
|
32 |
|
33 |
|
34 DataManager::DataManager() |
|
35 { |
|
36 m_userData = new QDir(cfgdir->absolutePath()); |
|
37 if (!m_userData->cd("Data")) |
|
38 m_userData = NULL; |
|
39 |
|
40 m_defaultData = new QDir(datadir->absolutePath()); |
|
41 |
|
42 m_hatModel = NULL; |
|
43 m_mapModel = NULL; |
|
44 m_themeModel = NULL; |
|
45 } |
|
46 |
|
47 |
|
48 DataManager & DataManager::instance() |
|
49 { |
|
50 static DataManager instance; |
|
51 return instance; |
|
52 } |
|
53 |
|
54 |
|
55 QStringList DataManager::entryList( |
|
56 const QString & subDirectory, |
|
57 QDir::Filters filters, |
|
58 const QStringList & nameFilters |
|
59 ) const |
|
60 { |
|
61 QStringList result; |
|
62 |
|
63 if (m_userData != NULL) |
|
64 { |
|
65 QDir tmpDir(*m_userData); |
|
66 if (tmpDir.cd(subDirectory)) |
|
67 result.append(tmpDir.entryList(nameFilters, filters)); |
|
68 } |
|
69 |
|
70 QDir tmpDir(*m_defaultData); |
|
71 if (tmpDir.cd(subDirectory)) |
|
72 result.append(tmpDir.entryList(nameFilters, filters)); |
|
73 |
|
74 result.removeDuplicates(); |
|
75 |
|
76 // sort case-insensitive |
|
77 QMap<QString, QString> sortedFileNames; |
|
78 foreach ( QString fn, result) |
|
79 { |
|
80 sortedFileNames.insert(fn.toLower(), fn); |
|
81 } |
|
82 result = sortedFileNames.values(); |
|
83 |
|
84 return result; |
|
85 } |
|
86 |
|
87 |
|
88 QString DataManager::findFileForRead( |
|
89 const QString & relativeDataFilePath) const |
|
90 { |
|
91 QString path; |
|
92 |
|
93 if (m_userData != NULL) |
|
94 path = m_userData->absolutePath()+"/"+relativeDataFilePath; |
|
95 |
|
96 if ((!path.isEmpty()) && (!QFile::exists(path))) |
|
97 path = m_defaultData->absolutePath()+"/"+relativeDataFilePath; |
|
98 |
|
99 return path; |
|
100 } |
|
101 |
|
102 |
|
103 QString DataManager::findFileForWrite( |
|
104 const QString & relativeDataFilePath) const |
|
105 { |
|
106 if (m_userData != NULL) |
|
107 { |
|
108 QString path = m_userData->absolutePath()+"/"+relativeDataFilePath; |
|
109 |
|
110 // create folders if needed |
|
111 QDir tmp; |
|
112 tmp.mkpath(QFileInfo(path).absolutePath()); |
|
113 |
|
114 return path; |
|
115 } |
|
116 |
|
117 |
|
118 return ""; |
|
119 } |
|
120 |
|
121 GameStyleModel * DataManager::gameStyleModel() |
|
122 { |
|
123 if (m_gameStyleModel == NULL) { |
|
124 m_gameStyleModel = new GameStyleModel(); |
|
125 m_gameStyleModel->loadGameStyles(); |
|
126 } |
|
127 return m_gameStyleModel; |
|
128 } |
|
129 |
|
130 HatModel * DataManager::hatModel() |
|
131 { |
|
132 if (m_hatModel == NULL) { |
|
133 m_hatModel = new HatModel(); |
|
134 m_hatModel->loadHats(); |
|
135 } |
|
136 return m_hatModel; |
|
137 } |
|
138 |
|
139 MapModel * DataManager::mapModel() |
|
140 { |
|
141 if (m_mapModel == NULL) { |
|
142 m_mapModel = new MapModel(); |
|
143 m_mapModel->loadMaps(); |
|
144 } |
|
145 return m_mapModel; |
|
146 } |
|
147 |
|
148 ThemeModel * DataManager::themeModel() |
|
149 { |
|
150 if (m_themeModel == NULL) { |
|
151 m_themeModel = new ThemeModel(); |
|
152 m_themeModel->loadThemes(); |
|
153 } |
|
154 return m_themeModel; |
|
155 } |
|
156 |
|
157 void DataManager::reload() |
|
158 { |
|
159 m_gameStyleModel->loadGameStyles(); |
|
160 m_hatModel->loadHats(); |
|
161 m_mapModel->loadMaps(); |
|
162 m_themeModel->loadThemes(); |
|
163 emit updated(); |
|
164 } |