Support mouse-event of <tizen:box-content> and type of <tizen:livebox>
[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 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 = xmlTextWriterSetIndent (writer, 1);
119     if (state < 0)
120     {
121         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterSetIndent failed");
122     }
123
124     state = xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
125     if (state < 0)
126     {
127         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartDocument failed");
128     }
129     this->serialize(writer);
130     state = xmlTextWriterEndDocument(writer);
131     if (state < 0)
132     {
133         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndDocument failed");
134     }
135     if (writer != NULL)
136     {
137         xmlFreeTextWriter(writer);
138         writer = NULL;
139     }
140 }
141
142 void Manifest::serialize(xmlTextWriterPtr writer)
143 {
144     startElement(writer, "manifest");
145     {
146         writeAttribute(writer, "xmlns", "http://tizen.org/ns/packages");
147         writeAttribute(writer, "package", this->package);
148         writeAttribute(writer, "type", this->type);
149         writeAttribute(writer, "version", this->version);
150         if (!this->installLocation.IsNull())
151             writeAttribute(writer, "install-location", (*this->installLocation),
152                     (*this->installLocation).empty());
153
154         FOREACH(l, this->label)
155         {
156             writeElementWithOneAttribute(writer, "label", l->getString(),
157                     "xml:lang", l->getLang(), l->hasLang());
158         }
159         FOREACH(i, this->icon)
160         {
161             writeElementWithOneAttribute(writer, "icon", i->getString(),
162                     "xml:lang", i->getLang(), i->hasLang());
163         }
164         FOREACH(a, this->author)
165         {
166             a->serialize(writer);
167         }
168         FOREACH(d, this->description)
169         {
170             writeElementWithOneAttribute(writer, "description", d->getString(),
171                     "xml:lang", d->getLang(), d->hasLang());
172         }
173         //FOREACH(c, this->compatibility) { c->serialize(writer); }
174         //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
175         FOREACH(s, this->serviceApplication) { s->serialize(writer); }
176         FOREACH(u, this->uiApplication) { u->serialize(writer); }
177         FOREACH(i, this->imeApplication) { i->serialize(writer); }
178         //FOREACH(f, this->font) { f->serialize(writer); }
179         FOREACH(l, this->livebox) { l->serialize(writer); }
180     }
181     endElement(writer);
182 }
183
184 void Author::serialize(xmlTextWriterPtr writer)
185 {
186     startElement(writer, "author");
187     writeAttribute(writer, "email", this->email, !this->email.empty());
188     writeAttribute(writer, "href", this->href, !this->href.empty());
189     writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
190     writeText(writer, body);
191     endElement(writer);
192 }
193
194 void ServiceApplication::serialize(xmlTextWriterPtr writer)
195 {
196     startElement(writer, "service-application");
197     writeAttribute(writer, "appid", this->appid);
198     writeAttribute(writer, "auto-restart", (!this->autoRestart.IsNull() &&
199             (*this->autoRestart)) ? "true" : "false");
200     writeAttribute(writer, "exec", this->exec);
201     writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
202             (*this->onBoot)) ? "true" : "false");
203     writeAttribute(writer, "type", this->type);
204     FOREACH(l, this->label)
205     {
206         writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
207                 l->getLang(), l->hasLang());
208     }
209     FOREACH(i, this->icon)
210     {
211         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
212                 i->getLang(), i->hasLang());
213     }
214     FOREACH(a, this->appControl)
215     {
216         a->serialize(writer);
217     }
218     endElement(writer);
219 }
220
221 void UiApplication::serialize(xmlTextWriterPtr writer)
222 {
223     startElement(writer, "ui-application");
224     writeAttribute(writer, "appid", this->appid);
225     writeAttribute(writer, "exec", this->exec);
226     if (!this->multiple.IsNull())
227         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
228     if (!this->nodisplay.IsNull())
229         writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
230     if (!this->taskmanage.IsNull())
231         writeAttribute(writer, "taskmanage", (*this->taskmanage) ? "true" : "false");
232     writeAttribute(writer, "type", this->type);
233     writeAttribute(writer, "extraid", this->extraid);
234     if (!this->categories.IsNull())
235         writeAttribute(writer, "categories", (*this->categories));
236     FOREACH(l, this->label)
237     {
238         writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
239                 l->getLang(), l->hasLang());
240     }
241     FOREACH(i, this->icon)
242     {
243         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
244                 i->getLang(), i->hasLang());
245     }
246     FOREACH(a, this->appControl)
247     {
248         a->serialize(writer);
249     }
250     FOREACH(c, this->appCategory)
251     {
252         startElement(writer, "category");
253         writeAttribute(writer, "name", *c);
254         endElement(writer);
255     }
256     endElement(writer);
257 }
258
259 void ImeApplication::serialize(xmlTextWriterPtr writer)
260 {
261     startElement(writer, "ime-application");
262     writeAttribute(writer, "appid", this->appid);
263     writeAttribute(writer, "exec", this->exec);
264     if (!this->multiple.IsNull())
265         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
266     if (!this->nodisplay.IsNull())
267         writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
268     writeAttribute(writer, "type", this->type);
269     FOREACH(l, this->label)
270     {
271         writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
272                 l->getLang(), l->hasLang());
273     }
274     FOREACH(i, this->icon)
275     {
276         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
277                 i->getLang(), i->hasLang());
278     }
279     endElement(writer);
280 }
281
282 void AppControl::serialize(xmlTextWriterPtr writer)
283 {
284     startElement(writer, "app-control");
285     FOREACH(o, this->operation)
286     {
287         startElement(writer, "operation");
288         writeAttribute(writer, "name", *o);
289         endElement(writer);
290     }
291     FOREACH(u, this->uri)
292     {
293         startElement(writer, "uri");
294         writeAttribute(writer, "name", *u);
295         endElement(writer);
296     }
297     FOREACH(m, this->mime)
298     {
299         startElement(writer, "mime");
300         writeAttribute(writer, "name", *m);
301         endElement(writer);
302     }
303     endElement(writer);
304 }
305
306 void LiveBox::serialize(xmlTextWriterPtr writer)
307 {
308     startElement(writer, "livebox");
309     if(!this->liveboxId.empty()) {
310         writeAttribute(writer, "appid", this->liveboxId);
311     }
312
313     if(!this->primary.empty()) {
314         writeAttribute(writer, "primary", this->primary);
315     }
316
317     if(!this->updatePeriod.empty()) {
318         writeAttribute(writer, "period", this->updatePeriod);
319     }
320
321     writeAttribute(writer, "abi", "html");
322     writeAttribute(writer, "network", "true");
323     writeAttribute(writer, "nodisplay", "false");
324
325     if(!this->label.empty()) {
326         startElement(writer, "label");
327         writeText(writer, this->label);
328         endElement(writer);
329     }
330
331     if(!this->icon.empty()) {
332         startElement(writer, "icon");
333         writeText(writer, this->icon);
334         endElement(writer);
335     }
336
337     if(!this->autoLaunch.empty()) {
338         startElement(writer, "launch");
339         writeText(writer, this->autoLaunch);
340         endElement(writer);
341     }
342
343     if(!this->box.boxSrc.empty() &&
344        !this->box.boxMouseEvent.empty() &&
345        !this->box.boxSize.empty())
346     {
347         startElement(writer, "box");
348         writeAttribute(writer, "type", "buffer");
349         writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
350
351         FOREACH(m, this->box.boxSize)
352         {
353             std::pair<DPL::String, DPL::String> boxSize = *m;
354             startElement(writer, "size");
355             if(!boxSize.second.empty())
356                 writeAttribute(writer, "preview", boxSize.second);
357             writeText(writer, boxSize.first);
358             endElement(writer);
359         }
360
361         startElement(writer, "script");
362         writeAttribute(writer, "src", this->box.boxSrc);
363         endElement(writer);
364
365         endElement(writer);
366
367         if(!this->box.pdSrc.empty() &&
368            !this->box.pdWidth.empty() &&
369            !this->box.pdHeight.empty())
370         {
371             startElement(writer, "pd");
372             writeAttribute(writer, "type", "buffer");
373
374             startElement(writer, "size");
375              DPL::String pdSize = this->box.pdWidth+DPL::String(L"x")+this->box.pdHeight;
376             writeText(writer, pdSize);
377             endElement(writer);
378
379             startElement(writer, "script");
380             writeAttribute(writer, "src", this->box.pdSrc);
381             endElement(writer);
382
383             endElement(writer);
384         }
385     }
386
387     endElement(writer);
388 }
389
390 } //namespace Jobs
391 } //namespace WidgetInstall