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_desktop_file.cpp
18 * @author Pawel Sikorski (p.sikorski@samgsung.com)
25 #include <dpl/assert.h>
28 #include <widget_install/task_desktop_file.h>
29 #include <widget_install/job_widget_install.h>
30 #include <widget_install/widget_install_errors.h>
31 #include <widget_install/widget_install_context.h>
32 #include <dpl/wrt-dao-ro/global_config.h>
33 #include <dpl/log/log.h>
34 #include <dpl/file_input.h>
35 #include <dpl/file_output.h>
37 #include <dpl/exception.h>
38 #include <dpl/foreach.h>
39 #include <dpl/sstream.h>
40 #include <dpl/string.h>
41 #include <dpl/optional.h>
44 using namespace WrtDB;
47 typedef std::map<DPL::String, DPL::String> LanguageTagMap;
49 LanguageTagMap getLanguageTagMap()
53 #define ADD(tag, l_tag) map.insert(std::make_pair(L ## # tag, L ## # l_tag));
54 #include "languages.def"
60 DPL::OptionalString getLangTag(const DPL::String& tag)
62 static LanguageTagMap TagsMap =
65 DPL::String langTag = tag;
67 LogDebug("Trying to map language tag: " << langTag);
68 size_t pos = langTag.find_first_of(L'_');
69 if (pos != DPL::String::npos) {
72 DPL::OptionalString ret;
74 LanguageTagMap::iterator it = TagsMap.find(langTag);
75 if (it != TagsMap.end()) {
78 LogDebug("Mapping IANA Language tag to language tag: " <<
79 langTag << " -> " << ret);
86 namespace WidgetInstall {
87 TaskDesktopFile::TaskDesktopFile(InstallerContext &inCont) :
88 DPL::TaskDecl<TaskDesktopFile>(this),
91 AddStep(&TaskDesktopFile::stepCopyIconFiles);
92 AddStep(&TaskDesktopFile::stepCreateDesktopFile);
93 AddStep(&TaskDesktopFile::stepCreateExecFile);
94 AddStep(&TaskDesktopFile::stepFinalize);
97 TaskDesktopFile::~TaskDesktopFile()
101 void TaskDesktopFile::stepCreateDesktopFile()
103 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
104 desktop_name << pkgname << ".desktop";
105 desktop_file << "/tmp/" << desktop_name.str();
106 LogInfo("desktop file : " << desktop_file.str());
107 std::ofstream file(desktop_file.str().c_str());
109 saveWidgetType(file);
110 saveWidgetExecPath(file);
111 saveWidgetName(file);
112 saveWidgetIcons(file);
113 saveWidgetVersion(file);
114 saveWidgetOtherInfo(file);
115 saveAppServiceInfo(file);
121 m_context.job->UpdateProgress(
122 InstallerContext::INSTALL_CREATE_DESKTOP,
123 "Widget Desktop Creation Finished");
126 void TaskDesktopFile::stepCreateExecFile()
128 //ln -s /usr/bin/wrt-client {widget-handle}
130 std::ostringstream real_path;
131 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
132 if (pkgname.IsNull()) {
133 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
136 real_path << GlobalConfig::GetUserInstalledWidgetPath() << "/";
137 real_path << pkgname << "/";
138 real_path << GlobalConfig::GetUserWidgetExecPath() << "/" <<
139 m_context.widgetHandle;
140 std::string wrt_client = GlobalConfig::GetWrtClientExec();
142 LogInfo("link -s " << wrt_client << " " << real_path.str());
143 symlink(wrt_client.c_str(), real_path.str().c_str());
145 m_context.job->UpdateProgress(
146 InstallerContext::INSTALL_CREATE_EXECFILE,
147 "Widget execfile creation Finished");
150 void TaskDesktopFile::stepCopyIconFiles()
152 LogDebug("CopyIconFiles");
154 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
155 if (pkgname.IsNull()) {
156 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
159 Assert(!!m_context.widgetHandle);
160 WidgetDAO dao(*m_context.widgetHandle);
161 WidgetDAO::WidgetLocalizedIconList locList = dao.getLocalizedIconList();
162 WidgetDAO::WidgetIconList list = dao.getIconList();
165 DPL::String i = it->widgetLocale;
166 DPL::OptionalString src;
169 if (icon->iconId == it->iconId) {
173 LogDebug("Icon for locale: " << i << "is : " << src);
175 std::ostringstream sourceFile;
176 std::ostringstream targetFile;
179 sourceFile << GlobalConfig::GetUserInstalledWidgetPath() << "/";
180 sourceFile << pkgname << "/";
181 sourceFile << GlobalConfig::GetWidgetSrcPath() << "/";
183 targetFile << GlobalConfig::GetUserInstalledWidgetPath() << "/";
184 targetFile << pkgname << "/";
185 targetFile << GlobalConfig::GetUserWidgetDesktopIconPath() << "/";
188 sourceFile << "locales/" << i << "/";
191 targetFile << getIconTargetFilename(i);
194 //Use WRT default (not from the widget) only if widget default (not
195 // localized) doesn't exist.
197 LogError("Using Default Icon for widget");
198 sourceFile << GlobalConfig::GetUserWidgetDefaultIconFile();
204 LogDebug("Copying icon: " << sourceFile.str() <<
205 " -> " << targetFile.str());
209 DPL::FileInput input(sourceFile.str());
210 DPL::FileOutput output(targetFile.str());
211 DPL::Copy(&input, &output);
214 Catch(DPL::FileInput::Exception::Base)
216 // Error while opening or closing source file
217 //ReThrowMsg(InstallerException::CopyIconFailed, sourceFile.str());
219 "Copying widget's icon failed. Widget's icon will not be"\
220 "available from Main Screen");
223 Catch(DPL::FileOutput::Exception::Base)
225 // Error while opening or closing target file
226 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
228 "Copying widget's icon failed. Widget's icon will not be"\
229 "available from Main Screen");
232 Catch(DPL::CopyFailed)
234 // Error while copying
235 //ReThrowMsg(InstallerException::CopyIconFailed, targetFile.str());
237 "Copying widget's icon failed. Widget's icon will not be"\
238 "available from Main Screen");
242 m_context.job->UpdateProgress(
243 InstallerContext::INSTALL_COPY_ICONFILE,
244 "Widget iconfile copy Finished");
247 void TaskDesktopFile::moveDesktopFile()
249 std::ostringstream destFile;
250 destFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
251 destFile << desktop_name.str();
252 LogInfo("cp " << desktop_file.str() << " " << destFile.str());
255 DPL::FileInput input(desktop_file.str());
256 DPL::FileOutput output(destFile.str());
257 DPL::Copy(&input, &output);
260 Catch(DPL::FileInput::Exception::Base)
262 // Error while opening or closing source file
263 // ReThrowMsg(InstallerException::CopyIconFailed,
264 // desktop_file.str());
266 "Creating Desktop File Failed. Widget's icon will not be available"\
270 Catch(DPL::FileOutput::Exception::Base)
272 // Error while opening or closing target file
273 // ReThrowMsg(InstallerException::CopyIconFailed,
276 "Creating Desktop File Failed. Widget's icon will not be available"\
280 Catch(DPL::CopyFailed)
282 // Error while copying
283 // ReThrowMsg(InstallerException::CopyIconFailed,
286 "Creating Desktop File Failed. Widget's icon will not be available"\
291 unlink(desktop_file.str().c_str());
294 void TaskDesktopFile::saveWidgetType(std::ofstream &file)
296 file << "Type=" << "Application" << std::endl;
299 void TaskDesktopFile::saveWidgetExecPath(std::ofstream &file)
301 DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
302 if (pkgname.IsNull()) {
303 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
307 file << GlobalConfig::GetUserInstalledWidgetPath() << "/";
308 file << pkgname << "/";
309 file << GlobalConfig::GetUserWidgetExecPath() << "/";
310 file << m_context.widgetHandle << std::endl;
313 void TaskDesktopFile::saveWidgetVersion(std::ofstream &file)
315 DPL::OptionalString widget_version = m_context.widgetConfig.version;
316 file << "Version=" << widget_version << std::endl;
319 void TaskDesktopFile::saveWidgetName(std::ofstream &file)
321 Assert(!!m_context.widgetHandle);
322 WidgetDAO dao(*m_context.widgetHandle);
323 LanguageTagsList languageTags(dao.getLanguageTags());
324 FOREACH(i, languageTags)
326 DPL::OptionalString tag = getLangTag(*i);// translate en -> en_US etc
327 if (tag.IsNull()) { tag = *i; }
329 saveLocalizedKey(file, L"Name", *tag);
331 DPL::OptionalString name = dao.getLocalizedInfo(*i).name;
335 file << "Widget " << *m_context.widgetHandle;
341 void TaskDesktopFile::saveWidgetIcons(std::ofstream &file)
343 //TODO this file will need to be updated when user locale preferences
345 Assert(!!m_context.widgetHandle);
346 WidgetDAO dao(*m_context.widgetHandle);
348 WidgetDAO::WidgetLocalizedIconList locList = dao.getLocalizedIconList();
349 WidgetDAO::WidgetIconList list = dao.getIconList();
351 LanguageTagsList languageTags(dao.getLanguageTags());
354 DPL::String i = it->widgetLocale;
355 DPL::OptionalString tag = getLangTag(i); // translate en -> en_US etc
356 if (tag.IsNull()) { tag = i; }
358 saveLocalizedKey(file, L"Icon", *tag);
360 DPL::OptionalString src;
363 if (icon->iconId == it->iconId) {
368 //If menuscreen need use absolute path of widget's icon, comment out
369 //the following lines.
370 //file << GlobalConfig::GetUserInstalledWidgetPath() << "/";
371 //file << WRT_WIDGET_PKGNAME_PREFIX << m_context.widgetHandle
373 //file << GlobalConfig::GetUserWidgetDesktopIconPath() << "/";
374 file << getIconTargetFilename(i) << std::endl;
379 DPL::String TaskDesktopFile::getIconTargetFilename(
380 const DPL::String& languageTag) const
382 DPL::OStringStream filename;
383 DPL::Optional<DPL::String> pkgname = m_context.widgetConfig.pkgname;
384 if (pkgname.IsNull()) {
385 ThrowMsg(Exceptions::InternalError, "No Package name exists.");
388 filename << DPL::ToUTF8String(*pkgname).c_str();
390 if (!languageTag.empty()) {
391 DPL::OptionalString tag = getLangTag(languageTag); // translate en -> en_US etc
392 if (tag.IsNull()) { tag = languageTag; }
394 LocalizationUtils::BCP47LanguageTagToLocale(*tag);
397 filename << L"." << languageTag;
399 filename << L"." << locale;
404 return filename.str();
407 void TaskDesktopFile::saveWidgetOtherInfo(std::ofstream &file)
409 DPL::Optional<DPL::String> widgetID = m_context.widgetConfig.guid;
412 // strncat(desktop, format_network, strlen(format_network));
413 // //TODO -- get the network value from the widget
414 // strncat(desktop, "True", 4);
415 // strncat(desktop, line, strlen(line));
418 file << "Comment=Widget application" << std::endl;
421 //file << "BG_SCHEDULE=True" << std::endl;
424 file << "Visible=True" << std::endl;
426 file << "X-SLP-BaseLayoutWidth=720" << std::endl;
427 file << "X-SLP-BaseLayoutHeight=1280" << std::endl;
428 file << "X-SLP-IsHorizontalScale=True" << std::endl;
429 file << "X-SLP-PackageType=wgt" << std::endl;
430 if (!widgetID.IsNull()) {
431 file << "X-SLP-PackageID=" << DPL::ToUTF8String(*widgetID).c_str() << std::endl;
435 void TaskDesktopFile::saveAppServiceInfo(std::ofstream &file)
437 Assert(!!m_context.widgetHandle);
438 WidgetDAOReadOnly dao(*m_context.widgetHandle);
439 WidgetApplicationServiceList appServiceList;
440 dao.getAppServiceList(appServiceList);
442 if (appServiceList.empty()) {
443 LogInfo("Widget doesn't contain application service");
447 // X-SLP-SVC=operation:scheme:mime;
448 file << "X-SLP-SVC=";
449 FOREACH(it, appServiceList) {
450 file << DPL::ToUTF8String(it->operation).c_str() << ":";
451 if (it->scheme.empty()) {
452 file << "NULL" << ":";
454 file << DPL::ToUTF8String(it->scheme).c_str() << ":";
456 if (it->mime.empty()) {
457 file << "NULL" << ";";
459 file << DPL::ToUTF8String(it->mime).c_str() << ";";
464 void TaskDesktopFile::stepFinalize()
466 LogInfo("Finished DesktopFile step");
469 void TaskDesktopFile::saveLocalizedKey(std::ofstream &file,
470 const DPL::String& key,
471 const DPL::String& languageTag)
474 LocalizationUtils::BCP47LanguageTagToLocale(languageTag);
477 if (!locale.empty()) {
478 file << "[" << locale << "]";
482 } //namespace WidgetInstall