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
46 typedef struct _pkgmgr_locale_x {
51 /* get the first locale value*/
52 static int __fallback_locale_cb(void *data, int ncols, char **coltxt, char **colname)
54 pkgmgr_locale_x *info = (pkgmgr_locale_x *)data;
57 info->locale = strdup(coltxt[0]);
64 static int __check_validation_of_qurey_cb(void *data, int ncols, char **coltxt, char **colname)
71 static int __check_app_locale_from_app_localized_info_by_exact(const char *appid, const char *locale)
73 int result_query = -1;
74 char query[MAX_QUERY_LEN];
76 snprintf(query, MAX_QUERY_LEN, "select exists(select locale from localname where package='%s' and locale='%s')", appid, locale);
77 db_exec_sqlite_query(query, __check_validation_of_qurey_cb, (void *)&result_query);
82 static int __check_app_locale_from_app_localized_info_by_fallback(const char *appid, const char *locale)
84 int result_query = -1;
85 char wildcard[2] = {'%','\0'};
86 char query[MAX_QUERY_LEN];
87 char lang[3] = {'\0'};
88 strncpy(lang, locale, LANGUAGE_LENGTH);
90 snprintf(query, MAX_QUERY_LEN, "select exists(select locale from localname where package='%s' and locale like '%s%s')", appid, lang, wildcard);
91 db_exec_sqlite_query(query, __check_validation_of_qurey_cb, (void *)&result_query);
96 static char* __get_app_locale_from_app_localized_info_by_fallback(const char *appid, const char *locale)
98 char wildcard[2] = {'%','\0'};
99 char lang[3] = {'\0'};
100 char query[MAX_QUERY_LEN];
101 char *locale_new = NULL;
102 pkgmgr_locale_x *info = NULL;
104 info = (pkgmgr_locale_x *)malloc(sizeof(pkgmgr_locale_x));
106 _E("Out of Memory!!!\n");
109 memset(info, NULL, sizeof(*info));
111 strncpy(lang, locale, 2);
112 snprintf(query, MAX_QUERY_LEN, "select locale from localname where package='%s' and locale like '%s%s'", appid, lang, wildcard);
113 db_exec_sqlite_query(query, __fallback_locale_cb, (void *)info);
114 locale_new = info->locale;
120 static char* __convert_syslocale_to_manifest_locale(char *syslocale)
122 char *locale = malloc(6);
124 _E("Malloc Failed\n");
128 sprintf(locale, "%c%c_%c%c", syslocale[0], syslocale[1], toupper(syslocale[3]), toupper(syslocale[4]));
132 static char* __get_app_locale_by_fallback(const char *appid, const char *syslocale)
138 char *locale_new = NULL;
139 int check_result = 0;
141 locale = __convert_syslocale_to_manifest_locale(syslocale);
143 /*check exact matching */
144 check_result = __check_app_locale_from_app_localized_info_by_exact(appid, locale);
147 if (check_result == 1) {
148 _D("%s find exact locale(%s)\n", appid, locale);
152 /* fallback matching */
153 check_result = __check_app_locale_from_app_localized_info_by_fallback(appid, locale);
154 if(check_result == 1) {
155 locale_new = __get_app_locale_from_app_localized_info_by_fallback(appid, locale);
156 _D("%s found (%s) language-locale in DB by fallback!\n", appid, locale_new);
158 if (locale_new == NULL)
159 locale_new = strdup(DEFAULT_LOCALE);
165 _D("%s DEFAULT_LOCALE)\n", appid);
166 return strdup(DEFAULT_LOCALE);
169 void appinfo_set_stmt(ail_appinfo_h ai, sqlite3_stmt *stmt)
174 ail_appinfo_h appinfo_create(void)
177 ai = calloc(1, sizeof(struct ail_appinfo));
178 retv_if (NULL == ai, NULL);
184 void appinfo_destroy(ail_appinfo_h ai)
192 static ail_error_e _retrieve_all_column(ail_appinfo_h ai)
198 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
199 retv_if(!ai->stmt, AIL_ERROR_INVALID_PARAMETER);
201 ai->values = calloc(NUM_OF_PROP, sizeof(char *));
202 retv_if(!ai->values, AIL_ERROR_OUT_OF_MEMORY);
204 for (i = 0; i < NUM_OF_PROP; i++) {
205 err = db_column_str(ai->stmt, i, &col);
206 if (AIL_ERROR_OK != err)
210 ai->values[i] = NULL;
212 ai->values[i] = strdup(col);
213 if (!ai->values[i]) {
214 err = AIL_ERROR_OUT_OF_MEMORY;
221 for (j = 0; j < i; ++j) {
233 EXPORT_API ail_error_e ail_package_destroy_appinfo(ail_appinfo_h ai)
235 return ail_destroy_appinfo(ai);
238 EXPORT_API ail_error_e ail_destroy_appinfo(ail_appinfo_h ai)
242 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
243 retv_if(!ai->values, AIL_ERROR_INVALID_PARAMETER);
245 for (i = 0; i < NUM_OF_PROP; i++) {
258 EXPORT_API ail_error_e ail_package_get_appinfo(const char *package, ail_appinfo_h *ai)
260 return ail_get_appinfo(package, ai);
264 EXPORT_API ail_error_e ail_get_appinfo(const char *appid, ail_appinfo_h *ai)
267 char query[AIL_SQL_QUERY_MAX_LEN];
268 sqlite3_stmt *stmt = NULL;
269 char w[AIL_SQL_QUERY_MAX_LEN];
271 retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
272 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
274 *ai = appinfo_create();
275 retv_if(!*ai, AIL_ERROR_OUT_OF_MEMORY);
277 snprintf(w, sizeof(w), sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR), appid);
279 snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
282 ret = db_open(DB_OPEN_RO);
285 ret = db_prepare(query, &stmt);
296 ret = _retrieve_all_column(*ai);
298 db_finalize((*ai)->stmt);
302 ret = db_finalize((*ai)->stmt);
309 appinfo_destroy(*ai);
315 EXPORT_API ail_error_e ail_appinfo_get_bool(const ail_appinfo_h ai, const char *property, bool *value)
317 ail_prop_bool_e prop;
320 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
321 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
322 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
324 prop = _ail_convert_to_prop_bool(property);
326 if (prop < E_AIL_PROP_BOOL_MIN || prop > E_AIL_PROP_BOOL_MAX)
327 return AIL_ERROR_INVALID_PARAMETER;
331 index = sql_get_app_info_idx(prop);
332 if (db_column_bool(ai->stmt, index, value) < 0)
333 return AIL_ERROR_DB_FAILED;
335 val = atoi(ai->values[prop]);
336 *value = (val == 0? false : true);
343 EXPORT_API ail_error_e ail_appinfo_get_int(const ail_appinfo_h ai, const char *property, int *value)
347 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
348 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
349 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
351 prop = _ail_convert_to_prop_int(property);
353 if (prop < E_AIL_PROP_INT_MIN || prop > E_AIL_PROP_INT_MAX)
354 return AIL_ERROR_INVALID_PARAMETER;
358 index = sql_get_app_info_idx(prop);
359 if (db_column_int(ai->stmt, index, value) < 0)
360 return AIL_ERROR_DB_FAILED;
362 *value = atoi(ai->values[prop]);
367 #define QUERY_GET_LOCALNAME "select name from localname where package='%s' and locale='%s'"
369 char *appinfo_get_localname(const char *package, char *locale)
377 snprintf(query, sizeof(query), QUERY_GET_LOCALNAME, package, locale);
379 _D("Query = %s",query);
380 retv_if (db_prepare(query, &stmt) < 0, NULL);
383 if (db_step(stmt) < 0)
385 if (db_column_str(stmt, 0, &str) < 0)
388 localname = strdup(str);
402 EXPORT_API ail_error_e ail_appinfo_get_str(const ail_appinfo_h ai, const char *property, char **str)
408 char *locale, *localname;
412 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
413 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
414 retv_if(!str, AIL_ERROR_INVALID_PARAMETER);
416 prop = _ail_convert_to_prop_str(property);
418 if (prop < E_AIL_PROP_STR_MIN || prop > E_AIL_PROP_STR_MAX)
419 return AIL_ERROR_INVALID_PARAMETER;
423 if (E_AIL_PROP_NAME_STR == prop) {
425 if (db_column_str(ai->stmt, E_AIL_PROP_X_SLP_PACKAGETYPE_STR, &pkg_type) < 0)
426 return AIL_ERROR_DB_FAILED;
427 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
429 locale = sql_get_locale();
430 retv_if (NULL == locale, AIL_ERROR_FAIL);
432 if (db_column_str(ai->stmt, E_AIL_PROP_PACKAGE_STR, &pkg) < 0){
434 return AIL_ERROR_DB_FAILED;
438 return AIL_ERROR_DB_FAILED;
441 locale_new = __get_app_locale_by_fallback(pkg, locale);
442 localname = (char *)appinfo_get_localname(pkg,locale_new);
446 if (db_column_str(ai->stmt, SQL_LOCALNAME_IDX, &localname) < 0)
447 return AIL_ERROR_DB_FAILED;
450 pkg_type = ai->values[E_AIL_PROP_X_SLP_PACKAGETYPE_STR];
451 pkg = ai->values[E_AIL_PROP_PACKAGE_STR];
452 retv_if (NULL == pkg, AIL_ERROR_FAIL);
454 locale = sql_get_locale();
455 retv_if (NULL == locale, AIL_ERROR_FAIL);
457 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
459 locale_new = __get_app_locale_by_fallback(pkg, locale);
460 localname = (char *)appinfo_get_localname(pkg,locale_new);
464 localname = (char *)appinfo_get_localname(pkg,locale);
472 if (ai->values[prop])
473 free(ai->values[prop]);
474 ai->values[prop] = localname;
483 index = sql_get_app_info_idx(prop);
484 if (db_column_str(ai->stmt, index, &value) < 0){
485 return AIL_ERROR_DB_FAILED;
489 *str = ai->values[prop];
494 EXPORT_API ail_error_e ail_close_appinfo_db(void)