remove not-used attribute (name of icon, name of image)
[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 = bf::path(getUserManifestPath(context_->uid.get()))
249       / bf::path(context_->pkgid.get());
250   xml_path += ".xml";
251   context_->xml_path.set(xml_path.string());
252
253   bs::error_code error;
254   if (!bf::exists(xml_path.parent_path(), error)) {
255     if (!common_installer::CreateDir(xml_path.parent_path())) {
256       LOG(ERROR) <<
257           "Directory for manifest xml is missing and cannot be created";
258       return Status::MANIFEST_ERROR;
259     }
260   }
261
262   xmlTextWriterPtr writer;
263
264   writer = xmlNewTextWriterFilename(context_->xml_path.get().c_str(), 0);
265   if (!writer) {
266     LOG(ERROR) << "Failed to create new file";
267     return Step::Status::MANIFEST_ERROR;
268   }
269
270   xmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
271
272   xmlTextWriterSetIndent(writer, 1);
273
274   // add manifest Element
275   xmlTextWriterStartElement(writer, BAD_CAST "manifest");
276
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);
289
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);
296     }
297     xmlTextWriterWriteString(writer, BAD_CAST label->name);
298     xmlTextWriterEndElement(writer);
299   }
300
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);
307     }
308     if (author->href && strlen(author->href)) {
309       xmlTextWriterWriteAttribute(writer, BAD_CAST "href",
310                                   BAD_CAST author->href);
311     }
312     xmlTextWriterWriteString(writer, BAD_CAST author->text);
313     xmlTextWriterEndElement(writer);
314   }
315
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);
322     }
323     xmlTextWriterWriteString(writer, BAD_CAST description->text);
324     xmlTextWriterEndElement(writer);
325   }
326
327   // add application
328   for (application_x* app :
329        GListRange<application_x*>(context_->manifest_data.get()->application)) {
330     AppCompType type;
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");
340     } else {
341       LOG(ERROR) << "Unknown application component_type";
342       xmlFreeTextWriter(writer);
343       return Status::ERROR;
344     }
345     GenerateApplicationCommonXml(app, writer, type);
346     xmlTextWriterEndElement(writer);
347   }
348
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);
356     }
357     xmlTextWriterEndElement(writer);
358   }
359
360   const auto& accounts =
361       context_->manifest_plugins_data.get().account_info.get().accounts();
362   if (!accounts.empty()) {
363     xmlTextWriterStartElement(writer, BAD_CAST "account");
364     // add account info
365     for (auto& account : accounts) {
366       xmlTextWriterStartElement(writer, BAD_CAST "account-provider");
367
368       xmlTextWriterWriteAttribute(writer, BAD_CAST "appid",
369                                   BAD_CAST account.appid.c_str());
370
371       if (!account.providerid.empty())
372         xmlTextWriterWriteAttribute(writer, BAD_CAST "providerid",
373                                     BAD_CAST account.providerid.c_str());
374
375       if (account.multiple_account_support)
376         xmlTextWriterWriteAttribute(writer,
377                                     BAD_CAST "multiple-accounts-support",
378                                     BAD_CAST "true");
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");
384         else
385           xmlTextWriterWriteAttribute(writer, BAD_CAST "section",
386                                       BAD_CAST "account");
387         xmlTextWriterWriteString(writer, BAD_CAST icon_pair.second.c_str());
388         xmlTextWriterEndElement(writer);
389       }
390
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);
398       }
399
400       for (auto& capability : account.capabilities) {
401         xmlTextWriterWriteFormatElement(writer, BAD_CAST "capability",
402           "%s", BAD_CAST capability.c_str());
403       }
404
405       xmlTextWriterEndElement(writer);
406     }
407     xmlTextWriterEndElement(writer);
408   }
409
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",
414                                 BAD_CAST profile);
415     xmlTextWriterEndElement(writer);
416   }
417
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);
437       }
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);
445       }
446       xmlTextWriterEndElement(writer);
447     }
448     xmlTextWriterEndElement(writer);
449   }
450
451   xmlTextWriterEndElement(writer);
452
453   xmlTextWriterEndDocument(writer);
454   xmlFreeTextWriter(writer);
455
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;
460   }
461
462   LOG(DEBUG) << "Successfully create manifest xml "
463       << context_->xml_path.get();
464   return Status::OK;
465 }
466
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);
471   return Status::OK;
472 }
473
474 }  // namespace pkgmgr
475 }  // namespace wgt