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.
27 #include <tzplatform_config.h>
30 #include "ail_private.h"
31 #include "ail_convert.h"
34 #include "ail_package.h"
38 #define LANGUAGE_LENGTH 2
39 #define DEFAULT_LOCALE "No Locale"
40 #define MAX_QUERY_LEN 4096
41 #define PKG_SD_PATH tzplatform_mkpath(TZ_SYS_STORAGE, "sdcard/app2sd/")
42 #define QUERY_GET_LOCALNAME "select name from localname where package='%s' and locale='%s'"
49 typedef struct _pkgmgr_locale_x {
54 /* get the first locale value*/
55 static int __fallback_locale_cb(void *data, int ncols, char **coltxt, char **colname)
57 pkgmgr_locale_x *info = (pkgmgr_locale_x *)data;
60 info->locale = strdup(coltxt[0]);
67 static int __check_validation_of_qurey_cb(void *data, int ncols, char **coltxt, char **colname)
74 static int __check_app_locale_from_app_localized_info_by_exact(const char *appid, const char *locale)
76 int result_query = -1;
77 char query[MAX_QUERY_LEN];
79 snprintf(query, MAX_QUERY_LEN, "select exists(select locale from localname where package='%s' and locale='%s')", appid, locale);
80 db_exec_sqlite_query(query, __check_validation_of_qurey_cb, (void *)&result_query);
85 static int __check_app_locale_from_app_localized_info_by_fallback(const char *appid, const char *locale)
87 int result_query = -1;
88 char wildcard[2] = {'%','\0'};
89 char query[MAX_QUERY_LEN];
90 char lang[3] = {'\0'};
91 strncpy(lang, locale, LANGUAGE_LENGTH);
93 snprintf(query, MAX_QUERY_LEN, "select exists(select locale from localname where package='%s' and locale like '%s%s')", appid, lang, wildcard);
94 db_exec_sqlite_query(query, __check_validation_of_qurey_cb, (void *)&result_query);
99 static char* __get_app_locale_from_app_localized_info_by_fallback(const char *appid, const char *locale)
101 char wildcard[2] = {'%','\0'};
102 char lang[3] = {'\0'};
103 char query[MAX_QUERY_LEN];
104 char *locale_new = NULL;
105 pkgmgr_locale_x *info = NULL;
107 info = (pkgmgr_locale_x *)malloc(sizeof(pkgmgr_locale_x));
109 _E("Out of Memory!!!\n");
112 memset(info, NULL, sizeof(*info));
114 strncpy(lang, locale, 2);
115 snprintf(query, MAX_QUERY_LEN, "select locale from localname where package='%s' and locale like '%s%s'", appid, lang, wildcard);
116 db_exec_sqlite_query(query, __fallback_locale_cb, (void *)info);
117 locale_new = info->locale;
123 static char* __convert_syslocale_to_manifest_locale(char *syslocale)
125 char *locale = malloc(6);
127 _E("Malloc Failed\n");
131 sprintf(locale, "%c%c_%c%c", syslocale[0], syslocale[1], toupper(syslocale[3]), toupper(syslocale[4]));
135 static char* __get_app_locale_by_fallback(const char *appid, const char *syslocale)
141 char *locale_new = NULL;
142 int check_result = 0;
144 locale = __convert_syslocale_to_manifest_locale(syslocale);
146 /*check exact matching */
147 check_result = __check_app_locale_from_app_localized_info_by_exact(appid, locale);
150 if (check_result == 1) {
151 _D("%s find exact locale(%s)\n", appid, locale);
155 /* fallback matching */
156 check_result = __check_app_locale_from_app_localized_info_by_fallback(appid, locale);
157 if(check_result == 1) {
158 locale_new = __get_app_locale_from_app_localized_info_by_fallback(appid, locale);
159 _D("%s found (%s) language-locale in DB by fallback!\n", appid, locale_new);
161 if (locale_new == NULL)
162 locale_new = strdup(DEFAULT_LOCALE);
168 _D("%s DEFAULT_LOCALE)\n", appid);
169 return strdup(DEFAULT_LOCALE);
172 static ail_error_e __retrieve_all_column(ail_appinfo_h ai)
178 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
179 retv_if(!ai->stmt, AIL_ERROR_INVALID_PARAMETER);
181 ai->values = calloc(NUM_OF_PROP, sizeof(char *));
182 retv_if(!ai->values, AIL_ERROR_OUT_OF_MEMORY);
184 for (i = 0; i < NUM_OF_PROP; i++) {
185 err = db_column_str(ai->stmt, i, &col);
186 if (AIL_ERROR_OK != err)
190 ai->values[i] = NULL;
192 ai->values[i] = strdup(col);
193 if (!ai->values[i]) {
194 err = AIL_ERROR_OUT_OF_MEMORY;
201 for (j = 0; j < i; ++j) {
212 int _appinfo_check_installed_storage(ail_appinfo_h ai)
215 ail_prop_str_e prop = -1;
217 char *installed_storage = NULL;
218 char buf[AIL_SQL_QUERY_MAX_LEN] = {'\0'};
220 retv_if(!ai, AIL_ERROR_OK);
223 prop = _ail_convert_to_prop_str(AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR);
224 index = sql_get_app_info_idx(prop);
225 if (db_column_str(ai->stmt, index, &installed_storage) < 0){
229 prop = _ail_convert_to_prop_str(AIL_PROP_X_SLP_PKGID_STR);
230 index = sql_get_app_info_idx(prop);
231 if (db_column_str(ai->stmt, index, &pkgid) < 0){
235 prop = _ail_convert_to_prop_str(AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR);
236 installed_storage = ai->values[prop];
238 prop = _ail_convert_to_prop_str(AIL_PROP_X_SLP_PKGID_STR);
239 pkgid = ai->values[prop];
242 if (strcmp(installed_storage, "installed_external") == 0) {
243 snprintf(buf, AIL_SQL_QUERY_MAX_LEN - 1, "%s%s", PKG_SD_PATH, pkgid);
244 if (access(buf, R_OK) != 0) {
245 _E("can not access [%s]", buf);
246 return AIL_ERROR_OK;//tmep, it will be fixed to :: return AIL_ERROR_FAIL;
253 void appinfo_set_stmt(ail_appinfo_h ai, sqlite3_stmt *stmt)
258 ail_appinfo_h appinfo_create(void)
261 ai = calloc(1, sizeof(struct ail_appinfo));
262 retv_if (NULL == ai, NULL);
268 void appinfo_destroy(ail_appinfo_h ai)
274 EXPORT_API ail_error_e ail_package_destroy_appinfo(ail_appinfo_h ai)
276 return ail_destroy_appinfo(ai);
279 EXPORT_API ail_error_e ail_destroy_appinfo(ail_appinfo_h ai)
283 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
284 retv_if(!ai->values, AIL_ERROR_INVALID_PARAMETER);
286 for (i = 0; i < NUM_OF_PROP; i++) {
300 EXPORT_API ail_error_e ail_package_get_appinfo(const char *package, ail_appinfo_h *ai)
302 return ail_get_appinfo(package, ai);
305 EXPORT_API ail_error_e ail_package_get_usr_appinfo(const char *package, uid_t uid, ail_appinfo_h *ai)
307 return ail_get_usr_appinfo(package, uid, ai);
312 EXPORT_API ail_error_e ail_get_appinfo(const char *appid, ail_appinfo_h *ai)
315 char query[AIL_SQL_QUERY_MAX_LEN];
316 sqlite3_stmt *stmt = NULL;
317 char w[AIL_SQL_QUERY_MAX_LEN];
319 retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
320 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
322 *ai = appinfo_create();
323 retv_if(!*ai, AIL_ERROR_OUT_OF_MEMORY);
325 snprintf(w, sizeof(w), sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR), appid);
327 snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
330 ret = db_open(DB_OPEN_RO, GLOBAL_USER);
333 ret = db_prepare(query, &stmt);
335 // ret = db_prepare(query, &stmt);
336 // if (ret < 0) break;
347 ret = _appinfo_check_installed_storage(*ai);
349 db_finalize((*ai)->stmt);
353 ret = __retrieve_all_column(*ai);
355 db_finalize((*ai)->stmt);
359 ret = db_finalize((*ai)->stmt);
366 appinfo_destroy(*ai);
371 EXPORT_API ail_error_e ail_get_usr_appinfo(const char *appid, uid_t uid, ail_appinfo_h *ai)
374 char query[AIL_SQL_QUERY_MAX_LEN];
375 sqlite3_stmt *stmt = NULL;
376 char w[AIL_SQL_QUERY_MAX_LEN];
378 retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
379 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
381 *ai = appinfo_create();
382 retv_if(!*ai, AIL_ERROR_OUT_OF_MEMORY);
384 snprintf(w, sizeof(w), sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR), appid);
386 snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
389 ret = db_open(DB_OPEN_RO, uid);
392 ret = db_prepare(query, &stmt);
394 // ret = db_prepare(query, &stmt);
395 // if (ret < 0) break;
406 ret = _appinfo_check_installed_storage(*ai);
408 db_finalize((*ai)->stmt);
412 ret = __retrieve_all_column(*ai);
414 db_finalize((*ai)->stmt);
418 ret = db_finalize((*ai)->stmt);
425 appinfo_destroy(*ai);
429 EXPORT_API ail_error_e ail_appinfo_get_bool(const ail_appinfo_h ai, const char *property, bool *value)
431 ail_prop_bool_e prop;
434 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
435 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
436 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
438 prop = _ail_convert_to_prop_bool(property);
440 if (prop < E_AIL_PROP_BOOL_MIN || prop > E_AIL_PROP_BOOL_MAX)
441 return AIL_ERROR_INVALID_PARAMETER;
445 index = sql_get_app_info_idx(prop);
446 if (db_column_bool(ai->stmt, index, value) < 0)
447 return AIL_ERROR_DB_FAILED;
449 val = atoi(ai->values[prop]);
450 *value = (val == 0? false : true);
455 EXPORT_API ail_error_e ail_appinfo_get_int(const ail_appinfo_h ai, const char *property, int *value)
459 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
460 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
461 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
463 prop = _ail_convert_to_prop_int(property);
465 if (prop < E_AIL_PROP_INT_MIN || prop > E_AIL_PROP_INT_MAX)
466 return AIL_ERROR_INVALID_PARAMETER;
470 index = sql_get_app_info_idx(prop);
471 if (db_column_int(ai->stmt, index, value) < 0)
472 return AIL_ERROR_DB_FAILED;
474 *value = atoi(ai->values[prop]);
479 char *appinfo_get_localname(const char *package, char *locale, uid_t uid)
481 db_open(DB_OPEN_RO, uid);
487 snprintf(query, sizeof(query), QUERY_GET_LOCALNAME, package, locale);
489 // _D("Query = %s",query);
491 retv_if (db_prepare(query, &stmt) < 0, NULL);
492 //retv_if (db_prepare(query, &stmt) < 0, NULL);
495 if (db_step(stmt) < 0)
497 if (db_column_str(stmt, 0, &str) < 0)
500 localname = strdup(str);
513 EXPORT_API ail_error_e ail_appinfo_get_str(const ail_appinfo_h ai, const char *property, char **str)
519 char *locale, *localname;
523 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
524 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
525 retv_if(!str, AIL_ERROR_INVALID_PARAMETER);
527 prop = _ail_convert_to_prop_str(property);
529 if (prop < E_AIL_PROP_STR_MIN || prop > E_AIL_PROP_STR_MAX)
530 return AIL_ERROR_INVALID_PARAMETER;
534 if (E_AIL_PROP_NAME_STR == prop) {
536 if (db_column_str(ai->stmt, E_AIL_PROP_X_SLP_PACKAGETYPE_STR, &pkg_type) < 0)
537 return AIL_ERROR_DB_FAILED;
538 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
540 locale = sql_get_locale();
541 retv_if (NULL == locale, AIL_ERROR_FAIL);
543 if (db_column_str(ai->stmt, E_AIL_PROP_PACKAGE_STR, &pkg) < 0){
545 return AIL_ERROR_DB_FAILED;
549 return AIL_ERROR_DB_FAILED;
552 locale_new = __get_app_locale_by_fallback(pkg, locale);
553 localname = (char *)appinfo_get_localname(pkg, locale_new, GLOBAL_USER);
557 if (db_column_str(ai->stmt, SQL_LOCALNAME_IDX, &localname) < 0)
558 return AIL_ERROR_DB_FAILED;
561 pkg_type = ai->values[E_AIL_PROP_X_SLP_PACKAGETYPE_STR];
562 pkg = ai->values[E_AIL_PROP_PACKAGE_STR];
563 retv_if (NULL == pkg, AIL_ERROR_FAIL);
565 locale = sql_get_locale();
566 retv_if (NULL == locale, AIL_ERROR_FAIL);
568 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
570 locale_new = __get_app_locale_by_fallback(pkg, locale);
571 localname = (char *)appinfo_get_localname(pkg, locale_new, GLOBAL_USER);
575 localname = (char *)appinfo_get_localname(pkg, locale, GLOBAL_USER);
583 if (ai->values[prop])
584 free(ai->values[prop]);
585 ai->values[prop] = localname;
594 index = sql_get_app_info_idx(prop);
595 if (db_column_str(ai->stmt, index, &value) < 0){
596 return AIL_ERROR_DB_FAILED;
600 *str = ai->values[prop];
604 EXPORT_API ail_error_e ail_appinfo_get_usr_str(const ail_appinfo_h ai, const char *property, uid_t uid, char **str)
610 char *locale, *localname;
614 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
615 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
616 retv_if(!str, AIL_ERROR_INVALID_PARAMETER);
618 prop = _ail_convert_to_prop_str(property);
620 if (prop < E_AIL_PROP_STR_MIN || prop > E_AIL_PROP_STR_MAX)
621 return AIL_ERROR_INVALID_PARAMETER;
625 if (E_AIL_PROP_NAME_STR == prop) {
627 if (db_column_str(ai->stmt, E_AIL_PROP_X_SLP_PACKAGETYPE_STR, &pkg_type) < 0)
628 return AIL_ERROR_DB_FAILED;
629 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
631 locale = sql_get_locale();
632 retv_if (NULL == locale, AIL_ERROR_FAIL);
634 if (db_column_str(ai->stmt, E_AIL_PROP_PACKAGE_STR, &pkg) < 0){
636 return AIL_ERROR_DB_FAILED;
640 return AIL_ERROR_DB_FAILED;
643 locale_new = __get_app_locale_by_fallback(pkg, locale);
644 localname = (char *)appinfo_get_localname(pkg, locale_new, uid);
648 if (db_column_str(ai->stmt, SQL_LOCALNAME_IDX, &localname) < 0)
649 return AIL_ERROR_DB_FAILED;
652 pkg_type = ai->values[E_AIL_PROP_X_SLP_PACKAGETYPE_STR];
653 pkg = ai->values[E_AIL_PROP_PACKAGE_STR];
654 retv_if (NULL == pkg, AIL_ERROR_FAIL);
656 locale = sql_get_locale();
657 retv_if (NULL == locale, AIL_ERROR_FAIL);
659 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
661 locale_new = __get_app_locale_by_fallback(pkg, locale);
662 localname = (char *)appinfo_get_localname(pkg, locale_new, uid);
666 localname = (char *)appinfo_get_localname(pkg, locale, uid);
674 if (ai->values[prop])
675 free(ai->values[prop]);
676 ai->values[prop] = localname;
685 index = sql_get_app_info_idx(prop);
686 if (db_column_str(ai->stmt, index, &value) < 0){
687 return AIL_ERROR_DB_FAILED;
691 *str = ai->values[prop];
697 EXPORT_API ail_error_e ail_close_appinfo_db(void)