Tizen 2.4.0 rev3 SDK Public Release
[apps/home/quickpanel.git] / daemon / notifications / noti_util.c
1 /* * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include <Elementary.h>
22 #include <E_DBus.h>
23
24 #include <unicode/uloc.h>
25 #include <unicode/udat.h>
26 #include <unicode/udatpg.h>
27 #include <unicode/ustring.h>
28
29 #include <tzsh.h>
30 #include <tzsh_quickpanel_service.h>
31 #include <runtime_info.h>
32 #include <vconf.h>
33 #include <system_settings.h>
34 #include <notification_list.h>
35
36 #include "quickpanel-ui.h"
37 #include "common.h"
38 #include "noti_util.h"
39
40 #define QP_NOTI_DAY_DEC (24 * 60 * 60)
41 #define QP_NOTI_TIME_LEN_LIMIT 12
42
43 HAPI int quickpanel_noti_util_get_event_count_from_noti(notification_h noti)
44 {
45         char *text_count = NULL;
46
47         retif(noti == NULL, 0, "Invalid parameter!");
48
49         notification_get_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &text_count);
50         if (text_count != NULL) {
51                 return atoi(text_count);
52         }
53         return 1;
54 }
55
56 HAPI int quickpanel_noti_util_get_event_count_by_pkgname(const char *pkgname)
57 {
58         int count = 0;
59         notification_h noti = NULL;
60         notification_list_h noti_list = NULL;
61
62         retif(pkgname == NULL, 0, "Invalid parameter!");
63
64         notification_get_detail_list(pkgname, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE, -1, &noti_list);
65         if (noti_list != NULL) {
66                 noti = notification_list_get_data(noti_list);
67                 if (noti != NULL) {
68                         count = quickpanel_noti_util_get_event_count_from_noti(noti);
69                 }
70                 notification_free_list(noti_list);
71                 return count;
72         }
73
74         return 0;
75 }
76
77 static char* _get_locale(void)
78 {
79         char locale_tmp[32] = { 0, };
80         char *locale = NULL;
81         int ret = 0;
82
83         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, &locale);
84         msgif(ret != SYSTEM_SETTINGS_ERROR_NONE, "ailed to set key(%s) : %d", SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, ret);
85
86
87         if (locale == NULL) {
88                 ERR("vconf_get_str() failed : region format");
89                 return strdup("en_US");
90         }
91
92         strncpy(locale_tmp, locale, sizeof(locale_tmp) - 1);
93
94         // remove .UTF-8
95         if (strlen(locale_tmp) > 0) {
96                 char *p = strstr(locale_tmp, ".UTF-8");
97                 if (p) {
98                         *p = 0;
99                 }
100         }
101
102         free(locale);
103
104         if (strlen(locale_tmp) > 0) {
105                 return strdup(locale_tmp);
106         }
107
108         return strdup("en_US");
109 }
110
111 static char* _get_timezone_from_vconf(void)
112 {
113         char *szTimezone = NULL;
114         szTimezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
115         if (szTimezone == NULL) {
116                 ERR("Cannot get time zone.");
117                 return strdup("N/A");
118         }
119
120         return szTimezone;
121 }
122
123 static char* _get_timezone(void)
124 {
125         char buf[1024] = {0,};
126         ssize_t len = readlink("/opt/etc/localtime", buf, sizeof(buf)-1);
127
128         if (len != -1) {
129                 buf[len] = '\0';
130         } else {
131                 ERR("failed to get a timezone information");
132                 return _get_timezone_from_vconf();
133         }
134
135         return strdup(buf + 20);
136 }
137
138 HAPI char *quickpanel_noti_util_get_time(time_t t, char *buf, int buf_len)
139 {
140         int ret = 0;
141         UErrorCode status = U_ZERO_ERROR;
142         UDate date;
143         UDateTimePatternGenerator *generator = NULL;
144         UDateFormat *formatter = NULL;
145         UChar utimezone_id[40] = {0,};
146         UChar skeleton[40] = { 0 };
147         UChar pattern[40] = { 0 };
148         UChar formatted[40] = { 0 };
149         int32_t patternCapacity, formattedCapacity;
150         int32_t skeletonLength, patternLength;
151         time_t today;
152         struct tm loc_time;
153         char *timezone = NULL;
154         char *locale = NULL;
155         char bf1[32] = { 0, };
156         bool is_24hour_enabled = FALSE;
157         int is_show_time = 0;
158
159         today = time(NULL);
160         localtime_r(&today, &loc_time);
161
162         loc_time.tm_sec = 0;
163         loc_time.tm_min = 0;
164         loc_time.tm_hour = 0;
165         today = mktime(&loc_time);
166
167         localtime_r(&t, &loc_time);
168
169         if (buf == NULL) {
170                 return NULL;
171         }
172
173         if (t < today) {
174                 /* ascii to unicode for input skeleton */
175                 u_uastrcpy(skeleton, UDAT_ABBR_MONTH_DAY);
176                 skeletonLength = strlen(UDAT_ABBR_MONTH_DAY);
177                 is_show_time = 0;
178         } else {
179                 ret = system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, &is_24hour_enabled);
180
181                 if (ret == SYSTEM_SETTINGS_ERROR_NONE && is_24hour_enabled == true) {
182                         /* ascii to unicode for input skeleton */
183                         u_uastrcpy(skeleton, "HHmm");
184                         skeletonLength = strlen("HHmm");
185                 } else {
186                         /* ascii to unicode for input skeleton */
187                         u_uastrcpy(skeleton, "hhmm");
188                         skeletonLength = strlen("hhmm");
189                 }
190                 is_show_time = 1;
191         }
192
193         /* set UDate  from time_t */
194         date = (UDate)t * 1000;
195
196         patternCapacity =
197                 (int32_t) (sizeof(pattern) / sizeof((pattern)[0]));
198
199         timezone = _get_timezone();
200         locale = _get_locale();
201
202         if (u_uastrncpy(utimezone_id, timezone, 40) == NULL) {
203                 ERR("u_uastrncpy() error.");
204                 ret = 0;
205                 goto err;
206         }
207
208         ucal_setDefaultTimeZone(utimezone_id , &status);
209         if (U_FAILURE(status)) {
210                 ERR("ucal_setDefaultTimeZone() is failed.");
211                 ret = 0;
212                 goto err;
213         }
214
215 #ifdef HAVE___SECURE_GETENV
216         uloc_setDefault(__secure_getenv("LC_TIME"), &status);
217 #elif defined HAVE_SECURE_GETENV
218         uloc_setDefault(secure_getenv("LC_TIME"), &status);
219 #else
220         uloc_setDefault(getenv("LC_TIME"), &status);
221 #endif
222         if (U_FAILURE(status)) {
223                 ERR("uloc_setDefault() is failed.");
224                 ret = 0;
225                 goto err;
226         }
227
228         /* open datetime pattern generator */
229         generator = udatpg_open(locale, &status);
230         if (generator == NULL) {
231                 ret = 0;
232                 goto err;
233         }
234
235         /* get best pattern using skeleton */
236         patternLength =
237                 udatpg_getBestPattern(generator, skeleton, skeletonLength,
238                                 pattern, patternCapacity, &status);
239
240         /* open datetime formatter using best pattern */
241         formatter =
242                 udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1,
243                                 pattern, patternLength, &status);
244         if (formatter == NULL) {
245                 ret = 0;
246                 goto err;
247         }
248
249         /* calculate formatted string capacity */
250         formattedCapacity =
251                 (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
252
253         /* formatting date using formatter by best pattern */
254         udat_format(formatter, date, formatted, formattedCapacity,
255                         NULL, &status);
256
257         /* unicode to ascii to display */
258         u_austrcpy(bf1, formatted);
259         ret = snprintf(buf, buf_len, "%s", bf1);
260
261         if (is_show_time == 1 && strlen(buf) > QP_NOTI_TIME_LEN_LIMIT) {
262                 if (is_24hour_enabled == TRUE) {
263                         ret = strftime(buf, buf_len, "%H:%M", &loc_time);
264                 } else {
265                         strftime(bf1, sizeof(bf1), "%l:%M", &loc_time);
266
267                         if (loc_time.tm_hour >= 0 && loc_time.tm_hour < 12) {
268                                 ret = snprintf(buf, buf_len, "%s%s", bf1, "AM");
269                         } else {
270                                 ret = snprintf(buf, buf_len, "%s%s", bf1, "PM");
271                         }
272                 }
273         }
274
275 err:
276         if (timezone) {
277                 free(timezone);
278                 timezone = NULL;
279         }
280
281         if (locale) {
282                 free(locale);
283                 locale = NULL;
284         }
285
286         if (generator) {
287                 udatpg_close(generator);
288                 generator = NULL;
289         }
290
291         if (formatter) {
292                 udat_close(formatter);
293                 formatter = NULL;
294         }
295
296         return ret <= 0 ? NULL : buf;
297 }
298
299
300 HAPI char *quickpanel_noti_util_get_text(notification_h noti, notification_text_type_e text_type)
301 {
302         time_t time = 0;
303         char *text = NULL;
304         char buf[128] = {0,};
305
306         if (notification_get_time_from_text(noti, text_type, &time) == NOTIFICATION_ERROR_NONE) {
307                 if ((int)time > 0) {
308                         quickpanel_noti_util_get_time(time, buf, sizeof(buf));
309                         text = buf;
310                 }
311         } else {
312                 notification_get_text(noti, text_type, &text);
313         }
314
315         DBG("text : %s", text);
316
317         if (text != NULL) {
318                 return elm_entry_utf8_to_markup(text);
319         }
320
321         return NULL;
322 }
323