Update wrt-installer_0.0.52
[framework/web/wrt-installer.git] / src / pkg-manager / backendlib.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  *
18  *
19  * @file       backendlib.cpp
20  * @author     Soyoung Kim (sy037.kim@samsung.com)
21  * @version    0.1
22  * @brief      This is implementation file for providing widget information
23  *             to package manager
24  */
25 #include "package-manager-plugin.h"
26 #include <dlog.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>
33 #include <string>
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>
39
40 using namespace WrtDB;
41
42 #undef TRUE
43 #undef FALSE
44 #define TRUE 0
45 #define FALSE -1
46 #define GET_DIRECTORY_SIZE_KB(x)    (x)/1024
47
48 #ifdef __cplusplus
49 extern "C"
50 {
51 #endif
52
53 static void pkg_native_plugin_on_unload();
54 static int pkg_plugin_app_is_installed(const char *pkg_name);
55 static int pkg_plugin_get_installed_apps_list(const char *category,
56         const char *option, package_manager_pkg_info_t **list, int *count);
57 static int pkg_plugin_get_app_detail_info(const char *pkg_name,
58         package_manager_pkg_detail_info_t *pkg_detail_info);
59 static int pkg_plugin_get_app_detail_info_from_package(const char *pkg_path,
60         package_manager_pkg_detail_info_t *pkg_detail_info);
61
62 static void pkg_native_plugin_on_unload()
63 {
64     LogDebug("pkg_native_plugin_unload() is called");
65 }
66
67 static int pkg_plugin_app_is_installed(const char *pkg_name)
68 {
69     LogDebug("pkg_plugin_app_is_installed() is called");
70
71     WrtDB::WrtDatabase::attachToThreadRO();
72
73     bool result = WidgetDAOReadOnly::isWidgetInstalled(
74                     DPL::FromUTF8String(pkg_name));
75     WrtDB::WrtDatabase::detachFromThread();
76
77     if (result) {
78         return TRUE;
79     } else {
80         return FALSE;
81     }
82 }
83
84 static int pkg_plugin_get_installed_apps_list(const char * /*category*/,
85         const char * /*option*/, package_manager_pkg_info_t **list, int *count)
86 {
87     LogDebug("pkg_plugin_get_installed_apps_list() is called");
88
89     package_manager_pkg_info_t *pkg_list = NULL;
90     package_manager_pkg_info_t *pkg_last = NULL;
91
92
93     WrtDB::WrtDatabase::attachToThreadRO();
94     WidgetHandleList hndlList = WidgetDAOReadOnly::getHandleList();
95     *count = 0;
96
97     FOREACH(iterator, hndlList) {
98         package_manager_pkg_info_t *pkg_info =
99             static_cast<package_manager_pkg_info_t*>
100             (malloc(sizeof(package_manager_pkg_info_t)));
101         if (NULL == pkg_info) {
102             LogError("Error in malloc");
103             return FALSE;
104         }
105
106         if (NULL == pkg_list) {
107             pkg_list = pkg_info;
108             pkg_last = pkg_info;
109         } else {
110             pkg_last->next = pkg_info;
111         }
112
113         WidgetDAOReadOnly widget(*iterator);
114         DPL::Optional<DPL::String> pkgname = widget.getPkgname();
115         strncpy(pkg_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
116         if(!pkgname.IsNull()) {
117             snprintf(pkg_info->pkg_name, PKG_NAME_STRING_LEN_MAX, "%s",
118                     DPL::ToUTF8String(*pkgname).c_str());
119         }
120
121         DPL::Optional<DPL::String> version = widget.getVersion();
122         if (!version.IsNull()) {
123             strncpy(pkg_info->version,
124                     DPL::ToUTF8String(*version).c_str(),
125                     PKG_VERSION_STRING_LEN_MAX);
126         }
127
128         (*count)++;
129         pkg_last = pkg_info;
130     }
131     *list = pkg_list;
132     WrtDB::WrtDatabase::detachFromThread();
133
134     return TRUE;
135 }
136
137 static int pkg_plugin_get_app_detail_info(const char *pkg_name,
138         package_manager_pkg_detail_info_t *pkg_detail_info)
139 {
140     LogDebug("pkg_plugin_get_app_detail_info() is called");
141
142
143     WrtDB::WrtDatabase::attachToThreadRO();
144     int handle = WidgetDAOReadOnly::getHandle(
145                     DPL::FromUTF8String(pkg_name));
146     WidgetDAOReadOnly widget(handle);
147
148     DPL::Optional<DPL::String> version = widget.getVersion();
149     DPL::Optional<DPL::String> id = widget.getGUID();
150     DPL::Optional<DPL::String> locale = widget.getDefaultlocale();
151
152     if (!version.IsNull()) {
153         strncpy(pkg_detail_info->version,
154                 DPL::ToUTF8String(*version).c_str(),
155                 PKG_VERSION_STRING_LEN_MAX);
156     }
157     snprintf(pkg_detail_info->optional_id, PKG_NAME_STRING_LEN_MAX, "%d",
158            handle);
159     WidgetLocalizedInfo localizedInfo;
160
161     if (locale.IsNull()) {
162         LogError("is NULL");
163         DPL::String languageTag(L"");
164         localizedInfo = widget.getLocalizedInfo(languageTag);
165     } else {
166         localizedInfo = widget.getLocalizedInfo(*locale);
167     }
168     DPL::Optional<DPL::String> desc(localizedInfo.description);
169
170     if (!desc.IsNull()) {
171         strncpy(pkg_detail_info->pkg_description,
172                 DPL::ToUTF8String(*desc).c_str(),
173                 PKG_VALUE_STRING_LEN_MAX);
174     }
175     strncpy(pkg_detail_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
176     strncpy(pkg_detail_info->pkg_name, pkg_name, PKG_NAME_STRING_LEN_MAX);
177
178     /* set installed time */
179     pkg_detail_info->installed_time = widget.getInstallTime();
180
181     /* set Widget size */
182     DPL::String pkgName = DPL::FromUTF8String(pkg_name);
183     std::string installPath = WidgetConfig::GetWidgetBasePath(pkgName);
184     std::string persistentPath =
185         WidgetConfig::GetWidgetPersistentStoragePath(pkgName);
186     std::string tempPath =
187         WidgetConfig::GetWidgetTemporaryStoragePath(pkgName);
188     installPath += "/";
189     tempPath += "/";
190     persistentPath += "/";
191
192     size_t installedSize = Utils::getFolderSize(installPath);
193     size_t persistentSize = Utils::getFolderSize(persistentPath);
194     size_t appSize = installedSize - persistentSize;
195     size_t dataSize = persistentSize + Utils::getFolderSize(tempPath);
196
197     pkg_detail_info->installed_size = GET_DIRECTORY_SIZE_KB(installedSize);
198     pkg_detail_info->app_size = GET_DIRECTORY_SIZE_KB(appSize);
199     pkg_detail_info->data_size = GET_DIRECTORY_SIZE_KB(dataSize);
200
201     WrtDB::WrtDatabase::detachFromThread();
202     return TRUE;
203 }
204
205 static int pkg_plugin_get_app_detail_info_from_package(
206         const char * /*pkg_path*/,
207         package_manager_pkg_detail_info_t * /*pkg_detail_info*/)
208 {
209     LogDebug("pkg_plugin_get_app_detail_info_from_package() is called");
210
211     return TRUE;
212 }
213
214 __attribute__ ((visibility("default")))
215 int pkg_plugin_on_load(pkg_plugin_set *set)
216 {
217     DPL::Log::LogSystemSingleton::Instance().SetTag("WGT-BACKLIB");
218     if (NULL == set) {
219         return FALSE;
220     }
221     memset(set, 0x00, sizeof(pkg_plugin_set));
222
223     set->plugin_on_unload = pkg_native_plugin_on_unload;
224     set->pkg_is_installed = pkg_plugin_app_is_installed;
225     set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
226     set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
227     set->get_pkg_detail_info_from_package =
228         pkg_plugin_get_app_detail_info_from_package;
229
230     return TRUE;
231 }
232
233 #ifdef __cplusplus
234 }
235 #endif