Update wrt-installer_0.0.51
[platform/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 //TODO shouldn't XML file creation happen here? If no, this method is not needed
112 void Manifest::generate(DPL::String filename)
113 {
114     xmlTextWriterPtr writer;
115     int state;
116     writer = xmlNewTextWriterFilename(DPL::ToUTF8String(filename).c_str(), 0); //compression set to 0
117     if (writer == NULL)
118         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlNewTextWriterFilename failed");
119     state = xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
120     if (state < 0)
121     {
122         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartDocument failed");
123     }
124     this->serialize(writer);
125     state = xmlTextWriterEndDocument(writer);
126     if (state < 0)
127     {
128         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndDocument failed");
129     }
130     if (writer != NULL)
131     {
132         xmlFreeTextWriter(writer);
133         writer = NULL;
134     }
135 }
136
137 void Manifest::serialize(xmlTextWriterPtr writer)
138 {
139     startElement(writer, "manifest");
140     {
141         writeAttribute(writer, "xmlns", "http://tizen.org/ns/packages");
142         writeAttribute(writer, "package", this->package);
143         writeAttribute(writer, "type", this->type);
144         writeAttribute(writer, "version", this->version);
145         if (!this->installLocation.IsNull())
146             writeAttribute(writer, "install-location", (*this->installLocation),
147                     (*this->installLocation).empty());
148
149         FOREACH(l, this->label)
150         {
151             writeElementWithOneAttribute(writer, "label", l->getString(),
152                     "xml:lang", l->getLang(), l->hasLang());
153         }
154         FOREACH(i, this->icon)
155         {
156             writeElementWithOneAttribute(writer, "icon", i->getString(),
157                     "xml:lang", i->getLang(), i->hasLang());
158         }
159         FOREACH(a, this->author)
160         {
161             a->serialize(writer);
162         }
163         FOREACH(d, this->description)
164         {
165             writeElementWithOneAttribute(writer, "description", d->getString(),
166                     "xml:lang", d->getLang(), d->hasLang());
167         }
168         //FOREACH(c, this->compatibility) { c->serialize(writer); }
169         //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
170         FOREACH(s, this->serviceApplication) { s->serialize(writer); }
171         FOREACH(u, this->uiApplication) { u->serialize(writer); }
172         FOREACH(i, this->imeApplication) { i->serialize(writer); }
173         //FOREACH(f, this->font) { f->serialize(writer); }
174         //FOREACH(l, this->livebox) { l->serialize(writer); }
175     }
176     endElement(writer);
177 }
178
179 void Author::serialize(xmlTextWriterPtr writer)
180 {
181     startElement(writer, "author");
182     writeAttribute(writer, "email", this->email, !this->email.empty());
183     writeAttribute(writer, "href", this->href, !this->href.empty());
184     writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
185     writeText(writer, body);
186     endElement(writer);
187 }
188
189 void ServiceApplication::serialize(xmlTextWriterPtr writer)
190 {
191     startElement(writer, "service-application");
192     writeAttribute(writer, "appid", this->appid);
193     writeAttribute(writer, "auto-restart", (!this->autoRestart.IsNull() &&
194             (*this->autoRestart)) ? "true" : "false");
195     writeAttribute(writer, "exec", this->exec);
196     writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
197             (*this->onBoot)) ? "true" : "false");
198     writeAttribute(writer, "type", this->type);
199     FOREACH(l, this->label)
200     {
201         writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
202                 l->getLang(), l->hasLang());
203     }
204     FOREACH(i, this->icon)
205     {
206         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
207                 i->getLang(), i->hasLang());
208     }
209     FOREACH(a, this->applicationService)
210     {
211         a->serialize(writer);
212     }
213     endElement(writer);
214 }
215
216 void UiApplication::serialize(xmlTextWriterPtr writer)
217 {
218     startElement(writer, "ui-application");
219     writeAttribute(writer, "appid", this->appid);
220     writeAttribute(writer, "exec", this->exec);
221     if (!this->multiple.IsNull())
222         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
223     if (!this->nodisplay.IsNull())
224         writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
225     if (!this->taskmanage.IsNull())
226         writeAttribute(writer, "taskmanage", (*this->taskmanage) ? "true" : "false");
227     writeAttribute(writer, "type", this->type);
228     writeAttribute(writer, "extraid", this->extraid);
229     if (!this->categories.IsNull())
230         writeAttribute(writer, "categories", (*this->categories));
231     FOREACH(l, this->label)
232     {
233         writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
234                 l->getLang(), l->hasLang());
235     }
236     FOREACH(i, this->icon)
237     {
238         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
239                 i->getLang(), i->hasLang());
240     }
241     FOREACH(a, this->applicationService)
242     {
243         a->serialize(writer);
244     }
245     endElement(writer);
246 }
247
248 void ImeApplication::serialize(xmlTextWriterPtr writer)
249 {
250     startElement(writer, "ime-application");
251     writeAttribute(writer, "appid", this->appid);
252     writeAttribute(writer, "exec", this->exec);
253     if (!this->multiple.IsNull())
254         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
255     if (!this->nodisplay.IsNull())
256         writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
257     writeAttribute(writer, "type", this->type);
258     FOREACH(l, this->label)
259     {
260         writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
261                 l->getLang(), l->hasLang());
262     }
263     FOREACH(i, this->icon)
264     {
265         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
266                 i->getLang(), i->hasLang());
267     }
268     endElement(writer);
269 }
270
271 void ApplicationService::serialize(xmlTextWriterPtr writer)
272 {
273     startElement(writer, "application-service");
274     FOREACH(o, this->operation)
275     {
276         startElement(writer, "operation");
277         writeAttribute(writer, "name", *o);
278         endElement(writer);
279     }
280     FOREACH(u, this->uri)
281     {
282         startElement(writer, "uri");
283         writeAttribute(writer, "name", *u);
284         endElement(writer);
285     }
286     FOREACH(m, this->mime)
287     {
288         startElement(writer, "mime");
289         writeAttribute(writer, "name", *m);
290         endElement(writer);
291     }
292     endElement(writer);
293 }
294
295 } //namespace Jobs
296 } //namespace WidgetInstall