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/file_output.h>
40 #include <dpl/exception.h>
41 #include <dpl/foreach.h>
42 #include <dpl/sstream.h>
43 #include <dpl/string.h>
44 #include <dpl/optional.h>
45 #include <dpl/utils/wrt_utility.h>
47 #include <libxml_utils.h>
48 #include <pkgmgr/pkgmgr_parser.h>
49 #include <dpl/localization/LanguageTagsProvider.h>
51 #define DEFAULT_ICON_NAME "icon.png"
53 using namespace WrtDB;
56 typedef std::map<DPL::String, DPL::String> LanguageTagMap;
58 LanguageTagMap getLanguageTagMap()
62 #define ADD(tag, l_tag) map.insert(std::make_pair(L ## # tag, L ## # l_tag));
63 #include "languages.def"
69 DPL::OptionalString getLangTag(const DPL::String& tag)
71 static LanguageTagMap TagsMap =
74 DPL::String langTag = tag;
76 LogDebug("Trying to map language tag: " << langTag);
77 size_t pos = langTag.find_first_of(L'_');
78 if (pos != DPL::String::npos) {
81 DPL::OptionalString ret;
83 LanguageTagMap::iterator it = TagsMap.find(langTag);
84 if (it != TagsMap.end()) {
87 LogDebug("Mapping IANA Language tag to language tag: " <<
88 langTag << " -> " << ret);
95 namespace WidgetInstall {
97 const char * TaskManifestFile::encoding = "UTF-8";
99 TaskManifestFile::TaskManifestFile(InstallerContext &inCont) :
100 DPL::TaskDecl<TaskManifestFile>(this),
103 if (false == m_context.existingWidgetInfo.isExist) {
104 AddStep(&TaskManifestFile::stepCopyIconFiles);
105 AddStep(&TaskManifestFile::stepCreateExecFile);
106 AddStep(&TaskManifestFile::stepGenerateManifest);
107 AddStep(&TaskManifestFile::stepParseManifest);
108 AddStep(&TaskManifestFile::stepFinalize);
110 // for widget update.
111 AddStep(&TaskManifestFile::stepBackupIconFiles);
112 AddStep(&TaskManifestFile::stepCopyIconFiles);
113 AddStep(&TaskManifestFile::stepGenerateManifest);
114 AddStep(&TaskManifestFile::stepParseUpgradedManifest);
115 AddStep(&TaskManifestFile::stepUpdateFinalize);
117 AddAbortStep(&TaskManifestFile::stepAbortIconFiles);
121 TaskManifestFile::~TaskManifestFile()
125 void TaskManifestFile::stepCreateExecFile()
127 std::string exec = m_context.locations->getExecFile();
128 std::string clientExeStr = GlobalConfig::GetWrtClientExec();
130 LogInfo("link -s " << clientExeStr << " " << exec);
131 symlink(clientExeStr.c_str(), exec.c_str());
133 m_context.job->UpdateProgress(
134 InstallerContext::INSTALL_CREATE_EXECFILE,
135 "Widget execfile creation Finished");
138 void TaskManifestFile::stepCopyIconFiles()
140 LogDebug("CopyIconFiles");
142 //This function copies icon to desktop icon path. For each locale avaliable
143 //which there is at least one icon in widget for, icon file is copied.
144 //Coping prioritize last positions when coping. If there is several icons
145 //with given locale, the one, that will be copied, will be icon
146 //which is declared by <icon> tag later than the others in config.xml of widget
148 std::vector<Locale> generatedLocales;
150 WrtDB::WidgetRegisterInfo::LocalizedIconList & icons = m_context.widgetConfig.localizationData.icons;
152 //reversed: last <icon> has highest priority to be copied if it has given locale (TODO: why was that working that way?)
153 for(WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator icon = icons.rbegin(); icon != icons.rend(); icon++)
155 FOREACH(locale, icon->availableLocales)
157 DPL::String src = icon->src;
158 LogDebug("Icon for locale: " << *locale << "is : " << src);
160 if(std::find(generatedLocales.begin(), generatedLocales.end(), *locale) != generatedLocales.end())
162 LogDebug("Skipping - has that locale");
167 generatedLocales.push_back(*locale);
170 std::ostringstream sourceFile;
171 std::ostringstream targetFile;
173 sourceFile << m_context.locations->getSourceDir() << "/";
175 if (!locale->empty()) {
176 sourceFile << "locales/" << *locale << "/";
181 targetFile << GlobalConfig::GetUserWidgetDesktopIconPath() << "/";
182 targetFile << getIconTargetFilename(*locale);
184 if (m_context.locations->browserRequest())
186 m_context.locations->setIconTargetFilenameForLocale(targetFile.str());
189 LogDebug("Copying icon: " << sourceFile.str() <<
190 " -> " << targetFile.str());
192 icon_list.push_back(targetFile.str());
196 DPL::FileInput input(sourceFile.str());
197 DPL::FileOutput output(targetFile.str());
198 DPL::Copy(&input, &output);
201 Catch(DPL::FileInput::Exception::Base)
203 // Error while opening or closing source file
204 //ReThrowMsg(InstallerException::CopyIconFailed, sourceFile.str());
206 "Copying widget's icon failed. Widget's icon will not be"\
207 "available from Main Screen");
210 Catch(DPL::FileOutput::Exception::Base)
212 // Error while opening or closing target file
213 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
215 "Copying widget's icon failed. Widget's icon will not be"\
216 "available from Main Screen");
219 Catch(DPL::CopyFailed)
221 // Error while copying
222 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
224 "Copying widget's icon failed. Widget's icon will not be"\
225 "available from Main Screen");
230 m_context.job->UpdateProgress(
231 InstallerContext::INSTALL_COPY_ICONFILE,
232 "Widget iconfile copy Finished");
235 void TaskManifestFile::stepBackupIconFiles()
237 LogDebug("Backup Icon Files");
239 backup_dir << m_context.locations->getPackageInstallationDir();
240 backup_dir << "/" << "backup" << "/";
244 m_context.job->UpdateProgress(
245 InstallerContext::INSTALL_BACKUP_ICONFILE,
246 "New Widget icon file backup Finished");
249 void TaskManifestFile::stepAbortIconFiles()
251 LogDebug("Abrot Icon Files");
252 FOREACH(it, icon_list)
254 LogDebug("Remove Update Icon : " << (*it));
255 unlink((*it).c_str());
258 std::ostringstream b_icon_dir;
259 b_icon_dir << backup_dir.str() << "icons";
261 std::list<std::string> fileList;
262 getFileList(b_icon_dir.str().c_str(), fileList);
264 FOREACH(back_icon, fileList)
266 std::ostringstream res_file;
267 res_file << GlobalConfig::GetUserWidgetDesktopIconPath();
268 res_file << "/" << (*back_icon);
270 std::ostringstream backup_file;
271 backup_file << b_icon_dir.str() << "/" << (*back_icon);
275 DPL::FileInput input(backup_file.str());
276 DPL::FileOutput output(res_file.str());
277 DPL::Copy(&input, &output);
279 Catch(DPL::FileInput::Exception::Base)
281 LogError("Restoration icon File Failed." << backup_file.str()
282 << " to " << res_file.str());
285 Catch(DPL::FileOutput::Exception::Base)
287 LogError("Restoration icon File Failed." << backup_file.str()
288 << " to " << res_file.str());
290 Catch(DPL::CopyFailed)
292 LogError("Restoration icon File Failed." << backup_file.str()
293 << " to " << res_file.str());
298 void TaskManifestFile::stepUpdateFinalize()
301 LogDebug("Finished Update Desktopfile");
304 DPL::String TaskManifestFile::getIconTargetFilename(
305 const DPL::String& languageTag) const
307 DPL::OStringStream filename;
308 DPL::Optional<DPL::String> pkgname = m_context.widgetConfig.pkgname;
309 if (pkgname.IsNull()) {
310 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
313 filename << DPL::ToUTF8String(*pkgname).c_str();
315 if (!languageTag.empty()) {
316 DPL::OptionalString tag = getLangTag(languageTag); // translate en -> en_US etc
317 if (tag.IsNull()) { tag = languageTag; }
319 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
322 filename << L"." << languageTag;
324 filename << L"." << locale;
329 return filename.str();
332 void TaskManifestFile::stepFinalize()
335 LogInfo("Finished ManifestFile step");
339 void TaskManifestFile::saveLocalizedKey(std::ofstream &file,
340 const DPL::String& key,
341 const DPL::String& languageTag)
344 LanguageTagsProvider::BCP47LanguageTagToLocale(languageTag);
347 if (!locale.empty()) {
348 file << "[" << locale << "]";
353 void TaskManifestFile::updateAilInfo()
355 // Update ail for desktop
356 std::string cfgPkgname =
357 DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
358 const char* pkgname = cfgPkgname.c_str();
360 LogDebug("Update ail desktop : " << pkgname );
361 ail_appinfo_h ai = NULL;
364 ret = ail_package_get_appinfo(pkgname, &ai);
366 ail_package_destroy_appinfo(ai);
369 if (AIL_ERROR_NO_DATA == ret) {
370 if (ail_desktop_add(pkgname) < 0) {
371 LogDebug("Failed to add ail desktop : " << pkgname);
373 } else if (AIL_ERROR_OK == ret) {
374 if (ail_desktop_update(pkgname) < 0) {
375 LogDebug("Failed to update ail desktop : " << pkgname);
380 void TaskManifestFile::backupIconFiles()
382 LogInfo("Backup Icon Files");
384 std::ostringstream b_icon_dir;
385 b_icon_dir << backup_dir.str() << "icons";
387 LogDebug("Create icon backup folder : " << b_icon_dir.str());
388 WrtUtilMakeDir(b_icon_dir.str());
390 std::list<std::string> fileList;
391 getFileList(GlobalConfig::GetUserWidgetDesktopIconPath(), fileList);
392 std::string pkgname = DPL::ToUTF8String(*m_context.widgetConfig.pkgname);
394 FOREACH(it, fileList)
396 if (0 == (strncmp((*it).c_str(), pkgname.c_str(),
397 strlen(pkgname.c_str())))) {
398 std::ostringstream icon_file, backup_icon;
399 icon_file << GlobalConfig::GetUserWidgetDesktopIconPath();
400 icon_file << "/" << (*it);
402 backup_icon << b_icon_dir.str() << "/" << (*it);
404 LogDebug("Backup icon file " << icon_file.str() << " to " <<
408 DPL::FileInput input(icon_file.str());
409 DPL::FileOutput output(backup_icon.str());
410 DPL::Copy(&input, &output);
412 Catch(DPL::FileInput::Exception::Base)
414 LogError("Backup Desktop File Failed.");
415 ReThrowMsg(Exceptions::BackupFailed, icon_file.str());
418 Catch(DPL::FileOutput::Exception::Base)
420 LogError("Backup Desktop File Failed.");
421 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
423 Catch(DPL::CopyFailed)
425 LogError("Backup Desktop File Failed.");
426 ReThrowMsg(Exceptions::BackupFailed, backup_icon.str());
428 unlink((*it).c_str());
433 void TaskManifestFile::getFileList(const char* path,
434 std::list<std::string> &list)
436 DIR* dir = opendir(path);
438 LogError("icon directory doesn't exist");
439 ThrowMsg(Exceptions::InternalError, path);
442 struct dirent* d_ent;
444 if ((d_ent = readdir(dir))) {
445 if(strcmp(d_ent->d_name, ".") == 0 ||
446 strcmp(d_ent->d_name, "..") == 0) {
449 std::string file_name = d_ent->d_name;
450 list.push_back(file_name);
455 void TaskManifestFile::stepGenerateManifest()
457 DPL::String pkgname = *m_context.widgetConfig.pkgname;
458 manifest_name = pkgname + L".xml";
459 manifest_file += L"/tmp/" + manifest_name;
461 //libxml - init and check
462 LibxmlSingleton::Instance().init();
464 writeManifest(manifest_file);
466 m_context.job->UpdateProgress(
467 InstallerContext::INSTALL_CREATE_MANIFEST,
468 "Widget Manifest Creation Finished");
471 void TaskManifestFile::stepParseManifest()
473 int code = pkgmgr_parser_parse_manifest_for_installation(
474 DPL::ToUTF8String(manifest_file).c_str(), NULL);
478 LogError("Manifest parser error: " << code);
479 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
482 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
485 m_context.job->UpdateProgress(
486 InstallerContext::INSTALL_CREATE_MANIFEST,
487 "Widget Manifest Parsing Finished");
488 LogDebug("Manifest parsed");
491 void TaskManifestFile::stepParseUpgradedManifest()
493 int code = pkgmgr_parser_parse_manifest_for_upgrade(
494 DPL::ToUTF8String(manifest_file).c_str(), NULL);
498 LogError("Manifest parser error: " << code);
499 ThrowMsg(ManifestParsingError, "Parser returncode: " << code);
502 // TODO : It will be removed. AIL update is temporary code request by pkgmgr team.
505 m_context.job->UpdateProgress(
506 InstallerContext::INSTALL_CREATE_MANIFEST,
507 "Widget Manifest Parsing Finished");
508 LogDebug("Manifest parsed");
511 void TaskManifestFile::commitManifest()
513 LogDebug("Commiting manifest file : " << manifest_file);
515 std::ostringstream destFile;
516 destFile << "/opt/share/packages" << "/"; //TODO constant with path
517 destFile << DPL::ToUTF8String(manifest_name);
518 LogInfo("cp " << manifest_file << " " << destFile.str());
520 DPL::FileInput input(DPL::ToUTF8String(manifest_file));
521 DPL::FileOutput output(destFile.str());
522 DPL::Copy(&input, &output);
523 LogDebug("Manifest writen to: " << destFile.str());
526 unlink((DPL::ToUTF8String(manifest_file)).c_str());
527 manifest_file = DPL::FromUTF8String(destFile.str().c_str());
530 void TaskManifestFile::writeManifest(const DPL::String & path)
532 LogDebug("Generating manifest file : " << path);
536 setWidgetExecPath(uiApp);
537 setWidgetName(manifest, uiApp);
538 setWidgetIcons(uiApp);
539 setWidgetManifest(manifest);
540 setWidgetOtherInfo(uiApp);
541 setAppServiceInfo(uiApp);
543 manifest.addUiApplication(uiApp);
544 manifest.generate(path);
545 LogDebug("Manifest file serialized");
548 void TaskManifestFile::setWidgetExecPath(UiApplication & uiApp)
550 uiApp.setExec(DPL::FromASCIIString(m_context.locations->getExecFile()));
553 void TaskManifestFile::setWidgetName(Manifest & manifest, UiApplication & uiApp)
555 bool defaultNameSaved = false;
557 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
558 std::pair<DPL::String, WrtDB::ConfigParserData::LocalizedData> defaultLocalizedData;
560 FOREACH(localizedData, m_context.widgetConfig.configInfo.localizedDataSet)
562 Locale i = localizedData->first;
563 DPL::OptionalString tag = getLangTag(i); // translate en -> en_US etc
568 DPL::OptionalString name = localizedData->second.name;
569 generateWidgetName(manifest, uiApp, tag, name, defaultNameSaved);
571 //store default locale localized data
572 if(!!defaultLocale && defaultLocale == i)
574 defaultLocalizedData = *localizedData;
578 if (!!defaultLocale && !defaultNameSaved)
580 DPL::OptionalString name = defaultLocalizedData.second.name;
581 generateWidgetName(manifest, uiApp, DPL::OptionalString::Null, name, defaultNameSaved);
585 if(!!m_context.widgetConfig.pkgname)
587 pkgname = *m_context.widgetConfig.pkgname;
588 uiApp.setAppid(pkgname);
592 if(!!m_context.widgetConfig.guid) {
593 uiApp.setExtraid(*m_context.widgetConfig.guid);
595 if(!pkgname.empty()) {
596 uiApp.setExtraid(DPL::String(L"http://") + pkgname);
601 uiApp.setType(DPL::FromASCIIString("webapp"));
602 manifest.setType(L"wgt");
603 uiApp.setTaskmanage(true);
606 void TaskManifestFile::generateWidgetName(Manifest & manifest, UiApplication &uiApp, const DPL::OptionalString& tag, DPL::OptionalString name, bool & defaultNameSaved)
612 LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
614 if (!locale.empty()) {
615 uiApp.addLabel(LabelType(*name,*tag));
619 uiApp.addLabel(LabelType(*name));
620 manifest.addLabel(LabelType(*name));
625 defaultNameSaved = true;
626 uiApp.addLabel(LabelType(*name));
627 manifest.addLabel(LabelType(*name));
632 void TaskManifestFile::setWidgetIcons(UiApplication & uiApp)
634 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
635 if (pkgname.IsNull()) {
636 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
639 //TODO this file will need to be updated when user locale preferences
641 bool defaultIconSaved = false;
643 DPL::OptionalString defaultLocale = m_context.widgetConfig.configInfo.defaultlocale;
645 std::vector<Locale> generatedLocales;
646 WrtDB::WidgetRegisterInfo::LocalizedIconList & icons = m_context.widgetConfig.localizationData.icons;
648 //reversed: last <icon> has highest priority to be writen to manifest if it has given locale (TODO: why was that working that way?)
649 for(WrtDB::WidgetRegisterInfo::LocalizedIconList::const_reverse_iterator icon = icons.rbegin(); icon != icons.rend(); icon++)
651 FOREACH(locale, icon->availableLocales)
653 if(std::find(generatedLocales.begin(), generatedLocales.end(), *locale) != generatedLocales.end())
655 LogDebug("Skipping - has that locale - already in manifest");
660 generatedLocales.push_back(*locale);
663 DPL::OptionalString tag = getLangTag(*locale); // translate en -> en_US etc
664 if (tag.IsNull()) { tag = *locale; }
666 generateWidgetIcon(uiApp, tag, *locale, defaultIconSaved);
669 if (!!defaultLocale && !defaultIconSaved)
671 generateWidgetIcon(uiApp, DPL::OptionalString::Null,
677 void TaskManifestFile::generateWidgetIcon(UiApplication & uiApp, const DPL::OptionalString& tag,
678 const DPL::String& language, bool & defaultIconSaved)
683 locale = LanguageTagsProvider::BCP47LanguageTagToLocale(*tag);
687 defaultIconSaved = true;
690 DPL::String iconText;
691 iconText += getIconTargetFilename(language);
695 uiApp.addIcon(IconType(iconText, locale));
699 uiApp.addIcon(IconType(iconText));
703 void TaskManifestFile::setWidgetManifest(Manifest & manifest)
705 if(!!m_context.widgetConfig.pkgname)
707 manifest.setPackage(*m_context.widgetConfig.pkgname);
709 if(!!m_context.widgetConfig.version)
711 manifest.setVersion(*m_context.widgetConfig.version);
713 DPL::String email = (!!m_context.widgetConfig.configInfo.authorEmail ?
714 *m_context.widgetConfig.configInfo.authorEmail : L"");
715 DPL::String href = (!!m_context.widgetConfig.configInfo.authorHref ?
716 *m_context.widgetConfig.configInfo.authorHref : L"");
717 DPL::String name = (!!m_context.widgetConfig.configInfo.authorName ?
718 *m_context.widgetConfig.configInfo.authorName : L"");
719 manifest.addAuthor(Author(email,href,L"",name));
722 void TaskManifestFile::setWidgetOtherInfo(UiApplication & uiApp)
724 uiApp.setNodisplay(false);
726 //There is no "X-TIZEN-PackageType=wgt"
727 //There is no X-TIZEN-PackageID in manifest "X-TIZEN-PackageID=" << DPL::ToUTF8String(*widgetID).c_str()
728 //There is no Comment in pkgmgr "Comment=Widget application"
729 //that were in desktop file
732 void TaskManifestFile::setAppServiceInfo(UiApplication & uiApp)
734 WrtDB::ConfigParserData::ServiceInfoList appServiceList = m_context.widgetConfig.configInfo.appServiceList;
736 if (appServiceList.empty()) {
737 LogInfo("Widget doesn't contain application service");
741 // x-tizen-svc=http://tizen.org/appcontrol/operation/pick|NULL|image;
742 FOREACH(it, appServiceList) {
743 ApplicationService appService;
744 if (!it->m_operation.empty()) {
745 appService.addOperation(it->m_operation); //TODO: encapsulation?
747 if (!it->m_scheme.empty()) {
748 appService.addUri(it->m_scheme);
750 if (!it->m_mime.empty()) {
751 appService.addMime(it->m_mime);
753 uiApp.addApplicationService(appService);
757 } //namespace WidgetInstall