3eed3f9f8b425a1aaf44a55bdc4711b39262245d
[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,
57                                               package_manager_pkg_info_t **list,
58                                               int *count);
59 static int pkg_plugin_get_app_detail_info(
60     const char *pkg_name,
61     package_manager_pkg_detail_info_t *
62     pkg_detail_info);
63 static int pkg_plugin_get_app_detail_info_from_package(
64     const char *pkg_path,
65     package_manager_pkg_detail_info_t
66     *pkg_detail_info);
67
68 static void pkg_native_plugin_on_unload()
69 {
70     LogDebug("pkg_native_plugin_unload() is called");
71 }
72
73 static int pkg_plugin_app_is_installed(const char *pkg_name)
74 {
75     LogDebug("pkg_plugin_app_is_installed() is called");
76
77     WrtDB::WrtDatabase::attachToThreadRO();
78
79     bool result = WidgetDAOReadOnly::isWidgetInstalled(
80             DPL::FromUTF8String(pkg_name));
81     WrtDB::WrtDatabase::detachFromThread();
82
83     if (result) {
84         return TRUE;
85     } else {
86         return FALSE;
87     }
88 }
89
90 static int pkg_plugin_get_installed_apps_list(const char * /*category*/,
91                                               const char * /*option*/,
92                                               package_manager_pkg_info_t **list,
93                                               int *count)
94 {
95     LogDebug("pkg_plugin_get_installed_apps_list() is called");
96
97     package_manager_pkg_info_t *pkg_list = NULL;
98     package_manager_pkg_info_t *pkg_last = NULL;
99
100     WrtDB::WrtDatabase::attachToThreadRO();
101     TizenAppIdList tizenAppidList = WidgetDAOReadOnly::getTizenAppidList();
102     *count = 0;
103
104     FOREACH(iterator, tizenAppidList) {
105         package_manager_pkg_info_t *pkg_info =
106             static_cast<package_manager_pkg_info_t*>
107             (malloc(sizeof(package_manager_pkg_info_t)));
108
109         if (NULL == pkg_list) {
110             pkg_list = pkg_info;
111             pkg_last = pkg_info;
112         } else {
113             pkg_last->next = pkg_info;
114         }
115
116         TizenAppId tzAppid = *iterator;
117         WidgetDAOReadOnly widget(tzAppid);
118         strncpy(pkg_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
119         snprintf(pkg_info->pkg_name, PKG_NAME_STRING_LEN_MAX, "%s",
120                  DPL::ToUTF8String(tzAppid).c_str());
121
122         DPL::Optional<DPL::String> version = widget.getVersion();
123         if (!version.IsNull()) {
124             strncpy(pkg_info->version,
125                     DPL::ToUTF8String(*version).c_str(),
126                     PKG_VERSION_STRING_LEN_MAX - 1);
127         }
128
129         (*count)++;
130         pkg_last = pkg_info;
131     }
132     *list = pkg_list;
133     WrtDB::WrtDatabase::detachFromThread();
134
135     return TRUE;
136 }
137
138 static int pkg_plugin_get_app_detail_info(
139     const char *pkg_name,
140     package_manager_pkg_detail_info_t *
141     pkg_detail_info)
142 {
143     LogDebug("pkg_plugin_get_app_detail_info() is called");
144
145     WrtDB::WrtDatabase::attachToThreadRO();
146     int handle = WidgetDAOReadOnly::getHandle(
147             DPL::FromUTF8String(pkg_name));
148     WidgetDAOReadOnly widget(handle);
149
150     DPL::Optional<DPL::String> version = widget.getVersion();
151     DPL::Optional<DPL::String> id = widget.getGUID();
152     DPL::Optional<DPL::String> locale = widget.getDefaultlocale();
153
154     if (!version.IsNull()) {
155         strncpy(pkg_detail_info->version,
156                 DPL::ToUTF8String(*version).c_str(),
157                 PKG_VERSION_STRING_LEN_MAX - 1);
158     }
159     snprintf(pkg_detail_info->optional_id, PKG_NAME_STRING_LEN_MAX, "%d",
160              handle);
161     WidgetLocalizedInfo localizedInfo;
162
163     if (locale.IsNull()) {
164         LogError("is NULL");
165         DPL::String languageTag(L"");
166         localizedInfo = widget.getLocalizedInfo(languageTag);
167     } else {
168         localizedInfo = widget.getLocalizedInfo(*locale);
169     }
170     DPL::Optional<DPL::String> desc(localizedInfo.description);
171
172     if (!desc.IsNull()) {
173         strncpy(pkg_detail_info->pkg_description,
174                 DPL::ToUTF8String(*desc).c_str(),
175                 PKG_VALUE_STRING_LEN_MAX - 1);
176     }
177     strncpy(pkg_detail_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
178     strncpy(pkg_detail_info->pkg_name, pkg_name, PKG_NAME_STRING_LEN_MAX - 1);
179
180     /* set installed time */
181     pkg_detail_info->installed_time = widget.getInstallTime();
182
183     /* set Widget size */
184     DPL::String pkgName = DPL::FromUTF8String(pkg_name);
185     std::string installPath = WidgetConfig::GetWidgetBasePath(pkgName);
186     std::string persistentPath =
187         WidgetConfig::GetWidgetPersistentStoragePath(pkgName);
188     std::string tempPath =
189         WidgetConfig::GetWidgetTemporaryStoragePath(pkgName);
190     installPath += "/";
191     tempPath += "/";
192     persistentPath += "/";
193
194     size_t installedSize = Utils::getFolderSize(installPath);
195     size_t persistentSize = Utils::getFolderSize(persistentPath);
196     size_t appSize = installedSize - persistentSize;
197     size_t dataSize = persistentSize + Utils::getFolderSize(tempPath);
198
199     pkg_detail_info->installed_size = GET_DIRECTORY_SIZE_KB(installedSize);
200     pkg_detail_info->app_size = GET_DIRECTORY_SIZE_KB(appSize);
201     pkg_detail_info->data_size = GET_DIRECTORY_SIZE_KB(dataSize);
202
203     WrtDB::WrtDatabase::detachFromThread();
204     return TRUE;
205 }
206
207 static int pkg_plugin_get_app_detail_info_from_package(
208     const char * /*pkg_path*/,
209     package_manager_pkg_detail_info_t * /*pkg_detail_info*/)
210 {
211     LogDebug("pkg_plugin_get_app_detail_info_from_package() is called");
212
213     return TRUE;
214 }
215
216 __attribute__ ((visibility("default")))
217 int pkg_plugin_on_load(pkg_plugin_set *set)
218 {
219     DPL::Log::LogSystemSingleton::Instance().SetTag("WGT-BACKLIB");
220     if (NULL == set) {
221         return FALSE;
222     }
223     memset(set, 0x00, sizeof(pkg_plugin_set));
224
225     set->plugin_on_unload = pkg_native_plugin_on_unload;
226     set->pkg_is_installed = pkg_plugin_app_is_installed;
227     set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
228     set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
229     set->get_pkg_detail_info_from_package =
230         pkg_plugin_get_app_detail_info_from_package;
231
232     return TRUE;
233 }
234
235 #ifdef __cplusplus
236 }
237 #endif