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