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