Update wrt-installer_0.0.51
[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 class DatabaseConnection
54 {
55   public:
56     void AttachDatabase()
57     {
58         WrtDB::WrtDatabase::attachToThreadRO();
59     }
60
61     void DetachDatabase()
62     {
63         WrtDB::WrtDatabase::detachFromThread();
64     }
65 };
66
67 static DPL::ScopedPtr<DatabaseConnection> g_databaseConnection;
68
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);
77
78 static int is_connected()
79 {
80     if (NULL == g_databaseConnection) {
81         Try {
82             g_databaseConnection.Reset(new DatabaseConnection());
83             g_databaseConnection->AttachDatabase();
84         }
85         Catch (DPL::DB::SqlConnection::Exception::Base) {
86             LogDebug("Fail to connect DB");
87             return FALSE;
88         }
89     }
90
91     return TRUE;
92 }
93
94 static void pkg_native_plugin_on_unload()
95 {
96     LogDebug("pkg_native_plugin_unload() is called");
97 }
98
99 static int pkg_plugin_app_is_installed(const char *pkg_name)
100 {
101     LogDebug("pkg_plugin_app_is_installed() is called");
102
103     if (FALSE == is_connected()) {
104         LogError("Fail DB Connect");
105         return FALSE;
106     }
107
108     Try {
109         if (WidgetDAOReadOnly::isWidgetInstalled(
110                 DPL::FromUTF8String(pkg_name))) {
111             return TRUE;
112         }
113     } Catch(DPL::DB::SqlConnection::Exception::Base) {
114         LogDebug("Databas Error");
115         return FALSE;
116     }
117
118     LogDebug("Widget Not Found");
119     return FALSE;
120 }
121
122 static int pkg_plugin_get_installed_apps_list(const char * /*category*/,
123         const char * /*option*/, package_manager_pkg_info_t **list, int *count)
124 {
125     LogDebug("pkg_plugin_get_installed_apps_list() is called");
126
127     package_manager_pkg_info_t *pkg_list = NULL;
128     package_manager_pkg_info_t *pkg_last = NULL;
129
130     Try {
131         if (FALSE == is_connected()) {
132             LogError("Fail DB Connect");
133             return FALSE;
134         }
135
136         WidgetHandleList hndlList = WidgetDAOReadOnly::getHandleList();
137         *count = 0;
138
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");
145                 return FALSE;
146             }
147
148             if (NULL == pkg_list) {
149                 pkg_list = pkg_info;
150                 pkg_last = pkg_info;
151             } else {
152                 pkg_last->next = pkg_info;
153             }
154
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());
161             }
162
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);
168             }
169
170             (*count)++;
171             pkg_last = pkg_info;
172         }
173         *list = pkg_list;
174     }
175     Catch (WidgetDAOReadOnly::Exception::DatabaseError) {
176         LogError("Database Error");
177         return FALSE;
178     }
179     return TRUE;
180 }
181
182 static int pkg_plugin_get_app_detail_info(const char *pkg_name,
183         package_manager_pkg_detail_info_t *pkg_detail_info)
184 {
185     LogDebug("pkg_plugin_get_app_detail_info() is called");
186
187     Try {
188         if (FALSE == is_connected()) {
189             LogError("Fail DB Connect");
190             return FALSE;
191         }
192
193         int handle = WidgetDAOReadOnly::getHandle(
194                         DPL::FromUTF8String(pkg_name));
195         WidgetDAOReadOnly widget(handle);
196
197         DPL::Optional<DPL::String> version = widget.getVersion();
198         DPL::Optional<DPL::String> id = widget.getGUID();
199         DPL::Optional<DPL::String> locale = widget.getDefaultlocale();
200
201         if (!version.IsNull()) {
202             strncpy(pkg_detail_info->version,
203                     DPL::ToUTF8String(*version).c_str(),
204                     PKG_VERSION_STRING_LEN_MAX);
205         }
206         snprintf(pkg_detail_info->optional_id, PKG_NAME_STRING_LEN_MAX, "%d",
207                handle);
208         WidgetLocalizedInfo localizedInfo;
209
210         if (locale.IsNull()) {
211             LogError("is NULL");
212             DPL::String languageTag(L"");
213             localizedInfo = widget.getLocalizedInfo(languageTag);
214         } else {
215             localizedInfo = widget.getLocalizedInfo(*locale);
216         }
217         DPL::Optional<DPL::String> desc(localizedInfo.description);
218
219         if (!desc.IsNull()) {
220             strncpy(pkg_detail_info->pkg_description,
221                     DPL::ToUTF8String(*desc).c_str(),
222                     PKG_VALUE_STRING_LEN_MAX);
223         }
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);
226
227         /* set installed time */
228         pkg_detail_info->installed_time = widget.getInstallTime();
229
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);
237         installPath += "/";
238         tempPath += "/";
239         persistentPath += "/";
240
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);
245
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);
249     }
250     Catch (WidgetDAOReadOnly::Exception::DatabaseError) {
251         LogError("Database Error");
252         return FALSE;
253     }
254     return TRUE;
255 }
256
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*/)
260 {
261     LogDebug("pkg_plugin_get_app_detail_info_from_package() is called");
262
263     return TRUE;
264 }
265
266 __attribute__ ((visibility("default")))
267 int pkg_plugin_on_load(pkg_plugin_set *set)
268 {
269     DPL::Log::LogSystemSingleton::Instance().SetTag("WGT-BACKLIB");
270     if (NULL == set) {
271         return FALSE;
272     }
273     memset(set, 0x00, sizeof(pkg_plugin_set));
274
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;
281
282     return TRUE;
283 }
284
285 #ifdef __cplusplus
286 }
287 #endif