395a2b70af86c5910c0044b712725753de924a91
[apps/core/preloaded/lockscreen.git] / src / info.c
1 /*
2  * Copyright 2012  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 #include <appcore-common.h>
18 #include <vconf.h>
19 #include <vconf-keys.h>
20 #include <dlog.h>
21 #include <app_service.h>
22 #include <unicode/udat.h>
23 #include <unicode/udatpg.h>
24
25 #include "lockscreen.h"
26 #include "log.h"
27
28 static Ecore_Timer *timer = NULL;
29
30 static bool get_formatted_date_from_utc_time(time_t* utc_time, char* date_str, int date_size)
31 {
32         UErrorCode status = U_ZERO_ERROR;
33         UDateTimePatternGenerator *generator;
34         UDateFormat *formatter;
35         UChar skeleton[40] = { 0 }
36                 , pattern[40] = { 0 }
37                 , formatted[40] = { 0 };
38         int32_t patternCapacity, formattedCapacity;
39         int32_t skeletonLength, patternLength, formattedLength;
40         UDate date;
41         const char *locale;
42         const char customSkeleton[] = UDAT_MONTH_WEEKDAY_DAY;
43
44         date = (UDate) (*utc_time) *1000;
45
46         uloc_setDefault(__secure_getenv("LC_TIME"), &status);
47         locale = uloc_getDefault();
48
49         generator = udatpg_open(locale, &status);
50         if (generator == NULL)
51                 return false;
52
53         patternCapacity = (int32_t) (sizeof(pattern) / sizeof((pattern)[0]));
54
55         u_uastrcpy(skeleton, customSkeleton);
56
57         skeletonLength = strlen(customSkeleton);
58
59         patternLength =
60                 udatpg_getBestPattern(generator, skeleton, skeletonLength, pattern,
61                                 patternCapacity, &status);
62
63         formatter =
64                 udat_open(UDAT_IGNORE, UDAT_DEFAULT, locale, NULL, -1, pattern,
65                         patternLength, &status);
66         if (formatter == NULL) {
67                 udatpg_close(generator);
68                 return false;
69         }
70
71         formattedCapacity =
72                 (int32_t) (sizeof(formatted) / sizeof((formatted)[0]));
73
74         formattedLength =
75                 udat_format(formatter, date, formatted, formattedCapacity, NULL,
76                         &status);
77
78         u_austrcpy(date_str, formatted);
79
80         udatpg_close(generator);
81
82         udat_close(formatter);
83
84         return true;
85 }
86
87 static Eina_Bool _set_info_time(void *data)
88 {
89         Evas_Object *info = (Evas_Object *) data;
90         if (info == NULL)
91                 return false;
92
93         struct tm st;
94         time_t tt;
95         char buf[512] = { 0, };
96         char bf1[32] = { 0, };
97         char bf2[32] = { 0, };
98         int r, hour;
99         enum appcore_time_format timeformat;
100
101         tt = time(NULL);
102         localtime_r(&tt, &st);
103
104         if (timer != NULL) {
105                 ecore_timer_del(timer);
106                 timer = NULL;
107         }
108
109         char utc_date[256] = { 0, };
110         get_formatted_date_from_utc_time(&tt, utc_date, sizeof(utc_date));
111         edje_object_part_text_set(_EDJ(info), "txt.date", utc_date);
112
113         timer = ecore_timer_add(60 - st.tm_sec, _set_info_time, info);
114
115         r = appcore_get_timeformat(&timeformat);
116         if (r == 0 && timeformat == APPCORE_TIME_FORMAT_24) {
117                 strftime(bf1, sizeof(bf1), "%H:%M", &st);
118                 snprintf(buf, sizeof(buf), "%s", bf1);
119         } else {
120                 strftime(bf1, sizeof(bf1), "%l", &st);
121                 hour = atoi(bf1);
122                 strftime(bf1, sizeof(bf1), ":%M", &st);
123                 snprintf(buf, sizeof(buf), "%d%s", hour, bf1);
124                 if (st.tm_hour >= 0 && st.tm_hour < 12) {
125                         snprintf(bf2, sizeof(bf2), "%s", "AM");
126                         if ((st.tm_hour - 10) < 0 && st.tm_hour != 0) {
127                                 edje_object_signal_emit(_EDJ(info), "digit,clock", "rect.clock.ampm");
128                         } else {
129                                 edje_object_signal_emit(_EDJ(info), "default,clock", "rect.clock.ampm");
130                         }
131                 } else {
132                         snprintf(bf2, sizeof(bf2), "%s", "PM");
133                         if ((st.tm_hour - 12) < 10 && (st.tm_hour - 12) != 0) {
134                                 edje_object_signal_emit(_EDJ(info), "digit,clock", "rect.clock.ampm");
135                         } else {
136                                 edje_object_signal_emit(_EDJ(info), "default,clock", "rect.clock.ampm");
137                         }
138                 }
139                 edje_object_part_text_set(_EDJ(info), "txt.clock.ampm", bf2);
140         }
141         edje_object_part_text_set(_EDJ(info), "txt.clock", buf);
142
143         return 0;
144 }
145
146 static void _set_info_helptext(void *data)
147 {
148         struct appdata *ad = data;
149         if (ad == NULL) {
150                 return;
151         }
152
153         edje_object_part_text_set(_EDJ(ad->info), "txt.helptext", _NOT_LOCALIZED("Drag along the dots to unlock"));
154 }
155
156 void _set_info(void *data)
157 {
158         LOGD("[ == %s == ]", __func__);
159         struct appdata *ad = data;
160         if (ad == NULL) {
161                 return;
162         }
163
164         int is_clock = -1;
165         int is_helptext = -1;
166
167         int retc = vconf_get_bool(VCONFKEY_LOCKSCREEN_CLOCK_DISPLAY, &is_clock);
168         int retht = vconf_get_bool(VCONFKEY_LOCKSCREEN_HELP_TEXT_DISPLAY, &is_helptext);
169
170         if(0 == retc) {
171                 if(is_clock) {
172                         _set_info_time(ad->info);
173                 }
174         }
175         if(0 == retht) {
176                 if(is_helptext) {
177                         _set_info_helptext(ad);
178                 }
179         }
180 }
181
182 void update_time(void *data)
183 {
184         struct appdata *ad = data;
185         if (ad == NULL) {
186                 return;
187         }
188
189         int is_clock = -1;
190         int retc = vconf_get_bool(VCONFKEY_LOCKSCREEN_CLOCK_DISPLAY, &is_clock);
191         if(0 == retc) {
192                 if(is_clock) {
193                         _set_info_time(ad->info);
194                 }
195         }
196 }