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