[Request]Update Flora license version
[apps/home/libslp-alarm.git] / kies_alarm / src / kies_alarm.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 "kies_alarm.h"
19 #include <alarm.h>
20 #include "alarm-engine.h"
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include <vconf.h>
24 #include <vconf-keys.h>
25 #include "kies_fwk_alarmmgr.h"
26 #include "kies_define.h"
27 /**********************************************************************
28 ******************define *************************************
29 ***********************************************************************/
30 typedef enum {
31         TYPE_SAVE_ALARM_MODE_BINARY,
32         TYPE_SAVE_ALARM_MODE_TEXT,
33         TYPE_SAVE_ALARM_MODE_MAX,
34 } TYPE_SAVE_ALARM_MODE;
35 /**********************************************************************
36 ******************common *************************************
37 ***********************************************************************/
38
39 //
40 static bool is_file_exist(char *file_path)
41 {
42         if (!file_path) {
43                 return false;
44         }
45         FILE *pFile = NULL;
46         pFile = fopen(file_path, "r");
47         if (!pFile) {
48                 return false;
49         }
50         fclose(pFile);
51         return true;
52 }
53
54 /**********************************************************************
55 ******************save *************************************
56 ***********************************************************************/
57
58 //
59 static int _write_alarm_to_file_binary(ADList * list, char * file_path)
60 {
61         int ret = SUCCESS;
62         int nErr = SUCCESS;
63         FILE *pFile = NULL;
64         ADList *cur_data = NULL;
65         struct alarm_data *pAlarm = NULL;
66         size_t cur_size = 0;
67         KIES_FUN_BEG();
68         KIES_RETVM_IF(!list, FAILED, "alarm list null");
69         KIES_RETVM_IF(!file_path, FAILED, "file_path null");
70         //KIES_RETVM_IF(is_file_exist(file_path),FAILED,"file path error");
71         pFile = fopen(file_path, "w");
72         KIES_RETVM_IF(!pFile, FAILED, "fopen error");
73         for (cur_data = list; cur_data; cur_data = cur_data->next) {
74                 pAlarm = &cur_data->ad;
75                 cur_size = fwrite(pAlarm, sizeof(struct alarm_data), 1, pFile);
76                 KIES_RETVM_IF(cur_size != 1, FAILED, "fwrite size error");
77         }
78  End:
79         FCLOSE_IF(pFile);
80         KIES_FUN_END();
81         return ret;
82 }
83
84 //
85 static int _write_alarm_to_file_text(ADList *list, char *file_path)
86 {
87         int ret = SUCCESS;
88         return ret;
89 }
90
91 //
92 static int _write_alarm_to_file(ADList *list,
93                                 char *file_path, TYPE_SAVE_ALARM_MODE save_mode)
94 {
95         int ret = FAILED;
96         KIES_FUN_BEG();
97         switch (save_mode) {
98         case TYPE_SAVE_ALARM_MODE_BINARY:
99                 ret = _write_alarm_to_file_binary(list, file_path);
100                 break;
101         case TYPE_SAVE_ALARM_MODE_TEXT:
102                 ret = _write_alarm_to_file_text(list, file_path);
103                 break;
104         case TYPE_SAVE_ALARM_MODE_MAX:
105                 KIES_INFO_RED("type is error, save_mode=%d", save_mode);
106                 break;
107         }
108         KIES_FUN_END();
109         return ret;
110 }
111
112 //when backup, firstly, we need to get alarm data from libslp-alarm
113 //then, save the data to file_path
114 A_DBAPI int alarm_backup(const char *file_path)
115 {
116         int nErr = SUCCESS;
117         int ret = SUCCESS;
118         int db_success = FAILED;
119         ADList *list = NULL;
120         KIES_FUN_BEG();
121         //init alarmdb
122         KIES_RETVM_IF(!file_path, FAILED, "file_path null");
123         nErr = alarmdb_init(NULL);
124         KIES_RETVM_IF(nErr != SUCCESS, FAILED, "alarmdb_init");
125         db_success = SUCCESS;
126         //get alarm data, because alarmdb is used by many APs
127         list = alarmdb_get_data_list_by_author(ALARM_DB_AUTHOR_ALARM);
128         KIES_RETVM_IF(!list, RESULT_ERR_NULL_ALARM, "alarm data is NULL");
129         nErr =
130             _write_alarm_to_file(list, (char *)file_path,
131                                  TYPE_SAVE_ALARM_MODE_BINARY);
132         KIES_RETVM_IF(nErr != SUCCESS, nErr, "_write_alarm_to_file NULL");
133  End:
134         FREE_ADLIST(list);
135         if (IS_EQUAL(db_success, SUCCESS)) {
136                 alarmdb_fini();
137         }
138         KIES_FUN_END();
139         return ret;
140 }
141
142 /**********************************************************************
143 ******************restore *************************************
144  ***********************************************************************/
145
146 //clear alarm data in db;
147 //if alarm is enable, remove from alarm-manger
148 static int _reset_alarm(ADList *list)
149 {
150         int nErr = SUCCESS;
151         int ret = SUCCESS;
152         ADList *cur_data = NULL;
153         struct alarm_data *pAlarm = NULL;
154         KIES_FUN_BEG();
155         KIES_RETVM_IF(!list, SUCCESS, "null list, nothing to do");
156         //
157         for (cur_data = list; cur_data; cur_data = cur_data->next) {
158                 pAlarm = &cur_data->ad;
159                 if (pAlarm->enable) {
160                         nErr = kies_fwk_alarmmgr_remove(pAlarm);
161                         KIES_RETVM_IF(nErr != SUCCESS, FAILED,
162                                       "alarmmgr_remove_alarm error");
163                 }
164                 nErr = alarmdb_del_data(pAlarm->id);
165                 KIES_RETVM_IF(nErr != SUCCESS, FAILED,
166                               "alarmdb_del_data error");
167         }
168  End:
169         KIES_FUN_END();
170         return ret;
171 }
172
173 //if enable alarm, register alarm
174 //then, add to alarm db
175 static int _save_alarm(struct alarm_data *pAlarm)
176 {
177         int ret = SUCCESS;
178         int nErr = SUCCESS;
179         KIES_FUN_BEG();
180         KIES_RETVM_IF(!pAlarm, FAILED, "null alarm");
181         if (pAlarm->enable) {
182                 nErr = kies_fwk_alarmmgr_create(pAlarm);
183                 KIES_RETVM_IF(nErr != SUCCESS, FAILED, "_register_alarm error");
184         }
185         nErr = alarmdb_add_data(pAlarm);
186         KIES_RETVM_IF(IS_EQUAL(nErr, -1), FAILED, "alarmdb_add_data error");
187  End:
188         KIES_FUN_END();
189         return ret;
190 }
191
192 //
193 static int _read_alarm_from_file_binary(char *file_path)
194 {
195         int ret = SUCCESS;
196         int nErr = SUCCESS;
197         FILE *pFile = NULL;
198         size_t cur_size = 0;
199         struct alarm_data alarm;
200         KIES_FUN_BEG();
201         KIES_RETVM_IF(!file_path, FAILED, "file_path null");
202         KIES_RETVM_IF(!is_file_exist(file_path), FAILED, "file path error");
203         pFile = fopen(file_path, "r");
204         KIES_RETVM_IF(!pFile, FAILED, "fopen error");
205         //
206         while (1) {
207                 cur_size = fread(&alarm, sizeof(alarm), 1, pFile);
208                 if (IS_EQUAL(0, cur_size)) {
209                         //file is null, success
210                         goto End;
211                 }
212                 KIES_RETVM_IF(cur_size != 1, FAILED, "fread size error");
213                 //
214                 nErr = _save_alarm(&alarm);
215                 KIES_RETVM_IF(nErr != SUCCESS, FAILED, "_save_alarm error");
216         }
217  End:
218         FCLOSE_IF(pFile);
219         KIES_FUN_END();
220         return ret;
221 }
222
223 //
224 static int _read_alarm_from_file_text(char *file_path)
225 {
226         int ret = SUCCESS;
227         return ret;
228 }
229
230 //
231 static int _read_alarm_from_file(char *file_path,
232                                  TYPE_SAVE_ALARM_MODE save_mode)
233 {
234         int ret = FAILED;
235         KIES_FUN_BEG();
236         switch (save_mode) {
237         case TYPE_SAVE_ALARM_MODE_BINARY:
238                 ret = _read_alarm_from_file_binary(file_path);
239                 break;
240         case TYPE_SAVE_ALARM_MODE_TEXT:
241                 ret = _read_alarm_from_file_text(file_path);
242                 break;
243         case TYPE_SAVE_ALARM_MODE_MAX:
244                 KIES_INFO_RED("type is error, save_mode=%d", save_mode);
245                 break;
246         }
247         KIES_FUN_END();
248         return ret;
249 }
250
251 //
252 A_DBAPI int alarm_restore(const char *file_path)
253 {
254         int nErr = SUCCESS;
255         int ret = SUCCESS;
256         int db_success = FAILED;
257         int mgr_success = FAILED;
258         ADList *list = NULL;
259         KIES_FUN_BEG();
260         //init alarmdb
261         KIES_RETVM_IF(!file_path, FAILED, "file_path null");
262         nErr = alarmdb_init(NULL);
263         KIES_RETVM_IF(nErr != SUCCESS, FAILED, "alarmdb_init");
264         db_success = SUCCESS;
265         nErr = alarmmgr_init(PKGNAME);
266         KIES_RETVM_IF(nErr != SUCCESS, FAILED, "alarmmgr_init error");
267         mgr_success = SUCCESS;
268         //get alarm data, because alarmdb is used by many APs
269         list = alarmdb_get_data_list_by_author(ALARM_DB_AUTHOR_ALARM);
270         //delete all alarm data
271         nErr = _reset_alarm(list);
272         KIES_RETVM_IF(nErr != SUCCESS, nErr, "_reset_alarm error");
273         //
274         nErr =
275             _read_alarm_from_file((char *)file_path,
276                                   TYPE_SAVE_ALARM_MODE_BINARY);
277         KIES_RETVM_IF(nErr != SUCCESS, nErr, "_write_alarm_to_file NULL");
278         //notify
279         vconf_set_bool(VCONFKEY_ALARM_RESTORE, true);
280  End:
281         FREE_ADLIST(list);
282         if (IS_EQUAL(db_success, SUCCESS)) {
283                 alarmdb_fini();
284         }
285         if (IS_EQUAL(mgr_success, SUCCESS)) {
286                 alarmmgr_fini();
287         }
288         KIES_FUN_END();
289         return ret;
290 }