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