sync with private git. updated the license and the boilerplates
[apps/home/quickpanel.git] / daemon / notifications / noti_util.c
1 /*
2  * Copyright 2012  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
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 #include <unicode/uloc.h>
18 #include <unicode/udat.h>
19 #include <unicode/udatpg.h>
20 #include <unicode/ustring.h>
21 #include <runtime_info.h>
22
23 #include "quickpanel-ui.h"
24 #include "common.h"
25 #include "noti_util.h"
26
27 #define QP_NOTI_DAY_DEC (24 * 60 * 60)
28
29 HAPI int quickpanel_noti_get_event_count_from_noti(notification_h noti) {
30         char *text_count = NULL;
31
32         retif(noti == NULL, 0, "Invalid parameter!");
33
34         notification_get_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, &text_count);
35         if (text_count != NULL) {
36                 return atoi(text_count);
37         }
38         return 1;
39 }
40
41 HAPI int quickpanel_noti_get_event_count_by_pkgname(const char *pkgname) {
42         int count = 0;
43         notification_h noti = NULL;
44         notification_list_h noti_list = NULL;
45
46         retif(pkgname == NULL, 0, "Invalid parameter!");
47
48         notification_get_detail_list(pkgname, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE, -1, &noti_list);
49         if (noti_list != NULL) {
50                 noti = notification_list_get_data(noti_list);
51                 if (noti != NULL) {
52                         count = quickpanel_noti_get_event_count_from_noti(noti);
53                 }
54                 notification_free_list(noti_list);
55                 return count;
56         }
57
58         return 0;
59 }
60
61 HAPI char *quickpanel_noti_get_time(time_t t, char *buf, int buf_len)
62 {
63         UErrorCode status = U_ZERO_ERROR;
64         UDateTimePatternGenerator *generator;
65         UDateFormat *formatter;
66         UChar skeleton[40] = { 0 };
67         UChar pattern[40] = { 0 };
68         UChar formatted[40] = { 0 };
69         int32_t patternCapacity, formattedCapacity;
70         int32_t skeletonLength, patternLength, formattedLength;
71         UDate date;
72         const char *locale;
73         const char customSkeleton[] = UDAT_YEAR_NUM_MONTH_DAY;
74         char bf1[32] = { 0, };
75         bool is_24hour_enabled = FALSE;
76
77         struct tm loc_time;
78         time_t today, yesterday;
79         int ret = 0;
80
81         today = time(NULL);
82         localtime_r(&today, &loc_time);
83
84         loc_time.tm_sec = 0;
85         loc_time.tm_min = 0;
86         loc_time.tm_hour = 0;
87         today = mktime(&loc_time);
88
89         yesterday = today - QP_NOTI_DAY_DEC;
90
91         localtime_r(&t, &loc_time);
92
93         if (t >= yesterday && t < today) {
94                 ret = snprintf(buf, buf_len, _S("IDS_COM_BODY_YESTERDAY"));
95         } else if (t < yesterday) {
96                 /* set UDate  from time_t */
97                 date = (UDate) t * 1000;
98
99                 /* get default locale  */
100                 /* for thread saftey  */
101                 uloc_setDefault(__secure_getenv("LC_TIME"), &status);
102                 locale = uloc_getDefault();
103
104                 /* open datetime pattern generator */
105                 generator = udatpg_open(locale, &status);
106                 if (generator == NULL)
107                         return NULL;
108
109                 /* calculate pattern string capacity */
110                 patternCapacity =
111                     (int32_t) (sizeof(pattern) / sizeof((pattern)[0]));
112
113                 /* ascii to unicode for input skeleton */
114                 u_uastrcpy(skeleton, customSkeleton);
115
116                 /* get skeleton length */
117                 skeletonLength = strlen(customSkeleton);
118
119                 /* get best pattern using skeleton */
120                 patternLength =
121                     udatpg_getBestPattern(generator, skeleton, skeletonLength,
122                                           pattern, patternCapacity, &status);
123
124                 /* open datetime formatter using best pattern */
125                 formatter =
126                     udat_open(UDAT_IGNORE, UDAT_DEFAULT, locale, NULL, -1,
127                               pattern, patternLength, &status);
128                 if (formatter == NULL) {
129                         udatpg_close(generator);
130                         return NULL;
131                 }
132
133                 /* calculate formatted string capacity */
134                 formattedCapacity =
135                     (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
136
137                 /* formatting date using formatter by best pattern */
138                 formattedLength =
139                     udat_format(formatter, date, formatted, formattedCapacity,
140                                 NULL, &status);
141
142                 /* unicode to ascii to display */
143                 u_austrcpy(bf1, formatted);
144
145                 /* close datetime pattern generator */
146                 udatpg_close(generator);
147
148                 /* close datetime formatter */
149                 udat_close(formatter);
150
151                 ret = snprintf(buf, buf_len, "%s", bf1);
152         } else {
153                 ret = runtime_info_get_value_bool(
154                                         RUNTIME_INFO_KEY_24HOUR_CLOCK_FORMAT_ENABLED, &is_24hour_enabled);
155                 if (ret == RUNTIME_INFO_ERROR_NONE && is_24hour_enabled == TRUE) {
156                         ret = strftime(buf, buf_len, "%H:%M", &loc_time);
157                 } else {
158                         strftime(bf1, sizeof(bf1), "%l:%M", &loc_time);
159
160                         if (loc_time.tm_hour >= 0 && loc_time.tm_hour < 12)
161                                 ret = snprintf(buf, buf_len, "%s%s", bf1, "AM");
162                         else
163                                 ret = snprintf(buf, buf_len, "%s%s", bf1, "PM");
164                 }
165
166         }
167
168         return ret <= 0 ? NULL : buf;
169 }