|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "hswidgetcomponentparser.h" |
|
21 #include <QFile> |
|
22 |
|
23 const char WIDGETPROVIDER[] = "widgetprovider"; |
|
24 const char WIDGET[] = "widget"; |
|
25 const char WIDGETMANIFEST[] = "hswidgetmanifest"; |
|
26 |
|
27 const char URI[] = "uri"; |
|
28 const char TITLE[] = "title"; |
|
29 const char ICONURI[] = "iconuri"; |
|
30 const char ICON[] = "icon"; |
|
31 const char DESCRIPTION[] = "description"; |
|
32 const char HIDDEN[] = "hidden"; |
|
33 const char SERVICEXML[] = "servicexml"; |
|
34 const char VERSION[] = "version"; |
|
35 const char VERSION_1_0[] = "1.0"; |
|
36 |
|
37 /*! |
|
38 \class HsWidgetComponentParser |
|
39 \ingroup group_hswidgetuninstaller |
|
40 \brief Parses widget manifest xml file. |
|
41 */ |
|
42 |
|
43 |
|
44 /*! |
|
45 Constructor, \a aComponentManifest path to file \a parent. |
|
46 */ |
|
47 HsWidgetComponentParser::HsWidgetComponentParser(const QString &aComponentManifest, QObject *parent) |
|
48 : QObject(parent),mError(false) |
|
49 { |
|
50 parseFile(aComponentManifest); |
|
51 } |
|
52 |
|
53 /*! |
|
54 Destructor. |
|
55 */ |
|
56 HsWidgetComponentParser::~HsWidgetComponentParser() |
|
57 { |
|
58 |
|
59 } |
|
60 |
|
61 /*! |
|
62 Returns parsed widget component descriptor. |
|
63 */ |
|
64 HsWidgetComponentDescriptor HsWidgetComponentParser::widgetComponentDescriptor() |
|
65 { |
|
66 return mComponentDescriptor; |
|
67 } |
|
68 |
|
69 /*! |
|
70 Returns true if error occurred in parsing. |
|
71 */ |
|
72 bool HsWidgetComponentParser::error() |
|
73 { |
|
74 return mError; |
|
75 } |
|
76 |
|
77 /*! |
|
78 Parse widget component descriptor from \a aComponentManifest file. |
|
79 */ |
|
80 void HsWidgetComponentParser::parseFile(const QString &aComponentManifest) |
|
81 { |
|
82 QFile file(aComponentManifest); |
|
83 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
|
84 mError = true; |
|
85 return; |
|
86 } |
|
87 mParser.setDevice(&file); |
|
88 if (mParser.readNextStartElement()) { |
|
89 if (mParser.name() == WIDGETMANIFEST){ |
|
90 parseWidgetManifestTag(); |
|
91 } else if(mParser.name() == WIDGETPROVIDER){ |
|
92 parseWidgetProviderTag(); |
|
93 } else { |
|
94 mParser.raiseError(QObject::tr("Malformed manifest")); |
|
95 } |
|
96 } else { |
|
97 mParser.raiseError(QObject::tr("Malformed manifest")); |
|
98 } |
|
99 mError = mParser.error(); |
|
100 |
|
101 } |
|
102 |
|
103 /*! |
|
104 Parse manifest tag. |
|
105 */ |
|
106 void HsWidgetComponentParser::parseWidgetManifestTag() |
|
107 { |
|
108 Q_ASSERT(mParser.isStartElement() && mParser.name() == WIDGETMANIFEST); |
|
109 |
|
110 /* Detect version */ |
|
111 if (mParser.attributes().hasAttribute(VERSION)) { |
|
112 QStringRef version = mParser.attributes().value(VERSION); |
|
113 mComponentDescriptor.version = version.toString(); |
|
114 if (version == VERSION_1_0) { |
|
115 parseVersion1_0(); |
|
116 } else { |
|
117 mParser.raiseError(QObject::tr("Unsupported version")); |
|
118 } |
|
119 |
|
120 } else { |
|
121 // newest is default |
|
122 parseVersion1_0(); |
|
123 mComponentDescriptor.version = VERSION_1_0; |
|
124 } |
|
125 } |
|
126 |
|
127 /*! |
|
128 Parse provider tag. |
|
129 */ |
|
130 void HsWidgetComponentParser::parseWidgetProviderTag() |
|
131 { |
|
132 Q_ASSERT(mParser.isStartElement() && mParser.name() == WIDGETPROVIDER); |
|
133 if (!mParser.readNextStartElement() || mParser.name() != WIDGET) { |
|
134 mParser.raiseError(QObject::tr("Malformed manifest")); |
|
135 return; |
|
136 } |
|
137 |
|
138 // everything is in attributes |
|
139 QXmlStreamAttributes attrs = mParser.attributes(); |
|
140 foreach (QXmlStreamAttribute attr, attrs) { |
|
141 QStringRef name = attr.name(); |
|
142 if (name == URI) { |
|
143 mComponentDescriptor.uri = attr.value().toString(); |
|
144 } else if (name == ICONURI) { |
|
145 mComponentDescriptor.iconUri = attr.value().toString(); |
|
146 } else if (name == TITLE) { |
|
147 mComponentDescriptor.title = attr.value().toString(); |
|
148 } else if (name == DESCRIPTION) { |
|
149 mComponentDescriptor.description = attr.value().toString(); |
|
150 } else if (name == HIDDEN) { |
|
151 mComponentDescriptor.hidden = attr.value().toString(); |
|
152 } |
|
153 } |
|
154 |
|
155 } |
|
156 /*! |
|
157 Parse version 1.0. |
|
158 */ |
|
159 void HsWidgetComponentParser::parseVersion1_0() |
|
160 { |
|
161 Q_ASSERT(mParser.isStartElement() && mParser.name() == WIDGETMANIFEST); |
|
162 /* We'll parse the XML until we reach </hswidgetmanifest> tag, end of cml or have an error. */ |
|
163 while( !(mParser.tokenType() == QXmlStreamReader::EndElement && mParser.name() == WIDGETMANIFEST) && |
|
164 !mParser.hasError() && !mParser.atEnd()) { |
|
165 |
|
166 mParser.readNext(); |
|
167 if (mParser.tokenType() != QXmlStreamReader::StartElement) { |
|
168 continue; |
|
169 } |
|
170 /* Read next start element.*/ |
|
171 QStringRef tag = mParser.name(); |
|
172 if(tag == URI) { |
|
173 parseUri(); |
|
174 } else if (tag == ICON) { |
|
175 parseIcon(); |
|
176 } else if (tag == TITLE) { |
|
177 parseTitle(); |
|
178 } else if (tag == DESCRIPTION) { |
|
179 parseDescription(); |
|
180 } else if (tag == HIDDEN) { |
|
181 parseHidden(); |
|
182 } else if (tag == SERVICEXML) { |
|
183 parseServiceXml(); |
|
184 } |
|
185 } |
|
186 } |
|
187 |
|
188 |
|
189 /*! |
|
190 Parse uri. |
|
191 */ |
|
192 void HsWidgetComponentParser::parseUri() |
|
193 { |
|
194 mParser.readNext(); |
|
195 if(mParser.tokenType() != QXmlStreamReader::Characters) { |
|
196 return; |
|
197 } |
|
198 mComponentDescriptor.uri = mParser.text().toString(); |
|
199 } |
|
200 |
|
201 /*! |
|
202 Parse icon. |
|
203 */ |
|
204 void HsWidgetComponentParser::parseIcon() |
|
205 { |
|
206 mParser.readNext(); |
|
207 if(mParser.tokenType() != QXmlStreamReader::Characters) { |
|
208 return; |
|
209 } |
|
210 mComponentDescriptor.iconUri = mParser.text().toString(); |
|
211 } |
|
212 |
|
213 /*! |
|
214 Parse title. |
|
215 */ |
|
216 void HsWidgetComponentParser::parseTitle() |
|
217 { |
|
218 mParser.readNext(); |
|
219 if(mParser.tokenType() != QXmlStreamReader::Characters) { |
|
220 return; |
|
221 } |
|
222 mComponentDescriptor.title = mParser.text().toString(); |
|
223 } |
|
224 |
|
225 /*! |
|
226 Parse description. |
|
227 */ |
|
228 void HsWidgetComponentParser::parseDescription() |
|
229 { |
|
230 mParser.readNext(); |
|
231 if(mParser.tokenType() != QXmlStreamReader::Characters) { |
|
232 return; |
|
233 } |
|
234 mComponentDescriptor.description = mParser.text().toString(); |
|
235 } |
|
236 |
|
237 /*! |
|
238 Parse hidden. |
|
239 */ |
|
240 void HsWidgetComponentParser::parseHidden() |
|
241 { |
|
242 mParser.readNext(); |
|
243 if(mParser.tokenType() != QXmlStreamReader::Characters) { |
|
244 return; |
|
245 } |
|
246 mComponentDescriptor.hidden = mParser.text().toString(); |
|
247 } |
|
248 |
|
249 /*! |
|
250 Parse service xml. |
|
251 */ |
|
252 void HsWidgetComponentParser::parseServiceXml() |
|
253 { |
|
254 mParser.readNext(); |
|
255 if(mParser.tokenType() != QXmlStreamReader::Characters) { |
|
256 return; |
|
257 } |
|
258 mComponentDescriptor.serviceXml = mParser.text().toString(); |
|
259 } |
|
260 |