2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_manifest_file.cpp
18 * @author Pawel Sikorski (p.sikorski@samgsung.com)
25 #include <dpl/assert.h>
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>
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>
48 #include <libxml_utils.h>
49 #include <pkgmgr/pkgmgr_parser.h>
50 #include <dpl/localization/LanguageTagsProvider.h>
52 #define DEFAULT_ICON_NAME "icon.png"
54 using namespace WrtDB;
57 typedef std::map<DPL::String, DPL::String> LanguageTagMap;
59 const char* const ST_TRUE = "true";
60 const char* const ST_NODISPLAY = "nodisplay";
62 LanguageTagMap getLanguageTagMap()
66 #define ADD(tag, l_tag) map.insert(std::make_pair(L ## # tag, L ## # l_tag));
67 #include "languages.def"
73 DPL::OptionalString getLangTag(const DPL::String& tag)
75 static LanguageTagMap TagsMap =
78 DPL::String langTag = tag;
80 LogDebug("Trying to map language tag: " << langTag);
81 size_t pos = langTag.find_first_of(L'_');
82 if (pos != DPL::String::npos) {
85 DPL::OptionalString ret;
87 LanguageTagMap::iterator it = TagsMap.find(langTag);
88 if (it != TagsMap.end()) {
91 LogDebug("Mapping IANA Language tag to language tag: " <<
92 langTag << " -> " << ret);
99 namespace WidgetInstall {
101 const char * TaskManifestFile::encoding = "UTF-8";
103 TaskManifestFile::TaskManifestFile(InstallerContext &inCont) :
104 DPL::TaskDecl<TaskManifestFile>(this),
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);
114 // for widget update.
115 AddStep(&TaskManifestFile::stepBackupIconFiles);
116 AddStep(&TaskManifestFile::stepCopyIconFiles);
117 AddStep(&TaskManifestFile::stepGenerateManifest);
118 AddStep(&TaskManifestFile::stepParseUpgradedManifest);
119 AddStep(&TaskManifestFile::stepUpdateFinalize);
121 AddAbortStep(&TaskManifestFile::stepAbortIconFiles);
125 TaskManifestFile::~TaskManifestFile()
129 void TaskManifestFile::stepCreateExecFile()
131 std::string exec = m_context.locations->getExecFile();
132 std::string clientExeStr = GlobalConfig::GetWrtClientExec();
134 LogInfo("link -s " << clientExeStr << " " << exec);
135 symlink(clientExeStr.c_str(), exec.c_str());
137 m_context.job->UpdateProgress(
138 InstallerContext::INSTALL_CREATE_EXECFILE,
139 "Widget execfile creation Finished");
142 void TaskManifestFile::stepCopyIconFiles()
144 LogDebug("CopyIconFiles");
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 widget
152 std::vector<Locale> generatedLocales;
154 WrtDB::WidgetRegisterInfo::LocalizedIconList & icons = m_context.widgetConfig.localizationData.icons;
156 //reversed: last <icon> has highest priority to be copied if it has given locale (TODO: why was that working that way?)
157 for(WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator icon = icons.rbegin(); icon != icons.rend(); icon++)
159 FOREACH(locale, icon->availableLocales)
161 DPL::String src = icon->src;
162 LogDebug("Icon for locale: " << *locale << "is : " << src);
164 if(std::find(generatedLocales.begin(), generatedLocales.end(), *locale) != generatedLocales.end())
166 LogDebug("Skipping - has that locale");
171 generatedLocales.push_back(*locale);
174 std::ostringstream sourceFile;
175 std::ostringstream targetFile;
177 sourceFile << m_context.locations->getSourceDir() << "/";
179 if (!locale->empty()) {
180 sourceFile << "locales/" << *locale << "/";
185 targetFile << GlobalConfig::GetUserWidgetDesktopIconPath() << "/";
186 targetFile << getIconTargetFilename(*locale);
189 if (m_context.widgetConfig.packagingType ==
190 WrtDB::PKG_TYPE_HOSTED_WEB_APP) {
191 m_context.locations->setIconTargetFilenameForLocale(targetFile.str());
194 LogDebug("Copying icon: " << sourceFile.str() <<
195 " -> " << targetFile.str());
197 icon_list.push_back(targetFile.str());
201 DPL::FileInput input(sourceFile.str());
202 DPL::FileOutput output(targetFile.str());
203 DPL::Copy(&input, &output);
206 Catch(DPL::FileInput::Exception::Base)
208 // Error while opening or closing source file
209 //ReThrowMsg(InstallerException::CopyIconFailed, sourceFile.str());
211 "Copying widget's icon failed. Widget's icon will not be"\
212 "available from Main Screen");
215 Catch(DPL::FileOutput::Exception::Base)
217 // Error while opening or closing target file
218 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
220 "Copying widget's icon failed. Widget's icon will not be"\
221 "available from Main Screen");
224 Catch(DPL::CopyFailed)
226 // Error while copying
227 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
229 "Copying widget's icon failed. Widget's icon will not be"\
230 "available from Main Screen");
235 m_context.job->UpdateProgress(
236 InstallerContext::INSTALL_COPY_ICONFILE,
237 "Widget iconfile copy Finished");
240 void TaskManifestFile::stepBackupIconFiles()
242 LogDebug("Backup Icon Files");
244 backup_dir << m_context.locations->getPackageInstallationDir();
245 backup_dir << "/" << "backup" << "/";
249 m_context.job->UpdateProgress(
250 InstallerContext::INSTALL_BACKUP_ICONFILE,
251 "New Widget icon file backup Finished");
254 void TaskManifestFile::stepAbortIconFiles()
256 LogDebug("Abrot Icon Files");
257 FOREACH(it, icon_list)
259 LogDebug("Remove Update Icon : " << (*it));
260 unlink((*it).c_str());
263 std::ostringstream b_icon_dir;
264 b_icon_dir << backup_dir.str() << "icons";
266 std::list<std::string> fileList;
267 getFileList(b_icon_dir.str().c_str(), fileList);
269 FOREACH(back_icon, fileList)
271 std::ostringstream res_file;
272 res_file << GlobalConfig::GetUserWidgetDesktopIconPath();
273 res_file << "/" << (*back_icon);
275 std::ostringstream backup_file;
276 backup_file << b_icon_dir.str() << "/" << (*back_icon);
280 DPL::FileInput input(backup_file.str());
281 DPL::FileOutput output(res_file.str());
282 DPL::Copy(&input, &output);
284 Catch(DPL::FileInput::Exception::Base)
286 LogError("Restoration icon File Failed." << backup_file.str()
287 << " to " << res_file.str());
290 Catch(DPL::FileOutput::Exception::Base)
292 LogError("Restoration icon File Failed." << backup_file.str()
293 << " to " << res_file.str());
295 Catch(DPL::CopyFailed)
297 LogError("Restoration icon File Failed." << backup_file.str()
298 << " to " << res_file.str());
303 void TaskManifestFile::stepUpdateFinalize()
306 LogDebug("Finished Update Desktopfile");
309 DPL::String TaskManifestFile::getIconTargetFilename(
310 const DPL::String& languageTag) const
312 DPL::OStringStream filename;
313 DPL::Optional<DPL::String> pkgname = m_context.widgetConfig.pkgname;
314 if (pkgname.IsNull()) {
315 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
318 filename << DPL::ToUTF8String(*pkgname).c_str();
320 if (!languageTag.empty()) {
321 DPL::OptionalString tag = getLangTag(languageTag); // translate en -> en_US etc
322 if (tag.IsNull()) { tag = languageTag; }
324 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
327 filename << L"." << languageTag;
329 filename << L"." << locale;
334 return filename.str();
337 void TaskManifestFile::stepFinalize()
340 LogInfo("Finished ManifestFile step");
344 void TaskManifestFile::saveLocalizedKey(std::ofstream &file,
345 const DPL::String& key,
346 const DPL::String& languageTag)
349 LanguageTagsProvider::BCP47LanguageTagToLocale(languageTag);
352 if (!locale.empty()) {
353 file << "[" << locale << "]";
358 void TaskManifestFile::updateAilInfo()
360 // Update ail for desktop
361 std::string cfgPkgname =
362 DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
363 const char* pkgname = cfgPkgname.c_str();
365 LogDebug("Update ail desktop : " << pkgname );
366 ail_appinfo_h ai = NULL;
369 ret = ail_package_get_appinfo(pkgname, &ai);
371 ail_package_destroy_appinfo(ai);
374 if (AIL_ERROR_NO_DATA == ret) {
375 if (ail_desktop_add(pkgname) < 0) {
376 LogWarning("Failed to add ail desktop : " << pkgname);
378 } else if (AIL_ERROR_OK == ret) {
379 if (ail_desktop_update(pkgname) < 0) {
380 LogWarning("Failed to update ail desktop : " << pkgname);
385 void TaskManifestFile::backupIconFiles()
387 LogInfo("Backup Icon Files");
389 std::ostringstream b_icon_dir;
390 b_icon_dir << backup_dir.str() << "icons";
392 LogDebug("Create icon backup folder : " << b_icon_dir.str());
393 WrtUtilMakeDir(b_icon_dir.str());
395 std::list<std::string> fileList;
396 getFileList(GlobalConfig::GetUserWidgetDesktopIconPath(), fileList);
397 std::string pkgname = DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
399 FOREACH(it, fileList)
401 if (0 == (strncmp((*it).c_str(), pkgname.c_str(),
402 strlen(pkgname.c_str())))) {
403 std::ostringstream icon_file, backup_icon;
404 icon_file << GlobalConfig::GetUserWidgetDesktopIconPath();
405 icon_file << "/" << (*it);
407 backup_icon << b_icon_dir.str() << "/" << (*it);
409 LogDebug("Backup icon file " << icon_file.str() << " to " <<
413 DPL::FileInput input(icon_file.str());
414 DPL::FileOutput output(backup_icon.str());
415 DPL::Copy(&input, &output);
417 Catch(DPL::FileInput::Exception::Base)
419 LogError("Backup Desktop File Failed.");
420 ReThrowMsg(Exceptions::BackupFailed, icon_file.str());
423 Catch(DPL::FileOutput::Exception::Base)
425 LogError("Backup Desktop File Failed.");
426 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
428 Catch(DPL::CopyFailed)
430 LogError("Backup Desktop File Failed.");
431 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
433 unlink((*it).c_str());
438 void TaskManifestFile::getFileList(const char* path,
439 std::list<std::string> &list)
441 DIR* dir = opendir(path);
443 LogError("icon directory doesn't exist");
444 ThrowMsg(Exceptions::InternalError, path);
447 struct dirent* d_ent;
449 if ((d_ent = readdir(dir))) {
450 if(strcmp(d_ent->d_name, ".") == 0 ||
451 strcmp(d_ent->d_name, "..") == 0) {
454 std::string file_name = d_ent->d_name;
455 list.push_back(file_name);
458 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
459 LogError("Failed to close dir: " << path << " with error: "
460 << DPL::GetErrnoString());
464 void TaskManifestFile::stepGenerateManifest()
466 DPL::String pkgname = *m_context.widgetConfig.pkgname;
467 manifest_name = pkgname + L".xml";
468 manifest_file += L"/tmp/" + manifest_name;
470 //libxml - init and check
471 LibxmlSingleton::Instance().init();
473 writeManifest(manifest_file);
475 m_context.job->UpdateProgress(
476 InstallerContext::INSTALL_CREATE_MANIFEST,
477 "Widget Manifest Creation Finished");
480 void TaskManifestFile::stepParseManifest()
482 int code = pkgmgr_parser_parse_manifest_for_installation(
483 DPL::ToUTF8String(manifest_file).c_str(), NULL);
487 LogError("Manifest parser error: " << code);
488 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
491 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
494 m_context.job->UpdateProgress(
495 InstallerContext::INSTALL_CREATE_MANIFEST,
496 "Widget Manifest Parsing Finished");
497 LogDebug("Manifest parsed");
500 void TaskManifestFile::stepParseUpgradedManifest()
502 int code = pkgmgr_parser_parse_manifest_for_upgrade(
503 DPL::ToUTF8String(manifest_file).c_str(), NULL);
507 LogError("Manifest parser error: " << code);
508 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
511 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
514 m_context.job->UpdateProgress(
515 InstallerContext::INSTALL_CREATE_MANIFEST,
516 "Widget Manifest Parsing Finished");
517 LogDebug("Manifest parsed");
520 void TaskManifestFile::commitManifest()
522 LogDebug("Commiting manifest file : " << manifest_file);
524 std::ostringstream destFile;
525 destFile << "/opt/share/packages" << "/"; //TODO constant with path
526 destFile << DPL::ToUTF8String(manifest_name);
527 LogInfo("cp " << manifest_file << " " << destFile.str());
529 DPL::FileInput input(DPL::ToUTF8String(manifest_file));
530 DPL::FileOutput output(destFile.str());
531 DPL::Copy(&input, &output);
532 LogDebug("Manifest writen to: " << destFile.str());
535 unlink((DPL::ToUTF8String(manifest_file)).c_str());
536 manifest_file = DPL::FromUTF8String(destFile.str().c_str());
539 void TaskManifestFile::writeManifest(const DPL::String & path)
541 LogDebug("Generating manifest file : " << path);
545 setWidgetExecPath(uiApp);
546 setWidgetName(manifest, uiApp);
547 setWidgetIcons(uiApp);
548 setWidgetManifest(manifest);
549 setWidgetOtherInfo(uiApp);
550 setAppServiceInfo(uiApp);
551 setAppCategory(uiApp);
553 manifest.addUiApplication(uiApp);
554 manifest.generate(path);
555 LogDebug("Manifest file serialized");
558 void TaskManifestFile::setWidgetExecPath(UiApplication & uiApp)
560 uiApp.setExec(DPL::FromASCIIString(m_context.locations->getExecFile()));
563 void TaskManifestFile::setWidgetName(Manifest & manifest, UiApplication & uiApp)
565 bool defaultNameSaved = false;
567 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
568 std::pair<DPL::String, WrtDB::ConfigParserData::LocalizedData> defaultLocalizedData;
570 FOREACH(localizedData, m_context.widgetConfig.configInfo.localizedDataSet)
572 Locale i = localizedData->first;
573 DPL::OptionalString tag = getLangTag(i); // translate en -> en_US etc
578 DPL::OptionalString name = localizedData->second.name;
579 generateWidgetName(manifest, uiApp, tag, name, defaultNameSaved);
581 //store default locale localized data
582 if(!!defaultLocale && defaultLocale == i)
584 defaultLocalizedData = *localizedData;
588 if (!!defaultLocale && !defaultNameSaved)
590 DPL::OptionalString name = defaultLocalizedData.second.name;
591 generateWidgetName(manifest, uiApp, DPL::OptionalString::Null, name, defaultNameSaved);
595 if(!!m_context.widgetConfig.pkgname)
597 pkgname = *m_context.widgetConfig.pkgname;
598 uiApp.setAppid(pkgname);
602 if(!!m_context.widgetConfig.guid) {
603 uiApp.setExtraid(*m_context.widgetConfig.guid);
605 if(!pkgname.empty()) {
606 uiApp.setExtraid(DPL::String(L"http://") + pkgname);
611 uiApp.setType(DPL::FromASCIIString("webapp"));
612 manifest.setType(L"wgt");
613 uiApp.setTaskmanage(true);
616 void TaskManifestFile::generateWidgetName(Manifest & manifest, UiApplication &uiApp, const DPL::OptionalString& tag, DPL::OptionalString name, bool & defaultNameSaved)
622 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
624 if (!locale.empty()) {
625 uiApp.addLabel(LabelType(*name,*tag));
629 uiApp.addLabel(LabelType(*name));
630 manifest.addLabel(LabelType(*name));
635 defaultNameSaved = true;
636 uiApp.addLabel(LabelType(*name));
637 manifest.addLabel(LabelType(*name));
642 void TaskManifestFile::setWidgetIcons(UiApplication & uiApp)
644 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
645 if (pkgname.IsNull()) {
646 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
649 //TODO this file will need to be updated when user locale preferences
651 bool defaultIconSaved = false;
653 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
655 std::vector<Locale> generatedLocales;
656 WrtDB::WidgetRegisterInfo::LocalizedIconList & icons = m_context.widgetConfig.localizationData.icons;
658 //reversed: last <icon> has highest priority to be writen to manifest if it has given locale (TODO: why was that working that way?)
659 for(WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator icon = icons.rbegin(); icon != icons.rend(); icon++)
661 FOREACH(locale, icon->availableLocales)
663 if(std::find(generatedLocales.begin(), generatedLocales.end(), *locale) != generatedLocales.end())
665 LogDebug("Skipping - has that locale - already in manifest");
670 generatedLocales.push_back(*locale);
673 DPL::OptionalString tag = getLangTag(*locale); // translate en -> en_US etc
674 if (tag.IsNull()) { tag = *locale; }
676 generateWidgetIcon(uiApp, tag, *locale, defaultIconSaved);
679 if (!!defaultLocale && !defaultIconSaved)
681 generateWidgetIcon(uiApp, DPL::OptionalString::Null,
687 void TaskManifestFile::generateWidgetIcon(UiApplication & uiApp, const DPL::OptionalString& tag,
688 const DPL::String& language, bool & defaultIconSaved)
693 locale = LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
697 defaultIconSaved = true;
700 DPL::String iconText;
701 iconText += getIconTargetFilename(language);
705 uiApp.addIcon(IconType(iconText, locale));
709 uiApp.addIcon(IconType(iconText));
713 void TaskManifestFile::setWidgetManifest(Manifest & manifest)
715 if(!!m_context.widgetConfig.pkgname)
717 manifest.setPackage(*m_context.widgetConfig.pkgname);
719 if(!!m_context.widgetConfig.version)
721 manifest.setVersion(*m_context.widgetConfig.version);
723 DPL::String email = (!!m_context.widgetConfig.configInfo.authorEmail ?
724 *m_context.widgetConfig.configInfo.authorEmail : L"");
725 DPL::String href = (!!m_context.widgetConfig.configInfo.authorHref ?
726 *m_context.widgetConfig.configInfo.authorHref : L"");
727 DPL::String name = (!!m_context.widgetConfig.configInfo.authorName ?
728 *m_context.widgetConfig.configInfo.authorName : L"");
729 manifest.addAuthor(Author(email,href,L"",name));
732 void TaskManifestFile::setWidgetOtherInfo(UiApplication & uiApp)
734 FOREACH(it, m_context.widgetConfig.configInfo.settingsList)
736 if(!strcmp(DPL::ToUTF8String(it->m_name).c_str(), ST_NODISPLAY)) {
737 if(!strcmp(DPL::ToUTF8String(it->m_value).c_str(), ST_TRUE)) {
738 uiApp.setNodisplay(true);
741 uiApp.setNodisplay(false);
746 //There is no "X-TIZEN-PackageType=wgt"
747 //There is no X-TIZEN-PackageID in manifest "X-TIZEN-PackageID=" << DPL::ToUTF8String(*widgetID).c_str()
748 //There is no Comment in pkgmgr "Comment=Widget application"
749 //that were in desktop file
752 void TaskManifestFile::setAppServiceInfo(UiApplication & uiApp)
754 WrtDB::ConfigParserData::ServiceInfoList appServiceList = m_context.widgetConfig.configInfo.appServiceList;
756 if (appServiceList.empty()) {
757 LogInfo("Widget doesn't contain application service");
761 // x-tizen-svc=http://tizen.org/appcontrol/operation/pick|NULL|image;
762 FOREACH(it, appServiceList) {
763 AppControl appControl;
764 if (!it->m_operation.empty()) {
765 appControl.addOperation(it->m_operation); //TODO: encapsulation?
767 if (!it->m_scheme.empty()) {
768 appControl.addUri(it->m_scheme);
770 if (!it->m_mime.empty()) {
771 appControl.addMime(it->m_mime);
773 uiApp.addAppControl(appControl);
777 void TaskManifestFile::setAppCategory(UiApplication &uiApp)
779 WrtDB::ConfigParserData::CategoryList categoryList =
780 m_context.widgetConfig.configInfo.categoryList;
782 if (categoryList.empty()) {
783 LogInfo("Widget doesn't contain application category");
786 FOREACH(it, categoryList) {
787 if (!(*it).empty()) {
788 uiApp.addAppCategory(*it);
793 } //namespace WidgetInstall