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