Update changed code
[apps/home/taskmanager.git] / src / _logic.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  * 
4  * Licensed under the Flora License, Version 1.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.tizenopensource.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
18
19
20 #include <appcore-common.h>
21 #include <aul.h>
22 #include <Ecore_X.h>
23 #include <vconf.h>
24
25 #include "taskmanager.h"
26 #include "_util_log.h"
27 #include "_util_efl.h"
28 #include "_genlist.h"
29 #include "_eina.h"
30 #include "_progressbar.h"
31 #include "_info.h"
32
33 int _dead_cb(int pid, void *data)
34 {
35 _D("func\n");
36         /* redraw list */
37         struct appdata *ad = (struct appdata *)data;
38         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
39
40         _fini_pthread();
41         if (ad->update_timer) {
42                 ecore_timer_del(ad->update_timer);
43                 ad->update_timer = NULL;
44         }
45         _subt_einalist_item(ad, pid);
46
47         _D("mode(%d) count(%d)\n", ad->mode, ad->endcnt);
48         switch (ad->mode) {
49         default:
50         case MODE_END_INUSE:
51         case MODE_DEL_HISTORY:
52         case MODE_DEL_ALL_HISTORY:
53         case MODE_KILL_INUSE:
54                 _D("aa\n");
55                 _del_popup_timer(ad);
56                 _del_progressbar(ad);
57                 refresh_app_info(ad);
58                 _restart_pthread(ad);
59                 break;
60
61         case MODE_END_ALL_INUSE:
62         case MODE_KILL_ALL_INUSE:
63                 _D("bb\n");
64                 if (ad->endcnt <= 1) {
65                         _D("count set 0\n");
66                         _del_popup_timer(ad);
67                         _del_progressbar(ad);
68                         refresh_app_info(ad);
69                         _restart_pthread(ad);
70
71                 } else {
72                         ad->endcnt--;
73                 }
74                 break;
75         }
76 }
77
78 static void _back_cb(void *data, Evas_Object *obj, void *event_info)
79 {
80         struct appdata *ad = (struct appdata *)data;
81         /*
82         _unset_notification_level(ad->win);
83         */
84         elm_exit();
85
86 }
87
88 static void _drag_start_cb(void *data, Evas_Object *obj, void *event_info)
89 {
90         _D("func\n");
91         struct appdata *ad = (struct appdata *)data;
92         if (ad->update_timer) {
93                 ecore_timer_del(ad->update_timer);
94                 ad->update_timer = NULL;
95         }
96 }
97
98 static void _drag_stop_cb(void *data, Evas_Object *obj, void *event_info)
99 {
100         _D("func\n");
101         struct appdata *ad = (struct appdata *)data;
102         if (ad->update_timer) {
103                 ecore_timer_del(ad->update_timer);
104                 ad->update_timer = NULL;
105         }
106         ad->update_timer = ecore_timer_add(2.0, _update_list, ad);
107 }
108
109
110 static void _anim_start_cb(void *data, Evas_Object *obj, void *event_info)
111 {
112         _D("func\n");
113         struct appdata *ad = (struct appdata *)data;
114         if (ad->update_timer) {
115                 ecore_timer_del(ad->update_timer);
116                 ad->update_timer = NULL;
117         }
118 }
119
120 static void _anim_stop_cb(void *data, Evas_Object *obj, void *event_info)
121 {
122         _D("func\n");
123         struct appdata *ad = (struct appdata *)data;
124         if (ad->update_timer) {
125                 ecore_timer_del(ad->update_timer);
126                 ad->update_timer = NULL;
127         }
128         ad->update_timer = ecore_timer_add(2.0, _update_list, ad);
129 }
130
131 static void _moved_cb(void *data, Evas_Object *obj, void *event_info)
132 {
133         _D("func\n");
134 }
135
136 int _app_create(struct appdata *ad)
137 {
138         Evas_Object *ly, *bg, *nv, *bt, *gl;
139
140         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
141         ad->ending = EINA_FALSE;
142
143         ly = _add_layout_main(ad->win, EINA_TRUE, EINA_FALSE);
144         retvm_if(ly == NULL, -1, "Failed to add layout main\n");
145
146         bg = _add_bg(ad->win, "group_list");
147         retvm_if(bg == NULL, -1, "Failed to add bg\n");
148         elm_object_part_content_set(ly, "elm.swallow.bg", bg);
149
150         nv = _add_naviframe(ly);
151         retvm_if(nv == NULL, -1, "Failed to add naviframe\n");
152         ad->nv = nv;
153
154         ly = _add_layout(ad->nv, EDJ_NAME, GRP_TM);
155         retvm_if(ly == NULL, -1, "Failed to add layout\n");
156         ad->ly = ly;
157
158         /* Load default content (running task) */
159         gl = _add_genlist(ly);
160         retvm_if(gl == NULL, -1, "Failed to add genlist\n");
161         elm_genlist_block_count_set(gl, 20);
162         evas_object_data_set(gl, "appdata", ad);
163         elm_object_part_content_set(ly, "list", gl);
164         evas_object_smart_callback_add(gl, "scroll,drag,start", _drag_start_cb, ad);
165         evas_object_smart_callback_add(gl, "scroll,drag,stop", _drag_stop_cb, ad);
166         evas_object_smart_callback_add(gl, "scroll,anim,start", _anim_start_cb, ad);
167         evas_object_smart_callback_add(gl, "scroll,anim,stop", _anim_stop_cb, ad);
168 //      evas_object_smart_callback_add(gl, "edge,bottom", _moved_cb, ad);
169         ad->gl = gl;
170
171         bt = elm_button_add(nv);
172         retvm_if(bt == NULL, -1, "Failed to add button\n");
173         elm_object_style_set(bt, "naviframe/end_btn/default");
174         evas_object_smart_callback_add(bt, "clicked", _back_cb, ad);
175
176         elm_naviframe_item_push(nv,
177                         _("IDS_TASKMGR_HEADER_TASK_SWITCHER"),
178                         bt, NULL, ly, NULL);
179
180         return 0;
181 }
182
183 static void _get_win_geometry(struct appdata *ad)
184 {
185         Ecore_X_Window focus_win;
186         Ecore_X_Window root_win;
187
188         focus_win = ecore_x_window_focus_get();
189         root_win = ecore_x_window_root_get(focus_win);
190         ecore_x_window_size_get(root_win, &ad->root_w, &ad->root_h);
191 }
192
193 static void _vconf_noti_cb(keynode_t *node, void *data)
194 {
195         elm_exit();
196 }
197
198 static void _set_vconf_noti(void *data)
199 {
200         vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
201                         _vconf_noti_cb, NULL);
202 }
203
204 /* this func is to exit taskmanager after launching application */
205 static Eina_Bool __climsg_cb(void *data, int type, void *event)
206 {
207         static Atom a_deact;
208         pid_t pid_a, pid_d;
209
210         struct appdata *ad = (struct appdata *)data;
211         Ecore_X_Event_Client_Message *ev = event;
212
213         if(ev == NULL) {
214                 _E("Invalid argument: event is NULL\n");
215                 ecore_timer_add(0.3, _exit_cb, NULL);
216                 return ECORE_CALLBACK_CANCEL;
217         }
218
219         pid_a = ev->data.l[1];
220         pid_d = ev->data.l[3];
221         a_deact = ecore_x_atom_get("_X_ILLUME_DEACTIVATE_WINDOW");
222
223         /* when pid_a == pid_d, this is useless data */
224         if (pid_a == pid_d) {
225                 return ECORE_CALLBACK_RENEW;
226         }
227
228         if (ev->message_type == a_deact) {
229                 _D("exit after 1.0 sec\n");
230                 ad->exit_timer = ecore_timer_add(0.3, _exit_cb, ad);
231                 return ECORE_CALLBACK_CANCEL;
232         } else {
233                 _D("messagre is act\n");
234
235         }
236
237         return ECORE_CALLBACK_CANCEL;
238 }
239
240 static Eina_Bool _ask_kill_process(void *data)
241 {
242         struct appdata *ad = data;
243
244         _del_progressbar(ad);
245
246         ad->mode *= 2;
247         /* why? check enum in taskmgr.h */
248
249         if (ad->popup_ask) {
250                 evas_object_del(ad->popup_ask);
251                 ad->popup_ask = NULL;
252         }
253         ad->popup_ask = _add_popup_ask(ad->win,
254                         "It might be an invalid process. Do you want to kill this proceess anyway?",
255                         ad);
256
257         return ECORE_CALLBACK_CANCEL;
258 }
259
260 void _ok_response_cb(void *data, Evas_Object *obj, void *event_info)
261 {
262         struct appdata *ad = (struct appdata *)data;
263
264         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
265         if (ad->popup_ask) {
266                 evas_object_del(ad->popup_ask);
267                 ad->popup_ask = NULL;
268         }
269
270         switch (ad->mode) {
271                 case MODE_END_INUSE:
272                         _D("end inuse\n");
273                         _del_popup_timer(ad);
274                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, ad);
275                         _show_progressbar(ad);
276                         response_end_inuse(ad);
277                         _restart_pthread(ad);
278                         break;
279
280                 case MODE_END_ALL_INUSE:
281                         _D("end all inuse\n");
282                         _del_popup_timer(ad);
283                         ad->popup_timer = ecore_timer_add(7.0, _ask_kill_process, ad);
284                         _show_progressbar(ad);
285                         response_end_all_inuse(ad);
286                         break;
287
288                 case MODE_DEL_HISTORY:
289                         _D("del inuse\n");
290                         _del_popup_timer(ad);
291                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, ad);
292                         response_del_history(ad);
293                         break;
294
295                 case MODE_DEL_ALL_HISTORY:
296                         _D("del all inuse\n");
297                         _del_popup_timer(ad);
298                         ad->popup_timer = ecore_timer_add(5.0, _ask_kill_process, ad);
299                         response_del_all_history(ad);
300                         break;
301
302                 case MODE_KILL_INUSE:
303                         _D("kill all inuse\n");
304                         response_kill_inuse(ad);
305                         break;
306
307                 case MODE_KILL_ALL_INUSE:
308                         _D("kill all inuse\n");
309                         response_kill_all_inuse(ad);
310                         break;
311
312                 default:
313                         printf("[Wanning] taskmanager: check mode [%d]\n",
314                                         ad->mode);
315                         break;
316         }
317 }
318
319 void _cancel_response_cb(void *data, Evas_Object *obj, void *event_info)
320 {
321         struct appdata *ad = (struct appdata *)data;
322
323         retm_if(data == NULL, "Invalid argument: appdata is NULL\n");
324         if (ad->popup_ask) {
325                 evas_object_del(ad->popup_ask);
326                 ad->popup_ask = NULL;
327         }
328 }
329
330 Eina_Bool _create_idler_cb(void *data)
331 {
332         Evas_Object *pu, *bt1, *bt2;
333         struct appdata *ad = (struct appdata *)data;
334         retvm_if(ad == NULL, ECORE_CALLBACK_CANCEL, "Invalid argument\n");
335
336         evas_object_show(ad->win);
337
338         _key_grab(ad);
339
340         _init_pthread();
341         _get_win_geometry(ad);
342         _set_vconf_noti(ad);
343         ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __climsg_cb, ad);
344
345         return ECORE_CALLBACK_CANCEL;
346 }
347
348
349