#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/smack.h>
-#include <db-util.h>
+
+#include <dlfcn.h>
+
+#include <unicode/utypes.h>
+#include <unicode/ucol.h>
+#include <unicode/uiter.h>
+#include <unicode/ustring.h>
+#include <vconf.h>
+
#include "media-server-ipc.h"
#include "media-util-dbg.h"
#include "media-util-internal.h"
#define BATCH_START "BEGIN"
#define BATCH_END "COMMIT"
+#define PATH_PLUGIN_LIBICU PATH_LIBDIR"/libicui18n.so"
+
+UCollator *(*icu_ucol_open)(const char *, UErrorCode *);
+void (*icu_ucol_close)(UCollator *);
+void (*icu_ucol_setStrength)(UCollator *, UCollationStrength);
+UCollationResult (*icu_ucol_strcollIter)(const UCollator *, UCharIterator *, UCharIterator *, UErrorCode *);
+void (*icu_uiter_setUTF8)(UCharIterator *, const char *, int32_t);
+void (*icu_uloc_setDefault)(const char* localeID, UErrorCode* status);
+const char* (*icu_uloc_getDefault)(void);
+
+static void *funcHandle = NULL;
static __thread GArray *sql_list = NULL;
return 200 - count;
}
+/* The collating function must return an integer that is negative, zero or positive */
+static int __media_db_collate_icu_8(void *ucol, int str1_len, const void *str1, int str2_len, const void *str2)
+{
+ UCharIterator uiter1, uiter2;
+ UErrorCode error = U_ZERO_ERROR;
+
+ icu_ucol_strcollIter = dlsym(funcHandle, "ucol_strcollIter");
+ icu_uiter_setUTF8 = dlsym(funcHandle, "uiter_setUTF8");
+
+ icu_uiter_setUTF8(&uiter1, (const char *) str1, str1_len);
+ icu_uiter_setUTF8(&uiter2, (const char *) str2, str2_len);
+
+ UCollationResult result = icu_ucol_strcollIter((UCollator *) ucol, &uiter1, &uiter2, &error);
+ if (U_FAILURE(error)) {
+ MSAPI_DBG_ERR("ucol_strcollIter error: %d", error);
+ return -1;
+ }
+
+ return result;
+}
+
+void __media_db_collation_cb(void* pArg, sqlite3* handle, int charset, const char* name)
+{
+ int ret = MS_MEDIA_ERR_NONE;
+ UErrorCode status = U_ZERO_ERROR;
+ const char* locale = NULL;
+ char *lang = NULL;
+ UCollator* ucol = NULL;
+
+ if (handle == NULL || !MS_STRING_VALID(name)) {
+ MSAPI_DBG_ERR("Input parameter is NULL");
+ return;
+ }
+
+ if (charset == SQLITE_UTF8 && !sqlite3_stricmp(name, "localized")) {
+ funcHandle = dlopen(PATH_PLUGIN_LIBICU, RTLD_LAZY);
+ if (funcHandle == NULL) {
+ MSAPI_DBG_ERR("dlopen failed");
+ return;
+ }
+
+ icu_ucol_open = dlsym(funcHandle, "ucol_open");
+ icu_ucol_close = dlsym(funcHandle, "ucol_close");
+ icu_ucol_setStrength = dlsym(funcHandle, "ucol_setStrength");
+ icu_uloc_setDefault = dlsym(funcHandle, "uloc_setDefault");
+ icu_uloc_getDefault = dlsym(funcHandle, "uloc_getDefault");
+
+ lang = vconf_get_str(VCONFKEY_LANGSET);
+ /* get current locale */
+ if (lang) {
+ icu_uloc_setDefault(lang, &status);
+ MS_SAFE_FREE(lang);
+ } else {
+ MSAPI_DBG_ERR("Fail to get current language vconf");
+ dlclose(funcHandle);
+ return;
+ }
+ locale = icu_uloc_getDefault();
+ if (locale == NULL) {
+ MSAPI_DBG_ERR("Fail to get current locale");
+ dlclose(funcHandle);
+ return;
+ }
+ MSAPI_DBG_INFO("locale : %s", locale);
+
+ ucol = icu_ucol_open(locale, &status);
+ if (status == U_USING_DEFAULT_WARNING) {
+ MSAPI_DBG("ucol_open success with default collate option");
+ } else if (U_FAILURE(status)) {
+ MSAPI_DBG_ERR("ucol_open fail : %d", status);
+ dlclose(funcHandle);
+ return;
+ }
+
+ icu_ucol_setStrength(ucol, UCOL_SECONDARY); /* NOTICE : MCD sets UCOL_PRIMARY */
+ if (U_FAILURE(status)) {
+ MSAPI_DBG_ERR("ucol_setStrength fail : %d", status);
+ dlclose(funcHandle);
+ return;
+ }
+
+ ret = sqlite3_create_collation_v2(handle, name, SQLITE_UTF8, ucol, __media_db_collate_icu_8, (void *)icu_ucol_close);
+ if (ret != SQLITE_OK) {
+ MSAPI_DBG_ERR("sqlite3_create_collation_v2 fail : %d", ret);
+ icu_ucol_close(ucol);
+ } else {
+ MSAPI_DBG("sqlite3_create_collation_v2 success");
+ }
+
+ dlclose(funcHandle);
+ } else {
+ MSAPI_DBG_ERR("No matching collator for %s", name);
+ }
+}
+
static int __media_db_connect_db_with_handle(sqlite3 **db_handle, uid_t uid, bool need_write)
{
int ret = SQLITE_OK;
/*Connect DB*/
if (need_write)
- ret = db_util_open_with_options(db_path, db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
+ ret = sqlite3_open_v2(db_path, db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
else
- ret = db_util_open_with_options(db_path, db_handle, SQLITE_OPEN_READONLY, NULL);
-
+ ret = sqlite3_open_v2(db_path, db_handle, SQLITE_OPEN_READONLY, NULL);
if (SQLITE_OK != ret) {
MSAPI_DBG_ERR("error when db open path[%s],ret[%d]", db_path, ret);
return MS_MEDIA_ERR_DB_CONNECT_FAIL;
}
+ /* Set for localized collate */
+ sqlite3_collation_needed(*db_handle, NULL, __media_db_collation_cb);
+
return MS_MEDIA_ERR_NONE;
}