06c594c9d5fc21415fee886eb0b703946d104959
[platform/core/security/krate.git] / tools / apps / kaskit / src / main.c
1 /*
2  * Tizen Krate launcher application
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <badge.h>
20 #include <app_control.h>
21 #include <app_manager.h>
22 #include <package_manager.h>
23
24 #include "kaskit.h"
25 #include "widget.h"
26
27 static package_manager_h __pkg_mgr;
28
29 static const char *__app_whitelist[] = {
30         "org.tizen.task-mgr",
31         NULL
32 };
33 static const char *__app_blacklist[] = {
34         "org.tizen.phone",
35         "org.tizen.message",
36         "org.tizen.setting",
37         NULL
38 };
39
40 struct app_icon_s {
41         char *id;
42         char *label;
43         char *icon;
44         char *package;
45         bool removable;
46 };
47
48 static void *__create_app_icon(void *data)
49 {
50         struct app_icon_s *app = (struct app_icon_s *)data;
51         unsigned int badge_show = 0, badge_count = 0;
52
53         _create_app_icon(app->package, app->id, app->label, app->icon, app->removable);
54
55         int ret = badge_get_display(app->id, &badge_show);
56         dlog_print(DLOG_ERROR, LOG_TAG, "badge_get_display err = %d", ret);
57         if (badge_show) {
58                 ret = badge_get_count(app->id, &badge_count);
59                 dlog_print(DLOG_ERROR, LOG_TAG, "badge_get_count err = %d", ret);
60                 if (badge_count > 0) {
61                         _update_app_icon_badge(app->id, badge_count);
62                 }
63         }
64
65         return NULL;
66 }
67
68 static bool __pkg_is_removable(const char *pkg_id)
69 {
70         bool removable = false;
71
72         package_info_h pkg_h;
73
74         package_info_create(pkg_id, &pkg_h);
75         package_info_is_removable_package(pkg_h, &removable);
76         package_info_destroy(pkg_h);
77
78         return removable;
79 }
80
81 static bool __app_is_in_blacklist(const char *app_id)
82 {
83         int i;
84         for (i = 0; __app_blacklist[i] != NULL; i++) {
85                 if (strcmp(app_id, __app_blacklist[i]) == 0) {
86                         return true;
87                 }
88         }
89         return false;
90 }
91
92 static bool __app_is_in_whitelist(const char *app_id)
93 {
94         int i;
95         for (i = 0; __app_whitelist[i] != NULL; i++) {
96                 if (strcmp(app_id, __app_whitelist[i]) == 0) {
97                         return true;
98                 }
99         }
100         return false;
101 }
102
103 static bool __get_app_info_cb(app_info_h app_h, void *user_data)
104 {
105         struct app_icon_s app = {NULL, };
106         bool nodisplay = true;
107
108         app_info_get_app_id(app_h, &app.id);
109         if (__app_is_in_blacklist(app.id)) {
110                 free(app.id);
111                 return true;
112         }
113
114         app_info_is_nodisplay(app_h, &nodisplay);
115         if (nodisplay && !__app_is_in_whitelist(app.id)) {
116                 free(app.id);
117                 return true;
118         }
119
120         if (user_data == NULL ||  !strncmp(user_data, app.package, PATH_MAX)) {
121                 app_info_get_label(app_h, &app.label);
122                 app_info_get_icon(app_h, &app.icon);
123                 app_info_get_package(app_h, &app.package);
124                 app.removable = __pkg_is_removable(app.package);
125
126                 ecore_main_loop_thread_safe_call_sync(__create_app_icon, &app);
127
128                 free(app.package);
129                 if (app.label != NULL) {
130                         free(app.label);
131                 }
132                 if (app.icon != NULL) {
133                         free(app.icon);
134                 }
135         }
136
137         free(app.id);
138         return true;
139 }
140
141 static void __create_icon_thread(void *data, Ecore_Thread *thread)
142 {
143         app_manager_foreach_app_info(__get_app_info_cb, data);
144         if (data != NULL) {
145                 free(data);
146         }
147         _set_kaskit_window_exit_cb();
148 }
149
150 static void __pkg_event_cb(const char *type,
151                            const char *pkg_id,
152                            package_manager_event_type_e event_type,
153                            package_manager_event_state_e event_state, int progress,
154                            package_manager_error_e error, void *user_data)
155 {
156         if (event_state == PACKAGE_MANAGER_EVENT_STATE_COMPLETED) {
157                 if (event_type == PACKAGE_MANAGER_EVENT_TYPE_INSTALL) {
158                         ecore_thread_run(__create_icon_thread, NULL, NULL, strdup(pkg_id));
159                 } else if (event_type == PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL) {
160                         _destroy_app_icon(pkg_id);
161                 }
162         }
163 }
164
165 static void __badge_changed_cb(unsigned int action, const char *app_id, unsigned int count, void *user_data)
166 {
167         _update_app_icon_badge(app_id, count);
168 }
169
170
171 static char *__get_current_krate_name()
172 {
173         struct passwd pwd, *result;
174         int bufsize;
175
176         bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
177         if (bufsize == -1) {
178                 bufsize = 16384;
179         }
180
181         char *ret, *buf = malloc(bufsize * sizeof(char));
182
183         if (buf == NULL) {
184                 return NULL;
185         }
186
187         getpwuid_r(getuid(), &pwd, buf, bufsize, &result);
188         if (result == NULL) {
189                 ret = NULL;
190         } else {
191                 ret = strdup(result->pw_name);
192         }
193         free(buf);
194         return ret;
195 }
196
197 void _icon_clicked_cb(const char *app_id)
198 {
199         app_control_h app_control;
200         app_control_create(&app_control);
201         app_control_set_app_id(app_control, app_id);
202         app_control_send_launch_request(app_control, NULL, NULL);
203         app_control_destroy(app_control);
204 }
205
206 void _icon_uninstalled_cb(const char *pkg_id)
207 {
208         package_manager_request_h pkg_mgr_req;
209         int id;
210
211         package_manager_request_create(&pkg_mgr_req);
212         package_manager_request_uninstall(pkg_mgr_req, pkg_id, &id);
213         package_manager_request_destroy(pkg_mgr_req);
214 }
215
216 static void __show_launcher()
217 {
218         char *krate_name = __get_current_krate_name();
219
220         _set_kaskit_window_title(krate_name);
221
222         package_manager_set_event_status(__pkg_mgr,
223                                                  PACKAGE_MANAGER_STATUS_TYPE_INSTALL |
224                                                  PACKAGE_MANAGER_STATUS_TYPE_UNINSTALL);
225         package_manager_set_event_cb(__pkg_mgr, __pkg_event_cb, NULL);
226
227         badge_register_changed_cb(__badge_changed_cb, NULL);
228
229         ecore_thread_run(__create_icon_thread, NULL, NULL, NULL);
230
231         free(krate_name);
232 }
233
234 static bool __app_create(void *data)
235 {
236         package_manager_create(&__pkg_mgr);
237
238         _create_kaskit_window();
239         __show_launcher();
240
241         return true;
242 }
243
244 static void __app_control(app_control_h app_control, void *data)
245 {
246 }
247
248 static void __app_pause(void *data)
249 {
250 }
251
252 static void __app_resume(void *data)
253 {
254 }
255
256 static void __app_terminate(void *data)
257 {
258         package_manager_destroy(__pkg_mgr);
259 }
260
261 int main(int argc, char *argv[])
262 {
263         int ret = 0;
264
265         ui_app_lifecycle_callback_s event_callback = {0, };
266
267         event_callback.create = __app_create;
268         event_callback.terminate = __app_terminate;
269         event_callback.pause = __app_pause;
270         event_callback.resume = __app_resume;
271         event_callback.app_control = __app_control;
272
273         ret = ui_app_main(argc, argv, &event_callback, NULL);
274         if (ret != APP_ERROR_NONE)
275                 dlog_print(DLOG_ERROR, LOG_TAG, "ui_app_main is failed. err = %d", ret);
276
277         return ret;
278 }