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"
33 #include "ail_package.h"
37 #define LANGUAGE_LENGTH 2
38 #define DEFAULT_LOCALE "No Locale"
39 #define MAX_QUERY_LEN 4096
40 #define PKG_SD_PATH "/opt/storage/sdcard/app2sd/"
41 #define QUERY_GET_LOCALNAME "select name from localname where package='%s' and locale='%s'"
48 typedef struct _pkgmgr_locale_x {
53 /* get the first locale value*/
54 static int __fallback_locale_cb(void *data, int ncols, char **coltxt, char **colname)
56 pkgmgr_locale_x *info = (pkgmgr_locale_x *)data;
59 info->locale = strdup(coltxt[0]);
66 static int __check_validation_of_qurey_cb(void *data, int ncols, char **coltxt, char **colname)
73 static int __check_app_locale_from_app_localized_info_by_exact(const char *appid, const char *locale)
75 int result_query = -1;
76 char query[MAX_QUERY_LEN];
78 snprintf(query, MAX_QUERY_LEN, "select exists(select locale from localname where package='%s' and locale='%s')", appid, locale);
79 db_exec_sqlite_query(query, __check_validation_of_qurey_cb, (void *)&result_query);
84 static int __check_app_locale_from_app_localized_info_by_fallback(const char *appid, const char *locale)
86 int result_query = -1;
87 char wildcard[2] = {'%','\0'};
88 char query[MAX_QUERY_LEN];
89 char lang[3] = {'\0'};
90 strncpy(lang, locale, LANGUAGE_LENGTH);
92 snprintf(query, MAX_QUERY_LEN, "select exists(select locale from localname where package='%s' and locale like '%s%s')", appid, lang, wildcard);
93 db_exec_sqlite_query(query, __check_validation_of_qurey_cb, (void *)&result_query);
98 static char* __get_app_locale_from_app_localized_info_by_fallback(const char *appid, const char *locale)
100 char wildcard[2] = {'%','\0'};
101 char lang[3] = {'\0'};
102 char query[MAX_QUERY_LEN];
103 char *locale_new = NULL;
104 pkgmgr_locale_x *info = NULL;
106 info = (pkgmgr_locale_x *)malloc(sizeof(pkgmgr_locale_x));
108 _E("Out of Memory!!!\n");
111 memset(info, NULL, sizeof(*info));
113 strncpy(lang, locale, 2);
114 snprintf(query, MAX_QUERY_LEN, "select locale from localname where package='%s' and locale like '%s%s'", appid, lang, wildcard);
115 db_exec_sqlite_query(query, __fallback_locale_cb, (void *)info);
116 locale_new = info->locale;
122 static char* __convert_syslocale_to_manifest_locale(char *syslocale)
124 char *locale = malloc(6);
126 _E("Malloc Failed\n");
130 sprintf(locale, "%c%c_%c%c", syslocale[0], syslocale[1], toupper(syslocale[3]), toupper(syslocale[4]));
134 static char* __get_app_locale_by_fallback(const char *appid, const char *syslocale)
140 char *locale_new = NULL;
141 int check_result = 0;
143 locale = __convert_syslocale_to_manifest_locale(syslocale);
145 /*check exact matching */
146 check_result = __check_app_locale_from_app_localized_info_by_exact(appid, locale);
149 if (check_result == 1) {
150 _D("%s find exact locale(%s)\n", appid, locale);
154 /* fallback matching */
155 check_result = __check_app_locale_from_app_localized_info_by_fallback(appid, locale);
156 if(check_result == 1) {
157 locale_new = __get_app_locale_from_app_localized_info_by_fallback(appid, locale);
158 _D("%s found (%s) language-locale in DB by fallback!\n", appid, locale_new);
160 if (locale_new == NULL)
161 locale_new = strdup(DEFAULT_LOCALE);
167 _D("%s DEFAULT_LOCALE)\n", appid);
168 return strdup(DEFAULT_LOCALE);
171 static ail_error_e __retrieve_all_column(ail_appinfo_h ai)
177 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
178 retv_if(!ai->stmt, AIL_ERROR_INVALID_PARAMETER);
180 ai->values = calloc(NUM_OF_PROP, sizeof(char *));
181 retv_if(!ai->values, AIL_ERROR_OUT_OF_MEMORY);
183 for (i = 0; i < NUM_OF_PROP; i++) {
184 err = db_column_str(ai->stmt, i, &col);
185 if (AIL_ERROR_OK != err)
189 ai->values[i] = NULL;
191 ai->values[i] = strdup(col);
192 if (!ai->values[i]) {
193 err = AIL_ERROR_OUT_OF_MEMORY;
200 for (j = 0; j < i; ++j) {
211 int _appinfo_check_installed_storage(ail_appinfo_h ai)
214 ail_prop_str_e prop = -1;
216 char *installed_storage = NULL;
217 char buf[AIL_SQL_QUERY_MAX_LEN] = {'\0'};
219 retv_if(!ai, AIL_ERROR_OK);
222 prop = _ail_convert_to_prop_str(AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR);
223 index = sql_get_app_info_idx(prop);
224 if (db_column_str(ai->stmt, index, &installed_storage) < 0){
228 prop = _ail_convert_to_prop_str(AIL_PROP_X_SLP_PKGID_STR);
229 index = sql_get_app_info_idx(prop);
230 if (db_column_str(ai->stmt, index, &pkgid) < 0){
234 prop = _ail_convert_to_prop_str(AIL_PROP_X_SLP_INSTALLEDSTORAGE_STR);
235 installed_storage = ai->values[prop];
237 prop = _ail_convert_to_prop_str(AIL_PROP_X_SLP_PKGID_STR);
238 pkgid = ai->values[prop];
241 if (strcmp(installed_storage, "installed_external") == 0) {
242 snprintf(buf, AIL_SQL_QUERY_MAX_LEN - 1, "%s%s", PKG_SD_PATH, pkgid);
243 if (access(buf, R_OK) != 0) {
244 _E("can not access [%s]", buf);
245 return AIL_ERROR_OK;//tmep, it will be fixed to :: return AIL_ERROR_FAIL;
252 void appinfo_set_stmt(ail_appinfo_h ai, sqlite3_stmt *stmt)
257 ail_appinfo_h appinfo_create(void)
260 ai = calloc(1, sizeof(struct ail_appinfo));
261 retv_if (NULL == ai, NULL);
267 void appinfo_destroy(ail_appinfo_h ai)
273 EXPORT_API ail_error_e ail_package_destroy_appinfo(ail_appinfo_h ai)
275 return ail_destroy_appinfo(ai);
278 EXPORT_API ail_error_e ail_destroy_appinfo(ail_appinfo_h ai)
282 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
283 retv_if(!ai->values, AIL_ERROR_INVALID_PARAMETER);
285 for (i = 0; i < NUM_OF_PROP; i++) {
299 EXPORT_API ail_error_e ail_package_get_appinfo(const char *package, ail_appinfo_h *ai)
301 return ail_get_appinfo(package, ai);
304 EXPORT_API ail_error_e ail_get_appinfo(const char *appid, ail_appinfo_h *ai)
307 char query[AIL_SQL_QUERY_MAX_LEN];
308 sqlite3_stmt *stmt = NULL;
309 char w[AIL_SQL_QUERY_MAX_LEN];
311 retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
312 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
314 *ai = appinfo_create();
315 retv_if(!*ai, AIL_ERROR_OUT_OF_MEMORY);
317 snprintf(w, sizeof(w), sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR), appid);
319 snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
322 ret = db_open(DB_OPEN_RO);
325 ret = db_prepare(query, &stmt);
336 ret = _appinfo_check_installed_storage(*ai);
338 db_finalize((*ai)->stmt);
342 ret = __retrieve_all_column(*ai);
344 db_finalize((*ai)->stmt);
348 ret = db_finalize((*ai)->stmt);
355 appinfo_destroy(*ai);
361 EXPORT_API ail_error_e ail_appinfo_get_bool(const ail_appinfo_h ai, const char *property, bool *value)
363 ail_prop_bool_e prop;
366 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
367 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
368 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
370 prop = _ail_convert_to_prop_bool(property);
372 if (prop < E_AIL_PROP_BOOL_MIN || prop > E_AIL_PROP_BOOL_MAX)
373 return AIL_ERROR_INVALID_PARAMETER;
377 index = sql_get_app_info_idx(prop);
378 if (db_column_bool(ai->stmt, index, value) < 0)
379 return AIL_ERROR_DB_FAILED;
381 val = atoi(ai->values[prop]);
382 *value = (val == 0? false : true);
389 EXPORT_API ail_error_e ail_appinfo_get_int(const ail_appinfo_h ai, const char *property, int *value)
393 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
394 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
395 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
397 prop = _ail_convert_to_prop_int(property);
399 if (prop < E_AIL_PROP_INT_MIN || prop > E_AIL_PROP_INT_MAX)
400 return AIL_ERROR_INVALID_PARAMETER;
404 index = sql_get_app_info_idx(prop);
405 if (db_column_int(ai->stmt, index, value) < 0)
406 return AIL_ERROR_DB_FAILED;
408 *value = atoi(ai->values[prop]);
413 char *appinfo_get_localname(const char *package, char *locale)
421 snprintf(query, sizeof(query), QUERY_GET_LOCALNAME, package, locale);
423 // _D("Query = %s",query);
424 retv_if (db_prepare(query, &stmt) < 0, NULL);
427 if (db_step(stmt) < 0)
429 if (db_column_str(stmt, 0, &str) < 0)
432 localname = strdup(str);
446 EXPORT_API ail_error_e ail_appinfo_get_str(const ail_appinfo_h ai, const char *property, char **str)
452 char *locale, *localname;
456 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
457 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
458 retv_if(!str, AIL_ERROR_INVALID_PARAMETER);
460 prop = _ail_convert_to_prop_str(property);
462 if (prop < E_AIL_PROP_STR_MIN || prop > E_AIL_PROP_STR_MAX)
463 return AIL_ERROR_INVALID_PARAMETER;
467 if (E_AIL_PROP_NAME_STR == prop) {
469 if (db_column_str(ai->stmt, E_AIL_PROP_X_SLP_PACKAGETYPE_STR, &pkg_type) < 0)
470 return AIL_ERROR_DB_FAILED;
471 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
473 locale = sql_get_locale();
474 retv_if (NULL == locale, AIL_ERROR_FAIL);
476 if (db_column_str(ai->stmt, E_AIL_PROP_PACKAGE_STR, &pkg) < 0){
478 return AIL_ERROR_DB_FAILED;
482 return AIL_ERROR_DB_FAILED;
485 locale_new = __get_app_locale_by_fallback(pkg, locale);
486 localname = (char *)appinfo_get_localname(pkg,locale_new);
490 if (db_column_str(ai->stmt, SQL_LOCALNAME_IDX, &localname) < 0)
491 return AIL_ERROR_DB_FAILED;
494 pkg_type = ai->values[E_AIL_PROP_X_SLP_PACKAGETYPE_STR];
495 pkg = ai->values[E_AIL_PROP_PACKAGE_STR];
496 retv_if (NULL == pkg, AIL_ERROR_FAIL);
498 locale = sql_get_locale();
499 retv_if (NULL == locale, AIL_ERROR_FAIL);
501 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
503 locale_new = __get_app_locale_by_fallback(pkg, locale);
504 localname = (char *)appinfo_get_localname(pkg,locale_new);
508 localname = (char *)appinfo_get_localname(pkg,locale);
516 if (ai->values[prop])
517 free(ai->values[prop]);
518 ai->values[prop] = localname;
527 index = sql_get_app_info_idx(prop);
528 if (db_column_str(ai->stmt, index, &value) < 0){
529 return AIL_ERROR_DB_FAILED;
533 *str = ai->values[prop];
538 EXPORT_API ail_error_e ail_close_appinfo_db(void)