summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/macros/lib/xmlhandler.cpp
blob: b35759e1d07e4cfd8532038901920a9dc79f24d5 (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
/***************************************************************************
 * This file is part of the KDE project
 * copyright (C) 2005 by Sebastian Sauer (mail@dipe.org)
 * copyright (C) 2006 by Bernd Steindorff (bernd@itii.de)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program 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
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "xmlhandler.h"
#include "macro.h"
#include "macroitem.h"
#include "action.h"

#include <qdom.h>
#include <kdebug.h>

using namespace KoMacro;

namespace KoMacro {

	/**
	* @internal d-pointer class to be more flexible on future extension of the
	* functionality without to much risk to break the binary compatibility.
	*/
	class XMLHandler::Private
	{
		public:

			/**
			* The @a Macro instance this @a XMLHandler
			* manages.
			*/
			Macro* const macro;

			/**
			* Constructor.
			*
			* @param macro The @a Macro instance this
			* @a XMLHandler manages.
			*/
			Private(Macro* const macro)
				: macro(macro)
			{
			}
	};

}

XMLHandler::XMLHandler(Macro* const macro)
	: d( new Private(macro) )
{
}

XMLHandler::~XMLHandler()
{
	delete d;
}

bool XMLHandler::parseXML(const QDomElement& element)
{
	// Remove old items. We should clear first.
	d->macro->clearItems();

	// We expect a <macro> element. Do we really need to be such strict or
	// would it be more wise to trust the application in that case?
	if(element.tagName() != "macro") {
		kdDebug() << QString("XMLHandler::parseXML() Invalid tagname \"%1\"").arg(element.tagName()) << endl;
		return false;
	}
	
	// To be flexible with the xml-scheme, we need a version-number for xml.
	// If there is more than one version, parsing should update old macro-data, so that it
	// could write out in the newer version in toXML().
	if( element.attribute("xmlversion") != "1"){
		kdDebug() << QString("XMLHandler::parseXML() Invalid xml-version \"%1\"").arg(element.attribute("xmlversion")) << endl;
		return false;
	}

	// Do we need to load the macro's name?
	// d->macro->setName(element.attribute("name"));

	// Iterate through the child nodes the passed QDomElement has and
	// build the MacroItem elements.
	for(QDomNode itemnode = element.firstChild(); ! itemnode.isNull(); itemnode = itemnode.nextSibling()) {
		// The tagname should be "item"
		if(itemnode.nodeName() == "item") {
			// The node is an element.
			const QDomElement itemelem = itemnode.toElement();

			// Create a new MacroItem
			KSharedPtr<MacroItem> item = new MacroItem();

			// Add the new item to our Macro.
			d->macro->addItem( item );

			// Each MacroItem may point to an Action instance. We
			// try to determinate this action now and if it's defined
			// and available, we set it.
			KSharedPtr<Action> action = Manager::self()->action( itemelem.attribute("action") );
			if(action.data()) {
				item->setAction(action);
			}

			// Set the comment
			item->setComment( itemelem.attribute("comment") );

			// Iterate through the children this item has and try
			// to fill the list of variables our new MacroItem has.
			for(QDomNode childnode = itemnode.firstChild(); ! childnode.isNull(); childnode = childnode.nextSibling()) {
				// The tagname should be "variable"
				if(childnode.nodeName() == "variable") {
					// The node is an element.
					const QDomElement childelem = childnode.toElement();

 					// The name the variable has.
 					const QString name = childelem.attribute("name");
					// The value the variable has.
					const QString value = childelem.text();

					// Store the new variable in our macroitem.
					item->addVariable(name, value);
				}
			}
		}
	}

	// Job was done successfully.
	return true;
}

QDomElement XMLHandler::toXML()
{
	// The QDomDocument provides us the functionality to create new QDomElement instances.
	QDomDocument document;

	// Create the Macro-QDomElement. This element will be returned.
	QDomElement macroelem = document.createElement("macro");

	// Set the Macro-XML-Version, it should be the newest Version.
	macroelem.setAttribute("xmlversion","1");

	// Do we need to store the macro's name? Normaly the application
	// could/should take care of it cause we don't know how the app
	// may store the XML and cause we don't like to introduce
	// redundancy at this point.
	//macroelem.setAttribute("name",d->macro->name());

	// The list of MacroItem-children a Macro provides.
	QValueList<KSharedPtr<MacroItem > > items = d->macro->items();

	// Create an iterator...
	QValueList<KSharedPtr<MacroItem > >::ConstIterator it(items.constBegin()), end(items.constEnd());
	// ...and iterate over the list of children the Macro provides.
	for(;it != end; ++it) {
		// We are iterating over MacroItem instances.
		KSharedPtr<MacroItem> item = *it;

		// Flag to determinate if we really need to remember this item what
		// is only the case if comment or action is defined.
		bool append = false;

		// Each MacroItem will have an own node.
		QDomElement itemelem = document.createElement("item");

		// Each MacroItem could point to an Action provided by the Manager.
		const KSharedPtr<Action> action = item->action();
		if( action ) {
			append = true;

			// Remember the name of the action.
			itemelem.setAttribute("action", action->name());

			// Each MacroItem could have a list of variables. We
			// iterate through that list and build a element
			// for each single variable.
			QMap<QString, KSharedPtr<Variable > > varmap = item->variables();

			for(QMap<QString, KSharedPtr<Variable > >::ConstIterator vit = varmap.constBegin(); vit != varmap.constEnd(); ++vit) {
				const KSharedPtr<Variable> v = vit.data();
				if(! v.data()) {
					// skip if the variable is NULL.
					continue;
				}
				// Create an own element for the variable. The tagname will be
				// the name of the variable.
				QDomElement varelement = document.createElement("variable");

				// Remember the name the value has.
				varelement.setAttribute("name", vit.key()); 

				// Remember the value as textnode.
				varelement.appendChild(document.createTextNode(v->toString()));

				// Add the new variable-element to our MacroItem.
				itemelem.appendChild(varelement);
			}
		}

		// Each MacroItem could have an optional comment.
		const QString comment = item->comment();
		if(! comment.isEmpty()) {
			append = true;
			itemelem.setAttribute("comment", item->comment());
		}

		// Check if we really need to remember the item.
		if(append) {
			macroelem.appendChild(itemelem);
		}

	}

	// Job done. Return the macro's element.
	return macroelem;
}