Update wrt-installer_0.0.54
[framework/web/wrt-installer.git] / src / jobs / widget_install / manifest.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    manifest.cpp
18  * @author  Mariusz Domanski (m.domanski@samsung.com)
19  */
20
21 #include "manifest.h"
22 #include "libxml_utils.h"
23 #include <widget_install/task_manifest_file.h>
24 #include <dpl/foreach.h>
25
26 namespace Jobs {
27 namespace WidgetInstall {
28
29 void writeElement(xmlTextWriterPtr writer, const char * name, DPL::String body)
30 {
31     int state = xmlTextWriterWriteElement(writer, BAD_CAST name,
32             BAD_CAST DPL::ToUTF8String(body).c_str());
33     if (state < 0)
34     {
35         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
36     }
37 }
38
39 void writeText(xmlTextWriterPtr writer, DPL::String text)
40 {
41     int state = xmlTextWriterWriteString(writer,
42             BAD_CAST DPL::ToUTF8String(text).c_str());
43     if (state < 0)
44     {
45         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteText failed");
46     }
47 }
48
49 void writeElement(xmlTextWriterPtr writer, const char * name, const char * body)
50 {
51     int state = xmlTextWriterWriteElement(writer, BAD_CAST name, BAD_CAST body);
52     if (state < 0)
53     {
54         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
55     }
56 }
57
58 void writeElementWithOneAttribute(xmlTextWriterPtr writer, const char * name,
59         DPL::String body, const char * nameAttr, DPL::String bodyAttr,
60         bool condition = true)
61 {
62     startElement(writer, name);
63     writeAttribute(writer, nameAttr, bodyAttr, condition);
64     writeText(writer, body);
65     endElement(writer);
66 }
67
68 void startElement(xmlTextWriterPtr writer, const char * name)
69 {
70     int state = xmlTextWriterStartElement(writer, BAD_CAST name);
71     if (state < 0)
72     {
73         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartElement failed");
74     }
75 }
76
77 void endElement(xmlTextWriterPtr writer)
78 {
79     int state = xmlTextWriterEndElement(writer);
80     if (state < 0)
81     {
82         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndElement failed");
83     }
84 }
85
86 void writeAttribute(xmlTextWriterPtr writer, const char * name, DPL::String body,
87         bool condition = true)
88 {
89     if (!condition)
90         return;
91     int state = xmlTextWriterWriteAttribute(writer, BAD_CAST name,
92             BAD_CAST DPL::ToUTF8String(body).c_str());
93     if (state < 0)
94     {
95         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteAttribute failed");
96     }
97 }
98
99 void writeAttribute(xmlTextWriterPtr writer, const char * name, const char * body,
100         bool condition = true)
101 {
102     if (!condition)
103         return;
104     int state = xmlTextWriterWriteAttribute(writer, BAD_CAST name, BAD_CAST body);
105     if (state < 0)
106     {
107         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteAttribute failed");
108     }
109 }
110
111 void Manifest::generate(DPL::String filename)
112 {
113     xmlTextWriterPtr writer;
114     int state;
115     writer = xmlNewTextWriterFilename(DPL::ToUTF8String(filename).c_str(), 0); //compression set to 0
116     if (writer == NULL)
117         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlNewTextWriterFilename failed");
118     state = xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
119     if (state < 0)
120     {
121         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartDocument failed");
122     }
123     this->serialize(writer);
124     state = xmlTextWriterEndDocument(writer);
125     if (state < 0)
126     {
127         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndDocument failed");
128     }
129     if (writer != NULL)
130     {
131         xmlFreeTextWriter(writer);
132         writer = NULL;
133     }
134 }
135
136 void Manifest::serialize(xmlTextWriterPtr writer)
137 {
138     startElement(writer, "manifest");
139     {
140         writeAttribute(writer, "xmlns", "http://tizen.org/ns/packages");
141         writeAttribute(writer, "package", this->package);
142         writeAttribute(writer, "type", this->type);
143         writeAttribute(writer, "version", this->version);
144         if (!this->installLocation.IsNull())
145             writeAttribute(writer, "install-location", (*this->installLocation),
146                     (*this->installLocation).empty());
147
148         FOREACH(l, this->label)
149         {
150             writeElementWithOneAttribute(writer, "label", l->getString(),
151                     "xml:lang", l->getLang(), l->hasLang());
152         }
153         FOREACH(i, this->icon)
154         {
155             writeElementWithOneAttribute(writer, "icon", i->getString(),
156                     "xml:lang", i->getLang(), i->hasLang());
157         }
158         FOREACH(a, this->author)
159         {
160             a->serialize(writer);
161         }
162         FOREACH(d, this->description)
163         {
164             writeElementWithOneAttribute(writer, "description", d->getString(),
165                     "xml:lang", d->getLang(), d->hasLang());
166         }
167         //FOREACH(c, this->compatibility) { c->serialize(writer); }
168         //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
169         FOREACH(s, this->serviceApplication) { s->serialize(writer); }
170         FOREACH(u, this->uiApplication) { u->serialize(writer); }
171         FOREACH(i, this->imeApplication) { i->serialize(writer); }
172         //FOREACH(f, this->font) { f->serialize(writer); }
173         //FOREACH(l, this->livebox) { l->serialize(writer); }
174     }
175     endElement(writer);
176 }
177
178 void Author::serialize(xmlTextWriterPtr writer)
179 {
180     startElement(writer, "author");
181     writeAttribute(writer, "email", this->email, !this->email.empty());
182     writeAttribute(writer, "href", this->href, !this->href.empty());
183     writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
184     writeText(writer, body);
185     endElement(writer);
186 }
187
188 void ServiceApplication::serialize(xmlTextWriterPtr writer)
189 {
190     startElement(writer, "service-application");
191     writeAttribute(writer, "appid", this->appid);
192     writeAttribute(writer, "auto-restart", (!this->autoRestart.IsNull() &&
193             (*this->autoRestart)) ? "true" : "false");
194     writeAttribute(writer, "exec", this->exec);
195     writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
196             (*this->onBoot)) ? "true" : "false");
197     writeAttribute(writer, "type", this->type);
198     FOREACH(l, this->label)
199     {
200         writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
201                 l->getLang(), l->hasLang());
202     }
203     FOREACH(i, this->icon)
204     {
205         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
206                 i->getLang(), i->hasLang());
207     }
208     FOREACH(a, this->applicationService)
209     {
210         a->serialize(writer);
211     }
212     endElement(writer);
213 }
214
215 void UiApplication::serialize(xmlTextWriterPtr writer)
216 {
217     startElement(writer, "ui-application");
218     writeAttribute(writer, "appid", this->appid);
219     writeAttribute(writer, "exec", this->exec);
220     if (!this->multiple.IsNull())
221         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
222     if (!this->nodisplay.IsNull())
223         writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
224     if (!this->taskmanage.IsNull())
225         writeAttribute(writer, "taskmanage", (*this->taskmanage) ? "true" : "false");
226     writeAttribute(writer, "type", this->type);
227     writeAttribute(writer, "extraid", this->extraid);
228     if (!this->categories.IsNull())
229         writeAttribute(writer, "categories", (*this->categories));
230     FOREACH(l, this->label)
231     {
232         writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
233                 l->getLang(), l->hasLang());
234     }
235     FOREACH(i, this->icon)
236     {
237         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
238                 i->getLang(), i->hasLang());
239     }
240     FOREACH(a, this->applicationService)
241     {
242         a->serialize(writer);
243     }
244     endElement(writer);
245 }
246
247 void ImeApplication::serialize(xmlTextWriterPtr writer)
248 {
249     startElement(writer, "ime-application");
250     writeAttribute(writer, "appid", this->appid);
251     writeAttribute(writer, "exec", this->exec);
252     if (!this->multiple.IsNull())
253         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
254     if (!this->nodisplay.IsNull())
255         writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
256     writeAttribute(writer, "type", this->type);
257     FOREACH(l, this->label)
258     {
259         writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
260                 l->getLang(), l->hasLang());
261     }
262     FOREACH(i, this->icon)
263     {
264         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
265                 i->getLang(), i->hasLang());
266     }
267     endElement(writer);
268 }
269
270 void ApplicationService::serialize(xmlTextWriterPtr writer)
271 {
272     startElement(writer, "application-service");
273     FOREACH(o, this->operation)
274     {
275         startElement(writer, "operation");
276         writeAttribute(writer, "name", *o);
277         endElement(writer);
278     }
279     FOREACH(u, this->uri)
280     {
281         startElement(writer, "uri");
282         writeAttribute(writer, "name", *u);
283         endElement(writer);
284     }
285     FOREACH(m, this->mime)
286     {
287         startElement(writer, "mime");
288         writeAttribute(writer, "name", *m);
289         endElement(writer);
290     }
291     endElement(writer);
292 }
293
294 } //namespace Jobs
295 } //namespace WidgetInstall