1 /* * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <Elementary.h>
24 #include <unicode/uloc.h>
25 #include <unicode/udat.h>
26 #include <unicode/udatpg.h>
27 #include <unicode/ustring.h>
30 #include <tzsh_quickpanel_service.h>
31 #include <runtime_info.h>
33 #include <system_settings.h>
34 #include <notification_list.h>
36 #include "quickpanel-ui.h"
38 #include "noti_util.h"
40 #define QP_NOTI_DAY_DEC (24 * 60 * 60)
41 #define QP_NOTI_TIME_LEN_LIMIT 12
43 HAPI int quickpanel_noti_util_get_event_count_from_noti(notification_h noti)
45 char *text_count = NULL;
47 retif(noti == NULL, 0, "Invalid parameter!");
49 notification_get_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &text_count);
50 if (text_count != NULL) {
51 return atoi(text_count);
56 HAPI int quickpanel_noti_util_get_event_count_by_pkgname(const char *pkgname)
59 notification_h noti = NULL;
60 notification_list_h noti_list = NULL;
62 retif(pkgname == NULL, 0, "Invalid parameter!");
64 notification_get_detail_list(pkgname, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE, -1, ¬i_list);
65 if (noti_list != NULL) {
66 noti = notification_list_get_data(noti_list);
68 count = quickpanel_noti_util_get_event_count_from_noti(noti);
70 notification_free_list(noti_list);
77 static char* _get_locale(void)
79 char locale_tmp[32] = { 0, };
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);
88 ERR("vconf_get_str() failed : region format");
89 return strdup("en_US");
92 strncpy(locale_tmp, locale, sizeof(locale_tmp) - 1);
95 if (strlen(locale_tmp) > 0) {
96 char *p = strstr(locale_tmp, ".UTF-8");
104 if (strlen(locale_tmp) > 0) {
105 return strdup(locale_tmp);
108 return strdup("en_US");
111 static char* _get_timezone_from_vconf(void)
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");
123 static char* _get_timezone(void)
125 char buf[1024] = {0,};
126 ssize_t len = readlink("/opt/etc/localtime", buf, sizeof(buf)-1);
131 ERR("failed to get a timezone information");
132 return _get_timezone_from_vconf();
135 return strdup(buf + 20);
138 HAPI char *quickpanel_noti_util_get_time(time_t t, char *buf, int buf_len)
141 UErrorCode status = U_ZERO_ERROR;
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;
153 char *timezone = NULL;
155 char bf1[32] = { 0, };
156 bool is_24hour_enabled = FALSE;
157 int is_show_time = 0;
160 localtime_r(&today, &loc_time);
164 loc_time.tm_hour = 0;
165 today = mktime(&loc_time);
167 localtime_r(&t, &loc_time);
174 /* ascii to unicode for input skeleton */
175 u_uastrcpy(skeleton, UDAT_ABBR_MONTH_DAY);
176 skeletonLength = strlen(UDAT_ABBR_MONTH_DAY);
179 ret = system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, &is_24hour_enabled);
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");
186 /* ascii to unicode for input skeleton */
187 u_uastrcpy(skeleton, "hhmm");
188 skeletonLength = strlen("hhmm");
193 /* set UDate from time_t */
194 date = (UDate)t * 1000;
197 (int32_t) (sizeof(pattern) / sizeof((pattern)[0]));
199 timezone = _get_timezone();
200 locale = _get_locale();
202 if (u_uastrncpy(utimezone_id, timezone, 40) == NULL) {
203 ERR("u_uastrncpy() error.");
208 ucal_setDefaultTimeZone(utimezone_id , &status);
209 if (U_FAILURE(status)) {
210 ERR("ucal_setDefaultTimeZone() is failed.");
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);
220 uloc_setDefault(getenv("LC_TIME"), &status);
222 if (U_FAILURE(status)) {
223 ERR("uloc_setDefault() is failed.");
228 /* open datetime pattern generator */
229 generator = udatpg_open(locale, &status);
230 if (generator == NULL) {
235 /* get best pattern using skeleton */
237 udatpg_getBestPattern(generator, skeleton, skeletonLength,
238 pattern, patternCapacity, &status);
240 /* open datetime formatter using best pattern */
242 udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1,
243 pattern, patternLength, &status);
244 if (formatter == NULL) {
249 /* calculate formatted string capacity */
251 (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
253 /* formatting date using formatter by best pattern */
254 udat_format(formatter, date, formatted, formattedCapacity,
257 /* unicode to ascii to display */
258 u_austrcpy(bf1, formatted);
259 ret = snprintf(buf, buf_len, "%s", bf1);
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);
265 strftime(bf1, sizeof(bf1), "%l:%M", &loc_time);
267 if (loc_time.tm_hour >= 0 && loc_time.tm_hour < 12) {
268 ret = snprintf(buf, buf_len, "%s%s", bf1, "AM");
270 ret = snprintf(buf, buf_len, "%s%s", bf1, "PM");
287 udatpg_close(generator);
292 udat_close(formatter);
296 return ret <= 0 ? NULL : buf;
300 HAPI char *quickpanel_noti_util_get_text(notification_h noti, notification_text_type_e text_type)
304 char buf[128] = {0,};
306 if (notification_get_time_from_text(noti, text_type, &time) == NOTIFICATION_ERROR_NONE) {
308 quickpanel_noti_util_get_time(time, buf, sizeof(buf));
312 notification_get_text(noti, text_type, &text);
315 DBG("text : %s", text);
318 return elm_entry_utf8_to_markup(text);