Update changed code
[apps/home/smartsearch.git] / src / common_util.cpp
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *     http://www.tizenopensource.org/license
9  *
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.
15  */
16
17
18
19
20
21
22 #include <smartsearch.h>
23 #include <common_util.h>
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26
27 #include <unicode/uloc.h>
28 #include <unicode/udat.h>
29 #include <unicode/udatpg.h>
30 #include <unicode/ustring.h>
31
32 static UDateTimePatternGenerator *search_pattern_generator = NULL;
33 static UDateFormat *search_formatter;
34
35 int search_util_date_time_format_init(void *data)
36 {
37         SEARCH_FUNC_START;
38
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, };
44         const char *locale;
45
46         /* Pattern Generator */
47         if (search_pattern_generator) {
48                 udatpg_close(search_pattern_generator);
49                 search_pattern_generator = NULL;
50         }
51
52         uloc_setDefault(getenv("LC_TIME"), &status);
53
54         locale = uloc_getDefault();
55
56         search_pattern_generator = udatpg_open(uloc_getDefault(), &status);
57
58         if (!search_pattern_generator) {
59                 SEARCH_DEBUG_WARNING
60                     ("pattern_generator / udatpg_open fail : %s",
61                      u_errorName(status));
62                 return -1;
63         }
64
65         /* Date Time Format From Skeletons */
66         enum appcore_time_format timeformat;
67         int ret;
68         ret = appcore_get_timeformat(&timeformat);
69         if (ret == -1) {
70                 // add exception handling
71         }
72
73         if (timeformat == APPCORE_TIME_FORMAT_24) {
74                 snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
75                          UDAT_HOUR24_MINUTE);
76         } else if (timeformat == APPCORE_TIME_FORMAT_12) {
77                 snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
78                          UDAT_HOUR_MINUTE);
79         } else {
80                 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
81                 return -1;
82         }
83
84         u_uastrncpy(customSkeleton, skeleton, strlen(skeleton));
85
86         bestPatternCapacity =
87             (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
88         bestPatternLength =
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");
94                 return -1;
95         }
96
97         search_formatter =
98             udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern,
99                       -1, &status);
100
101         SEARCH_FUNC_END;
102
103         return 0;
104 }
105
106 void search_util_date_time_format_deinit()
107 {
108         SEARCH_FUNC_START;
109
110         if (search_pattern_generator) {
111                 udatpg_close(search_pattern_generator);
112                 search_pattern_generator = NULL;
113         }
114
115         if (search_formatter) {
116                 udat_close(search_formatter);
117                 search_formatter = NULL;
118         }
119
120         SEARCH_FUNC_END;
121
122         return;
123 }
124
125 void search_util_date_time_format_get_val(const struct tm *tm, char *format_val)
126 {
127         SEARCH_FUNC_START;
128
129         UDate date;
130         time_t time;
131         UChar formatted[64] = { 0, };
132         int32_t formattedCapacity, formattedLength;
133         UErrorCode status = U_ZERO_ERROR;
134         char formattedString[128] = { 0, };
135
136         time = timelocal((struct tm *)tm);
137         date = (UDate) time *1000;
138
139         formattedCapacity =
140             (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
141         formattedLength =
142             udat_format(search_formatter, date, formatted, formattedCapacity,
143                         NULL, &status);
144         if (formattedLength == -1) {
145                 SEARCH_DEBUG_WARNING("udat_format fail");
146                 return;
147         }
148
149         u_austrncpy(formattedString, formatted, 128);
150
151         snprintf(format_val, MAX_LENGTH_PER_LINE, "%s", formattedString);
152
153         SEARCH_FUNC_END;
154 }
155
156 void search_sql_make_keyword_bind_value(char *src, char *dest, int type)
157 {
158         char *tmp;
159
160         if(type == SEARCH_SQL_BIND_TYPE_DUPLEX) {
161                 *dest = '%';
162                 ++dest;
163         }
164
165         for (tmp = src; *tmp; ++tmp, ++dest) {
166                 if ((*tmp == '%') || (*tmp == '_') || (*tmp == *DB_ESCAPE_CHAR)) {
167                         *dest = *DB_ESCAPE_CHAR;
168                         ++dest;
169                 }
170
171                 *dest = *tmp;
172         }
173
174         *dest = '%';
175 }
176
177 void search_get_date_string(char *date_string)
178 {
179         struct tm *time_tm = NULL;
180
181         unsigned long seconds;
182         seconds = atol(date_string);
183
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);
188 }
189
190
191 #if (!CHECK_VALIDATE_UTF8)
192 const char *search_markup_keyword(const char *string, char *searchword,
193                                  bool *result)
194 {
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, };
199         int word_len = 0;
200         int search_len = 0;
201         int i = 0;
202         bool found = false;
203
204         SEARCH_RET_IF_STR_INVALID(string, return_string);
205         SEARCH_RET_IF_STR_INVALID(searchword, return_string);
206
207         strncpy(pstr, string, DEF_BUF_LEN);
208
209         word_len = strlen(pstr);
210         search_len = strlen(searchword);
211
212         for (i = 0; i < word_len; i++) {
213                 if (!strncasecmp(searchword, &pstr[i], search_len)) {
214                         found = true;
215                         break;
216                 }
217         }
218
219         *result = found;
220
221         if (found) {
222                 if (i == 0) {
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],
227                                         &pstr[search_len]);
228                 } else if (i > 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]);
236                 }
237         } else {
238                 snprintf(return_string, 128, "%s", pstr);
239         }
240
241         return return_string;
242 }
243
244
245 #else
246 const char *search_markup_keyword(const char *string, char *searchword,
247                                  bool *result)
248 {
249         char pstr[DEF_BUF_LEN + 1] = {0,};
250         static char return_string[DEF_BUF_LEN + 1] = { 0, };
251         int word_len = 0;
252         int search_len = 0;
253         int i = 0;
254         bool found = false;
255         gchar* markup_text_start;
256         gchar* markup_text_end;
257         gchar* markup_text;
258
259         SEARCH_RET_IF_STR_INVALID(string, return_string);
260         SEARCH_RET_IF_STR_INVALID(searchword, return_string);
261
262         if(g_utf8_validate(string,-1,NULL)) {
263
264                 strncpy(pstr, string, DEF_BUF_LEN);
265
266                 word_len = strlen(pstr);
267                 search_len = strlen(searchword);
268
269                 for (i = 0; i < word_len; i++) {
270                         if (!strncasecmp(searchword, &pstr[i], search_len)) {
271                                 found = true;
272                                 break;
273                         }
274                 }
275
276                 *result = found;
277                 memset(return_string, 0x00, DEF_BUF_LEN);
278
279                 if (found) {
280                         if (i == 0) {
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,
284                                                         DEF_BUF_LEN,
285                                                         "<match>%s</match>%s",
286                                                         markup_text,
287                                                         (char*)markup_text_end);
288                                 g_free(markup_text);
289                                 g_free(markup_text_end);
290                         } else {
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,
295                                                         DEF_BUF_LEN,
296                                                         "%s<match>%s</match>%s",
297                                                         (char*)markup_text_start,
298                                                         markup_text,
299                                                         (char*)markup_text_end);
300                                 g_free(markup_text);
301                                 g_free(markup_text_start);
302                                 g_free(markup_text_end);
303                         }
304                 } else {
305                         snprintf(return_string, 128, "%s", pstr);
306                 }
307         }
308
309         return return_string;
310 }
311 #endif
312
313 char *search_get_main_window_name()
314 {
315         SEARCH_FUNC_START;
316
317         XTextProperty tp;
318         int count = 0, i, ret;
319         char **list = NULL;
320         char return_win_name[256] = { 0, };
321         int revert_to;
322         Window focus_win;
323         Display *dpy;
324         int screen = 0;
325
326         dpy = XOpenDisplay(0);
327         screen = DefaultScreen(dpy);
328
329         XGetInputFocus(dpy, &focus_win, &revert_to);
330
331         if (focus_win) {
332                 XGetWMName(dpy, focus_win, &tp);
333                 if (tp.nitems > 0) {
334                         ret =
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],
339                                                 strlen(list[i]));
340                                 XFreeStringList(list);
341                         }
342                 }
343         } else {
344                 return NULL;
345         }
346
347         SEARCH_FUNC_END;
348
349         return strdup(return_win_name);
350 }