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