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