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