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