diff options
Diffstat (limited to 'src/common_util.cpp')
-rwxr-xr-x | src/common_util.cpp | 432 |
1 files changed, 432 insertions, 0 deletions
diff --git a/src/common_util.cpp b/src/common_util.cpp new file mode 100755 index 0000000..101b732 --- /dev/null +++ b/src/common_util.cpp @@ -0,0 +1,432 @@ +/* + * Copyright 2012-2013 Samsung Electronics Co., Ltd/ + * + * Licensed under the Flora License, Version 1.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + + + + + +#include <smartsearch.h> +#include <common_util.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> + +#include <unicode/uloc.h> +#include <unicode/udat.h> +#include <unicode/udatpg.h> +#include <unicode/ustring.h> + +static UDateTimePatternGenerator *search_pattern_generator = NULL; +static UDateFormat *search_formatter_yymmdd_12th; +static UDateFormat *search_formatter_yymmdd_24th; + +static UDateFormat *search_formatter_yymmdd; +static UDateFormat *search_formatter_yymm; + +UDateFormat *__search_util_make_date_format(const char *skeleton) +{ + SEARCH_FUNC_START; + + UDateFormat *formatter = NULL; + UChar customSkeleton[SEARCH_MAX_UCHAR_SIZE] = { '\0' }; + int32_t bestPatternCapacity, bestPatternLength; + UChar bestPattern[SEARCH_MAX_UCHAR_SIZE] = { 0, }; + UErrorCode status = U_ZERO_ERROR; + const char *locale = NULL; + + /* Pattern Generator */ + if (search_pattern_generator) { + udatpg_close(search_pattern_generator); + search_pattern_generator = NULL; + } + + uloc_setDefault(getenv("LC_TIME"), &status); + + locale = uloc_getDefault(); + + search_pattern_generator = udatpg_open(uloc_getDefault(), &status); + + if (!search_pattern_generator) { + SEARCH_DEBUG_WARNING + ("pattern_generator / udatpg_open fail : %s", + u_errorName(status)); + return NULL; + } + + SEARCH_DEBUG_LOG("skeleton : %s", skeleton); + + u_uastrncpy(customSkeleton, skeleton, strlen(skeleton)); + + bestPatternCapacity = + (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0])); + bestPatternLength = + udatpg_getBestPattern(search_pattern_generator, customSkeleton, + u_strlen(customSkeleton), bestPattern, + bestPatternCapacity, &status); + + if (bestPatternLength == 0) { + SEARCH_DEBUG_WARNING("udatpg_getBestPattern fail"); + return NULL; + } + + formatter = + udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern, + -1, &status); + + SEARCH_FUNC_END; + + return formatter; + } + + +void search_util_date_time_format_init() +{ + SEARCH_FUNC_START; + + /* Date Time Format From Skeletons */ + enum appcore_time_format timeformat; + int ret; + char *skeleton = NULL; + + ret = appcore_get_timeformat(&timeformat); + if (ret == -1) { + SEARCH_DEBUG_WARNING("Fail to get time format!"); + return; + } + + SEARCH_MALLOC(skeleton, SEARCH_MAX_CHAR_SIZE, char); + snprintf(skeleton, SEARCH_MAX_CHAR_SIZE, "%s%s", UDAT_YEAR_MONTH_DAY, UDAT_HOUR_MINUTE); + search_formatter_yymmdd_12th = __search_util_make_date_format(skeleton); + snprintf(skeleton, SEARCH_MAX_CHAR_SIZE, "%s%s", UDAT_YEAR_MONTH_DAY, UDAT_HOUR24_MINUTE); + search_formatter_yymmdd_24th = __search_util_make_date_format(skeleton); + snprintf(skeleton, SEARCH_MAX_CHAR_SIZE, "%s", UDAT_YEAR_MONTH_DAY); + search_formatter_yymm = __search_util_make_date_format(skeleton); + SEARCH_FREE(skeleton); + + if (timeformat == APPCORE_TIME_FORMAT_24) { + search_formatter_yymmdd = search_formatter_yymmdd_24th; + } else if (timeformat == APPCORE_TIME_FORMAT_12) { + search_formatter_yymmdd = search_formatter_yymmdd_12th; + } else { + SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error"); + return; + } + + SEARCH_FUNC_END; + + return; + } + +void search_region_format_cb(void *data) +{ + SEARCH_FUNC_START; + + enum appcore_time_format timeformat; + int ret; + + ret = appcore_get_timeformat(&timeformat); + + if (ret == -1) { + SEARCH_DEBUG_WARNING("Fail to get time format!"); + return; + } + + if (timeformat == APPCORE_TIME_FORMAT_24) { + search_formatter_yymmdd = search_formatter_yymmdd_24th; + } else if (timeformat == APPCORE_TIME_FORMAT_12) { + search_formatter_yymmdd = search_formatter_yymmdd_12th; + } else { + SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error"); + return; +} + + SEARCH_FUNC_END; +} + +void search_util_date_time_format_deinit() +{ + SEARCH_FUNC_START; + + if (search_pattern_generator) { + udatpg_close(search_pattern_generator); + search_pattern_generator = NULL; + } + + if (search_formatter_yymmdd_12th) { + udat_close(search_formatter_yymmdd_12th); + search_formatter_yymmdd_12th = NULL; + } + + if (search_formatter_yymmdd_24th) { + udat_close(search_formatter_yymmdd_24th); + search_formatter_yymmdd_24th = NULL; + } + + if (search_formatter_yymm) { + udat_close(search_formatter_yymm); + search_formatter_yymm = NULL; + } + + SEARCH_FUNC_END; + + return; +} + +void search_util_date_time_format_get_val(time_t time, char *format_val, int type) +{ + SEARCH_FUNC_START; + + UDate date; + UChar formatted[SEARCH_MAX_UCHAR_SIZE] = { 0, }; + int32_t formattedCapacity, formattedLength; + UErrorCode status = U_ZERO_ERROR; + char formattedString[SEARCH_MAX_CHAR_SIZE] = { 0, }; + + date = (UDate) time *1000; + + formattedCapacity = + (int32_t) (sizeof(formatted) / sizeof((formatted)[0])); + + switch(type) { + case SEARCH_DATE_TYPE_YYMMDD: + if (search_formatter_yymmdd) { + formattedLength = + udat_format(search_formatter_yymmdd, date, formatted, formattedCapacity, + NULL, &status); + if (formattedLength == -1) { + SEARCH_DEBUG_WARNING("udat_format fail"); + return; + } + } + break; + case SEARCH_DATE_TYPE_YYMM: + if (search_formatter_yymm) { + formattedLength = + udat_format(search_formatter_yymm, date, formatted, formattedCapacity, + NULL, &status); + if (formattedLength == -1) { + SEARCH_DEBUG_WARNING("udat_format fail"); + return; + } + } + break; + } + + + u_austrncpy(formattedString, formatted, SEARCH_MAX_CHAR_SIZE); + + SEARCH_DEBUG_LOG("formattedString : %s", formattedString); + + snprintf(format_val, MAX_LENGTH_PER_LINE, "%s", formattedString); + + SEARCH_FUNC_END; +} + +void search_sql_make_keyword_bind_value(char *src, char *dest, int type) +{ + char *tmp; + + if(type == SEARCH_SQL_BIND_TYPE_DUPLEX) { + *dest = '%'; + ++dest; + } + + for (tmp = src; *tmp; ++tmp, ++dest) { + if ((*tmp == '%') || (*tmp == '_') || (*tmp == *DB_ESCAPE_CHAR)) { + *dest = *DB_ESCAPE_CHAR; + ++dest; + } + + *dest = *tmp; + } + + *dest = '%'; +} + +void search_get_date_string(char *date_string) +{ + struct tm *time_tm = NULL; + + unsigned long seconds; + seconds = atol(date_string); + + time_tm = gmtime((time_t *) & seconds); + sprintf(date_string, "%4d-%02d-%02d %02d:%02d:%02d", time_tm->tm_year + + 1900, time_tm->tm_mon + 1, time_tm->tm_mday, time_tm->tm_hour, + time_tm->tm_min, time_tm->tm_sec); +} + + +#if (!CHECK_VALIDATE_UTF8) +const char *search_markup_keyword(const char *string, char *searchword, + bool *result) +{ + char pstr[DEF_BUF_LEN + 1] = {0,}; + char result_str[DEF_BUF_LEN + 1] = {0,}; + char start_str[DEF_BUF_LEN + 1] = {0,}; + static char return_string[DEF_BUF_LEN + 1] = { 0, }; + int word_len = 0; + int search_len = 0; + int i = 0; + bool found = false; + + SEARCH_RET_IF_STR_INVALID(string, return_string); + SEARCH_RET_IF_STR_INVALID(searchword, return_string); + + strncpy(pstr, string, DEF_BUF_LEN); + + word_len = strlen(pstr); + search_len = strlen(searchword); + + for (i = 0; i < word_len; i++) { + if (!strncasecmp(searchword, &pstr[i], search_len)) { + found = true; + break; + } + } + + *result = found; + + if (found) { + if (i == 0) { + strncpy(result_str, &pstr[i], search_len); + result_str[search_len] = '\0'; + snprintf(return_string, 128, + "<match>%s</match>%s", &result_str[0], + &pstr[search_len]); + } else if (i > 0) { + strncpy(start_str, &pstr[0], i); + start_str[i + 1] = '\0'; + strncpy(result_str, &pstr[i], search_len); + result_str[search_len] = '\0'; + snprintf(return_string, 128, + "%s<match>%s</match>%s", &start_str[0], + &result_str[0], &pstr[i + search_len]); + } + } else { + snprintf(return_string, 128, "%s", pstr); + } + + return return_string; +} + + +#else +const char *search_markup_keyword(const char *string, char *searchword, + bool *result) +{ + char pstr[DEF_BUF_LEN + 1] = {0,}; + static char return_string[DEF_BUF_LEN + 1] = { 0, }; + int word_len = 0; + int search_len = 0; + int i = 0; + bool found = false; + gchar* markup_text_start; + gchar* markup_text_end; + gchar* markup_text; + + SEARCH_RET_IF_STR_INVALID(string, return_string); + SEARCH_RET_IF_STR_INVALID(searchword, return_string); + + if(g_utf8_validate(string,-1,NULL)) { + + strncpy(pstr, string, DEF_BUF_LEN); + + word_len = strlen(pstr); + search_len = strlen(searchword); + + for (i = 0; i < word_len; i++) { + if (!strncasecmp(searchword, &pstr[i], search_len)) { + found = true; + break; + } + } + + *result = found; + memset(return_string, 0x00, DEF_BUF_LEN); + + if (found) { + if (i == 0) { + markup_text = g_markup_escape_text(&pstr[0], search_len); + markup_text_end = g_markup_escape_text(&pstr[search_len], word_len-search_len); + snprintf(return_string, + DEF_BUF_LEN, + "<match>%s</match>%s", + markup_text, + (char*)markup_text_end); + g_free(markup_text); + g_free(markup_text_end); + } else { + markup_text_start = g_markup_escape_text(&pstr[0], i); + markup_text = g_markup_escape_text(&pstr[i], search_len); + markup_text_end = g_markup_escape_text(&pstr[i+search_len], word_len-(i+search_len)); + snprintf(return_string, + DEF_BUF_LEN, + "%s<match>%s</match>%s", + (char*)markup_text_start, + markup_text, + (char*)markup_text_end); + g_free(markup_text); + g_free(markup_text_start); + g_free(markup_text_end); + } + } else { + snprintf(return_string, 128, "%s", pstr); + } + } + + return return_string; +} +#endif + +char *search_get_main_window_name() +{ + SEARCH_FUNC_START; + + XTextProperty tp; + int count = 0, i, ret; + char **list = NULL; + char return_win_name[256] = { 0, }; + int revert_to; + Window focus_win; + Display *dpy; + int screen = 0; + + dpy = XOpenDisplay(0); + screen = DefaultScreen(dpy); + + XGetInputFocus(dpy, &focus_win, &revert_to); + + if (focus_win) { + XGetWMName(dpy, focus_win, &tp); + if (tp.nitems > 0) { + ret = + XmbTextPropertyToTextList(dpy, &tp, &list, &count); + if ((ret == Success || ret > 0) && list != NULL) { + for (i = 0; i < count; i++) + strncpy(return_win_name, list[i], + strlen(list[i])); + XFreeStringList(list); + } + } + } else { + return NULL; + } + + SEARCH_FUNC_END; + + return strdup(return_win_name); +} |