Update wrt-installer_0.0.52
[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 #include <shortcut/shortcut.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/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
51 #define DEFAULT_ICON_NAME   "icon.png"
52
53 using namespace WrtDB;
54
55 namespace {
56 typedef std::map<DPL::String, DPL::String> LanguageTagMap;
57
58 LanguageTagMap getLanguageTagMap()
59 {
60     LanguageTagMap map;
61
62 #define ADD(tag, l_tag) map.insert(std::make_pair(L ## # tag, L ## # l_tag));
63 #include "languages.def"
64 #undef ADD
65
66     return map;
67 }
68
69 DPL::OptionalString getLangTag(const DPL::String& tag)
70 {
71     static LanguageTagMap TagsMap =
72         getLanguageTagMap();
73
74     DPL::String langTag = tag;
75
76     LogDebug("Trying to map language tag: " << langTag);
77     size_t pos = langTag.find_first_of(L'_');
78     if (pos != DPL::String::npos) {
79         langTag.erase(pos);
80     }
81     DPL::OptionalString ret;
82
83     LanguageTagMap::iterator it = TagsMap.find(langTag);
84     if (it != TagsMap.end()) {
85         ret = it->second;
86     }
87     LogDebug("Mapping IANA Language tag to language tag: " <<
88              langTag << " -> " << ret);
89
90     return ret;
91 }
92 }
93
94 namespace Jobs {
95 namespace WidgetInstall {
96
97 const char * TaskManifestFile::encoding = "UTF-8";
98
99 TaskManifestFile::TaskManifestFile(InstallerContext &inCont) :
100     DPL::TaskDecl<TaskManifestFile>(this),
101     m_context(inCont)
102 {
103     if (false == m_context.existingWidgetInfo.isExist) {
104         AddStep(&TaskManifestFile::stepCopyIconFiles);
105         AddStep(&TaskManifestFile::stepCreateExecFile);
106         AddStep(&TaskManifestFile::stepGenerateManifest);
107         AddStep(&TaskManifestFile::stepParseManifest);
108         if (m_context.browserRequest) {
109             AddStep(&TaskManifestFile::stepCreateShortcut);
110         }
111         AddStep(&TaskManifestFile::stepFinalize);
112     } else {
113     // for widget update.
114         AddStep(&TaskManifestFile::stepBackupIconFiles);
115         AddStep(&TaskManifestFile::stepCopyIconFiles);
116         AddStep(&TaskManifestFile::stepGenerateManifest);
117         AddStep(&TaskManifestFile::stepParseUpgradedManifest);
118         AddStep(&TaskManifestFile::stepUpdateFinalize);
119
120         AddAbortStep(&TaskManifestFile::stepAbortIconFiles);
121     }
122 }
123
124 TaskManifestFile::~TaskManifestFile()
125 {
126 }
127
128 void TaskManifestFile::stepCreateExecFile()
129 {
130     //ln -s /usr/bin/wrt-client {widget-handle}
131
132     std::ostringstream real_path;
133     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
134     if (pkgname.IsNull()) {
135         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
136     }
137
138     real_path << GlobalConfig::GetUserInstalledWidgetPath() << "/";
139     real_path << pkgname << "/";
140     real_path << GlobalConfig::GetUserWidgetExecPath() << "/" <<
141     m_context.widgetHandle;
142     std::string clientExeStr = GlobalConfig::GetWrtClientExec();
143
144     LogInfo("link -s " << clientExeStr << " " << real_path.str());
145     symlink(clientExeStr.c_str(), real_path.str().c_str());
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     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
157     if (pkgname.IsNull()) {
158         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
159     }
160     Assert(!!m_context.widgetHandle);
161
162     WidgetDAOReadOnly dao(*m_context.widgetHandle);
163     WidgetDAOReadOnly::WidgetLocalizedIconList locList = dao.getLocalizedIconList();
164     WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
165
166     FOREACH(it, locList)
167     {
168         DPL::String i = it->widgetLocale;
169         DPL::OptionalString src;
170         FOREACH(icon, list)
171         {
172             if (icon->iconId == it->iconId) {
173                 src = icon->iconSrc;
174             }
175         }
176         LogDebug("Icon for locale: " << i << "is : " << src);
177
178         std::ostringstream sourceFile;
179         std::ostringstream targetFile;
180
181         if (!!src) {
182             if (m_context.browserRequest) {
183                 size_t pos = m_context.widgetSource.rfind("/");
184                 sourceFile << m_context.widgetSource.substr(0, pos+1);
185             } else {
186                 sourceFile << GlobalConfig::GetUserInstalledWidgetPath() << "/";
187                 sourceFile << pkgname << "/";
188                 sourceFile << GlobalConfig::GetWidgetSrcPath() << "/";
189             }
190
191             if (!i.empty()) {
192                 sourceFile << "locales/" << i << "/";
193             }
194
195             sourceFile << *src;
196
197             targetFile << GlobalConfig::GetUserWidgetDesktopIconPath() << "/";
198             targetFile << getIconTargetFilename(i);
199
200             if (m_context.browserRequest) {
201                 m_context.installedIconPath = targetFile.str();
202             }
203         } else {
204             //Use WRT default (not from the widget) only if widget default (not
205             // localized) doesn't exist.
206             if (i.empty()) {
207                 LogError("Using Default Icon for widget");
208                 sourceFile << GlobalConfig::GetUserWidgetDefaultIconFile();
209             } else {
210                 continue;
211             }
212         }
213
214         LogDebug("Copying icon: " << sourceFile.str() <<
215                  " -> " << targetFile.str());
216
217         icon_list.push_back(targetFile.str());
218
219         Try
220         {
221             DPL::FileInput input(sourceFile.str());
222             DPL::FileOutput output(targetFile.str());
223             DPL::Copy(&input, &output);
224         }
225
226         Catch(DPL::FileInput::Exception::Base)
227         {
228             // Error while opening or closing source file
229             //ReThrowMsg(InstallerException::CopyIconFailed, sourceFile.str());
230             LogError(
231                 "Copying widget's icon failed. Widget's icon will not be"\
232                 "available from Main Screen");
233         }
234
235         Catch(DPL::FileOutput::Exception::Base)
236         {
237             // Error while opening or closing target file
238             //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
239             LogError(
240                 "Copying widget's icon failed. Widget's icon will not be"\
241                 "available from Main Screen");
242         }
243
244         Catch(DPL::CopyFailed)
245         {
246             // Error while copying
247             //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
248             LogError(
249                 "Copying widget's icon failed. Widget's icon will not be"\
250                 "available from Main Screen");
251         }
252     }
253
254     m_context.job->UpdateProgress(
255         InstallerContext::INSTALL_COPY_ICONFILE,
256         "Widget iconfile copy Finished");
257 }
258
259 void TaskManifestFile::stepCreateShortcut()
260 {
261     LogInfo("create shortcut");
262
263     WidgetDAOReadOnly dao(*m_context.widgetHandle);
264     LanguageTagsList languageTags(dao.getLanguageTags());
265     DPL::OptionalString name; 
266     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
267
268     FOREACH(i, languageTags)
269     {
270         name = dao.getLocalizedInfo(*i).name;
271         if (!name.IsNull()) {
272             LogInfo("found name: " << DPL::ToUTF8String(*name).c_str());
273             break;
274         }
275     }
276
277     add_to_home_shortcut(DPL::ToUTF8String(*pkgname).c_str(),
278                          DPL::ToUTF8String(*name).c_str(),
279                          LAUNCH_BY_PACKAGE,
280                          DPL::ToUTF8String(*name).c_str(),
281                          m_context.installedIconPath.c_str(),
282                          NULL, NULL);
283
284     m_context.job->UpdateProgress(
285         InstallerContext::INSTALL_CREATE_SHORTCUT,
286         "shortcut creation Finished");
287 }
288
289 void TaskManifestFile::stepBackupIconFiles()
290 {
291     LogDebug("Backup Icon Files");
292
293     backup_dir << GlobalConfig::GetUserInstalledWidgetPath();
294     backup_dir << "/" << m_context.widgetConfig.pkgname;
295     backup_dir << "/" << "backup" << "/";
296
297     backupIconFiles();
298
299     m_context.job->UpdateProgress(
300         InstallerContext::INSTALL_BACKUP_ICONFILE,
301         "New Widget icon file backup Finished");
302 }
303
304 void TaskManifestFile::stepAbortIconFiles()
305 {
306     LogDebug("Abrot Icon Files");
307     FOREACH(it, icon_list)
308     {
309         LogDebug("Remove Update Icon : " << (*it));
310         unlink((*it).c_str());
311     }
312
313     std::ostringstream b_icon_dir;
314     b_icon_dir << backup_dir.str() << "icons";
315
316     std::list<std::string> fileList;
317     getFileList(b_icon_dir.str().c_str(), fileList);
318
319     FOREACH(back_icon, fileList)
320     {
321         std::ostringstream res_file;
322         res_file << GlobalConfig::GetUserWidgetDesktopIconPath();
323         res_file << "/" << (*back_icon);
324
325         std::ostringstream backup_file;
326         backup_file << b_icon_dir.str() << "/" << (*back_icon);
327
328         Try
329         {
330             DPL::FileInput input(backup_file.str());
331             DPL::FileOutput output(res_file.str());
332             DPL::Copy(&input, &output);
333         }
334         Catch(DPL::FileInput::Exception::Base)
335         {
336             LogError("Restoration icon File Failed." << backup_file.str()
337                     << " to " << res_file.str());
338         }
339
340         Catch(DPL::FileOutput::Exception::Base)
341         {
342             LogError("Restoration icon File Failed." << backup_file.str()
343                     << " to " << res_file.str());
344         }
345         Catch(DPL::CopyFailed)
346         {
347             LogError("Restoration icon File Failed." << backup_file.str()
348                     << " to " << res_file.str());
349         }
350     }
351 }
352
353 void TaskManifestFile::stepUpdateFinalize()
354 {
355     LogDebug("Finished Update Desktopfile");
356 }
357
358 DPL::String TaskManifestFile::getIconTargetFilename(
359         const DPL::String& languageTag) const
360 {
361     DPL::OStringStream filename;
362     DPL::Optional<DPL::String> pkgname = m_context.widgetConfig.pkgname;
363     if (pkgname.IsNull()) {
364         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
365     }
366
367     filename << DPL::ToUTF8String(*pkgname).c_str();
368
369     if (!languageTag.empty()) {
370         DPL::OptionalString tag = getLangTag(languageTag); // translate en -> en_US etc
371         if (tag.IsNull()) { tag = languageTag; }
372         DPL::String locale =
373             LocalizationUtils::BCP47LanguageTagToLocale(*tag);
374
375        if(locale.empty()) {
376             filename << L"." << languageTag;
377         } else {
378             filename << L"." << locale;
379         }
380     }
381
382     filename << L".png";
383     return filename.str();
384 }
385
386 void TaskManifestFile::stepFinalize()
387 {
388     LogInfo("Finished ManifestFile step");
389 }
390
391 void TaskManifestFile::saveLocalizedKey(std::ofstream &file,
392         const DPL::String& key,
393         const DPL::String& languageTag)
394 {
395     DPL::String locale =
396         LocalizationUtils::BCP47LanguageTagToLocale(languageTag);
397
398     file << key;
399     if (!locale.empty()) {
400         file << "[" << locale << "]";
401     }
402     file << "=";
403 }
404
405 void TaskManifestFile::updateAilInfo()
406 {
407     // Update ail for desktop
408     std::string cfgPkgname =
409         DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
410     const char* pkgname = cfgPkgname.c_str();
411
412     LogDebug("Update ail desktop : " << pkgname );
413     ail_appinfo_h ai = NULL;
414     ail_error_e ret;
415
416     ret = ail_package_get_appinfo(pkgname, &ai);
417     if (ai) {
418         ail_package_destroy_appinfo(ai);
419     }
420     
421     if (AIL_ERROR_NO_DATA == ret) {
422         if (ail_desktop_add(pkgname) < 0) {
423             LogDebug("Failed to add ail desktop : " << pkgname);
424         }
425     } else if (AIL_ERROR_OK == ret) {
426         if (ail_desktop_update(pkgname) < 0) {
427             LogDebug("Failed to update ail desktop : " << pkgname);
428         }
429     }
430 }
431
432 void TaskManifestFile::backupIconFiles()
433 {
434     LogInfo("Backup Icon Files");
435
436     std::ostringstream b_icon_dir;
437     b_icon_dir << backup_dir.str() << "icons";
438
439     LogDebug("Create icon backup folder : " << b_icon_dir.str());
440     _WrtMakeDir(b_icon_dir.str().c_str(), 0755, WRT_FILEUTILS_RECUR);
441
442     std::list<std::string> fileList;
443     getFileList(GlobalConfig::GetUserWidgetDesktopIconPath(), fileList);
444     std::string pkgname = DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
445     
446     FOREACH(it, fileList)
447     {
448         if (0 == (strncmp((*it).c_str(), pkgname.c_str(),
449                         strlen(pkgname.c_str())))) {
450             std::ostringstream icon_file, backup_icon;
451             icon_file << GlobalConfig::GetUserWidgetDesktopIconPath();
452             icon_file << "/" << (*it);
453
454             backup_icon << b_icon_dir.str() << "/" << (*it);
455
456             LogDebug("Backup icon file " << icon_file.str() << " to " <<
457                     backup_icon.str());
458             Try
459             {
460                 DPL::FileInput input(icon_file.str());
461                 DPL::FileOutput output(backup_icon.str());
462                 DPL::Copy(&input, &output);
463             }
464             Catch(DPL::FileInput::Exception::Base)
465             {
466                 LogError("Backup Desktop File Failed.");
467                 ReThrowMsg(Exceptions::BackupFailed, icon_file.str());
468             }
469
470             Catch(DPL::FileOutput::Exception::Base)
471             {
472                 LogError("Backup Desktop File Failed.");
473                 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
474             }
475             Catch(DPL::CopyFailed)
476             {
477                 LogError("Backup Desktop File Failed.");
478                 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
479             }
480             unlink((*it).c_str());
481         }
482     }
483 }
484
485 void TaskManifestFile::getFileList(const char* path,
486         std::list<std::string> &list)
487 {
488     DIR* dir = opendir(path);
489     if (!dir) {
490         LogError("icon directory doesn't exist");
491         ThrowMsg(Exceptions::InternalError, path);
492     }
493
494     struct dirent* d_ent;
495     do {
496         if ((d_ent = readdir(dir))) {
497             if(strcmp(d_ent->d_name, ".") == 0 || 
498                     strcmp(d_ent->d_name, "..") == 0) {
499                 continue;
500             }
501             std::string file_name = d_ent->d_name;
502             list.push_back(file_name);
503         }
504     }while(d_ent);
505 }
506
507 void TaskManifestFile::stepGenerateManifest()
508 {
509     DPL::String pkgname = *m_context.widgetConfig.pkgname;
510     manifest_name = pkgname + L".xml";
511     manifest_file += L"/tmp/" + manifest_name;
512
513     //libxml - init and check
514     LibxmlSingleton::Instance().init();
515
516     writeManifest(manifest_file);
517     validateManifest();
518     commitManifest();
519
520     m_context.job->UpdateProgress(
521         InstallerContext::INSTALL_CREATE_MANIFEST,
522         "Widget Manifest Creation Finished");
523 }
524
525 void TaskManifestFile::stepParseManifest()
526 {
527     int code = pkgmgr_parser_parse_manifest_for_installation(
528             DPL::ToUTF8String(manifest_file).c_str(), NULL);
529
530     if(code != 0)
531     {
532         LogError("Manifest parser error: " << code);
533         ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
534     }
535
536     // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
537     updateAilInfo();
538
539     m_context.job->UpdateProgress(
540         InstallerContext::INSTALL_CREATE_MANIFEST,
541         "Widget Manifest Parsing Finished");
542     LogDebug("Manifest parsed");
543 }
544
545 void TaskManifestFile::stepParseUpgradedManifest()
546 {
547     int code = pkgmgr_parser_parse_manifest_for_upgrade(
548             DPL::ToUTF8String(manifest_file).c_str(), NULL);
549
550     if(code != 0)
551     {
552         LogError("Manifest parser error: " << code);
553         ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
554     }
555
556     // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
557     updateAilInfo();
558
559     m_context.job->UpdateProgress(
560         InstallerContext::INSTALL_CREATE_MANIFEST,
561         "Widget Manifest Parsing Finished");
562     LogDebug("Manifest parsed");
563 }
564
565 void TaskManifestFile::validateManifest()
566 {
567     int code = pkgmgr_parser_check_manifest_validation(
568             DPL::ToUTF8String(manifest_name).c_str());
569
570     if(code != 0)
571     {
572         LogError("Manifest validation error");
573         //TODO: manifest files are not yet validating properly because of href
574         // attribute in author element (incompatible types in W3C spec. and
575         // manifest spec.)
576         //ThrowMsg(ManifestValidationError, "Validation returncode: " << code);
577     }
578
579     m_context.job->UpdateProgress(
580         InstallerContext::INSTALL_CREATE_MANIFEST,
581         "Widget Manifest Validation Finished");
582     LogDebug("Manifest validated");
583 }
584
585 void TaskManifestFile::commitManifest()
586 {
587     LogDebug("Commiting manifest file : " << manifest_file);
588
589     std::ostringstream destFile;
590     destFile << "/opt/share/packages" << "/"; //TODO constant with path
591     destFile << DPL::ToUTF8String(manifest_name);
592     LogInfo("cp " << manifest_file << " " << destFile.str());
593
594     DPL::FileInput input(DPL::ToUTF8String(manifest_file));
595     DPL::FileOutput output(destFile.str());
596     DPL::Copy(&input, &output);
597     LogDebug("Manifest writen to: " << destFile.str());
598
599     //removing temp file
600     unlink((DPL::ToUTF8String(manifest_file)).c_str());
601     manifest_file = DPL::FromUTF8String(destFile.str().c_str());
602 }
603
604 void TaskManifestFile::writeManifest(const DPL::String & path)
605 {
606     LogDebug("Generating manifest file : " << path);
607     Manifest manifest;
608     UiApplication uiApp;
609
610     setWidgetExecPath(uiApp);
611     setWidgetName(manifest, uiApp);
612     setWidgetIcons(uiApp);
613     setWidgetManifest(manifest);
614     setWidgetOtherInfo(uiApp);
615     setAppServiceInfo(uiApp);
616
617     manifest.addUiApplication(uiApp);
618     manifest.generate(path);
619     LogDebug("Manifest file serialized");
620 }
621
622 void TaskManifestFile::setWidgetExecPath(UiApplication & uiApp)
623 {
624     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
625     if (pkgname.IsNull()) {
626         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
627     }
628
629     std::ostringstream path;
630     path << GlobalConfig::GetUserInstalledWidgetPath() << "/" << *pkgname << "/";
631     path << GlobalConfig::GetUserWidgetExecPath() << "/" << *m_context.widgetHandle;
632     uiApp.setExec(DPL::FromASCIIString(path.str()));
633 }
634
635 void TaskManifestFile::setWidgetName(Manifest & manifest, UiApplication & uiApp)
636 {
637     Assert(!!m_context.widgetHandle);
638     WidgetDAOReadOnly dao(*m_context.widgetHandle);
639     LanguageTagsList languageTags(dao.getLanguageTags());
640     bool defaultNameSaved = false;
641
642     //labels
643     FOREACH(i, languageTags)
644     {
645         DPL::OptionalString tag = getLangTag(*i); // translate en -> en_US etc
646         if (tag.IsNull())
647         {
648             tag = *i;
649         }
650         DPL::OptionalString name = dao.getLocalizedInfo(*i).name;
651         generateWidgetName(manifest, uiApp, tag, name, defaultNameSaved);
652     }
653     DPL::OptionalString defaultLocale = dao.getDefaultlocale();
654     if (!!defaultLocale && !defaultNameSaved)
655     {
656         DPL::OptionalString name = dao.getLocalizedInfo(*defaultLocale).name;
657         generateWidgetName(manifest, uiApp, DPL::OptionalString::Null, name, defaultNameSaved);
658     }
659     //appid
660     DPL::String pkgname;
661     if(!!m_context.widgetConfig.pkgname)
662     {
663         pkgname = *m_context.widgetConfig.pkgname;
664         uiApp.setAppid(pkgname);
665     }
666
667     //extraid
668     if(!!m_context.widgetConfig.guid) {
669         uiApp.setExtraid(*m_context.widgetConfig.guid);
670     } else {
671         if(!pkgname.empty()) {
672             uiApp.setExtraid(DPL::String(L"http://") + pkgname);
673         }
674     }
675
676     //type
677     uiApp.setType(DPL::FromASCIIString("webapp"));
678     manifest.setType(L"wgt");
679     uiApp.setTaskmanage(true);
680 }
681
682 void TaskManifestFile::generateWidgetName(Manifest & manifest, UiApplication &uiApp, const DPL::OptionalString& tag, DPL::OptionalString name, bool & defaultNameSaved)
683 {
684     if (!!name) {
685         if (!!tag)
686         {
687             DPL::String locale =
688                 LocalizationUtils::BCP47LanguageTagToLocale(*tag);
689
690             if (!locale.empty()) {
691                 uiApp.addLabel(LabelType(*name,*tag));
692             }
693             else
694             {
695                 uiApp.addLabel(LabelType(*name));
696                 manifest.addLabel(LabelType(*name));
697             }
698         }
699         else
700         {
701             defaultNameSaved = true;
702             uiApp.addLabel(LabelType(*name));
703             manifest.addLabel(LabelType(*name));
704         }
705     }
706 }
707
708 void TaskManifestFile::setWidgetIcons(UiApplication & uiApp)
709 {
710     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
711     if (pkgname.IsNull()) {
712         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
713     }
714
715     //TODO this file will need to be updated when user locale preferences
716     //changes.
717     Assert(!!m_context.widgetHandle);
718     WidgetDAOReadOnly dao(*m_context.widgetHandle);
719
720     WidgetDAOReadOnly::WidgetLocalizedIconList locList = dao.getLocalizedIconList();
721     WidgetDAOReadOnly::WidgetIconList list = dao.getIconList();
722     bool defaultIconSaved = false;
723
724     FOREACH(it, locList)
725     {
726         DPL::String i = it->widgetLocale;
727         DPL::OptionalString tag = getLangTag(i); // translate en -> en_US etc
728         if (tag.IsNull()) { tag = i; }
729
730         generateWidgetIcon(uiApp, tag, i, it->iconId, list, defaultIconSaved);
731     }
732     DPL::OptionalString defaultLocale = dao.getDefaultlocale();
733     if (!!defaultLocale && !defaultIconSaved)
734     {
735         int iconId = -1;
736         FOREACH(it, locList)
737         {
738             if (it->widgetLocale == *defaultLocale)
739             {
740                 iconId = it->iconId;
741             }
742         }
743         if (-1 != iconId)
744         {
745             generateWidgetIcon(uiApp, DPL::OptionalString::Null,
746                                DPL::String(),
747                                iconId,
748                                list,
749                                defaultIconSaved);
750         }
751     }
752 }
753
754 void TaskManifestFile::generateWidgetIcon(UiApplication & uiApp, const DPL::OptionalString& tag, const DPL::String& language, int iconId, const WrtDB::WidgetDAOReadOnly::WidgetIconList & list, bool & defaultIconSaved)
755 {
756     DPL::String locale;
757     if (!!tag)
758     {
759         locale = LocalizationUtils::BCP47LanguageTagToLocale(*tag);
760     }
761     else
762     {
763         defaultIconSaved = true;
764     }
765
766     DPL::OptionalString src;
767     FOREACH(icon, list)
768     {
769         if (icon->iconId == iconId) {
770             src = icon->iconSrc;
771         }
772     }
773     if (!!src) {
774         DPL::String iconText;
775         iconText += /*DPL::FromASCIIString(GlobalConfig::GetUserWidgetDesktopIconPath()) + L"/" +*/ getIconTargetFilename(language);
776         if(!locale.empty())
777         {
778             uiApp.addIcon(IconType(iconText,locale));
779         }
780         else
781         {
782             uiApp.addIcon(IconType(iconText));
783         }
784     }
785 }
786
787 void TaskManifestFile::setWidgetManifest(Manifest & manifest)
788 {
789     if(!!m_context.widgetConfig.pkgname)
790     {
791         manifest.setPackage(*m_context.widgetConfig.pkgname);
792     }
793     if(!!m_context.widgetConfig.version)
794     {
795         manifest.setVersion(*m_context.widgetConfig.version);
796     }
797     DPL::String email = (!!m_context.widgetConfig.configInfo.authorEmail ?
798                             *m_context.widgetConfig.configInfo.authorEmail : L"");
799     DPL::String href = (!!m_context.widgetConfig.configInfo.authorHref ?
800                             *m_context.widgetConfig.configInfo.authorHref : L"");
801     DPL::String name = (!!m_context.widgetConfig.configInfo.authorName ?
802                             *m_context.widgetConfig.configInfo.authorName : L"");
803     manifest.addAuthor(Author(email,href,L"",name));
804 }
805
806 void TaskManifestFile::setWidgetOtherInfo(UiApplication & uiApp)
807 {
808     uiApp.setNodisplay(false);
809     //TODO(t.iwanek):
810     //There is no "X-TIZEN-PackageType=wgt", there is not field in manifest
811     //There is no X-TIZEN-PackageID in manifest "X-TIZEN-PackageID=" << DPL::ToUTF8String(*widgetID).c_str()
812     //There is no Comment in pkgmgr "Comment=Widget application"
813 }
814
815 void TaskManifestFile::setAppServiceInfo(UiApplication & uiApp)
816 {
817     Assert(!!m_context.widgetHandle);
818     WidgetDAOReadOnly dao(*m_context.widgetHandle);
819     WidgetApplicationServiceList appServiceList;
820     dao.getAppServiceList(appServiceList);
821
822     if (appServiceList.empty()) {
823         LogInfo("Widget doesn't contain application service");
824         return;
825     }
826
827     // x-tizen-svc=http://tizen.org/appsvc/operation/pick|NULL|image;
828     FOREACH(it, appServiceList) {
829         ApplicationService appService;
830         appService.addOperation(it->operation);
831         appService.addOperation(it->scheme);
832         appService.addOperation(it->mime);
833         uiApp.addApplicationService(appService);
834     }
835 }
836
837 } //namespace WidgetInstall
838 } //namespace Jobs