2 * Copyright 2012-2013 Samsung Electronics Co., Ltd/
4 * Licensed under the Flora License, Version 1.1 (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.
23 #include <smartsearch.h>
24 #include <common_util.h>
26 #include <X11/Xutil.h>
28 #include <unicode/uloc.h>
29 #include <unicode/udat.h>
30 #include <unicode/udatpg.h>
31 #include <unicode/ustring.h>
33 static UDateTimePatternGenerator *search_pattern_generator = NULL;
34 static UDateFormat *search_formatter_yymmdd_12th;
35 static UDateFormat *search_formatter_yymmdd_24th;
37 static UDateFormat *search_formatter_yymmdd;
38 static UDateFormat *search_formatter_yymm;
40 UDateFormat *__search_util_make_date_format(const char *skeleton)
44 UDateFormat *formatter = NULL;
45 UChar customSkeleton[SEARCH_MAX_UCHAR_SIZE] = { '\0' };
46 int32_t bestPatternCapacity, bestPatternLength;
47 UChar bestPattern[SEARCH_MAX_UCHAR_SIZE] = { 0, };
48 UErrorCode status = U_ZERO_ERROR;
49 const char *locale = NULL;
51 /* Pattern Generator */
52 if (search_pattern_generator) {
53 udatpg_close(search_pattern_generator);
54 search_pattern_generator = NULL;
57 uloc_setDefault(getenv("LC_TIME"), &status);
59 locale = uloc_getDefault();
61 search_pattern_generator = udatpg_open(uloc_getDefault(), &status);
63 if (!search_pattern_generator) {
65 ("pattern_generator / udatpg_open fail : %s",
70 SEARCH_DEBUG_LOG("skeleton : %s", skeleton);
72 u_uastrncpy(customSkeleton, skeleton, strlen(skeleton));
75 (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
77 udatpg_getBestPattern(search_pattern_generator, customSkeleton,
78 u_strlen(customSkeleton), bestPattern,
79 bestPatternCapacity, &status);
81 if (bestPatternLength == 0) {
82 SEARCH_DEBUG_WARNING("udatpg_getBestPattern fail");
87 udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern,
96 void search_util_date_time_format_init()
100 /* Date Time Format From Skeletons */
101 enum appcore_time_format timeformat;
103 char *skeleton = NULL;
105 ret = appcore_get_timeformat(&timeformat);
107 SEARCH_DEBUG_WARNING("Fail to get time format!");
111 SEARCH_MALLOC(skeleton, SEARCH_MAX_CHAR_SIZE, char);
112 snprintf(skeleton, SEARCH_MAX_CHAR_SIZE, "%s%s", UDAT_YEAR_MONTH_DAY, UDAT_HOUR_MINUTE);
113 search_formatter_yymmdd_12th = __search_util_make_date_format(skeleton);
114 snprintf(skeleton, SEARCH_MAX_CHAR_SIZE, "%s%s", UDAT_YEAR_MONTH_DAY, UDAT_HOUR24_MINUTE);
115 search_formatter_yymmdd_24th = __search_util_make_date_format(skeleton);
116 snprintf(skeleton, SEARCH_MAX_CHAR_SIZE, "%s", UDAT_YEAR_MONTH_DAY);
117 search_formatter_yymm = __search_util_make_date_format(skeleton);
118 SEARCH_FREE(skeleton);
120 if (timeformat == APPCORE_TIME_FORMAT_24) {
121 search_formatter_yymmdd = search_formatter_yymmdd_24th;
122 } else if (timeformat == APPCORE_TIME_FORMAT_12) {
123 search_formatter_yymmdd = search_formatter_yymmdd_12th;
125 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
134 void search_region_format_cb(void *data)
138 enum appcore_time_format timeformat;
141 ret = appcore_get_timeformat(&timeformat);
144 SEARCH_DEBUG_WARNING("Fail to get time format!");
148 if (timeformat == APPCORE_TIME_FORMAT_24) {
149 search_formatter_yymmdd = search_formatter_yymmdd_24th;
150 } else if (timeformat == APPCORE_TIME_FORMAT_12) {
151 search_formatter_yymmdd = search_formatter_yymmdd_12th;
153 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
160 void search_util_date_time_format_deinit()
164 if (search_pattern_generator) {
165 udatpg_close(search_pattern_generator);
166 search_pattern_generator = NULL;
169 if (search_formatter_yymmdd_12th) {
170 udat_close(search_formatter_yymmdd_12th);
171 search_formatter_yymmdd_12th = NULL;
174 if (search_formatter_yymmdd_24th) {
175 udat_close(search_formatter_yymmdd_24th);
176 search_formatter_yymmdd_24th = NULL;
179 if (search_formatter_yymm) {
180 udat_close(search_formatter_yymm);
181 search_formatter_yymm = NULL;
189 void search_util_date_time_format_get_val(time_t time, char *format_val, int type)
194 UChar formatted[SEARCH_MAX_UCHAR_SIZE] = { 0, };
195 int32_t formattedCapacity, formattedLength;
196 UErrorCode status = U_ZERO_ERROR;
197 char formattedString[SEARCH_MAX_CHAR_SIZE] = { 0, };
199 date = (UDate) time *1000;
202 (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
205 case SEARCH_DATE_TYPE_YYMMDD:
206 if (search_formatter_yymmdd) {
208 udat_format(search_formatter_yymmdd, date, formatted, formattedCapacity,
210 if (formattedLength == -1) {
211 SEARCH_DEBUG_WARNING("udat_format fail");
216 case SEARCH_DATE_TYPE_YYMM:
217 if (search_formatter_yymm) {
219 udat_format(search_formatter_yymm, date, formatted, formattedCapacity,
221 if (formattedLength == -1) {
222 SEARCH_DEBUG_WARNING("udat_format fail");
230 u_austrncpy(formattedString, formatted, SEARCH_MAX_CHAR_SIZE);
232 SEARCH_DEBUG_LOG("formattedString : %s", formattedString);
234 snprintf(format_val, MAX_LENGTH_PER_LINE, "%s", formattedString);
239 void search_sql_make_keyword_bind_value(char *src, char *dest, int type)
243 if(type == SEARCH_SQL_BIND_TYPE_DUPLEX) {
248 for (tmp = src; *tmp; ++tmp, ++dest) {
249 if ((*tmp == '%') || (*tmp == '_') || (*tmp == *DB_ESCAPE_CHAR)) {
250 *dest = *DB_ESCAPE_CHAR;
260 void search_get_date_string(char *date_string)
262 struct tm *time_tm = NULL;
264 unsigned long seconds;
265 seconds = atol(date_string);
267 time_tm = gmtime((time_t *) & seconds);
268 sprintf(date_string, "%4d-%02d-%02d %02d:%02d:%02d", time_tm->tm_year
269 + 1900, time_tm->tm_mon + 1, time_tm->tm_mday, time_tm->tm_hour,
270 time_tm->tm_min, time_tm->tm_sec);
274 #if (!CHECK_VALIDATE_UTF8)
275 const char *search_markup_keyword(const char *string, char *searchword,
278 char pstr[DEF_BUF_LEN + 1] = {0,};
279 char result_str[DEF_BUF_LEN + 1] = {0,};
280 char start_str[DEF_BUF_LEN + 1] = {0,};
281 static char return_string[DEF_BUF_LEN + 1] = { 0, };
287 SEARCH_RET_IF_STR_INVALID(string, return_string);
288 SEARCH_RET_IF_STR_INVALID(searchword, return_string);
290 strncpy(pstr, string, DEF_BUF_LEN);
292 word_len = strlen(pstr);
293 search_len = strlen(searchword);
295 for (i = 0; i < word_len; i++) {
296 if (!strncasecmp(searchword, &pstr[i], search_len)) {
306 strncpy(result_str, &pstr[i], search_len);
307 result_str[search_len] = '\0';
308 snprintf(return_string, 128,
309 "<match>%s</match>%s", &result_str[0],
312 strncpy(start_str, &pstr[0], i);
313 start_str[i + 1] = '\0';
314 strncpy(result_str, &pstr[i], search_len);
315 result_str[search_len] = '\0';
316 snprintf(return_string, 128,
317 "%s<match>%s</match>%s", &start_str[0],
318 &result_str[0], &pstr[i + search_len]);
321 snprintf(return_string, 128, "%s", pstr);
324 return return_string;
329 const char *search_markup_keyword(const char *string, char *searchword,
332 char pstr[DEF_BUF_LEN + 1] = {0,};
333 static char return_string[DEF_BUF_LEN + 1] = { 0, };
338 gchar* markup_text_start;
339 gchar* markup_text_end;
342 SEARCH_RET_IF_STR_INVALID(string, return_string);
343 SEARCH_RET_IF_STR_INVALID(searchword, return_string);
345 if(g_utf8_validate(string,-1,NULL)) {
347 strncpy(pstr, string, DEF_BUF_LEN);
349 word_len = strlen(pstr);
350 search_len = strlen(searchword);
352 for (i = 0; i < word_len; i++) {
353 if (!strncasecmp(searchword, &pstr[i], search_len)) {
360 memset(return_string, 0x00, DEF_BUF_LEN);
364 markup_text = g_markup_escape_text(&pstr[0], search_len);
365 markup_text_end = g_markup_escape_text(&pstr[search_len], word_len-search_len);
366 snprintf(return_string,
368 "<match>%s</match>%s",
370 (char*)markup_text_end);
372 g_free(markup_text_end);
374 markup_text_start = g_markup_escape_text(&pstr[0], i);
375 markup_text = g_markup_escape_text(&pstr[i], search_len);
376 markup_text_end = g_markup_escape_text(&pstr[i+search_len], word_len-(i+search_len));
377 snprintf(return_string,
379 "%s<match>%s</match>%s",
380 (char*)markup_text_start,
382 (char*)markup_text_end);
384 g_free(markup_text_start);
385 g_free(markup_text_end);
388 snprintf(return_string, 128, "%s", pstr);
392 return return_string;
396 char *search_get_main_window_name()
401 int count = 0, i, ret;
403 char return_win_name[256] = { 0, };
409 dpy = XOpenDisplay(0);
410 screen = DefaultScreen(dpy);
412 XGetInputFocus(dpy, &focus_win, &revert_to);
415 XGetWMName(dpy, focus_win, &tp);
418 XmbTextPropertyToTextList(dpy, &tp, &list, &count);
419 if ((ret == Success || ret > 0) && list != NULL) {
420 for (i = 0; i < count; i++)
421 strncpy(return_win_name, list[i],
423 XFreeStringList(list);
432 return strdup(return_win_name);