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(filter == NULL, AIL_ERROR_INVALID_PARAMETER);
52 f = (struct ail_filter *)calloc(1, sizeof(struct ail_filter));
53 retv_if(f == NULL, 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(filter == NULL, 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(filter == NULL, AIL_ERROR_INVALID_PARAMETER);
100 retv_if(property == NULL, 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(c == NULL, 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(filter == NULL, AIL_ERROR_INVALID_PARAMETER);
124 retv_if(property == NULL, 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(c == NULL, 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(filter == NULL, AIL_ERROR_INVALID_PARAMETER);
148 retv_if(property == NULL, 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(value == NULL, AIL_ERROR_INVALID_PARAMETER);
156 retv_if(strlen(value) == 0, AIL_ERROR_INVALID_PARAMETER);
158 c = (struct element *)calloc(1, sizeof(struct element_str));
159 retv_if(c == NULL, 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];
180 f = sql_get_filter(e->prop);
188 snprintf(buf, sizeof(buf), f, ELEMENT_BOOL(e)->value);
191 snprintf(buf, sizeof(buf), f, ELEMENT_INT(e)->value);
194 if (E_AIL_PROP_NAME_STR == e->prop)
195 snprintf(buf, sizeof(buf), f, ELEMENT_STR(e)->value, ELEMENT_STR(e)->value);
197 snprintf(buf, sizeof(buf), f, ELEMENT_STR(e)->value);
200 _E("Invalid property type");
205 *condition = strdup(buf);
210 char *_get_where_clause(ail_filter_h filter)
213 char w[AIL_SQL_QUERY_MAX_LEN] = {0,};
218 snprintf(w, AIL_SQL_QUERY_MAX_LEN, " WHERE ");
220 for (l = filter->list; l; l = g_slist_next(l)) {
221 _get_condition(l->data, &c);
229 strncat(w, c, sizeof(w)-strlen(w)-1);
230 w[sizeof(w) - 1] = '\0';
236 if (g_slist_next(l)) {
237 strncat(w, " and ", sizeof(w)-strlen(w)-1);
238 w[sizeof(w) - 1] = '\0';
245 EXPORT_API ail_error_e ail_filter_count_appinfo(ail_filter_h filter, int *cnt)
247 char q[AIL_SQL_QUERY_MAX_LEN];
253 int filter_count = 0;
255 retv_if(!cnt, AIL_ERROR_INVALID_PARAMETER);
257 if (db_open(DB_OPEN_RO, GLOBAL_USER) != AIL_ERROR_OK)
258 return AIL_ERROR_DB_FAILED;
260 snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
263 retv_if(tmp_q == NULL, AIL_ERROR_OUT_OF_MEMORY);
264 l = sql_get_locale();
266 _E("Failed to get locale string");
268 return AIL_ERROR_FAIL;
270 snprintf(q, sizeof(q), tmp_q, l);
274 if (filter && filter->list) {
275 w = _get_where_clause(filter);
276 retv_if(w == NULL, AIL_ERROR_FAIL);
277 strncat(q, w, sizeof(q)-strlen(q)-1);
278 q[sizeof(q)-1] = '\0';
281 _D("No filter exists. All records are retreived");
285 if (db_prepare_globalro(q, &stmt) != AIL_ERROR_OK) {
286 _E("db_prepare_globalro fail for query = %s", q);
287 return AIL_ERROR_DB_FAILED;
289 ai = appinfo_create();
293 return AIL_ERROR_FAIL;
296 appinfo_set_stmt(ai, stmt);
297 while (db_step(stmt) == AIL_ERROR_OK) {
299 if (_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
313 EXPORT_API ail_error_e ail_filter_count_usr_appinfo(ail_filter_h filter, int *cnt, uid_t uid)
315 char q[AIL_SQL_QUERY_MAX_LEN];
321 int filter_count = 0;
323 retv_if(!cnt, AIL_ERROR_INVALID_PARAMETER);
325 /* is_admin ; redirect */
326 if (uid == GLOBAL_USER)
327 return ail_filter_count_appinfo(filter, cnt);
329 if (db_open(DB_OPEN_RO, uid) != AIL_ERROR_OK)
330 return AIL_ERROR_DB_FAILED;
332 snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
335 retv_if(tmp_q == NULL, AIL_ERROR_OUT_OF_MEMORY);
336 l = sql_get_locale();
338 _E("Failed to get locale string");
340 return AIL_ERROR_FAIL;
342 snprintf(q, sizeof(q), tmp_q, l);
346 if (filter && filter->list) {
347 w = _get_where_clause(filter);
348 retv_if(w == NULL, AIL_ERROR_FAIL);
349 strncat(q, w, sizeof(q) - strlen(q) - 1);
350 q[sizeof(q)-1] = '\0';
353 _D("No filter exists. All records are retreived");
356 if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
357 _E("db_prepare fail for query = %s", q);
358 return AIL_ERROR_DB_FAILED;
361 ai = appinfo_create();
363 _E("Failed to create appinfo");
365 return AIL_ERROR_OUT_OF_MEMORY;
368 appinfo_set_stmt(ai, stmt);
369 while (db_step(stmt) == AIL_ERROR_OK) {
370 if (_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
385 EXPORT_API ail_error_e ail_filter_list_appinfo_foreach(ail_filter_h filter, ail_list_appinfo_cb cb, void *user_data)
387 char q[AIL_SQL_QUERY_MAX_LEN];
395 retv_if(cb == NULL, AIL_ERROR_INVALID_PARAMETER);
397 if (db_open(DB_OPEN_RO, GLOBAL_USER) != AIL_ERROR_OK)
398 return AIL_ERROR_DB_FAILED;
400 snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
403 retv_if(tmp_q == NULL, AIL_ERROR_OUT_OF_MEMORY);
404 l = sql_get_locale();
406 _E("Failed to get locale string");
408 return AIL_ERROR_FAIL;
410 snprintf(q, sizeof(q), tmp_q, l);
414 if (filter && filter->list) {
415 w = _get_where_clause(filter);
416 retv_if(w == NULL, AIL_ERROR_FAIL);
417 strncat(q, w, sizeof(q)-strlen(q)-1);
418 q[sizeof(q) - 1] = '\0';
419 strncat(q, " order by app_info.package", sizeof(q) - strlen(q) - 1);
420 q[sizeof(q) - 1] = '\0';
423 _D("No filter exists. All records are retreived");
427 if (db_prepare_globalro(q, &stmt) != AIL_ERROR_OK) {
428 _E("db_prepare fail for query = %s", q);
429 return AIL_ERROR_DB_FAILED;
431 /* if (db_prepare_globalro(q, &stmt) != AIL_ERROR_OK) {
432 _E("db_prepare fail for query = %s",q);
433 return AIL_ERROR_DB_FAILED;
435 ai = appinfo_create();
439 return AIL_ERROR_FAIL;
442 appinfo_set_stmt(ai, stmt);
444 while ((i = db_step(stmt)) == AIL_ERROR_OK) {
445 _E("------------------------dbstep : %u\n", i);
446 if (_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
449 r = cb(ai, user_data, GLOBAL_USER);
450 if (AIL_CB_RET_CANCEL == r)
459 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)
461 char q[AIL_SQL_QUERY_MAX_LEN];
469 retv_if(cb == NULL, AIL_ERROR_INVALID_PARAMETER);
471 if (db_open(DB_OPEN_RO, uid) != AIL_ERROR_OK)
472 return AIL_ERROR_DB_FAILED;
474 snprintf(q, sizeof(q), "SELECT %s FROM %s", SQL_FLD_APP_INFO_WITH_LOCALNAME, SQL_TBL_APP_INFO_WITH_LOCALNAME);
477 retv_if(tmp_q == NULL, AIL_ERROR_OUT_OF_MEMORY);
478 l = sql_get_locale();
480 _E("Failed to get locale string");
482 return AIL_ERROR_FAIL;
484 snprintf(q, sizeof(q), tmp_q, l);
488 if (filter && filter->list) {
489 w = _get_where_clause(filter);
490 retv_if(w == NULL, AIL_ERROR_FAIL);
491 strncat(q, w, sizeof(q) - strlen(q) - 1);
492 q[sizeof(q) - 1] = '\0';
493 strncat(q, " order by app_info.package", sizeof(q) - strlen(q) - 1);
494 q[sizeof(q) - 1] = '\0';
497 _D("No filter exists. All records are retreived");
501 if (db_prepare(q, &stmt) != AIL_ERROR_OK) {
502 _E("db_prepare fail for query = %s", q);
503 return AIL_ERROR_DB_FAILED;
506 ai = appinfo_create();
508 _E("Failed to create appinfo");
510 return AIL_ERROR_DB_FAILED;
513 appinfo_set_stmt(ai, stmt);
515 while ((i = db_step(stmt)) == AIL_ERROR_OK) {
516 if (_appinfo_check_installed_storage(ai) != AIL_ERROR_OK)
519 r = cb(ai, user_data, uid);
520 if (AIL_CB_RET_CANCEL == r)