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