Beta release
[framework/api/app-manager.git] / src / app_manager.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 <dlog.h>
28
29 #include <app_manager_private.h>
30 #include <app_manager.h>
31
32 #ifdef LOG_TAG
33 #undef LOG_TAG
34 #endif
35
36 #define LOG_TAG "TIZEN_N_APP_MANAGER"
37
38 #define MENU_PKG_VCONF_KEY "memory/menuscreen/desktop"
39
40 #define EVENT_CREATE "create"
41 #define EVENT_UPDATE "update"
42 #define EVENT_DELETE "delete"
43
44 typedef struct {
45         app_manager_app_running_cb cb;
46         void *user_data;
47         bool *foreach_break;
48 } running_apps_foreach_cb_context;
49
50 typedef struct {
51         app_manager_app_installed_cb cb;
52         void *user_data;
53 } installed_apps_foreach_cb_context;
54
55 static app_manager_app_list_changed_cb app_list_changed_cb = NULL;
56
57 static void app_manager_meun_pkg_changed(keynode_t* node, void *data);
58
59 static int foreach_running_app_cb_broker(const aul_app_info * appcore_app_info, void *appcore_user_data)
60 {
61         ail_appinfo_h handle;
62         ail_error_e ret;
63         bool task_manage = false;
64         running_apps_foreach_cb_context *foreach_cb_context = NULL;
65
66         if (appcore_app_info == NULL || appcore_user_data == NULL) 
67         {
68                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback context", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
69                 return 0;
70         }
71
72         ret = ail_package_get_appinfo(appcore_app_info->pkg_name, &handle);
73         if (ret != AIL_ERROR_OK)
74         {
75                 LOGE("[%s] DB_FAILED(0x%08x) : failed to get the app-info", __FUNCTION__, APP_MANAGER_ERROR_DB_FAILED);
76                 return 0;
77         }
78
79         // do not call callback function when X-SLP-TaskManage is set to false
80         ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_TASKMANAGE_BOOL, &task_manage);
81
82         ail_package_destroy_appinfo(handle);
83
84         if (ret != AIL_ERROR_OK || task_manage == false)
85         {
86                 return 0;
87         }
88
89         foreach_cb_context = (running_apps_foreach_cb_context *)appcore_user_data;
90
91         if (foreach_cb_context->cb != NULL && *(foreach_cb_context->foreach_break) == false)
92         {
93                 if (foreach_cb_context->cb(appcore_app_info->pkg_name, foreach_cb_context->user_data) == false)
94                 {
95                         *(foreach_cb_context->foreach_break) = true;
96                 }
97         }
98
99         return 0;
100 }
101
102 static ail_cb_ret_e foreach_installed_app_cb_broker(const ail_appinfo_h appinfo, void *ail_user_data)
103 {
104         installed_apps_foreach_cb_context *foreach_cb_context = NULL;
105         char *package;
106
107         if (appinfo == NULL || ail_user_data == NULL)
108         {
109                 return AIL_CB_RET_CANCEL;
110         }
111
112         foreach_cb_context = (installed_apps_foreach_cb_context *)ail_user_data;
113
114         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
115
116         if (foreach_cb_context->cb(package, foreach_cb_context->user_data)  == false)
117         {
118                 return AIL_CB_RET_CANCEL;
119         }
120         
121         return AIL_CB_RET_CONTINUE;
122
123 }
124
125
126 static int app_manager_ail_error_handler(ail_error_e ail_error, const char *func)
127 {
128         int error_code;
129         char *error_msg;
130
131         switch(ail_error)
132         {
133                 case AIL_ERROR_FAIL:
134                         error_code = APP_MANAGER_ERROR_INVALID_PARAMETER;
135                         error_msg = "INVALID_PARAMETER";
136                         break;
137
138                 case AIL_ERROR_DB_FAILED:
139                         error_code = APP_MANAGER_ERROR_DB_FAILED;
140                         error_msg = "DB_FAILED";
141                         break;
142
143                 case AIL_ERROR_OUT_OF_MEMORY:
144                         error_code = APP_MANAGER_ERROR_OUT_OF_MEMORY;
145                         error_msg = "OUT_OF_MEMORY";
146                         break;
147
148                 case AIL_ERROR_INVALID_PARAMETER:
149                         error_code = APP_MANAGER_ERROR_INVALID_PARAMETER;
150                         error_msg = "INVALID_PARAMETER";
151                         break;
152                 
153                 case AIL_ERROR_OK:
154                         error_code = APP_MANAGER_ERROR_NONE;
155                         break;
156                         
157                 default:
158                         error_code = APP_MANAGER_ERROR_INVALID_PARAMETER;
159                         error_msg = "INVALID_PARAMETER";
160         }
161
162         if (error_code != APP_MANAGER_ERROR_NONE)
163         {
164                 LOGE("[%s] %s(0x%08x)", func, error_msg, error_code);
165         }
166
167         return error_code;
168 }
169
170
171 int app_manager_foreach_app_running(app_manager_app_running_cb callback, void *user_data)
172 {
173         bool foreach_break = false;
174
175         running_apps_foreach_cb_context foreach_cb_context = {
176                 .cb = callback,
177                 .user_data = user_data,
178                 .foreach_break = &foreach_break
179         };
180
181         if (callback == NULL)
182         {
183                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
184                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
185         }
186
187         aul_app_get_running_app_info(foreach_running_app_cb_broker, &foreach_cb_context);
188
189         return APP_MANAGER_ERROR_NONE;
190 }
191
192 int app_manager_is_running(const char *package, bool *is_running)
193 {
194         if (package == NULL)
195         {
196                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
197                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
198         }
199
200         if (is_running == NULL)
201         {
202                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
203                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
204         }
205
206         *is_running = aul_app_is_running(package);
207
208         return APP_MANAGER_ERROR_NONE;
209 }
210
211  int app_manager_foreach_app_installed(app_manager_app_installed_cb callback, void *user_data)
212 {
213         ail_filter_h filter;
214         ail_error_e ret;
215
216         if (callback == NULL)
217         {
218                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
219                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
220         }
221
222         ret = ail_filter_new(&filter);
223         if (ret != AIL_ERROR_OK)
224         {
225                 return app_manager_ail_error_handler(ret, __FUNCTION__);
226         }
227
228         // Provide visible application to 3rd party developer
229         ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
230         if (ret != AIL_ERROR_OK)
231         {
232                 ail_filter_destroy(filter);
233                 return app_manager_ail_error_handler(ret, __FUNCTION__);
234         }
235
236         // Provide task manageable app only to 3rd party developer
237         ret = ail_filter_add_bool(filter, AIL_PROP_X_SLP_TASKMANAGE_BOOL, true);
238         if (ret != AIL_ERROR_OK)
239         {
240                 ail_filter_destroy(filter);
241                 return app_manager_ail_error_handler(ret, __FUNCTION__);
242         }
243
244         installed_apps_foreach_cb_context foreach_cb_context = {
245                 .cb = callback,
246                 .user_data = user_data,
247         };
248
249         ail_filter_list_appinfo_foreach(filter, foreach_installed_app_cb_broker, &foreach_cb_context);
250
251         ail_filter_destroy(filter);
252         
253         return APP_MANAGER_ERROR_NONE;
254 }
255
256 static int app_manager_get_appinfo(const char *package, ail_prop_str_e property, char **value)
257 {
258         ail_error_e ail_error;
259         ail_appinfo_h appinfo;
260         char *appinfo_value;
261         char *appinfo_value_dup;
262
263         ail_error = ail_package_get_appinfo(package, &appinfo);
264         if (ail_error != AIL_ERROR_OK)
265         {
266                 return app_manager_ail_error_handler(ail_error, __FUNCTION__);
267         }
268
269         ail_error = ail_appinfo_get_str(appinfo, property, &appinfo_value);
270         if (ail_error != AIL_ERROR_OK)
271         {
272                 ail_package_destroy_appinfo(appinfo);
273                 return app_manager_ail_error_handler(ail_error, __FUNCTION__);
274         }
275
276         appinfo_value_dup = strdup(appinfo_value);
277
278         ail_package_destroy_appinfo(appinfo);
279
280         if (appinfo_value_dup == NULL)
281         {
282                 LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, APP_MANAGER_ERROR_OUT_OF_MEMORY);
283                 return APP_MANAGER_ERROR_OUT_OF_MEMORY;
284         }
285
286         *value = appinfo_value_dup;
287         
288         return APP_MANAGER_ERROR_NONE;
289 }
290
291 int app_manager_get_app_name(const char *package, char** name)
292 {
293         if (package == NULL)
294         {
295                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
296                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
297         }
298
299         return app_manager_get_appinfo(package, AIL_PROP_NAME_STR, name);
300 }
301  
302 int app_manager_get_app_icon_path(const char *package, char** icon_path)
303 {
304         if (package == NULL)
305         {
306                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
307                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
308         }
309
310         return app_manager_get_appinfo(package, AIL_PROP_ICON_STR, icon_path);
311 }
312
313 int app_manager_get_app_version(const char *package, char** version)
314 {
315         if (package == NULL)
316         {
317                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
318                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
319         }
320
321         return app_manager_get_appinfo(package, AIL_PROP_VERSION_STR, version);
322 }
323
324 int app_manager_set_app_list_changed_cb(app_manager_app_list_changed_cb callback, void* user_data)
325 {
326        if (callback == NULL) 
327         {
328                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid callback", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
329                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
330         }
331
332         if (app_list_changed_cb == NULL)
333         {
334                 vconf_notify_key_changed(MENU_PKG_VCONF_KEY, app_manager_meun_pkg_changed, user_data);
335         }
336
337         app_list_changed_cb = callback;
338
339         return APP_MANAGER_ERROR_NONE;
340 }
341
342 int app_manager_unset_app_list_changed_cb()
343 {       
344         if (app_list_changed_cb != NULL)
345         {
346                 if (vconf_ignore_key_changed(MENU_PKG_VCONF_KEY, app_manager_meun_pkg_changed))
347                 {
348                         LOGE("[%s] DB_FAILED(0x%08x)", __FUNCTION__, APP_MANAGER_ERROR_DB_FAILED);
349                         return APP_MANAGER_ERROR_DB_FAILED;
350                 }
351         }
352
353         app_list_changed_cb = NULL;
354
355         return APP_MANAGER_ERROR_NONE;
356 }
357
358 static void app_manager_meun_pkg_changed(keynode_t* node, void *data)
359 {
360         char *pkg_event;
361         char type[10];
362         char package[1024];
363
364         pkg_event = vconf_get_str(vconf_keynode_get_name(node));
365         if(!pkg_event) {
366                 LOGE("[%s] failed to get the package event");
367                 return;
368         }
369
370         if (sscanf(pkg_event, "%10[^:]:%1023s", type, package) != 2)
371         {
372                 LOGE("[%s] failed to parse the package event format : [%s], [%s]", __FUNCTION__, type, package);
373         }
374
375
376         if(app_list_changed_cb == NULL)
377         {
378                 return;
379         }
380         
381         if(!strcasecmp(type, EVENT_CREATE))
382         {
383                 // A new application has been installed.
384                 app_list_changed_cb(APP_MANAGER_EVENT_INSTALLED, package, data);
385         }
386         else if( !strcasecmp(type, EVENT_UPDATE))
387         {
388                 // An existing application has been updated.
389                 app_list_changed_cb(APP_MANAGER_EVENT_UPDATED , package, data);
390         }
391
392         else if( !strcasecmp(type, EVENT_DELETE))
393         {
394                 // An existing application has been uninstalled.
395                 app_list_changed_cb(APP_MANAGER_EVENT_UNINSTALLED  , package, data);
396         }
397         else
398         {
399                 LOGE("[%s] invalid event : type(%s)", __FUNCTION__, type);
400         }
401
402 }
403