The source code moved from the SPIN with license changed to Flora 1.1
[apps/native/home/homescreen-efl.git] / src / app_mgr.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include <app_manager.h>
18 #include <package_manager.h>
19 #include "homescreen-efl.h"
20 #include "app_mgr.h"
21 #include "util.h"
22 #include "data_model.h"
23 #include "app_icon.h"
24
25 static struct
26 {
27         package_manager_h pack_mgr;
28 } s_app_mgr_info = {
29         .pack_mgr = NULL,
30 };
31
32 static bool __app_info_cb(app_info_h ai, void *ud);
33 static void __app_mgr_event_cb(const char *type, const char *package,
34                 package_manager_event_type_e event_type,
35                 package_manager_event_state_e event_state,
36                 int progress,
37                 package_manager_error_e error,
38                 void *user_data);
39
40 static app_mgr_item_t *__app_mgr_load_item_info(app_info_h ai_h);
41
42 HAPI void app_mgr_init(void)
43 {
44         package_manager_error_e res_stat = PACKAGE_MANAGER_ERROR_NONE;
45         package_manager_error_e res_cb = PACKAGE_MANAGER_ERROR_NONE;
46
47         if (s_app_mgr_info.pack_mgr)
48                 return;
49
50         if (PACKAGE_MANAGER_ERROR_NONE ==
51                 package_manager_create(&s_app_mgr_info.pack_mgr)) {
52                 LOGD("PACKAGE_EVENT_MGR - handle obtained");
53
54                 res_stat = package_manager_set_event_status(
55                         s_app_mgr_info.pack_mgr,
56                         PACKAGE_MANAGER_STATUS_TYPE_INSTALL |
57                         PACKAGE_MANAGER_STATUS_TYPE_UNINSTALL);
58
59                 res_cb = package_manager_set_event_cb(
60                 s_app_mgr_info.pack_mgr, __app_mgr_event_cb, NULL);
61
62                 if (res_stat != PACKAGE_MANAGER_ERROR_NONE ||
63                         res_cb != PACKAGE_MANAGER_ERROR_NONE)
64                         LOGE("App mgr initialization failed!");
65         } else {
66                 s_app_mgr_info.pack_mgr = NULL;
67         }
68 }
69
70 HAPI void app_mgr_deinit(void)
71 {
72         if (s_app_mgr_info.pack_mgr) {
73                 LOGD("PACKAGE_EVENT_MGR - freeing handle");
74                 package_manager_unset_event_cb(s_app_mgr_info.pack_mgr);
75                 package_manager_destroy(s_app_mgr_info.pack_mgr);
76         }
77 }
78
79 HAPI Eina_List *app_mgr_get_apps(void)
80 {
81         Eina_List *apps = NULL;
82         app_manager_foreach_app_info(__app_info_cb, &apps);
83         return apps;
84 }
85
86 HAPI void app_mgr_free_apps(Eina_List *apps)
87 {
88         app_mgr_item_t *app_mgr_item;
89         EINA_LIST_FREE(apps, app_mgr_item)
90                 free(app_mgr_item);
91 }
92
93 HAPI Eina_Bool app_mgr_uninstall_app(const char *appid)
94 {
95         LOGI("");
96
97         package_manager_request_h request;
98         int id = 0;
99
100         if (package_manager_request_create(&request)
101                 != PACKAGE_MANAGER_ERROR_NONE) {
102                 LOGE("Could not create unistall request. App: %s", appid);
103                 return EINA_FALSE;
104         }
105
106         int ret = package_manager_request_set_mode(request, PACKAGE_MANAGER_REQUEST_MODE_DEFAULT);
107         if (ret != PACKAGE_MANAGER_ERROR_NONE) {
108                 LOGE("Could not set request mode. App: %s", appid);
109                 return EINA_FALSE;
110         }
111
112         if (package_manager_request_uninstall(request, appid, &id) !=
113                 PACKAGE_MANAGER_ERROR_NONE) {
114                 LOGE("Could not uninstall application. App: %s", appid);
115                 return EINA_FALSE;
116         }
117
118         if (package_manager_request_destroy(request) !=
119                 PACKAGE_MANAGER_ERROR_NONE) {
120                 LOGE("Could not destroy unistall request. App: %s", appid);
121                 return EINA_FALSE;
122         }
123
124         return EINA_TRUE;
125 }
126
127 HAPI Eina_Bool app_mgr_app_get_badge_count(const char *app_id,
128         unsigned int *count)
129 {
130         unsigned int to_be_displayed = 0;
131         int result = BADGE_ERROR_NONE;
132
133         if (!app_id) {
134                 LOGE("app_item is NULL in badge count");
135                 return EINA_FALSE;
136         }
137
138         *count = 0;
139         result = badge_get_display(app_id, &to_be_displayed);
140
141         if (result != BADGE_ERROR_NONE) {
142                 if ((result == BADGE_ERROR_SERVICE_NOT_READY ||
143                         result == BADGE_ERROR_NOT_EXIST))
144                         return true;
145
146                 LOGE("badge_get_display error %d", result);
147                 return false;
148         }
149
150         if (!to_be_displayed)
151                 return EINA_TRUE;
152
153         result = badge_get_count(app_id, count);
154
155         if (result != BADGE_ERROR_NONE) {
156                 *count = 0;
157                 if ((result == BADGE_ERROR_SERVICE_NOT_READY ||
158                         result == BADGE_ERROR_NOT_EXIST))
159                         return true;
160
161                 LOGE("badge_get_count error %d", result);
162                 return false;
163         }
164
165         return EINA_TRUE;
166 }
167
168 HAPI void app_mgr_register_badge_callback(badge_change_cb callback)
169 {
170         int ret = BADGE_ERROR_NONE;
171         ret = badge_register_changed_cb(callback, NULL);
172
173         if (ret != BADGE_ERROR_NONE)
174                 LOGE("Could not register badge callback");
175 }
176
177 HAPI void app_mgr_unregister_badge_callback(badge_change_cb callback)
178 {
179         int ret = BADGE_ERROR_NONE;
180         ret = badge_unregister_changed_cb(callback);
181
182         if (ret != BADGE_ERROR_NONE)
183                 LOGE("Could not unregister badge callback");
184 }
185
186 HAPI char *app_mgr_get_app_label(const char *app_id)
187 {
188         app_info_h ai_handle;
189         char *label = NULL;
190
191         if (!app_id) {
192                 LOGE("Invalid parameter");
193                 return NULL;
194         }
195
196         if (app_info_create(app_id, &ai_handle) != APP_MANAGER_ERROR_NONE) {
197                 LOGE("Failed to load application id");
198                 return NULL;
199         }
200
201         if (app_info_get_label(ai_handle, &label) != APP_MANAGER_ERROR_NONE) {
202                 LOGE("Failed to get label!");
203                 app_info_destroy(ai_handle);
204                 return NULL;
205         }
206
207         app_info_destroy(ai_handle);
208         return label;
209 }
210
211 static void __app_mgr_event_cb(const char *type, const char *package,
212         package_manager_event_type_e event_type,
213         package_manager_event_state_e event_state, int progress,
214         package_manager_error_e error, void *user_data)
215 {
216         if (PACKAGE_MANAGER_ERROR_NONE != error) {
217                 LOGE("PACKAGE_EVENT_MGR error in cb");
218                 return;
219         }
220
221         if (PACKAGE_MANAGER_EVENT_STATE_FAILED == event_state)
222                 progress = 0;
223
224         if (PACKAGE_MANAGER_EVENT_TYPE_INSTALL == event_type && progress == 100) {
225                 LOGD("%s", package);
226                 app_info_h ai_handle = NULL;
227                 app_mgr_item_t *app_mgr_item = NULL;
228                 Tree_node_t *item = NULL;
229
230                 app_info_create(package, &ai_handle);
231                 if (!ai_handle) {
232                         LOGE("Failed to get app info handle");
233                         return;
234                 }
235
236                 app_mgr_item = __app_mgr_load_item_info(ai_handle);
237                 item = data_model_install_application(app_mgr_item);
238                 if (!item) {
239                         LOGE("Failed to create app item");
240                         app_info_destroy(ai_handle);
241                         return;
242                 }
243
244                 item->data->layout = app_icon_create(item, APP_ICON_TYPE_APP);
245                 app_info_destroy(ai_handle);
246                 home_screen_mvc_update_view();
247
248         }
249
250         if (PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL == event_type && progress == 100)
251                 LOGI("%s", package);
252 }
253
254 static bool __app_info_cb(app_info_h ai, void *ud)
255 {
256         app_mgr_item_t *app_mgr_item = NULL;
257         bool nodisplay = false;
258         Eina_List **list = (Eina_List **)ud;
259
260         if (app_info_is_nodisplay(ai, &nodisplay)) {
261                 LOGE("[FAILED][app_info_is_nodisplay]");
262                 return false;
263         }
264
265         #ifndef HOME_SCREEN_EFL_TEST_RUN
266                 if (nodisplay)
267                         return true;
268         #endif
269
270         app_mgr_item = __app_mgr_load_item_info(ai);
271
272         if (!app_mgr_item) {
273                 LOGE("[Failed to initialie app_mgr_item]");
274                 return false;
275         }
276
277
278         *list = eina_list_append(*list, app_mgr_item);
279         return true;
280 }
281
282 static app_mgr_item_t *__app_mgr_load_item_info(app_info_h ai_h)
283 {
284         package_info_h p_handle = NULL;
285         package_manager_error_e ret = PACKAGE_MANAGER_ERROR_NONE;
286
287         if (!ai_h) {
288                 LOGE("[INVALID_PARAMS]");
289                 return NULL;
290         }
291
292         app_mgr_item_t *item = (app_mgr_item_t *) calloc(1, sizeof(*item));
293         if (!item) {
294                 LOGE("Failed to create app_mgr_item");
295                 return NULL;
296         }
297
298         if (app_info_get_label(ai_h, &item->label)) {
299                 LOGE("[FAILED][app_info_get_label]");
300                 free(item);
301                 return NULL;
302         }
303
304         if (app_info_get_exec(ai_h, &item->exec)) {
305                 LOGE("[FAILED][app_info_get_exec]");
306                 free(item->label);
307                 free(item);
308                 return NULL;
309         }
310
311         if (app_info_get_icon(ai_h, &item->icon)) {
312                 LOGE("[FAILED][app_info_get_icon]");
313                 free(item->label);
314                 free(item->exec);
315                 free(item);
316                 return NULL;
317         }
318
319         if (app_info_get_app_id(ai_h, &item->appid)) {
320                 LOGE("[FAILED][app_info_get_app_id]");
321                 free(item->label);
322                 free(item->exec);
323                 free(item->icon);
324                 free(item);
325                 return NULL;
326         }
327
328         if (app_info_get_package(ai_h, &item->package)) {
329                 LOGE("[FAILED][app_info_get_package]");
330                 free(item->label);
331                 free(item->exec);
332                 free(item->icon);
333                 free(item->appid);
334                 free(item);
335                 return NULL;
336         }
337
338         ret = package_manager_get_package_info(item->package, &p_handle);
339         if (ret != PACKAGE_MANAGER_ERROR_NONE) {
340                 LOGW("Failed to inialize package handle for item : %s",
341                         item->package);
342                 item->removable = false;
343                 return item;
344         }
345
346         ret = package_info_is_removable_package(p_handle, &item->removable);
347
348         if (ret != PACKAGE_MANAGER_ERROR_NONE) {
349                 LOGE("Failed to get pacakge removeable flag");
350                 free(item->label);
351                 free(item->exec);
352                 free(item->icon);
353                 free(item->appid);
354                 free(item->package);
355                 free(item);
356                 return NULL;
357         }
358
359         package_info_destroy(p_handle);
360
361         return item;
362 }