summaryrefslogtreecommitdiffstats
path: root/data/instancemodel.cpp
blob: 239be5c5032a8e000afb1efa3c9d19043662e231 (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Copyright 2013 MultiMC Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "instancemodel.h"

#include <QString>
#include <QDir>
#include <QDirIterator>
#include "stdinstance.h"

#include "../util/pathutils.h"

#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>


#define GROUP_FILE_FORMAT_VERSION 1

InstanceModel::InstanceModel( QObject* parent ) :
	QAbstractItemModel()
{
}

InstanceModel::~InstanceModel()
{
	saveGroupInfo();
	for(int i = 0; i < groups.size(); i++)
	{
		delete groups[i];
	}
}

void InstanceModel::addInstance( InstanceBase* inst, const QString& groupName )
{
	auto group = getGroupByName(groupName);
	group->addInstance(inst);
}

void InstanceGroup::addInstance ( InstanceBase* inst )
{
	instances.append(inst);
	inst->setGroup(this);
	// TODO: notify model.
}


void InstanceModel::initialLoad(QString dir)
{
	groupFile = dir + "/instgroups.json";
	implicitGroup = new InstanceGroup("Ungrouped", this);
	groups.append(implicitGroup);
	
	// temporary map from instance ID to group name
	QMap<QString, QString> groupMap;
	
		using namespace boost::property_tree;
	ptree pt;

	try
	{
		read_json(groupFile.toStdString(), pt);

		if (pt.get_optional<int>("formatVersion") != GROUP_FILE_FORMAT_VERSION)
		{
			// TODO: Discard old formats.
		}

		BOOST_FOREACH(const ptree::value_type& vp, pt.get_child("groups"))
		{
			ptree gPt = vp.second;
			QString groupName = QString::fromUtf8(vp.first.c_str());

			InstanceGroup *group = new InstanceGroup(groupName, this);
			groups.push_back(group);

			if (gPt.get_child_optional("hidden"))
				group->setHidden(gPt.get<bool>("hidden"));

			QVector<QString> groupInstances;
			BOOST_FOREACH(const ptree::value_type& v, gPt.get_child("instances"))
			{
				QString key = QString::fromUtf8(v.second.data().c_str());
				groupMap[key] = groupName;
			}
		}
	}
	catch (json_parser_error e)
	{
		qDebug("Failed to read group list. JSON parser error.");
//		wxLogError(_(), 
//			e.line(), wxStr(e.message()).c_str());
	}
	catch (ptree_error e)
	{
		qDebug("Failed to read group list. Unknown ptree error.");
	}
	
	qDebug("Loading instances");
	QDir instDir(dir);
	QDirIterator iter(instDir);
	
	while (iter.hasNext())
	{
		QString subDir = iter.next();
		if (QFileInfo(PathCombine(subDir, "instance.cfg")).exists())
		{
			// TODO Differentiate between different instance types.
			InstanceBase* inst = new StdInstance(subDir);
			QString instID = inst->getInstID();
			auto iter = groupMap.find(instID);
			if(iter != groupMap.end())
			{
				addInstance(inst,iter.value());
			}
			else
			{
				addInstance(inst);
			}
		}
	}
}

int InstanceModel::columnCount ( const QModelIndex& parent ) const
{
	// for now...
	return 1;
}

QVariant InstanceModel::data ( const QModelIndex& index, int role ) const
{
	if (!index.isValid())
		return QVariant();

	if (role != Qt::DisplayRole)
		return QVariant();

	InstanceModelItem *item = static_cast<InstanceModelItem*>(index.internalPointer());

	return item->data(index.column());
}

QModelIndex InstanceModel::index ( int row, int column, const QModelIndex& parent ) const
{
	if (!hasIndex(row, column, parent))
		return QModelIndex();
	
	InstanceModelItem *parentItem;

	if (!parent.isValid())
		parentItem = (InstanceModelItem *) this;
	else
		parentItem = static_cast<InstanceModelItem*>(parent.internalPointer());

	InstanceModelItem *childItem = parentItem->getChild(row);
	if (childItem)
		return createIndex(row, column, childItem);
	else
		return QModelIndex();
	
}

QModelIndex InstanceModel::parent ( const QModelIndex& index ) const
{
	if (!index.isValid())
		return QModelIndex();

	InstanceModelItem *childItem = static_cast<InstanceModelItem*>(index.internalPointer());
	InstanceModelItem *parentItem = childItem->getParent();

	if (parentItem == this)
		return QModelIndex();

	return createIndex(parentItem->getRow(), 0, parentItem);
}

int InstanceModel::rowCount ( const QModelIndex& parent ) const
{
	InstanceModelItem *parentItem;
	if (parent.column() > 0)
		return 0;

	if (!parent.isValid())
		parentItem = (InstanceModelItem*) this;
	else
		parentItem = static_cast<InstanceModelItem*>(parent.internalPointer());

	return parentItem->numChildren();
}

bool InstanceModel::saveGroupInfo() const
{
	/*
	using namespace boost::property_tree;
	ptree pt;

	pt.put<int>("formatVersion", GROUP_FILE_FORMAT_VERSION);

	try
	{
		typedef QMap<InstanceGroup *, QVector<InstanceBase*> > GroupListMap;

		GroupListMap groupLists;
		for (auto iter = instances.begin(); iter != instances.end(); iter++)
		{
			InstanceGroup *group = getInstanceGroup(*iter);
			
			if (group != nullptr)
				groupLists[group].push_back(*iter);
		}

		ptree groupsPtree;
		for (auto iter = groupLists.begin(); iter != groupLists.end(); iter++)
		{
			auto group = iter.key();
			auto & gList = iter.value();

			ptree groupTree;

			groupTree.put<bool>("hidden", group->isHidden());

			ptree instList;
			for (auto iter2 = gList.begin(); iter2 != gList.end(); iter2++)
			{
				std::string instID((*iter2)->getInstID().toUtf8());
				instList.push_back(std::make_pair("", ptree(instID)));
			}
			groupTree.put_child("instances", instList);

			groupsPtree.push_back(std::make_pair(std::string(group->getName().toUtf8()), groupTree));
		}
		pt.put_child("groups", groupsPtree);

		write_json(groupFile.toStdString(), pt);
	}
	catch (json_parser_error e)
	{
// 		wxLogError(_("Failed to read group list.\nJSON parser error at line %i: %s"), 
// 			e.line(), wxStr(e.message()).c_str());
		return false;
	}
	catch (ptree_error e)
	{
// 		wxLogError(_("Failed to save group list. Unknown ptree error."));
		return false;
	}

	return true;
	*/
	return false;
}

void InstanceModel::setInstanceGroup ( InstanceBase* inst, const QString& groupName )
{
	/*
	InstanceGroup *prevGroup = getInstanceGroup(inst);

	if (prevGroup != nullptr)
	{
		groupsMap.remove(inst);
	}

	if (!groupName.isEmpty())
	{
		InstanceGroup *newGroup = nullptr;

		for (auto iter = root->groups.begin(); iter != root->groups.end(); iter++)
		{
			if ((*iter)->getName() == groupName)
			{
				newGroup = *iter;
			}
		}

		if (newGroup == nullptr)
		{
			newGroup = new InstanceGroup(groupName, this);
			root->groups.push_back(newGroup);
		}

		groupsMap[inst] = newGroup;
	}

	// TODO: propagate change, reflect in model, etc.
	//InstanceGroupChanged(inst);
	*/
}

InstanceGroup* InstanceModel::getGroupByName ( const QString& name ) const
{
	for (auto iter = groups.begin(); iter != groups.end(); iter++)
	{
		if ((*iter)->getName() == name)
			return *iter;
	}
	return nullptr;
}
/*
void InstanceModel::setGroupFile ( QString filename )
{
	groupFile = filename;
}*/

int InstanceModel::numChildren() const
{
	return groups.count();
}

InstanceModelItem* InstanceModel::getChild ( int index ) const
{
	return groups[index];
}

QVariant InstanceModel::data ( int role ) const
{
	switch(role)
	{
		case Qt::DisplayRole:
			return "name";
	}
	return QVariant();
}


InstanceGroup::InstanceGroup(const QString& name, InstanceModel *parent)
{
	this->name = name;
	this->model = parent;
	this->hidden = false;
}

InstanceGroup::~InstanceGroup()
{
	for(int i = 0; i < instances.size(); i++)
	{
		delete instances[i];
	}
}


QString InstanceGroup::getName() const
{
	return name;
}

void InstanceGroup::setName(const QString& name)
{
	this->name = name;
	//TODO: propagate change
}

InstanceModelItem* InstanceGroup::getParent() const
{
	return model;
}

bool InstanceGroup::isHidden() const
{
	return hidden;
}

void InstanceGroup::setHidden(bool hidden)
{
	this->hidden = hidden;
	//TODO: propagate change
}

int InstanceGroup::getRow() const
{
	return model->getIndexOf( this);
}

InstanceModelItem* InstanceGroup::getChild ( int index ) const
{
	return instances[index];
}

int InstanceGroup::numChildren() const
{
	return instances.size();
}

QVariant InstanceGroup::data ( int role ) const
{
	switch(role)
	{
		case Qt::DisplayRole:
			return name;
		default:
			return QVariant();
	}
}