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 <dpl/ace-dao-rw/AceDAO.h>
29 #include <vcore/VCore.h>
30 #include <dpl/wrt-dao-ro/WrtDatabase.h>
31 #include <dpl/wrt-dao-rw/widget_dao.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/ace-dao-ro/wrt_db_types.h>
39 #include <dpl/utils/folder_size.h>
41 using namespace WrtDB;
47 #define GET_DIRECTORY_SIZE_KB(x) (x)/1024
54 class DatabaseConnection
59 WrtDB::WrtDatabase::attachToThread();
64 WrtDB::WrtDatabase::detachFromThread();
68 static DPL::ScopedPtr<DatabaseConnection> g_databaseConnection;
70 static void pkg_native_plugin_on_unload();
71 static int pkg_plugin_app_is_installed(const char *pkg_name);
72 static int pkg_plugin_get_installed_apps_list(const char *category,
73 const char *option, package_manager_pkg_info_t **list, int *count);
74 static int pkg_plugin_get_app_detail_info(const char *pkg_name,
75 package_manager_pkg_detail_info_t *pkg_detail_info);
76 static int pkg_plugin_get_app_detail_info_from_package(const char *pkg_path,
77 package_manager_pkg_detail_info_t *pkg_detail_info);
79 static int is_connected()
81 if (NULL == g_databaseConnection) {
83 g_databaseConnection.Reset(new DatabaseConnection());
84 g_databaseConnection->AttachDatabase();
86 Catch (DPL::DB::SqlConnection::Exception::Base) {
87 LogDebug("Fail to connect DB");
95 static void pkg_native_plugin_on_unload()
97 LogDebug("pkg_native_plugin_unload() is called");
100 static int pkg_plugin_app_is_installed(const char *pkg_name)
102 LogDebug("pkg_plugin_app_is_installed() is called");
104 if (FALSE == is_connected()) {
105 LogError("Fail DB Connect");
110 if (WidgetDAOReadOnly::isWidgetInstalled(
111 DPL::FromUTF8String(pkg_name))) {
114 } Catch(DPL::DB::SqlConnection::Exception::Base) {
115 LogDebug("Databas Error");
119 LogDebug("Widget Not Found");
123 static int pkg_plugin_get_installed_apps_list(const char * /*category*/,
124 const char * /*option*/, package_manager_pkg_info_t **list, int *count)
126 LogDebug("pkg_plugin_get_installed_apps_list() is called");
128 package_manager_pkg_info_t *pkg_list = NULL;
129 package_manager_pkg_info_t *pkg_last = NULL;
132 if (FALSE == is_connected()) {
133 LogError("Fail DB Connect");
137 WidgetHandleList hndlList = WidgetDAO::getHandleList();
140 FOREACH(iterator, hndlList) {
141 package_manager_pkg_info_t *pkg_info =
142 static_cast<package_manager_pkg_info_t*>
143 (malloc(sizeof(package_manager_pkg_info_t)));
144 if (NULL == pkg_info) {
145 LogError("Error in malloc");
149 if (NULL == pkg_list) {
153 pkg_last->next = pkg_info;
156 WidgetDAO widget(*iterator);
157 DPL::Optional<DPL::String> pkgname = widget.getPkgname();
158 strncpy(pkg_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
159 if(!pkgname.IsNull()) {
160 snprintf(pkg_info->pkg_name, PKG_NAME_STRING_LEN_MAX, "%s",
161 DPL::ToUTF8String(*pkgname).c_str());
164 DPL::Optional<DPL::String> version = widget.getVersion();
165 if (!version.IsNull()) {
166 strncpy(pkg_info->version,
167 DPL::ToUTF8String(*version).c_str(),
168 PKG_VERSION_STRING_LEN_MAX);
176 Catch (WidgetDAO::Exception::DatabaseError) {
177 LogError("Database Error");
183 static int pkg_plugin_get_app_detail_info(const char *pkg_name,
184 package_manager_pkg_detail_info_t *pkg_detail_info)
186 LogDebug("pkg_plugin_get_app_detail_info() is called");
189 if (FALSE == is_connected()) {
190 LogError("Fail DB Connect");
194 int handle = WidgetDAOReadOnly::getHandle(
195 DPL::FromUTF8String(pkg_name));
196 WidgetDAO widget(handle);
198 DPL::Optional<DPL::String> version = widget.getVersion();
199 DPL::Optional<DPL::String> id = widget.getGUID();
200 DPL::Optional<DPL::String> locale = widget.getDefaultlocale();
202 if (!version.IsNull()) {
203 strncpy(pkg_detail_info->version,
204 DPL::ToUTF8String(*version).c_str(),
205 PKG_VERSION_STRING_LEN_MAX);
207 snprintf(pkg_detail_info->optional_id, PKG_NAME_STRING_LEN_MAX, "%d",
209 WidgetLocalizedInfo localizedInfo;
211 if (locale.IsNull()) {
213 DPL::String languageTag(L"");
214 localizedInfo = widget.getLocalizedInfo(languageTag);
216 localizedInfo = widget.getLocalizedInfo(*locale);
218 DPL::Optional<DPL::String> desc(localizedInfo.description);
220 if (!desc.IsNull()) {
221 strncpy(pkg_detail_info->pkg_description,
222 DPL::ToUTF8String(*desc).c_str(),
223 PKG_VALUE_STRING_LEN_MAX);
225 strncpy(pkg_detail_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
226 strncpy(pkg_detail_info->pkg_name, pkg_name, PKG_NAME_STRING_LEN_MAX);
228 /* set installed time */
229 pkg_detail_info->installed_time = widget.getInstallTime();
231 /* set Widget size */
232 DPL::String pkgName = DPL::FromUTF8String(pkg_name);
233 std::string installPath = WidgetConfig::GetWidgetBasePath(pkgName);
234 std::string persistentPath =
235 WidgetConfig::GetWidgetPersistentStoragePath(pkgName);
236 std::string tempPath =
237 WidgetConfig::GetWidgetTemporaryStoragePath(pkgName);
240 persistentPath += "/";
242 size_t installedSize = Utils::getFolderSize(installPath);
243 size_t persistentSize = Utils::getFolderSize(persistentPath);
244 size_t appSize = installedSize - persistentSize;
245 size_t dataSize = persistentSize + Utils::getFolderSize(tempPath);
247 pkg_detail_info->installed_size = GET_DIRECTORY_SIZE_KB(installedSize);
248 pkg_detail_info->app_size = GET_DIRECTORY_SIZE_KB(appSize);
249 pkg_detail_info->data_size = GET_DIRECTORY_SIZE_KB(dataSize);
251 Catch (WidgetDAO::Exception::DatabaseError) {
252 LogError("Database Error");
258 static int pkg_plugin_get_app_detail_info_from_package(
259 const char * /*pkg_path*/,
260 package_manager_pkg_detail_info_t * /*pkg_detail_info*/)
262 LogDebug("pkg_plugin_get_app_detail_info_from_package() is called");
267 __attribute__ ((visibility("default")))
268 int pkg_plugin_on_load(pkg_plugin_set *set)
270 DPL::Log::LogSystemSingleton::Instance().SetTag("WGT-BACKLIB");
274 memset(set, 0x00, sizeof(pkg_plugin_set));
276 set->plugin_on_unload = pkg_native_plugin_on_unload;
277 set->pkg_is_installed = pkg_plugin_app_is_installed;
278 set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
279 set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
280 set->get_pkg_detail_info_from_package =
281 pkg_plugin_get_app_detail_info_from_package;