summaryrefslogtreecommitdiff
path: root/src/common_util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common_util.cpp')
-rwxr-xr-xsrc/common_util.cpp354
1 files changed, 354 insertions, 0 deletions
diff --git a/src/common_util.cpp b/src/common_util.cpp
new file mode 100755
index 0000000..02b4f51
--- /dev/null
+++ b/src/common_util.cpp
@@ -0,0 +1,354 @@
+/*
+ * Copyright 2012 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://www.tizenopensource.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;
+
+int search_util_date_time_format_init(void *data)
+{
+ SEARCH_FUNC_START;
+
+ UErrorCode status = U_ZERO_ERROR;
+ UChar customSkeleton[64] = { '\0' };
+ char skeleton[128] = { 0, };
+ int hr = 0;
+ int32_t bestPatternCapacity, bestPatternLength;
+ UChar bestPattern[64] = { 0, };
+ const char *locale;
+
+ /* 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 -1;
+ }
+
+ /* Date Time Format From Skeletons */
+ enum appcore_time_format timeformat;
+ int ret;
+ ret = appcore_get_timeformat(&timeformat);
+ if (ret == -1) {
+ // add exception handling
+ }
+
+ if (timeformat == APPCORE_TIME_FORMAT_24) {
+ snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
+ UDAT_HOUR24_MINUTE);
+ } else if (timeformat == APPCORE_TIME_FORMAT_12) {
+ snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
+ UDAT_HOUR_MINUTE);
+ } else {
+ SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
+ return -1;
+ }
+
+ 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 -1;
+ }
+
+ search_formatter =
+ udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern,
+ -1, &status);
+
+ SEARCH_FUNC_END;
+
+ return 0;
+}
+
+void search_util_date_time_format_deinit()
+{
+ SEARCH_FUNC_START;
+
+ UErrorCode status = U_ZERO_ERROR;
+
+ if (search_pattern_generator) {
+ udatpg_close(search_pattern_generator);
+ search_pattern_generator = NULL;
+ }
+
+ if (search_formatter) {
+ udat_close(search_formatter);
+ search_formatter = NULL;
+ }
+
+ SEARCH_FUNC_END;
+
+ return;
+}
+
+void search_util_date_time_format_get_val(const struct tm *tm, char *format_val)
+{
+ SEARCH_FUNC_START;
+
+ UDate date;
+ time_t time;
+ UChar formatted[64] = { 0, };
+ int32_t formattedCapacity, formattedLength;
+ UErrorCode status = U_ZERO_ERROR;
+ char formattedString[128] = { 0, };
+
+ time = timelocal((struct tm *)tm);
+ date = (UDate) time *1000;
+
+ formattedCapacity =
+ (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
+ formattedLength =
+ udat_format(search_formatter, date, formatted, formattedCapacity,
+ NULL, &status);
+ if (formattedLength == -1) {
+ SEARCH_DEBUG_WARNING("udat_format fail");
+ return;
+ }
+
+ u_austrncpy(formattedString, formatted, 128);
+
+ 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)
+{
+ SEARCH_FUNC_START;
+
+ 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;
+
+ 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);
+ }
+
+ SEARCH_FUNC_END;
+
+ 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);
+}