5c198bff0c52a5475e7958ba03a43d298eaffad9
[apps/home/clock.git] / clock-common / src / clock_fwk_alarmmgr.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 "clock_fwk_alarmmgr.h"
18 #include "clock_fwk_util.h"
19
20 /**********************************************************************
21 ******************Global function ref*************************************
22 ***********************************************************************/
23
24 /**
25 * send
26 * This function is  used to create mgr
27 * @param           data[in]         pointer to AData
28 * @return          when success, return EINA_TRUE or EINA_FALSE if error
29 * @exception
30 */
31 int clk_fwk_alarmmgr_create(AData * alarm)
32 {
33         int ret = SUCCESS;
34         int nErr = SUCCESS;
35         alarm_entry_t *alarm_entry = NULL;
36         alarm_date_t alarm_data;
37         CLK_FUN_BEG();
38         CLK_RETVM_IF(!alarm, FAILED, "para error");
39         /*create alarm */
40         alarm_entry = alarmmgr_create_alarm();
41         CLK_RETVM_IF(!alarm_entry, FAILED, "alarmmgr_create_alarm error");
42         /*set repeat */
43         int repeat_value = 0;
44         alarm_repeat_mode_t repeat_mode = 0;
45         if (alarm->repeat_once) {
46                 repeat_mode = ALARM_REPEAT_MODE_ONCE;
47         } else if (alarm->repeat_weekly) {
48                 repeat_mode = ALARM_REPEAT_MODE_WEEKLY;
49         }
50         repeat_value = alarm->repeat_weekly;
51         nErr = alarmmgr_set_repeat_mode(alarm_entry, repeat_mode, repeat_value);
52         CLK_RETVM_IF(nErr != SUCCESS, FAILED, "alarmmgr_set_repeat_mode error");
53         /*set time_data */
54         struct tm pt;
55         localtime_r(&alarm->atime, &pt);
56 //    CLK_RETVM_IF(!pt, FAILED, "localtime NULL");
57         if (IS_EQUAL(alarm->author, ALARM_DB_AUTHOR_TIMER)) {
58                 SET_TIME_DATA_T(&alarm_data, pt.tm_year + 1900,
59                                 pt.tm_mon + 1, pt.tm_mday, pt.tm_hour,
60                                 pt.tm_min, pt.tm_sec);
61         } else if (IS_EQUAL(repeat_mode, ALARM_REPEAT_MODE_ONCE)) {
62                 SET_TIME_DATA_T(&alarm_data, pt.tm_year + 1900, pt.tm_mon + 1,
63                                 pt.tm_mday, pt.tm_hour, pt.tm_min, 0);
64                 if (difftime((double)alarm->atime, (double)time(NULL)) <= 0.0) {
65                         alarm->atime += 7 * 24 * 3600;
66                         localtime_r(&alarm->atime, &pt);
67                         SET_TIME_DATA_T(&alarm_data, pt.tm_year + 1900,
68                                         pt.tm_mon + 1, pt.tm_mday,
69                                         pt.tm_hour, pt.tm_min, 0);
70                 }
71         } else {
72                 SET_TIME_DATA_T(&alarm_data, 0, 0, 0, pt.tm_hour, pt.tm_min, 0);
73         }
74         nErr = alarmmgr_set_time(alarm_entry, alarm_data);
75         CLK_RETVM_IF(nErr != SUCCESS, FAILED, "alarmmgr_set_time error");
76         /*set type   */
77         nErr = alarmmgr_set_type(alarm_entry, ALARM_TYPE_DEFAULT);
78         CLK_RETVM_IF(nErr != SUCCESS, FAILED, "alarmmgr_set_type error");
79         /*create new    */
80         int alarm_mgr_id = 0;
81         nErr =
82             alarmmgr_add_alarm_with_localtime(alarm_entry, ALARMRING_PKGNAME,
83                                               &alarm_mgr_id);
84         CLK_RETVM_IF(SUCCESS != nErr, FAILED,
85                      "alarmmgr_add_alarm_with_localtime error,nErr=%d", nErr);
86
87         alarm->alarm_mgr_id = alarm_mgr_id;
88  End:
89         if (alarm_entry) {
90                 alarmmgr_free_alarm(alarm_entry);
91         }
92         CLK_FUN_END();
93         return ret;
94 }
95
96 /**
97 * send
98 * This function is  used to remove mgr
99 * @param           data[in]         pointer to AData
100 * @return          when success, return EINA_TRUE or EINA_FALSE if error
101 * @exception
102 */
103 int clk_fwk_alarmmgr_remove(AData * alarm)
104 {
105         int ret = SUCCESS;
106         ret = alarmmgr_remove_alarm(alarm->alarm_mgr_id);
107         if (ret != SUCCESS) {
108                 CLK_ERR("Failed to remove alarm[%d], error code: %d\n",
109                         alarm->alarm_mgr_id, ret);
110         }
111         alarm->alarm_mgr_id = -1;
112         return ret;
113 }
114
115 /**
116 * send
117 * This function is  used to update mgr
118 * @param           data[in]         pointer to AData
119 * @return          when success, return EINA_TRUE or EINA_FALSE if error
120 * @exception
121 */
122 int clk_fwk_alarmmgr_update(AData * alarm)
123 {
124         int ret = SUCCESS;
125         ret = clk_fwk_alarmmgr_remove(alarm);
126         ret |= clk_fwk_alarmmgr_create(alarm);
127         return ret;
128 }