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.
6 #include "wgt/step/step_generate_xml.h"
8 #include <boost/filesystem/path.hpp>
9 #include <boost/system/error_code.hpp>
11 #include <common/utils/file_util.h>
12 #include <common/utils/glist_range.h>
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>
26 namespace bs = boost::system;
27 namespace bf = boost::filesystem;
31 void WriteUIApplicationAttributes(
32 xmlTextWriterPtr writer, application_x *app) {
33 xmlTextWriterWriteAttribute(writer, BAD_CAST "taskmanage",
36 xmlTextWriterWriteAttribute(writer, BAD_CAST "nodisplay",
37 BAD_CAST app->nodisplay);
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);
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",
80 void WriteWidgetApplicationAttributes(
81 xmlTextWriterPtr writer, application_x *app) {
83 xmlTextWriterWriteAttribute(writer, BAD_CAST "nodisplay",
84 BAD_CAST app->nodisplay);
86 xmlTextWriterWriteAttribute(writer, BAD_CAST "multiple",
87 BAD_CAST app->multiple);
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);
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());
105 xmlTextWriterWriteAttribute(writer, BAD_CAST "type", BAD_CAST app->type);
107 xmlTextWriterWriteAttribute(writer, BAD_CAST "type", BAD_CAST "capp");
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
114 case AppCompType::UIAPP:
115 WriteServiceApplicationAttributes(writer, app);
117 case AppCompType::SVCAPP:
118 WriteUIApplicationAttributes(writer, app);
120 case AppCompType::WIDGETAPP:
121 WriteWidgetApplicationAttributes(writer, app);
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);
131 xmlTextWriterWriteString(writer, BAD_CAST label->name);
132 xmlTextWriterEndElement(writer);
136 icon_x* iconx = reinterpret_cast<icon_x*>(app->icon->data);
137 xmlTextWriterWriteFormatElement(
138 writer, BAD_CAST "icon", "%s", BAD_CAST iconx->text);
140 // Default icon setting is role of the platform
141 LOG(DEBUG) << "Icon was not found in application";
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",
149 BAD_CAST image->lang);
152 xmlTextWriterWriteAttribute(writer, BAD_CAST "section",
153 BAD_CAST image->section);
154 xmlTextWriterEndElement(writer);
157 for (appcontrol_x* appc : GListRange<appcontrol_x*>(app->appcontrol)) {
158 xmlTextWriterStartElement(writer, BAD_CAST "app-control");
160 if (appc->operation) {
161 xmlTextWriterStartElement(writer, BAD_CAST "operation");
162 xmlTextWriterWriteAttribute(writer, BAD_CAST "name",
163 BAD_CAST appc->operation);
164 xmlTextWriterEndElement(writer);
168 xmlTextWriterStartElement(writer, BAD_CAST "uri");
169 xmlTextWriterWriteAttribute(writer, BAD_CAST "name",
171 xmlTextWriterEndElement(writer);
175 xmlTextWriterStartElement(writer, BAD_CAST "mime");
176 xmlTextWriterWriteAttribute(writer, BAD_CAST "name",
177 BAD_CAST appc->mime);
178 xmlTextWriterEndElement(writer);
181 xmlTextWriterEndElement(writer);
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);
191 if (datacontrol->providerid) {
192 xmlTextWriterWriteAttribute(writer, BAD_CAST "providerid",
193 BAD_CAST datacontrol->providerid);
195 if (datacontrol->type) {
196 xmlTextWriterWriteAttribute(writer, BAD_CAST "type",
197 BAD_CAST datacontrol->type);
199 xmlTextWriterEndElement(writer);
202 for (metadata_x* meta : GListRange<metadata_x*>(app->metadata)) {
203 xmlTextWriterStartElement(writer, BAD_CAST "metadata");
204 xmlTextWriterWriteAttribute(writer, BAD_CAST "key",
207 xmlTextWriterWriteAttribute(writer, BAD_CAST "value",
208 BAD_CAST meta->value);
209 xmlTextWriterEndElement(writer);
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);
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);
226 return Step::Status::OK;
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;
234 if (context_->pkgid.get().empty()) {
235 LOG(ERROR) << "pkgid attribute is empty";
236 return Step::Status::PACKAGE_NOT_FOUND; }
238 if (!context_->manifest_data.get()->application) {
239 LOG(ERROR) << "No application in package";
240 return Step::Status::INVALID_VALUE;
242 // TODO(p.sikorski) check context_->uid.get()
244 return Step::Status::OK;
247 common_installer::Step::Status StepGenerateXml::process() {
249 bf::path(getUserManifestPath(context_->uid.get(), false))
250 / bf::path(context_->pkgid.get());
252 context_->xml_path.set(xml_path.string());
254 bs::error_code error;
255 if (!bf::exists(xml_path.parent_path(), error)) {
256 if (!common_installer::CreateDir(xml_path.parent_path())) {
258 "Directory for manifest xml is missing and cannot be created";
259 return Status::MANIFEST_ERROR;
263 xmlTextWriterPtr writer;
265 writer = xmlNewTextWriterFilename(context_->xml_path.get().c_str(), 0);
267 LOG(ERROR) << "Failed to create new file";
268 return Step::Status::MANIFEST_ERROR;
271 xmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
273 xmlTextWriterSetIndent(writer, 1);
275 // add manifest Element
276 xmlTextWriterStartElement(writer, BAD_CAST "manifest");
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);
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);
298 xmlTextWriterWriteString(writer, BAD_CAST label->name);
299 xmlTextWriterEndElement(writer);
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);
309 if (author->href && strlen(author->href)) {
310 xmlTextWriterWriteAttribute(writer, BAD_CAST "href",
311 BAD_CAST author->href);
313 xmlTextWriterWriteString(writer, BAD_CAST author->text);
314 xmlTextWriterEndElement(writer);
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);
324 xmlTextWriterWriteString(writer, BAD_CAST description->text);
325 xmlTextWriterEndElement(writer);
329 for (application_x* app :
330 GListRange<application_x*>(context_->manifest_data.get()->application)) {
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");
342 LOG(ERROR) << "Unknown application component_type";
343 xmlFreeTextWriter(writer);
344 return Status::ERROR;
346 GenerateApplicationCommonXml(app, writer, type);
347 xmlTextWriterEndElement(writer);
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);
358 xmlTextWriterEndElement(writer);
361 const auto& accounts =
362 context_->manifest_plugins_data.get().account_info.get().accounts();
363 if (!accounts.empty()) {
364 xmlTextWriterStartElement(writer, BAD_CAST "account");
366 for (auto& account : accounts) {
367 xmlTextWriterStartElement(writer, BAD_CAST "account-provider");
369 xmlTextWriterWriteAttribute(writer, BAD_CAST "appid",
370 BAD_CAST account.appid.c_str());
372 if (!account.providerid.empty())
373 xmlTextWriterWriteAttribute(writer, BAD_CAST "providerid",
374 BAD_CAST account.providerid.c_str());
376 if (account.multiple_account_support)
377 xmlTextWriterWriteAttribute(writer,
378 BAD_CAST "multiple-accounts-support",
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");
386 xmlTextWriterWriteAttribute(writer, BAD_CAST "section",
388 xmlTextWriterWriteString(writer, BAD_CAST icon_pair.second.c_str());
389 xmlTextWriterEndElement(writer);
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);
401 for (auto& capability : account.capabilities) {
402 xmlTextWriterWriteFormatElement(writer, BAD_CAST "capability",
403 "%s", BAD_CAST capability.c_str());
406 xmlTextWriterEndElement(writer);
408 xmlTextWriterEndElement(writer);
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",
416 xmlTextWriterEndElement(writer);
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);
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);
447 xmlTextWriterEndElement(writer);
449 xmlTextWriterEndElement(writer);
452 xmlTextWriterEndElement(writer);
454 xmlTextWriterEndDocument(writer);
455 xmlFreeTextWriter(writer);
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;
463 LOG(DEBUG) << "Successfully create manifest xml "
464 << context_->xml_path.get();
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);
475 } // namespace pkgmgr