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"
27 #include <dpl/wrt-dao-ro/global_config.h>
28 #include <vcore/VCore.h>
29 #include <dpl/wrt-dao-ro/WrtDatabase.h>
30 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
31 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
32 #include <dpl/wrt-dao-ro/widget_config.h>
34 #include <dpl/db/sql_connection.h>
35 #include <dpl/log/log.h>
36 #include <dpl/foreach.h>
37 #include <dpl/utils/folder_size.h>
38 #include <dpl/wrt-dao-ro/wrt_db_types.h>
40 using namespace WrtDB;
46 #define GET_DIRECTORY_SIZE_KB(x) (x)/1024
53 class DatabaseConnection
58 WrtDB::WrtDatabase::attachToThreadRO();
63 WrtDB::WrtDatabase::detachFromThread();
67 static DPL::ScopedPtr<DatabaseConnection> g_databaseConnection;
69 static void pkg_native_plugin_on_unload();
70 static int pkg_plugin_app_is_installed(const char *pkg_name);
71 static int pkg_plugin_get_installed_apps_list(const char *category,
72 const char *option, package_manager_pkg_info_t **list, int *count);
73 static int pkg_plugin_get_app_detail_info(const char *pkg_name,
74 package_manager_pkg_detail_info_t *pkg_detail_info);
75 static int pkg_plugin_get_app_detail_info_from_package(const char *pkg_path,
76 package_manager_pkg_detail_info_t *pkg_detail_info);
78 static int is_connected()
80 if (NULL == g_databaseConnection) {
82 g_databaseConnection.Reset(new DatabaseConnection());
83 g_databaseConnection->AttachDatabase();
85 Catch (DPL::DB::SqlConnection::Exception::Base) {
86 LogDebug("Fail to connect DB");
94 static void pkg_native_plugin_on_unload()
96 LogDebug("pkg_native_plugin_unload() is called");
99 static int pkg_plugin_app_is_installed(const char *pkg_name)
101 LogDebug("pkg_plugin_app_is_installed() is called");
103 if (FALSE == is_connected()) {
104 LogError("Fail DB Connect");
109 if (WidgetDAOReadOnly::isWidgetInstalled(
110 DPL::FromUTF8String(pkg_name))) {
113 } Catch(DPL::DB::SqlConnection::Exception::Base) {
114 LogDebug("Databas Error");
118 LogDebug("Widget Not Found");
122 static int pkg_plugin_get_installed_apps_list(const char * /*category*/,
123 const char * /*option*/, package_manager_pkg_info_t **list, int *count)
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;
131 if (FALSE == is_connected()) {
132 LogError("Fail DB Connect");
136 WidgetHandleList hndlList = WidgetDAOReadOnly::getHandleList();
139 FOREACH(iterator, hndlList) {
140 package_manager_pkg_info_t *pkg_info =
141 static_cast<package_manager_pkg_info_t*>
142 (malloc(sizeof(package_manager_pkg_info_t)));
143 if (NULL == pkg_info) {
144 LogError("Error in malloc");
148 if (NULL == pkg_list) {
152 pkg_last->next = pkg_info;
155 WidgetDAOReadOnly widget(*iterator);
156 DPL::Optional<DPL::String> pkgname = widget.getPkgname();
157 strncpy(pkg_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
158 if(!pkgname.IsNull()) {
159 snprintf(pkg_info->pkg_name, PKG_NAME_STRING_LEN_MAX, "%s",
160 DPL::ToUTF8String(*pkgname).c_str());
163 DPL::Optional<DPL::String> version = widget.getVersion();
164 if (!version.IsNull()) {
165 strncpy(pkg_info->version,
166 DPL::ToUTF8String(*version).c_str(),
167 PKG_VERSION_STRING_LEN_MAX);
175 Catch (WidgetDAOReadOnly::Exception::DatabaseError) {
176 LogError("Database Error");
182 static int pkg_plugin_get_app_detail_info(const char *pkg_name,
183 package_manager_pkg_detail_info_t *pkg_detail_info)
185 LogDebug("pkg_plugin_get_app_detail_info() is called");
188 if (FALSE == is_connected()) {
189 LogError("Fail DB Connect");
193 int handle = WidgetDAOReadOnly::getHandle(
194 DPL::FromUTF8String(pkg_name));
195 WidgetDAOReadOnly widget(handle);
197 DPL::Optional<DPL::String> version = widget.getVersion();
198 DPL::Optional<DPL::String> id = widget.getGUID();
199 DPL::Optional<DPL::String> locale = widget.getDefaultlocale();
201 if (!version.IsNull()) {
202 strncpy(pkg_detail_info->version,
203 DPL::ToUTF8String(*version).c_str(),
204 PKG_VERSION_STRING_LEN_MAX);
206 snprintf(pkg_detail_info->optional_id, PKG_NAME_STRING_LEN_MAX, "%d",
208 WidgetLocalizedInfo localizedInfo;
210 if (locale.IsNull()) {
212 DPL::String languageTag(L"");
213 localizedInfo = widget.getLocalizedInfo(languageTag);
215 localizedInfo = widget.getLocalizedInfo(*locale);
217 DPL::Optional<DPL::String> desc(localizedInfo.description);
219 if (!desc.IsNull()) {
220 strncpy(pkg_detail_info->pkg_description,
221 DPL::ToUTF8String(*desc).c_str(),
222 PKG_VALUE_STRING_LEN_MAX);
224 strncpy(pkg_detail_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
225 strncpy(pkg_detail_info->pkg_name, pkg_name, PKG_NAME_STRING_LEN_MAX);
227 /* set installed time */
228 pkg_detail_info->installed_time = widget.getInstallTime();
230 /* set Widget size */
231 DPL::String pkgName = DPL::FromUTF8String(pkg_name);
232 std::string installPath = WidgetConfig::GetWidgetBasePath(pkgName);
233 std::string persistentPath =
234 WidgetConfig::GetWidgetPersistentStoragePath(pkgName);
235 std::string tempPath =
236 WidgetConfig::GetWidgetTemporaryStoragePath(pkgName);
239 persistentPath += "/";
241 size_t installedSize = Utils::getFolderSize(installPath);
242 size_t persistentSize = Utils::getFolderSize(persistentPath);
243 size_t appSize = installedSize - persistentSize;
244 size_t dataSize = persistentSize + Utils::getFolderSize(tempPath);
246 pkg_detail_info->installed_size = GET_DIRECTORY_SIZE_KB(installedSize);
247 pkg_detail_info->app_size = GET_DIRECTORY_SIZE_KB(appSize);
248 pkg_detail_info->data_size = GET_DIRECTORY_SIZE_KB(dataSize);
250 Catch (WidgetDAOReadOnly::Exception::DatabaseError) {
251 LogError("Database Error");
257 static int pkg_plugin_get_app_detail_info_from_package(
258 const char * /*pkg_path*/,
259 package_manager_pkg_detail_info_t * /*pkg_detail_info*/)
261 LogDebug("pkg_plugin_get_app_detail_info_from_package() is called");
266 __attribute__ ((visibility("default")))
267 int pkg_plugin_on_load(pkg_plugin_set *set)
269 DPL::Log::LogSystemSingleton::Instance().SetTag("WGT-BACKLIB");
273 memset(set, 0x00, sizeof(pkg_plugin_set));
275 set->plugin_on_unload = pkg_native_plugin_on_unload;
276 set->pkg_is_installed = pkg_plugin_app_is_installed;
277 set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
278 set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
279 set->get_pkg_detail_info_from_package =
280 pkg_plugin_get_app_detail_info_from_package;