Upload Tizen2.0 source
[framework/api/app-manager.git] / src / app_manager_deprecated.c
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 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <aul.h>
24 #include <aul_service.h>
25 #include <vconf.h>
26 #include <ail.h>
27 #include <package-manager.h>
28 #include <dlog.h>
29
30 #include <app_manager.h>
31 #include <app_manager_private.h>
32
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #endif
36
37 #define LOG_TAG "TIZEN_N_APP_MANAGER"
38
39 typedef struct {
40         app_manager_app_running_cb cb;
41         void *user_data;
42         bool foreach_break;
43 } running_apps_foreach_cb_context;
44
45 typedef struct {
46         app_manager_app_installed_cb cb;
47         void *user_data;
48 } installed_apps_foreach_cb_context;
49
50 static pkgmgr_client *package_manager = NULL;
51 static app_manager_app_list_changed_cb app_list_changed_cb = NULL;
52 static void *app_list_changed_cb_data = NULL;
53
54 static int foreach_running_app_cb_broker(const aul_app_info * appcore_app_info, void *appcore_user_data)
55 {
56         ail_appinfo_h handle;
57         ail_error_e ret;
58         bool task_manage = false;
59         running_apps_foreach_cb_context *foreach_cb_context = NULL;
60
61         if (appcore_app_info == NULL || appcore_user_data == NULL) 
62         {
63                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback context", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
64                 return 0;
65         }
66
67         ret = ail_package_get_appinfo(appcore_app_info->pkg_name, &handle);
68         if (ret != AIL_ERROR_OK)
69         {
70                 LOGE("[%s] DB_FAILED(0x%08x) : failed to get the app-info", __FUNCTION__, APP_MANAGER_ERROR_DB_FAILED);
71                 return 0;
72         }
73
74         // do not call callback function when X-SLP-TaskManage is set to false
75         ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_TASKMANAGE_BOOL, &task_manage);
76
77         ail_package_destroy_appinfo(handle);
78
79         if (ret != AIL_ERROR_OK || task_manage == false)
80         {
81                 return 0;
82         }
83
84         foreach_cb_context = (running_apps_foreach_cb_context *)appcore_user_data;
85
86         if (foreach_cb_context->cb != NULL && foreach_cb_context->foreach_break == false)
87         {
88                 if (foreach_cb_context->cb(appcore_app_info->pkg_name, foreach_cb_context->user_data) == false)
89                 {
90                         foreach_cb_context->foreach_break = true;
91                 }
92         }
93
94         return 0;
95 }
96
97 static ail_cb_ret_e foreach_installed_app_cb_broker(const ail_appinfo_h appinfo, void *ail_user_data)
98 {
99         installed_apps_foreach_cb_context *foreach_cb_context = NULL;
100         char *package;
101
102         if (appinfo == NULL || ail_user_data == NULL)
103         {
104                 return AIL_CB_RET_CANCEL;
105         }
106
107         foreach_cb_context = (installed_apps_foreach_cb_context *)ail_user_data;
108
109         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
110
111         if (foreach_cb_context->cb(package, foreach_cb_context->user_data)  == false)
112         {
113                 return AIL_CB_RET_CANCEL;
114         }
115         
116         return AIL_CB_RET_CONTINUE;
117
118 }
119
120
121 static int app_manager_ail_error_handler(ail_error_e ail_error, const char *func)
122 {
123         int error_code;
124         char *error_msg;
125
126         switch(ail_error)
127         {
128                 case AIL_ERROR_FAIL:
129                         error_code = APP_MANAGER_ERROR_INVALID_PARAMETER;
130                         error_msg = "INVALID_PARAMETER";
131                         break;
132
133                 case AIL_ERROR_DB_FAILED:
134                         error_code = APP_MANAGER_ERROR_DB_FAILED;
135                         error_msg = "DB_FAILED";
136                         break;
137
138                 case AIL_ERROR_OUT_OF_MEMORY:
139                         error_code = APP_MANAGER_ERROR_OUT_OF_MEMORY;
140                         error_msg = "OUT_OF_MEMORY";
141                         break;
142
143                 case AIL_ERROR_INVALID_PARAMETER:
144                         error_code = APP_MANAGER_ERROR_INVALID_PARAMETER;
145                         error_msg = "INVALID_PARAMETER";
146                         break;
147                 
148                 case AIL_ERROR_OK:
149                         error_code = APP_MANAGER_ERROR_NONE;
150                         break;
151                         
152                 default:
153                         error_code = APP_MANAGER_ERROR_INVALID_PARAMETER;
154                         error_msg = "INVALID_PARAMETER";
155         }
156
157         if (error_code != APP_MANAGER_ERROR_NONE)
158         {
159                 LOGE("[%s] %s(0x%08x)", func, error_msg, error_code);
160         }
161
162         return error_code;
163 }
164
165
166 int app_manager_foreach_app_running(app_manager_app_running_cb callback, void *user_data)
167 {
168         running_apps_foreach_cb_context foreach_cb_context = {
169                 .cb = callback,
170                 .user_data = user_data,
171                 .foreach_break = false
172         };
173
174         if (callback == NULL)
175         {
176                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
177                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
178         }
179
180         aul_app_get_running_app_info(foreach_running_app_cb_broker, &foreach_cb_context);
181
182         return APP_MANAGER_ERROR_NONE;
183 }
184
185  int app_manager_foreach_app_installed(app_manager_app_installed_cb callback, void *user_data)
186 {
187         ail_filter_h filter;
188         ail_error_e ret;
189
190         if (callback == NULL)
191         {
192                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
193                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
194         }
195
196         ret = ail_filter_new(&filter);
197         if (ret != AIL_ERROR_OK)
198         {
199                 return app_manager_ail_error_handler(ret, __FUNCTION__);
200         }
201
202         installed_apps_foreach_cb_context foreach_cb_context = {
203                 .cb = callback,
204                 .user_data = user_data,
205         };
206
207         ail_filter_list_appinfo_foreach(filter, foreach_installed_app_cb_broker, &foreach_cb_context);
208
209         ail_filter_destroy(filter);
210         
211         return APP_MANAGER_ERROR_NONE;
212 }
213
214 static int app_manager_get_appinfo(const char *package, const char *property, char **value)
215 {
216         ail_error_e ail_error;
217         ail_appinfo_h appinfo;
218         char *appinfo_value;
219         char *appinfo_value_dup;
220
221         ail_error = ail_package_get_appinfo(package, &appinfo);
222         if (ail_error != AIL_ERROR_OK)
223         {
224                 return app_manager_ail_error_handler(ail_error, __FUNCTION__);
225         }
226
227         ail_error = ail_appinfo_get_str(appinfo, property, &appinfo_value);
228         if (ail_error != AIL_ERROR_OK)
229         {
230                 ail_package_destroy_appinfo(appinfo);
231                 return app_manager_ail_error_handler(ail_error, __FUNCTION__);
232         }
233
234         appinfo_value_dup = strdup(appinfo_value);
235
236         ail_package_destroy_appinfo(appinfo);
237
238         if (appinfo_value_dup == NULL)
239         {
240                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, APP_MANAGER_ERROR_OUT_OF_MEMORY);
241                 return APP_MANAGER_ERROR_OUT_OF_MEMORY;
242         }
243
244         *value = appinfo_value_dup;
245         
246         return APP_MANAGER_ERROR_NONE;
247 }
248
249 int app_manager_get_app_name(const char *package, char** name)
250 {
251         if (package == NULL)
252         {
253                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
254                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
255         }
256
257         return app_manager_get_appinfo(package, AIL_PROP_NAME_STR, name);
258 }
259  
260 int app_manager_get_app_icon_path(const char *package, char** icon_path)
261 {
262         if (package == NULL)
263         {
264                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
265                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
266         }
267
268         return app_manager_get_appinfo(package, AIL_PROP_ICON_STR, icon_path);
269 }
270
271 int app_manager_get_app_version(const char *package, char** version)
272 {
273         if (package == NULL)
274         {
275                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
276                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
277         }
278
279         return app_manager_get_appinfo(package, AIL_PROP_VERSION_STR, version);
280 }
281
282 static app_manger_event_type_e app_manager_app_list_pkgmgr_event(const char *value)
283 {
284         if (!strcasecmp(value, "install"))
285         {
286                 return APP_MANAGER_EVENT_INSTALLED;
287         }
288         else if (!strcasecmp(value, "uninstall"))
289         {
290                 return APP_MANAGER_EVENT_UNINSTALLED;   
291         }
292         else if (!strcasecmp(value, "update"))
293         {
294                 return APP_MANAGER_EVENT_UPDATED;               
295         }
296         else
297         {
298                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
299         }
300 }
301
302 static int app_manager_app_list_changed_cb_broker(int id, const char *type, const char *package, const char *key, const char *val, const void *msg, void *data)
303 {
304         static int event_id = -1;
305         static app_manger_event_type_e event_type;
306
307         if (!strcasecmp(key, "start"))
308         {
309                 event_id = id;
310                 event_type = app_manager_app_list_pkgmgr_event(val);
311         }
312         else if (!strcasecmp(key, "end") && !strcasecmp(val, "ok") && id == event_id)
313         {
314                 if (app_list_changed_cb != NULL && event_type >= 0)
315                 {
316                         app_list_changed_cb(event_type, package, app_list_changed_cb_data);
317                 }
318
319                 event_id = -1;
320                 event_type = -1;
321         }
322
323         return APP_MANAGER_ERROR_NONE;
324 }
325
326 int app_manager_set_app_list_changed_cb(app_manager_app_list_changed_cb callback, void* user_data)
327 {
328        if (callback == NULL) 
329         {
330                 LOGE("[%s] INVALID_PARAMETER(0x%08x)", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
331                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
332         }
333
334         if (app_list_changed_cb == NULL)
335         {
336                 package_manager = pkgmgr_client_new(PC_LISTENING);
337
338                 if (package_manager == NULL)
339                 {
340                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, APP_MANAGER_ERROR_OUT_OF_MEMORY);
341                         return APP_MANAGER_ERROR_OUT_OF_MEMORY;
342                 }
343
344                 pkgmgr_client_listen_status(package_manager, app_manager_app_list_changed_cb_broker, NULL);
345         }
346
347         app_list_changed_cb = callback;
348         app_list_changed_cb_data = user_data;
349
350         return APP_MANAGER_ERROR_NONE;
351 }
352
353 int app_manager_unset_app_list_changed_cb()
354 {       
355         if (app_list_changed_cb != NULL)
356         {
357                 pkgmgr_client_free(package_manager);
358                 package_manager = NULL;
359         }
360
361         app_list_changed_cb = NULL;
362         app_list_changed_cb_data = NULL;
363
364         return APP_MANAGER_ERROR_NONE;
365 }
366