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 LanguageTagMap getLanguageTagMap()
63 #define ADD(tag, l_tag) map.insert(std::make_pair(L ## # tag, L ## # l_tag));
64 #include "languages.def"
70 DPL::OptionalString getLangTag(const DPL::String& tag)
72 static LanguageTagMap TagsMap =
75 DPL::String langTag = tag;
77 LogDebug("Trying to map language tag: " << langTag);
78 size_t pos = langTag.find_first_of(L'_');
79 if (pos != DPL::String::npos) {
82 DPL::OptionalString ret;
84 LanguageTagMap::iterator it = TagsMap.find(langTag);
85 if (it != TagsMap.end()) {
88 LogDebug("Mapping IANA Language tag to language tag: " <<
89 langTag << " -> " << ret);
96 namespace WidgetInstall {
98 const char * TaskManifestFile::encoding = "UTF-8";
100 TaskManifestFile::TaskManifestFile(InstallerContext &inCont) :
101 DPL::TaskDecl<TaskManifestFile>(this),
104 if (false == m_context.existingWidgetInfo.isExist) {
105 AddStep(&TaskManifestFile::stepCopyIconFiles);
106 AddStep(&TaskManifestFile::stepCreateExecFile);
107 AddStep(&TaskManifestFile::stepGenerateManifest);
108 AddStep(&TaskManifestFile::stepParseManifest);
109 AddStep(&TaskManifestFile::stepFinalize);
111 // for widget update.
112 AddStep(&TaskManifestFile::stepBackupIconFiles);
113 AddStep(&TaskManifestFile::stepCopyIconFiles);
114 AddStep(&TaskManifestFile::stepGenerateManifest);
115 AddStep(&TaskManifestFile::stepParseUpgradedManifest);
116 AddStep(&TaskManifestFile::stepUpdateFinalize);
118 AddAbortStep(&TaskManifestFile::stepAbortIconFiles);
122 TaskManifestFile::~TaskManifestFile()
126 void TaskManifestFile::stepCreateExecFile()
128 std::string exec = m_context.locations->getExecFile();
129 std::string clientExeStr = GlobalConfig::GetWrtClientExec();
131 LogInfo("link -s " << clientExeStr << " " << exec);
132 symlink(clientExeStr.c_str(), exec.c_str());
134 m_context.job->UpdateProgress(
135 InstallerContext::INSTALL_CREATE_EXECFILE,
136 "Widget execfile creation Finished");
139 void TaskManifestFile::stepCopyIconFiles()
141 LogDebug("CopyIconFiles");
143 //This function copies icon to desktop icon path. For each locale avaliable
144 //which there is at least one icon in widget for, icon file is copied.
145 //Coping prioritize last positions when coping. If there is several icons
146 //with given locale, the one, that will be copied, will be icon
147 //which is declared by <icon> tag later than the others in config.xml of widget
149 std::vector<Locale> generatedLocales;
151 WrtDB::WidgetRegisterInfo::LocalizedIconList & icons = m_context.widgetConfig.localizationData.icons;
153 //reversed: last <icon> has highest priority to be copied if it has given locale (TODO: why was that working that way?)
154 for(WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator icon = icons.rbegin(); icon != icons.rend(); icon++)
156 FOREACH(locale, icon->availableLocales)
158 DPL::String src = icon->src;
159 LogDebug("Icon for locale: " << *locale << "is : " << src);
161 if(std::find(generatedLocales.begin(), generatedLocales.end(), *locale) != generatedLocales.end())
163 LogDebug("Skipping - has that locale");
168 generatedLocales.push_back(*locale);
171 std::ostringstream sourceFile;
172 std::ostringstream targetFile;
174 sourceFile << m_context.locations->getSourceDir() << "/";
176 if (!locale->empty()) {
177 sourceFile << "locales/" << *locale << "/";
182 targetFile << GlobalConfig::GetUserWidgetDesktopIconPath() << "/";
183 targetFile << getIconTargetFilename(*locale);
186 if (m_context.widgetConfig.packagingType ==
187 WrtDB::PKG_TYPE_HOSTED_WEB_APP) {
188 m_context.locations->setIconTargetFilenameForLocale(targetFile.str());
191 LogDebug("Copying icon: " << sourceFile.str() <<
192 " -> " << targetFile.str());
194 icon_list.push_back(targetFile.str());
198 DPL::FileInput input(sourceFile.str());
199 DPL::FileOutput output(targetFile.str());
200 DPL::Copy(&input, &output);
203 Catch(DPL::FileInput::Exception::Base)
205 // Error while opening or closing source file
206 //ReThrowMsg(InstallerException::CopyIconFailed, sourceFile.str());
208 "Copying widget's icon failed. Widget's icon will not be"\
209 "available from Main Screen");
212 Catch(DPL::FileOutput::Exception::Base)
214 // Error while opening or closing target file
215 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
217 "Copying widget's icon failed. Widget's icon will not be"\
218 "available from Main Screen");
221 Catch(DPL::CopyFailed)
223 // Error while copying
224 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
226 "Copying widget's icon failed. Widget's icon will not be"\
227 "available from Main Screen");
232 m_context.job->UpdateProgress(
233 InstallerContext::INSTALL_COPY_ICONFILE,
234 "Widget iconfile copy Finished");
237 void TaskManifestFile::stepBackupIconFiles()
239 LogDebug("Backup Icon Files");
241 backup_dir << m_context.locations->getPackageInstallationDir();
242 backup_dir << "/" << "backup" << "/";
246 m_context.job->UpdateProgress(
247 InstallerContext::INSTALL_BACKUP_ICONFILE,
248 "New Widget icon file backup Finished");
251 void TaskManifestFile::stepAbortIconFiles()
253 LogDebug("Abrot Icon Files");
254 FOREACH(it, icon_list)
256 LogDebug("Remove Update Icon : " << (*it));
257 unlink((*it).c_str());
260 std::ostringstream b_icon_dir;
261 b_icon_dir << backup_dir.str() << "icons";
263 std::list<std::string> fileList;
264 getFileList(b_icon_dir.str().c_str(), fileList);
266 FOREACH(back_icon, fileList)
268 std::ostringstream res_file;
269 res_file << GlobalConfig::GetUserWidgetDesktopIconPath();
270 res_file << "/" << (*back_icon);
272 std::ostringstream backup_file;
273 backup_file << b_icon_dir.str() << "/" << (*back_icon);
277 DPL::FileInput input(backup_file.str());
278 DPL::FileOutput output(res_file.str());
279 DPL::Copy(&input, &output);
281 Catch(DPL::FileInput::Exception::Base)
283 LogError("Restoration icon File Failed." << backup_file.str()
284 << " to " << res_file.str());
287 Catch(DPL::FileOutput::Exception::Base)
289 LogError("Restoration icon File Failed." << backup_file.str()
290 << " to " << res_file.str());
292 Catch(DPL::CopyFailed)
294 LogError("Restoration icon File Failed." << backup_file.str()
295 << " to " << res_file.str());
300 void TaskManifestFile::stepUpdateFinalize()
303 LogDebug("Finished Update Desktopfile");
306 DPL::String TaskManifestFile::getIconTargetFilename(
307 const DPL::String& languageTag) const
309 DPL::OStringStream filename;
310 DPL::String pkgname = m_context.widgetConfig.pkgname_NOTNULL;
312 filename << DPL::ToUTF8String(pkgname).c_str();
314 if (!languageTag.empty()) {
315 DPL::OptionalString tag = getLangTag(languageTag); // translate en -> en_US etc
316 if (tag.IsNull()) { tag = languageTag; }
318 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
321 filename << L"." << languageTag;
323 filename << L"." << locale;
328 return filename.str();
331 void TaskManifestFile::stepFinalize()
334 LogInfo("Finished ManifestFile step");
338 void TaskManifestFile::saveLocalizedKey(std::ofstream &file,
339 const DPL::String& key,
340 const DPL::String& languageTag)
343 LanguageTagsProvider::BCP47LanguageTagToLocale(languageTag);
346 if (!locale.empty()) {
347 file << "[" << locale << "]";
352 void TaskManifestFile::updateAilInfo()
354 // Update ail for desktop
355 std::string cfgPkgname =
356 DPL::ToUTF8String(m_context.widgetConfig.pkgname_NOTNULL);
357 const char* pkgname = cfgPkgname.c_str();
359 LogDebug("Update ail desktop : " << pkgname );
360 ail_appinfo_h ai = NULL;
363 ret = ail_package_get_appinfo(pkgname, &ai);
365 ail_package_destroy_appinfo(ai);
368 if (AIL_ERROR_NO_DATA == ret) {
369 if (ail_desktop_add(pkgname) < 0) {
370 LogWarning("Failed to add ail desktop : " << pkgname);
372 } else if (AIL_ERROR_OK == ret) {
373 if (ail_desktop_update(pkgname) < 0) {
374 LogWarning("Failed to update ail desktop : " << pkgname);
379 void TaskManifestFile::backupIconFiles()
381 LogInfo("Backup Icon Files");
383 std::ostringstream b_icon_dir;
384 b_icon_dir << backup_dir.str() << "icons";
386 LogDebug("Create icon backup folder : " << b_icon_dir.str());
387 WrtUtilMakeDir(b_icon_dir.str());
389 std::list<std::string> fileList;
390 getFileList(GlobalConfig::GetUserWidgetDesktopIconPath(), fileList);
391 std::string pkgname = DPL::ToUTF8String(m_context.widgetConfig.pkgname_NOTNULL);
393 FOREACH(it, fileList)
395 if (0 == (strncmp((*it).c_str(), pkgname.c_str(),
396 strlen(pkgname.c_str())))) {
397 std::ostringstream icon_file, backup_icon;
398 icon_file << GlobalConfig::GetUserWidgetDesktopIconPath();
399 icon_file << "/" << (*it);
401 backup_icon << b_icon_dir.str() << "/" << (*it);
403 LogDebug("Backup icon file " << icon_file.str() << " to " <<
407 DPL::FileInput input(icon_file.str());
408 DPL::FileOutput output(backup_icon.str());
409 DPL::Copy(&input, &output);
411 Catch(DPL::FileInput::Exception::Base)
413 LogError("Backup Desktop File Failed.");
414 ReThrowMsg(Exceptions::BackupFailed, icon_file.str());
417 Catch(DPL::FileOutput::Exception::Base)
419 LogError("Backup Desktop File Failed.");
420 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
422 Catch(DPL::CopyFailed)
424 LogError("Backup Desktop File Failed.");
425 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
427 unlink((*it).c_str());
432 void TaskManifestFile::getFileList(const char* path,
433 std::list<std::string> &list)
435 DIR* dir = opendir(path);
437 LogError("icon directory doesn't exist");
438 ThrowMsg(Exceptions::InternalError, path);
441 struct dirent* d_ent;
443 if ((d_ent = readdir(dir))) {
444 if(strcmp(d_ent->d_name, ".") == 0 ||
445 strcmp(d_ent->d_name, "..") == 0) {
448 std::string file_name = d_ent->d_name;
449 list.push_back(file_name);
452 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
453 LogError("Failed to close dir: " << path << " with error: "
454 << DPL::GetErrnoString());
458 void TaskManifestFile::stepGenerateManifest()
460 DPL::String pkgname = m_context.widgetConfig.pkgname_NOTNULL;
461 manifest_name = pkgname + L".xml";
462 manifest_file += L"/tmp/" + manifest_name;
464 //libxml - init and check
465 LibxmlSingleton::Instance().init();
467 writeManifest(manifest_file);
469 m_context.job->UpdateProgress(
470 InstallerContext::INSTALL_CREATE_MANIFEST,
471 "Widget Manifest Creation Finished");
474 void TaskManifestFile::stepParseManifest()
476 int code = pkgmgr_parser_parse_manifest_for_installation(
477 DPL::ToUTF8String(manifest_file).c_str(), NULL);
481 LogError("Manifest parser error: " << code);
482 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
485 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
488 m_context.job->UpdateProgress(
489 InstallerContext::INSTALL_CREATE_MANIFEST,
490 "Widget Manifest Parsing Finished");
491 LogDebug("Manifest parsed");
494 void TaskManifestFile::stepParseUpgradedManifest()
496 int code = pkgmgr_parser_parse_manifest_for_upgrade(
497 DPL::ToUTF8String(manifest_file).c_str(), NULL);
501 LogError("Manifest parser error: " << code);
502 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
505 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
508 m_context.job->UpdateProgress(
509 InstallerContext::INSTALL_CREATE_MANIFEST,
510 "Widget Manifest Parsing Finished");
511 LogDebug("Manifest parsed");
514 void TaskManifestFile::commitManifest()
516 LogDebug("Commiting manifest file : " << manifest_file);
518 std::ostringstream destFile;
519 destFile << "/opt/share/packages" << "/"; //TODO constant with path
520 destFile << DPL::ToUTF8String(manifest_name);
521 LogInfo("cp " << manifest_file << " " << destFile.str());
523 DPL::FileInput input(DPL::ToUTF8String(manifest_file));
524 DPL::FileOutput output(destFile.str());
525 DPL::Copy(&input, &output);
526 LogDebug("Manifest writen to: " << destFile.str());
529 unlink((DPL::ToUTF8String(manifest_file)).c_str());
530 manifest_file = DPL::FromUTF8String(destFile.str().c_str());
533 void TaskManifestFile::writeManifest(const DPL::String & path)
535 LogDebug("Generating manifest file : " << path);
539 setWidgetExecPath(uiApp);
540 setWidgetName(manifest, uiApp);
541 setWidgetIcons(uiApp);
542 setWidgetManifest(manifest);
543 setWidgetOtherInfo(uiApp);
544 setAppServiceInfo(uiApp);
546 manifest.addUiApplication(uiApp);
547 manifest.generate(path);
548 LogDebug("Manifest file serialized");
551 void TaskManifestFile::setWidgetExecPath(UiApplication & uiApp)
553 uiApp.setExec(DPL::FromASCIIString(m_context.locations->getExecFile()));
556 void TaskManifestFile::setWidgetName(Manifest & manifest, UiApplication & uiApp)
558 bool defaultNameSaved = false;
560 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
561 std::pair<DPL::String, WrtDB::ConfigParserData::LocalizedData> defaultLocalizedData;
563 FOREACH(localizedData, m_context.widgetConfig.configInfo.localizedDataSet)
565 Locale i = localizedData->first;
566 DPL::OptionalString tag = getLangTag(i); // translate en -> en_US etc
571 DPL::OptionalString name = localizedData->second.name;
572 generateWidgetName(manifest, uiApp, tag, name, defaultNameSaved);
574 //store default locale localized data
575 if(!!defaultLocale && defaultLocale == i)
577 defaultLocalizedData = *localizedData;
581 if (!!defaultLocale && !defaultNameSaved)
583 DPL::OptionalString name = defaultLocalizedData.second.name;
584 generateWidgetName(manifest, uiApp, DPL::OptionalString::Null, name, defaultNameSaved);
589 pkgname = m_context.widgetConfig.pkgname_NOTNULL;
590 uiApp.setAppid(pkgname);
593 if(!!m_context.widgetConfig.guid) {
594 uiApp.setExtraid(*m_context.widgetConfig.guid);
596 if(!pkgname.empty()) {
597 uiApp.setExtraid(DPL::String(L"http://") + pkgname);
602 uiApp.setType(DPL::FromASCIIString("webapp"));
603 manifest.setType(L"wgt");
604 uiApp.setTaskmanage(true);
607 void TaskManifestFile::generateWidgetName(Manifest & manifest, UiApplication &uiApp, const DPL::OptionalString& tag, DPL::OptionalString name, bool & defaultNameSaved)
613 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
615 if (!locale.empty()) {
616 uiApp.addLabel(LabelType(*name,*tag));
620 uiApp.addLabel(LabelType(*name));
621 manifest.addLabel(LabelType(*name));
626 defaultNameSaved = true;
627 uiApp.addLabel(LabelType(*name));
628 manifest.addLabel(LabelType(*name));
633 void TaskManifestFile::setWidgetIcons(UiApplication & uiApp)
635 DPL::String pkgname = m_context.widgetConfig.pkgname_NOTNULL;
637 //TODO this file will need to be updated when user locale preferences
639 bool defaultIconSaved = false;
641 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
643 std::vector<Locale> generatedLocales;
644 WrtDB::WidgetRegisterInfo::LocalizedIconList & icons = m_context.widgetConfig.localizationData.icons;
646 //reversed: last <icon> has highest priority to be writen to manifest if it has given locale (TODO: why was that working that way?)
647 for(WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator icon = icons.rbegin(); icon != icons.rend(); icon++)
649 FOREACH(locale, icon->availableLocales)
651 if(std::find(generatedLocales.begin(), generatedLocales.end(), *locale) != generatedLocales.end())
653 LogDebug("Skipping - has that locale - already in manifest");
658 generatedLocales.push_back(*locale);
661 DPL::OptionalString tag = getLangTag(*locale); // translate en -> en_US etc
662 if (tag.IsNull()) { tag = *locale; }
664 generateWidgetIcon(uiApp, tag, *locale, defaultIconSaved);
667 if (!!defaultLocale && !defaultIconSaved)
669 generateWidgetIcon(uiApp, DPL::OptionalString::Null,
675 void TaskManifestFile::generateWidgetIcon(UiApplication & uiApp, const DPL::OptionalString& tag,
676 const DPL::String& language, bool & defaultIconSaved)
681 locale = LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
685 defaultIconSaved = true;
688 DPL::String iconText;
689 iconText += getIconTargetFilename(language);
693 uiApp.addIcon(IconType(iconText, locale));
697 uiApp.addIcon(IconType(iconText));
701 void TaskManifestFile::setWidgetManifest(Manifest & manifest)
703 manifest.setPackage(m_context.widgetConfig.pkgname_NOTNULL);
705 if(!!m_context.widgetConfig.version)
707 manifest.setVersion(*m_context.widgetConfig.version);
709 DPL::String email = (!!m_context.widgetConfig.configInfo.authorEmail ?
710 *m_context.widgetConfig.configInfo.authorEmail : L"");
711 DPL::String href = (!!m_context.widgetConfig.configInfo.authorHref ?
712 *m_context.widgetConfig.configInfo.authorHref : L"");
713 DPL::String name = (!!m_context.widgetConfig.configInfo.authorName ?
714 *m_context.widgetConfig.configInfo.authorName : L"");
715 manifest.addAuthor(Author(email,href,L"",name));
718 void TaskManifestFile::setWidgetOtherInfo(UiApplication & uiApp)
720 uiApp.setNodisplay(false);
722 //There is no "X-TIZEN-PackageType=wgt"
723 //There is no X-TIZEN-PackageID in manifest "X-TIZEN-PackageID=" << DPL::ToUTF8String(*widgetID).c_str()
724 //There is no Comment in pkgmgr "Comment=Widget application"
725 //that were in desktop file
728 void TaskManifestFile::setAppServiceInfo(UiApplication & uiApp)
730 WrtDB::ConfigParserData::ServiceInfoList appServiceList = m_context.widgetConfig.configInfo.appServiceList;
732 if (appServiceList.empty()) {
733 LogInfo("Widget doesn't contain application service");
737 // x-tizen-svc=http://tizen.org/appcontrol/operation/pick|NULL|image;
738 FOREACH(it, appServiceList) {
739 ApplicationService appService;
740 if (!it->m_operation.empty()) {
741 appService.addOperation(it->m_operation); //TODO: encapsulation?
743 if (!it->m_scheme.empty()) {
744 appService.addUri(it->m_scheme);
746 if (!it->m_mime.empty()) {
747 appService.addMime(it->m_mime);
749 uiApp.addApplicationService(appService);
753 } //namespace WidgetInstall