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);
185 if (m_context.locations->browserRequest())
187 m_context.locations->setIconTargetFilenameForLocale(targetFile.str());
190 LogDebug("Copying icon: " << sourceFile.str() <<
191 " -> " << targetFile.str());
193 icon_list.push_back(targetFile.str());
197 DPL::FileInput input(sourceFile.str());
198 DPL::FileOutput output(targetFile.str());
199 DPL::Copy(&input, &output);
202 Catch(DPL::FileInput::Exception::Base)
204 // Error while opening or closing source file
205 //ReThrowMsg(InstallerException::CopyIconFailed, sourceFile.str());
207 "Copying widget's icon failed. Widget's icon will not be"\
208 "available from Main Screen");
211 Catch(DPL::FileOutput::Exception::Base)
213 // Error while opening or closing target file
214 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
216 "Copying widget's icon failed. Widget's icon will not be"\
217 "available from Main Screen");
220 Catch(DPL::CopyFailed)
222 // Error while copying
223 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
225 "Copying widget's icon failed. Widget's icon will not be"\
226 "available from Main Screen");
231 m_context.job->UpdateProgress(
232 InstallerContext::INSTALL_COPY_ICONFILE,
233 "Widget iconfile copy Finished");
236 void TaskManifestFile::stepBackupIconFiles()
238 LogDebug("Backup Icon Files");
240 backup_dir << m_context.locations->getPackageInstallationDir();
241 backup_dir << "/" << "backup" << "/";
245 m_context.job->UpdateProgress(
246 InstallerContext::INSTALL_BACKUP_ICONFILE,
247 "New Widget icon file backup Finished");
250 void TaskManifestFile::stepAbortIconFiles()
252 LogDebug("Abrot Icon Files");
253 FOREACH(it, icon_list)
255 LogDebug("Remove Update Icon : " << (*it));
256 unlink((*it).c_str());
259 std::ostringstream b_icon_dir;
260 b_icon_dir << backup_dir.str() << "icons";
262 std::list<std::string> fileList;
263 getFileList(b_icon_dir.str().c_str(), fileList);
265 FOREACH(back_icon, fileList)
267 std::ostringstream res_file;
268 res_file << GlobalConfig::GetUserWidgetDesktopIconPath();
269 res_file << "/" << (*back_icon);
271 std::ostringstream backup_file;
272 backup_file << b_icon_dir.str() << "/" << (*back_icon);
276 DPL::FileInput input(backup_file.str());
277 DPL::FileOutput output(res_file.str());
278 DPL::Copy(&input, &output);
280 Catch(DPL::FileInput::Exception::Base)
282 LogError("Restoration icon File Failed." << backup_file.str()
283 << " to " << res_file.str());
286 Catch(DPL::FileOutput::Exception::Base)
288 LogError("Restoration icon File Failed." << backup_file.str()
289 << " to " << res_file.str());
291 Catch(DPL::CopyFailed)
293 LogError("Restoration icon File Failed." << backup_file.str()
294 << " to " << res_file.str());
299 void TaskManifestFile::stepUpdateFinalize()
302 LogDebug("Finished Update Desktopfile");
305 DPL::String TaskManifestFile::getIconTargetFilename(
306 const DPL::String& languageTag) const
308 DPL::OStringStream filename;
309 DPL::Optional<DPL::String> pkgname = m_context.widgetConfig.pkgname;
310 if (pkgname.IsNull()) {
311 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
314 filename << DPL::ToUTF8String(*pkgname).c_str();
316 if (!languageTag.empty()) {
317 DPL::OptionalString tag = getLangTag(languageTag); // translate en -> en_US etc
318 if (tag.IsNull()) { tag = languageTag; }
320 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
323 filename << L"." << languageTag;
325 filename << L"." << locale;
330 return filename.str();
333 void TaskManifestFile::stepFinalize()
336 LogInfo("Finished ManifestFile step");
340 void TaskManifestFile::saveLocalizedKey(std::ofstream &file,
341 const DPL::String& key,
342 const DPL::String& languageTag)
345 LanguageTagsProvider::BCP47LanguageTagToLocale(languageTag);
348 if (!locale.empty()) {
349 file << "[" << locale << "]";
354 void TaskManifestFile::updateAilInfo()
356 // Update ail for desktop
357 std::string cfgPkgname =
358 DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
359 const char* pkgname = cfgPkgname.c_str();
361 LogDebug("Update ail desktop : " << pkgname );
362 ail_appinfo_h ai = NULL;
365 ret = ail_package_get_appinfo(pkgname, &ai);
367 ail_package_destroy_appinfo(ai);
370 if (AIL_ERROR_NO_DATA == ret) {
371 if (ail_desktop_add(pkgname) < 0) {
372 LogWarning("Failed to add ail desktop : " << pkgname);
374 } else if (AIL_ERROR_OK == ret) {
375 if (ail_desktop_update(pkgname) < 0) {
376 LogWarning("Failed to update ail desktop : " << pkgname);
381 void TaskManifestFile::backupIconFiles()
383 LogInfo("Backup Icon Files");
385 std::ostringstream b_icon_dir;
386 b_icon_dir << backup_dir.str() << "icons";
388 LogDebug("Create icon backup folder : " << b_icon_dir.str());
389 WrtUtilMakeDir(b_icon_dir.str());
391 std::list<std::string> fileList;
392 getFileList(GlobalConfig::GetUserWidgetDesktopIconPath(), fileList);
393 std::string pkgname = DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
395 FOREACH(it, fileList)
397 if (0 == (strncmp((*it).c_str(), pkgname.c_str(),
398 strlen(pkgname.c_str())))) {
399 std::ostringstream icon_file, backup_icon;
400 icon_file << GlobalConfig::GetUserWidgetDesktopIconPath();
401 icon_file << "/" << (*it);
403 backup_icon << b_icon_dir.str() << "/" << (*it);
405 LogDebug("Backup icon file " << icon_file.str() << " to " <<
409 DPL::FileInput input(icon_file.str());
410 DPL::FileOutput output(backup_icon.str());
411 DPL::Copy(&input, &output);
413 Catch(DPL::FileInput::Exception::Base)
415 LogError("Backup Desktop File Failed.");
416 ReThrowMsg(Exceptions::BackupFailed, icon_file.str());
419 Catch(DPL::FileOutput::Exception::Base)
421 LogError("Backup Desktop File Failed.");
422 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
424 Catch(DPL::CopyFailed)
426 LogError("Backup Desktop File Failed.");
427 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
429 unlink((*it).c_str());
434 void TaskManifestFile::getFileList(const char* path,
435 std::list<std::string> &list)
437 DIR* dir = opendir(path);
439 LogError("icon directory doesn't exist");
440 ThrowMsg(Exceptions::InternalError, path);
443 struct dirent* d_ent;
445 if ((d_ent = readdir(dir))) {
446 if(strcmp(d_ent->d_name, ".") == 0 ||
447 strcmp(d_ent->d_name, "..") == 0) {
450 std::string file_name = d_ent->d_name;
451 list.push_back(file_name);
454 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
455 LogError("Failed to close dir: " << path << " with error: "
456 << DPL::GetErrnoString());
460 void TaskManifestFile::stepGenerateManifest()
462 DPL::String pkgname = *m_context.widgetConfig.pkgname;
463 manifest_name = pkgname + L".xml";
464 manifest_file += L"/tmp/" + manifest_name;
466 //libxml - init and check
467 LibxmlSingleton::Instance().init();
469 writeManifest(manifest_file);
471 m_context.job->UpdateProgress(
472 InstallerContext::INSTALL_CREATE_MANIFEST,
473 "Widget Manifest Creation Finished");
476 void TaskManifestFile::stepParseManifest()
478 int code = pkgmgr_parser_parse_manifest_for_installation(
479 DPL::ToUTF8String(manifest_file).c_str(), NULL);
483 LogError("Manifest parser error: " << code);
484 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
487 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
490 m_context.job->UpdateProgress(
491 InstallerContext::INSTALL_CREATE_MANIFEST,
492 "Widget Manifest Parsing Finished");
493 LogDebug("Manifest parsed");
496 void TaskManifestFile::stepParseUpgradedManifest()
498 int code = pkgmgr_parser_parse_manifest_for_upgrade(
499 DPL::ToUTF8String(manifest_file).c_str(), NULL);
503 LogError("Manifest parser error: " << code);
504 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
507 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
510 m_context.job->UpdateProgress(
511 InstallerContext::INSTALL_CREATE_MANIFEST,
512 "Widget Manifest Parsing Finished");
513 LogDebug("Manifest parsed");
516 void TaskManifestFile::commitManifest()
518 LogDebug("Commiting manifest file : " << manifest_file);
520 std::ostringstream destFile;
521 destFile << "/opt/share/packages" << "/"; //TODO constant with path
522 destFile << DPL::ToUTF8String(manifest_name);
523 LogInfo("cp " << manifest_file << " " << destFile.str());
525 DPL::FileInput input(DPL::ToUTF8String(manifest_file));
526 DPL::FileOutput output(destFile.str());
527 DPL::Copy(&input, &output);
528 LogDebug("Manifest writen to: " << destFile.str());
531 unlink((DPL::ToUTF8String(manifest_file)).c_str());
532 manifest_file = DPL::FromUTF8String(destFile.str().c_str());
535 void TaskManifestFile::writeManifest(const DPL::String & path)
537 LogDebug("Generating manifest file : " << path);
541 setWidgetExecPath(uiApp);
542 setWidgetName(manifest, uiApp);
543 setWidgetIcons(uiApp);
544 setWidgetManifest(manifest);
545 setWidgetOtherInfo(uiApp);
546 setAppServiceInfo(uiApp);
548 manifest.addUiApplication(uiApp);
549 manifest.generate(path);
550 LogDebug("Manifest file serialized");
553 void TaskManifestFile::setWidgetExecPath(UiApplication & uiApp)
555 uiApp.setExec(DPL::FromASCIIString(m_context.locations->getExecFile()));
558 void TaskManifestFile::setWidgetName(Manifest & manifest, UiApplication & uiApp)
560 bool defaultNameSaved = false;
562 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
563 std::pair<DPL::String, WrtDB::ConfigParserData::LocalizedData> defaultLocalizedData;
565 FOREACH(localizedData, m_context.widgetConfig.configInfo.localizedDataSet)
567 Locale i = localizedData->first;
568 DPL::OptionalString tag = getLangTag(i); // translate en -> en_US etc
573 DPL::OptionalString name = localizedData->second.name;
574 generateWidgetName(manifest, uiApp, tag, name, defaultNameSaved);
576 //store default locale localized data
577 if(!!defaultLocale && defaultLocale == i)
579 defaultLocalizedData = *localizedData;
583 if (!!defaultLocale && !defaultNameSaved)
585 DPL::OptionalString name = defaultLocalizedData.second.name;
586 generateWidgetName(manifest, uiApp, DPL::OptionalString::Null, name, defaultNameSaved);
590 if(!!m_context.widgetConfig.pkgname)
592 pkgname = *m_context.widgetConfig.pkgname;
593 uiApp.setAppid(pkgname);
597 if(!!m_context.widgetConfig.guid) {
598 uiApp.setExtraid(*m_context.widgetConfig.guid);
600 if(!pkgname.empty()) {
601 uiApp.setExtraid(DPL::String(L"http://") + pkgname);
606 uiApp.setType(DPL::FromASCIIString("webapp"));
607 manifest.setType(L"wgt");
608 uiApp.setTaskmanage(true);
611 void TaskManifestFile::generateWidgetName(Manifest & manifest, UiApplication &uiApp, const DPL::OptionalString& tag, DPL::OptionalString name, bool & defaultNameSaved)
617 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
619 if (!locale.empty()) {
620 uiApp.addLabel(LabelType(*name,*tag));
624 uiApp.addLabel(LabelType(*name));
625 manifest.addLabel(LabelType(*name));
630 defaultNameSaved = true;
631 uiApp.addLabel(LabelType(*name));
632 manifest.addLabel(LabelType(*name));
637 void TaskManifestFile::setWidgetIcons(UiApplication & uiApp)
639 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
640 if (pkgname.IsNull()) {
641 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
644 //TODO this file will need to be updated when user locale preferences
646 bool defaultIconSaved = false;
648 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
650 std::vector<Locale> generatedLocales;
651 WrtDB::WidgetRegisterInfo::LocalizedIconList & icons = m_context.widgetConfig.localizationData.icons;
653 //reversed: last <icon> has highest priority to be writen to manifest if it has given locale (TODO: why was that working that way?)
654 for(WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator icon = icons.rbegin(); icon != icons.rend(); icon++)
656 FOREACH(locale, icon->availableLocales)
658 if(std::find(generatedLocales.begin(), generatedLocales.end(), *locale) != generatedLocales.end())
660 LogDebug("Skipping - has that locale - already in manifest");
665 generatedLocales.push_back(*locale);
668 DPL::OptionalString tag = getLangTag(*locale); // translate en -> en_US etc
669 if (tag.IsNull()) { tag = *locale; }
671 generateWidgetIcon(uiApp, tag, *locale, defaultIconSaved);
674 if (!!defaultLocale && !defaultIconSaved)
676 generateWidgetIcon(uiApp, DPL::OptionalString::Null,
682 void TaskManifestFile::generateWidgetIcon(UiApplication & uiApp, const DPL::OptionalString& tag,
683 const DPL::String& language, bool & defaultIconSaved)
688 locale = LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
692 defaultIconSaved = true;
695 DPL::String iconText;
696 iconText += getIconTargetFilename(language);
700 uiApp.addIcon(IconType(iconText, locale));
704 uiApp.addIcon(IconType(iconText));
708 void TaskManifestFile::setWidgetManifest(Manifest & manifest)
710 if(!!m_context.widgetConfig.pkgname)
712 manifest.setPackage(*m_context.widgetConfig.pkgname);
714 if(!!m_context.widgetConfig.version)
716 manifest.setVersion(*m_context.widgetConfig.version);
718 DPL::String email = (!!m_context.widgetConfig.configInfo.authorEmail ?
719 *m_context.widgetConfig.configInfo.authorEmail : L"");
720 DPL::String href = (!!m_context.widgetConfig.configInfo.authorHref ?
721 *m_context.widgetConfig.configInfo.authorHref : L"");
722 DPL::String name = (!!m_context.widgetConfig.configInfo.authorName ?
723 *m_context.widgetConfig.configInfo.authorName : L"");
724 manifest.addAuthor(Author(email,href,L"",name));
727 void TaskManifestFile::setWidgetOtherInfo(UiApplication & uiApp)
729 uiApp.setNodisplay(false);
731 //There is no "X-TIZEN-PackageType=wgt"
732 //There is no X-TIZEN-PackageID in manifest "X-TIZEN-PackageID=" << DPL::ToUTF8String(*widgetID).c_str()
733 //There is no Comment in pkgmgr "Comment=Widget application"
734 //that were in desktop file
737 void TaskManifestFile::setAppServiceInfo(UiApplication & uiApp)
739 WrtDB::ConfigParserData::ServiceInfoList appServiceList = m_context.widgetConfig.configInfo.appServiceList;
741 if (appServiceList.empty()) {
742 LogInfo("Widget doesn't contain application service");
746 // x-tizen-svc=http://tizen.org/appcontrol/operation/pick|NULL|image;
747 FOREACH(it, appServiceList) {
748 ApplicationService appService;
749 if (!it->m_operation.empty()) {
750 appService.addOperation(it->m_operation); //TODO: encapsulation?
752 if (!it->m_scheme.empty()) {
753 appService.addUri(it->m_scheme);
755 if (!it->m_mime.empty()) {
756 appService.addMime(it->m_mime);
758 uiApp.addApplicationService(appService);
762 } //namespace WidgetInstall