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++) {
259 EXPORT_API ail_error_e ail_package_get_appinfo(const char *package, ail_appinfo_h *ai)
261 return ail_get_appinfo(package, ai);
265 EXPORT_API ail_error_e ail_get_appinfo(const char *appid, ail_appinfo_h *ai)
268 char query[AIL_SQL_QUERY_MAX_LEN];
269 sqlite3_stmt *stmt = NULL;
270 char w[AIL_SQL_QUERY_MAX_LEN];
272 retv_if(!appid, AIL_ERROR_INVALID_PARAMETER);
273 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
275 *ai = appinfo_create();
276 retv_if(!*ai, AIL_ERROR_OUT_OF_MEMORY);
278 snprintf(w, sizeof(w), sql_get_filter(E_AIL_PROP_X_SLP_APPID_STR), appid);
280 snprintf(query, sizeof(query), "SELECT %s FROM %s WHERE %s",SQL_FLD_APP_INFO, SQL_TBL_APP_INFO, w);
283 ret = db_open(DB_OPEN_RO);
286 ret = db_prepare(query, &stmt);
297 ret = _retrieve_all_column(*ai);
299 db_finalize((*ai)->stmt);
303 ret = db_finalize((*ai)->stmt);
310 appinfo_destroy(*ai);
316 EXPORT_API ail_error_e ail_appinfo_get_bool(const ail_appinfo_h ai, const char *property, bool *value)
318 ail_prop_bool_e prop;
321 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
322 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
323 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
325 prop = _ail_convert_to_prop_bool(property);
327 if (prop < E_AIL_PROP_BOOL_MIN || prop > E_AIL_PROP_BOOL_MAX)
328 return AIL_ERROR_INVALID_PARAMETER;
332 index = sql_get_app_info_idx(prop);
333 if (db_column_bool(ai->stmt, index, value) < 0)
334 return AIL_ERROR_DB_FAILED;
336 val = atoi(ai->values[prop]);
337 *value = (val == 0? false : true);
344 EXPORT_API ail_error_e ail_appinfo_get_int(const ail_appinfo_h ai, const char *property, int *value)
348 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
349 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
350 retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
352 prop = _ail_convert_to_prop_int(property);
354 if (prop < E_AIL_PROP_INT_MIN || prop > E_AIL_PROP_INT_MAX)
355 return AIL_ERROR_INVALID_PARAMETER;
359 index = sql_get_app_info_idx(prop);
360 if (db_column_int(ai->stmt, index, value) < 0)
361 return AIL_ERROR_DB_FAILED;
363 *value = atoi(ai->values[prop]);
368 #define QUERY_GET_LOCALNAME "select name from localname where package='%s' and locale='%s'"
370 char *appinfo_get_localname(const char *package, char *locale)
378 snprintf(query, sizeof(query), QUERY_GET_LOCALNAME, package, locale);
380 _D("Query = %s",query);
381 retv_if (db_prepare(query, &stmt) < 0, NULL);
384 if (db_step(stmt) < 0)
386 if (db_column_str(stmt, 0, &str) < 0)
389 localname = strdup(str);
403 EXPORT_API ail_error_e ail_appinfo_get_str(const ail_appinfo_h ai, const char *property, char **str)
409 char *locale, *localname;
413 retv_if(!ai, AIL_ERROR_INVALID_PARAMETER);
414 retv_if(!property, AIL_ERROR_INVALID_PARAMETER);
415 retv_if(!str, AIL_ERROR_INVALID_PARAMETER);
417 prop = _ail_convert_to_prop_str(property);
419 if (prop < E_AIL_PROP_STR_MIN || prop > E_AIL_PROP_STR_MAX)
420 return AIL_ERROR_INVALID_PARAMETER;
424 if (E_AIL_PROP_NAME_STR == prop) {
426 if (db_column_str(ai->stmt, E_AIL_PROP_X_SLP_PACKAGETYPE_STR, &pkg_type) < 0)
427 return AIL_ERROR_DB_FAILED;
428 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
430 locale = sql_get_locale();
431 retv_if (NULL == locale, AIL_ERROR_FAIL);
433 if (db_column_str(ai->stmt, E_AIL_PROP_PACKAGE_STR, &pkg) < 0){
435 return AIL_ERROR_DB_FAILED;
439 return AIL_ERROR_DB_FAILED;
442 locale_new = __get_app_locale_by_fallback(pkg, locale);
443 localname = (char *)appinfo_get_localname(pkg,locale_new);
447 if (db_column_str(ai->stmt, SQL_LOCALNAME_IDX, &localname) < 0)
448 return AIL_ERROR_DB_FAILED;
451 pkg_type = ai->values[E_AIL_PROP_X_SLP_PACKAGETYPE_STR];
452 pkg = ai->values[E_AIL_PROP_PACKAGE_STR];
453 retv_if (NULL == pkg, AIL_ERROR_FAIL);
455 locale = sql_get_locale();
456 retv_if (NULL == locale, AIL_ERROR_FAIL);
458 if(pkg_type && (strcasecmp(pkg_type, "tpk") ==0))
460 locale_new = __get_app_locale_by_fallback(pkg, locale);
461 localname = (char *)appinfo_get_localname(pkg,locale_new);
465 localname = (char *)appinfo_get_localname(pkg,locale);
473 if (ai->values[prop])
474 free(ai->values[prop]);
475 ai->values[prop] = localname;
484 index = sql_get_app_info_idx(prop);
485 if (db_column_str(ai->stmt, index, &value) < 0){
486 return AIL_ERROR_DB_FAILED;
490 *str = ai->values[prop];
495 EXPORT_API ail_error_e ail_close_appinfo_db(void)