modification for seperation of RO/RW location
[platform/core/appfw/wgt-backend.git] / src / wgt / step / step_generate_xml.cc
1 /* 2014, Copyright © Intel Coporation, license APACHE-2.0, see LICENSE file */
2 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a apache 2.0 license that can be
4 // found in the LICENSE file.
5
6 #include "wgt/step/step_generate_xml.h"
7
8 #include <boost/filesystem/path.hpp>
9 #include <boost/system/error_code.hpp>
10
11 #include <common/utils/file_util.h>
12 #include <common/utils/glist_range.h>
13
14 #include <libxml/parser.h>
15 #include <libxml/xmlreader.h>
16 #include <pkgmgr-info.h>
17 #include <pkgmgr_parser.h>
18 #include <tzplatform_config.h>
19 #include <unistd.h>
20
21 #include <cassert>
22 #include <cstring>
23 #include <string>
24
25
26 namespace bs = boost::system;
27 namespace bf = boost::filesystem;
28
29 namespace {
30
31 void WriteUIApplicationAttributes(
32     xmlTextWriterPtr writer, application_x *app) {
33   xmlTextWriterWriteAttribute(writer, BAD_CAST "taskmanage",
34       BAD_CAST "true");
35   if (app->nodisplay)
36     xmlTextWriterWriteAttribute(writer, BAD_CAST "nodisplay",
37         BAD_CAST app->nodisplay);
38   if (app->multiple)
39     xmlTextWriterWriteAttribute(writer, BAD_CAST "multiple",
40         BAD_CAST app->multiple);
41   if (app->launch_mode && strlen(app->launch_mode))
42     xmlTextWriterWriteAttribute(writer, BAD_CAST "launch_mode",
43         BAD_CAST app->launch_mode);
44   if (app->ui_gadget && strlen(app->ui_gadget))
45     xmlTextWriterWriteAttribute(writer, BAD_CAST "ui-gadget",
46         BAD_CAST app->ui_gadget);
47   if (app->submode && strlen(app->submode))
48     xmlTextWriterWriteAttribute(writer, BAD_CAST "submode",
49         BAD_CAST app->submode);
50   if (app->submode_mainid && strlen(app->submode_mainid))
51     xmlTextWriterWriteAttribute(writer, BAD_CAST "submode-mainid",
52         BAD_CAST app->submode_mainid);
53   if (app->indicatordisplay && strlen(app->indicatordisplay))
54     xmlTextWriterWriteAttribute(writer, BAD_CAST "indicatordisplay",
55         BAD_CAST app->indicatordisplay);
56   if (app->portraitimg && strlen(app->portraitimg))
57     xmlTextWriterWriteAttribute(writer, BAD_CAST "portrait-effectimage",
58         BAD_CAST app->portraitimg);
59   if (app->landscapeimg && strlen(app->landscapeimg))
60     xmlTextWriterWriteAttribute(writer, BAD_CAST "landscape-effectimage",
61         BAD_CAST app->landscapeimg);
62   if (app->effectimage_type && strlen(app->effectimage_type))
63     xmlTextWriterWriteAttribute(writer, BAD_CAST "effectimage-type",
64         BAD_CAST app->effectimage_type);
65   if (app->hwacceleration && strlen(app->hwacceleration))
66     xmlTextWriterWriteAttribute(writer, BAD_CAST "hwacceleration",
67         BAD_CAST app->hwacceleration);
68 }
69
70 void WriteServiceApplicationAttributes(
71     xmlTextWriterPtr writer, application_x *app) {
72   xmlTextWriterWriteAttribute(writer, BAD_CAST "auto-restart",
73       BAD_CAST(app->autorestart ? app->autorestart : "false"));
74   xmlTextWriterWriteAttribute(writer, BAD_CAST "on-boot",
75       BAD_CAST(app->onboot ? app->onboot : "false"));
76   xmlTextWriterWriteAttribute(writer, BAD_CAST "taskmanage",
77       BAD_CAST "false");
78 }
79
80 void WriteWidgetApplicationAttributes(
81     xmlTextWriterPtr writer, application_x *app) {
82   if (app->nodisplay)
83     xmlTextWriterWriteAttribute(writer, BAD_CAST "nodisplay",
84         BAD_CAST app->nodisplay);
85   if (app->multiple)
86     xmlTextWriterWriteAttribute(writer, BAD_CAST "multiple",
87         BAD_CAST app->multiple);
88 }
89
90 }  // namespace
91
92 namespace wgt {
93 namespace pkgmgr {
94
95 common_installer::Step::Status StepGenerateXml::GenerateApplicationCommonXml(
96     application_x* app, xmlTextWriterPtr writer, AppCompType type) {
97   xmlTextWriterWriteAttribute(writer, BAD_CAST "appid", BAD_CAST app->appid);
98
99   // binary is a symbolic link named <appid> and is located in <pkgid>/<appid>
100   bf::path exec_path = context_->pkg_path.get()
101       / bf::path("bin") / bf::path(app->appid);
102   xmlTextWriterWriteAttribute(writer, BAD_CAST "exec",
103                               BAD_CAST exec_path.string().c_str());
104   if (app->type)
105     xmlTextWriterWriteAttribute(writer, BAD_CAST "type", BAD_CAST app->type);
106   else
107     xmlTextWriterWriteAttribute(writer, BAD_CAST "type", BAD_CAST "capp");
108
109   if (app->process_pool && strlen(app->process_pool))
110     xmlTextWriterWriteAttribute(writer, BAD_CAST "process-pool",
111                                 BAD_CAST app->process_pool);
112   // app-specific attributes
113   switch (type) {
114   case AppCompType::UIAPP:
115     WriteServiceApplicationAttributes(writer, app);
116     break;
117   case AppCompType::SVCAPP:
118     WriteUIApplicationAttributes(writer, app);
119     break;
120   case AppCompType::WIDGETAPP:
121     WriteWidgetApplicationAttributes(writer, app);
122     break;
123   }
124
125   for (label_x* label : GListRange<label_x*>(app->label)) {
126     xmlTextWriterStartElement(writer, BAD_CAST "label");
127     if (label->lang && strcmp(DEFAULT_LOCALE, label->lang) != 0) {
128       xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
129                                   BAD_CAST label->lang);
130     }
131     xmlTextWriterWriteString(writer, BAD_CAST label->name);
132     xmlTextWriterEndElement(writer);
133   }
134
135   if (app->icon) {
136     icon_x* iconx = reinterpret_cast<icon_x*>(app->icon->data);
137     xmlTextWriterWriteFormatElement(
138         writer, BAD_CAST "icon", "%s", BAD_CAST iconx->text);
139   } else {
140     // Default icon setting is role of the platform
141     LOG(DEBUG) << "Icon was not found in application";
142   }
143
144   for (image_x* image : GListRange<image_x*>(app->image)) {
145     xmlTextWriterStartElement(writer, BAD_CAST "image");
146     if (image->lang && strcmp(DEFAULT_LOCALE, image->lang) != 0) {
147       xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
148
149                                   BAD_CAST image->lang);
150     }
151     if (image->section)
152       xmlTextWriterWriteAttribute(writer, BAD_CAST "section",
153                                   BAD_CAST image->section);
154     xmlTextWriterEndElement(writer);
155   }
156
157   for (appcontrol_x* appc : GListRange<appcontrol_x*>(app->appcontrol)) {
158     xmlTextWriterStartElement(writer, BAD_CAST "app-control");
159
160     if (appc->operation) {
161       xmlTextWriterStartElement(writer, BAD_CAST "operation");
162       xmlTextWriterWriteAttribute(writer, BAD_CAST "name",
163           BAD_CAST appc->operation);
164       xmlTextWriterEndElement(writer);
165     }
166
167     if (appc->uri) {
168       xmlTextWriterStartElement(writer, BAD_CAST "uri");
169       xmlTextWriterWriteAttribute(writer, BAD_CAST "name",
170           BAD_CAST appc->uri);
171       xmlTextWriterEndElement(writer);
172     }
173
174     if (appc->mime) {
175       xmlTextWriterStartElement(writer, BAD_CAST "mime");
176       xmlTextWriterWriteAttribute(writer, BAD_CAST "name",
177           BAD_CAST appc->mime);
178       xmlTextWriterEndElement(writer);
179     }
180
181     xmlTextWriterEndElement(writer);
182   }
183
184   for (datacontrol_x* datacontrol :
185        GListRange<datacontrol_x*>(app->datacontrol)) {
186     xmlTextWriterStartElement(writer, BAD_CAST "datacontrol");
187     if (datacontrol->access) {
188       xmlTextWriterWriteAttribute(writer, BAD_CAST "access",
189           BAD_CAST datacontrol->access);
190     }
191     if (datacontrol->providerid) {
192       xmlTextWriterWriteAttribute(writer, BAD_CAST "providerid",
193           BAD_CAST datacontrol->providerid);
194     }
195     if (datacontrol->type) {
196       xmlTextWriterWriteAttribute(writer, BAD_CAST "type",
197           BAD_CAST datacontrol->type);
198     }
199     xmlTextWriterEndElement(writer);
200   }
201
202   for (metadata_x* meta : GListRange<metadata_x*>(app->metadata)) {
203     xmlTextWriterStartElement(writer, BAD_CAST "metadata");
204     xmlTextWriterWriteAttribute(writer, BAD_CAST "key",
205         BAD_CAST meta->key);
206     if (meta->value)
207       xmlTextWriterWriteAttribute(writer, BAD_CAST "value",
208           BAD_CAST meta->value);
209     xmlTextWriterEndElement(writer);
210   }
211
212   for (const char* category : GListRange<char*>(app->category)) {
213     xmlTextWriterStartElement(writer, BAD_CAST "category");
214     xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST category);
215     xmlTextWriterEndElement(writer);
216   }
217
218   for (const char* background_category : GListRange<char*>(
219       app->background_category)) {
220     xmlTextWriterStartElement(writer, BAD_CAST "background-category");
221     xmlTextWriterWriteAttribute(writer, BAD_CAST "value",
222         BAD_CAST background_category);
223     xmlTextWriterEndElement(writer);
224   }
225
226   return Step::Status::OK;
227 }
228
229 common_installer::Step::Status StepGenerateXml::precheck() {
230   if (!context_->manifest_data.get()) {
231     LOG(ERROR) << "manifest_data attribute is empty";
232     return Step::Status::INVALID_VALUE;
233   }
234   if (context_->pkgid.get().empty()) {
235     LOG(ERROR) << "pkgid attribute is empty";
236     return Step::Status::PACKAGE_NOT_FOUND;   }
237
238   if (!context_->manifest_data.get()->application) {
239     LOG(ERROR) << "No application in package";
240     return Step::Status::INVALID_VALUE;
241   }
242   // TODO(p.sikorski) check context_->uid.get()
243
244   return Step::Status::OK;
245 }
246
247 common_installer::Step::Status StepGenerateXml::process() {
248   bf::path xml_path =
249       bf::path(getUserManifestPath(context_->uid.get(), false))
250       / bf::path(context_->pkgid.get());
251   xml_path += ".xml";
252   context_->xml_path.set(xml_path.string());
253
254   bs::error_code error;
255   if (!bf::exists(xml_path.parent_path(), error)) {
256     if (!common_installer::CreateDir(xml_path.parent_path())) {
257       LOG(ERROR) <<
258           "Directory for manifest xml is missing and cannot be created";
259       return Status::MANIFEST_ERROR;
260     }
261   }
262
263   xmlTextWriterPtr writer;
264
265   writer = xmlNewTextWriterFilename(context_->xml_path.get().c_str(), 0);
266   if (!writer) {
267     LOG(ERROR) << "Failed to create new file";
268     return Step::Status::MANIFEST_ERROR;
269   }
270
271   xmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
272
273   xmlTextWriterSetIndent(writer, 1);
274
275   // add manifest Element
276   xmlTextWriterStartElement(writer, BAD_CAST "manifest");
277
278   xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns",
279       BAD_CAST "http://tizen.org/ns/packages");
280   xmlTextWriterWriteAttribute(writer, BAD_CAST "package",
281       BAD_CAST context_->manifest_data.get()->package);
282   xmlTextWriterWriteAttribute(writer, BAD_CAST "type",
283       BAD_CAST context_->manifest_data.get()->type);
284   xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
285       BAD_CAST context_->manifest_data.get()->version);
286   xmlTextWriterWriteAttribute(writer, BAD_CAST "api-version",
287       BAD_CAST context_->manifest_data.get()->api_version);
288   xmlTextWriterWriteAttribute(writer, BAD_CAST "nodisplay-setting",
289       BAD_CAST context_->manifest_data.get()->nodisplay_setting);
290
291   for (label_x* label :
292        GListRange<label_x*>(context_->manifest_data.get()->label)) {
293     xmlTextWriterStartElement(writer, BAD_CAST "label");
294     if (label->lang && strcmp(DEFAULT_LOCALE, label->lang) != 0) {
295       xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
296                                   BAD_CAST label->lang);
297     }
298     xmlTextWriterWriteString(writer, BAD_CAST label->name);
299     xmlTextWriterEndElement(writer);
300   }
301
302   for (author_x* author :
303        GListRange<author_x*>(context_->manifest_data.get()->author)) {
304     xmlTextWriterStartElement(writer, BAD_CAST "author");
305     if (author->email && strlen(author->email)) {
306       xmlTextWriterWriteAttribute(writer, BAD_CAST "email",
307                                   BAD_CAST author->email);
308     }
309     if (author->href && strlen(author->href)) {
310       xmlTextWriterWriteAttribute(writer, BAD_CAST "href",
311                                   BAD_CAST author->href);
312     }
313     xmlTextWriterWriteString(writer, BAD_CAST author->text);
314     xmlTextWriterEndElement(writer);
315   }
316
317   for (description_x* description :
318        GListRange<description_x*>(context_->manifest_data.get()->description)) {
319     xmlTextWriterStartElement(writer, BAD_CAST "description");
320     if (description->lang && strcmp(DEFAULT_LOCALE, description->lang) != 0) {
321       xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
322                                   BAD_CAST description->lang);
323     }
324     xmlTextWriterWriteString(writer, BAD_CAST description->text);
325     xmlTextWriterEndElement(writer);
326   }
327
328   // add application
329   for (application_x* app :
330        GListRange<application_x*>(context_->manifest_data.get()->application)) {
331     AppCompType type;
332     if (strcmp(app->component_type, "uiapp") == 0) {
333       type = AppCompType::UIAPP;
334       xmlTextWriterStartElement(writer, BAD_CAST "ui-application");
335     } else if (strcmp(app->component_type, "svcapp") == 0) {
336       type = AppCompType::SVCAPP;
337       xmlTextWriterStartElement(writer, BAD_CAST "service-application");
338     } else if (strcmp(app->component_type, "widgetapp") == 0) {
339       type = AppCompType::WIDGETAPP;
340       xmlTextWriterStartElement(writer, BAD_CAST "widget-application");
341     } else {
342       LOG(ERROR) << "Unknown application component_type";
343       xmlFreeTextWriter(writer);
344       return Status::ERROR;
345     }
346     GenerateApplicationCommonXml(app, writer, type);
347     xmlTextWriterEndElement(writer);
348   }
349
350   // add privilege element
351   if (context_->manifest_data.get()->privileges) {
352     xmlTextWriterStartElement(writer, BAD_CAST "privileges");
353     for (const char* priv :
354          GListRange<char*>(context_->manifest_data.get()->privileges)) {
355       xmlTextWriterWriteFormatElement(writer, BAD_CAST "privilege",
356         "%s", BAD_CAST priv);
357     }
358     xmlTextWriterEndElement(writer);
359   }
360
361   const auto& accounts =
362       context_->manifest_plugins_data.get().account_info.get().accounts();
363   if (!accounts.empty()) {
364     xmlTextWriterStartElement(writer, BAD_CAST "account");
365     // add account info
366     for (auto& account : accounts) {
367       xmlTextWriterStartElement(writer, BAD_CAST "account-provider");
368
369       xmlTextWriterWriteAttribute(writer, BAD_CAST "appid",
370                                   BAD_CAST account.appid.c_str());
371
372       if (!account.providerid.empty())
373         xmlTextWriterWriteAttribute(writer, BAD_CAST "providerid",
374                                     BAD_CAST account.providerid.c_str());
375
376       if (account.multiple_account_support)
377         xmlTextWriterWriteAttribute(writer,
378                                     BAD_CAST "multiple-accounts-support",
379                                     BAD_CAST "true");
380       for (auto& icon_pair : account.icon_paths) {
381         xmlTextWriterStartElement(writer, BAD_CAST "icon");
382         if (icon_pair.first == "AccountSmall")
383           xmlTextWriterWriteAttribute(writer, BAD_CAST "section",
384                                       BAD_CAST "account-small");
385         else
386           xmlTextWriterWriteAttribute(writer, BAD_CAST "section",
387                                       BAD_CAST "account");
388         xmlTextWriterWriteString(writer, BAD_CAST icon_pair.second.c_str());
389         xmlTextWriterEndElement(writer);
390       }
391
392       for (auto& name_pair : account.names) {
393         xmlTextWriterStartElement(writer, BAD_CAST "label");
394         if (!name_pair.second.empty())
395           xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
396                                       BAD_CAST name_pair.second.c_str());
397         xmlTextWriterWriteString(writer, BAD_CAST name_pair.first.c_str());
398         xmlTextWriterEndElement(writer);
399       }
400
401       for (auto& capability : account.capabilities) {
402         xmlTextWriterWriteFormatElement(writer, BAD_CAST "capability",
403           "%s", BAD_CAST capability.c_str());
404       }
405
406       xmlTextWriterEndElement(writer);
407     }
408     xmlTextWriterEndElement(writer);
409   }
410
411   for (const char* profile :
412        GListRange<char*>(context_->manifest_data.get()->deviceprofile)) {
413     xmlTextWriterStartElement(writer, BAD_CAST "profile");
414     xmlTextWriterWriteAttribute(writer, BAD_CAST "name",
415                                 BAD_CAST profile);
416     xmlTextWriterEndElement(writer);
417   }
418
419   const auto& shortcuts =
420       context_->manifest_plugins_data.get().shortcut_info.get();
421   if (!shortcuts.empty()) {
422     xmlTextWriterStartElement(writer, BAD_CAST "shortcut-list");
423     for (auto& shortcut : shortcuts) {
424       xmlTextWriterStartElement(writer, BAD_CAST "shortcut");
425       if (!shortcut.app_id.empty())
426         xmlTextWriterWriteAttribute(writer, BAD_CAST "appid",
427                                     BAD_CAST shortcut.app_id.c_str());
428       if (!shortcut.app_id.empty())
429         xmlTextWriterWriteAttribute(writer, BAD_CAST "extra_data",
430                                     BAD_CAST shortcut.extra_data.c_str());
431       if (!shortcut.app_id.empty())
432         xmlTextWriterWriteAttribute(writer, BAD_CAST "extra_key",
433                                     BAD_CAST shortcut.extra_key.c_str());
434       if (!shortcut.icon.empty()) {
435         xmlTextWriterStartElement(writer, BAD_CAST "icon");
436         xmlTextWriterWriteString(writer, BAD_CAST shortcut.icon.c_str());
437         xmlTextWriterEndElement(writer);
438       }
439       for (auto& label : shortcut.labels) {
440         xmlTextWriterStartElement(writer, BAD_CAST "label");
441         if (!label.first.empty())
442           xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
443                                       BAD_CAST label.first.c_str());
444         xmlTextWriterWriteString(writer, BAD_CAST label.second.c_str());
445         xmlTextWriterEndElement(writer);
446       }
447       xmlTextWriterEndElement(writer);
448     }
449     xmlTextWriterEndElement(writer);
450   }
451
452   xmlTextWriterEndElement(writer);
453
454   xmlTextWriterEndDocument(writer);
455   xmlFreeTextWriter(writer);
456
457   if (pkgmgr_parser_check_manifest_validation(
458       context_->xml_path.get().c_str()) != 0) {
459     LOG(ERROR) << "Manifest is not valid";
460     return Step::Status::MANIFEST_ERROR;
461   }
462
463   LOG(DEBUG) << "Successfully create manifest xml "
464       << context_->xml_path.get();
465   return Status::OK;
466 }
467
468 common_installer::Step::Status StepGenerateXml::undo() {
469   bs::error_code error;
470   if (bf::exists(context_->xml_path.get()))
471     bf::remove_all(context_->xml_path.get(), error);
472   return Status::OK;
473 }
474
475 }  // namespace pkgmgr
476 }  // namespace wgt