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() {
248 bf::path xml_path = bf::path(getUserManifestPath(context_->uid.get()))
249 / bf::path(context_->pkgid.get());
251 context_->xml_path.set(xml_path.string());
253 bs::error_code error;
254 if (!bf::exists(xml_path.parent_path(), error)) {
255 if (!common_installer::CreateDir(xml_path.parent_path())) {
257 "Directory for manifest xml is missing and cannot be created";
258 return Status::MANIFEST_ERROR;
262 xmlTextWriterPtr writer;
264 writer = xmlNewTextWriterFilename(context_->xml_path.get().c_str(), 0);
266 LOG(ERROR) << "Failed to create new file";
267 return Step::Status::MANIFEST_ERROR;
270 xmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
272 xmlTextWriterSetIndent(writer, 1);
274 // add manifest Element
275 xmlTextWriterStartElement(writer, BAD_CAST "manifest");
277 xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns",
278 BAD_CAST "http://tizen.org/ns/packages");
279 xmlTextWriterWriteAttribute(writer, BAD_CAST "package",
280 BAD_CAST context_->manifest_data.get()->package);
281 xmlTextWriterWriteAttribute(writer, BAD_CAST "type",
282 BAD_CAST context_->manifest_data.get()->type);
283 xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
284 BAD_CAST context_->manifest_data.get()->version);
285 xmlTextWriterWriteAttribute(writer, BAD_CAST "api-version",
286 BAD_CAST context_->manifest_data.get()->api_version);
287 xmlTextWriterWriteAttribute(writer, BAD_CAST "nodisplay-setting",
288 BAD_CAST context_->manifest_data.get()->nodisplay_setting);
290 for (label_x* label :
291 GListRange<label_x*>(context_->manifest_data.get()->label)) {
292 xmlTextWriterStartElement(writer, BAD_CAST "label");
293 if (label->lang && strcmp(DEFAULT_LOCALE, label->lang) != 0) {
294 xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
295 BAD_CAST label->lang);
297 xmlTextWriterWriteString(writer, BAD_CAST label->name);
298 xmlTextWriterEndElement(writer);
301 for (author_x* author :
302 GListRange<author_x*>(context_->manifest_data.get()->author)) {
303 xmlTextWriterStartElement(writer, BAD_CAST "author");
304 if (author->email && strlen(author->email)) {
305 xmlTextWriterWriteAttribute(writer, BAD_CAST "email",
306 BAD_CAST author->email);
308 if (author->href && strlen(author->href)) {
309 xmlTextWriterWriteAttribute(writer, BAD_CAST "href",
310 BAD_CAST author->href);
312 xmlTextWriterWriteString(writer, BAD_CAST author->text);
313 xmlTextWriterEndElement(writer);
316 for (description_x* description :
317 GListRange<description_x*>(context_->manifest_data.get()->description)) {
318 xmlTextWriterStartElement(writer, BAD_CAST "description");
319 if (description->lang && strcmp(DEFAULT_LOCALE, description->lang) != 0) {
320 xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
321 BAD_CAST description->lang);
323 xmlTextWriterWriteString(writer, BAD_CAST description->text);
324 xmlTextWriterEndElement(writer);
328 for (application_x* app :
329 GListRange<application_x*>(context_->manifest_data.get()->application)) {
331 if (strcmp(app->component_type, "uiapp") == 0) {
332 type = AppCompType::UIAPP;
333 xmlTextWriterStartElement(writer, BAD_CAST "ui-application");
334 } else if (strcmp(app->component_type, "svcapp") == 0) {
335 type = AppCompType::SVCAPP;
336 xmlTextWriterStartElement(writer, BAD_CAST "service-application");
337 } else if (strcmp(app->component_type, "widgetapp") == 0) {
338 type = AppCompType::WIDGETAPP;
339 xmlTextWriterStartElement(writer, BAD_CAST "widget-application");
341 LOG(ERROR) << "Unknown application component_type";
342 xmlFreeTextWriter(writer);
343 return Status::ERROR;
345 GenerateApplicationCommonXml(app, writer, type);
346 xmlTextWriterEndElement(writer);
349 // add privilege element
350 if (context_->manifest_data.get()->privileges) {
351 xmlTextWriterStartElement(writer, BAD_CAST "privileges");
352 for (const char* priv :
353 GListRange<char*>(context_->manifest_data.get()->privileges)) {
354 xmlTextWriterWriteFormatElement(writer, BAD_CAST "privilege",
355 "%s", BAD_CAST priv);
357 xmlTextWriterEndElement(writer);
360 const auto& accounts =
361 context_->manifest_plugins_data.get().account_info.get().accounts();
362 if (!accounts.empty()) {
363 xmlTextWriterStartElement(writer, BAD_CAST "account");
365 for (auto& account : accounts) {
366 xmlTextWriterStartElement(writer, BAD_CAST "account-provider");
368 xmlTextWriterWriteAttribute(writer, BAD_CAST "appid",
369 BAD_CAST account.appid.c_str());
371 if (!account.providerid.empty())
372 xmlTextWriterWriteAttribute(writer, BAD_CAST "providerid",
373 BAD_CAST account.providerid.c_str());
375 if (account.multiple_account_support)
376 xmlTextWriterWriteAttribute(writer,
377 BAD_CAST "multiple-accounts-support",
379 for (auto& icon_pair : account.icon_paths) {
380 xmlTextWriterStartElement(writer, BAD_CAST "icon");
381 if (icon_pair.first == "AccountSmall")
382 xmlTextWriterWriteAttribute(writer, BAD_CAST "section",
383 BAD_CAST "account-small");
385 xmlTextWriterWriteAttribute(writer, BAD_CAST "section",
387 xmlTextWriterWriteString(writer, BAD_CAST icon_pair.second.c_str());
388 xmlTextWriterEndElement(writer);
391 for (auto& name_pair : account.names) {
392 xmlTextWriterStartElement(writer, BAD_CAST "label");
393 if (!name_pair.second.empty())
394 xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
395 BAD_CAST name_pair.second.c_str());
396 xmlTextWriterWriteString(writer, BAD_CAST name_pair.first.c_str());
397 xmlTextWriterEndElement(writer);
400 for (auto& capability : account.capabilities) {
401 xmlTextWriterWriteFormatElement(writer, BAD_CAST "capability",
402 "%s", BAD_CAST capability.c_str());
405 xmlTextWriterEndElement(writer);
407 xmlTextWriterEndElement(writer);
410 for (const char* profile :
411 GListRange<char*>(context_->manifest_data.get()->deviceprofile)) {
412 xmlTextWriterStartElement(writer, BAD_CAST "profile");
413 xmlTextWriterWriteAttribute(writer, BAD_CAST "name",
415 xmlTextWriterEndElement(writer);
418 const auto& shortcuts =
419 context_->manifest_plugins_data.get().shortcut_info.get();
420 if (!shortcuts.empty()) {
421 xmlTextWriterStartElement(writer, BAD_CAST "shortcut-list");
422 for (auto& shortcut : shortcuts) {
423 xmlTextWriterStartElement(writer, BAD_CAST "shortcut");
424 if (!shortcut.app_id.empty())
425 xmlTextWriterWriteAttribute(writer, BAD_CAST "appid",
426 BAD_CAST shortcut.app_id.c_str());
427 if (!shortcut.app_id.empty())
428 xmlTextWriterWriteAttribute(writer, BAD_CAST "extra_data",
429 BAD_CAST shortcut.extra_data.c_str());
430 if (!shortcut.app_id.empty())
431 xmlTextWriterWriteAttribute(writer, BAD_CAST "extra_key",
432 BAD_CAST shortcut.extra_key.c_str());
433 if (!shortcut.icon.empty()) {
434 xmlTextWriterStartElement(writer, BAD_CAST "icon");
435 xmlTextWriterWriteString(writer, BAD_CAST shortcut.icon.c_str());
436 xmlTextWriterEndElement(writer);
438 for (auto& label : shortcut.labels) {
439 xmlTextWriterStartElement(writer, BAD_CAST "label");
440 if (!label.first.empty())
441 xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
442 BAD_CAST label.first.c_str());
443 xmlTextWriterWriteString(writer, BAD_CAST label.second.c_str());
444 xmlTextWriterEndElement(writer);
446 xmlTextWriterEndElement(writer);
448 xmlTextWriterEndElement(writer);
451 xmlTextWriterEndElement(writer);
453 xmlTextWriterEndDocument(writer);
454 xmlFreeTextWriter(writer);
456 if (pkgmgr_parser_check_manifest_validation(
457 context_->xml_path.get().c_str()) != 0) {
458 LOG(ERROR) << "Manifest is not valid";
459 return Step::Status::MANIFEST_ERROR;
462 LOG(DEBUG) << "Successfully create manifest xml "
463 << context_->xml_path.get();
467 common_installer::Step::Status StepGenerateXml::undo() {
468 bs::error_code error;
469 if (bf::exists(context_->xml_path.get()))
470 bf::remove_all(context_->xml_path.get(), error);
474 } // namespace pkgmgr