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