4 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
25 #include "cal_typedef.h"
26 #include "cal_internal.h"
27 #include "cal_server_calendar_delete.h"
28 #include "cal_server_service.h"
30 #include "cal_db_util.h"
32 #define CAL_SERVER_CALENDAR_DELETE_COUNT 50
33 #define CAL_SERVER_CALENDAR_DELETE_STEP_TIME 1
34 #define CAL_SERVER_CALENDAR_DELETE_THREAD_NAME "cal_server_calendar_delete"
37 STEP_1, /* create calendar_id_list */
38 STEP_2, /* delete schedule_table <-- CAL_SERVER_CALENDAR_DELETE_COUNT */
39 STEP_3, /* delete calendar_table*/
40 } __calendar_delete_step_e;
43 GList *calendar_id_list;
44 int current_calendar_id;
45 __calendar_delete_step_e step;
46 } __calendar_delete_data_s;
48 GThread *_cal_server_calendar_delete_thread = NULL;
49 GCond _cal_server_calendar_delete_cond;
50 GMutex _cal_server_calendar_delete_mutex;
52 static bool _cal_server_calendar_delete_step(int ret, __calendar_delete_data_s* data);
53 static int _cal_server_calendar_delete_step1(__calendar_delete_data_s* data);
54 static int _cal_server_calendar_delete_step2(__calendar_delete_data_s* data);
55 static int _cal_server_calendar_delete_step3(__calendar_delete_data_s* data);
56 static bool _cal_server_calendar_run(__calendar_delete_data_s* data);
57 static gpointer _cal_server_calendar_main(gpointer user_data);
59 static bool _cal_server_calendar_delete_step(int ret, __calendar_delete_data_s* data)
61 if (CALENDAR_ERROR_NONE != ret && CALENDAR_ERROR_NO_DATA != ret) {
63 ERR("_cal_server_calendar_delete_step Fail(%d)", ret);
64 if (data->calendar_id_list)
65 g_list_free(data->calendar_id_list);
73 if (ret == CALENDAR_ERROR_NO_DATA) {
74 if (data->calendar_id_list)
75 g_list_free(data->calendar_id_list);
82 if (ret == CALENDAR_ERROR_NO_DATA)
97 static int _cal_server_calendar_delete_step1(__calendar_delete_data_s* data)
100 char query[CAL_DB_SQL_MIN_LEN] = {0,};
101 sqlite3_stmt *stmt = NULL;
106 if (NULL == data->calendar_id_list) {
108 snprintf(query, sizeof(query), "SELECT id FROM %s WHERE deleted = 1", CAL_TABLE_CALENDAR);
109 ret = cal_db_util_query_prepare(query, &stmt);
110 if (CALENDAR_ERROR_NONE != ret) {
111 /* LCOV_EXCL_START */
112 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
113 SECURE("query[%s]", query);
117 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
119 id = sqlite3_column_int(stmt, 0);
120 data->calendar_id_list = g_list_append(data->calendar_id_list, GINT_TO_POINTER(id));
122 sqlite3_finalize(stmt);
125 count = g_list_length(data->calendar_id_list);
127 return CALENDAR_ERROR_NO_DATA;
129 GList *cursor = g_list_first(data->calendar_id_list);
131 data->current_calendar_id = GPOINTER_TO_INT(cursor->data);
132 data->calendar_id_list = g_list_remove(data->calendar_id_list, GINT_TO_POINTER(data->current_calendar_id));
134 return CALENDAR_ERROR_NONE;
136 return CALENDAR_ERROR_NO_DATA;
140 static int _cal_server_calendar_delete_step2(__calendar_delete_data_s* data)
142 char query[CAL_DB_SQL_MIN_LEN] = {0};
145 sqlite3_stmt *stmt = NULL;
150 ret = cal_db_util_begin_trans();
151 if (CALENDAR_ERROR_NONE != ret) {
152 /* LCOV_EXCL_START */
153 ERR("cal_db_util_begin_trans() failed");
154 return CALENDAR_ERROR_DB_FAILED;
159 snprintf(query, sizeof(query), "SELECT id FROM %s WHERE calendar_id = %d LIMIT %d",
160 CAL_TABLE_SCHEDULE, data->current_calendar_id, CAL_SERVER_CALENDAR_DELETE_COUNT);
162 ret = cal_db_util_query_prepare(query, &stmt);
163 if (CALENDAR_ERROR_NONE != ret) {
164 /* LCOV_EXCL_START */
165 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
166 SECURE("query[%s]", query);
167 cal_db_util_end_trans(false);
172 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
174 id = sqlite3_column_int(stmt, 0);
175 list = g_list_append(list, GINT_TO_POINTER(id));
177 sqlite3_finalize(stmt);
180 count = g_list_length(list);
182 cal_db_util_end_trans(false);
183 return CALENDAR_ERROR_NO_DATA;
186 GList *cursor = g_list_first(list);
188 int id = GPOINTER_TO_INT(cursor->data);
189 /* delete event table */
190 snprintf(query, sizeof(query), "DELETE FROM %s WHERE id=%d", CAL_TABLE_SCHEDULE, id);
191 ret = cal_db_util_query_exec(query);
192 if (CALENDAR_ERROR_NONE != ret) {
193 /* LCOV_EXCL_START */
194 ERR("cal_db_util_query_exec() Fail(%d)", ret);
195 SECURE("[%s]", query);
196 cal_db_util_end_trans(false);
201 cursor = g_list_next(cursor);
206 cal_db_util_end_trans(true);
208 return CALENDAR_ERROR_NONE;
211 static int _cal_server_calendar_delete_step3(__calendar_delete_data_s* data)
213 int ret = CALENDAR_ERROR_NONE;
214 char query[CAL_DB_SQL_MIN_LEN] = {0};
216 ret = cal_db_util_begin_trans();
217 if (CALENDAR_ERROR_NONE != ret) {
218 /* LCOV_EXCL_START */
219 ERR("cal_db_util_begin_trans() failed");
220 return CALENDAR_ERROR_DB_FAILED;
226 /* delete event table */
227 snprintf(query, sizeof(query), "DELETE FROM %s WHERE id=%d", CAL_TABLE_CALENDAR, data->current_calendar_id);
228 ret = cal_db_util_query_exec(query);
229 if (CALENDAR_ERROR_NONE != ret) {
230 /* LCOV_EXCL_START */
231 ERR("cal_db_util_query_exec() Fail(%d)", ret);
232 SECURE("[%s]", query);
233 cal_db_util_end_trans(false);
237 cal_db_util_end_trans(true);
239 return CALENDAR_ERROR_NONE;
242 static bool _cal_server_calendar_run(__calendar_delete_data_s* data)
244 int ret = CALENDAR_ERROR_NONE;
249 /* LCOV_EXCL_START */
255 switch (data->step) {
257 ret = _cal_server_calendar_delete_step1(data);
260 ret = _cal_server_calendar_delete_step2(data);
263 ret = _cal_server_calendar_delete_step3(data);
266 /* LCOV_EXCL_START */
268 if (data->calendar_id_list)
269 g_list_free(data->calendar_id_list);
276 return _cal_server_calendar_delete_step(ret, data);
280 static gpointer _cal_server_calendar_main(gpointer user_data)
282 int ret = CALENDAR_ERROR_NONE;
286 __calendar_delete_data_s *callback_data = NULL;
287 callback_data = calloc(1, sizeof(__calendar_delete_data_s));
288 if (NULL == callback_data) {
289 /* LCOV_EXCL_START */
290 ERR("calloc() Fail");
295 callback_data->step = STEP_1;
299 if (CALENDAR_ERROR_NONE != ret) {
300 /* LCOV_EXCL_START */
301 ERR("cal_connect() Fail(%d)", ret);
308 sleep(CAL_SERVER_CALENDAR_DELETE_STEP_TIME); /* sleep 1 sec. */
309 ret = _cal_server_calendar_run(callback_data);
311 DBG("_cal_server_calendar_run() return false");
312 callback_data = NULL;
318 CAL_FREE(callback_data);
320 g_mutex_lock(&_cal_server_calendar_delete_mutex);
322 g_cond_wait(&_cal_server_calendar_delete_cond, &_cal_server_calendar_delete_mutex);
323 g_mutex_unlock(&_cal_server_calendar_delete_mutex);
329 void cal_server_calendar_delete_start(void)
333 if (NULL == _cal_server_calendar_delete_thread) {
334 g_mutex_init(&_cal_server_calendar_delete_mutex);
335 g_cond_init(&_cal_server_calendar_delete_cond);
336 _cal_server_calendar_delete_thread = g_thread_new(CAL_SERVER_CALENDAR_DELETE_THREAD_NAME,
337 _cal_server_calendar_main, NULL);
339 /* don't use mutex. */
340 g_cond_signal(&_cal_server_calendar_delete_cond);