Changeset View
Changeset View
Standalone View
Standalone View
src/gui/kconfigloader.cpp
Show All 18 Lines | |||||
19 | 19 | | |||
20 | #include "kconfigloader.h" | 20 | #include "kconfigloader.h" | ||
21 | #include "kconfigloader_p.h" | 21 | #include "kconfigloader_p.h" | ||
22 | #include "kconfigloaderhandler_p.h" | 22 | #include "kconfigloaderhandler_p.h" | ||
23 | 23 | | |||
24 | #include <QColor> | 24 | #include <QColor> | ||
25 | #include <QFont> | 25 | #include <QFont> | ||
26 | #include <QHash> | 26 | #include <QHash> | ||
27 | #include <QXmlContentHandler> | | |||
28 | #include <QUrl> | 27 | #include <QUrl> | ||
29 | 28 | | |||
30 | #include <QDebug> | 29 | #include <QDebug> | ||
31 | 30 | | |||
32 | void ConfigLoaderPrivate::parse(KConfigLoader *loader, QIODevice *xml) | 31 | void ConfigLoaderPrivate::parse(KConfigLoader *loader, QIODevice *xml) | ||
33 | { | 32 | { | ||
34 | clearData(); | 33 | clearData(); | ||
35 | loader->clearItems(); | 34 | loader->clearItems(); | ||
Show All 16 Lines | 50 | { | |||
52 | if (!input->open(QIODevice::ReadOnly)) { | 51 | if (!input->open(QIODevice::ReadOnly)) { | ||
53 | qWarning() << "Impossible to open device"; | 52 | qWarning() << "Impossible to open device"; | ||
54 | return false; | 53 | return false; | ||
55 | } | 54 | } | ||
56 | QXmlStreamReader reader(input); | 55 | QXmlStreamReader reader(input); | ||
57 | 56 | | |||
58 | while (!reader.atEnd()) { | 57 | while (!reader.atEnd()) { | ||
59 | reader.readNext(); | 58 | reader.readNext(); | ||
60 | if (reader.hasError()) | 59 | if (reader.hasError()) { | ||
61 | return false; | 60 | return false; | ||
61 | } | ||||
62 | 62 | | |||
63 | switch (reader.tokenType()) { | 63 | switch (reader.tokenType()) { | ||
64 | case QXmlStreamReader::StartElement: | 64 | case QXmlStreamReader::StartElement: | ||
65 | if (!startElement(reader.namespaceUri(), reader.name(), | 65 | if (!startElement(reader.name(), reader.attributes())) { | ||
66 | reader.qualifiedName(), reader.attributes())) { | | |||
67 | return false; | 66 | return false; | ||
68 | } | 67 | } | ||
69 | break; | 68 | break; | ||
70 | case QXmlStreamReader::EndElement: | 69 | case QXmlStreamReader::EndElement: | ||
71 | if (!endElement(reader.namespaceUri(), reader.name(), | 70 | if (!endElement(reader.name())) { | ||
72 | reader.qualifiedName())) { | | |||
73 | return false; | 71 | return false; | ||
74 | } | 72 | } | ||
75 | break; | 73 | break; | ||
76 | case QXmlStreamReader::Characters: | 74 | case QXmlStreamReader::Characters: | ||
77 | if (!reader.isWhitespace() && !reader.text().trimmed().isEmpty()) { | 75 | if (!reader.isWhitespace() && !reader.text().trimmed().isEmpty()) { | ||
78 | if (!characters(reader.text())) | 76 | if (!characters(reader.text())) { | ||
79 | return false; | 77 | return false; | ||
80 | } | 78 | } | ||
79 | } | ||||
81 | break; | 80 | break; | ||
82 | default: | 81 | default: | ||
83 | break; | 82 | break; | ||
84 | } | 83 | } | ||
85 | } | 84 | } | ||
86 | 85 | | |||
87 | if (reader.isEndDocument()) | 86 | if (reader.isEndDocument()) { | ||
88 | return false; | 87 | return false; | ||
88 | } | ||||
89 | 89 | | |||
90 | return true; | 90 | return true; | ||
91 | } | 91 | } | ||
92 | 92 | | |||
93 | bool ConfigLoaderHandler::startElement(const QStringRef &namespaceURI, const QStringRef &localName, | 93 | bool ConfigLoaderHandler::startElement(const QStringRef &localName, const QXmlStreamAttributes &attrs) | ||
94 | const QStringRef &qName, const QXmlStreamAttributes &attrs) | | |||
95 | { | 94 | { | ||
96 | Q_UNUSED(namespaceURI) | | |||
97 | Q_UNUSED(qName) | | |||
98 | | ||||
99 | // qDebug() << "ConfigLoaderHandler::startElement(" << localName << qName; | 95 | // qDebug() << "ConfigLoaderHandler::startElement(" << localName << qName; | ||
100 | const int numAttrs = attrs.count(); | | |||
101 | const QString tag = localName.toString().toLower(); | 96 | const QString tag = localName.toString().toLower(); | ||
102 | if (tag == QLatin1String("group")) { | 97 | if (tag == QLatin1String("group")) { | ||
103 | QString group; | 98 | QString group; | ||
104 | for (int i = 0; i < numAttrs; ++i) { | 99 | for (const auto &attr : attrs) { | ||
105 | const QStringRef name = attrs.at(i).name(); | 100 | const QStringRef name = attr.name(); | ||
106 | if (name.compare(QLatin1String("name"), Qt::CaseInsensitive) == 0) { | 101 | if (name.compare(QLatin1String("name"), Qt::CaseInsensitive) == 0) { | ||
107 | //qDebug() << "set group to" << attrs.value(i); | 102 | //qDebug() << "set group to" << attrs.value(i); | ||
108 | group = attrs.at(i).value().toString(); | 103 | group = attr.value().toString(); | ||
109 | } | 104 | } | ||
110 | } | 105 | } | ||
111 | if (group.isEmpty()) { | 106 | if (group.isEmpty()) { | ||
112 | group = d->baseGroup; | 107 | group = d->baseGroup; | ||
113 | } else { | 108 | } else { | ||
114 | d->groups.append(group); | 109 | d->groups.append(group); | ||
115 | if (!d->baseGroup.isEmpty()) { | 110 | if (!d->baseGroup.isEmpty()) { | ||
116 | group = d->baseGroup + QLatin1Char('\x1d') + group; | 111 | group = d->baseGroup + QLatin1Char('\x1d') + group; | ||
117 | } | 112 | } | ||
118 | } | 113 | } | ||
119 | 114 | | |||
120 | if (m_config) { | 115 | if (m_config) { | ||
121 | m_config->setCurrentGroup(group); | 116 | m_config->setCurrentGroup(group); | ||
122 | } | 117 | } | ||
123 | } else if (tag == QLatin1String("entry")) { | 118 | } else if (tag == QLatin1String("entry")) { | ||
124 | for (int i = 0; i < numAttrs; ++i) { | 119 | for (const auto &attr : attrs) { | ||
125 | const QStringRef name = attrs.at(i).name(); | 120 | const QStringRef name = attr.name(); | ||
126 | if (name.compare(QLatin1String("name"), Qt::CaseInsensitive) == 0) { | 121 | if (name.compare(QLatin1String("name"), Qt::CaseInsensitive) == 0) { | ||
127 | m_name = attrs.at(i).value().trimmed().toString(); | 122 | m_name = attr.value().trimmed().toString(); | ||
128 | } else if (name.compare(QLatin1String("type"), Qt::CaseInsensitive) == 0) { | 123 | } else if (name.compare(QLatin1String("type"), Qt::CaseInsensitive) == 0) { | ||
129 | m_type = attrs.at(i).value().toString().toLower(); | 124 | m_type = attr.value().toString().toLower(); | ||
130 | } else if (name.compare(QLatin1String("key"), Qt::CaseInsensitive) == 0) { | 125 | } else if (name.compare(QLatin1String("key"), Qt::CaseInsensitive) == 0) { | ||
131 | m_key = attrs.at(i).value().trimmed().toString(); | 126 | m_key = attr.value().trimmed().toString(); | ||
132 | } | 127 | } | ||
133 | } | 128 | } | ||
134 | } else if (tag == QLatin1String("choice")) { | 129 | } else if (tag == QLatin1String("choice")) { | ||
135 | m_choice.name.clear(); | 130 | m_choice.name.clear(); | ||
136 | m_choice.label.clear(); | 131 | m_choice.label.clear(); | ||
137 | m_choice.whatsThis.clear(); | 132 | m_choice.whatsThis.clear(); | ||
138 | for (int i = 0; i < numAttrs; ++i) { | 133 | for (const auto &attr : attrs) { | ||
139 | const QStringRef name = attrs.at(i).name(); | 134 | const QStringRef name = attr.name(); | ||
140 | if (name.compare(QLatin1String("name"), Qt::CaseInsensitive) == 0) { | 135 | if (name.compare(QLatin1String("name"), Qt::CaseInsensitive) == 0) { | ||
141 | m_choice.name = attrs.at(i).value().toString(); | 136 | m_choice.name = attr.value().toString(); | ||
142 | } | 137 | } | ||
143 | } | 138 | } | ||
144 | m_inChoice = true; | 139 | m_inChoice = true; | ||
145 | } | 140 | } | ||
146 | 141 | | |||
147 | return true; | 142 | return true; | ||
148 | } | 143 | } | ||
149 | 144 | | |||
150 | bool ConfigLoaderHandler::characters(const QStringRef &ch) | 145 | bool ConfigLoaderHandler::characters(const QStringRef &ch) | ||
151 | { | 146 | { | ||
152 | m_cdata.append(ch.toString()); | 147 | m_cdata.append(ch.toString()); | ||
153 | return true; | 148 | return true; | ||
154 | } | 149 | } | ||
155 | 150 | | |||
156 | QString ConfigLoaderHandler::name() const | 151 | bool ConfigLoaderHandler::endElement(const QStringRef &localName) | ||
157 | { | | |||
158 | return m_name; | | |||
159 | } | | |||
160 | | ||||
161 | void ConfigLoaderHandler::setName(const QString &name) | | |||
162 | { | | |||
163 | m_name = name; | | |||
164 | } | | |||
165 | | ||||
166 | QString ConfigLoaderHandler::key() const | | |||
167 | { | 152 | { | ||
168 | return m_key; | | |||
169 | } | | |||
170 | | ||||
171 | void ConfigLoaderHandler::setKey(const QString &key) | | |||
172 | { | | |||
173 | m_key = key; | | |||
174 | } | | |||
175 | | ||||
176 | QString ConfigLoaderHandler::type() const | | |||
177 | { | | |||
178 | return m_type; | | |||
179 | } | | |||
180 | | ||||
181 | QString ConfigLoaderHandler::defaultValue() const | | |||
182 | { | | |||
183 | return m_default; | | |||
184 | } | | |||
185 | | ||||
186 | bool ConfigLoaderHandler::endElement(const QStringRef &namespaceURI, | | |||
187 | const QStringRef &localName, const QStringRef &qName) | | |||
188 | { | | |||
189 | Q_UNUSED(namespaceURI) | | |||
190 | Q_UNUSED(qName) | | |||
191 | | ||||
192 | // qDebug() << "ConfigLoaderHandler::endElement(" << localName << qName; | 153 | // qDebug() << "ConfigLoaderHandler::endElement(" << localName << qName; | ||
193 | const QStringRef tag = localName; | 154 | const QStringRef tag = localName; | ||
194 | if (tag.compare(QLatin1String("entry"), Qt::CaseInsensitive) == 0) { | 155 | if (tag.compare(QLatin1String("entry"), Qt::CaseInsensitive) == 0) { | ||
195 | addItem(); | 156 | addItem(); | ||
196 | resetState(); | 157 | resetState(); | ||
197 | } else if (tag.compare(QLatin1String("label"), Qt::CaseInsensitive) == 0) { | 158 | } else if (tag.compare(QLatin1String("label"), Qt::CaseInsensitive) == 0) { | ||
198 | if (m_inChoice) { | 159 | if (m_inChoice) { | ||
199 | m_choice.label = m_cdata.trimmed(); | 160 | m_choice.label = m_cdata.trimmed(); | ||
▲ Show 20 Lines • Show All 279 Lines • Show Last 20 Lines |