Change tizen:metadata details
[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 void writeElement(xmlTextWriterPtr writer, const char * name, DPL::String body)
29 {
30     int state = xmlTextWriterWriteElement(writer, BAD_CAST name,
31                                           BAD_CAST DPL::ToUTF8String(
32                                               body).c_str());
33     if (state < 0) {
34         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
35     }
36 }
37
38 void writeText(xmlTextWriterPtr writer, DPL::String text)
39 {
40     int state = xmlTextWriterWriteString(writer,
41                                          BAD_CAST DPL::ToUTF8String(text).c_str());
42     if (state < 0) {
43         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteText failed");
44     }
45 }
46
47 void writeElement(xmlTextWriterPtr writer, const char * name, const char * body)
48 {
49     int state = xmlTextWriterWriteElement(writer, BAD_CAST name, BAD_CAST body);
50     if (state < 0) {
51         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
52     }
53 }
54
55 void writeElementWithOneAttribute(xmlTextWriterPtr writer,
56                                   const char * name,
57                                   DPL::String body,
58                                   const char * nameAttr,
59                                   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         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartElement failed");
73     }
74 }
75
76 void endElement(xmlTextWriterPtr writer)
77 {
78     int state = xmlTextWriterEndElement(writer);
79     if (state < 0) {
80         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndElement failed");
81     }
82 }
83
84 void writeAttribute(xmlTextWriterPtr writer,
85                     const char * name,
86                     DPL::String body,
87                     bool condition = true)
88 {
89     if (!condition) {
90         return;
91     }
92     int state = xmlTextWriterWriteAttribute(writer, BAD_CAST name,
93                                             BAD_CAST DPL::ToUTF8String(
94                                                 body).c_str());
95     if (state < 0) {
96         ThrowMsg(LibxmlUtils::Libxml2Error,
97                  "xmlTextWriterWriteAttribute failed");
98     }
99 }
100
101 void writeAttribute(xmlTextWriterPtr writer,
102                     const char * name,
103                     const char * body,
104                     bool condition = true)
105 {
106     if (!condition) {
107         return;
108     }
109     int state = xmlTextWriterWriteAttribute(writer,
110                                             BAD_CAST name,
111                                             BAD_CAST body);
112     if (state < 0) {
113         ThrowMsg(LibxmlUtils::Libxml2Error,
114                  "xmlTextWriterWriteAttribute failed");
115     }
116 }
117
118 void Manifest::generate(DPL::String filename)
119 {
120     xmlTextWriterPtr writer;
121     int state;
122
123     //compression set to 0
124     writer = xmlNewTextWriterFilename(DPL::ToUTF8String(filename).c_str(), 0);
125
126     if (writer == NULL) {
127         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlNewTextWriterFilename failed");
128     }
129     state = xmlTextWriterSetIndent(writer, 1);
130     if (state < 0) {
131         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterSetIndent failed");
132     }
133
134     state = xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
135     if (state < 0) {
136         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartDocument failed");
137     }
138     this->serialize(writer);
139     state = xmlTextWriterEndDocument(writer);
140     if (state < 0) {
141         ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndDocument failed");
142     }
143     if (writer != NULL) {
144         xmlFreeTextWriter(writer);
145         writer = NULL;
146     }
147 }
148
149 void Manifest::serialize(xmlTextWriterPtr writer)
150 {
151     startElement(writer, "manifest");
152     {
153         writeAttribute(writer, "xmlns", "http://tizen.org/ns/packages");
154         writeAttribute(writer, "package", this->package);
155         writeAttribute(writer, "type", this->type);
156         writeAttribute(writer, "version", this->version);
157         if (!this->installLocation.IsNull()) {
158             writeAttribute(writer, "install-location", (*this->installLocation),
159                            (*this->installLocation).empty());
160         }
161
162         FOREACH(l, this->label)
163         {
164             writeElementWithOneAttribute(writer, "label", l->getString(),
165                                          "xml:lang", l->getLang(), l->hasLang());
166         }
167         FOREACH(i, this->icon)
168         {
169             writeElementWithOneAttribute(writer, "icon", i->getString(),
170                                          "xml:lang", i->getLang(), i->hasLang());
171         }
172         FOREACH(a, this->author)
173         {
174             a->serialize(writer);
175         }
176         FOREACH(d, this->description)
177         {
178             writeElementWithOneAttribute(writer, "description", d->getString(),
179                                          "xml:lang", d->getLang(), d->hasLang());
180         }
181         //FOREACH(c, this->compatibility) { c->serialize(writer); }
182         //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
183         FOREACH(s, this->serviceApplication) {
184             s->serialize(writer);
185         }
186         FOREACH(u, this->uiApplication) {
187             u->serialize(writer);
188         }
189         FOREACH(i, this->imeApplication) {
190             i->serialize(writer);
191         }
192         //FOREACH(f, this->font) { f->serialize(writer); }
193         FOREACH(l, this->livebox) {
194             l->serialize(writer);
195         }
196         FOREACH(acc, this->account)
197         {
198             acc->serialize(writer);
199         }
200
201         if (!this->privileges.isEmpty()) {
202             this->privileges.serialize(writer);
203         }
204     }
205     endElement(writer);
206 }
207
208 void Author::serialize(xmlTextWriterPtr writer)
209 {
210     startElement(writer, "author");
211     writeAttribute(writer, "email", this->email, !this->email.empty());
212     writeAttribute(writer, "href", this->href, !this->href.empty());
213     writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
214     writeText(writer, body);
215     endElement(writer);
216 }
217
218 void ServiceApplication::serialize(xmlTextWriterPtr writer)
219 {
220     startElement(writer, "service-application");
221     writeAttribute(writer, "appid", this->appid);
222     writeAttribute(writer, "auto-restart",
223                    (!this->autoRestart.IsNull() &&
224                     (*this->autoRestart)) ? "true" :
225                    "false");
226     writeAttribute(writer, "exec", this->exec);
227     writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
228                                        (*this->onBoot)) ? "true" : "false");
229     writeAttribute(writer, "type", this->type);
230     FOREACH(l, this->label)
231     {
232         writeElementWithOneAttribute(writer, "label",
233                                      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->appControl)
242     {
243         a->serialize(writer);
244     }
245     endElement(writer);
246 }
247
248 void UiApplication::serialize(xmlTextWriterPtr writer)
249 {
250     startElement(writer, "ui-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     }
256     if (!this->nodisplay.IsNull()) {
257         writeAttribute(writer,
258                        "nodisplay",
259                        (*this->nodisplay) ? "true" : "false");
260     }
261     if (!this->taskmanage.IsNull()) {
262         writeAttribute(writer,
263                        "taskmanage",
264                        (*this->taskmanage) ? "true" : "false");
265     }
266     writeAttribute(writer, "type", this->type);
267     writeAttribute(writer, "extraid", this->extraid);
268     if (!this->categories.IsNull()) {
269         writeAttribute(writer, "categories", (*this->categories));
270     }
271     FOREACH(l, this->label)
272     {
273         writeElementWithOneAttribute(writer, "label",
274                                      l->getString(), "xml:lang",
275                                      l->getLang(), l->hasLang());
276     }
277     FOREACH(i, this->icon)
278     {
279         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
280                                      i->getLang(), i->hasLang());
281     }
282     FOREACH(a, this->appControl)
283     {
284         a->serialize(writer);
285     }
286     FOREACH(c, this->appCategory)
287     {
288         startElement(writer, "category");
289         writeAttribute(writer, "name", *c);
290         endElement(writer);
291     }
292     FOREACH(m, this->metadata) {
293         m->serialize(writer);
294     }
295     endElement(writer);
296 }
297
298 void ImeApplication::serialize(xmlTextWriterPtr writer)
299 {
300     startElement(writer, "ime-application");
301     writeAttribute(writer, "appid", this->appid);
302     writeAttribute(writer, "exec", this->exec);
303     if (!this->multiple.IsNull()) {
304         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
305     }
306     if (!this->nodisplay.IsNull()) {
307         writeAttribute(writer,
308                        "nodisplay",
309                        (*this->nodisplay) ? "true" : "false");
310     }
311     writeAttribute(writer, "type", this->type);
312     FOREACH(l, this->label)
313     {
314         writeElementWithOneAttribute(writer, "label",
315                                      l->getString(), "xml:lang",
316                                      l->getLang(), l->hasLang());
317     }
318     FOREACH(i, this->icon)
319     {
320         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
321                                      i->getLang(), i->hasLang());
322     }
323     endElement(writer);
324 }
325
326 void AppControl::serialize(xmlTextWriterPtr writer)
327 {
328     startElement(writer, "app-control");
329     FOREACH(o, this->operation)
330     {
331         startElement(writer, "operation");
332         writeAttribute(writer, "name", *o);
333         endElement(writer);
334     }
335     FOREACH(u, this->uri)
336     {
337         startElement(writer, "uri");
338         writeAttribute(writer, "name", *u);
339         endElement(writer);
340     }
341     FOREACH(m, this->mime)
342     {
343         startElement(writer, "mime");
344         writeAttribute(writer, "name", *m);
345         endElement(writer);
346     }
347     endElement(writer);
348 }
349
350 void LiveBox::serialize(xmlTextWriterPtr writer)
351 {
352     startElement(writer, "livebox");
353     if (!this->liveboxId.empty()) {
354         writeAttribute(writer, "appid", this->liveboxId);
355     }
356
357     if (!this->primary.empty()) {
358         writeAttribute(writer, "primary", this->primary);
359     }
360
361     if (!this->updatePeriod.empty()) {
362         writeAttribute(writer, "period", this->updatePeriod);
363     }
364
365     writeAttribute(writer, "abi", "html");
366     writeAttribute(writer, "network", "true");
367     writeAttribute(writer, "nodisplay", "false");
368
369     if (!this->label.empty()) {
370         int defaultLabelChk = 0;
371         FOREACH(m, this->label)
372         {
373             std::pair<DPL::String, DPL::String> boxLabel = *m;
374             startElement(writer, "label");
375             if (!boxLabel.first.empty()) {
376                 writeAttribute(writer, "xml:lang", boxLabel.first);
377             } else {
378                 defaultLabelChk++;
379             }
380             writeText(writer, boxLabel.second);
381             endElement(writer);
382         }
383         if(!defaultLabelChk) {
384             startElement(writer, "label");
385             writeText(writer, DPL::FromUTF8String("NO NAME"));
386             endElement(writer);
387         }
388     }
389     if (!this->icon.empty()) {
390         startElement(writer, "icon");
391         writeText(writer, this->icon);
392         endElement(writer);
393     }
394
395     if (!this->autoLaunch.empty()) {
396         startElement(writer, "launch");
397         writeText(writer, this->autoLaunch);
398         endElement(writer);
399     }
400
401     if (!this->box.boxSrc.empty() &&
402         !this->box.boxMouseEvent.empty() &&
403         !this->box.boxSize.empty())
404     {
405         startElement(writer, "box");
406         writeAttribute(writer, "type", "buffer");
407         writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
408         writeAttribute(writer, "touch_effect", this->box.boxTouchEffect);
409
410         FOREACH(m, this->box.boxSize)
411         {
412             std::pair<DPL::String, DPL::String> boxSize = *m;
413             startElement(writer, "size");
414             if (!boxSize.second.empty()) {
415                 writeAttribute(writer, "preview", boxSize.second);
416             }
417             writeText(writer, boxSize.first);
418             endElement(writer);
419         }
420
421         startElement(writer, "script");
422         writeAttribute(writer, "src", this->box.boxSrc);
423         endElement(writer);
424
425         endElement(writer);
426
427         if (!this->box.pdSrc.empty() &&
428             !this->box.pdWidth.empty() &&
429             !this->box.pdHeight.empty())
430         {
431             startElement(writer, "pd");
432             writeAttribute(writer, "type", "buffer");
433
434             startElement(writer, "size");
435             DPL::String pdSize = this->box.pdWidth + DPL::String(L"x") +
436                 this->box.pdHeight;
437             writeText(writer, pdSize);
438             endElement(writer);
439
440             startElement(writer, "script");
441             writeAttribute(writer, "src", this->box.pdSrc);
442             endElement(writer);
443
444             endElement(writer);
445         }
446     }
447
448     endElement(writer);
449 }
450
451 void Account::serialize(xmlTextWriterPtr writer)
452 {
453     startElement(writer, "account");
454     {
455         startElement(writer, "account-provider");
456         writeAttribute(writer, "app-id", this->provider.appid);
457         writeAttribute(writer, "multiple-accounts-support",
458                 this->provider.multiAccount);
459
460         FOREACH(i, this->provider.icon)
461         {
462             startElement(writer, "icon");
463             writeAttribute(writer, "section", i->first);
464             writeText(writer, i->second);
465             endElement(writer);
466         }
467         FOREACH(n, this->provider.name)
468         {
469             writeElementWithOneAttribute(writer, "label",
470                     n->getString(), "xml:lang",
471                     n->getLang(), n->hasLang());
472         }
473         FOREACH(c, this->provider.capability)
474         {
475             startElement(writer, "capability");
476             writeText(writer, *c);
477             endElement(writer);
478         }
479         endElement(writer);
480     }
481     endElement(writer);
482 }
483
484 void Privilege::serialize(xmlTextWriterPtr writer)
485 {
486     startElement(writer, "privileges");
487     {
488         FOREACH(it, this->name)
489         {
490             startElement(writer, "privilege");
491             writeText(writer, *it);
492             endElement(writer);
493         }
494     }
495     endElement(writer);
496 }
497
498 void Metadata::serialize(xmlTextWriterPtr writer)
499 {
500     startElement(writer, "metadata");
501     writeAttribute(writer, "key", *this->key);
502     if (!this->value.IsNull()) {
503         writeAttribute(writer, "value", *this->value);
504     }
505     endElement(writer);
506 }
507 } //namespace Jobs
508 } //namespace WidgetInstall