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);
93 EXPORT_API ail_error_e ail_filter_add_bool(ail_filter_h filter, const char *property, bool value)
98 retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
99 retv_if (NULL == property, AIL_ERROR_INVALID_PARAMETER);
101 prop = _ail_convert_to_prop_bool(property);
103 if (prop < E_AIL_PROP_BOOL_MIN || prop > E_AIL_PROP_BOOL_MAX)
104 return AIL_ERROR_INVALID_PARAMETER;
106 c = (struct element *)calloc(1, sizeof(struct element_bool));
107 retv_if (NULL == c, AIL_ERROR_OUT_OF_MEMORY);
109 ELEMENT_BOOL(c)->prop = (int)prop;
110 ELEMENT_BOOL(c)->value = value;
112 _add_cond_to_filter(filter, c);
117 EXPORT_API ail_error_e ail_filter_add_int(ail_filter_h filter, const char *property, int value)
122 retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
123 retv_if (NULL == property, AIL_ERROR_INVALID_PARAMETER);
125 prop = _ail_convert_to_prop_int(property);
127 if (prop < E_AIL_PROP_INT_MIN || prop > E_AIL_PROP_INT_MAX)
128 return AIL_ERROR_INVALID_PARAMETER;
130 c = (struct element *)calloc(1, sizeof(struct element_int));
131 retv_if (NULL == c, AIL_ERROR_OUT_OF_MEMORY);
133 ELEMENT_INT(c)->prop = (int)prop;
134 ELEMENT_INT(c)->value = value;
136 _add_cond_to_filter(filter, c);
141 EXPORT_API ail_error_e ail_filter_add_str(ail_filter_h filter, const char *property, const char *value)
143 struct element *c; //condition
146 retv_if (NULL == filter, AIL_ERROR_INVALID_PARAMETER);
147 retv_if (NULL == property, AIL_ERROR_INVALID_PARAMETER);
149 prop = _ail_convert_to_prop_str(property);
151 if (prop < E_AIL_PROP_STR_MIN || prop > E_AIL_PROP_STR_MAX)
152 return AIL_ERROR_INVALID_PARAMETER;
154 retv_if (NULL == value, AIL_ERROR_INVALID_PARAMETER);
155 retv_if (strlen(value) == 0, AIL_ERROR_INVALID_PARAMETER);
157 c = (struct element *)calloc(1, sizeof(struct element_str));
159 retv_if (NULL == c, AIL_ERROR_OUT_OF_MEMORY);
161 ELEMENT_STR(c)->prop = (int)prop;
162 ELEMENT_STR(c)->value = strdup(value);
163 if (!ELEMENT_STR(c)->value) {
165 return AIL_ERROR_OUT_OF_MEMORY;
168 _add_cond_to_filter(filter, c);
173 static void _get_condition(gpointer data, char **condition)
175 struct element *e = (struct element *)data;
177 char buf[AIL_SQL_QUERY_MAX_LEN];
179 f = sql_get_filter(e->prop);
185 snprintf(buf, sizeof(buf), f, ELEMENT_BOOL(e)->value);
188 snprintf(buf, sizeof(buf), f, ELEMENT_INT(e)->value);
191 if (E_AIL_PROP_NAME_STR == e->prop) {
192 snprintf(buf, sizeof(buf), f, ELEMENT_STR(e)->value, ELEMENT_STR(e)->value);
194 snprintf(buf, sizeof(buf), f, ELEMENT_STR(e)->value);
198 _E("Invalid property type");
203 *condition = strdup(buf);
208 char *_get_where_clause(ail_filter_h filter)
211 char w[AIL_SQL_QUERY_MAX_LEN] = {0,};
216 snprintf(w, AIL_SQL_QUERY_MAX_LEN, " WHERE ");
218 for (l = filter->list; l; l = g_slist_next(l)) {
219 _get_condition(l->data, &c);
226 strncat(w, c, sizeof(w)-strlen(w)-1);
227 w[sizeof(w)-1] = '\0';
230 if (g_slist_next(l)) {
231 strncat(w, " and ", sizeof(w)-strlen(w)-1);
232 w[sizeof(w)-1] = '\0';
241 EXPORT_API ail_error_e ail_filter_count_appinfo(ail_filter_h filter, int *cnt)
243 char q[AIL_SQL_QUERY_MAX_LEN];
251 retv_if(!cnt, AIL_ERROR_INVALID_PARAMETER);
253 if (db_open(DB_OPEN_RO) != AIL_ERROR_OK)
254 return AIL_ERROR_DB_FAILED;
256 snprintf(q, sizeof(q), "SELECT COUNT (*) FROM %s", SQL_TBL_APP_INFO_WITH_LOCALNAME);
259 retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
260 l = sql_get_locale();
262 _E("Failed to get locale string");
264 return AIL_ERROR_FAIL;
266 snprintf(q, sizeof(q), tmp_q, l);
270 if (filter && filter->list) {
271 w = _get_where_clause(filter);
272 retv_if (NULL == w, AIL_ERROR_FAIL);
273 strncat(q, w, sizeof(q)-strlen(q)-1);
274 q[sizeof(q)-1] = '\0';
280 if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
281 return AIL_ERROR_DB_FAILED;
285 if (r == AIL_ERROR_OK) {
286 db_column_int(stmt, 0, &n);
297 EXPORT_API ail_error_e ail_filter_list_appinfo_foreach(ail_filter_h filter, ail_list_appinfo_cb cb, void *user_data)
299 char q[AIL_SQL_QUERY_MAX_LEN];
307 retv_if (NULL == cb, AIL_ERROR_INVALID_PARAMETER);
309 if (db_open(DB_OPEN_RO) != AIL_ERROR_OK)
310 return AIL_ERROR_DB_FAILED;
312 snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
315 retv_if (NULL == tmp_q, AIL_ERROR_OUT_OF_MEMORY);
316 l = sql_get_locale();
318 _E("Failed to get locale string");
320 return AIL_ERROR_FAIL;
322 snprintf(q, sizeof(q), tmp_q, l);
326 if (filter && filter->list) {
327 w = _get_where_clause(filter);
328 retv_if (NULL == w, AIL_ERROR_FAIL);
329 strncat(q, w, sizeof(q)-strlen(q)-1);
330 q[sizeof(q)-1] = '\0';
331 strncat(q, " order by app_info.package", sizeof(q)-strlen(q)-1);
332 q[sizeof(q)-1] = '\0';
336 _D("No filter exists. All records are retreived");
340 if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
341 return AIL_ERROR_DB_FAILED;
344 ai = appinfo_create();
346 appinfo_set_stmt(ai, stmt);
347 while (db_step(stmt) == AIL_ERROR_OK) {
349 r = cb(ai, user_data);
350 if (AIL_CB_RET_CANCEL == r)