-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathprojectList.cpp
More file actions
356 lines (298 loc) · 9.59 KB
/
projectList.cpp
File metadata and controls
356 lines (298 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "projectList.h"
#include <QtXml/QDomDocument>
#ifdef Q_OS_WIN
HWND ProjectList::appWindow = NULL;
#endif
QString DirEntry::getAppPath()
{
return ProjectList::getAppPath(QFileInfo(mPath).absolutePath());
}
QString ProjectEntry::getLevelPath()
{
return ProjectList::getAppPath(QFileInfo(mPath).absolutePath()) + "/levels";
}
ProjectList::ProjectList(QWidget *parent)
{
Q_UNUSED(parent)
mAppName = "No app name has been set";
mParent = NULL;
mProcess = NULL;
}
void ProjectList::setAppName(QString name)
{
mAppName = name;
}
void ProjectList::setParent(QWidget *parent)
{
mParent = parent;
}
QString ProjectList::getAppPath(QString path)
{
QString basePath = QCoreApplication::applicationDirPath();
if(!path.isEmpty())
{
basePath = path;
}
#if defined(Q_OS_MACOS)
int appIndex = basePath.lastIndexOf(QString(".app"));
int basePathIndex = basePath.lastIndexOf("/", appIndex);
QString macPath = basePath.left(basePathIndex);
// since this is getting the app path, lets default it to this
if(path.isEmpty())
{
QDir::setCurrent(macPath);
}
return macPath;
#else
return basePath;
#endif
}
void ProjectList::buildList()
{
// Load in the xml file, parse it, and build out the controls
QDomDocument doc("projects");
QString baseAppPath = getAppPath();
QString projectsPath = baseAppPath + "projects.xml";
QFile file(projectsPath);
if(!file.exists())
file.setFileName("projects.xml");
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::critical(mParent, mAppName,
"Unable to find projects.xml file.",
QMessageBox::Ok,
QMessageBox::Ok);
return;
}
if (!doc.setContent(&file))
{
file.close();
QMessageBox::critical(mParent, mAppName,
"Unable to find projects.xml file.",
QMessageBox::Ok,
QMessageBox::Ok);
return;
}
file.close();
bool checkRemovals = false;
QMap<QString, bool> dirNameRemovalMap;
QMap<ProjectEntry*, bool> dirEntryRemovalMap;
if(mProjectDirNameList.size() > 0 || mProjectDirectoryList.size() > 0)
{
checkRemovals = true;
for(int i=0; i<mProjectDirNameList.size(); i++)
{
dirNameRemovalMap.insert(mProjectDirNameList.at(i), false);
}
QList<ProjectEntry*> entryList = mProjectDirectoryList.values();
for(int i=0; i<entryList.size(); i++)
{
dirEntryRemovalMap.insert(entryList.at(i), false);
}
}
// Process
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull())
{
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull() && e.tagName() == "entry")
{
// projectDirectory
if(e.hasAttribute("type") && e.attribute("type") == "projectDirectory" && e.hasAttribute("path"))
{
QString title = e.text();
if(mProjectDirNameList.count(title) == 0)
{
mProjectDirNameList.append(title);
emit projectCategoryAdded(title);
}
if(checkRemovals)
dirNameRemovalMap.insert(title, true);
// Read through the project directory and build the tree entries
QString projectEntryPath(e.attribute("path"));
QDir dir(projectEntryPath);
// Check if the path contains "templates" and ignore if it does. Older projects.xml files included
// the Templates directory as a project directory.
if(!projectEntryPath.contains("templates", Qt::CaseInsensitive) && dir.exists())
{
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList projects = dir.entryInfoList();
for(int i=0; i<projects.size(); ++i)
{
QFileInfo projectInfo = projects.at(i);
// Find an executable within the directory
QDir projDir(projectInfo.filePath());
if(projDir.cd("game"))
{
// Add the template path
ProjectEntry *newEntry = new ProjectEntry();
newEntry->mPath = projDir.path();
newEntry->mName = projectInfo.fileName();
newEntry->mRootName = title;
newEntry->mRootPath = projectInfo.absoluteFilePath();
// Check for command line arguments
if(e.hasAttribute("args"))
{
newEntry->mArgs = e.attribute("args", "");
}
mProjectDirectoryList.insert(title, newEntry);
emit projectEntryAdded(newEntry);
if(checkRemovals)
dirEntryRemovalMap.insert(newEntry, true);
}
}
}
}
else if(e.hasAttribute("type") && e.attribute("type") == "templateDirectory" && e.hasAttribute("path"))
{
QString title = e.text();
QString templateEntryPath(e.attribute("path"));
QDir dir(templateEntryPath);
if(dir.exists())
{
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList templates = dir.entryInfoList();
for(int i=0; i<templates.size(); ++i)
{
QFileInfo templateInfo = templates.at(i);
// Find the game directory to tell us this is a valid template
QDir templateDir(templateInfo.filePath());
if(templateDir.cd("game"))
{
// Add the template path
TemplateEntry *newEntry = new TemplateEntry();
newEntry->mPath = templateDir.path();
newEntry->mName = templateInfo.fileName();
newEntry->mRootName = title;
newEntry->mRootPath = templateInfo.absoluteFilePath();
mTemplateDirectoryList.insert(title, newEntry);
newEntry->findPackages();
}
}
}
}
}
n = n.nextSibling();
}
// now lets check for removals and emit any removal signals, we see if there are
// any false entires still existing in our originally generated lists
if(checkRemovals)
{
QList<QString> nameList = dirNameRemovalMap.keys();
for(int i=0; i<nameList.size(); i++)
{
bool exists = dirNameRemovalMap.value(nameList.at(i));
if(!exists)
{
emit projectCategoryRemoved(nameList.at(i));
mProjectDirNameList.removeAt(i);
}
}
QList<ProjectEntry*> entryList = dirEntryRemovalMap.keys();
for(int i=0; i<entryList.size(); i++)
{
bool exists = dirEntryRemovalMap.value(entryList.at(i));
if(!exists)
{
ProjectEntry *entry = entryList.at(i);
emit projectEntryRemoved(entry);
mProjectDirectoryList.remove(entry->mRootName, entry);
delete entry;
}
}
}
}
void TemplateEntry::findPackages()
{
static const QString templateFileName = "template.xml";
static const QString packageDirName = "Packages";
// Load in the xml file, parse it, and build out the controls
QDomDocument doc("template");
QString basePath = mRootPath;
QString templatePath = basePath + QDir::separator() + templateFileName;
QFile file(templatePath);
if (!file.open(QIODevice::ReadOnly))
{
return;
}
if (!doc.setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull())
{
QDomElement e = n.toElement();
if(!e.isNull() && e.tagName() == "package")
{
if(e.hasAttribute("name") && e.hasAttribute("inclusion"))
{
QString name = e.attribute("name");
QString inclusion = e.attribute("inclusion");
QDir packageDir(packageDirName + QDir::separator() + name + QDir::separator() + "package");
QString path = packageDir.absolutePath();
if(inclusion == "required")
{
mRequiredPackages.append(path);
}
else if(inclusion == "recommended")
{
mRecommendedPackages.append(path);
}
}
}
n = n.nextSibling();
}
}
void ProjectList::appStandardData()
{
}
void ProjectList::appErrorData()
{
}
void ProjectList::appFinished()
{
emit maximizeApp();
}
void ProjectList::appStateChanged(QProcess::ProcessState newState)
{
if(newState == QProcess::NotRunning)
appFinished();
}
#ifdef Q_OS_WIN
BOOL CALLBACK ProjectList::EnumWindowsProc(HWND hwnd, LPARAM param)
{
DWORD id = GetWindowThreadProcessId(hwnd, NULL);
if (id == (DWORD)param)
{
// store the HWND
ProjectList::appWindow = hwnd;
return false;
}
return true;
}
#endif
void ProjectList::toggleEditor(int editorMode)
{
Q_UNUSED(editorMode)
#ifdef Q_OS_WIN
//PostMessage(ProjectList::appWindow, WM_TOGGLE_EDITOR, editorMode, 0);
#endif
}
ProjectEntry *ProjectList::getFirstProjectEntry()
{
ProjectEntry* entry = NULL;
QList<QString> keyList = mProjectDirectoryList.keys();
if(keyList.size() > 0)
{
if(mProjectDirectoryList.count(keyList.at(0)) > 0)
{
entry = mProjectDirectoryList.values(keyList.at(0)).at(0);
}
}
return entry;
}