upload codes for TIZEN 2.0
[apps/home/clock.git] / timer / src / timer_utils.c
1 /*
2 *
3 * Copyright 2012  Samsung Electronics Co., Ltd
4 *
5 * Licensed under the Flora License, Version 1.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *    http://www.tizenopensource.org/license
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 #include "timer_utils.h"
19 #include "clock_fwk_widget.h"
20 /**
21  * Debug function:  to read time setted last time
22  * This function requires data as parameters
23  *
24  * timer_vconf_load_start_time()
25  * @param[in] data                  Pointer to user data, which contains the data of application
26  *
27  * @return      indicate whether it succeed
28  */
29 int timer_vconf_load_start_time(void *data)
30 {
31         retvm_if(!data, FAILED, "data null");
32         struct appdata *ad = (struct appdata *)data;
33         int nErr = SUCCESS;
34         int start_time = 0;
35
36         nErr = vconf_get_int(VCONF_TIMER_START_TIME, &start_time);
37         retvm_if(nErr != SUCCESS, FAILED, "vconf_get_int error");
38         // set start time
39         ad->chour[0] = '0' + (start_time / 100000) % 10;
40         ad->chour[1] = '0' + (start_time / 10000) % 10;
41         ad->chour[2] = '\0';
42
43         ad->cminute[0] = '0' + (start_time / 1000) % 10;
44         ad->cminute[1] = '0' + (start_time / 100) % 10;
45         ad->cminute[2] = '\0';
46
47         ad->csecond[0] = '0' + (start_time / 10) % 10;
48         ad->csecond[1] = '0' + start_time % 10;
49         ad->csecond[2] = '\0';
50         return SUCCESS;
51 }
52
53 /**
54  * Debug function: to save the setted time
55  * This function requires data as parameters
56  *
57  * timer_vconf_set_start_time()
58  * @param[in] data                  Pointer to user data, which contains the data of application
59  *
60  * @return       indicate whether it succeed
61  */
62 int timer_vconf_set_start_time(void *data)
63 {
64         retvm_if(!data, FAILED, "data null");
65         struct appdata *ad = (struct appdata *)data;
66         int nErr = SUCCESS;
67         int start_time = 0;
68         // read start time
69         start_time += (ad->chour[0] - '0') * 100000;
70         start_time += (ad->chour[1] - '0') * 10000;
71         start_time += (ad->cminute[0] - '0') * 1000;
72         start_time += (ad->cminute[1] - '0') * 100;
73         start_time += (ad->csecond[0] - '0') * 10;
74         start_time += ad->csecond[1] - '0';
75
76         nErr = vconf_set_int(VCONF_TIMER_START_TIME, start_time);
77         retvm_if(nErr != SUCCESS, FAILED, "vconf_set_int error");
78         vconf_sync_key(VCONF_TIMER_START_TIME);
79         return SUCCESS;
80 }
81
82 /**
83  * Debug function:  to get which unit is focused last time
84  * This function requires data as parameters
85  *
86  * timer_vconf_load_focus_id()
87  * @param[in] data                  Pointer to user data, which contains the data of application
88  *
89  * @return      indicate whether it succeed
90  */
91 int timer_vconf_load_focus_id(void *data)
92 {
93         retvm_if(!data, FAILED, "data null");
94         struct appdata *ad = (struct appdata *)data;
95         int nErr = SUCCESS;
96         int panel_focus_id = 0;
97
98         nErr = vconf_get_int(VCONF_TIMER_PANEL_FOCUS_ID, &panel_focus_id);
99         retvm_if(nErr != SUCCESS, FAILED, "vconf_get_int error");
100         ad->entry_number = panel_focus_id;
101         return SUCCESS;
102 }
103
104 /**
105  * Debug function:  to save which unit is focused
106  * This function requires data as parameters
107  *
108  * timer_vconf_set_focus_id()
109  * @param[in] data                  Pointer to user data, which contains the data of application
110  *
111  * @return       indicate whether it succeed
112  */
113 int timer_vconf_set_focus_id(void *data)
114 {
115         retvm_if(!data, FAILED, "data null");
116         struct appdata *ad = (struct appdata *)data;
117         int nErr = SUCCESS;
118         int panel_focus_id = ad->entry_number;
119
120         nErr = vconf_set_int(VCONF_TIMER_PANEL_FOCUS_ID, panel_focus_id);
121         retvm_if(nErr != SUCCESS, FAILED, "vconf_set_int error");
122         vconf_sync_key(VCONF_TIMER_PANEL_FOCUS_ID);
123         return SUCCESS;
124 }
125
126 //***************************************************************
127 // when start counting ,call this function to register to alarm-manager
128 //***************************************************************
129 int timer_utils_register_alarm(void *data)
130 {
131         retvm_if(!data, FAILED, "data null");
132         int ret = SUCCESS;
133         int nErr = SUCCESS;
134         struct appdata *ad = (struct appdata *)data;
135
136         //delete before
137         timer_utils_unregister_alarm(data);
138         //create new
139         time_t alert_t;
140         alert_t = time(NULL);
141         alert_t += ad->due_time.tv_sec;
142         //
143         AData *pAlarm = alarmdb_create_data();
144         retvm_if(!pAlarm, FAILED, "pAlarm null");
145         pAlarm->enable = ALARM_DB_ENABLE_ON;
146         pAlarm->author = ALARM_DB_AUTHOR_TIMER;
147         snprintf(pAlarm->name, sizeof(pAlarm->name), "%s",
148                  STRING_TIMER_IDS_TMR_POP_TIME_IS_UP_);
149         pAlarm->stime = alert_t;
150         pAlarm->atime = alert_t;
151         pAlarm->repeat_once = EINA_TRUE;
152         pAlarm->volume = DEFAULT_ALARM_DB_VOLUME;
153         pAlarm->type = DEFAULT_ALARM_DB_TYPE;
154         //
155         snprintf(pAlarm->tone, sizeof(pAlarm->tone), "%s",
156                  ALARM_SOUND_PATH "/" TIMER_DEF_TONE);
157         //
158         nErr = clk_fwk_alarmmgr_create(pAlarm);
159         CLK_GOTO_IF_RUN(nErr != SUCCESS, End, ret =
160                         TIMER_POPUP_ID_ALARMMGR, "alarmapp_mgr_create error");
161         nErr = alarmdb_add_data(pAlarm);
162         CLK_GOTO_IF_RUN(IS_EQUAL(nErr, -1), End, ret =
163                         TIMER_POPUP_ID_ALARMDB, "alarmdb_add_data error");
164         ad->alarm_mgr_id = pAlarm->alarm_mgr_id;
165         ad->slp_alarm_id = pAlarm->id;
166  End:
167         if (pAlarm) {
168                 alarmdb_free_data(pAlarm);
169         }
170         return ret;
171 }
172
173 //***************************************************************
174 // when stop counting ,call this function to unregister to alarm-manager
175 //***************************************************************
176 int timer_utils_unregister_alarm(void *data)
177 {
178         retv_if(!data, FAILED);
179         AData *alarm = NULL;
180         ADList *list = alarmdb_get_data_list_by_author(ALARM_DB_AUTHOR_TIMER);
181         ADList *cur = list;
182         ADList *pre = NULL;
183         retv_if(!list, SUCCESS);
184         while (cur) {
185                 alarm = &list->ad;
186                 if (IS_EQUAL(alarm->enable, ALARM_DB_ENABLE_ON)) {
187                         alarmmgr_remove_alarm(alarm->alarm_mgr_id);
188                 }
189                 pre = cur;
190                 cur = cur->next;
191                 alarmdb_del_data(alarm->id);
192         }
193         alarmdb_free_data_list(list);
194         return SUCCESS;
195 }
196
197 /**
198  * Debug function: to read time which is saved in vconf
199  * This function requires data as parameters
200  *
201  * _timer_utils_read_setting_time()
202  * @param[in] data                  Pointer to user data, which contains the data of application
203  *
204  * @return     void
205  */
206 int timer_utils_time_get(void *data)
207 {
208         retvm_if(!data, FAILED, "data null");
209         struct appdata *ad = (struct appdata *)data;
210
211         ad->due_time.tv_sec =
212             atoi(ad->chour) * 3600 + atoi(ad->cminute) * 60 +
213             atoi(ad->csecond) + 1;
214         ad->due_time.tv_usec = 0;
215         return SUCCESS;
216 }
217
218 /**
219  * Debug function: to check whether the start time is zero
220  *  if all zero, return 1, else return 0;
221  * This function requires data as parameters
222  *
223  * _timer_utils_read_setting_time()
224  * @param[in] data         Pointer to user data, which contains the data of application
225  *
226  * @return     void
227  */
228 Eina_Bool is_timer_utils_time_validate(struct appdata * ad)
229 {
230         retvm_if(NULL_CHECK(ad), EINA_FALSE, "ad null");
231         return IS_EQUAL(0, ad->due_time.tv_sec) ? EINA_FALSE : EINA_TRUE;
232 }
233
234 /**
235  * Debug function: to read alarm info from server
236  * This function requires data as parameters
237  *
238  * timer_utils_read_alarm_state()
239  * @param[in] data         Pointer to user data, which contains the data of application
240  *
241  * @return     indicates whether it is successful.
242  */
243 TIMER_VIEW_PAGE timer_utils_read_alarm_state(void *data)
244 {
245         retvm_if(!data, TIMER_VIEW_PAGE_SETTING, "data null");
246         TIMER_VIEW_PAGE ret = TIMER_VIEW_PAGE_MAX;
247         struct appdata *ad = (struct appdata *)data;
248         time_t current_t;
249         AData *alarm_data = NULL;
250
251         ADList *list = alarmdb_get_data_list_by_author(ALARM_DB_AUTHOR_TIMER);
252         retv_if(!list, TIMER_VIEW_PAGE_SETTING);
253         ad->slp_alarm_id = list->ad.id;
254         alarmdb_free_data_list(list);
255
256         alarm_data = alarmdb_get_data(ad->slp_alarm_id);
257         retvm_if(!alarm_data, TIMER_VIEW_PAGE_SETTING, "get data error");
258         // get current time
259         current_t = time(NULL);
260         if (alarm_data->atime < current_t) {
261                 timer_utils_unregister_alarm(data);
262                 ret = TIMER_VIEW_PAGE_SETTING;
263         } else {
264                 ad->due_time.tv_sec = alarm_data->atime - current_t;
265                 ad->due_time.tv_usec = 0;
266                 ret = TIMER_VIEW_PAGE_MOTION;
267         }
268         alarmdb_free_data(alarm_data);
269         return ret;
270 }
271
272 //
273 void set_default_time(struct appdata *ad)
274 {
275         retm_if(!ad, "ad null");
276         ad->chour[0] = '0';
277         ad->chour[1] = '0';
278         ad->chour[2] = '\0';
279
280         ad->cminute[0] = '0';
281         ad->cminute[1] = '1';
282         ad->cminute[2] = '\0';
283
284         ad->csecond[0] = '0';
285         ad->csecond[1] = '0';
286         ad->csecond[2] = '\0';
287 }
288
289 /**
290 * send
291 * This function is  used to show views
292 * @param           data[in]         pointer to struct appdata
293 * @param           bVisible[in]     Eina_Bool to controlbar visible(EINA_TRUE) or invisible(EINA_FALSE)
294 * @return          when success, return SUCCESS or FAILED if error
295 * @exception
296 */
297 int timer_show_view(void *data, Eina_Bool bVisible)
298 {
299         retvm_if(!data, FAILED, "data null");
300         struct appdata *ad = (struct appdata *)data;
301         EVAS_OBJECT_SHOWIF(ad->layout);
302         //set controbbar visible
303         if (ad->controlbar_cb) {
304                 (ad->controlbar_cb) (bVisible);
305         }
306         return SUCCESS;
307 }
308
309 /**
310 * send
311 * This function is  used to create popup
312 * @param           parent[in]           pointer to evas object parent
313 * @param           result[in]           int, popup type
314 * @return          void
315 * @exception
316 */
317 void timer_widget_create_popup(Evas_Object * parent, TIMER_POPUP_ID id)
318 {
319         retm_if(!parent, "parent null");
320         char msg[BUF_SIZE_256] = { 0, };
321
322         switch (id) {
323         case TIMER_POPUP_ID_ALARMMGR:
324                 snprintf(msg, sizeof(msg), "%s",
325                          STRING_TIMER_ALARM_SERVER_FAILED_);
326                 break;
327         case TIMER_POPUP_ID_ALARMDB:
328                 snprintf(msg, sizeof(msg), "%s", STRING_TIMER_DB_FAILED_);
329                 break;
330         default:
331                 snprintf(msg, sizeof(msg), "%s",
332                          STRING_TIMER_IDS_COM_POP_INTERNAL_ERROR_S_);
333                 break;
334         }
335         widget_create_popup(parent, msg, NULL, 1.0, NULL, NULL);
336 }
337
338 void timer_utils_view_change(struct appdata *ad)
339 {
340         ASSERT(ad);
341         TIMER_EFFECT_ID effect_id;
342         if (!IS_EQUAL(ad->page, TIMER_VIEW_PAGE_MOTION)) {
343                 effect_id = TIMER_EFFECT_ID_LEFT;
344                 timer_view_motion.create(ad);
345                 elm_object_part_content_set(ad->eo_effect,
346                                             STRING_TIMER_ELM_SWALLOW_RIGHT,
347                                             ad->view_motion->eo_content);
348         } else {
349                 effect_id = TIMER_EFFECT_ID_RIGHT;
350                 timer_view_setting.create(ad);
351                 elm_object_part_content_set(ad->eo_effect,
352                                             STRING_TIMER_ELM_SWALLOW_LEFT,
353                                             ad->view_setting->eo_content);
354         }
355         if (IS_EQUAL(ad->page, TIMER_VIEW_PAGE_MOTION)) {
356                 timer_view_setting.destroy(ad);
357                 Evas_Object *eo = elm_object_part_content_unset(ad->eo_effect,
358                                                                 STRING_TIMER_ELM_SWALLOW_RIGHT);
359                 elm_object_part_content_set(ad->eo_effect,
360                                             STRING_TIMER_ELM_SWALLOW_CENTER,
361                                             eo);
362         } else {
363                 timer_view_motion.destroy(ad);
364                 Evas_Object *eo = elm_object_part_content_unset(ad->eo_effect,
365                                                                 STRING_TIMER_ELM_SWALLOW_LEFT);
366                 elm_object_part_content_set(ad->eo_effect,
367                                             STRING_TIMER_ELM_SWALLOW_CENTER,
368                                             eo);
369         }
370 }
371
372 void timer_repaint_time(void *data)
373 {
374         retm_if(NULL_CHECK(data), "data null");
375         struct appdata *ad = (struct appdata *)data;
376         motion_view *view = ad->view_motion;
377         retm_if(NULL_CHECK(view), "view null");
378         struct timeval time_past = { 0, };
379         char time_str[BUF_SIZE] = { 0, };
380         struct tm show_time;
381         //get time passed
382         time_past.tv_sec =
383             ad->current_systime.tv_sec - ad->start_systime.tv_sec;
384         time_past.tv_usec =
385             ad->current_systime.tv_usec - ad->start_systime.tv_usec;
386         if ((time_past.tv_sec) && (time_past.tv_usec < 0)) {
387                 time_past.tv_sec -= 1;
388                 time_past.tv_usec += 1000000;
389         }
390         //reduce due-time according to time passed
391         ad->due_time.tv_sec -= time_past.tv_sec;
392         ad->due_time.tv_usec -= time_past.tv_usec;
393         if ((ad->due_time.tv_sec) && (ad->due_time.tv_usec < 0)) {
394                 ad->due_time.tv_sec -= 1;
395                 ad->due_time.tv_usec += 1000000;
396         }
397
398         gmtime_r(&ad->due_time.tv_sec, &show_time);
399
400         strftime(time_str, BUF_SIZE, "%02H:%02M:%S", &show_time);
401         edje_object_part_text_set(_EDJ(view->eo_content),
402                                   STRING_TIMER_MOTION_TIME, time_str);
403
404 }