[Installer] code clean up - src/configuration_parser/WidgetConfigurationManager not...
[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 <regex.h>
27 #include <dlog.h>
28 #include <dpl/wrt-dao-ro/global_config.h>
29 #include <vcore/VCore.h>
30 #include <dpl/wrt-dao-ro/WrtDatabase.h>
31 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
32 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
33 #include <dpl/wrt-dao-ro/widget_config.h>
34 #include <string>
35 #include <dpl/db/sql_connection.h>
36 #include <dpl/log/log.h>
37 #include <dpl/foreach.h>
38 #include <dpl/utils/folder_size.h>
39 #include <dpl/wrt-dao-ro/wrt_db_types.h>
40
41 using namespace WrtDB;
42
43 #undef TRUE
44 #undef FALSE
45 #define TRUE 0
46 #define FALSE -1
47 #define GET_DIRECTORY_SIZE_KB(x)    (x) / 1024
48
49 #ifdef __cplusplus
50 extern "C"
51 {
52 #endif
53
54 static void pkg_native_plugin_on_unload();
55 static int pkg_plugin_app_is_installed(const char *pkg_name);
56 static int pkg_plugin_get_installed_apps_list(const char *category,
57                                               const char *option,
58                                               package_manager_pkg_info_t **list,
59                                               int *count);
60 static int pkg_plugin_get_app_detail_info(
61     const char *pkg_name,
62     package_manager_pkg_detail_info_t *
63     pkg_detail_info);
64 static int pkg_plugin_get_app_detail_info_from_package(
65     const char *pkg_path,
66     package_manager_pkg_detail_info_t
67     *pkg_detail_info);
68
69 static void pkg_native_plugin_on_unload()
70 {
71     LogDebug("pkg_native_plugin_unload() is called");
72 }
73
74 static int pkg_plugin_app_is_installed(const char *pkg_name)
75 {
76     const char* REG_PKGID_PATTERN = "^[a-zA-Z0-9]{10}$";
77     LogDebug("pkg_plugin_app_is_installed() is called");
78
79     WrtDB::WrtDatabase::attachToThreadRO();
80
81     regex_t reg;
82     if (regcomp(&reg, REG_PKGID_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
83         LogDebug("Regcomp failed");
84     }
85
86     WrtDB::TizenAppId appid;
87
88     if ((regexec(&reg, pkg_name,
89                     static_cast<size_t>(0), NULL, 0) == 0))
90     {
91         WrtDB::TizenPkgId pkgid(DPL::FromUTF8String(pkg_name));
92         appid = WidgetDAOReadOnly::getTzAppId(pkgid);
93     } else {
94         appid = DPL::FromUTF8String(pkg_name);
95     }
96
97     bool result = WidgetDAOReadOnly::isWidgetInstalled(appid);
98     WrtDB::WrtDatabase::detachFromThread();
99
100     if (result) {
101         return TRUE;
102     } else {
103         return FALSE;
104     }
105 }
106
107 static int pkg_plugin_get_installed_apps_list(const char * /*category*/,
108                                               const char * /*option*/,
109                                               package_manager_pkg_info_t **list,
110                                               int *count)
111 {
112     LogDebug("pkg_plugin_get_installed_apps_list() is called");
113
114     package_manager_pkg_info_t *pkg_list = NULL;
115     package_manager_pkg_info_t *pkg_last = NULL;
116
117     WrtDB::WrtDatabase::attachToThreadRO();
118     TizenAppIdList tizenAppidList = WidgetDAOReadOnly::getTizenAppidList();
119     *count = 0;
120
121     FOREACH(iterator, tizenAppidList) {
122         package_manager_pkg_info_t *pkg_info =
123             static_cast<package_manager_pkg_info_t*>
124             (malloc(sizeof(package_manager_pkg_info_t)));
125
126         if (NULL == pkg_list) {
127             pkg_list = pkg_info;
128             pkg_last = pkg_info;
129         } else {
130             pkg_last->next = pkg_info;
131         }
132
133         TizenAppId tzAppid = *iterator;
134         WidgetDAOReadOnly widget(tzAppid);
135         strncpy(pkg_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
136         snprintf(pkg_info->pkg_name, PKG_NAME_STRING_LEN_MAX, "%s",
137                  DPL::ToUTF8String(tzAppid).c_str());
138
139         DPL::Optional<DPL::String> version = widget.getVersion();
140         if (!version.IsNull()) {
141             strncpy(pkg_info->version,
142                     DPL::ToUTF8String(*version).c_str(),
143                     PKG_VERSION_STRING_LEN_MAX - 1);
144         }
145
146         (*count)++;
147         pkg_last = pkg_info;
148     }
149     *list = pkg_list;
150     WrtDB::WrtDatabase::detachFromThread();
151
152     return TRUE;
153 }
154
155 static int pkg_plugin_get_app_detail_info(
156     const char *pkg_name,
157     package_manager_pkg_detail_info_t *
158     pkg_detail_info)
159 {
160     LogDebug("pkg_plugin_get_app_detail_info() is called");
161
162     WrtDB::WrtDatabase::attachToThreadRO();
163     int handle = WidgetDAOReadOnly::getHandle(
164             DPL::FromUTF8String(pkg_name));
165     WidgetDAOReadOnly widget(handle);
166
167     DPL::Optional<DPL::String> version = widget.getVersion();
168     DPL::Optional<DPL::String> id = widget.getGUID();
169     DPL::Optional<DPL::String> locale = widget.getDefaultlocale();
170
171     if (!version.IsNull()) {
172         strncpy(pkg_detail_info->version,
173                 DPL::ToUTF8String(*version).c_str(),
174                 PKG_VERSION_STRING_LEN_MAX - 1);
175     }
176     snprintf(pkg_detail_info->optional_id, PKG_NAME_STRING_LEN_MAX, "%d",
177              handle);
178     WidgetLocalizedInfo localizedInfo;
179
180     if (locale.IsNull()) {
181         LogError("is NULL");
182         DPL::String languageTag(L"");
183         localizedInfo = widget.getLocalizedInfo(languageTag);
184     } else {
185         localizedInfo = widget.getLocalizedInfo(*locale);
186     }
187     DPL::Optional<DPL::String> desc(localizedInfo.description);
188
189     if (!desc.IsNull()) {
190         strncpy(pkg_detail_info->pkg_description,
191                 DPL::ToUTF8String(*desc).c_str(),
192                 PKG_VALUE_STRING_LEN_MAX - 1);
193     }
194     strncpy(pkg_detail_info->pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX);
195     strncpy(pkg_detail_info->pkg_name, pkg_name, PKG_NAME_STRING_LEN_MAX - 1);
196
197     /* set installed time */
198     pkg_detail_info->installed_time = widget.getInstallTime();
199
200     /* set Widget size */
201     DPL::String pkgName = DPL::FromUTF8String(pkg_name);
202     std::string installPath = WidgetConfig::GetWidgetBasePath(pkgName);
203     std::string persistentPath =
204         WidgetConfig::GetWidgetPersistentStoragePath(pkgName);
205     std::string tempPath =
206         WidgetConfig::GetWidgetTemporaryStoragePath(pkgName);
207     installPath += "/";
208     tempPath += "/";
209     persistentPath += "/";
210
211     size_t installedSize = Utils::getFolderSize(installPath);
212     size_t persistentSize = Utils::getFolderSize(persistentPath);
213     size_t appSize = installedSize - persistentSize;
214     size_t dataSize = persistentSize + Utils::getFolderSize(tempPath);
215
216     pkg_detail_info->installed_size = GET_DIRECTORY_SIZE_KB(installedSize);
217     pkg_detail_info->app_size = GET_DIRECTORY_SIZE_KB(appSize);
218     pkg_detail_info->data_size = GET_DIRECTORY_SIZE_KB(dataSize);
219
220     WrtDB::WrtDatabase::detachFromThread();
221     return TRUE;
222 }
223
224 static int pkg_plugin_get_app_detail_info_from_package(
225     const char * /*pkg_path*/,
226     package_manager_pkg_detail_info_t * /*pkg_detail_info*/)
227 {
228     LogDebug("pkg_plugin_get_app_detail_info_from_package() is called");
229
230     return TRUE;
231 }
232
233 __attribute__ ((visibility("default")))
234 int pkg_plugin_on_load(pkg_plugin_set *set)
235 {
236     DPL::Log::LogSystemSingleton::Instance().SetTag("WGT-BACKLIB");
237     if (NULL == set) {
238         return FALSE;
239     }
240     memset(set, 0x00, sizeof(pkg_plugin_set));
241
242     set->plugin_on_unload = pkg_native_plugin_on_unload;
243     set->pkg_is_installed = pkg_plugin_app_is_installed;
244     set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
245     set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
246     set->get_pkg_detail_info_from_package =
247         pkg_plugin_get_app_detail_info_from_package;
248
249     return TRUE;
250 }
251
252 #ifdef __cplusplus
253 }
254 #endif