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.
19 * @file backendlib.cpp
20 * @author Soyoung Kim (sy037.kim@samsung.com)
22 * @brief This is implementation file for providing widget information
25 #include "package-manager-plugin.h"
26 #include <package-manager.h>
29 #include <dpl/wrt-dao-ro/global_config.h>
30 #include <vcore/VCore.h>
31 #include <dpl/wrt-dao-ro/WrtDatabase.h>
32 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
33 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
34 #include <dpl/wrt-dao-ro/widget_config.h>
36 #include <dpl/db/sql_connection.h>
37 #include <dpl/log/log.h>
38 #include <dpl/foreach.h>
39 #include <dpl/utils/folder_size.h>
40 #include <dpl/wrt-dao-ro/wrt_db_types.h>
42 #include <dpl/abstract_waitable_input_adapter.h>
43 #include <dpl/abstract_waitable_output_adapter.h>
44 #include <dpl/zip_input.h>
45 #include <dpl/binary_queue.h>
46 #include <dpl/file_input.h>
47 #include <dpl/wrt-dao-ro/config_parser_data.h>
48 #include "root_parser.h"
49 #include "widget_parser.h"
50 #include "parser_runner.h"
52 using namespace WrtDB;
58 #define GET_DIRECTORY_SIZE_KB(x) (x) / 1024
65 static void pkg_native_plugin_on_unload();
66 static int pkg_plugin_app_is_installed(const char *pkg_name);
67 static int pkg_plugin_get_installed_apps_list(const char *category,
69 package_manager_pkg_info_t **list,
71 static int pkg_plugin_get_app_detail_info(
73 package_manager_pkg_detail_info_t *
75 static int pkg_plugin_get_app_detail_info_from_package(
77 package_manager_pkg_detail_info_t
80 pkgmgr_info *pkgmgr_client_check_pkginfo_from_file(const char *pkg_path);
82 static void pkg_native_plugin_on_unload()
84 LogDebug("pkg_native_plugin_unload() is called");
87 static int pkg_plugin_app_is_installed(const char *pkg_name)
89 const char* REG_PKGID_PATTERN = "^[a-zA-Z0-9]{10}$";
90 LogDebug("pkg_plugin_app_is_installed() is called");
92 WrtDB::WrtDatabase::attachToThreadRO();
95 if (regcomp(®, REG_PKGID_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
96 LogDebug("Regcomp failed");
99 WrtDB::TizenAppId appid;
101 if (!(regexec(®, pkg_name,
102 static_cast<size_t>(0), NULL, 0) == 0))
104 LogError("Invalid argument : " << pkg_name);
109 WrtDB::TizenPkgId pkgid(DPL::FromUTF8String(pkg_name));
110 appid = WidgetDAOReadOnly::getTzAppId(pkgid);
111 LogDebug("appid : " << appid);
112 } Catch(WidgetDAOReadOnly::Exception::WidgetNotExist) {
113 WrtDB::WrtDatabase::detachFromThread();
116 WrtDB::WrtDatabase::detachFromThread();
120 static int pkg_plugin_get_installed_apps_list(const char * /*category*/,
121 const char * /*option*/,
122 package_manager_pkg_info_t **list,
125 LogDebug("pkg_plugin_get_installed_apps_list() is called");
127 package_manager_pkg_info_t *pkg_list = NULL;
128 package_manager_pkg_info_t *pkg_last = NULL;
130 WrtDB::WrtDatabase::attachToThreadRO();
131 TizenAppIdList tizenAppidList = WidgetDAOReadOnly::getTizenAppidList();
134 FOREACH(iterator, tizenAppidList) {
135 package_manager_pkg_info_t *pkg_info =
136 static_cast<package_manager_pkg_info_t*>
137 (malloc(sizeof(package_manager_pkg_info_t)));
139 if (NULL == pkg_list) {
143 pkg_last->next = pkg_info;
146 TizenAppId tzAppid = *iterator;
147 WidgetDAOReadOnly widget(tzAppid);
148 strncpy(pkg_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
149 snprintf(pkg_info->pkg_name, PKG_NAME_STRING_LEN_MAX, "%s",
150 DPL::ToUTF8String(tzAppid).c_str());
152 DPL::Optional<DPL::String> version = widget.getVersion();
153 if (!version.IsNull()) {
154 strncpy(pkg_info->version,
155 DPL::ToUTF8String(*version).c_str(),
156 PKG_VERSION_STRING_LEN_MAX - 1);
163 WrtDB::WrtDatabase::detachFromThread();
168 static int pkg_plugin_get_app_detail_info(
169 const char *pkg_name,
170 package_manager_pkg_detail_info_t *
173 LogDebug("pkg_plugin_get_app_detail_info() is called");
175 WrtDB::WrtDatabase::attachToThreadRO();
177 WrtDB::TizenAppId appid;
179 WrtDB::TizenPkgId pkgid(DPL::FromUTF8String(pkg_name));
180 appid = WidgetDAOReadOnly::getTzAppId(pkgid);
181 LogDebug("appid : " << appid);
182 } Catch(WidgetDAOReadOnly::Exception::WidgetNotExist) {
183 WrtDB::WrtDatabase::detachFromThread();
187 WidgetDAOReadOnly widget(appid);
189 DPL::Optional<DPL::String> version = widget.getVersion();
190 DPL::Optional<DPL::String> id = widget.getGUID();
191 DPL::Optional<DPL::String> locale = widget.getDefaultlocale();
193 if (!version.IsNull()) {
194 strncpy(pkg_detail_info->version,
195 DPL::ToUTF8String(*version).c_str(),
196 PKG_VERSION_STRING_LEN_MAX - 1);
198 snprintf(pkg_detail_info->pkgid, PKG_NAME_STRING_LEN_MAX, "%s",
200 snprintf(pkg_detail_info->optional_id, PKG_NAME_STRING_LEN_MAX, "%s",
201 DPL::ToUTF8String(appid).c_str());
202 WidgetLocalizedInfo localizedInfo;
204 if (locale.IsNull()) {
205 LogDebug("locale is NULL");
206 DPL::String languageTag(L"");
207 localizedInfo = widget.getLocalizedInfo(languageTag);
209 localizedInfo = widget.getLocalizedInfo(*locale);
211 DPL::Optional<DPL::String> desc(localizedInfo.description);
213 if (!desc.IsNull()) {
214 strncpy(pkg_detail_info->pkg_description,
215 DPL::ToUTF8String(*desc).c_str(),
216 PKG_VALUE_STRING_LEN_MAX - 1);
218 strncpy(pkg_detail_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
219 strncpy(pkg_detail_info->pkg_name, pkg_name, PKG_NAME_STRING_LEN_MAX - 1);
221 std::string min_version = DPL::ToUTF8String((*widget.getMinimumWacVersion()));
223 strncpy(pkg_detail_info->min_platform_version, min_version.c_str(),
224 PKG_VERSION_STRING_LEN_MAX - 1);
226 /* set installed time */
227 pkg_detail_info->installed_time = widget.getInstallTime();
229 /* set Widget size */
230 DPL::String pkgName = DPL::FromUTF8String(pkg_name);
231 std::string installPath = WidgetConfig::GetWidgetBasePath(pkgName);
232 std::string persistentPath =
233 WidgetConfig::GetWidgetPersistentStoragePath(pkgName);
234 std::string tempPath =
235 WidgetConfig::GetWidgetTemporaryStoragePath(pkgName);
238 persistentPath += "/";
240 size_t installedSize = Utils::getFolderSize(installPath);
241 size_t persistentSize = Utils::getFolderSize(persistentPath);
242 size_t appSize = installedSize - persistentSize;
243 size_t dataSize = persistentSize + Utils::getFolderSize(tempPath);
245 pkg_detail_info->installed_size = GET_DIRECTORY_SIZE_KB(installedSize);
246 pkg_detail_info->app_size = GET_DIRECTORY_SIZE_KB(appSize);
247 pkg_detail_info->data_size = GET_DIRECTORY_SIZE_KB(dataSize);
249 WrtDB::WrtDatabase::detachFromThread();
253 int getConfigParserData(const std::string &widgetPath, ConfigParserData& configInfo)
255 const char* CONFIG_XML = "config.xml";
256 const char* WITH_OSP_XML = "res/wgt/config.xml";
261 std::unique_ptr<DPL::ZipInput> zipFile(
262 new DPL::ZipInput(widgetPath));
264 std::unique_ptr<DPL::ZipInput::File> configFile;
266 // Open config.xml file
268 configFile.reset(zipFile->OpenFile(CONFIG_XML));
270 Catch(DPL::ZipInput::Exception::OpenFileFailed)
272 configFile.reset(zipFile->OpenFile(WITH_OSP_XML));
276 DPL::BinaryQueue buffer;
277 DPL::AbstractWaitableInputAdapter inputAdapter(configFile.get());
278 DPL::AbstractWaitableOutputAdapter outputAdapter(&buffer);
279 DPL::Copy(&inputAdapter, &outputAdapter);
280 parser.Parse(&buffer,
282 new RootParser<WidgetParser>(configInfo,
287 Catch(DPL::ZipInput::Exception::OpenFailed)
289 LogError("Failed to open widget package");
292 Catch(DPL::ZipInput::Exception::OpenFileFailed)
294 LogError("Failed to open config.xml file");
297 Catch(DPL::CopyFailed)
299 LogError("Failed to extract config.xml file");
302 Catch(DPL::FileInput::Exception::OpenFailed)
304 LogError("Failed to open config.xml file");
307 Catch(ElementParser::Exception::ParseError)
309 LogError("Failed to parse config.xml file");
312 Catch(DPL::ZipInput::Exception::SeekFileFailed)
314 LogError("Failed to seek widget archive - corrupted package?");
321 char* getIconInfo(const std::string widgetPath,
322 const std::string icon_name, int &icon_size)
325 std::unique_ptr<DPL::ZipInput> zipFile(
326 new DPL::ZipInput(widgetPath));
328 std::unique_ptr<DPL::ZipInput::File> iconFile;
330 iconFile.reset(zipFile->OpenFile(icon_name));
332 DPL::BinaryQueue buffer;
333 DPL::AbstractWaitableInputAdapter inputAdapter(iconFile.get());
334 DPL::AbstractWaitableOutputAdapter outputAdapter(&buffer);
335 DPL::Copy(&inputAdapter, &outputAdapter);
336 icon_size = buffer.Size();
337 char *getBuffer = (char*) calloc(1, (sizeof(char) * icon_size) + 1);
338 buffer.Flatten(getBuffer, buffer.Size());
341 Catch(DPL::ZipInput::Exception::OpenFailed)
343 LogError("Failed to open widget package");
346 Catch(DPL::ZipInput::Exception::OpenFileFailed)
348 LogError("Failed to open icon file");
353 int getWidgetDetailInfoFromPackage(const char* pkgPath,
354 package_manager_pkg_detail_info_t* pkg_detail_info)
356 const std::string widget_path(pkgPath);
357 ConfigParserData configInfo;
359 if (FALSE == getConfigParserData(widget_path, configInfo)) {
363 strncpy(pkg_detail_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
364 if (!configInfo.tizenPkgId.IsNull()) {
365 strncpy(pkg_detail_info->pkgid,
366 DPL::ToUTF8String(*configInfo.tizenPkgId).c_str(), PKG_TYPE_STRING_LEN_MAX - 1);
368 if (!configInfo.tizenAppId.IsNull()) {
369 strncpy(pkg_detail_info->pkg_name,
370 DPL::ToUTF8String(*configInfo.tizenAppId).c_str(),
371 PKG_NAME_STRING_LEN_MAX);
373 if (!configInfo.version.IsNull()) {
374 strncpy(pkg_detail_info->version,
375 DPL::ToUTF8String(*configInfo.version).c_str(),
376 PKG_VERSION_STRING_LEN_MAX - 1);
379 DPL::Optional<DPL::String> name;
380 DPL::Optional<DPL::String> desc;
381 DPL::OptionalString defaultLocale = configInfo.defaultlocale;
383 FOREACH(localizedData, configInfo.localizedDataSet)
385 Locale i = localizedData->first;
386 if (!!defaultLocale) {
387 if (defaultLocale == i) {
388 name = localizedData->second.name;
389 desc = localizedData->second.description;
393 name = localizedData->second.name;
394 desc = localizedData->second.description;
399 if( !name.IsNull()) {
400 strncpy(pkg_detail_info->label, DPL::ToUTF8String(*name).c_str(),
401 PKG_LABEL_STRING_LEN_MAX - 1);
404 if (!desc.IsNull()) {
405 strncpy(pkg_detail_info->pkg_description,
406 DPL::ToUTF8String(*desc).c_str(),
407 PKG_VALUE_STRING_LEN_MAX - 1);
410 if (!configInfo.tizenMinVersionRequired.IsNull()) {
411 strncpy(pkg_detail_info->min_platform_version,
412 DPL::ToUTF8String(*configInfo.tizenMinVersionRequired).c_str(),
413 PKG_VERSION_STRING_LEN_MAX);
416 if (!configInfo.authorName.IsNull()) {
417 strncpy(pkg_detail_info->author,
418 DPL::ToUTF8String(*configInfo.authorName).c_str(),
419 PKG_VALUE_STRING_LEN_MAX - 1);
422 std::string icon_name;
424 FOREACH(i, configInfo.iconsList) {
425 LogDebug("icon name: " << i->src);
426 icon_name = DPL::ToUTF8String(i->src);
429 pkg_detail_info->icon_buf = getIconInfo(widget_path, icon_name,
430 pkg_detail_info->icon_size);
431 LogDebug("icon size : " << pkg_detail_info->icon_size);
433 GList *privilege_list = NULL;
434 FOREACH(it, configInfo.featuresList) {
435 LogDebug("privilege : " << it->name);
436 int length = DPL::ToUTF8String(it->name).length();
437 char *privilege = (char*) calloc(1, (sizeof(char) * length) + 1);
438 snprintf(privilege, length + 1, "%s",
439 DPL::ToUTF8String(it->name).c_str());
440 privilege_list = g_list_append(privilege_list, privilege);
442 pkg_detail_info->privilege_list = privilege_list;
447 static int pkg_plugin_get_app_detail_info_from_package(
448 const char * pkg_path,
449 package_manager_pkg_detail_info_t * pkg_detail_info)
451 LogDebug("pkg_plugin_get_app_detail_info_from_package() is called");
452 return getWidgetDetailInfoFromPackage(pkg_path, pkg_detail_info);
455 pkgmgr_info *pkgmgr_client_check_pkginfo_from_file(const char *pkg_path)
457 LogDebug("pkgmgr_client_check_pkginfo_from_file() is called");
458 package_manager_pkg_detail_info_t pkg_detail_info;
459 int ret = getWidgetDetailInfoFromPackage(pkg_path, &pkg_detail_info);
461 LogError("Failed to get package detail info ");
464 return &pkg_detail_info;
467 __attribute__ ((visibility("default")))
468 int pkg_plugin_on_load(pkg_plugin_set *set)
470 DPL::Log::LogSystemSingleton::Instance().SetTag("WGT-BACKLIB");
474 memset(set, 0x00, sizeof(pkg_plugin_set));
476 set->plugin_on_unload = pkg_native_plugin_on_unload;
477 set->pkg_is_installed = pkg_plugin_app_is_installed;
478 set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
479 set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
480 set->get_pkg_detail_info_from_package =
481 pkg_plugin_get_app_detail_info_from_package;