Revert "Register a new appid for dynamic box and its symbolic link to /usr/bin/WebPro...
[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         }
160         writeAttribute(writer, "storeclient-id", this->storeClientId,
161                 !this->storeClientId.empty());
162
163         FOREACH(l, this->label)
164         {
165             writeElementWithOneAttribute(writer, "label", l->getString(),
166                                          "xml:lang", l->getLang(), l->hasLang());
167         }
168         FOREACH(i, this->icon)
169         {
170             writeElementWithOneAttribute(writer, "icon", i->getString(),
171                                          "xml:lang", i->getLang(), i->hasLang());
172         }
173         FOREACH(a, this->author)
174         {
175             a->serialize(writer);
176         }
177         FOREACH(d, this->description)
178         {
179             writeElementWithOneAttribute(writer, "description", d->getString(),
180                                          "xml:lang", d->getLang(), d->hasLang());
181         }
182         //FOREACH(c, this->compatibility) { c->serialize(writer); }
183         //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
184         FOREACH(s, this->serviceApplication) {
185             s->serialize(writer);
186         }
187         FOREACH(u, this->uiApplication) {
188             u->serialize(writer);
189         }
190         FOREACH(i, this->imeApplication) {
191             i->serialize(writer);
192         }
193         //FOREACH(f, this->font) { f->serialize(writer); }
194         FOREACH(l, this->livebox) {
195             l->serialize(writer);
196         }
197         FOREACH(acc, this->account)
198         {
199             acc->serialize(writer);
200         }
201
202         if (!this->privileges.isEmpty()) {
203             this->privileges.serialize(writer);
204         }
205     }
206     endElement(writer);
207 }
208
209 void Author::serialize(xmlTextWriterPtr writer)
210 {
211     startElement(writer, "author");
212     writeAttribute(writer, "email", this->email, !this->email.empty());
213     writeAttribute(writer, "href", this->href, !this->href.empty());
214     writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
215     writeText(writer, body);
216     endElement(writer);
217 }
218
219 void ServiceApplication::serialize(xmlTextWriterPtr writer)
220 {
221     startElement(writer, "service-application");
222     writeAttribute(writer, "appid", this->appid);
223     writeAttribute(writer, "auto-restart",
224                    (!this->autoRestart.IsNull() &&
225                     (*this->autoRestart)) ? "true" :
226                    "false");
227     writeAttribute(writer, "exec", this->exec);
228     writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
229                                        (*this->onBoot)) ? "true" : "false");
230     writeAttribute(writer, "type", this->type);
231     FOREACH(l, this->label)
232     {
233         writeElementWithOneAttribute(writer, "label",
234                                      l->getString(), "xml:lang",
235                                      l->getLang(), l->hasLang());
236     }
237     FOREACH(i, this->icon)
238     {
239         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
240                                      i->getLang(), i->hasLang());
241     }
242     FOREACH(a, this->appControl)
243     {
244         a->serialize(writer);
245     }
246     endElement(writer);
247 }
248
249 void UiApplication::serialize(xmlTextWriterPtr writer)
250 {
251     startElement(writer, "ui-application");
252     writeAttribute(writer, "appid", this->appid);
253     writeAttribute(writer, "exec", this->exec);
254     if (!this->multiple.IsNull()) {
255         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
256     }
257     if (!this->nodisplay.IsNull()) {
258         writeAttribute(writer,
259                        "nodisplay",
260                        (*this->nodisplay) ? "true" : "false");
261     }
262     if (!this->taskmanage.IsNull()) {
263         writeAttribute(writer,
264                        "taskmanage",
265                        (*this->taskmanage) ? "true" : "false");
266     }
267     writeAttribute(writer, "type", this->type);
268     writeAttribute(writer, "extraid", this->extraid);
269     if (!this->categories.IsNull()) {
270         writeAttribute(writer, "categories", (*this->categories));
271     }
272     FOREACH(l, this->label)
273     {
274         writeElementWithOneAttribute(writer, "label",
275                                      l->getString(), "xml:lang",
276                                      l->getLang(), l->hasLang());
277     }
278     FOREACH(i, this->icon)
279     {
280         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
281                                      i->getLang(), i->hasLang());
282     }
283     FOREACH(a, this->appControl)
284     {
285         a->serialize(writer);
286     }
287     FOREACH(c, this->appCategory)
288     {
289         startElement(writer, "category");
290         writeAttribute(writer, "name", *c);
291         endElement(writer);
292     }
293     FOREACH(m, this->metadata) {
294         m->serialize(writer);
295     }
296     endElement(writer);
297 }
298
299 void ImeApplication::serialize(xmlTextWriterPtr writer)
300 {
301     startElement(writer, "ime-application");
302     writeAttribute(writer, "appid", this->appid);
303     writeAttribute(writer, "exec", this->exec);
304     if (!this->multiple.IsNull()) {
305         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
306     }
307     if (!this->nodisplay.IsNull()) {
308         writeAttribute(writer,
309                        "nodisplay",
310                        (*this->nodisplay) ? "true" : "false");
311     }
312     writeAttribute(writer, "type", this->type);
313     FOREACH(l, this->label)
314     {
315         writeElementWithOneAttribute(writer, "label",
316                                      l->getString(), "xml:lang",
317                                      l->getLang(), l->hasLang());
318     }
319     FOREACH(i, this->icon)
320     {
321         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
322                                      i->getLang(), i->hasLang());
323     }
324     endElement(writer);
325 }
326
327 void AppControl::serialize(xmlTextWriterPtr writer)
328 {
329     startElement(writer, "app-control");
330     FOREACH(o, this->operation)
331     {
332         startElement(writer, "operation");
333         writeAttribute(writer, "name", *o);
334         endElement(writer);
335     }
336     FOREACH(u, this->uri)
337     {
338         startElement(writer, "uri");
339         writeAttribute(writer, "name", *u);
340         endElement(writer);
341     }
342     FOREACH(m, this->mime)
343     {
344         startElement(writer, "mime");
345         writeAttribute(writer, "name", *m);
346         endElement(writer);
347     }
348     endElement(writer);
349 }
350
351 void LiveBox::serialize(xmlTextWriterPtr writer)
352 {
353     startElement(writer, "livebox");
354     if (!this->liveboxId.empty()) {
355         writeAttribute(writer, "appid", this->liveboxId);
356     }
357
358     if (!this->primary.empty()) {
359         writeAttribute(writer, "primary", this->primary);
360     }
361
362     if (!this->updatePeriod.empty()) {
363         writeAttribute(writer, "period", this->updatePeriod);
364     }
365
366     writeAttribute(writer, "abi", "html");
367     writeAttribute(writer, "network", "true");
368     writeAttribute(writer, "nodisplay", "false");
369
370     if (!this->label.empty()) {
371         int defaultLabelChk = 0;
372         FOREACH(m, this->label)
373         {
374             std::pair<DPL::String, DPL::String> boxLabel = *m;
375             startElement(writer, "label");
376             if (!boxLabel.first.empty()) {
377                 writeAttribute(writer, "xml:lang", boxLabel.first);
378             } else {
379                 defaultLabelChk++;
380             }
381             writeText(writer, boxLabel.second);
382             endElement(writer);
383         }
384         if(!defaultLabelChk) {
385             startElement(writer, "label");
386             writeText(writer, DPL::FromUTF8String("NO NAME"));
387             endElement(writer);
388         }
389     }
390     if (!this->icon.empty()) {
391         startElement(writer, "icon");
392         writeText(writer, this->icon);
393         endElement(writer);
394     }
395
396     if (!this->autoLaunch.empty()) {
397         startElement(writer, "launch");
398         writeText(writer, this->autoLaunch);
399         endElement(writer);
400     }
401
402     if (!this->box.boxSrc.empty() &&
403         !this->box.boxMouseEvent.empty() &&
404         !this->box.boxSize.empty())
405     {
406         startElement(writer, "box");
407         writeAttribute(writer, "type", "buffer");
408         writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
409         writeAttribute(writer, "touch_effect", this->box.boxTouchEffect);
410
411         FOREACH(it, this->box.boxSize)
412         {
413             startElement(writer, "size");
414             if (!(*it).m_preview.empty()) {
415                 writeAttribute(writer, "preview", (*it).m_preview);
416             }
417             if (!(*it).m_useDecoration.empty()) {
418                 writeAttribute(writer, "need_frame", (*it).m_useDecoration);
419             } else {
420                 // default value of use-decoration is "true"
421                 writeAttribute(writer, "need_frame", DPL::String(L"true"));
422             }
423
424             writeText(writer, (*it).m_size);
425             endElement(writer);
426         }
427
428         startElement(writer, "script");
429         writeAttribute(writer, "src", this->box.boxSrc);
430         endElement(writer);
431
432         endElement(writer);
433
434         if (!this->box.pdSrc.empty() &&
435             !this->box.pdWidth.empty() &&
436             !this->box.pdHeight.empty())
437         {
438             startElement(writer, "pd");
439             writeAttribute(writer, "type", "buffer");
440
441             startElement(writer, "size");
442             DPL::String pdSize = this->box.pdWidth + DPL::String(L"x") +
443                 this->box.pdHeight;
444             writeText(writer, pdSize);
445             endElement(writer);
446
447             startElement(writer, "script");
448             writeAttribute(writer, "src", this->box.pdSrc);
449             endElement(writer);
450
451             endElement(writer);
452         }
453     }
454
455     endElement(writer);
456 }
457
458 void Account::serialize(xmlTextWriterPtr writer)
459 {
460     startElement(writer, "account");
461     {
462         startElement(writer, "account-provider");
463         writeAttribute(writer, "app-id", this->provider.appid);
464         writeAttribute(writer, "multiple-accounts-support",
465                 this->provider.multiAccount);
466
467         FOREACH(i, this->provider.icon)
468         {
469             startElement(writer, "icon");
470             writeAttribute(writer, "section", i->first);
471             writeText(writer, i->second);
472             endElement(writer);
473         }
474         FOREACH(n, this->provider.name)
475         {
476             writeElementWithOneAttribute(writer, "label",
477                     n->getString(), "xml:lang",
478                     n->getLang(), n->hasLang());
479         }
480         FOREACH(c, this->provider.capability)
481         {
482             startElement(writer, "capability");
483             writeText(writer, *c);
484             endElement(writer);
485         }
486         endElement(writer);
487     }
488     endElement(writer);
489 }
490
491 void Privilege::serialize(xmlTextWriterPtr writer)
492 {
493     startElement(writer, "privileges");
494     {
495         FOREACH(it, this->name)
496         {
497             startElement(writer, "privilege");
498             writeText(writer, *it);
499             endElement(writer);
500         }
501     }
502     endElement(writer);
503 }
504
505 void Metadata::serialize(xmlTextWriterPtr writer)
506 {
507     startElement(writer, "metadata");
508     writeAttribute(writer, "key", *this->key);
509     if (!this->value.IsNull()) {
510         writeAttribute(writer, "value", *this->value);
511     }
512     endElement(writer);
513 }
514 } //namespace Jobs
515 } //namespace WidgetInstall