Add Account parser and generate manifest.
[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     endElement(writer);
202 }
203
204 void Author::serialize(xmlTextWriterPtr writer)
205 {
206     startElement(writer, "author");
207     writeAttribute(writer, "email", this->email, !this->email.empty());
208     writeAttribute(writer, "href", this->href, !this->href.empty());
209     writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
210     writeText(writer, body);
211     endElement(writer);
212 }
213
214 void ServiceApplication::serialize(xmlTextWriterPtr writer)
215 {
216     startElement(writer, "service-application");
217     writeAttribute(writer, "appid", this->appid);
218     writeAttribute(writer, "auto-restart",
219                    (!this->autoRestart.IsNull() &&
220                     (*this->autoRestart)) ? "true" :
221                    "false");
222     writeAttribute(writer, "exec", this->exec);
223     writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
224                                        (*this->onBoot)) ? "true" : "false");
225     writeAttribute(writer, "type", this->type);
226     FOREACH(l, this->label)
227     {
228         writeElementWithOneAttribute(writer, "label",
229                                      l->getString(), "xml:lang",
230                                      l->getLang(), l->hasLang());
231     }
232     FOREACH(i, this->icon)
233     {
234         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
235                                      i->getLang(), i->hasLang());
236     }
237     FOREACH(a, this->appControl)
238     {
239         a->serialize(writer);
240     }
241     endElement(writer);
242 }
243
244 void UiApplication::serialize(xmlTextWriterPtr writer)
245 {
246     startElement(writer, "ui-application");
247     writeAttribute(writer, "appid", this->appid);
248     writeAttribute(writer, "exec", this->exec);
249     if (!this->multiple.IsNull()) {
250         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
251     }
252     if (!this->nodisplay.IsNull()) {
253         writeAttribute(writer,
254                        "nodisplay",
255                        (*this->nodisplay) ? "true" : "false");
256     }
257     if (!this->taskmanage.IsNull()) {
258         writeAttribute(writer,
259                        "taskmanage",
260                        (*this->taskmanage) ? "true" : "false");
261     }
262     writeAttribute(writer, "type", this->type);
263     writeAttribute(writer, "extraid", this->extraid);
264     if (!this->categories.IsNull()) {
265         writeAttribute(writer, "categories", (*this->categories));
266     }
267     FOREACH(l, this->label)
268     {
269         writeElementWithOneAttribute(writer, "label",
270                                      l->getString(), "xml:lang",
271                                      l->getLang(), l->hasLang());
272     }
273     FOREACH(i, this->icon)
274     {
275         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
276                                      i->getLang(), i->hasLang());
277     }
278     FOREACH(a, this->appControl)
279     {
280         a->serialize(writer);
281     }
282     FOREACH(c, this->appCategory)
283     {
284         startElement(writer, "category");
285         writeAttribute(writer, "name", *c);
286         endElement(writer);
287     }
288     endElement(writer);
289 }
290
291 void ImeApplication::serialize(xmlTextWriterPtr writer)
292 {
293     startElement(writer, "ime-application");
294     writeAttribute(writer, "appid", this->appid);
295     writeAttribute(writer, "exec", this->exec);
296     if (!this->multiple.IsNull()) {
297         writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
298     }
299     if (!this->nodisplay.IsNull()) {
300         writeAttribute(writer,
301                        "nodisplay",
302                        (*this->nodisplay) ? "true" : "false");
303     }
304     writeAttribute(writer, "type", this->type);
305     FOREACH(l, this->label)
306     {
307         writeElementWithOneAttribute(writer, "label",
308                                      l->getString(), "xml:lang",
309                                      l->getLang(), l->hasLang());
310     }
311     FOREACH(i, this->icon)
312     {
313         writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
314                                      i->getLang(), i->hasLang());
315     }
316     endElement(writer);
317 }
318
319 void AppControl::serialize(xmlTextWriterPtr writer)
320 {
321     startElement(writer, "app-control");
322     FOREACH(o, this->operation)
323     {
324         startElement(writer, "operation");
325         writeAttribute(writer, "name", *o);
326         endElement(writer);
327     }
328     FOREACH(u, this->uri)
329     {
330         startElement(writer, "uri");
331         writeAttribute(writer, "name", *u);
332         endElement(writer);
333     }
334     FOREACH(m, this->mime)
335     {
336         startElement(writer, "mime");
337         writeAttribute(writer, "name", *m);
338         endElement(writer);
339     }
340     endElement(writer);
341 }
342
343 void LiveBox::serialize(xmlTextWriterPtr writer)
344 {
345     startElement(writer, "livebox");
346     if (!this->liveboxId.empty()) {
347         writeAttribute(writer, "appid", this->liveboxId);
348     }
349
350     if (!this->primary.empty()) {
351         writeAttribute(writer, "primary", this->primary);
352     }
353
354     if (!this->updatePeriod.empty()) {
355         writeAttribute(writer, "period", this->updatePeriod);
356     }
357
358     writeAttribute(writer, "abi", "html");
359     writeAttribute(writer, "network", "true");
360     writeAttribute(writer, "nodisplay", "false");
361
362     if (!this->label.empty()) {
363         startElement(writer, "label");
364         writeText(writer, this->label);
365         endElement(writer);
366     }
367
368     if (!this->icon.empty()) {
369         startElement(writer, "icon");
370         writeText(writer, this->icon);
371         endElement(writer);
372     }
373
374     if (!this->autoLaunch.empty()) {
375         startElement(writer, "launch");
376         writeText(writer, this->autoLaunch);
377         endElement(writer);
378     }
379
380     if (!this->box.boxSrc.empty() &&
381         !this->box.boxMouseEvent.empty() &&
382         !this->box.boxSize.empty())
383     {
384         startElement(writer, "box");
385         writeAttribute(writer, "type", "buffer");
386         writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
387         writeAttribute(writer, "touch_effect", this->box.boxTouchEffect);
388
389         FOREACH(m, this->box.boxSize)
390         {
391             std::pair<DPL::String, DPL::String> boxSize = *m;
392             startElement(writer, "size");
393             if (!boxSize.second.empty()) {
394                 writeAttribute(writer, "preview", boxSize.second);
395             }
396             writeText(writer, boxSize.first);
397             endElement(writer);
398         }
399
400         startElement(writer, "script");
401         writeAttribute(writer, "src", this->box.boxSrc);
402         endElement(writer);
403
404         endElement(writer);
405
406         if (!this->box.pdSrc.empty() &&
407             !this->box.pdWidth.empty() &&
408             !this->box.pdHeight.empty())
409         {
410             startElement(writer, "pd");
411             writeAttribute(writer, "type", "buffer");
412
413             startElement(writer, "size");
414             DPL::String pdSize = this->box.pdWidth + DPL::String(L"x") +
415                 this->box.pdHeight;
416             writeText(writer, pdSize);
417             endElement(writer);
418
419             startElement(writer, "script");
420             writeAttribute(writer, "src", this->box.pdSrc);
421             endElement(writer);
422
423             endElement(writer);
424         }
425     }
426
427     endElement(writer);
428 }
429
430 void Account::serialize(xmlTextWriterPtr writer)
431 {
432     startElement(writer, "account");
433     {
434         startElement(writer, "account-provider");
435         writeAttribute(writer, "app-id", this->provider.appid);
436         writeAttribute(writer, "multiple-accounts-support",
437                 this->provider.multiAccount);
438
439         FOREACH(i, this->provider.icon)
440         {
441             startElement(writer, "icon");
442             writeAttribute(writer, "section", i->first);
443             writeText(writer, i->second);
444             endElement(writer);
445         }
446         FOREACH(n, this->provider.name)
447         {
448             writeElementWithOneAttribute(writer, "label",
449                     n->getString(), "xml:lang",
450                     n->getLang(), n->hasLang());
451         }
452         FOREACH(c, this->provider.capability)
453         {
454             startElement(writer, "capability");
455             writeText(writer, *c);
456             endElement(writer);
457         }
458         endElement(writer);
459     }
460     endElement(writer);
461 }
462 } //namespace Jobs
463 } //namespace WidgetInstall