[Request]Update Flora license version
[apps/home/libslp-alarm.git] / src / libalarm.c
1 /*
2 *
3 * Copyright 2012  Samsung Electronics Co., Ltd
4 *
5 * Licensed under the Flora License, Version 1.1 (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://floralicense.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 <limits.h>
19 #include "libalarm.h"
20
21 /**********************************************************************
22 ******************define, struct ,typedef, union, enum, global val *************************************
23 ***********************************************************************/
24
25 //
26 #define COMP_FUNC(type) \
27         int __cmp_##type##_(type * d1, type * d2) \
28 { \
29             if (d1->ad.id == 0 && d2->ad.id == 0) return -1; \
30             return (d1->ad.id - d2->ad.id); \
31 } \
32 COMP_FUNC(ADList);
33 /**********************************************************************
34 ******************Local function declear, extern function declear*************************************
35 ***********************************************************************/
36
37 /**********************************************************************
38 ******************Global val , static global val*************************************
39 ***********************************************************************/
40
41 /**********************************************************************
42 ******************Local function  ref*************************************
43 ***********************************************************************/
44 static DBHandle *db = NULL;
45
46 /**********************************************************************
47 ******************Global function  ref*************************************
48 ***********************************************************************/
49
50 //
51 A_DBAPI int alarmdb_init(const char *dbfile)
52 {
53         char *name = NULL;
54         char defname[PATH_MAX] = { 0, };
55
56         if (db) {
57                 DB_INFO("Already initialized");
58                 return 0;
59         }
60         if (dbfile)
61                 name = (char *)dbfile;
62         if (name == NULL) {
63                 snprintf(defname, sizeof(defname), "%s/%s", DBROOT, DBNAME);
64                 name = defname;
65         }
66         DB_INFO("DB name : %s", name);
67         db = db_init(name);
68         retv_if(db == NULL, -1);
69         return 0;
70 }
71
72 //
73 A_DBAPI void alarmdb_fini(void)
74 {
75         if (db) {
76                 db_fini(db);
77                 db = NULL;
78         }
79 }
80
81 //
82 A_DBAPI struct alarm_data *alarmdb_create_data(void)
83 {
84         AData *ad = (struct alarm_data *)calloc(1, sizeof(struct alarm_data));
85         if (!ad) {
86                 return NULL;
87         }
88         //init
89         memset(ad, 0, sizeof(AData));
90         MAGIC_VALUE_SET(ad->_magic);
91         return ad;
92 }
93
94 //
95 A_DBAPI struct alarm_data *alarmdb_get_data(int id)
96 {
97         int rc;
98         struct alarm_data *ad = NULL;
99
100         retvm_if(db == NULL, NULL, "DB handler is NULL, needs alarm_init");
101         retvm_if(id < 1, NULL, "Invalid alarm data id");
102
103         ad = alarmdb_create_data();
104         retv_if(ad == NULL, NULL);
105
106         rc = get_data(db, id, ad);
107         if (rc) {
108                 alarmdb_free_data(ad);
109                 return NULL;
110         }
111         return ad;
112 }
113
114 //
115 A_DBAPI struct alarm_data *alarmdb_get_data_by_author(int id, char author)
116 {
117         int rc;
118         struct alarm_data *ad = NULL;
119
120         retvm_if(db == NULL, NULL, "DB handler is NULL, needs alarm_init");
121         retvm_if(id < 1, NULL, "Invalid alarm data id");
122
123         ad = alarmdb_create_data();
124         retv_if(ad == NULL, NULL);
125
126         rc = get_data_by_author(db, id, ad, author);
127         if (rc) {
128                 alarmdb_free_data(ad);
129                 return NULL;
130         }
131         return ad;
132 }
133
134 //
135 A_DBAPI void alarmdb_free_data(struct alarm_data *ad)
136 {
137         retm_if(ad == NULL, "alarm data is NULL\n");
138         retm_if(!MAGIC_VALUE_CHECK(ad->_magic),
139                 "alarm data is error, ad->_magic=%d,ALARM_DB_MAGIC_VALUE=%d\n",
140                 ad->_magic, ALARM_DB_MAGIC_VALUE);
141         if (ad) {
142                 free(ad);
143         }
144 }
145
146 //
147 A_DBAPI int alarmdb_add_data(struct alarm_data *ad)
148 {
149         retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init");
150         return insert_data(db, ad);
151 }
152
153 //
154 A_DBAPI int alarmdb_mod_data(struct alarm_data *ad)
155 {
156         retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init");
157         return update_data(db, ad);
158 }
159
160 //
161 A_DBAPI int alarmdb_del_data(int id)
162 {
163         retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init");
164         retvm_if(id < 1, -1, "Invalid alarm data id");
165         return remove_data(db, id);
166 }
167
168 //
169 A_DBAPI int alarmdb_set_enable(int id, bool enable)
170 {
171         retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init");
172         return update_enable(db, id, enable);
173 }
174
175 //
176 A_DBAPI int alarmdb_get_num_of_enable(void)
177 {
178         retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init");
179         return get_num_of_enable((sqlite3 *)db);
180 }
181
182 //
183 A_DBAPI int alarmdb_set_snooze(int id, bool enable)
184 {
185         retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init");
186         return update_snooze((db), id, enable);
187 }
188
189 //
190 A_DBAPI struct alarm_data_list *alarmdb_get_data_list_all(void)
191 {
192         retvm_if(db == NULL, NULL, "DB handler is NULL, needs alarm_init");
193         return get_data_list_all(db);
194 }
195
196 //
197 A_DBAPI struct alarm_data_list *alarmdb_get_data_list_by_author(char author)
198 {
199         retvm_if(db == NULL, NULL, "DB handler is NULL, needs alarm_init");
200         return get_data_list_by_author(db, author);
201 }
202
203 //
204 A_DBAPI void alarmdb_free_data_list(struct alarm_data_list *adl)
205 {
206         struct alarm_data_list *d = NULL;
207         struct alarm_data_list *t = NULL;
208
209         retm_if(adl == NULL, "Invalid argument: alarm data list is NULL\n");
210         t = adl;
211         while (t) {
212                 d = t;
213                 t = t->next;
214                 free(d);
215         }
216 }
217
218 //
219 A_DBAPI int alarmdb_get_last_id(void)
220 {
221         return get_last_id(db);
222 }
223
224 //
225 A_DBAPI int alarmdb_get_last_id_by_author(char author)
226 {
227         return get_last_id_by_author(db, author);
228 }
229
230 //
231 A_DBAPI int alarmdb_get_number_of_data_by_author(char author)
232 {
233         return get_number_of_data_by_author(db, author);
234 }
235
236 /*
237 A_DBAPI int alarmdb_get_power_on_by_author(char author)
238 {
239     return get_poweron_by_author(db, author);
240 }
241 */
242 //
243 A_DBAPI int alarmdb_get_power_onoff_by_author(char author)
244 {
245         return get_power_onoff_by_author(db, author);
246 }