2 * Copyright 2012 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://www.tizenopensource.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;
35 int search_util_date_time_format_init(void *data)
39 UErrorCode status = U_ZERO_ERROR;
40 UChar customSkeleton[64] = { '\0' };
41 char skeleton[128] = { 0, };
43 int32_t bestPatternCapacity, bestPatternLength;
44 UChar bestPattern[64] = { 0, };
47 /* Pattern Generator */
48 if (search_pattern_generator) {
49 udatpg_close(search_pattern_generator);
50 search_pattern_generator = NULL;
53 uloc_setDefault(getenv("LC_TIME"), &status);
55 locale = uloc_getDefault();
57 search_pattern_generator = udatpg_open(uloc_getDefault(), &status);
59 if (!search_pattern_generator) {
61 ("pattern_generator / udatpg_open fail : %s",
66 /* Date Time Format From Skeletons */
67 enum appcore_time_format timeformat;
69 ret = appcore_get_timeformat(&timeformat);
71 // add exception handling
74 if (timeformat == APPCORE_TIME_FORMAT_24) {
75 snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
77 } else if (timeformat == APPCORE_TIME_FORMAT_12) {
78 snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
81 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
85 u_uastrncpy(customSkeleton, skeleton, strlen(skeleton));
88 (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
90 udatpg_getBestPattern(search_pattern_generator, customSkeleton,
91 u_strlen(customSkeleton), bestPattern,
92 bestPatternCapacity, &status);
93 if (bestPatternLength == 0) {
94 SEARCH_DEBUG_WARNING("udatpg_getBestPattern fail");
99 udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern,
107 void search_util_date_time_format_deinit()
111 UErrorCode status = U_ZERO_ERROR;
113 if (search_pattern_generator) {
114 udatpg_close(search_pattern_generator);
115 search_pattern_generator = NULL;
118 if (search_formatter) {
119 udat_close(search_formatter);
120 search_formatter = NULL;
128 void search_util_date_time_format_get_val(const struct tm *tm, char *format_val)
134 UChar formatted[64] = { 0, };
135 int32_t formattedCapacity, formattedLength;
136 UErrorCode status = U_ZERO_ERROR;
137 char formattedString[128] = { 0, };
139 time = timelocal((struct tm *)tm);
140 date = (UDate) time *1000;
143 (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
145 udat_format(search_formatter, date, formatted, formattedCapacity,
147 if (formattedLength == -1) {
148 SEARCH_DEBUG_WARNING("udat_format fail");
152 u_austrncpy(formattedString, formatted, 128);
154 snprintf(format_val, MAX_LENGTH_PER_LINE, "%s", formattedString);
159 void search_sql_make_keyword_bind_value(char *src, char *dest, int type)
163 if(type == SEARCH_SQL_BIND_TYPE_DUPLEX) {
168 for (tmp = src; *tmp; ++tmp, ++dest) {
169 if ((*tmp == '%') || (*tmp == '_') || (*tmp == *DB_ESCAPE_CHAR)) {
170 *dest = *DB_ESCAPE_CHAR;
180 void search_get_date_string(char *date_string)
182 struct tm *time_tm = NULL;
184 unsigned long seconds;
185 seconds = atol(date_string);
187 time_tm = gmtime((time_t *) & seconds);
188 sprintf(date_string, "%4d-%02d-%02d %02d:%02d:%02d", time_tm->tm_year
189 + 1900, time_tm->tm_mon + 1, time_tm->tm_mday, time_tm->tm_hour,
190 time_tm->tm_min, time_tm->tm_sec);
194 #if (!CHECK_VALIDATE_UTF8)
195 const char *search_markup_keyword(const char *string, char *searchword,
200 char pstr[DEF_BUF_LEN + 1] = {0,};
201 char result_str[DEF_BUF_LEN + 1] = {0,};
202 char start_str[DEF_BUF_LEN + 1] = {0,};
203 static char return_string[DEF_BUF_LEN + 1] = { 0, };
209 strncpy(pstr, string, DEF_BUF_LEN);
211 word_len = strlen(pstr);
212 search_len = strlen(searchword);
214 for (i = 0; i < word_len; i++) {
215 if (!strncasecmp(searchword, &pstr[i], search_len)) {
225 strncpy(result_str, &pstr[i], search_len);
226 result_str[search_len] = '\0';
227 snprintf(return_string, 128,
228 "<match>%s</match>%s", &result_str[0],
231 strncpy(start_str, &pstr[0], i);
232 start_str[i + 1] = '\0';
233 strncpy(result_str, &pstr[i], search_len);
234 result_str[search_len] = '\0';
235 snprintf(return_string, 128,
236 "%s<match>%s</match>%s", &start_str[0],
237 &result_str[0], &pstr[i + search_len]);
240 snprintf(return_string, 128, "%s", pstr);
245 return return_string;
250 const char *search_markup_keyword(const char *string, char *searchword,
253 char pstr[DEF_BUF_LEN + 1] = {0,};
254 static char return_string[DEF_BUF_LEN + 1] = { 0, };
259 gchar* markup_text_start;
260 gchar* markup_text_end;
263 SEARCH_RET_IF_STR_INVALID(string, return_string);
264 SEARCH_RET_IF_STR_INVALID(searchword, return_string);
266 if(g_utf8_validate(string,-1,NULL)) {
268 strncpy(pstr, string, DEF_BUF_LEN);
270 word_len = strlen(pstr);
271 search_len = strlen(searchword);
273 for (i = 0; i < word_len; i++) {
274 if (!strncasecmp(searchword, &pstr[i], search_len)) {
281 memset(return_string, 0x00, DEF_BUF_LEN);
285 markup_text = g_markup_escape_text(&pstr[0], search_len);
286 markup_text_end = g_markup_escape_text(&pstr[search_len], word_len-search_len);
287 snprintf(return_string,
289 "<match>%s</match>%s",
291 (char*)markup_text_end);
293 g_free(markup_text_end);
295 markup_text_start = g_markup_escape_text(&pstr[0], i);
296 markup_text = g_markup_escape_text(&pstr[i], search_len);
297 markup_text_end = g_markup_escape_text(&pstr[i+search_len], word_len-(i+search_len));
298 snprintf(return_string,
300 "%s<match>%s</match>%s",
301 (char*)markup_text_start,
303 (char*)markup_text_end);
305 g_free(markup_text_start);
306 g_free(markup_text_end);
309 snprintf(return_string, 128, "%s", pstr);
313 return return_string;
317 char *search_get_main_window_name()
322 int count = 0, i, ret;
324 char return_win_name[256] = { 0, };
330 dpy = XOpenDisplay(0);
331 screen = DefaultScreen(dpy);
333 XGetInputFocus(dpy, &focus_win, &revert_to);
336 XGetWMName(dpy, focus_win, &tp);
339 XmbTextPropertyToTextList(dpy, &tp, &list, &count);
340 if ((ret == Success || ret > 0) && list != NULL) {
341 for (i = 0; i < count; i++)
342 strncpy(return_win_name, list[i],
344 XFreeStringList(list);
353 return strdup(return_win_name);