Cutting new release
[profile/ivi/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://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 "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->etime = ad->time_on_pad;
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", 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);
213         ad->due_time.tv_usec = 300000;
214         ad->time_on_pad = ad->due_time.tv_sec;
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         snprintf(time_str, sizeof(time_str), "%d", show_time.tm_hour/10);
401         edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/hour/1");
402         snprintf(time_str, sizeof(time_str), "%d", show_time.tm_hour%10);
403         edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/hour/2");
404         snprintf(time_str, sizeof(time_str), "%d", show_time.tm_min/10);
405         edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/min/1");
406         snprintf(time_str, sizeof(time_str), "%d", show_time.tm_min%10);
407         edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/min/2");
408         snprintf(time_str, sizeof(time_str), "%d", show_time.tm_sec/10);
409         edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/sec/1");
410         snprintf(time_str, sizeof(time_str), "%d", show_time.tm_sec%10);
411         edje_object_signal_emit(_EDJ(view->eo_content), time_str, "time/image/sec/2");
412
413 }
414
415 void timer_utils_view_update(struct appdata *ad)
416 {
417         ASSERT(ad);
418         if (!IS_EQUAL(ad->page, TIMER_VIEW_PAGE_MOTION)){
419                 timer_view_motion.update(ad);
420         } else {
421                 timer_view_setting.update(ad);
422         }
423
424 }
425