Upload Tizen2.0 source
[framework/api/app-manager.git] / src / app_info.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 <ail.h>
24 #include <package-manager.h>
25 #include <dlog.h>
26
27 #include <app_info.h>
28 #include <app_manager.h>
29 #include <app_manager_private.h>
30
31 #ifdef LOG_TAG
32 #undef LOG_TAG
33 #endif
34
35 #define LOG_TAG "TIZEN_N_APP_MANAGER"
36
37 static int app_info_create(const char *app_id, app_info_h *app_info);
38
39 struct app_info_s {
40         char *app_id;
41         ail_appinfo_h ail_app_info;
42 };
43
44 typedef struct _foreach_context_{
45         app_manager_app_info_cb callback;
46         void *user_data;
47 } foreach_context_s;
48
49
50 static ail_cb_ret_e app_info_foreach_app_info_cb(const ail_appinfo_h ail_app_info, void *cb_data)
51 {
52         foreach_context_s *foreach_context = cb_data;
53         app_info_h app_info = NULL;
54         char *app_id;
55         bool iteration_next = true;
56
57         if (ail_app_info == NULL || foreach_context == NULL)
58         {
59                 app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
60                 return AIL_CB_RET_CANCEL;
61         }
62
63         ail_appinfo_get_str(ail_app_info, AIL_PROP_PACKAGE_STR, &app_id);
64
65         if (app_info_create(app_id, &app_info) == APP_MANAGER_ERROR_NONE)
66         {
67                 iteration_next = foreach_context->callback(app_info, foreach_context->user_data);
68                 app_info_destroy(app_info);
69         }
70
71         if (iteration_next == true)
72         {
73                 return AIL_CB_RET_CONTINUE;
74         }
75         else
76         {
77                 return AIL_CB_RET_CANCEL;
78         }
79 }
80
81 int app_info_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
82 {
83         foreach_context_s foreach_context = {
84                 .callback = callback,
85                 .user_data = user_data,
86         };
87
88         if (callback == NULL)
89         {
90                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
91         }
92
93         ail_filter_list_appinfo_foreach(NULL, app_info_foreach_app_info_cb, &foreach_context);
94
95         return APP_MANAGER_ERROR_NONE;
96 }
97
98 int app_info_get_app_info(const char *app_id, app_info_h *app_info)
99 {
100         return app_info_create(app_id, app_info);
101 }
102
103 static int app_info_create(const char *app_id, app_info_h *app_info)
104 {
105         app_info_h app_info_created;
106         ail_appinfo_h ail_app_info;
107
108         if (app_id == NULL || app_info == NULL)
109         {
110                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
111         }
112
113         if (ail_package_get_appinfo(app_id, &ail_app_info) != AIL_ERROR_OK)
114         {
115                 return app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
116         }
117
118         app_info_created = calloc(1, sizeof(struct app_info_s));
119
120         if (app_info_created == NULL)
121         {
122                 ail_package_destroy_appinfo(ail_app_info);
123                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
124         }
125
126         app_info_created->app_id = strdup(app_id);
127
128         if (app_info_created->app_id == NULL)
129         {
130                 ail_package_destroy_appinfo(ail_app_info);
131                 free(app_info_created);
132                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
133         }
134
135         app_info_created->ail_app_info = ail_app_info;
136
137         *app_info = app_info_created;
138
139         return APP_MANAGER_ERROR_NONE;
140 }
141
142 int app_info_destroy(app_info_h app_info)
143 {
144         if (app_info == NULL)
145         {
146                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
147         }
148
149         free(app_info->app_id);
150
151         ail_package_destroy_appinfo(app_info->ail_app_info);
152
153         free(app_info); 
154
155         return APP_MANAGER_ERROR_NONE;
156 }
157
158 int app_info_get_app_id(app_info_h app_info, char **app_id)
159 {
160         char *app_id_dup;
161
162         if (app_info == NULL || app_id == NULL)
163         {
164                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
165         }
166
167         app_id_dup = strdup(app_info->app_id);
168
169         if (app_id_dup == NULL)
170         {
171                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
172         }
173
174         *app_id = app_id_dup;
175
176         return APP_MANAGER_ERROR_NONE;
177 }
178
179
180 int app_info_get_name(app_info_h app_info, char **name)
181 {
182         char *ail_value = NULL;
183         char *name_dup;
184
185         if (app_info == NULL || name == NULL)
186         {
187                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
188         }
189
190         if (ail_appinfo_get_str(app_info->ail_app_info, AIL_PROP_NAME_STR, &ail_value) != AIL_ERROR_OK)
191         {
192                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
193         }
194
195         name_dup = strdup(ail_value);
196
197         if (name_dup == NULL)
198         {
199                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
200         }
201
202         *name = name_dup;
203
204         return APP_MANAGER_ERROR_NONE;
205 }
206
207
208 int app_info_get_version(app_info_h app_info, char **version)
209 {
210         char *ail_value = NULL;
211         char *version_dup;
212
213         if (app_info == NULL || version == NULL)
214         {
215                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
216         }
217
218         if (ail_appinfo_get_str(app_info->ail_app_info, AIL_PROP_VERSION_STR, &ail_value) != AIL_ERROR_OK)
219         {
220                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
221         }
222
223         version_dup = strdup(ail_value);
224
225         if (version_dup == NULL)
226         {
227                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
228         }
229
230         *version = version_dup;
231
232         return APP_MANAGER_ERROR_NONE;
233 }
234
235
236 int app_info_get_icon(app_info_h app_info, char **path)
237 {
238         char *ail_value = NULL;
239         char *path_dup;
240
241         if (app_info == NULL || path == NULL)
242         {
243                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
244         }
245
246         if (ail_appinfo_get_str(app_info->ail_app_info, AIL_PROP_ICON_STR, &ail_value) != AIL_ERROR_OK)
247         {
248                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
249         }
250
251         path_dup = strdup(ail_value);
252
253         if (path_dup == NULL)
254         {
255                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
256         }
257
258         *path = path_dup;
259
260         return APP_MANAGER_ERROR_NONE;
261 }
262
263
264 int app_info_is_equal(app_info_h lhs, app_info_h rhs, bool *equal)
265 {
266         if (lhs == NULL || rhs == NULL || equal == NULL)
267         {
268                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
269         }
270
271         if (!strcmp(lhs->app_id, rhs->app_id))
272         {
273                 *equal = true;
274         }
275         else
276         {
277                 *equal = false;
278         }
279
280         return APP_MANAGER_ERROR_NONE;
281 }
282
283
284 int app_info_clone(app_info_h *clone, app_info_h app_info)
285 {
286         int retval;
287
288         if (clone == NULL || app_info == NULL)
289         {
290                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
291         }
292
293         retval = app_info_create(app_info->app_id, clone);
294
295         if (retval != APP_MANAGER_ERROR_NONE)
296         {
297                 return app_manager_error(retval, __FUNCTION__, NULL);
298         }
299
300         return APP_MANAGER_ERROR_NONE;
301 }
302
303 static pkgmgr_client *package_event_listener = NULL;
304 static app_manager_app_info_event_cb app_info_event_cb = NULL;
305 static void *app_info_event_cb_data = NULL;
306
307 static app_info_event_e app_info_get_app_info_event(const char *value)
308 {
309         if (!strcasecmp(value, "install"))
310         {
311                 return APP_INFO_EVENT_INSTALLED;
312         }
313         else if (!strcasecmp(value, "uninstall"))
314         {
315                 return APP_INFO_EVENT_UNINSTALLED;      
316         }
317         else if (!strcasecmp(value, "update"))
318         {
319                 return APP_INFO_EVENT_UPDATED;          
320         }
321         else
322         {
323                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
324         }
325 }
326
327 static int app_info_package_event_listener_cb(
328         int id, const char *type, const char *package, const char *key, const char *val, const void *msg, void *data)
329 {
330         static int event_id = -1;
331         static app_info_event_e event_type = -1;
332
333         if (!strcasecmp(key, "start"))
334         {
335                 event_id = id;
336                 event_type = app_info_get_app_info_event(val);
337         }
338         else if (!strcasecmp(key, "end") && !strcasecmp(val, "ok") && id == event_id)
339         {
340                 if (app_info_event_cb != NULL && event_type >= 0)
341                 {
342                         app_info_h app_info;
343                         
344                         if (app_info_create(package, &app_info) == APP_MANAGER_ERROR_NONE)
345                         {
346                                 app_info_event_cb(app_info, event_type, app_info_event_cb_data);
347                                 app_info_destroy(app_info);
348                         }
349                 }
350
351                 event_id = -1;
352                 event_type = -1;
353         }
354
355         return APP_MANAGER_ERROR_NONE;
356 }
357
358 int app_info_set_event_cb(app_manager_app_info_event_cb callback, void *user_data)
359 {
360         if (callback == NULL)
361         {
362                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
363         }
364
365         if (app_info_event_cb == NULL)
366         {
367                 package_event_listener = pkgmgr_client_new(PC_LISTENING);
368                 
369                 if (package_event_listener == NULL)
370                 {
371                         return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
372                 }
373
374                 pkgmgr_client_listen_status(package_event_listener, app_info_package_event_listener_cb, NULL);
375         }
376
377         app_info_event_cb = callback;
378         app_info_event_cb_data = user_data;
379
380         return APP_MANAGER_ERROR_NONE;
381 }
382
383
384 void app_info_unset_event_cb(void)
385 {
386         if (app_info_event_cb != NULL)
387         {
388                 pkgmgr_client_free(package_event_listener);
389                 package_event_listener = NULL;
390         }
391
392         app_info_event_cb = NULL;
393         app_info_event_cb_data = NULL;
394 }
395