Flora license update
[apps/home/smartsearch.git] / src / common_util.cpp
1 /*
2  * Copyright 2012-2013 Samsung Electronics Co., Ltd/
3  *
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
7  *
8  *     http://floralicense.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
23 #include <smartsearch.h>
24 #include <common_util.h>
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27
28 #include <unicode/uloc.h>
29 #include <unicode/udat.h>
30 #include <unicode/udatpg.h>
31 #include <unicode/ustring.h>
32
33 static UDateTimePatternGenerator *search_pattern_generator = NULL;
34 static UDateFormat *search_formatter_yymmdd_12th;
35 static UDateFormat *search_formatter_yymmdd_24th;
36
37 static UDateFormat *search_formatter_yymmdd;
38 static UDateFormat *search_formatter_yymm;
39
40 UDateFormat *__search_util_make_date_format(const char *skeleton)
41 {
42         SEARCH_FUNC_START;
43
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;
50
51         /* Pattern Generator */
52         if (search_pattern_generator) {
53                 udatpg_close(search_pattern_generator);
54                 search_pattern_generator = NULL;
55         }
56
57         uloc_setDefault(getenv("LC_TIME"), &status);
58
59         locale = uloc_getDefault();
60
61         search_pattern_generator = udatpg_open(uloc_getDefault(), &status);
62
63         if (!search_pattern_generator) {
64                 SEARCH_DEBUG_WARNING
65                     ("pattern_generator / udatpg_open fail : %s",
66                      u_errorName(status));
67                 return NULL;
68         }
69
70         SEARCH_DEBUG_LOG("skeleton : %s", skeleton);
71
72         u_uastrncpy(customSkeleton, skeleton, strlen(skeleton));
73
74         bestPatternCapacity =
75             (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
76         bestPatternLength =
77             udatpg_getBestPattern(search_pattern_generator, customSkeleton,
78                                   u_strlen(customSkeleton), bestPattern,
79                                   bestPatternCapacity, &status);
80
81         if (bestPatternLength == 0) {
82                 SEARCH_DEBUG_WARNING("udatpg_getBestPattern fail");
83                 return NULL;
84         }
85
86         formatter =
87             udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, bestPattern,
88                       -1, &status);
89
90         SEARCH_FUNC_END;
91
92         return formatter;
93         }
94
95
96 void search_util_date_time_format_init()
97 {
98         SEARCH_FUNC_START;
99
100         /* Date Time Format From Skeletons */
101         enum appcore_time_format timeformat;
102         int ret;
103         char *skeleton = NULL;
104
105         ret = appcore_get_timeformat(&timeformat);
106         if (ret == -1) {
107                 SEARCH_DEBUG_WARNING("Fail to get time format!");
108                 return;
109         }
110
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);
119
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;
124         } else {
125                 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
126                 return;
127         }
128
129         SEARCH_FUNC_END;
130
131                 return;
132         }
133
134 void search_region_format_cb(void *data)
135 {
136         SEARCH_FUNC_START;
137
138         enum appcore_time_format timeformat;
139         int ret;
140
141         ret = appcore_get_timeformat(&timeformat);
142
143         if (ret == -1) {
144                 SEARCH_DEBUG_WARNING("Fail to get time format!");
145                 return;
146         }
147
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;
152         } else {
153                 SEARCH_DEBUG_WARNING("appcore_get_timeformat unknown error");
154         return;
155 }
156
157         SEARCH_FUNC_END;
158 }
159
160 void search_util_date_time_format_deinit()
161 {
162         SEARCH_FUNC_START;
163
164         if (search_pattern_generator) {
165                 udatpg_close(search_pattern_generator);
166                 search_pattern_generator = NULL;
167         }
168
169         if (search_formatter_yymmdd_12th) {
170                 udat_close(search_formatter_yymmdd_12th);
171                 search_formatter_yymmdd_12th = NULL;
172         }
173
174         if (search_formatter_yymmdd_24th) {
175                 udat_close(search_formatter_yymmdd_24th);
176                 search_formatter_yymmdd_24th = NULL;
177         }
178
179         if (search_formatter_yymm) {
180                 udat_close(search_formatter_yymm);
181                 search_formatter_yymm = NULL;
182         }
183
184         SEARCH_FUNC_END;
185
186         return;
187 }
188
189 void search_util_date_time_format_get_val(time_t time, char *format_val, int type)
190 {
191         SEARCH_FUNC_START;
192
193         UDate date;
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, };
198
199         date = (UDate) time *1000;
200
201         formattedCapacity =
202             (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
203
204         switch(type) {
205         case SEARCH_DATE_TYPE_YYMMDD:
206                 if (search_formatter_yymmdd) {
207                         formattedLength =
208                             udat_format(search_formatter_yymmdd, date, formatted, formattedCapacity,
209                                         NULL, &status);
210                         if (formattedLength == -1) {
211                                 SEARCH_DEBUG_WARNING("udat_format fail");
212                                 return;
213                         }
214                 }
215         break;
216         case SEARCH_DATE_TYPE_YYMM:
217                 if (search_formatter_yymm) {
218         formattedLength =
219                             udat_format(search_formatter_yymm, date, formatted, formattedCapacity,
220                         NULL, &status);
221         if (formattedLength == -1) {
222                 SEARCH_DEBUG_WARNING("udat_format fail");
223                 return;
224         }
225                 }
226         break;
227         }
228
229
230         u_austrncpy(formattedString, formatted, SEARCH_MAX_CHAR_SIZE);
231
232         SEARCH_DEBUG_LOG("formattedString : %s", formattedString);
233
234         snprintf(format_val, MAX_LENGTH_PER_LINE, "%s", formattedString);
235
236         SEARCH_FUNC_END;
237 }
238
239 void search_sql_make_keyword_bind_value(char *src, char *dest, int type)
240 {
241         char *tmp;
242
243         if(type == SEARCH_SQL_BIND_TYPE_DUPLEX) {
244                 *dest = '%';
245                 ++dest;
246         }
247
248         for (tmp = src; *tmp; ++tmp, ++dest) {
249                 if ((*tmp == '%') || (*tmp == '_') || (*tmp == *DB_ESCAPE_CHAR)) {
250                         *dest = *DB_ESCAPE_CHAR;
251                         ++dest;
252                 }
253
254                 *dest = *tmp;
255         }
256
257         *dest = '%';
258 }
259
260 void search_get_date_string(char *date_string)
261 {
262         struct tm *time_tm = NULL;
263
264         unsigned long seconds;
265         seconds = atol(date_string);
266
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);
271 }
272
273
274 #if (!CHECK_VALIDATE_UTF8)
275 const char *search_markup_keyword(const char *string, char *searchword,
276                                  bool *result)
277 {
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, };
282         int word_len = 0;
283         int search_len = 0;
284         int i = 0;
285         bool found = false;
286
287         SEARCH_RET_IF_STR_INVALID(string, return_string);
288         SEARCH_RET_IF_STR_INVALID(searchword, return_string);
289
290         strncpy(pstr, string, DEF_BUF_LEN);
291
292         word_len = strlen(pstr);
293         search_len = strlen(searchword);
294
295         for (i = 0; i < word_len; i++) {
296                 if (!strncasecmp(searchword, &pstr[i], search_len)) {
297                         found = true;
298                         break;
299                 }
300         }
301
302         *result = found;
303
304         if (found) {
305                 if (i == 0) {
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],
310                                         &pstr[search_len]);
311                 } else if (i > 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]);
319                 }
320         } else {
321                 snprintf(return_string, 128, "%s", pstr);
322         }
323
324         return return_string;
325 }
326
327
328 #else
329 const char *search_markup_keyword(const char *string, char *searchword,
330                                  bool *result)
331 {
332         char pstr[DEF_BUF_LEN + 1] = {0,};
333         static char return_string[DEF_BUF_LEN + 1] = { 0, };
334         int word_len = 0;
335         int search_len = 0;
336         int i = 0;
337         bool found = false;
338         gchar* markup_text_start;
339         gchar* markup_text_end;
340         gchar* markup_text;
341
342         SEARCH_RET_IF_STR_INVALID(string, return_string);
343         SEARCH_RET_IF_STR_INVALID(searchword, return_string);
344
345         if(g_utf8_validate(string,-1,NULL)) {
346
347                 strncpy(pstr, string, DEF_BUF_LEN);
348
349                 word_len = strlen(pstr);
350                 search_len = strlen(searchword);
351
352                 for (i = 0; i < word_len; i++) {
353                         if (!strncasecmp(searchword, &pstr[i], search_len)) {
354                                 found = true;
355                                 break;
356                         }
357                 }
358
359                 *result = found;
360                 memset(return_string, 0x00, DEF_BUF_LEN);
361
362                 if (found) {
363                         if (i == 0) {
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,
367                                                         DEF_BUF_LEN,
368                                                         "<match>%s</match>%s",
369                                                         markup_text,
370                                                         (char*)markup_text_end);
371                                 g_free(markup_text);
372                                 g_free(markup_text_end);
373                         } else {
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,
378                                                         DEF_BUF_LEN,
379                                                         "%s<match>%s</match>%s",
380                                                         (char*)markup_text_start,
381                                                         markup_text,
382                                                         (char*)markup_text_end);
383                                 g_free(markup_text);
384                                 g_free(markup_text_start);
385                                 g_free(markup_text_end);
386                         }
387                 } else {
388                         snprintf(return_string, 128, "%s", pstr);
389                 }
390         }
391
392         return return_string;
393 }
394 #endif
395
396 char *search_get_main_window_name()
397 {
398         SEARCH_FUNC_START;
399
400         XTextProperty tp;
401         int count = 0, i, ret;
402         char **list = NULL;
403         char return_win_name[256] = { 0, };
404         int revert_to;
405         Window focus_win;
406         Display *dpy;
407         int screen = 0;
408
409         dpy = XOpenDisplay(0);
410         screen = DefaultScreen(dpy);
411
412         XGetInputFocus(dpy, &focus_win, &revert_to);
413
414         if (focus_win) {
415                 XGetWMName(dpy, focus_win, &tp);
416                 if (tp.nitems > 0) {
417                         ret =
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],
422                                                 strlen(list[i]));
423                                 XFreeStringList(list);
424                         }
425                 }
426         } else {
427                 return NULL;
428         }
429
430         SEARCH_FUNC_END;
431
432         return strdup(return_win_name);
433 }