4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include "ail_private.h"
30 #include "ail_convert.h"
32 #include "ail_package.h"
35 char *_get_where_clause(ail_filter_h filter);
41 static inline void _add_cond_to_filter(ail_filter_h filter, struct element *cond)
43 filter->list = g_slist_append(filter->list, cond);
46 EXPORT_API ail_error_e ail_filter_new(ail_filter_h *filter)
50 retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
52 f = (struct ail_filter *)calloc(1, sizeof(struct ail_filter));
53 retv_if (NULL == f, AIL_ERROR_OUT_OF_MEMORY);
60 static inline void _destroy_cond(gpointer data, gpointer user_data)
66 cond = (struct element *)data;
69 ELEMENT_TYPE(cond, t);
70 if (VAL_TYPE_STR == t) {
71 if(ELEMENT_STR(cond)->value)
72 free(ELEMENT_STR(cond)->value);
79 EXPORT_API ail_error_e ail_filter_destroy(ail_filter_h filter)
81 retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
84 g_slist_foreach(filter->list, _destroy_cond, NULL);
85 g_slist_free(filter->list);
94 EXPORT_API ail_error_e ail_filter_add_bool(ail_filter_h filter, const char *property, bool value)
99 retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
100 retv_if (NULL == property, AIL_ERROR_INVALID_PARAMETER);
102 prop = _ail_convert_to_prop_bool(property);
104 if (prop < E_AIL_PROP_BOOL_MIN || prop > E_AIL_PROP_BOOL_MAX)
105 return AIL_ERROR_INVALID_PARAMETER;
107 c = (struct element *)calloc(1, sizeof(struct element_bool));
108 retv_if (NULL == c, AIL_ERROR_OUT_OF_MEMORY);
110 ELEMENT_BOOL(c)->prop = (int)prop;
111 ELEMENT_BOOL(c)->value = value;
113 _add_cond_to_filter(filter, c);
118 EXPORT_API ail_error_e ail_filter_add_int(ail_filter_h filter, const char *property, int value)
123 retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
124 retv_if (NULL == property, AIL_ERROR_INVALID_PARAMETER);
126 prop = _ail_convert_to_prop_int(property);
128 if (prop < E_AIL_PROP_INT_MIN || prop > E_AIL_PROP_INT_MAX)
129 return AIL_ERROR_INVALID_PARAMETER;
131 c = (struct element *)calloc(1, sizeof(struct element_int));
132 retv_if (NULL == c, AIL_ERROR_OUT_OF_MEMORY);
134 ELEMENT_INT(c)->prop = (int)prop;
135 ELEMENT_INT(c)->value = value;
137 _add_cond_to_filter(filter, c);
142 EXPORT_API ail_error_e ail_filter_add_str(ail_filter_h filter, const char *property, const char *value)
144 struct element *c; //condition
147 retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
148 retv_if (NULL == property, AIL_ERROR_INVALID_PARAMETER);
150 prop = _ail_convert_to_prop_str(property);
152 if (prop < E_AIL_PROP_STR_MIN || prop > E_AIL_PROP_STR_MAX)
153 return AIL_ERROR_INVALID_PARAMETER;
155 retv_if (NULL == value, AIL_ERROR_INVALID_PARAMETER);
156 retv_if (strlen(value) == 0, AIL_ERROR_INVALID_PARAMETER);
158 c = (struct element *)calloc(1, sizeof(struct element_str));
160 retv_if (NULL == c, AIL_ERROR_OUT_OF_MEMORY);
162 ELEMENT_STR(c)->prop = (int)prop;
163 ELEMENT_STR(c)->value = strdup(value);
164 if (!ELEMENT_STR(c)->value) {
166 return AIL_ERROR_OUT_OF_MEMORY;
169 _add_cond_to_filter(filter, c);
174 static void _get_condition(gpointer data, char **condition)
176 struct element *e = (struct element *)data;
178 char buf[AIL_SQL_QUERY_MAX_LEN];
180 f = sql_get_filter(e->prop);
186 snprintf(buf, sizeof(buf), f, ELEMENT_BOOL(e)->value);
189 snprintf(buf, sizeof(buf), f, ELEMENT_INT(e)->value);
192 if (E_AIL_PROP_NAME_STR == e->prop) {
193 snprintf(buf, sizeof(buf), f, ELEMENT_STR(e)->value, ELEMENT_STR(e)->value);
195 snprintf(buf, sizeof(buf), f, ELEMENT_STR(e)->value);
199 _E("Invalid property type");
204 *condition = strdup(buf);
209 char *_get_where_clause(ail_filter_h filter)
212 char w[AIL_SQL_QUERY_MAX_LEN] = {0,};
217 snprintf(w, AIL_SQL_QUERY_MAX_LEN, " WHERE ");
219 for (l = filter->list; l; l = g_slist_next(l)) {
220 _get_condition(l->data, &c);
227 strncat(w, c, sizeof(w)-strlen(w)-1);
228 w[sizeof(w)-1] = '\0';
231 if (g_slist_next(l)) {
232 strncat(w, " and ", sizeof(w)-strlen(w)-1);
233 w[sizeof(w)-1] = '\0';
237 // _D("where = %s", w);
242 EXPORT_API ail_error_e ail_filter_count_appinfo(ail_filter_h filter, int *cnt)
244 char q[AIL_SQL_QUERY_MAX_LEN];
251 int filter_count = 0;
253 retv_if(!cnt, AIL_ERROR_INVALID_PARAMETER);
255 if (db_open(DB_OPEN_RO, GLOBAL_USER) != AIL_ERROR_OK)
256 return AIL_ERROR_DB_FAILED;
258 snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
261 retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
262 l = sql_get_locale();
264 _E("Failed to get locale string");
266 return AIL_ERROR_FAIL;
268 snprintf(q, sizeof(q), tmp_q, l);
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';
280 _D("No filter exists. All records are retreived");
283 if (db_prepare_globalro(q, &stmt) != AIL_ERROR_OK) {
284 _E("db_prepare_globalro fail for query = %s",q);
285 return AIL_ERROR_DB_FAILED;
287 ai = appinfo_create();
289 appinfo_set_stmt(ai, stmt);
290 while (db_step(stmt) == AIL_ERROR_OK) {
292 if(_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
306 EXPORT_API ail_error_e ail_filter_count_usr_appinfo(ail_filter_h filter, int *cnt, uid_t uid)
308 char q[AIL_SQL_QUERY_MAX_LEN];
315 int filter_count = 0;
317 retv_if(!cnt, AIL_ERROR_INVALID_PARAMETER);
319 //is_admin ; redirect
320 if (uid == GLOBAL_USER)
321 return ail_filter_count_appinfo(filter, cnt);
323 if (db_open(DB_OPEN_RO, uid) != AIL_ERROR_OK)
324 return AIL_ERROR_DB_FAILED;
326 snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
329 retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
330 l = sql_get_locale();
332 _E("Failed to get locale string");
334 return AIL_ERROR_FAIL;
336 snprintf(q, sizeof(q), tmp_q, l);
340 if (filter && filter->list) {
341 w = _get_where_clause(filter);
342 retv_if (NULL == w, AIL_ERROR_FAIL);
343 strncat(q, w, sizeof(q)-strlen(q)-1);
344 q[sizeof(q)-1] = '\0';
348 _D("No filter exists. All records are retreived");
350 if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
351 _E("db_prepare fail for query = %s",q);
352 return AIL_ERROR_DB_FAILED;
354 ai = appinfo_create();
356 appinfo_set_stmt(ai, stmt);
357 while (db_step(stmt) == AIL_ERROR_OK) {
359 if(_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
374 EXPORT_API ail_error_e ail_filter_list_appinfo_foreach(ail_filter_h filter, ail_list_appinfo_cb cb, void *user_data)
376 char q[AIL_SQL_QUERY_MAX_LEN];
384 retv_if (NULL == cb, AIL_ERROR_INVALID_PARAMETER);
386 if (db_open(DB_OPEN_RO, GLOBAL_USER) != AIL_ERROR_OK)
387 return AIL_ERROR_DB_FAILED;
389 snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
392 retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
393 l = sql_get_locale();
395 _E("Failed to get locale string");
397 return AIL_ERROR_FAIL;
399 snprintf(q, sizeof(q), tmp_q, l);
403 if (filter && filter->list) {
404 w = _get_where_clause(filter);
405 retv_if (NULL == w, AIL_ERROR_FAIL);
406 strncat(q, w, sizeof(q)-strlen(q)-1);
407 q[sizeof(q)-1] = '\0';
408 strncat(q, " order by app_info.package", sizeof(q)-strlen(q)-1);
409 q[sizeof(q)-1] = '\0';
413 _D("No filter exists. All records are retreived");
415 // _D("Query = %s",q);
417 if (db_prepare_globalro(q, &stmt) != AIL_ERROR_OK) {
418 _E("db_prepare fail for query = %s",q);
419 return AIL_ERROR_DB_FAILED;
421 /*if (db_prepare_globalro(q, &stmt) != AIL_ERROR_OK) {
422 _E("db_prepare fail for query = %s",q);
423 return AIL_ERROR_DB_FAILED;
425 ai = appinfo_create();
427 appinfo_set_stmt(ai, stmt);
429 while (i = db_step(stmt) == AIL_ERROR_OK) {
430 _E("------------------------dbstep : %u\n", i);
431 if(_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
434 r = cb(ai, user_data,GLOBAL_USER);
435 if (AIL_CB_RET_CANCEL == r)
444 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)
446 char q[AIL_SQL_QUERY_MAX_LEN];
454 retv_if (NULL == cb, AIL_ERROR_INVALID_PARAMETER);
456 if (db_open(DB_OPEN_RO, uid) != AIL_ERROR_OK)
457 return AIL_ERROR_DB_FAILED;
459 snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
462 retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
463 l = sql_get_locale();
465 _E("Failed to get locale string");
467 return AIL_ERROR_FAIL;
469 snprintf(q, sizeof(q), tmp_q, l);
473 if (filter && filter->list) {
474 w = _get_where_clause(filter);
475 retv_if (NULL == w, AIL_ERROR_FAIL);
476 strncat(q, w, sizeof(q)-strlen(q)-1);
477 q[sizeof(q)-1] = '\0';
478 strncat(q, " order by app_info.package", sizeof(q)-strlen(q)-1);
479 q[sizeof(q)-1] = '\0';
483 _D("No filter exists. All records are retreived");
486 if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
487 _E("db_prepare fail for query = %s",q);
488 return AIL_ERROR_DB_FAILED;
490 ai = appinfo_create();
491 appinfo_set_stmt(ai, stmt);
493 while (i = db_step(stmt) == AIL_ERROR_OK) {
494 if(_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
497 r = cb(ai, user_data,uid);
498 if (AIL_CB_RET_CANCEL == r)