3 * Copyright 2012 Samsung Electronics Co., Ltd
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
9 * http://floralicense.org/license/
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.
18 #include "kies_alarm.h"
20 #include "alarm-engine.h"
24 #include <vconf-keys.h>
25 #include "kies_fwk_alarmmgr.h"
26 #include "kies_define.h"
27 /**********************************************************************
28 ******************define *************************************
29 ***********************************************************************/
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 ***********************************************************************/
40 static bool is_file_exist(char *file_path)
46 pFile = fopen(file_path, "r");
54 /**********************************************************************
55 ******************save *************************************
56 ***********************************************************************/
59 static int _write_alarm_to_file_binary(ADList * list, char * file_path)
64 ADList *cur_data = NULL;
65 struct alarm_data *pAlarm = NULL;
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");
85 static int _write_alarm_to_file_text(ADList *list, char *file_path)
92 static int _write_alarm_to_file(ADList *list,
93 char *file_path, TYPE_SAVE_ALARM_MODE save_mode)
98 case TYPE_SAVE_ALARM_MODE_BINARY:
99 ret = _write_alarm_to_file_binary(list, file_path);
101 case TYPE_SAVE_ALARM_MODE_TEXT:
102 ret = _write_alarm_to_file_text(list, file_path);
104 case TYPE_SAVE_ALARM_MODE_MAX:
105 KIES_INFO_RED("type is error, save_mode=%d", save_mode);
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)
118 int db_success = FAILED;
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");
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");
135 if (IS_EQUAL(db_success, SUCCESS)) {
142 /**********************************************************************
143 ******************restore *************************************
144 ***********************************************************************/
146 //clear alarm data in db;
147 //if alarm is enable, remove from alarm-manger
148 static int _reset_alarm(ADList *list)
152 ADList *cur_data = NULL;
153 struct alarm_data *pAlarm = NULL;
155 KIES_RETVM_IF(!list, SUCCESS, "null list, nothing to do");
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");
164 nErr = alarmdb_del_data(pAlarm->id);
165 KIES_RETVM_IF(nErr != SUCCESS, FAILED,
166 "alarmdb_del_data error");
173 //if enable alarm, register alarm
174 //then, add to alarm db
175 static int _save_alarm(struct alarm_data *pAlarm)
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");
185 nErr = alarmdb_add_data(pAlarm);
186 KIES_RETVM_IF(IS_EQUAL(nErr, -1), FAILED, "alarmdb_add_data error");
193 static int _read_alarm_from_file_binary(char *file_path)
199 struct alarm_data alarm;
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");
207 cur_size = fread(&alarm, sizeof(alarm), 1, pFile);
208 if (IS_EQUAL(0, cur_size)) {
209 //file is null, success
212 KIES_RETVM_IF(cur_size != 1, FAILED, "fread size error");
214 nErr = _save_alarm(&alarm);
215 KIES_RETVM_IF(nErr != SUCCESS, FAILED, "_save_alarm error");
224 static int _read_alarm_from_file_text(char *file_path)
231 static int _read_alarm_from_file(char *file_path,
232 TYPE_SAVE_ALARM_MODE save_mode)
237 case TYPE_SAVE_ALARM_MODE_BINARY:
238 ret = _read_alarm_from_file_binary(file_path);
240 case TYPE_SAVE_ALARM_MODE_TEXT:
241 ret = _read_alarm_from_file_text(file_path);
243 case TYPE_SAVE_ALARM_MODE_MAX:
244 KIES_INFO_RED("type is error, save_mode=%d", save_mode);
252 A_DBAPI int alarm_restore(const char *file_path)
256 int db_success = FAILED;
257 int mgr_success = FAILED;
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");
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");
279 vconf_set_bool(VCONFKEY_ALARM_RESTORE, true);
282 if (IS_EQUAL(db_success, SUCCESS)) {
285 if (IS_EQUAL(mgr_success, SUCCESS)) {