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"
28 #include <dpl/wrt-dao-ro/global_config.h>
29 #include <vcore/VCore.h>
30 #include <dpl/wrt-dao-ro/WrtDatabase.h>
31 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
32 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
33 #include <dpl/wrt-dao-ro/widget_config.h>
35 #include <dpl/db/sql_connection.h>
36 #include <dpl/log/log.h>
37 #include <dpl/foreach.h>
38 #include <dpl/utils/folder_size.h>
39 #include <dpl/wrt-dao-ro/wrt_db_types.h>
41 using namespace WrtDB;
47 #define GET_DIRECTORY_SIZE_KB(x) (x) / 1024
54 static void pkg_native_plugin_on_unload();
55 static int pkg_plugin_app_is_installed(const char *pkg_name);
56 static int pkg_plugin_get_installed_apps_list(const char *category,
58 package_manager_pkg_info_t **list,
60 static int pkg_plugin_get_app_detail_info(
62 package_manager_pkg_detail_info_t *
64 static int pkg_plugin_get_app_detail_info_from_package(
66 package_manager_pkg_detail_info_t
69 static void pkg_native_plugin_on_unload()
71 LogDebug("pkg_native_plugin_unload() is called");
74 static int pkg_plugin_app_is_installed(const char *pkg_name)
76 const char* REG_PKGID_PATTERN = "^[a-zA-Z0-9]{10}$";
77 LogDebug("pkg_plugin_app_is_installed() is called");
79 WrtDB::WrtDatabase::attachToThreadRO();
82 if (regcomp(®, REG_PKGID_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
83 LogDebug("Regcomp failed");
86 WrtDB::TizenAppId appid;
88 if ((regexec(®, pkg_name,
89 static_cast<size_t>(0), NULL, 0) == 0))
91 WrtDB::TizenPkgId pkgid(DPL::FromUTF8String(pkg_name));
92 appid = WidgetDAOReadOnly::getTzAppId(pkgid);
94 appid = DPL::FromUTF8String(pkg_name);
97 bool result = WidgetDAOReadOnly::isWidgetInstalled(appid);
98 WrtDB::WrtDatabase::detachFromThread();
107 static int pkg_plugin_get_installed_apps_list(const char * /*category*/,
108 const char * /*option*/,
109 package_manager_pkg_info_t **list,
112 LogDebug("pkg_plugin_get_installed_apps_list() is called");
114 package_manager_pkg_info_t *pkg_list = NULL;
115 package_manager_pkg_info_t *pkg_last = NULL;
117 WrtDB::WrtDatabase::attachToThreadRO();
118 TizenAppIdList tizenAppidList = WidgetDAOReadOnly::getTizenAppidList();
121 FOREACH(iterator, tizenAppidList) {
122 package_manager_pkg_info_t *pkg_info =
123 static_cast<package_manager_pkg_info_t*>
124 (malloc(sizeof(package_manager_pkg_info_t)));
126 if (NULL == pkg_list) {
130 pkg_last->next = pkg_info;
133 TizenAppId tzAppid = *iterator;
134 WidgetDAOReadOnly widget(tzAppid);
135 strncpy(pkg_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
136 snprintf(pkg_info->pkg_name, PKG_NAME_STRING_LEN_MAX, "%s",
137 DPL::ToUTF8String(tzAppid).c_str());
139 DPL::Optional<DPL::String> version = widget.getVersion();
140 if (!version.IsNull()) {
141 strncpy(pkg_info->version,
142 DPL::ToUTF8String(*version).c_str(),
143 PKG_VERSION_STRING_LEN_MAX - 1);
150 WrtDB::WrtDatabase::detachFromThread();
155 static int pkg_plugin_get_app_detail_info(
156 const char *pkg_name,
157 package_manager_pkg_detail_info_t *
160 LogDebug("pkg_plugin_get_app_detail_info() is called");
162 WrtDB::WrtDatabase::attachToThreadRO();
163 int handle = WidgetDAOReadOnly::getHandle(
164 DPL::FromUTF8String(pkg_name));
165 WidgetDAOReadOnly widget(handle);
167 DPL::Optional<DPL::String> version = widget.getVersion();
168 DPL::Optional<DPL::String> id = widget.getGUID();
169 DPL::Optional<DPL::String> locale = widget.getDefaultlocale();
171 if (!version.IsNull()) {
172 strncpy(pkg_detail_info->version,
173 DPL::ToUTF8String(*version).c_str(),
174 PKG_VERSION_STRING_LEN_MAX - 1);
176 snprintf(pkg_detail_info->optional_id, PKG_NAME_STRING_LEN_MAX, "%d",
178 WidgetLocalizedInfo localizedInfo;
180 if (locale.IsNull()) {
182 DPL::String languageTag(L"");
183 localizedInfo = widget.getLocalizedInfo(languageTag);
185 localizedInfo = widget.getLocalizedInfo(*locale);
187 DPL::Optional<DPL::String> desc(localizedInfo.description);
189 if (!desc.IsNull()) {
190 strncpy(pkg_detail_info->pkg_description,
191 DPL::ToUTF8String(*desc).c_str(),
192 PKG_VALUE_STRING_LEN_MAX - 1);
194 strncpy(pkg_detail_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
195 strncpy(pkg_detail_info->pkg_name, pkg_name, PKG_NAME_STRING_LEN_MAX - 1);
197 /* set installed time */
198 pkg_detail_info->installed_time = widget.getInstallTime();
200 /* set Widget size */
201 DPL::String pkgName = DPL::FromUTF8String(pkg_name);
202 std::string installPath = WidgetConfig::GetWidgetBasePath(pkgName);
203 std::string persistentPath =
204 WidgetConfig::GetWidgetPersistentStoragePath(pkgName);
205 std::string tempPath =
206 WidgetConfig::GetWidgetTemporaryStoragePath(pkgName);
209 persistentPath += "/";
211 size_t installedSize = Utils::getFolderSize(installPath);
212 size_t persistentSize = Utils::getFolderSize(persistentPath);
213 size_t appSize = installedSize - persistentSize;
214 size_t dataSize = persistentSize + Utils::getFolderSize(tempPath);
216 pkg_detail_info->installed_size = GET_DIRECTORY_SIZE_KB(installedSize);
217 pkg_detail_info->app_size = GET_DIRECTORY_SIZE_KB(appSize);
218 pkg_detail_info->data_size = GET_DIRECTORY_SIZE_KB(dataSize);
220 WrtDB::WrtDatabase::detachFromThread();
224 static int pkg_plugin_get_app_detail_info_from_package(
225 const char * /*pkg_path*/,
226 package_manager_pkg_detail_info_t * /*pkg_detail_info*/)
228 LogDebug("pkg_plugin_get_app_detail_info_from_package() is called");
233 __attribute__ ((visibility("default")))
234 int pkg_plugin_on_load(pkg_plugin_set *set)
236 DPL::Log::LogSystemSingleton::Instance().SetTag("WGT-BACKLIB");
240 memset(set, 0x00, sizeof(pkg_plugin_set));
242 set->plugin_on_unload = pkg_native_plugin_on_unload;
243 set->pkg_is_installed = pkg_plugin_app_is_installed;
244 set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
245 set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
246 set->get_pkg_detail_info_from_package =
247 pkg_plugin_get_app_detail_info_from_package;