1 /****************************************************************************
3 ** Copyright (C) 2011 Takumi Asaki
4 ** All rights reserved.
5 ** Contact: Takumi Asaki (takumi.asaki@gmail.com)
7 ** This file is part of the fontmanager application.
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ****************************************************************************/
39 #include "fontsconfelement.h"
41 #include "fontconfigdefs.h"
43 #include <QXmlStreamReader>
45 FontsConfElement::FontsConfElement(const QString &type, QObject *parent) :
46 QObject(parent), mType(type)
50 QString FontsConfElement::type() const
55 void FontsConfElement::clear()
58 mChildElements.clear();
61 bool FontsConfElement::parse(QXmlStreamReader &xml)
63 if (xml.atEnd() || xml.error() != QXmlStreamReader::NoError)
67 if (!xml.isStartElement()) {
68 check = xml.readNextStartElement();
73 mAttributes = xml.attributes();
75 while (!xml.atEnd()) {
76 QXmlStreamReader::TokenType t = xml.readNext();
77 if (t == QXmlStreamReader::Characters) {
78 mText = xml.text().toString();
79 } else if (t == QXmlStreamReader::StartElement) {
80 FontsConfElementPointer elem(new FontsConfElement(xml.name().toString(), this));
81 check = elem->parse(xml);
84 addChildElement(elem);
85 } else if (t == QXmlStreamReader::EndElement)
92 void FontsConfElement::save(QXmlStreamWriter &xml)
94 if (mChildElements.count() == 0) {
96 xml.writeTextElement(mType, mText);
98 xml.writeEmptyElement(mType);
99 xml.writeAttributes(mAttributes);
101 xml.writeStartElement(mType);
102 xml.writeAttributes(mAttributes);
103 if (!mText.isEmpty())
104 xml.writeCharacters(mText);
105 foreach (FontsConfElementPointer elem, mChildElements) {
108 xml.writeEndElement();
112 QXmlStreamAttributes FontsConfElement::attributes() const
117 bool FontsConfElement::hasAttribute(const QString &key) const
119 return mAttributes.hasAttribute(key);
122 QString FontsConfElement::value(const QString &key) const
124 return mAttributes.value(key).toString();
127 void FontsConfElement::setAttribute(const QString &key, const QString &val)
129 mAttributes.append(key, val);
132 int FontsConfElement::count() const
134 return mChildElements.count();
137 FontsConfElementPointer FontsConfElement::childElementAt(int index) const
139 return mChildElements.at(index);
142 FontsConfElementList FontsConfElement::findChildrenElements(const QString &type) const
144 FontsConfElementList list;
145 foreach (FontsConfElementPointer elem, mChildElements) {
146 if (elem->type() == type)
152 int FontsConfElement::indexOf(const QString &type, int from) const
154 if (from < 0 || from >= mChildElements.count())
158 while (index < mChildElements.count()) {
159 if (mChildElements.at(index)->type() == type)
166 FontsConfElementPointer FontsConfElement::childElementOf(const QString &type, int from) const
168 int index = indexOf(type, from);
170 return FontsConfElementPointer();
171 return childElementAt(index);
174 void FontsConfElement::addChildElement(FontsConfElementPointer elem)
176 if (!elem || mChildElements.contains(elem))
178 elem->setParent(this);
179 mChildElements.append(elem);
182 void FontsConfElement::insertChildElement(FontsConfElementPointer elem, int index)
184 if (!elem || mChildElements.contains(elem))
186 elem->setParent(this);
187 mChildElements.insert(index, elem);
190 void FontsConfElement::removeAt(int index)
192 mChildElements.removeAt(index);
195 void FontsConfElement::removeOne(FontsConfElementPointer elem)
197 mChildElements.removeOne(elem);
200 QString FontsConfElement::text() const
205 void FontsConfElement::setText(const QString &str)
210 QString FontsConfElement::readAttribute(QXmlStreamReader &xml, const QString &attr, const QString &defaultValue)
212 if (xml.attributes().hasAttribute(attr))
213 return xml.attributes().value(attr).toString();
217 QVariant FontsConfElement::readValue(QXmlStreamReader &xml)
221 if (xml.isStartElement())
224 check = xml.readNextStartElement();
227 QString elemName(xml.name().toString());
228 if (elemName != BOOL_DEF || elemName != STRING_DEF || elemName != INT_DEF || elemName != DOUBLE_DEF) {
233 QXmlStreamReader::TokenType t = xml.tokenType();
234 while (t != QXmlStreamReader::Characters || t != QXmlStreamReader::EndElement)
236 if (t != QXmlStreamReader::Characters)
238 QString str = xml.text().toString();
239 if (elemName == BOOL_DEF) {
240 value = (str == TRUE_DEF);
241 } else if (elemName == STRING_DEF) {
243 } else if (elemName == INT_DEF) {
244 value = str.toInt(&check);
245 } else if (elemName == DOUBLE_DEF) {
246 value = str.toDouble(&check);