Updating license in spec file
[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         int hr = 0;
43         int32_t bestPatternCapacity, bestPatternLength;
44         UChar bestPattern[64] = { 0, };
45         const char *locale;
46
47         /* Pattern Generator */
48         if (search_pattern_generator) {
49                 udatpg_close(search_pattern_generator);
50                 search_pattern_generator = NULL;
51         }
52
53         uloc_setDefault(getenv("LC_TIME"), &status);
54
55         locale = uloc_getDefault();
56
57         search_pattern_generator = udatpg_open(uloc_getDefault(), &status);
58
59         if (!search_pattern_generator) {
60                 SEARCH_DEBUG_WARNING
61                     ("pattern_generator / udatpg_open fail : %s",
62                      u_errorName(status));
63                 return -1;
64         }
65
66         /* Date Time Format From Skeletons */
67         enum appcore_time_format timeformat;
68         int ret;
69         ret = appcore_get_timeformat(&timeformat);
70         if (ret == -1) {
71                 // add exception handling
72         }
73
74         if (timeformat == APPCORE_TIME_FORMAT_24) {
75                 snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
76                          UDAT_HOUR24_MINUTE);
77         } else if (timeformat == APPCORE_TIME_FORMAT_12) {
78                 snprintf(skeleton, 128, "%s%s", UDAT_YEAR_MONTH_DAY,
79                          UDAT_HOUR_MINUTE);
80         } else {
81                 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
82                 return -1;
83         }
84
85         u_uastrncpy(customSkeleton, skeleton, strlen(skeleton));
86
87         bestPatternCapacity =
88             (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
89         bestPatternLength =
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");
95                 return -1;
96         }
97
98         search_formatter =
99             udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern,
100                       -1, &status);
101
102         SEARCH_FUNC_END;
103
104         return 0;
105 }
106
107 void search_util_date_time_format_deinit()
108 {
109         SEARCH_FUNC_START;
110
111         UErrorCode status = U_ZERO_ERROR;
112
113         if (search_pattern_generator) {
114                 udatpg_close(search_pattern_generator);
115                 search_pattern_generator = NULL;
116         }
117
118         if (search_formatter) {
119                 udat_close(search_formatter);
120                 search_formatter = NULL;
121         }
122
123         SEARCH_FUNC_END;
124
125         return;
126 }
127
128 void search_util_date_time_format_get_val(const struct tm *tm, char *format_val)
129 {
130         SEARCH_FUNC_START;
131
132         UDate date;
133         time_t time;
134         UChar formatted[64] = { 0, };
135         int32_t formattedCapacity, formattedLength;
136         UErrorCode status = U_ZERO_ERROR;
137         char formattedString[128] = { 0, };
138
139         time = timelocal((struct tm *)tm);
140         date = (UDate) time *1000;
141
142         formattedCapacity =
143             (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
144         formattedLength =
145             udat_format(search_formatter, date, formatted, formattedCapacity,
146                         NULL, &status);
147         if (formattedLength == -1) {
148                 SEARCH_DEBUG_WARNING("udat_format fail");
149                 return;
150         }
151
152         u_austrncpy(formattedString, formatted, 128);
153
154         snprintf(format_val, MAX_LENGTH_PER_LINE, "%s", formattedString);
155
156         SEARCH_FUNC_END;
157 }
158
159 void search_sql_make_keyword_bind_value(char *src, char *dest, int type)
160 {
161         char *tmp;
162
163         if(type == SEARCH_SQL_BIND_TYPE_DUPLEX) {
164                 *dest = '%';
165                 ++dest;
166         }       
167
168         for (tmp = src; *tmp; ++tmp, ++dest) {
169                 if ((*tmp == '%') || (*tmp == '_') || (*tmp == *DB_ESCAPE_CHAR)) {
170                         *dest = *DB_ESCAPE_CHAR;
171                         ++dest;
172                 }
173
174                 *dest = *tmp;
175         }
176
177         *dest = '%';
178 }
179
180 void search_get_date_string(char *date_string)
181 {
182         struct tm *time_tm = NULL;
183
184         unsigned long seconds;
185         seconds = atol(date_string);
186
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);
191 }
192
193
194 #if (!CHECK_VALIDATE_UTF8)
195 const char *search_markup_keyword(const char *string, char *searchword,
196                                  bool *result)
197 {
198         SEARCH_FUNC_START;
199
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, };
204         int word_len = 0;
205         int search_len = 0;
206         int i = 0;
207         bool found = false;
208
209         strncpy(pstr, string, DEF_BUF_LEN);
210
211         word_len = strlen(pstr);
212         search_len = strlen(searchword);
213
214         for (i = 0; i < word_len; i++) {
215                 if (!strncasecmp(searchword, &pstr[i], search_len)) {
216                         found = true;
217                         break;
218                 }
219         }
220
221         *result = found;
222
223         if (found) {
224                 if (i == 0) {
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],
229                                         &pstr[search_len]);
230                 } else if (i > 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]);
238                 }
239         } else {
240                 snprintf(return_string, 128, "%s", pstr);
241         }
242
243         SEARCH_FUNC_END;
244
245         return return_string;
246 }
247
248
249 #else
250 const char *search_markup_keyword(const char *string, char *searchword,
251                                  bool *result)
252 {
253         char pstr[DEF_BUF_LEN + 1] = {0,};
254         static char return_string[DEF_BUF_LEN + 1] = { 0, };
255         int word_len = 0;
256         int search_len = 0;
257         int i = 0;
258         bool found = false;
259         gchar* markup_text_start;
260         gchar* markup_text_end;
261         gchar* markup_text;
262
263         SEARCH_RET_IF_STR_INVALID(string, return_string);
264         SEARCH_RET_IF_STR_INVALID(searchword, return_string);
265
266         if(g_utf8_validate(string,-1,NULL)) {
267
268                 strncpy(pstr, string, DEF_BUF_LEN);
269
270                 word_len = strlen(pstr);
271                 search_len = strlen(searchword);
272
273                 for (i = 0; i < word_len; i++) {
274                         if (!strncasecmp(searchword, &pstr[i], search_len)) {
275                                 found = true;
276                                 break;
277                         }
278                 }
279
280                 *result = found;
281                 memset(return_string, 0x00, DEF_BUF_LEN);
282
283                 if (found) {                    
284                         if (i == 0) {
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, 
288                                                         DEF_BUF_LEN, 
289                                                         "<match>%s</match>%s",
290                                                         markup_text,
291                                                         (char*)markup_text_end);
292                                 g_free(markup_text);
293                                 g_free(markup_text_end);
294                         } else {
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, 
299                                                         DEF_BUF_LEN, 
300                                                         "%s<match>%s</match>%s",
301                                                         (char*)markup_text_start,
302                                                         markup_text,
303                                                         (char*)markup_text_end);
304                                 g_free(markup_text);
305                                 g_free(markup_text_start);
306                                 g_free(markup_text_end);
307                         }
308                 } else {
309                         snprintf(return_string, 128, "%s", pstr);
310                 }
311         }
312
313         return return_string;
314 }
315 #endif
316
317 char *search_get_main_window_name()
318 {
319         SEARCH_FUNC_START;
320
321         XTextProperty tp;
322         int count = 0, i, ret;
323         char **list = NULL;
324         char return_win_name[256] = { 0, };
325         int revert_to;
326         Window focus_win;
327         Display *dpy;
328         int screen = 0;
329
330         dpy = XOpenDisplay(0);
331         screen = DefaultScreen(dpy);
332
333         XGetInputFocus(dpy, &focus_win, &revert_to);
334
335         if (focus_win) {
336                 XGetWMName(dpy, focus_win, &tp);
337                 if (tp.nitems > 0) {
338                         ret =
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],
343                                                 strlen(list[i]));
344                                 XFreeStringList(list);
345                         }
346                 }
347         } else {
348                 return NULL;
349         }
350
351         SEARCH_FUNC_END;
352
353         return strdup(return_win_name);
354 }