tizen 2.3 release
[apps/home/settings.git] / setting-common / src / setting-common-alarmmgr.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <setting-debug.h>
22 #include <setting-common-alarmmgr.h>
23 #define EXPORT_PUBLIC __attribute__ ((visibility ("default")))
24
25
26 /**********************************************************************
27 ******************Global function ref*************************************
28 ***********************************************************************/
29
30 /**
31 * send
32 * This function is  used to create mgr
33 * @param           data[in]         pointer to AData
34 * @return          when success, return EINA_TRUE or EINA_FALSE if error
35 * @exception
36 */
37 EXPORT_PUBLIC
38 int setting_common_alarmmgr_create(BM_AData * alarm)
39 {
40         //SETTING_TRACE_BEGIN;
41         int nErr = -1;  //no err
42         alarm_entry_t *alarm_entry = NULL;
43         alarm_date_t alarm_data;
44
45         /*create alarm */
46         alarm_entry = alarmmgr_create_alarm();
47         /*set repeat */
48         int repeat_value = 0;
49         alarm_repeat_mode_t repeat_mode = 0;
50         nErr = alarmmgr_set_repeat_mode(alarm_entry, repeat_mode, repeat_value);
51         /*set time_data */
52         struct tm pt;
53         memset(&pt, 0, sizeof(struct tm));
54         time_t ctime = time(NULL);
55         if (NULL == localtime_r(&ctime, &pt)) {
56                 SETTING_TRACE_ERROR("fail to call localtime_r");
57         }
58
59         SETTING_TRACE("alarm->hour[%d], alarm->min[%d]",alarm->hour, alarm->min);
60         pt.tm_hour = alarm->hour;
61         pt.tm_min = alarm->min;
62         SET_BM_TIME_DATA_T(&alarm_data, pt.tm_year + 1900, pt.tm_mon + 1, pt.tm_mday, pt.tm_hour, pt.tm_min, 0);
63         nErr |= alarmmgr_set_time(alarm_entry, alarm_data);
64
65         /*set type   */
66         nErr = alarmmgr_set_type(alarm_entry, ALARM_TYPE_DEFAULT);
67
68         /* bundle */
69         //bundle *b = NULL;
70         //b = bundle_create();
71         //appsvc_set_operation(b, APPSVC_OPERATION_DEFAULT);
72         //appsvc_set_pkgname(b, SETTING_BM_ALARM_APP);
73
74         /*create new    */
75         int alarm_mgr_id = 0;
76         nErr = alarmmgr_add_alarm_with_localtime(alarm_entry, SETTING_BM_ALARM_APP, &alarm_mgr_id);
77         //nErr = alarmmgr_add_alarm_appsvc_with_localtime(alarm_entry, (void *)b, &alarm_mgr_id);
78         SETTING_TRACE("alarm_mgr_id [%d]", alarm_mgr_id);
79         if(nErr)
80         {
81                 SETTING_TRACE("*** [ERR] alarmmgr_add_alarm_with_localtime failed ***");
82         }
83
84         alarm->alarm_mgr_id = alarm_mgr_id;
85         //vconf_set_int(VCONFKEY_SETAPPL_BM_ALARM_ID_START, alarm->alarm_mgr_id);
86
87         if (alarm_entry) {
88                 alarmmgr_free_alarm(alarm_entry);
89         }
90         return nErr;
91 }
92
93 /**
94 * send
95 * This function is  used to remove mgr
96 * @param           data[in]         pointer to AData
97 * @return          when success, return EINA_TRUE or EINA_FALSE if error
98 * @exception
99 */
100 #if SUPPORT_BLOCKINGMODE
101 EXPORT_PUBLIC
102 int setting_common_alarmmgr_remove(BM_AData * alarm)
103 {
104         //SETTING_TRACE_BEGIN;
105         int start_block_id = -1;
106         int end_block_id = -1;
107         vconf_get_int(VCONFKEY_SETAPPL_BM_ALARM_ID_START, &start_block_id);
108         vconf_get_int(VCONFKEY_SETAPPL_BM_ALARM_ID_END, &end_block_id);
109         SETTING_TRACE("alarm->alarm_mgr_id [%d]",alarm->alarm_mgr_id);
110         int ret = -1;
111         if(start_block_id==alarm->alarm_mgr_id) {
112                 SETTING_TRACE("start id remove");
113                 ret = alarmmgr_remove_alarm(alarm->alarm_mgr_id);
114                 vconf_set_int(VCONFKEY_SETAPPL_BM_ALARM_ID_START, -1);
115         }
116         else if(end_block_id==alarm->alarm_mgr_id)      {
117                 SETTING_TRACE("end id remove");
118                 ret = alarmmgr_remove_alarm(alarm->alarm_mgr_id);
119                 vconf_set_int(VCONFKEY_SETAPPL_BM_ALARM_ID_END, -1);
120         }
121         else
122         {
123                 SETTING_TRACE("nothing to remove just create");
124         }
125
126         if(ret)
127         {
128                 SETTING_TRACE("*** [ERR] alarmmgr_remove_alarm failed : err_code=[%d]", ret);
129         }
130
131         return ret;
132 }
133 #endif
134 /**
135 * send
136 * This function is  used to update mgr
137 * @param           data[in]         pointer to AData
138 * @return          when success, return EINA_TRUE or EINA_FALSE if error
139 * @exception
140 */
141 EXPORT_PUBLIC
142 int setting_common_alarmmgr_update(BM_AData * alarm)
143 {
144         //SETTING_TRACE_BEGIN;
145         int ret = -1;
146 #if SUPPORT_BLOCKINGMODE
147         ret = setting_common_alarmmgr_remove(alarm);
148 #endif
149         ret = setting_common_alarmmgr_create(alarm);
150         SETTING_TRACE("*** [ERR] setting_common_alarmmgr_update failed ***");
151
152         return ret;
153 }