summaryrefslogtreecommitdiff
path: root/src/projectserialiser.cc
blob: 707ff184e8373799f46805a0cabdbf6bf3f479d6 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            projectserialiser.cc
 *
 *  Sat May 12 10:11:22 CEST 2018
 *  Copyright 2018 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "projectserialiser.h"

#include <vector>
#include <iostream>

#include <QDomDocument>

/*
<?xml version="1.0" encoding="UTF-8"?>
<dgedit version="1.0">
	<project_name>MyProject</project_name>
	<raw_file_root>/some/path</raw_file_root>
</dgedit>
*/

class DomHelper
{
public:
	DomHelper(const QDomDocument& doc) : elem(doc.documentElement()) {}
	DomHelper(const QDomElement& elem) : elem(elem) {}

	DomHelper operator()(const QString& name)
	{
		auto node = elem.namedItem(name);
		if(!node.isElement())
		{
			std::cout << "No such child-element: '" << name.toStdString() << "'\n";
			QDomElement e;
			return DomHelper(e);
		}

		return DomHelper(node.toElement());
	}

	QString operator[](const QString& name)
	{
		auto attrs = elem.attributes();
		auto node = attrs.namedItem(name);
		if(!node.isAttr())
		{
			std::cout << "No such attribute: '" << name.toStdString() << "'\n";
			return "";
		}
		return node.toAttr().value();
	}

	QString text()
	{
		return elem.text();
	}

	// Get child nodes with tag_name or all child nodes if tag_name == "".
	std::vector<DomHelper> children(const QString& tag_name = "")
	{
		std::vector<DomHelper> children;
		auto child_nodes = elem.childNodes();
		for(int i = 0; i < child_nodes.count(); ++i)
		{
			auto node = child_nodes.at(i);
			if(!node.isElement())
			{
				continue;
			}

			auto elem = node.toElement();
			if(elem.tagName() == tag_name || tag_name == "")
			{
				children.emplace_back(DomHelper(elem));
			}
		}

		return children;
	}

private:
	QDomElement elem;
};

QString ProjectSerialiser::serialise(const Project& project)
{
	QDomDocument doc;
	auto header =
		doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
	doc.appendChild(header);

	auto dgedit = doc.createElement("dgedit");
	doc.appendChild(dgedit);

	dgedit.setAttribute("version", "1.0");

	auto project_name = doc.createElement("project_name");
	project_name.appendChild(doc.createTextNode(project.project_name));
	dgedit.appendChild(project_name);

	auto raw_file_root = doc.createElement("raw_file_root");
	raw_file_root.appendChild(doc.createTextNode(project.raw_file_root));
	dgedit.appendChild(raw_file_root);

	auto instruments = doc.createElement("instruments");
	dgedit.appendChild(instruments);

	for(const auto& i : project.instruments)
	{
		auto instrument = doc.createElement("instrument");
		instruments.appendChild(instrument);

		auto instrument_name = doc.createElement("instrument_name");
		instrument_name.appendChild(doc.createTextNode(i.instrument_name));
		instrument.appendChild(instrument_name);
	}

	return doc.toString();
}

bool ProjectSerialiser::deserialise(const QString& data, Project& project)
{
	QDomDocument doc;
	if(!doc.setContent(data))
	{
		std::cout << "Could not parse XML\n";
		return false;
	}

	project.reset();
	DomHelper dom(doc);

	QString version = dom["version"];
	if(version != "1.0")
	{
		std::cout << "Bad version: '" << version.toStdString() << "'\n";
		return false;
	}

	project.project_name = dom("project_name").text();
	project.raw_file_root = dom("raw_file_root").text();

	auto instruments = dom("instruments").children("instrument");
	for(auto& instrument : instruments)
	{
		project.instruments.emplace_back(Instrument(project, project.next_id));
		++project.next_id;
		auto& instr = project.instruments.back();
		instr.instrument_name = instrument("instrument_name").text();
	}

	return true;
}