6a0ed22dfacc0c6a55a9a196a5b9fb8ec5beb1e7
[profile/ivi/taskmanager.git] / src / _eina.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 <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21
22 #include <Eina.h>
23 #include <ail.h>
24 #include <aul.h>
25 #include <rua.h>
26
27 #include "taskmanager.h"
28 #include "_util_log.h"
29
30 #define TASKMANAGER_ICON_NAME   "org.tizen.taskmgr.png"
31
32 static int grp_cnt[TS_MAX];
33
34 void _init_grp_cnt(void)
35 {
36         int i;
37
38         for(i = 0; i < TS_MAX; i++) {
39                 grp_cnt[i] = 0;
40         }
41 }
42
43 int _get_grp_cnt(int which)
44 {
45         return grp_cnt[which];
46 }
47
48 int runapp_info_get(const aul_app_info *ainfo, void *data)
49 {
50         ail_appinfo_h handle;
51         ail_error_e ret;
52
53         int i;
54         int vali;
55         char *valc;
56         bool valb;
57         char *icn_path;
58         char buf[1024] = { 0, };
59         struct appdata *ad = data;
60         struct _task_info *info;
61         float usr = 0.0, sys = 0.0;
62
63         retvm_if(ainfo == NULL, -1, "Invalid argument: ainfo is NULL\n");
64         retvm_if(data == NULL, -1, "Invalid argument: data is NULL\n");
65
66         retvm_if(ainfo->pid <= 0, -1, "Invalid pid(%u)\n", ainfo->pid);
67
68         /* filtering */
69         if (ainfo->pid == getpid()) {
70                 return 0;
71         }
72
73         retvm_if(ainfo->pkg_name == NULL, 0, "Invalid pkg_name(%s)\n", ainfo->pkg_name);
74
75 //      _D("running app is (%s)\n", ainfo->pkg_name);
76         ret = ail_package_get_appinfo(ainfo->pkg_name, &handle);
77         retvm_if(ret != AIL_ERROR_OK, -1,
78                         "Failed to get appinfo, pkg_name:%s\n", ainfo->pkg_name);
79
80         ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_TASKMANAGE_BOOL, &valb);
81         if (valb == 0) {
82                 goto exit;
83         }
84         info = calloc(1, sizeof(struct _task_info));
85         if (info == NULL) {
86                 _E("Failed to calloc task_info\n");
87                 goto exit;
88         }
89         info->pkg_name = strdup(ainfo->pkg_name);
90
91         ret = ail_appinfo_get_str(handle, AIL_PROP_NAME_STR, &valc);
92         if (valc == NULL) {
93                 _D("%s: Failed to get ail name\n", ainfo->pkg_name);
94                 valc = "Unknown";
95         }
96         info->app_name = strdup(valc);
97
98         ret = ail_appinfo_get_str(handle, AIL_PROP_ICON_STR, &valc);
99         if (valc == NULL || (ecore_file_exists(valc) == EINA_FALSE)) {
100                 _D("%s: Failed to get ail icon\n", ainfo->pkg_name);
101                 valc = TASKMANAGER_ICON_NAME;
102         }
103
104         snprintf(buf, sizeof(buf), "%s", valc);
105         info->icn_path = strdup(buf);
106 //      _D("get app name[%s] set [%s], icon path[%s]\n", ainfo->pkg_name, info->app_name, buf);
107
108         info->ad = ad;
109         info->pid = ainfo->pid;
110         info->category = TS_INUSE;
111 //      info->mem_total = ad->mem_total;
112         _D("%s/pid(%d)\n", info->app_name, info->pid);
113
114         ad->applist[TS_INUSE] = eina_list_prepend(ad->applist[TS_INUSE], info);
115         grp_cnt[TS_INUSE]++;
116
117  exit:
118         ret = ail_package_destroy_appinfo(handle);
119         retvm_if(ret != AIL_ERROR_OK, -1, "Failed to destroy appinfo\n");
120         return 0;
121 }
122
123 int taskmanager_get_history_app_info(void *data)
124 {
125         struct appdata *ad = data;
126         struct _task_info *info, *info_r;
127         Eina_List *l_r;
128         int flag = 0;
129
130         struct rua_rec rec_result = { 0, };
131         char *val;
132         char **table = NULL;
133         char buf[1024] = { 0, };
134         int nrows = 0, ncols = 0;
135         int row = 0;
136         int i, cnt;
137
138         ail_appinfo_h handle;
139         ail_error_e ret;
140         bool valb;
141         char *valc;
142         int vali;
143
144         retvm_if(data == NULL, -1, "Invalid argument: data is NULL\n");
145         retvm_if(rua_init() == -1, -1, "Failed to rua_init\n");
146
147         if (rua_history_load_db(&table, &nrows, &ncols) == -1) {
148                 rua_fini();
149                 return -1;
150         }
151
152         if (nrows == 0) {
153                 rua_history_unload_db(&table);
154                 rua_fini();
155                 return 0;
156         }
157
158         ad->applist[TS_HISTORY] = eina_list_nth_list(ad->applist[TS_HISTORY], 0);
159         for (row = 0; row < nrows; row++) {
160                 rua_history_get_rec(&rec_result, table, nrows, ncols, row);
161
162                 /* filtering
163                  * pkg_name could be NULL or 0 length because it is launch by fork.
164                  */
165                 if (rec_result.pkg_name == NULL
166                     || strlen(rec_result.pkg_name) < 1) {
167                         continue;
168                 }
169                 ret = ail_package_get_appinfo(rec_result.pkg_name, &handle);
170                 if (ret != AIL_ERROR_OK) {
171                         _D("Failed to get appinfo(%d)\n", ret);
172                         continue;
173                 }
174
175                 ret = ail_appinfo_get_bool(handle, AIL_PROP_X_SLP_TASKMANAGE_BOOL, &valb);
176                 if(valb == 0) {
177                         goto cont;
178                 }
179
180                 EINA_LIST_FOREACH(ad->applist[TS_INUSE], l_r, info_r) {
181                         if (info_r != NULL) {
182                                 if (!strcmp
183                                     (rec_result.pkg_name, info_r->pkg_name)) {
184                                         flag = 1;
185                                         break;
186                                 }
187                         }
188                 }
189
190                 if (flag == 0) {
191                         info = calloc(1, sizeof(struct _task_info));
192                         retvm_if(info == NULL, -1, "Failed to calloc _task_info\n");
193
194                         info->pkg_name = strdup(rec_result.pkg_name);
195
196                         ret = ail_appinfo_get_str(handle, AIL_PROP_NAME_STR, &valc);
197                         if (valc == NULL) {
198                                 _D("Failed to get ail name\n");
199                                 valc = "Unknown";
200                         }
201                         info->app_name = strdup(valc);
202
203                         ret = ail_appinfo_get_str(handle, AIL_PROP_ICON_STR, &valc);
204                         if (valc == NULL || (ecore_file_exists(valc) == EINA_FALSE)) {
205                                 _D("Failed to get ail icon\n");
206                                 valc = TASKMANAGER_ICON_NAME;
207                         }
208                         snprintf(buf, sizeof(buf), "%s", valc);
209                         info->icn_path = strdup(buf);
210
211                         info->ad = ad;
212                         info->pid = 0;
213                         info->category = TS_HISTORY;
214
215                         if (rec_result.arg != NULL) {
216                                 if (strlen(rec_result.arg) > 0) {
217                                         info->b = bundle_decode(
218                                                         (const bundle_raw *)rec_result.arg,
219                                                         strlen(rec_result.arg));
220                                 }
221                         }
222
223                         ad->applist[TS_HISTORY] =
224                             eina_list_append(ad->applist[TS_HISTORY], info);
225                         grp_cnt[TS_HISTORY]++;
226
227                 }
228
229                 flag = 0;
230
231 cont:
232                 ret = ail_package_destroy_appinfo(handle);
233         }
234
235         rua_history_unload_db(&table);
236         rua_fini();
237
238         return 0;
239 }
240
241 int _free_einalist_all(struct appdata *ad)
242 {
243         Eina_List *l;
244         struct _task_info *info = NULL;
245         int i;
246
247         if (ad == NULL) {
248                 printf("[Error] Invalid argument: appdata is NULL\n");
249                 return -1;
250         }
251
252         for (i = 0; i < TS_MAX; i++) {
253                 if (ad->applist[i] == NULL)
254                         continue;
255
256                 EINA_LIST_FOREACH(ad->applist[i], l, info) {
257                         if (info != NULL) {
258                                 if (info->b)
259                                         bundle_free(info->b);
260
261                                 taskmanager_free_info(info);
262                                 info = NULL;
263                         }
264                 }
265
266                 eina_list_free(ad->applist[i]);
267                 ad->applist[i] = NULL;
268         }
269
270         return 0;
271 }
272
273 int _subt_einalist_item(struct appdata *ad, int pid)
274 {
275 _D("func\n");
276         Eina_List *l, *l_next;
277         int ret = -1;
278         struct _task_info *info;
279
280         retvm_if(ad == NULL, -1, "Invalid argument: appdata is NULL\n");
281         retvm_if(ad->applist[TS_INUSE] == NULL, -1, "applist is NULL\n");
282
283         EINA_LIST_FOREACH(ad->applist[TS_INUSE], l, info) {
284                 _D("pid(%u):(%u)\n", pid, info->pid);
285                 if (info == NULL) {
286                         _E("Failed to get info\n");
287                         continue;
288                 }
289
290                 if (pid > 0 && pid == info->pid) {
291                         if(info->app_name)      _D("killed [%s]\n", info->app_name);
292                         ad->applist[TS_INUSE] =
293                             eina_list_remove_list(ad->applist[TS_INUSE], l);
294                         taskmanager_free_info(info);
295                         info = NULL;
296                         ret = 0;
297                 }
298         }
299         return ret;
300 }
301
302
303
304