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