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, };
42 int32_t bestPatternCapacity, bestPatternLength;
43 UChar bestPattern[64] = { 0, };
46 /* Pattern Generator */
47 if (search_pattern_generator) {
48 udatpg_close(search_pattern_generator);
49 search_pattern_generator = NULL;
52 uloc_setDefault(getenv("LC_TIME"), &status);
54 locale = uloc_getDefault();
56 search_pattern_generator = udatpg_open(uloc_getDefault(), &status);
58 if (!search_pattern_generator) {
60 ("pattern_generator / udatpg_open fail : %s",
65 /* Date Time Format From Skeletons */
66 enum appcore_time_format timeformat;
68 ret = appcore_get_timeformat(&timeformat);
70 // add exception handling
73 if (timeformat == APPCORE_TIME_FORMAT_24) {
74 snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
76 } else if (timeformat == APPCORE_TIME_FORMAT_12) {
77 snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
80 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
84 u_uastrncpy(customSkeleton, skeleton, strlen(skeleton));
87 (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
89 udatpg_getBestPattern(search_pattern_generator, customSkeleton,
90 u_strlen(customSkeleton), bestPattern,
91 bestPatternCapacity, &status);
92 if (bestPatternLength == 0) {
93 SEARCH_DEBUG_WARNING("udatpg_getBestPattern fail");
98 udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern,
106 void search_util_date_time_format_deinit()
110 if (search_pattern_generator) {
111 udatpg_close(search_pattern_generator);
112 search_pattern_generator = NULL;
115 if (search_formatter) {
116 udat_close(search_formatter);
117 search_formatter = NULL;
125 void search_util_date_time_format_get_val(const struct tm *tm, char *format_val)
131 UChar formatted[64] = { 0, };
132 int32_t formattedCapacity, formattedLength;
133 UErrorCode status = U_ZERO_ERROR;
134 char formattedString[128] = { 0, };
136 time = timelocal((struct tm *)tm);
137 date = (UDate) time *1000;
140 (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
142 udat_format(search_formatter, date, formatted, formattedCapacity,
144 if (formattedLength == -1) {
145 SEARCH_DEBUG_WARNING("udat_format fail");
149 u_austrncpy(formattedString, formatted, 128);
151 snprintf(format_val, MAX_LENGTH_PER_LINE, "%s", formattedString);
156 void search_sql_make_keyword_bind_value(char *src, char *dest, int type)
160 if(type == SEARCH_SQL_BIND_TYPE_DUPLEX) {
165 for (tmp = src; *tmp; ++tmp, ++dest) {
166 if ((*tmp == '%') || (*tmp == '_') || (*tmp == *DB_ESCAPE_CHAR)) {
167 *dest = *DB_ESCAPE_CHAR;
177 void search_get_date_string(char *date_string)
179 struct tm *time_tm = NULL;
181 unsigned long seconds;
182 seconds = atol(date_string);
184 time_tm = gmtime((time_t *) & seconds);
185 sprintf(date_string, "%4d-%02d-%02d %02d:%02d:%02d", time_tm->tm_year
186 + 1900, time_tm->tm_mon + 1, time_tm->tm_mday, time_tm->tm_hour,
187 time_tm->tm_min, time_tm->tm_sec);
191 #if (!CHECK_VALIDATE_UTF8)
192 const char *search_markup_keyword(const char *string, char *searchword,
195 char pstr[DEF_BUF_LEN + 1] = {0,};
196 char result_str[DEF_BUF_LEN + 1] = {0,};
197 char start_str[DEF_BUF_LEN + 1] = {0,};
198 static char return_string[DEF_BUF_LEN + 1] = { 0, };
204 SEARCH_RET_IF_STR_INVALID(string, return_string);
205 SEARCH_RET_IF_STR_INVALID(searchword, return_string);
207 strncpy(pstr, string, DEF_BUF_LEN);
209 word_len = strlen(pstr);
210 search_len = strlen(searchword);
212 for (i = 0; i < word_len; i++) {
213 if (!strncasecmp(searchword, &pstr[i], search_len)) {
223 strncpy(result_str, &pstr[i], search_len);
224 result_str[search_len] = '\0';
225 snprintf(return_string, 128,
226 "<match>%s</match>%s", &result_str[0],
229 strncpy(start_str, &pstr[0], i);
230 start_str[i + 1] = '\0';
231 strncpy(result_str, &pstr[i], search_len);
232 result_str[search_len] = '\0';
233 snprintf(return_string, 128,
234 "%s<match>%s</match>%s", &start_str[0],
235 &result_str[0], &pstr[i + search_len]);
238 snprintf(return_string, 128, "%s", pstr);
241 return return_string;
246 const char *search_markup_keyword(const char *string, char *searchword,
249 char pstr[DEF_BUF_LEN + 1] = {0,};
250 static char return_string[DEF_BUF_LEN + 1] = { 0, };
255 gchar* markup_text_start;
256 gchar* markup_text_end;
259 SEARCH_RET_IF_STR_INVALID(string, return_string);
260 SEARCH_RET_IF_STR_INVALID(searchword, return_string);
262 if(g_utf8_validate(string,-1,NULL)) {
264 strncpy(pstr, string, DEF_BUF_LEN);
266 word_len = strlen(pstr);
267 search_len = strlen(searchword);
269 for (i = 0; i < word_len; i++) {
270 if (!strncasecmp(searchword, &pstr[i], search_len)) {
277 memset(return_string, 0x00, DEF_BUF_LEN);
281 markup_text = g_markup_escape_text(&pstr[0], search_len);
282 markup_text_end = g_markup_escape_text(&pstr[search_len], word_len-search_len);
283 snprintf(return_string,
285 "<match>%s</match>%s",
287 (char*)markup_text_end);
289 g_free(markup_text_end);
291 markup_text_start = g_markup_escape_text(&pstr[0], i);
292 markup_text = g_markup_escape_text(&pstr[i], search_len);
293 markup_text_end = g_markup_escape_text(&pstr[i+search_len], word_len-(i+search_len));
294 snprintf(return_string,
296 "%s<match>%s</match>%s",
297 (char*)markup_text_start,
299 (char*)markup_text_end);
301 g_free(markup_text_start);
302 g_free(markup_text_end);
305 snprintf(return_string, 128, "%s", pstr);
309 return return_string;
313 char *search_get_main_window_name()
318 int count = 0, i, ret;
320 char return_win_name[256] = { 0, };
326 dpy = XOpenDisplay(0);
327 screen = DefaultScreen(dpy);
329 XGetInputFocus(dpy, &focus_win, &revert_to);
332 XGetWMName(dpy, focus_win, &tp);
335 XmbTextPropertyToTextList(dpy, &tp, &list, &count);
336 if ((ret == Success || ret > 0) && list != NULL) {
337 for (i = 0; i < count; i++)
338 strncpy(return_win_name, list[i],
340 XFreeStringList(list);
349 return strdup(return_win_name);