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::Optional<DPL::String> pkgname = m_context.widgetConfig.pkgname;
311 if (pkgname.IsNull()) {
312 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
315 filename << DPL::ToUTF8String(*pkgname).c_str();
317 if (!languageTag.empty()) {
318 DPL::OptionalString tag = getLangTag(languageTag); // translate en -> en_US etc
319 if (tag.IsNull()) { tag = languageTag; }
321 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
324 filename << L"." << languageTag;
326 filename << L"." << locale;
331 return filename.str();
334 void TaskManifestFile::stepFinalize()
337 LogInfo("Finished ManifestFile step");
341 void TaskManifestFile::saveLocalizedKey(std::ofstream &file,
342 const DPL::String& key,
343 const DPL::String& languageTag)
346 LanguageTagsProvider::BCP47LanguageTagToLocale(languageTag);
349 if (!locale.empty()) {
350 file << "[" << locale << "]";
355 void TaskManifestFile::updateAilInfo()
357 // Update ail for desktop
358 std::string cfgPkgname =
359 DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
360 const char* pkgname = cfgPkgname.c_str();
362 LogDebug("Update ail desktop : " << pkgname );
363 ail_appinfo_h ai = NULL;
366 ret = ail_package_get_appinfo(pkgname, &ai);
368 ail_package_destroy_appinfo(ai);
371 if (AIL_ERROR_NO_DATA == ret) {
372 if (ail_desktop_add(pkgname) < 0) {
373 LogWarning("Failed to add ail desktop : " << pkgname);
375 } else if (AIL_ERROR_OK == ret) {
376 if (ail_desktop_update(pkgname) < 0) {
377 LogWarning("Failed to update ail desktop : " << pkgname);
382 void TaskManifestFile::backupIconFiles()
384 LogInfo("Backup Icon Files");
386 std::ostringstream b_icon_dir;
387 b_icon_dir << backup_dir.str() << "icons";
389 LogDebug("Create icon backup folder : " << b_icon_dir.str());
390 WrtUtilMakeDir(b_icon_dir.str());
392 std::list<std::string> fileList;
393 getFileList(GlobalConfig::GetUserWidgetDesktopIconPath(), fileList);
394 std::string pkgname = DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
396 FOREACH(it, fileList)
398 if (0 == (strncmp((*it).c_str(), pkgname.c_str(),
399 strlen(pkgname.c_str())))) {
400 std::ostringstream icon_file, backup_icon;
401 icon_file << GlobalConfig::GetUserWidgetDesktopIconPath();
402 icon_file << "/" << (*it);
404 backup_icon << b_icon_dir.str() << "/" << (*it);
406 LogDebug("Backup icon file " << icon_file.str() << " to " <<
410 DPL::FileInput input(icon_file.str());
411 DPL::FileOutput output(backup_icon.str());
412 DPL::Copy(&input, &output);
414 Catch(DPL::FileInput::Exception::Base)
416 LogError("Backup Desktop File Failed.");
417 ReThrowMsg(Exceptions::BackupFailed, icon_file.str());
420 Catch(DPL::FileOutput::Exception::Base)
422 LogError("Backup Desktop File Failed.");
423 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
425 Catch(DPL::CopyFailed)
427 LogError("Backup Desktop File Failed.");
428 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
430 unlink((*it).c_str());
435 void TaskManifestFile::getFileList(const char* path,
436 std::list<std::string> &list)
438 DIR* dir = opendir(path);
440 LogError("icon directory doesn't exist");
441 ThrowMsg(Exceptions::InternalError, path);
444 struct dirent* d_ent;
446 if ((d_ent = readdir(dir))) {
447 if(strcmp(d_ent->d_name, ".") == 0 ||
448 strcmp(d_ent->d_name, "..") == 0) {
451 std::string file_name = d_ent->d_name;
452 list.push_back(file_name);
455 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
456 LogError("Failed to close dir: " << path << " with error: "
457 << DPL::GetErrnoString());
461 void TaskManifestFile::stepGenerateManifest()
463 DPL::String pkgname = *m_context.widgetConfig.pkgname;
464 manifest_name = pkgname + L".xml";
465 manifest_file += L"/tmp/" + manifest_name;
467 //libxml - init and check
468 LibxmlSingleton::Instance().init();
470 writeManifest(manifest_file);
472 m_context.job->UpdateProgress(
473 InstallerContext::INSTALL_CREATE_MANIFEST,
474 "Widget Manifest Creation Finished");
477 void TaskManifestFile::stepParseManifest()
479 int code = pkgmgr_parser_parse_manifest_for_installation(
480 DPL::ToUTF8String(manifest_file).c_str(), NULL);
484 LogError("Manifest parser error: " << code);
485 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
488 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
491 m_context.job->UpdateProgress(
492 InstallerContext::INSTALL_CREATE_MANIFEST,
493 "Widget Manifest Parsing Finished");
494 LogDebug("Manifest parsed");
497 void TaskManifestFile::stepParseUpgradedManifest()
499 int code = pkgmgr_parser_parse_manifest_for_upgrade(
500 DPL::ToUTF8String(manifest_file).c_str(), NULL);
504 LogError("Manifest parser error: " << code);
505 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
508 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
511 m_context.job->UpdateProgress(
512 InstallerContext::INSTALL_CREATE_MANIFEST,
513 "Widget Manifest Parsing Finished");
514 LogDebug("Manifest parsed");
517 void TaskManifestFile::commitManifest()
519 LogDebug("Commiting manifest file : " << manifest_file);
521 std::ostringstream destFile;
522 destFile << "/opt/share/packages" << "/"; //TODO constant with path
523 destFile << DPL::ToUTF8String(manifest_name);
524 LogInfo("cp " << manifest_file << " " << destFile.str());
526 DPL::FileInput input(DPL::ToUTF8String(manifest_file));
527 DPL::FileOutput output(destFile.str());
528 DPL::Copy(&input, &output);
529 LogDebug("Manifest writen to: " << destFile.str());
532 unlink((DPL::ToUTF8String(manifest_file)).c_str());
533 manifest_file = DPL::FromUTF8String(destFile.str().c_str());
536 void TaskManifestFile::writeManifest(const DPL::String & path)
538 LogDebug("Generating manifest file : " << path);
542 setWidgetExecPath(uiApp);
543 setWidgetName(manifest, uiApp);
544 setWidgetIcons(uiApp);
545 setWidgetManifest(manifest);
546 setWidgetOtherInfo(uiApp);
547 setAppServiceInfo(uiApp);
549 manifest.addUiApplication(uiApp);
550 manifest.generate(path);
551 LogDebug("Manifest file serialized");
554 void TaskManifestFile::setWidgetExecPath(UiApplication & uiApp)
556 uiApp.setExec(DPL::FromASCIIString(m_context.locations->getExecFile()));
559 void TaskManifestFile::setWidgetName(Manifest & manifest, UiApplication & uiApp)
561 bool defaultNameSaved = false;
563 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
564 std::pair<DPL::String, WrtDB::ConfigParserData::LocalizedData> defaultLocalizedData;
566 FOREACH(localizedData, m_context.widgetConfig.configInfo.localizedDataSet)
568 Locale i = localizedData->first;
569 DPL::OptionalString tag = getLangTag(i); // translate en -> en_US etc
574 DPL::OptionalString name = localizedData->second.name;
575 generateWidgetName(manifest, uiApp, tag, name, defaultNameSaved);
577 //store default locale localized data
578 if(!!defaultLocale && defaultLocale == i)
580 defaultLocalizedData = *localizedData;
584 if (!!defaultLocale && !defaultNameSaved)
586 DPL::OptionalString name = defaultLocalizedData.second.name;
587 generateWidgetName(manifest, uiApp, DPL::OptionalString::Null, name, defaultNameSaved);
591 if(!!m_context.widgetConfig.pkgname)
593 pkgname = *m_context.widgetConfig.pkgname;
594 uiApp.setAppid(pkgname);
598 if(!!m_context.widgetConfig.guid) {
599 uiApp.setExtraid(*m_context.widgetConfig.guid);
601 if(!pkgname.empty()) {
602 uiApp.setExtraid(DPL::String(L"http://") + pkgname);
607 uiApp.setType(DPL::FromASCIIString("webapp"));
608 manifest.setType(L"wgt");
609 uiApp.setTaskmanage(true);
612 void TaskManifestFile::generateWidgetName(Manifest & manifest, UiApplication &uiApp, const DPL::OptionalString& tag, DPL::OptionalString name, bool & defaultNameSaved)
618 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
620 if (!locale.empty()) {
621 uiApp.addLabel(LabelType(*name,*tag));
625 uiApp.addLabel(LabelType(*name));
626 manifest.addLabel(LabelType(*name));
631 defaultNameSaved = true;
632 uiApp.addLabel(LabelType(*name));
633 manifest.addLabel(LabelType(*name));
638 void TaskManifestFile::setWidgetIcons(UiApplication & uiApp)
640 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
641 if (pkgname.IsNull()) {
642 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
645 //TODO this file will need to be updated when user locale preferences
647 bool defaultIconSaved = false;
649 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
651 std::vector<Locale> generatedLocales;
652 WrtDB::WidgetRegisterInfo::LocalizedIconList & icons = m_context.widgetConfig.localizationData.icons;
654 //reversed: last <icon> has highest priority to be writen to manifest if it has given locale (TODO: why was that working that way?)
655 for(WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator icon = icons.rbegin(); icon != icons.rend(); icon++)
657 FOREACH(locale, icon->availableLocales)
659 if(std::find(generatedLocales.begin(), generatedLocales.end(), *locale) != generatedLocales.end())
661 LogDebug("Skipping - has that locale - already in manifest");
666 generatedLocales.push_back(*locale);
669 DPL::OptionalString tag = getLangTag(*locale); // translate en -> en_US etc
670 if (tag.IsNull()) { tag = *locale; }
672 generateWidgetIcon(uiApp, tag, *locale, defaultIconSaved);
675 if (!!defaultLocale && !defaultIconSaved)
677 generateWidgetIcon(uiApp, DPL::OptionalString::Null,
683 void TaskManifestFile::generateWidgetIcon(UiApplication & uiApp, const DPL::OptionalString& tag,
684 const DPL::String& language, bool & defaultIconSaved)
689 locale = LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
693 defaultIconSaved = true;
696 DPL::String iconText;
697 iconText += getIconTargetFilename(language);
701 uiApp.addIcon(IconType(iconText, locale));
705 uiApp.addIcon(IconType(iconText));
709 void TaskManifestFile::setWidgetManifest(Manifest & manifest)
711 if(!!m_context.widgetConfig.pkgname)
713 manifest.setPackage(*m_context.widgetConfig.pkgname);
715 if(!!m_context.widgetConfig.version)
717 manifest.setVersion(*m_context.widgetConfig.version);
719 DPL::String email = (!!m_context.widgetConfig.configInfo.authorEmail ?
720 *m_context.widgetConfig.configInfo.authorEmail : L"");
721 DPL::String href = (!!m_context.widgetConfig.configInfo.authorHref ?
722 *m_context.widgetConfig.configInfo.authorHref : L"");
723 DPL::String name = (!!m_context.widgetConfig.configInfo.authorName ?
724 *m_context.widgetConfig.configInfo.authorName : L"");
725 manifest.addAuthor(Author(email,href,L"",name));
728 void TaskManifestFile::setWidgetOtherInfo(UiApplication & uiApp)
730 uiApp.setNodisplay(false);
732 //There is no "X-TIZEN-PackageType=wgt"
733 //There is no X-TIZEN-PackageID in manifest "X-TIZEN-PackageID=" << DPL::ToUTF8String(*widgetID).c_str()
734 //There is no Comment in pkgmgr "Comment=Widget application"
735 //that were in desktop file
738 void TaskManifestFile::setAppServiceInfo(UiApplication & uiApp)
740 WrtDB::ConfigParserData::ServiceInfoList appServiceList = m_context.widgetConfig.configInfo.appServiceList;
742 if (appServiceList.empty()) {
743 LogInfo("Widget doesn't contain application service");
747 // x-tizen-svc=http://tizen.org/appcontrol/operation/pick|NULL|image;
748 FOREACH(it, appServiceList) {
749 ApplicationService appService;
750 if (!it->m_operation.empty()) {
751 appService.addOperation(it->m_operation); //TODO: encapsulation?
753 if (!it->m_scheme.empty()) {
754 appService.addUri(it->m_scheme);
756 if (!it->m_mime.empty()) {
757 appService.addMime(it->m_mime);
759 uiApp.addApplicationService(appService);
763 } //namespace WidgetInstall