Merge "Add ail_list tool" into tizen
[platform/core/appfw/ail.git] / src / ail_filter.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23
24 #include <stdlib.h>
25 #include <glib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include "ail.h"
29 #include "ail_private.h"
30 #include "ail_convert.h"
31 #include "ail_sql.h"
32 #include "ail_package.h"
33 #include "ail_db.h"
34
35 char *_get_where_clause(ail_filter_h filter);
36
37 struct ail_filter {
38         GSList *list;
39 };
40
41 static inline void _add_cond_to_filter(ail_filter_h filter, struct element *cond)
42 {
43         filter->list = g_slist_append(filter->list, cond);
44 }
45
46 EXPORT_API ail_error_e ail_filter_new(ail_filter_h *filter)
47 {
48         struct ail_filter *f;
49
50         retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
51
52         f = (struct ail_filter *)calloc(1, sizeof(struct ail_filter));
53         retv_if (NULL == f, AIL_ERROR_OUT_OF_MEMORY);
54
55         *filter = f;
56
57         return AIL_ERROR_OK;
58 }
59
60 static inline void _destroy_cond(gpointer data, gpointer user_data)
61 {
62         if (!data)
63                 return;
64
65         struct element *cond;
66         cond  = (struct element *)data;
67
68         int t;
69         ELEMENT_TYPE(cond, t);
70         if (VAL_TYPE_STR == t) {
71                 if(ELEMENT_STR(cond)->value)
72                         free(ELEMENT_STR(cond)->value);
73         }
74         free(cond);
75         return;
76 }
77
78
79 EXPORT_API ail_error_e ail_filter_destroy(ail_filter_h filter)
80 {
81         retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
82
83         if (filter->list){
84                 g_slist_foreach(filter->list, _destroy_cond, NULL);
85                 g_slist_free(filter->list);
86         }
87
88         free(filter);
89         db_close();
90
91         return AIL_ERROR_OK;
92 }
93
94 EXPORT_API ail_error_e ail_filter_add_bool(ail_filter_h filter, const char *property, bool value)
95 {
96         struct element *c;
97         ail_prop_bool_e prop;
98
99         retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
100         retv_if (NULL == property, AIL_ERROR_INVALID_PARAMETER);
101
102         prop = _ail_convert_to_prop_bool(property);
103
104         if (prop < E_AIL_PROP_BOOL_MIN || prop > E_AIL_PROP_BOOL_MAX)
105                 return AIL_ERROR_INVALID_PARAMETER;
106
107         c = (struct element *)calloc(1, sizeof(struct element_bool));
108         retv_if (NULL == c, AIL_ERROR_OUT_OF_MEMORY);
109
110         ELEMENT_BOOL(c)->prop = (int)prop;
111         ELEMENT_BOOL(c)->value = value;
112
113         _add_cond_to_filter(filter, c);
114
115         return AIL_ERROR_OK;
116 }
117
118 EXPORT_API ail_error_e ail_filter_add_int(ail_filter_h filter, const char *property, int value)
119 {
120         struct element *c;
121         ail_prop_int_e prop;
122
123         retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
124         retv_if (NULL == property, AIL_ERROR_INVALID_PARAMETER);
125
126         prop = _ail_convert_to_prop_int(property);
127
128         if (prop < E_AIL_PROP_INT_MIN || prop > E_AIL_PROP_INT_MAX)
129                 return AIL_ERROR_INVALID_PARAMETER;
130
131         c = (struct element *)calloc(1, sizeof(struct element_int));
132         retv_if (NULL == c, AIL_ERROR_OUT_OF_MEMORY);
133
134         ELEMENT_INT(c)->prop = (int)prop;
135         ELEMENT_INT(c)->value = value;
136
137         _add_cond_to_filter(filter, c);
138
139         return AIL_ERROR_OK;
140 }
141
142 EXPORT_API ail_error_e ail_filter_add_str(ail_filter_h filter, const char *property, const char *value)
143 {
144         struct element *c; //condition
145         ail_prop_str_e prop;
146
147         retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
148         retv_if (NULL == property, AIL_ERROR_INVALID_PARAMETER);
149
150         prop = _ail_convert_to_prop_str(property);
151
152         if (prop < E_AIL_PROP_STR_MIN || prop > E_AIL_PROP_STR_MAX)
153                 return AIL_ERROR_INVALID_PARAMETER;
154
155         retv_if (NULL == value, AIL_ERROR_INVALID_PARAMETER);
156         retv_if (strlen(value) == 0, AIL_ERROR_INVALID_PARAMETER);
157
158         c = (struct element *)calloc(1, sizeof(struct element_str));
159
160         retv_if (NULL == c, AIL_ERROR_OUT_OF_MEMORY);
161
162         ELEMENT_STR(c)->prop = (int)prop;
163         ELEMENT_STR(c)->value = strdup(value);
164         if (!ELEMENT_STR(c)->value) {
165                 free(c);
166                 return AIL_ERROR_OUT_OF_MEMORY;
167         }
168
169         _add_cond_to_filter(filter, c);
170
171         return AIL_ERROR_OK;
172 }
173
174 static void _get_condition(gpointer data, char **condition)
175 {
176         struct element *e = (struct element *)data;
177         const char *f;
178         char buf[AIL_SQL_QUERY_MAX_LEN];
179
180         f =  sql_get_filter(e->prop);
181         int t;
182         ELEMENT_TYPE(e, t);
183
184         switch (t) {
185                 case VAL_TYPE_BOOL:
186                         snprintf(buf, sizeof(buf), f, ELEMENT_BOOL(e)->value);
187                         break;
188                 case VAL_TYPE_INT:
189                         snprintf(buf, sizeof(buf), f, ELEMENT_INT(e)->value);
190                         break;
191                 case VAL_TYPE_STR:
192                         if (E_AIL_PROP_NAME_STR == e->prop) {
193                                 snprintf(buf, sizeof(buf), f, ELEMENT_STR(e)->value, ELEMENT_STR(e)->value);
194                         } else {
195                                 snprintf(buf, sizeof(buf), f, ELEMENT_STR(e)->value);
196                         }
197                         break;
198                 default:
199                         _E("Invalid property type");
200                         *condition = NULL;
201                         return;
202         }
203
204         *condition = strdup(buf);
205
206         return;
207 }
208
209 char *_get_where_clause(ail_filter_h filter)
210 {
211         char *c;
212         char w[AIL_SQL_QUERY_MAX_LEN] = {0,};
213         c = NULL;
214
215         GSList *l;
216         
217         snprintf(w, AIL_SQL_QUERY_MAX_LEN, " WHERE ");
218
219         for (l = filter->list; l; l = g_slist_next(l)) {
220                 _get_condition(l->data, &c);
221                 if (!c) return NULL;
222                 if (*c == 0) {
223                         free(c);
224                         return NULL;
225                 }
226
227                 strncat(w, c, sizeof(w)-strlen(w)-1);
228                 w[sizeof(w)-1] = '\0';
229                 if(c) free(c);
230
231                 if (g_slist_next(l)) {
232                         strncat(w, " and ", sizeof(w)-strlen(w)-1);
233                         w[sizeof(w)-1] = '\0';
234                 }
235         }
236
237 //      _D("where = %s", w);
238
239         return strdup(w);
240 }
241
242 EXPORT_API ail_error_e ail_filter_count_appinfo(ail_filter_h filter, int *cnt)
243 {
244         char q[AIL_SQL_QUERY_MAX_LEN];
245         char *w;
246         char *tmp_q;
247         char *l;
248         ail_cb_ret_e r;
249         sqlite3_stmt *stmt;
250         ail_appinfo_h ai;
251         int filter_count = 0;
252
253         retv_if(!cnt, AIL_ERROR_INVALID_PARAMETER);
254
255         if (db_open(DB_OPEN_RO, GLOBAL_USER) != AIL_ERROR_OK)
256                 return AIL_ERROR_DB_FAILED;
257
258         snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
259
260         tmp_q = strdup(q);
261         retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
262         l = sql_get_locale();
263         if (NULL == l) {
264                 _E("Failed to get locale string");
265                 free(tmp_q);
266                 return AIL_ERROR_FAIL;
267         }
268         snprintf(q, sizeof(q), tmp_q, l);
269         free(l);
270         free(tmp_q);
271
272         if (filter && filter->list) {
273                 w = _get_where_clause(filter);
274                 retv_if (NULL == w, AIL_ERROR_FAIL);
275                 strncat(q, w, sizeof(q)-strlen(q)-1);
276                 q[sizeof(q)-1] = '\0';
277                 free(w);
278         }
279         else
280                 _D("No filter exists. All records are retreived");
281 //is_admin
282         if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
283                 _E("db_prepare fail for query = %s",q);
284                 return AIL_ERROR_DB_FAILED;
285         }
286 /*      if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
287                 _E("db_prepare fail for query = %s",q);
288                 return AIL_ERROR_DB_FAILED;
289         }*/
290         ai = appinfo_create();
291
292         appinfo_set_stmt(ai, stmt);
293         while (db_step(stmt) == AIL_ERROR_OK) {
294
295                 if(_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
296                         continue;
297
298                 filter_count++;
299         }
300
301         db_finalize(stmt);
302
303         appinfo_destroy(ai);
304         *cnt = filter_count;
305
306         return AIL_ERROR_OK;
307 }
308
309 EXPORT_API ail_error_e ail_filter_count_usr_appinfo(ail_filter_h filter, int *cnt, uid_t uid)
310 {
311         char q[AIL_SQL_QUERY_MAX_LEN];
312         char *w;
313         char *tmp_q;
314         char *l;
315         ail_cb_ret_e r;
316         sqlite3_stmt *stmt;
317         ail_appinfo_h ai;
318         int filter_count = 0;
319
320         retv_if(!cnt, AIL_ERROR_INVALID_PARAMETER);
321
322         if (db_open(DB_OPEN_RO, uid) != AIL_ERROR_OK)
323                 return AIL_ERROR_DB_FAILED;
324
325         snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
326
327         tmp_q = strdup(q);
328         retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
329         l = sql_get_locale();
330         if (NULL == l) {
331                 _E("Failed to get locale string");
332                 free(tmp_q);
333                 return AIL_ERROR_FAIL;
334         }
335         snprintf(q, sizeof(q), tmp_q, l);
336         free(l);
337         free(tmp_q);
338
339         if (filter && filter->list) {
340                 w = _get_where_clause(filter);
341                 retv_if (NULL == w, AIL_ERROR_FAIL);
342                 strncat(q, w, sizeof(q)-strlen(q)-1);
343                 q[sizeof(q)-1] = '\0';
344                 free(w);
345         }
346         else
347                 _D("No filter exists. All records are retreived");
348
349         if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
350                 _E("db_prepare fail for query = %s",q);
351                 return AIL_ERROR_DB_FAILED;
352         }
353         ai = appinfo_create();
354
355         appinfo_set_stmt(ai, stmt);
356         while (db_step(stmt) == AIL_ERROR_OK) {
357
358                 if(_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
359                         continue;
360
361                 filter_count++;
362         }
363
364         db_finalize(stmt);
365
366         appinfo_destroy(ai);
367         *cnt = filter_count;
368
369         return AIL_ERROR_OK;
370 }
371
372
373 EXPORT_API ail_error_e ail_filter_list_appinfo_foreach(ail_filter_h filter, ail_list_appinfo_cb cb, void *user_data)
374 {
375         char q[AIL_SQL_QUERY_MAX_LEN];
376         char *tmp_q;
377         char *w;
378         char *l;
379         ail_cb_ret_e r;
380         sqlite3_stmt *stmt;
381         ail_appinfo_h ai;
382
383         retv_if (NULL == cb, AIL_ERROR_INVALID_PARAMETER);
384
385         if (db_open(DB_OPEN_RO, GLOBAL_USER) != AIL_ERROR_OK)
386                 return AIL_ERROR_DB_FAILED;
387
388         snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
389
390         tmp_q = strdup(q);
391         retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
392         l = sql_get_locale();
393         if (NULL == l) {
394                 _E("Failed to get locale string");
395                 free(tmp_q);
396                 return AIL_ERROR_FAIL;
397         }
398         snprintf(q, sizeof(q), tmp_q, l);
399         free(l);
400         free(tmp_q);
401
402         if (filter && filter->list) {
403                 w = _get_where_clause(filter);
404                 retv_if (NULL == w, AIL_ERROR_FAIL);
405                 strncat(q, w, sizeof(q)-strlen(q)-1);
406                 q[sizeof(q)-1] = '\0';
407                 strncat(q, " order by app_info.package", sizeof(q)-strlen(q)-1);
408                 q[sizeof(q)-1] = '\0';
409                 free(w);
410         }
411         else
412                 _D("No filter exists. All records are retreived");
413
414 //      _D("Query = %s",q);
415 //is_admin
416         if (db_prepare_globalro(q, &stmt) != AIL_ERROR_OK) {
417                 _E("db_prepare fail for query = %s",q);
418                 return AIL_ERROR_DB_FAILED;
419         }
420         /*if (db_prepare_globalro(q, &stmt) != AIL_ERROR_OK) {
421                 _E("db_prepare fail for query = %s",q);
422                 return AIL_ERROR_DB_FAILED;
423         }*/
424         ai = appinfo_create();
425
426         appinfo_set_stmt(ai, stmt);
427         uint i = 0;
428         while (i = db_step(stmt) == AIL_ERROR_OK) {
429                 _E("------------------------dbstep : %u\n", i);
430                 if(_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
431                         continue;
432
433                 r = cb(ai, user_data);
434                 if (AIL_CB_RET_CANCEL == r)
435                         break;
436         }
437         appinfo_destroy(ai);
438
439         db_finalize(stmt);
440         return AIL_ERROR_OK;
441 }
442
443 EXPORT_API ail_error_e ail_filter_list_usr_appinfo_foreach(ail_filter_h filter, ail_list_appinfo_cb cb, void *user_data, uid_t uid)
444 {
445         char q[AIL_SQL_QUERY_MAX_LEN];
446         char *tmp_q;
447         char *w;
448         char *l;
449         ail_cb_ret_e r;
450         sqlite3_stmt *stmt;
451         ail_appinfo_h ai;
452
453         retv_if (NULL == cb, AIL_ERROR_INVALID_PARAMETER);
454
455         if (db_open(DB_OPEN_RO, uid) != AIL_ERROR_OK)
456                 return AIL_ERROR_DB_FAILED;
457
458         snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
459
460         tmp_q = strdup(q);
461         retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
462         l = sql_get_locale();
463         if (NULL == l) {
464                 _E("Failed to get locale string");
465                 free(tmp_q);
466                 return AIL_ERROR_FAIL;
467         }
468         snprintf(q, sizeof(q), tmp_q, l);
469         free(l);
470         free(tmp_q);
471
472         if (filter && filter->list) {
473                 w = _get_where_clause(filter);
474                 retv_if (NULL == w, AIL_ERROR_FAIL);
475                 strncat(q, w, sizeof(q)-strlen(q)-1);
476                 q[sizeof(q)-1] = '\0';
477                 strncat(q, " order by app_info.package", sizeof(q)-strlen(q)-1);
478                 q[sizeof(q)-1] = '\0';
479                 free(w);
480         }
481         else
482                 _D("No filter exists. All records are retreived");
483
484 //is_admin
485         if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
486                 _E("db_prepare fail for query = %s",q);
487                 return AIL_ERROR_DB_FAILED;
488         }
489         ai = appinfo_create();
490         appinfo_set_stmt(ai, stmt);
491         uint i = 0;
492         while (i = db_step(stmt) == AIL_ERROR_OK) {
493                 if(_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
494                         continue;
495
496                 r = cb(ai, user_data);
497                 if (AIL_CB_RET_CANCEL == r)
498                         break;
499         }
500         appinfo_destroy(ai);
501
502         db_finalize(stmt);
503         return AIL_ERROR_OK;
504 }