OSDN Git Service

66f75752e65467770883a3507141061d07507e7a
[fontmanager/fontmanager.git] / fontsconfelement.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Takumi Asaki
4 ** All rights reserved.
5 ** Contact: Takumi Asaki (takumi.asaki@gmail.com)
6 **
7 ** This file is part of the fontmanager application.
8 **
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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
23 **     permission.
24 **
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."
36 **
37 ****************************************************************************/
38
39 #include "fontsconfelement.h"
40
41 #include "fontconfigdefs.h"
42
43 #include <QXmlStreamReader>
44
45 FontsConfElement::FontsConfElement(const QString &type, QObject *parent) :
46     QObject(parent), mType(type)
47 {
48 }
49
50 QString FontsConfElement::type() const
51 {
52     return mType;
53 }
54
55 void FontsConfElement::clear()
56 {
57     mAttributes.clear();
58     mChildElements.clear();
59 }
60
61 bool FontsConfElement::parse(QXmlStreamReader &xml)
62 {
63     if (xml.atEnd() || xml.error() != QXmlStreamReader::NoError)
64         return false;
65
66     bool check = true;
67     if (!xml.isStartElement()) {
68         check = xml.readNextStartElement();
69         if (!check)
70             return false;
71     }
72
73     mAttributes = xml.attributes();
74
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);
82             if (!check)
83                 return false;
84             addChildElement(elem);
85         } else if (t == QXmlStreamReader::EndElement)
86             return true;
87     }
88
89     return false;
90 }
91
92 void FontsConfElement::save(QXmlStreamWriter &xml)
93 {
94     if (mChildElements.count() == 0) {
95         if (!mText.isEmpty())
96             xml.writeTextElement(mType, mText);
97         else
98             xml.writeEmptyElement(mType);
99         xml.writeAttributes(mAttributes);
100     } else {
101         xml.writeStartElement(mType);
102         xml.writeAttributes(mAttributes);
103         if (!mText.isEmpty())
104             xml.writeCharacters(mText);
105         foreach (FontsConfElementPointer elem, mChildElements) {
106             elem->save(xml);
107         }
108         xml.writeEndElement();
109     }
110 }
111
112 QXmlStreamAttributes FontsConfElement::attributes() const
113 {
114     return mAttributes;
115 }
116
117 bool FontsConfElement::hasAttribute(const QString &key) const
118 {
119     return mAttributes.hasAttribute(key);
120 }
121
122 QString FontsConfElement::value(const QString &key) const
123 {
124     return mAttributes.value(key).toString();
125 }
126
127 void FontsConfElement::setAttribute(const QString &key, const QString &val)
128 {
129     mAttributes.append(key, val);
130 }
131
132 int FontsConfElement::count() const
133 {
134     return mChildElements.count();
135 }
136
137 FontsConfElementPointer FontsConfElement::childElementAt(int index) const
138 {
139     return mChildElements.at(index);
140 }
141
142 FontsConfElementList FontsConfElement::findChildrenElements(const QString &type) const
143 {
144     FontsConfElementList list;
145     foreach (FontsConfElementPointer elem, mChildElements) {
146         if (elem->type() == type)
147             list << elem;
148     }
149     return list;
150 }
151
152 int FontsConfElement::indexOf(const QString &type, int from) const
153 {
154     if (from < 0 || from >= mChildElements.count())
155         return -1;
156
157     int index = from;
158     while (index < mChildElements.count()) {
159         if (mChildElements.at(index)->type() == type)
160             return index;
161         index++;
162     }
163     return -1;
164 }
165
166 FontsConfElementPointer FontsConfElement::childElementOf(const QString &type, int from) const
167 {
168     int index = indexOf(type, from);
169     if (index < 0)
170         return FontsConfElementPointer();
171     return childElementAt(index);
172 }
173
174 void FontsConfElement::addChildElement(FontsConfElementPointer elem)
175 {
176     if (!elem || mChildElements.contains(elem))
177         return;
178     elem->setParent(this);
179     mChildElements.append(elem);
180 }
181
182 void FontsConfElement::insertChildElement(FontsConfElementPointer elem, int index)
183 {
184     if (!elem || mChildElements.contains(elem))
185         return;
186     elem->setParent(this);
187     mChildElements.insert(index, elem);
188 }
189
190 void FontsConfElement::removeAt(int index)
191 {
192     mChildElements.removeAt(index);
193 }
194
195 void FontsConfElement::removeOne(FontsConfElementPointer elem)
196 {
197     mChildElements.removeOne(elem);
198 }
199
200 QString FontsConfElement::text() const
201 {
202     return mText;
203 }
204
205 void FontsConfElement::setText(const QString &str)
206 {
207     mText = str;
208 }
209
210 QString FontsConfElement::readAttribute(QXmlStreamReader &xml, const QString &attr, const QString &defaultValue)
211 {
212     if (xml.attributes().hasAttribute(attr))
213         return xml.attributes().value(attr).toString();
214     return defaultValue;
215 }
216
217 QVariant FontsConfElement::readValue(QXmlStreamReader &xml)
218 {
219
220     bool check = false;
221     if (xml.isStartElement())
222         check = true;
223     else
224         check = xml.readNextStartElement();
225     if (!check)
226         return QVariant();
227     QString elemName(xml.name().toString());
228     if (elemName != BOOL_DEF || elemName != STRING_DEF || elemName != INT_DEF || elemName != DOUBLE_DEF) {
229         return QVariant();
230     }
231
232     QVariant value;
233     QXmlStreamReader::TokenType t = xml.tokenType();
234     while (t != QXmlStreamReader::Characters || t != QXmlStreamReader::EndElement)
235         t = xml.readNext();
236     if (t != QXmlStreamReader::Characters)
237         return value;
238     QString str = xml.text().toString();
239     if (elemName == BOOL_DEF) {
240         value = (str == TRUE_DEF);
241     } else if (elemName == STRING_DEF) {
242         value = str;
243     } else if (elemName == INT_DEF) {
244         value = str.toInt(&check);
245     } else if (elemName == DOUBLE_DEF) {
246         value = str.toDouble(&check);
247     }
248     if (!check)
249         return QVariant();
250     return value;
251 }