[Release] wrt-installer_0.1.16
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_manifest_file.cpp
1 /*
2  * Copyright (c) 2011 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    task_manifest_file.cpp
18  * @author  Pawel Sikorski (p.sikorski@samgsung.com)
19  * @version
20  * @brief
21  */
22
23 //SYSTEM INCLUDES
24 #include <unistd.h>
25 #include <string>
26 #include <dpl/assert.h>
27 #include <dirent.h>
28 #include <fstream>
29 #include <ail.h>
30
31 //WRT INCLUDES
32 #include <widget_install/task_manifest_file.h>
33 #include <widget_install/job_widget_install.h>
34 #include <widget_install/widget_install_errors.h>
35 #include <widget_install/widget_install_context.h>
36 #include <dpl/wrt-dao-ro/global_config.h>
37 #include <dpl/log/log.h>
38 #include <dpl/file_input.h>
39 #include <dpl/errno_string.h>
40 #include <dpl/file_output.h>
41 #include <dpl/copy.h>
42 #include <dpl/exception.h>
43 #include <dpl/foreach.h>
44 #include <dpl/sstream.h>
45 #include <dpl/string.h>
46 #include <dpl/optional.h>
47 #include <dpl/utils/wrt_utility.h>
48 #include <map>
49 #include <libxml_utils.h>
50 #include <pkgmgr/pkgmgr_parser.h>
51 #include <dpl/localization/LanguageTagsProvider.h>
52
53 #define DEFAULT_ICON_NAME   "icon.png"
54
55 using namespace WrtDB;
56
57 namespace {
58 typedef std::map<DPL::String, DPL::String> LanguageTagMap;
59
60 const char* const ST_TRUE = "true";
61 const char* const ST_NODISPLAY = "nodisplay";
62
63 LanguageTagMap getLanguageTagMap()
64 {
65     LanguageTagMap map;
66
67 #define ADD(tag, l_tag) map.insert(std::make_pair(L###tag, L###l_tag));
68 #include "languages.def"
69 #undef ADD
70
71     return map;
72 }
73
74 DPL::OptionalString getLangTag(const DPL::String& tag)
75 {
76     static LanguageTagMap TagsMap =
77         getLanguageTagMap();
78
79     DPL::String langTag = tag;
80
81     LogDebug("Trying to map language tag: " << langTag);
82     size_t pos = langTag.find_first_of(L'_');
83     if (pos != DPL::String::npos) {
84         langTag.erase(pos);
85     }
86     DPL::OptionalString ret;
87
88     LanguageTagMap::iterator it = TagsMap.find(langTag);
89     if (it != TagsMap.end()) {
90         ret = it->second;
91     }
92     LogDebug("Mapping IANA Language tag to language tag: " <<
93              langTag << " -> " << ret);
94
95     return ret;
96 }
97 }
98
99 namespace Jobs {
100 namespace WidgetInstall {
101 const char * TaskManifestFile::encoding = "UTF-8";
102
103 TaskManifestFile::TaskManifestFile(InstallerContext &inCont) :
104     DPL::TaskDecl<TaskManifestFile>(this),
105     m_context(inCont),
106     writer(NULL)
107 {
108     if (false == m_context.existingWidgetInfo.isExist) {
109         AddStep(&TaskManifestFile::stepCopyIconFiles);
110         AddStep(&TaskManifestFile::stepCreateExecFile);
111         AddStep(&TaskManifestFile::stepGenerateManifest);
112         AddStep(&TaskManifestFile::stepParseManifest);
113         AddStep(&TaskManifestFile::stepFinalize);
114
115         AddAbortStep(&TaskManifestFile::stepAbortParseManifest);
116     } else {
117         // for widget update.
118         AddStep(&TaskManifestFile::stepBackupIconFiles);
119         AddStep(&TaskManifestFile::stepCopyIconFiles);
120         AddStep(&TaskManifestFile::stepGenerateManifest);
121         AddStep(&TaskManifestFile::stepParseUpgradedManifest);
122         AddStep(&TaskManifestFile::stepUpdateFinalize);
123
124         AddAbortStep(&TaskManifestFile::stepAbortIconFiles);
125     }
126 }
127
128 TaskManifestFile::~TaskManifestFile()
129 {}
130
131 void TaskManifestFile::stepCreateExecFile()
132 {
133     std::string exec = m_context.locations->getExecFile();
134     std::string clientExeStr = GlobalConfig::GetWrtClientExec();
135
136     LogInfo("link -s " << clientExeStr << " " << exec);
137
138     errno = 0;
139     if (symlink(clientExeStr.c_str(), exec.c_str()) !=  0)
140     {
141         int error = errno;
142         if(error)
143             LogPedantic("Failed to make a symbolic name for a file "
144                     << "[" <<  DPL::GetErrnoString(error) << "]");
145         ThrowMsg(Exceptions::InstallationFailed,
146                 "Symbolic link creating is not done.");
147     }
148     m_context.job->UpdateProgress(
149             InstallerContext::INSTALL_CREATE_EXECFILE,
150             "Widget execfile creation Finished");
151 }
152
153 void TaskManifestFile::stepCopyIconFiles()
154 {
155     LogDebug("CopyIconFiles");
156
157     //This function copies icon to desktop icon path. For each locale avaliable
158     //which there is at least one icon in widget for, icon file is copied.
159     //Coping prioritize last positions when coping. If there is several icons
160     //with given locale, the one, that will be copied, will be icon
161     //which is declared by <icon> tag later than the others in config.xml of
162     // widget
163
164     std::vector<Locale> generatedLocales;
165
166     WrtDB::WidgetRegisterInfo::LocalizedIconList & icons =
167         m_context.widgetConfig.localizationData.icons;
168
169     //reversed: last <icon> has highest priority to be copied if it has given
170     // locale (TODO: why was that working that way?)
171     for (WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator
172          icon = icons.rbegin();
173          icon != icons.rend();
174          ++icon)
175     {
176         FOREACH(locale, icon->availableLocales)
177         {
178             DPL::String src = icon->src;
179             LogDebug("Icon for locale: " << *locale << "is : " << src);
180
181             if (std::find(generatedLocales.begin(), generatedLocales.end(),
182                           *locale) != generatedLocales.end())
183             {
184                 LogDebug("Skipping - has that locale");
185                 continue;
186             } else {
187                 generatedLocales.push_back(*locale);
188             }
189
190             std::ostringstream sourceFile;
191             std::ostringstream targetFile;
192
193             sourceFile << m_context.locations->getSourceDir() << "/";
194
195             if (!locale->empty()) {
196                 sourceFile << "locales/" << *locale << "/";
197             }
198
199             sourceFile << src;
200
201             targetFile << GlobalConfig::GetUserWidgetDesktopIconPath() << "/";
202             targetFile << getIconTargetFilename(*locale);
203
204             if (m_context.widgetConfig.packagingType ==
205                 WrtDB::PKG_TYPE_HOSTED_WEB_APP)
206             {
207                 m_context.locations->setIconTargetFilenameForLocale(
208                     targetFile.str());
209             }
210
211             LogDebug("Copying icon: " << sourceFile.str() <<
212                      " -> " << targetFile.str());
213
214             icon_list.push_back(targetFile.str());
215
216             Try
217             {
218                 DPL::FileInput input(sourceFile.str());
219                 DPL::FileOutput output(targetFile.str());
220                 DPL::Copy(&input, &output);
221             }
222
223             Catch(DPL::FileInput::Exception::Base)
224             {
225                 // Error while opening or closing source file
226                 //ReThrowMsg(InstallerException::CopyIconFailed,
227                 // sourceFile.str());
228                 LogError(
229                     "Copying widget's icon failed. Widget's icon will not be" \
230                     "available from Main Screen");
231             }
232
233             Catch(DPL::FileOutput::Exception::Base)
234             {
235                 // Error while opening or closing target file
236                 //ReThrowMsg(InstallerException::CopyIconFailed,
237                 // targetFile.str());
238                 LogError(
239                     "Copying widget's icon failed. Widget's icon will not be" \
240                     "available from Main Screen");
241             }
242
243             Catch(DPL::CopyFailed)
244             {
245                 // Error while copying
246                 //ReThrowMsg(InstallerException::CopyIconFailed,
247                 // targetFile.str());
248                 LogError(
249                     "Copying widget's icon failed. Widget's icon will not be" \
250                     "available from Main Screen");
251             }
252         }
253     }
254
255     m_context.job->UpdateProgress(
256         InstallerContext::INSTALL_COPY_ICONFILE,
257         "Widget iconfile copy Finished");
258 }
259
260 void TaskManifestFile::stepBackupIconFiles()
261 {
262     LogDebug("Backup Icon Files");
263
264     backup_dir << m_context.locations->getPackageInstallationDir();
265     backup_dir << "/" << "backup" << "/";
266
267     backupIconFiles();
268
269     m_context.job->UpdateProgress(
270         InstallerContext::INSTALL_BACKUP_ICONFILE,
271         "New Widget icon file backup Finished");
272 }
273
274 void TaskManifestFile::stepAbortIconFiles()
275 {
276     LogDebug("Abrot Icon Files");
277     FOREACH(it, icon_list)
278     {
279         LogDebug("Remove Update Icon : " << (*it));
280         unlink((*it).c_str());
281     }
282
283     std::ostringstream b_icon_dir;
284     b_icon_dir << backup_dir.str() << "icons";
285
286     std::list<std::string> fileList;
287     getFileList(b_icon_dir.str().c_str(), fileList);
288
289     FOREACH(back_icon, fileList)
290     {
291         std::ostringstream res_file;
292         res_file << GlobalConfig::GetUserWidgetDesktopIconPath();
293         res_file << "/" << (*back_icon);
294
295         std::ostringstream backup_file;
296         backup_file << b_icon_dir.str() << "/" << (*back_icon);
297
298         Try
299         {
300             DPL::FileInput input(backup_file.str());
301             DPL::FileOutput output(res_file.str());
302             DPL::Copy(&input, &output);
303         }
304         Catch(DPL::FileInput::Exception::Base)
305         {
306             LogError("Restoration icon File Failed." << backup_file.str()
307                                                      << " to " << res_file.str());
308         }
309
310         Catch(DPL::FileOutput::Exception::Base)
311         {
312             LogError("Restoration icon File Failed." << backup_file.str()
313                                                      << " to " << res_file.str());
314         }
315         Catch(DPL::CopyFailed)
316         {
317             LogError("Restoration icon File Failed." << backup_file.str()
318                                                      << " to " << res_file.str());
319         }
320     }
321 }
322
323 void TaskManifestFile::stepUpdateFinalize()
324 {
325     commitManifest();
326     LogDebug("Finished Update Desktopfile");
327 }
328
329 DPL::String TaskManifestFile::getIconTargetFilename(
330     const DPL::String& languageTag) const
331 {
332     DPL::OStringStream filename;
333     TizenAppId appid = m_context.widgetConfig.tzAppid;
334
335     filename << DPL::ToUTF8String(appid).c_str();
336
337     if (!languageTag.empty()) {
338         DPL::OptionalString tag = getLangTag(languageTag); // translate en ->
339                                                            // en_US etc
340         if (tag.IsNull()) {
341             tag = languageTag;
342         }
343         DPL::String locale =
344             LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
345
346         if (locale.empty()) {
347             filename << L"." << languageTag;
348         } else {
349             filename << L"." << locale;
350         }
351     }
352
353     filename << L".png";
354     return filename.str();
355 }
356
357 void TaskManifestFile::stepFinalize()
358 {
359     commitManifest();
360     LogInfo("Finished ManifestFile step");
361 }
362
363 void TaskManifestFile::saveLocalizedKey(std::ofstream &file,
364                                         const DPL::String& key,
365                                         const DPL::String& languageTag)
366 {
367     DPL::String locale =
368         LanguageTagsProvider::BCP47LanguageTagToLocale(languageTag);
369
370     file << key;
371     if (!locale.empty()) {
372         file << "[" << locale << "]";
373     }
374     file << "=";
375 }
376
377 void TaskManifestFile::updateAilInfo()
378 {
379     // Update ail for desktop
380     std::string cfgAppid =
381         DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
382     const char* appid = cfgAppid.c_str();
383
384     LogDebug("Update ail desktop : " << appid);
385     ail_appinfo_h ai = NULL;
386     ail_error_e ret;
387
388     ret = ail_get_appinfo(appid, &ai);
389     if (ai) {
390         ail_destroy_appinfo(ai);
391     }
392
393     if (AIL_ERROR_NO_DATA == ret) {
394         if (ail_desktop_add(appid) < 0) {
395             LogWarning("Failed to add ail desktop : " << appid);
396         }
397     } else if (AIL_ERROR_OK == ret) {
398         if (ail_desktop_update(appid) < 0) {
399             LogWarning("Failed to update ail desktop : " << appid);
400         }
401     }
402 }
403
404 void TaskManifestFile::backupIconFiles()
405 {
406     LogInfo("Backup Icon Files");
407
408     std::ostringstream b_icon_dir;
409     b_icon_dir << backup_dir.str() << "icons";
410
411     LogDebug("Create icon backup folder : " << b_icon_dir.str());
412     WrtUtilMakeDir(b_icon_dir.str());
413
414     std::list<std::string> fileList;
415     getFileList(GlobalConfig::GetUserWidgetDesktopIconPath(), fileList);
416     std::string appid = DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
417
418     FOREACH(it, fileList)
419     {
420         if (0 == (strncmp((*it).c_str(), appid.c_str(),
421                           strlen(appid.c_str()))))
422         {
423             std::ostringstream icon_file, backup_icon;
424             icon_file << GlobalConfig::GetUserWidgetDesktopIconPath();
425             icon_file << "/" << (*it);
426
427             backup_icon << b_icon_dir.str() << "/" << (*it);
428
429             LogDebug("Backup icon file " << icon_file.str() << " to " <<
430                      backup_icon.str());
431             Try
432             {
433                 DPL::FileInput input(icon_file.str());
434                 DPL::FileOutput output(backup_icon.str());
435                 DPL::Copy(&input, &output);
436             }
437             Catch(DPL::FileInput::Exception::Base)
438             {
439                 LogError("Backup Desktop File Failed.");
440                 ReThrowMsg(Exceptions::BackupFailed, icon_file.str());
441             }
442
443             Catch(DPL::FileOutput::Exception::Base)
444             {
445                 LogError("Backup Desktop File Failed.");
446                 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
447             }
448             Catch(DPL::CopyFailed)
449             {
450                 LogError("Backup Desktop File Failed.");
451                 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
452             }
453             unlink((*it).c_str());
454         }
455     }
456 }
457
458 void TaskManifestFile::getFileList(const char* path,
459                                    std::list<std::string> &list)
460 {
461     DIR* dir = opendir(path);
462     if (!dir) {
463         LogError("icon directory doesn't exist");
464         ThrowMsg(Exceptions::InternalError, path);
465     }
466
467     struct dirent entry;
468     struct dirent *result;
469     int return_code;
470     errno = 0;
471     for (return_code = readdir_r(dir, &entry, &result);
472             result != NULL && return_code == 0;
473             return_code = readdir_r(dir, &entry, &result))
474     {
475         if (strcmp(entry.d_name, ".") == 0 ||
476             strcmp(entry.d_name, "..") == 0)
477         {
478             continue;
479         }
480         std::string file_name = entry.d_name;
481         list.push_back(file_name);
482     }
483
484     if (return_code != 0 || errno != 0) {
485         LogError("readdir_r() failed with " << DPL::GetErrnoString());
486     }
487
488     if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
489         LogError("Failed to close dir: " << path << " with error: "
490                                          << DPL::GetErrnoString());
491     }
492 }
493
494 void TaskManifestFile::stepGenerateManifest()
495 {
496     TizenPkgId pkgid = m_context.widgetConfig.tzPkgid;
497     manifest_name = pkgid + L".xml";
498     manifest_file += L"/tmp/" + manifest_name;
499
500     //libxml - init and check
501     LibxmlSingleton::Instance().init();
502
503     writeManifest(manifest_file);
504
505     m_context.job->UpdateProgress(
506         InstallerContext::INSTALL_CREATE_MANIFEST,
507         "Widget Manifest Creation Finished");
508 }
509
510 void TaskManifestFile::stepParseManifest()
511 {
512     int code = pkgmgr_parser_parse_manifest_for_installation(
513             DPL::ToUTF8String(manifest_file).c_str(), NULL);
514
515     if (code != 0) {
516         LogError("Manifest parser error: " << code);
517         ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
518     }
519
520     // TODO : It will be removed. AIL update is temporary code request by pkgmgr
521     // team.
522     updateAilInfo();
523
524     m_context.job->UpdateProgress(
525         InstallerContext::INSTALL_CREATE_MANIFEST,
526         "Widget Manifest Parsing Finished");
527     LogDebug("Manifest parsed");
528 }
529
530 void TaskManifestFile::stepParseUpgradedManifest()
531 {
532     int code = pkgmgr_parser_parse_manifest_for_upgrade(
533             DPL::ToUTF8String(manifest_file).c_str(), NULL);
534
535     if (code != 0) {
536         LogError("Manifest parser error: " << code);
537         ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
538     }
539
540     // TODO : It will be removed. AIL update is temporary code request by pkgmgr
541     // team.
542     updateAilInfo();
543
544     m_context.job->UpdateProgress(
545         InstallerContext::INSTALL_CREATE_MANIFEST,
546         "Widget Manifest Parsing Finished");
547     LogDebug("Manifest parsed");
548 }
549
550 void TaskManifestFile::commitManifest()
551 {
552     LogDebug("Commiting manifest file : " << manifest_file);
553
554     std::ostringstream destFile;
555     destFile << "/opt/share/packages" << "/"; //TODO constant with path
556     destFile << DPL::ToUTF8String(manifest_name);
557     LogInfo("cp " << manifest_file << " " << destFile.str());
558
559     DPL::FileInput input(DPL::ToUTF8String(manifest_file));
560     DPL::FileOutput output(destFile.str());
561     DPL::Copy(&input, &output);
562     LogDebug("Manifest writen to: " << destFile.str());
563
564     //removing temp file
565     unlink((DPL::ToUTF8String(manifest_file)).c_str());
566     manifest_file = DPL::FromUTF8String(destFile.str().c_str());
567 }
568
569 void TaskManifestFile::writeManifest(const DPL::String & path)
570 {
571     LogDebug("Generating manifest file : " << path);
572     Manifest manifest;
573     UiApplication uiApp;
574
575     setWidgetExecPath(uiApp);
576     setWidgetName(manifest, uiApp);
577     setWidgetIcons(uiApp);
578     setWidgetManifest(manifest);
579     setWidgetOtherInfo(uiApp);
580     setAppServiceInfo(uiApp);
581     setAppControlInfo(uiApp);
582     setAppCategory(uiApp);
583     setLiveBoxInfo(manifest);
584     setAccount(manifest);
585
586     manifest.addUiApplication(uiApp);
587     manifest.generate(path);
588     LogDebug("Manifest file serialized");
589 }
590
591 void TaskManifestFile::setWidgetExecPath(UiApplication & uiApp)
592 {
593     uiApp.setExec(DPL::FromASCIIString(m_context.locations->getExecFile()));
594 }
595
596 void TaskManifestFile::setWidgetName(Manifest & manifest, UiApplication & uiApp)
597 {
598     bool defaultNameSaved = false;
599
600     DPL::OptionalString defaultLocale =
601         m_context.widgetConfig.configInfo.defaultlocale;
602     std::pair<DPL::String,
603               WrtDB::ConfigParserData::LocalizedData> defaultLocalizedData;
604     //labels
605     FOREACH(localizedData, m_context.widgetConfig.configInfo.localizedDataSet)
606     {
607         Locale i = localizedData->first;
608         DPL::OptionalString tag = getLangTag(i); // translate en -> en_US etc
609         if (tag.IsNull()) {
610             tag = i;
611         }
612         DPL::OptionalString name = localizedData->second.name;
613         generateWidgetName(manifest, uiApp, tag, name, defaultNameSaved);
614
615         //store default locale localized data
616         if (!!defaultLocale && defaultLocale == i) {
617             defaultLocalizedData = *localizedData;
618         }
619     }
620
621     if (!!defaultLocale && !defaultNameSaved) {
622         DPL::OptionalString name = defaultLocalizedData.second.name;
623         generateWidgetName(manifest,
624                            uiApp,
625                            DPL::OptionalString::Null,
626                            name,
627                            defaultNameSaved);
628     }
629     //appid
630     TizenAppId appid = m_context.widgetConfig.tzAppid;
631     uiApp.setAppid(appid);
632
633     //extraid
634     if (!!m_context.widgetConfig.guid) {
635         uiApp.setExtraid(*m_context.widgetConfig.guid);
636     } else {
637         if (!appid.empty()) {
638             uiApp.setExtraid(DPL::String(L"http://") + appid);
639         }
640     }
641
642     //type
643     uiApp.setType(DPL::FromASCIIString("webapp"));
644     manifest.setType(L"wgt");
645 }
646
647 void TaskManifestFile::generateWidgetName(Manifest & manifest,
648                                           UiApplication &uiApp,
649                                           const DPL::OptionalString& tag,
650                                           DPL::OptionalString name,
651                                           bool & defaultNameSaved)
652 {
653     if (!!name) {
654         if (!!tag) {
655             DPL::String locale =
656                 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
657
658             if (!locale.empty()) {
659                 uiApp.addLabel(LabelType(*name, *tag));
660             } else {
661                 uiApp.addLabel(LabelType(*name));
662                 manifest.addLabel(LabelType(*name));
663             }
664         } else {
665             defaultNameSaved = true;
666             uiApp.addLabel(LabelType(*name));
667             manifest.addLabel(LabelType(*name));
668         }
669     }
670 }
671
672 void TaskManifestFile::setWidgetIcons(UiApplication & uiApp)
673 {
674     //TODO this file will need to be updated when user locale preferences
675     //changes.
676     bool defaultIconSaved = false;
677
678     DPL::OptionalString defaultLocale =
679         m_context.widgetConfig.configInfo.defaultlocale;
680
681     std::vector<Locale> generatedLocales;
682     WrtDB::WidgetRegisterInfo::LocalizedIconList & icons =
683         m_context.widgetConfig.localizationData.icons;
684
685     //reversed: last <icon> has highest priority to be writen to manifest if it
686     // has given locale (TODO: why was that working that way?)
687     for (WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator
688          icon = icons.rbegin();
689          icon != icons.rend();
690          ++icon)
691     {
692         FOREACH(locale, icon->availableLocales)
693         {
694             if (std::find(generatedLocales.begin(), generatedLocales.end(),
695                           *locale) != generatedLocales.end())
696             {
697                 LogDebug("Skipping - has that locale - already in manifest");
698                 continue;
699             } else {
700                 generatedLocales.push_back(*locale);
701             }
702
703             DPL::OptionalString tag = getLangTag(*locale); // translate en ->
704                                                            // en_US etc
705             if (tag.IsNull()) {
706                 tag = *locale;
707             }
708
709             generateWidgetIcon(uiApp, tag, *locale, defaultIconSaved);
710         }
711     }
712     if (!!defaultLocale && !defaultIconSaved) {
713         generateWidgetIcon(uiApp, DPL::OptionalString::Null,
714                            DPL::String(),
715                            defaultIconSaved);
716     }
717 }
718
719 void TaskManifestFile::generateWidgetIcon(UiApplication & uiApp,
720                                           const DPL::OptionalString& tag,
721                                           const DPL::String& language,
722                                           bool & defaultIconSaved)
723 {
724     DPL::String locale;
725     if (!!tag) {
726         locale = LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
727     } else {
728         defaultIconSaved = true;
729     }
730
731     DPL::String iconText;
732     iconText += getIconTargetFilename(language);
733
734     if (!locale.empty()) {
735         uiApp.addIcon(IconType(iconText, locale));
736     } else {
737         uiApp.addIcon(IconType(iconText));
738     }
739     std::ostringstream iconPath;
740     iconPath << GlobalConfig::GetUserWidgetDesktopIconPath() << "/";
741     iconPath << getIconTargetFilename(locale);
742      m_context.job->SendProgressIconPath(iconPath.str());
743 }
744
745 void TaskManifestFile::setWidgetManifest(Manifest & manifest)
746 {
747     manifest.setPackage(m_context.widgetConfig.tzPkgid);
748
749     if (!!m_context.widgetConfig.version) {
750         manifest.setVersion(*m_context.widgetConfig.version);
751     }
752     DPL::String email = (!!m_context.widgetConfig.configInfo.authorEmail ?
753                          *m_context.widgetConfig.configInfo.authorEmail : L"");
754     DPL::String href = (!!m_context.widgetConfig.configInfo.authorHref ?
755                         *m_context.widgetConfig.configInfo.authorHref : L"");
756     DPL::String name = (!!m_context.widgetConfig.configInfo.authorName ?
757                         *m_context.widgetConfig.configInfo.authorName : L"");
758     manifest.addAuthor(Author(email, href, L"", name));
759 }
760
761 void TaskManifestFile::setWidgetOtherInfo(UiApplication & uiApp)
762 {
763     FOREACH(it, m_context.widgetConfig.configInfo.settingsList)
764     {
765         if (!strcmp(DPL::ToUTF8String(it->m_name).c_str(), ST_NODISPLAY)) {
766             if (!strcmp(DPL::ToUTF8String(it->m_value).c_str(), ST_TRUE)) {
767                 uiApp.setNodisplay(true);
768                 uiApp.setTaskmanage(false);
769             } else {
770                 uiApp.setNodisplay(false);
771                 uiApp.setTaskmanage(true);
772             }
773         }
774     }
775     //TODO
776     //There is no "X-TIZEN-PackageType=wgt"
777     //There is no X-TIZEN-PackageID in manifest "X-TIZEN-PackageID=" <<
778     // DPL::ToUTF8String(*widgetID).c_str()
779     //There is no Comment in pkgmgr "Comment=Widget application"
780     //that were in desktop file
781 }
782
783 void TaskManifestFile::setAppServiceInfo(UiApplication & uiApp)
784 {
785     WrtDB::ConfigParserData::ServiceInfoList appServiceList =
786         m_context.widgetConfig.configInfo.appServiceList;
787
788     if (appServiceList.empty()) {
789         LogInfo("Widget doesn't contain application service");
790         return;
791     }
792
793     // x-tizen-svc=http://tizen.org/appcontrol/operation/pick|NULL|image;
794     FOREACH(it, appServiceList) {
795         AppControl appControl;
796         if (!it->m_operation.empty()) {
797             appControl.addOperation(it->m_operation); //TODO: encapsulation?
798         }
799         if (!it->m_scheme.empty()) {
800             appControl.addUri(it->m_scheme);
801         }
802         if (!it->m_mime.empty()) {
803             appControl.addMime(it->m_mime);
804         }
805         uiApp.addAppControl(appControl);
806     }
807 }
808
809 void TaskManifestFile::setAppControlInfo(UiApplication & uiApp)
810 {
811     WrtDB::ConfigParserData::AppControlInfoList appControlList =
812         m_context.widgetConfig.configInfo.appControlList;
813
814     if (appControlList.empty()) {
815         LogInfo("Widget doesn't contain app control");
816         return;
817     }
818
819     // x-tizen-svc=http://tizen.org/appcontrol/operation/pick|NULL|image;
820     FOREACH(it, appControlList) {
821         AppControl appControl;
822         if (!it->m_operation.empty()) {
823             appControl.addOperation(it->m_operation); //TODO: encapsulation?
824         }
825         if (!it->m_uriList.empty()) {
826             FOREACH(uri, it->m_uriList) {
827                 appControl.addUri(*uri);
828             }
829         }
830         if (!it->m_mimeList.empty()) {
831             FOREACH(mime, it->m_mimeList) {
832                 appControl.addMime(*mime);
833             }
834         }
835         uiApp.addAppControl(appControl);
836     }
837 }
838
839 void TaskManifestFile::setAppCategory(UiApplication &uiApp)
840 {
841     WrtDB::ConfigParserData::CategoryList categoryList =
842         m_context.widgetConfig.configInfo.categoryList;
843
844     if (categoryList.empty()) {
845         LogInfo("Widget doesn't contain application category");
846         return;
847     }
848     FOREACH(it, categoryList) {
849         if (!(*it).empty()) {
850             uiApp.addAppCategory(*it);
851         }
852     }
853 }
854
855 void TaskManifestFile::stepAbortParseManifest()
856 {
857     LogError("[Parse Manifest] Abroting....");
858
859     int code = pkgmgr_parser_parse_manifest_for_uninstallation(
860             DPL::ToUTF8String(manifest_file).c_str(), NULL);
861
862     if (0 != code) {
863         LogWarning("Manifest parser error: " << code);
864         ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
865     }
866     int ret = unlink(DPL::ToUTF8String(manifest_file).c_str());
867     if (0 != ret) {
868         LogWarning("No manifest file found: " << manifest_file);
869     }
870 }
871
872 void TaskManifestFile::setLiveBoxInfo(Manifest& manifest)
873 {
874     FOREACH(it, m_context.widgetConfig.configInfo.m_livebox) {
875         LogInfo("setLiveBoxInfo");
876         LiveBoxInfo liveBox;
877         DPL::Optional<WrtDB::ConfigParserData::LiveboxInfo> ConfigInfo = *it;
878         DPL::String appid = m_context.widgetConfig.tzAppid;
879
880         if (ConfigInfo->m_liveboxId != L"") {
881             size_t found = ConfigInfo->m_liveboxId.find_last_of(L".");
882             if (found != std::string::npos) {
883                 if (0 == ConfigInfo->m_liveboxId.compare(0, found, appid)) {
884                     liveBox.setLiveboxId(ConfigInfo->m_liveboxId);
885                 } else {
886                     DPL::String liveboxId =
887                         appid + DPL::String(L".") + ConfigInfo->m_liveboxId;
888                     liveBox.setLiveboxId(liveboxId);
889                 }
890             } else {
891                 DPL::String liveboxId =
892                     appid + DPL::String(L".") + ConfigInfo->m_liveboxId;
893                 liveBox.setLiveboxId(liveboxId);
894             }
895         }
896
897         if (ConfigInfo->m_primary != L"") {
898             liveBox.setPrimary(ConfigInfo->m_primary);
899         }
900
901         if (ConfigInfo->m_autoLaunch == L"true") {
902             liveBox.setAutoLaunch(appid);
903         }
904
905         if (ConfigInfo->m_updatePeriod != L"") {
906             liveBox.setUpdatePeriod(ConfigInfo->m_updatePeriod);
907         }
908
909         if (ConfigInfo->m_label != L"") {
910             liveBox.setLabel(ConfigInfo->m_label);
911         }
912
913         DPL::String defaultLocale
914             = DPL::FromUTF8String(
915                     m_context.locations->getPackageInstallationDir())
916                 + DPL::String(L"/res/wgt/");
917
918         if (ConfigInfo->m_icon != L"") {
919             liveBox.setIcon(defaultLocale + ConfigInfo->m_icon);
920         }
921
922         if (ConfigInfo->m_boxInfo.m_boxSrc.empty() ||
923             ConfigInfo->m_boxInfo.m_boxSize.empty())
924         {
925             LogInfo("Widget doesn't contain box");
926             return;
927         } else {
928             BoxInfoType box;
929             if (!ConfigInfo->m_boxInfo.m_boxSrc.empty()) {
930                 if ((0 == ConfigInfo->m_boxInfo.m_boxSrc.compare(0, 4, L"http"))
931                     || (0 ==
932                         ConfigInfo->m_boxInfo.m_boxSrc.compare(0, 5, L"https")))
933                 {
934                     box.boxSrc = ConfigInfo->m_boxInfo.m_boxSrc;
935                 } else {
936                     box.boxSrc = defaultLocale + ConfigInfo->m_boxInfo.m_boxSrc;
937                 }
938             }
939
940             if (ConfigInfo->m_boxInfo.m_boxMouseEvent == L"true") {
941                 box.boxMouseEvent = ConfigInfo->m_boxInfo.m_boxMouseEvent;
942             } else {
943                 box.boxMouseEvent = L"false";
944             }
945
946             if (ConfigInfo->m_boxInfo.m_boxTouchEffect == L"true") {
947                 box.boxTouchEffect = ConfigInfo->m_boxInfo.m_boxTouchEffect;
948             } else {
949                 box.boxTouchEffect= L"false";
950             }
951
952             std::list<std::pair<DPL::String, DPL::String> > BoxSizeList
953                 = ConfigInfo->m_boxInfo.m_boxSize;
954             FOREACH(im, BoxSizeList) {
955                 std::pair<DPL::String, DPL::String> boxSize = *im;
956                 if (!boxSize.second.empty()) {
957                     boxSize.second = defaultLocale + boxSize.second;
958                 }
959                 box.boxSize.push_back(boxSize);
960             }
961
962             if (!ConfigInfo->m_boxInfo.m_pdSrc.empty()
963                 && !ConfigInfo->m_boxInfo.m_pdWidth.empty()
964                 && !ConfigInfo->m_boxInfo.m_pdHeight.empty())
965             {
966                 box.pdSrc = defaultLocale + ConfigInfo->m_boxInfo.m_pdSrc;
967                 box.pdWidth = ConfigInfo->m_boxInfo.m_pdWidth;
968                 box.pdHeight = ConfigInfo->m_boxInfo.m_pdHeight;
969             }
970             liveBox.setBox(box);
971         }
972         manifest.addLivebox(liveBox);
973     }
974 }
975
976 void TaskManifestFile::setAccount(Manifest& manifest)
977 {
978     WrtDB::ConfigParserData::AccountProvider account =
979         m_context.widgetConfig.configInfo.accountProvider;
980
981     AccountProviderType provider;
982
983     if (account.m_iconSet.empty()) {
984         LogInfo("Widget doesn't contain Account");
985         return;
986     }
987     if (account.m_multiAccountSupport) {
988         provider.multiAccount = L"ture";
989     } else {
990         provider.multiAccount = L"false";
991     }
992     provider.appid = m_context.widgetConfig.tzAppid;
993
994     FOREACH(it, account.m_iconSet) {
995         std::pair<DPL::String, DPL::String> icon;
996
997         if (it->first == ConfigParserData::IconSectionType::DefaultIcon) {
998             icon.first = L"account";
999         } else if (it->first == ConfigParserData::IconSectionType::SmallIcon) {
1000             icon.first = L"account-small";
1001         }
1002         icon.second = it->second;
1003
1004         provider.icon.push_back(icon);
1005     }
1006
1007     FOREACH(it, account.m_displayNameSet) {
1008         provider.name.push_back(LabelType(it->second, it->first));
1009     }
1010
1011     FOREACH(it, account.m_capabilityList) {
1012         provider.capability.push_back(*it);
1013     }
1014
1015     Account accountInfo;
1016     accountInfo.addAccountProvider(provider);
1017     manifest.addAccount(accountInfo);
1018 }
1019
1020 } //namespace WidgetInstall
1021 } //namespace Jobs