2 * Copyright 2012-2013 Samsung Electronics Co., Ltd/
4 * Licensed under the Flora License, Version 1.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <smartsearch.h>
23 #include <common_util.h>
25 #include <X11/Xutil.h>
27 #include <unicode/uloc.h>
28 #include <unicode/udat.h>
29 #include <unicode/udatpg.h>
30 #include <unicode/ustring.h>
32 static UDateTimePatternGenerator *search_pattern_generator = NULL;
33 static UDateFormat *search_formatter_yymmdd_12th;
34 static UDateFormat *search_formatter_yymmdd_24th;
36 static UDateFormat *search_formatter_yymmdd;
37 static UDateFormat *search_formatter_yymm;
39 UDateFormat *__search_util_make_date_format(const char *skeleton)
43 UDateFormat *formatter = NULL;
44 UChar customSkeleton[SEARCH_MAX_UCHAR_SIZE] = { '\0' };
45 int32_t bestPatternCapacity, bestPatternLength;
46 UChar bestPattern[SEARCH_MAX_UCHAR_SIZE] = { 0, };
47 UErrorCode status = U_ZERO_ERROR;
48 const char *locale = NULL;
50 /* Pattern Generator */
51 if (search_pattern_generator) {
52 udatpg_close(search_pattern_generator);
53 search_pattern_generator = NULL;
56 uloc_setDefault(getenv("LC_TIME"), &status);
58 locale = uloc_getDefault();
60 search_pattern_generator = udatpg_open(uloc_getDefault(), &status);
62 if (!search_pattern_generator) {
64 ("pattern_generator / udatpg_open fail : %s",
69 SEARCH_DEBUG_LOG("skeleton : %s", skeleton);
71 u_uastrncpy(customSkeleton, skeleton, strlen(skeleton));
74 (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
76 udatpg_getBestPattern(search_pattern_generator, customSkeleton,
77 u_strlen(customSkeleton), bestPattern,
78 bestPatternCapacity, &status);
80 if (bestPatternLength == 0) {
81 SEARCH_DEBUG_WARNING("udatpg_getBestPattern fail");
86 udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern,
95 void search_util_date_time_format_init()
99 /* Date Time Format From Skeletons */
100 enum appcore_time_format timeformat;
102 char *skeleton = NULL;
104 ret = appcore_get_timeformat(&timeformat);
106 SEARCH_DEBUG_WARNING("Fail to get time format!");
110 SEARCH_MALLOC(skeleton, SEARCH_MAX_CHAR_SIZE, char);
111 snprintf(skeleton, SEARCH_MAX_CHAR_SIZE, "%s%s", UDAT_YEAR_MONTH_DAY, UDAT_HOUR_MINUTE);
112 search_formatter_yymmdd_12th = __search_util_make_date_format(skeleton);
113 snprintf(skeleton, SEARCH_MAX_CHAR_SIZE, "%s%s", UDAT_YEAR_MONTH_DAY, UDAT_HOUR24_MINUTE);
114 search_formatter_yymmdd_24th = __search_util_make_date_format(skeleton);
115 snprintf(skeleton, SEARCH_MAX_CHAR_SIZE, "%s", UDAT_YEAR_MONTH_DAY);
116 search_formatter_yymm = __search_util_make_date_format(skeleton);
117 SEARCH_FREE(skeleton);
119 if (timeformat == APPCORE_TIME_FORMAT_24) {
120 search_formatter_yymmdd = search_formatter_yymmdd_24th;
121 } else if (timeformat == APPCORE_TIME_FORMAT_12) {
122 search_formatter_yymmdd = search_formatter_yymmdd_12th;
124 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
133 void search_region_format_cb(void *data)
137 enum appcore_time_format timeformat;
140 ret = appcore_get_timeformat(&timeformat);
143 SEARCH_DEBUG_WARNING("Fail to get time format!");
147 if (timeformat == APPCORE_TIME_FORMAT_24) {
148 search_formatter_yymmdd = search_formatter_yymmdd_24th;
149 } else if (timeformat == APPCORE_TIME_FORMAT_12) {
150 search_formatter_yymmdd = search_formatter_yymmdd_12th;
152 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
159 void search_util_date_time_format_deinit()
163 if (search_pattern_generator) {
164 udatpg_close(search_pattern_generator);
165 search_pattern_generator = NULL;
168 if (search_formatter_yymmdd_12th) {
169 udat_close(search_formatter_yymmdd_12th);
170 search_formatter_yymmdd_12th = NULL;
173 if (search_formatter_yymmdd_24th) {
174 udat_close(search_formatter_yymmdd_24th);
175 search_formatter_yymmdd_24th = NULL;
178 if (search_formatter_yymm) {
179 udat_close(search_formatter_yymm);
180 search_formatter_yymm = NULL;
188 void search_util_date_time_format_get_val(time_t time, char *format_val, int type)
193 UChar formatted[SEARCH_MAX_UCHAR_SIZE] = { 0, };
194 int32_t formattedCapacity, formattedLength;
195 UErrorCode status = U_ZERO_ERROR;
196 char formattedString[SEARCH_MAX_CHAR_SIZE] = { 0, };
198 date = (UDate) time *1000;
201 (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
204 case SEARCH_DATE_TYPE_YYMMDD:
205 if (search_formatter_yymmdd) {
207 udat_format(search_formatter_yymmdd, date, formatted, formattedCapacity,
209 if (formattedLength == -1) {
210 SEARCH_DEBUG_WARNING("udat_format fail");
215 case SEARCH_DATE_TYPE_YYMM:
216 if (search_formatter_yymm) {
218 udat_format(search_formatter_yymm, date, formatted, formattedCapacity,
220 if (formattedLength == -1) {
221 SEARCH_DEBUG_WARNING("udat_format fail");
229 u_austrncpy(formattedString, formatted, SEARCH_MAX_CHAR_SIZE);
231 SEARCH_DEBUG_LOG("formattedString : %s", formattedString);
233 snprintf(format_val, MAX_LENGTH_PER_LINE, "%s", formattedString);
238 void search_sql_make_keyword_bind_value(char *src, char *dest, int type)
242 if(type == SEARCH_SQL_BIND_TYPE_DUPLEX) {
247 for (tmp = src; *tmp; ++tmp, ++dest) {
248 if ((*tmp == '%') || (*tmp == '_') || (*tmp == *DB_ESCAPE_CHAR)) {
249 *dest = *DB_ESCAPE_CHAR;
259 void search_get_date_string(char *date_string)
261 struct tm *time_tm = NULL;
263 unsigned long seconds;
264 seconds = atol(date_string);
266 time_tm = gmtime((time_t *) & seconds);
267 sprintf(date_string, "%4d-%02d-%02d %02d:%02d:%02d", time_tm->tm_year
268 + 1900, time_tm->tm_mon + 1, time_tm->tm_mday, time_tm->tm_hour,
269 time_tm->tm_min, time_tm->tm_sec);
273 #if (!CHECK_VALIDATE_UTF8)
274 const char *search_markup_keyword(const char *string, char *searchword,
277 char pstr[DEF_BUF_LEN + 1] = {0,};
278 char result_str[DEF_BUF_LEN + 1] = {0,};
279 char start_str[DEF_BUF_LEN + 1] = {0,};
280 static char return_string[DEF_BUF_LEN + 1] = { 0, };
286 SEARCH_RET_IF_STR_INVALID(string, return_string);
287 SEARCH_RET_IF_STR_INVALID(searchword, return_string);
289 strncpy(pstr, string, DEF_BUF_LEN);
291 word_len = strlen(pstr);
292 search_len = strlen(searchword);
294 for (i = 0; i < word_len; i++) {
295 if (!strncasecmp(searchword, &pstr[i], search_len)) {
305 strncpy(result_str, &pstr[i], search_len);
306 result_str[search_len] = '\0';
307 snprintf(return_string, 128,
308 "<match>%s</match>%s", &result_str[0],
311 strncpy(start_str, &pstr[0], i);
312 start_str[i + 1] = '\0';
313 strncpy(result_str, &pstr[i], search_len);
314 result_str[search_len] = '\0';
315 snprintf(return_string, 128,
316 "%s<match>%s</match>%s", &start_str[0],
317 &result_str[0], &pstr[i + search_len]);
320 snprintf(return_string, 128, "%s", pstr);
323 return return_string;
328 const char *search_markup_keyword(const char *string, char *searchword,
331 char pstr[DEF_BUF_LEN + 1] = {0,};
332 static char return_string[DEF_BUF_LEN + 1] = { 0, };
337 gchar* markup_text_start;
338 gchar* markup_text_end;
341 SEARCH_RET_IF_STR_INVALID(string, return_string);
342 SEARCH_RET_IF_STR_INVALID(searchword, return_string);
344 if(g_utf8_validate(string,-1,NULL)) {
346 strncpy(pstr, string, DEF_BUF_LEN);
348 word_len = strlen(pstr);
349 search_len = strlen(searchword);
351 for (i = 0; i < word_len; i++) {
352 if (!strncasecmp(searchword, &pstr[i], search_len)) {
359 memset(return_string, 0x00, DEF_BUF_LEN);
363 markup_text = g_markup_escape_text(&pstr[0], search_len);
364 markup_text_end = g_markup_escape_text(&pstr[search_len], word_len-search_len);
365 snprintf(return_string,
367 "<match>%s</match>%s",
369 (char*)markup_text_end);
371 g_free(markup_text_end);
373 markup_text_start = g_markup_escape_text(&pstr[0], i);
374 markup_text = g_markup_escape_text(&pstr[i], search_len);
375 markup_text_end = g_markup_escape_text(&pstr[i+search_len], word_len-(i+search_len));
376 snprintf(return_string,
378 "%s<match>%s</match>%s",
379 (char*)markup_text_start,
381 (char*)markup_text_end);
383 g_free(markup_text_start);
384 g_free(markup_text_end);
387 snprintf(return_string, 128, "%s", pstr);
391 return return_string;
395 char *search_get_main_window_name()
400 int count = 0, i, ret;
402 char return_win_name[256] = { 0, };
408 dpy = XOpenDisplay(0);
409 screen = DefaultScreen(dpy);
411 XGetInputFocus(dpy, &focus_win, &revert_to);
414 XGetWMName(dpy, focus_win, &tp);
417 XmbTextPropertyToTextList(dpy, &tp, &list, &count);
418 if ((ret == Success || ret > 0) && list != NULL) {
419 for (i = 0; i < count; i++)
420 strncpy(return_win_name, list[i],
422 XFreeStringList(list);
431 return strdup(return_win_name);