Fix issue
[apps/home/taskmanager.git] / src / _genlist.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 <stdio.h>
21 #include <unistd.h>
22 #include <appcore-efl.h>
23 #include <vconf.h>
24 #include <utilX.h>
25 #include <aul.h>
26 #include <Ecore_X.h>
27 #include <Eina.h>
28 #include <unistd.h>
29 #include <time.h>
30 #include <sys/time.h>
31 #include <pthread.h>
32
33 #include "taskmanager.h"
34 #include "_genlist.h"
35 #include "_util_log.h"
36 #include "_util_efl.h"
37 #include "_logic.h"
38 #include "_cpu.h"
39 #include "_eina.h"
40 #include "_info.h"
41 #include "_progressbar.h"
42
43 pthread_t pt;
44 pthread_cond_t  pc;
45 pthread_mutex_t pm;
46 pthread_mutex_t mutex_for_graph_update = PTHREAD_MUTEX_INITIALIZER;
47 static Elm_Object_Item *g_egi;
48
49 /* group list:gl, data list:dl, button list:bl, no list: nl */
50 static Elm_Genlist_Item_Class itc_gl;
51 static Elm_Genlist_Item_Class itc_hl;
52 static Elm_Genlist_Item_Class itc_dl;
53 static Elm_Genlist_Item_Class itc_bl;
54 static Elm_Genlist_Item_Class itc_nl;
55 static Elm_Genlist_Item_Class itc_separator4;
56 static Elm_Genlist_Item_Class itc_separator2;
57
58 static char *button_text[TS_MAX] = {
59         "IDS_TASKMGR_BUTTON_CLOSE_ALL_APPLICATIONS",
60         "IDS_TASKMGR_BUTTON_CLEAR_HISTORY_ABB"
61 };
62
63 static void end_all_inuse_cb(void *data, Evas_Object *obj, void *event_info);
64 static void delete_all_history_cb(void *data, Evas_Object *obj,
65                                   void *event_info);
66 void (*func_del[TS_MAX]) (void *data, Evas_Object *obj, void *event_info) = {
67 &end_all_inuse_cb, &delete_all_history_cb};
68
69 static void end_inuse_cb(void *data, Evas_Object *obj, void *event_info);
70 static void delete_history_cb(void *data, Evas_Object *obj, void *event_info);
71 void (*func_end[TS_MAX]) (void *data, Evas_Object *obj, void *event_info) = {
72 &end_inuse_cb, &delete_history_cb};
73
74 static char *group_name[TS_MAX] = {
75         "IDS_TASKMGR_HEADER_RUNNING",
76         "IDS_TASKMGR_MBODY_RECENTLY_USED"
77 };
78
79 static char *nolist_text[TS_MAX] = {
80         "IDS_TASKMGR_MBODY_NO_APPS_OPEN",
81         "IDS_TASKMGR_MBODY_NO_RECENTLY_USED_APPS"
82 };
83
84 void taskmanager_free_info(struct _task_info *info);
85 Eina_Bool _update_list(void *data);
86
87 static void clear_genlist(void *data)
88 {
89         ret_if(data == NULL);
90
91         struct appdata *ad = data;
92
93         if (ad->gl) {
94                 elm_genlist_clear(ad->gl);
95         }
96 }
97
98 void clear_task_manager_list(void *data)
99 {
100         ret_if(data == NULL);
101
102         struct appdata *ad = data;
103         _free_einalist_all(ad);
104         clear_genlist(ad);
105 }
106
107 static void app_genlist_item_update(void *data)
108 {
109         ret_if(data == NULL);
110
111         struct appdata *ad = (struct appdata *)data;
112
113         Elm_Object_Item *it_r;
114         Eina_List *realized_item_list, *l_r;
115         unsigned int cnt = 0;
116
117         realized_item_list = elm_genlist_realized_items_get(ad->gl);
118         cnt = eina_list_count(realized_item_list);
119         if (cnt > 0) {
120                 EINA_LIST_FOREACH(realized_item_list, l_r, it_r) {
121                         if (it_r != NULL) {
122                                 elm_genlist_item_update(it_r);
123                         }
124                 }
125         }
126 }
127
128 Eina_Bool alert_app_info(void *data)
129 {
130         retv_if(data == NULL, -1);
131
132         struct appdata *ad = (struct appdata *)data;
133
134         app_genlist_item_update(ad);
135         return ECORE_CALLBACK_CANCEL;
136 }
137
138 static void end_all_inuse_cb(void *data, Evas_Object *obj, void *event_info)
139 {
140         ret_if(data == NULL);
141
142         struct appdata *ad = data;
143         char buf[_BUF_MAX] = { 0, };
144
145         ad->mode = MODE_END_ALL_INUSE;
146         snprintf(buf, sizeof(buf), T_("IDS_TASKMGR_POP_CLOSE_ALL_APPS_Q_THIS_MAY_CAUSE_DATA_TO_BE_LOST"));
147         if (ad->popup_ask) {
148                 evas_object_del(ad->popup_ask);
149                 ad->popup_ask = NULL;
150         }
151         ad->popup_ask = _add_popup_ask(ad->win, buf, ad);
152 }
153
154 static void
155 delete_all_history_cb(void *data, Evas_Object *obj, void *event_info)
156 {
157 _D("func\n");
158         ret_if(data == NULL);
159
160         struct appdata *ad = data;
161         char buf[_BUF_MAX] = { 0, };
162
163         ad->mode = MODE_DEL_ALL_HISTORY;
164         snprintf(buf, sizeof(buf), T_("IDS_TASKMGR_POP_CLEAR_ALL_APP_HISTORY_Q"));
165         if (ad->popup_ask) {
166                 evas_object_del(ad->popup_ask);
167                 ad->popup_ask = NULL;
168         }
169         ad->popup_ask = _add_popup_ask(ad->win, buf, ad);
170 }
171
172 static void end_inuse_cb(void *data, Evas_Object *obj, void *event_info)
173 {
174         ret_if(data == NULL);
175
176         struct _task_info *info_ev = (struct _task_info *)data;
177         struct appdata *ad = info_ev->ad;
178         char buf[_BUF_MAX] = { 0, };
179
180         ad->mode = MODE_END_INUSE;
181         snprintf(buf, _BUF_MAX, T_("IDS_TASKMGR_POP_CLOSE_PS_APP_Q_THIS_MAY_CAUSE_DATA_TO_BE_LOST"), info_ev->app_name);
182         if (ad->popup_ask) {
183                 evas_object_del(ad->popup_ask);
184                 ad->popup_ask = NULL;
185         }
186         ad->popup_ask = _add_popup_ask(ad->win, buf, ad);
187         g_egi = (void *)info_ev->it;
188 }
189
190 static void delete_history_cb(void *data, Evas_Object *obj, void *event_info)
191 {
192         ret_if(data == NULL);
193
194         struct _task_info *info_ev = (struct _task_info *)data;
195         struct appdata *ad = info_ev->ad;
196         char buf[_BUF_MAX] = { 0, };
197
198         ad->mode = MODE_DEL_HISTORY;
199
200         snprintf(buf, _BUF_MAX, T_("IDS_TASKMGR_POP_CLEAR_PS_HISTORY_Q"), info_ev->app_name);
201         if (ad->popup_ask) {
202                 evas_object_del(ad->popup_ask);
203                 ad->popup_ask = NULL;
204         }
205         ad->popup_ask = _add_popup_ask(ad->win, buf, ad);
206         g_egi = (void *)info_ev->it;
207 }
208
209 static void nl_sel(void *data, Evas_Object *obj, void *event_info)
210 {
211 _D("func\n");
212         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
213         elm_genlist_item_selected_set(item, EINA_FALSE);
214         return;
215 }
216
217 static char *nl_text_get(void *data, Evas_Object *obj, const char *part)
218 {
219         char buf[_BUF_MAX] = { 0, };
220
221         if (!strcmp(part, "elm.text")) {
222                 snprintf(buf, sizeof(buf), "%s", T_(nolist_text[(int)data]));
223
224                 return strdup(buf);
225         }
226         return NULL;
227 }
228
229 static void _gl_sel_app(void *data, Evas_Object *obj, void *event_info)
230 {
231 _D("func\n");
232         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
233         struct appdata *ad = (struct appdata *)data;
234         struct _task_info *info;
235         /* parameter to block double click */
236         static int selected = 0;
237         Elm_Object_Item *it = (Elm_Object_Item *) event_info;
238
239         elm_genlist_item_selected_set(it, EINA_FALSE);
240
241         retm_if(ad == NULL, "Invalid argument: appdata is NULL\n");
242
243         _fini_pthread();
244         if (ad->update_timer) {
245                 ecore_timer_del(ad->update_timer);
246                 ad->update_timer = NULL;
247         }
248
249         if (selected == 1)
250                 return;
251         selected = 1;
252
253         if (item == NULL) {
254                 _E("[Error] Cannot find genlist item\n");
255                 selected = 0;
256                 return;
257         }
258
259         info = (struct _task_info *)elm_object_item_data_get(item);
260         if (info == NULL) {
261                 _E("[Error] Cannot get item data: info\n");
262                 selected = 0;
263                 return;
264         }
265
266         if (info->pid) {
267                 /* when application is alive */
268                 aul_resume_pid(info->pid);
269                 selected = 0;
270         } else {
271                 /* when application is dead */
272                 if (info->pkg_name == NULL) {
273                         util_show_popup_with_message(info->ad->win,
274                                         3.0,
275                                         T_("IDS_TASKMGR_POP_UNABLE_TO_OPEN_APPLICATION"));
276                         selected = 0;
277
278                 } else {
279                         _unset_notification_level(info->ad->win);
280
281                         if (!strcmp(info->pkg_name, "org.tizen.phone")) {
282                                 /* exception : Because dialer doesn't need bundle
283                                  * since being unifyed dialer, voice call and video call
284                                  */
285                                 aul_launch_app(info->pkg_name, NULL);
286                                 selected = 0;
287                         } else {
288                                 aul_launch_app(info->pkg_name, info->b);
289                                 selected = 0;
290                         }
291                 }
292         }
293 }
294
295 static Evas_Object *_gl_content_get_app(void *data, Evas_Object *obj,
296                                      const char *part)
297 {
298         struct _task_info *info = (struct _task_info *)data;
299         char buf[_BUF_MAX] = { 0, };
300
301         Evas_Object *icon = NULL;
302         Evas_Object *btn = NULL;
303
304         Evas_Object *rt, *icon_ly = NULL;
305
306         retvm_if(data == NULL, NULL, "Invalid argument: task info is NULL\n");
307
308         if (!strcmp(part, "elm.icon.1")) {
309                 snprintf(buf, sizeof(buf), "%s", info->icn_path);
310                 retvm_if(buf == NULL, NULL, "%s icon is NULL\n", info->app_name);
311                 if (!ecore_file_exists(buf) || strlen(buf) < 4)
312                         snprintf((char *)buf, (size_t) sizeof(buf),
313                                  (const char *)IMAGEDIR "/icon_taskmgr.png");
314
315                 if (!strncmp(&buf[strlen(buf) - 3], "edj", 3)) {
316                         icon_ly = _add_layout(obj, buf, "icon");
317
318                 } else {
319                         icon_ly = elm_icon_add(obj);
320                         elm_icon_file_set(icon_ly, buf, NULL);
321                 }
322
323                 icon = _add_layout(obj, EDJ_NAME, "icon");
324                 retvm_if(icon == NULL, NULL, "Cannot add layout: icon\n");
325
326                 rt = evas_object_rectangle_add(evas_object_evas_get(obj));
327                 retvm_if(rt == NULL, NULL, "Failed to add rectangle\n");
328
329                 evas_object_color_set(rt, 0, 0, 0, 0);
330                 evas_object_size_hint_min_set(rt, 
331                                         (int)72 * elm_scale_get(), 
332                                         (int)72 * elm_scale_get());
333                 elm_object_part_content_set(icon, "icon_ly", rt);
334
335                 elm_object_part_content_set(icon, "icon", icon_ly);
336
337                 return icon;
338
339         } else if (!strcmp(part, "elm.icon.2")) {
340                 btn = elm_button_add(obj);
341                 elm_object_text_set(btn, S_("IDS_COM_BODY_END"));
342                 elm_object_style_set(btn, "default");
343
344                 evas_object_smart_callback_add(btn, "clicked",
345                                                func_end[info->category], info);
346                 elm_object_focus_set(btn, EINA_FALSE);
347                 evas_object_propagate_events_set(btn, EINA_FALSE);
348
349                 return btn;
350         }
351
352         return NULL;
353 }
354
355 static char *_gl_text_get_app(void *data, Evas_Object *obj, const char *part)
356 {
357         struct _task_info *info = (struct _task_info *)data;
358         char buf[_BUF_MAX] = { 0, };
359         Evas_Object *eo;
360
361         retvm_if(data == NULL, NULL, "Invalid argument: task info is NULL\n");
362         retvm_if(part == NULL, NULL, "Invalid argument: part is NULL\n");
363
364         if (!strcmp(part, "elm.text.1")) {
365                 snprintf(buf, _BUF_MAX, "%s", info->app_name);
366                 return strdup(buf);
367
368         } else if (!strcmp(part, "elm.text.2")) {
369                 if (info->category == TS_INUSE) {
370                         snprintf(buf, _BUF_MAX, "CPU: %.1f%%", info->cpu);
371                         return strdup(buf);
372
373                 } else {
374                         if (info->it) {
375                                 elm_object_signal_emit(info->it, "prog.hide.mem","taskmanager");
376                         }
377                         return NULL;
378                 }
379         }
380         return NULL;
381 }
382
383 static void _bl_sel(void *data, Evas_Object *obj, void *event_info)
384 {
385 _D("func\n");
386         int mode = (int)data;
387         struct appdata *ad;
388         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
389
390         elm_genlist_item_selected_set(item, EINA_FALSE);
391 }
392
393 static char *_bl_text_get(void *data, Evas_Object *obj, const char *part)
394 {
395         if (!strcmp(part, "elm.text")) {
396                 return strdup(T_(button_text[(int)data]));
397
398         }
399         return NULL;
400 }
401
402 static Evas_Object *_bl_content_get(void *data, Evas_Object *obj,
403                                 const char *part)
404 {
405         Evas_Object *btn = NULL;
406         struct appdata *ad = evas_object_data_get(obj, "appdata");
407
408         if (!strcmp(part, "elm.icon")) {
409
410                 btn = elm_button_add(obj);
411                 elm_object_style_set(btn, "default");
412
413                 elm_object_text_set(btn, T_(button_text[(int)data]));
414                 evas_object_smart_callback_add(btn, "clicked",
415                                                func_del[(int)data], ad);
416                 elm_object_focus_set(btn, EINA_FALSE);
417
418                 evas_object_size_hint_min_set(btn, 0, 50);
419                 evas_object_size_hint_max_set(btn, 0, 50);
420                 evas_object_propagate_events_set(btn, EINA_FALSE);
421
422                 return btn;
423
424         }
425         return NULL;
426 }
427
428 static char *_gl_text_get_title(void *data, Evas_Object *obj, const char *part)
429 {
430         char buf[_BUF_MAX];
431
432         if (!strcmp(part, "elm.text")) {
433                 snprintf(buf, sizeof(buf), "%s (%d)",
434                                 T_(group_name[(int)data]), _get_grp_cnt((int)data));
435                 return strdup(buf);
436         }
437         return NULL;
438 }
439
440 static char *_gl_text_get_his(void *data, Evas_Object *obj, const char *part)
441 {
442         struct _task_info *info = (struct _task_info *)data;
443         char buf[_BUF_MAX] = { 0, };
444         Evas_Object *eo;
445
446         if (!strcmp(part, "elm.text")) {
447                 snprintf(buf, _BUF_MAX, "%s", info->app_name);
448                 return strdup(buf);
449         }
450         return NULL;
451 }
452
453 static Evas_Object *_gl_content_get_his(void *data, Evas_Object *obj,
454                                      const char *part)
455 {
456         struct _task_info *info = (struct _task_info *)data;
457         char buf[_BUF_MAX] = { 0, };
458
459         Evas_Object *icon = NULL;
460         Evas_Object *btn = NULL;
461
462         Evas_Object *rt, *icon_ly = NULL;
463         retvm_if(data == NULL, NULL, "Invalid argument: task info is NULL\n");
464
465         if (!strcmp(part, "elm.icon.1")) {
466                 snprintf(buf, sizeof(buf), "%s", info->icn_path);
467                 retvm_if(buf == NULL, NULL, "%s icon is NULL\n", info->app_name);
468                 if (!ecore_file_exists(buf) || strlen(buf) < 4)
469                         snprintf((char *)buf, (size_t) sizeof(buf),
470                                  (const char *)IMAGEDIR "/icon_taskmgr.png");
471
472                 if (!strncmp(&buf[strlen(buf) - 3], "edj", 3)) {
473                         icon_ly = _add_layout(obj, buf, "icon");
474
475                 } else {
476                         icon_ly = elm_icon_add(obj);
477                         elm_icon_file_set(icon_ly, buf, NULL);
478                 }
479
480                 icon = _add_layout(obj, EDJ_NAME, "icon");
481                 retvm_if (icon == NULL, NULL, "Cannot add layout: icon\n");
482
483                 rt = evas_object_rectangle_add(evas_object_evas_get(obj));
484                 retvm_if (rt == NULL, NULL, "Failed to add rectangle\n");
485
486                 evas_object_color_set(rt, 0, 0, 0, 0);
487                 evas_object_size_hint_min_set(rt, 
488                                         (int)72 * elm_scale_get(), 
489                                         (int)72 * elm_scale_get());
490                 elm_object_part_content_set(icon, "icon_ly", rt);
491
492                 elm_object_part_content_set(icon, "icon", icon_ly);
493
494                 return icon;
495
496         } else if (!strcmp(part, "elm.icon.2")) {
497                 btn = elm_button_add(obj);
498                 elm_object_text_set(btn, S_("IDS_COM_OPT_DELETE"));
499                 elm_object_style_set(btn, "default");
500
501                 evas_object_smart_callback_add(btn, "clicked",
502                                                func_end[info->category], info);
503                 elm_object_focus_set(btn, EINA_FALSE);
504                 evas_object_propagate_events_set(btn, EINA_FALSE);
505
506                 return btn;
507         }
508
509         return NULL;
510
511 }
512
513 void _set_itc(void)
514 {
515         itc_gl.item_style = "grouptitle";
516         itc_gl.func.text_get = _gl_text_get_title;
517
518         itc_dl.item_style = "2text.2icon.7";
519         itc_dl.func.text_get = _gl_text_get_app;
520         itc_dl.func.content_get = _gl_content_get_app;
521
522         itc_hl.item_style = "1text.2icon.1";
523         itc_hl.func.text_get = _gl_text_get_his;
524         itc_hl.func.content_get = _gl_content_get_his;
525
526         itc_separator4.item_style = "dialogue/seperator.4";
527         itc_separator2.item_style = "dialogue/seperator.2";
528
529         itc_bl.item_style = "dialogue/bg/1icon";
530         itc_bl.func.content_get = _bl_content_get;
531
532         itc_nl.item_style = "1text";
533         itc_nl.func.text_get = nl_text_get;
534
535 }
536
537 int check_genlist(struct appdata *ad)
538 {
539         Elm_Object_Item *egi;
540         struct _task_info *info;
541         int i = 1;
542
543         egi = elm_genlist_first_item_get(ad->gl);
544         while(egi) {
545                 info = (struct _task_info *)elm_object_item_data_get(egi);
546                 if(info) {
547                         _D("%s info[0x%x]\n", (int)info < 3 ? "-" : info->app_name, info);
548                 } else {
549                         _D("group\n");
550                 }
551                 egi = elm_genlist_item_next_get(egi);
552         }
553
554         return 0;
555 }
556
557 int _set_genlist_from_eina(struct appdata *ad)
558 {
559 _D("func\n");
560         Eina_List *l;
561         Elm_Object_Item *git, *item;
562         struct _task_info *info;
563         int i;
564
565         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
566         retvm_if(ad->gl == NULL, -1, "Invalid argument:genlist is NULL\n");
567
568         for (i = 0; i < TS_MAX; i++) {
569                 git = elm_genlist_item_append(ad->gl, &itc_gl,
570                                                  (void *)i, NULL,
571                                                  ELM_GENLIST_ITEM_NONE,
572                                               NULL, NULL);
573                 retvm_if(git == NULL, -1, "Failed append item\n");
574                 elm_genlist_item_select_mode_set(git, EINA_TRUE);
575
576                 if (eina_list_count(ad->applist[i]) > 0) {
577
578                         item = elm_genlist_item_append(ad->gl, &itc_separator4,
579                                                 NULL, git,
580                                                 ELM_GENLIST_ITEM_NONE,
581                                                 NULL, NULL);
582                         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
583
584
585                         item = elm_genlist_item_append(ad->gl, &itc_bl,
586                                                 (void *)i, git,
587                                                 ELM_GENLIST_ITEM_NONE,
588                                                 _bl_sel, (void *)i);
589
590                         item = elm_genlist_item_append(ad->gl, &itc_separator2,
591                                                 NULL, git,
592                                                 ELM_GENLIST_ITEM_NONE,
593                                                 NULL, NULL);
594                         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
595
596                         ad->applist[i] = eina_list_nth_list(ad->applist[i], 0);
597                         EINA_LIST_FOREACH(ad->applist[i], l, info) {
598                                 if (info != NULL) {
599                                         info->it = elm_genlist_item_append(ad->gl,
600                                                                     (i == TS_INUSE) ? &itc_dl : &itc_hl,
601                                                                     (void *)info, git,
602                                                                     ELM_GENLIST_ITEM_NONE,
603                                                                     _gl_sel_app, ad);
604                                 }
605                         }
606                 } else {
607                         item = elm_genlist_item_append(ad->gl, &itc_nl,
608                                                 (void *)i, git,
609                                                 ELM_GENLIST_ITEM_NONE,
610                                                 nl_sel, NULL);
611                         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
612                 }
613         }
614         return 0;
615 }
616
617 Eina_Bool _update_list(void *data)
618 {
619 _D("func\n");
620         struct appdata *ad = data;
621         Eina_List *l, *l_next;
622         struct _task_info *info;
623
624         pthread_mutex_lock(&mutex_for_graph_update);
625
626         if(ad->applist[TS_INUSE] == NULL) {
627                 _D("inuse is NULL\n");
628                 return ECORE_CALLBACK_CANCEL;
629         }
630         ad->applist[TS_INUSE] = eina_list_nth_list(ad->applist[TS_INUSE], 0);
631         _D("%d\n", eina_list_count(ad->applist[TS_INUSE]));
632
633         EINA_LIST_FOREACH_SAFE(ad->applist[TS_INUSE], l, l_next, info) {
634                 elm_genlist_item_update(info->it);
635
636         }
637         pthread_mutex_unlock(&mutex_for_graph_update);
638         return ECORE_CALLBACK_RENEW;
639 }
640
641 void *_update_pthread_cb(void *data)
642 {
643 _D("func\n");
644         struct appdata *ad = data;
645         long tick;
646         int ncpu;
647         Eina_List *l, *l_next;
648         struct _task_info *info;
649
650         while(1) {
651
652                 if (ad->ending == EINA_FALSE) {
653                         if(ad->applist[TS_INUSE] == NULL) {
654                                 _D("in use list is NULL\n");
655                                 return ECORE_CALLBACK_CANCEL;
656                         }
657
658                         _get_sysconf(&ncpu, &tick);
659
660                         ad->applist[TS_INUSE] = eina_list_nth_list(ad->applist[TS_INUSE], 0);
661                         if (eina_list_count(ad->applist[TS_INUSE]) < 1) {
662                                 _D("list count is 0\n");
663                                 return NULL;
664                         }
665
666                         EINA_LIST_FOREACH_SAFE(ad->applist[TS_INUSE], l, l_next, info) {
667                                 if (info) {
668                                         info->cpu = _get_cpu_ratio(info, ncpu, tick);
669                                         _D("%d / %lf\n", info->pid, info->cpu);
670                                 }
671
672                         }
673                         sleep(2);
674                 }
675         }
676
677         return NULL;
678
679 }
680
681 void _set_genlist(struct appdata *ad)
682 {
683 _D("func\n");
684         retm_if(ad == NULL, "Invalid argument: appdata is NULL\n");
685         int ret = AUL_R_ERROR;
686         int retry_cnt = 0;
687         int sleep_value = 1000;
688
689         _init_grp_cnt();
690
691         while (ret != AUL_R_OK && retry_cnt < 5) {
692                 usleep(sleep_value);
693                 ret = aul_app_get_running_app_info(runapp_info_get, ad);
694
695                 if (ret != AUL_R_OK) {
696                         _D("Fail to get running app information from ail");
697                 }
698
699                 retry_cnt++;
700                 sleep_value *= 2;
701         }
702
703         taskmanager_get_history_app_info(ad);
704         _set_genlist_from_eina(ad);
705
706 }
707
708 void _init_pthread(void)
709 {
710         pthread_mutex_init(&pm, NULL);
711         pthread_cond_init(&pc, NULL);
712 }
713
714 void _fini_pthread(void)
715 {
716 _D("func\n");
717         if (pt) {
718                 pthread_cancel(pt);
719         }
720 }
721
722 void refresh_app_info(struct appdata *ad)
723 {
724 _D("func\n");
725         retm_if(ad == NULL, "Invalid argument: appdata is NULL\n");
726
727         _free_einalist_all(ad);
728         clear_genlist(ad);
729
730         _set_genlist(ad);
731
732         alert_app_info(ad);
733
734 }
735
736 void _del_popup_timer(struct appdata *ad)
737 {
738         if (ad->popup_timer) {
739                 ecore_timer_del(ad->popup_timer);
740                 ad->popup_timer = NULL;
741         }
742 }
743
744 void taskmanager_free_info(struct _task_info *info)
745 {
746         if (info) {
747                 if (info->app_name) {
748                         free(info->app_name);
749                         info->app_name = NULL;
750                 }
751                 if (info->pkg_name) {
752                         free(info->pkg_name);
753                         info->pkg_name = NULL;
754                 }
755                 if (info->icn_path) {
756                         free(info->icn_path);
757                         info->icn_path = NULL;
758                 }
759
760                 free(info);
761         }
762 }
763
764 void _restart_pthread(struct appdata *ad)
765 {
766 _D("func\n");
767         pthread_cancel(pt);
768         if (eina_list_count(ad->applist[TS_INUSE]) > 0) {
769
770                 pthread_create(&pt, NULL, _update_pthread_cb, ad);
771                 pthread_detach(pt);
772                 if (ad->update_timer) {
773                         ecore_timer_del(ad->update_timer);
774                         ad->update_timer = NULL;
775                 }
776                 ad->update_timer = ecore_timer_add(2.0, _update_list, ad);
777         }
778 }
779 int response_end_inuse(struct appdata *ad)
780 {
781 _D("func\n");
782         Eina_List *l, *l_next;
783         struct _task_info *info;
784         Elm_Object_Item *egi;
785
786         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
787
788         ad->ending = EINA_TRUE;
789
790         EINA_LIST_FOREACH_SAFE(ad->applist[TS_INUSE], l, l_next, info) {
791                 _D("applist pid : %d", info->pid);
792                 if (info->it == g_egi) {
793                         _D("matched applist pid : %d", info->pid);
794                         if (info->pid > 0) {
795                                 if (aul_terminate_pid(info->pid) < 0) {
796                                         kill(info->pid, SIGKILL);
797                                 }
798                         }
799                         break;
800                 }
801         }
802         ad->ending = EINA_FALSE;
803
804         return 0;
805 }
806
807 Eina_Bool _refresh_idler_cb(void *data)
808 {
809 _D("func\n");
810         struct appdata *ad = (struct appdata *)data;
811         retvm_if(data == NULL, ECORE_CALLBACK_CANCEL, "Invalid argument: appdata is NULL\n:");
812
813         _del_popup_timer(ad);
814         _del_progressbar(ad);
815         refresh_app_info(ad);
816         return ECORE_CALLBACK_CANCEL;
817 }
818
819 int response_end_all_inuse(struct appdata *ad)
820 {
821         Eina_List *l;
822         struct _task_info *info;
823
824         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
825
826         ad->ending = EINA_TRUE;
827
828         if(ad->update_timer) {
829                 ecore_timer_del(ad->update_timer);
830                 ad->update_timer = NULL;
831         }
832
833         ad->endcnt = eina_list_count(ad->applist[TS_INUSE]);
834         _D("set end count (%d)\n", ad->endcnt);
835
836         EINA_LIST_FOREACH(ad->applist[TS_INUSE], l, info) {
837                 if (info != NULL) {
838                         _D("applist pid : %d", info->pid);
839                         if (info->pid > 0) {
840                                 if (aul_terminate_pid(info->pid) < 0) {
841                                         kill(info->pid, SIGKILL);
842                                 }
843                                 _D("terminated\n");
844                         }
845                 }
846         }
847         ad->ending = EINA_FALSE;
848         return 0;
849 }
850
851 int response_del_history(struct appdata *ad)
852 {
853         Eina_List *l, *l_next;
854         struct _task_info *info;
855         Elm_Object_Item *egi;
856
857         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
858
859         _show_progressbar(ad);
860         EINA_LIST_FOREACH_SAFE(ad->applist[TS_HISTORY], l, l_next, info) {
861                 _D("history applist pid : %d", info->pid);
862                 if (info->it == g_egi) {
863
864                         if (rua_init() == -1) {
865                                 break;
866                         }
867                         rua_delete_history_with_pkgname(info->pkg_name);
868                         rua_fini();
869
870                         ad->applist[TS_HISTORY] =
871                             eina_list_remove_list(ad->applist[TS_HISTORY], l);
872
873                         if (info->b) {
874                                 bundle_free(info->b);
875                         }
876
877                         elm_object_item_del(info->it);
878                         taskmanager_free_info(info);
879                         break;
880                 }
881         }
882         alert_app_info(ad);
883         refresh_app_info(ad);
884         _del_popup_timer(ad);
885         _del_progressbar(ad);
886
887         ad->mode = MODE_NONE;
888         return 0;
889 }
890
891 int response_del_all_history(struct appdata *ad)
892 {
893         Eina_List *l;
894         struct _task_info *info;
895
896         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
897
898         if(ad->update_timer) {
899                 _D("update timer is deleted\n");
900                 ecore_timer_del(ad->update_timer);
901                 ad->update_timer = NULL;
902         }
903
904
905         _show_progressbar(ad);
906
907         if (rua_init() == -1) {
908                 return -1;
909         }
910
911         EINA_LIST_FOREACH(ad->applist[TS_HISTORY], l, info) {
912                 if (info != NULL) {
913                         rua_delete_history_with_pkgname(info->pkg_name);
914                 }
915         }
916
917         if (eina_list_count(ad->applist[TS_INUSE]) == 0) {
918                 rua_clear_history();
919         }
920
921         rua_fini();
922         refresh_app_info(ad);
923         _del_popup_timer(ad);
924         _del_progressbar(ad);
925
926         ad->mode = MODE_NONE;
927         return 0;
928 }
929
930 int response_kill_inuse(struct appdata *ad)
931 {
932         Eina_List *l, *l_next;
933         struct _task_info *info;
934         Elm_Object_Item *egi;
935
936         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
937
938         _show_progressbar(ad);
939
940         EINA_LIST_FOREACH_SAFE(ad->applist[TS_INUSE], l, l_next, info) {
941                 _D("kill applist pid : %d", info->pid);
942                 if (info->it == g_egi) {
943                         if (info->pid > 0) {
944                                 kill(info->pid, SIGKILL);
945                         }
946
947                         ad->applist[TS_INUSE] =
948                             eina_list_remove_list(ad->applist[TS_INUSE], l);
949                         taskmanager_free_info(info);
950                         break;
951                 }
952         }
953         refresh_app_info(ad);
954         _del_progressbar(ad);
955
956         return 0;
957 }
958
959 int response_kill_all_inuse(struct appdata *ad)
960 {
961         Eina_List *l;
962         struct _task_info *info;
963
964         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
965
966         _show_progressbar(ad);
967
968         EINA_LIST_FOREACH(ad->applist[TS_INUSE], l, info) {
969                 _D("kill all applist pid : %d", info->pid);
970                 if (info != NULL) {
971                         if (info->pid > 0) {
972                                 kill(info->pid, SIGKILL);
973                         }
974                 }
975         }
976         refresh_app_info(ad);
977         _del_progressbar(ad);
978
979         return 0;
980 }
981