26ebab02f201af427a7e99881ef22e66d6874d68
[platform/core/system/sync-agent.git] / src / fw-plugins / common-public / task / src / plugin_interface.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 <stdio.h>
19 #include <glib.h>
20 #include <pthread.h>
21 #include <calendar2.h>
22 #include <account.h>
23
24 #include "extern_info.h"
25 #include "item_change_info.h"
26 #include "ext_datastore_info_task.h"
27 #include "in_datastore_info_task.h"
28
29 /* for log */
30 #include "utility/sync_util.h"
31 #include "utility/fw_async_queue.h"
32 #include "plugin/data_connector_interface.h"
33 #include "data-adapter/agent_handler_manager.h"
34
35 #ifndef EXPORT_API
36 #define EXPORT_API __attribute__ ((visibility("default")))
37 #endif
38
39 #ifndef SYNC_AGENT_LOG
40 #undef LOG_TAG
41 #define LOG_TAG "PLUGIN_DA_TASK"
42 #endif
43
44 #define FW_TASK 11
45
46 static sync_agent_util_async_queue_s *queue;
47
48 static int latest_task_version = 0;
49
50 static pthread_mutex_t lockx;
51
52 static int is_noti_from_me = 0;
53
54 static int is_storage_changed = 0;
55
56 static sync_agent_add_item_cb_plugin callback_add_item;
57
58 static sync_agent_del_item_cb_plugin callback_del_item;
59
60 static sync_agent_update_item_cb_plugin callback_update_item;
61
62 static sync_agent_get_account_id_list_cb_plugin callback_get_account_id_list;
63
64 static void _set_is_noti_from_me(int set_flag);
65
66 static int __get_is_noti_from_me(void);
67
68 static void _process_task_change(const char *view_uri, void *user_data);
69
70 static void *_rutine_task_change(void *);
71
72 static sync_agent_da_return_e _convert_service_error_to_common_error(int err);
73
74 static bool _get_account_id(account_h account, void *user_data);
75
76 static bool __is_task_id(const char* capability_type, account_capability_state_e capability_value, void *user_data);
77
78 static sync_agent_da_return_e _get_service_folder_list_by_account_id(int account_id, calendar_list_h * item_list, int *count);
79
80 static sync_agent_da_return_e _get_service_event_list_by_folder_id(int folder_id, calendar_list_h * item_list, int *count);
81
82 typedef struct account_id {
83         int account_id;
84         struct account_id *next;
85 } account_id_s;
86
87 static sync_agent_da_return_e _get_service_event_list_by_folder_id(int folder_id, calendar_list_h * item_list, int *count)
88 {
89         _INNER_FUNC_ENTER;
90
91         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
92         int service_ret = 0;
93
94         unsigned int ids[1];
95         calendar_query_h query = NULL;
96         calendar_filter_h filter = NULL;
97         calendar_list_h list = NULL;
98
99         // start pre condition
100         service_ret = calendar_query_create(_calendar_todo._uri, &query);
101         if (service_ret != CALENDAR_ERROR_NONE) {
102                 _DEBUG_ERROR("[da_task_plugIn] in calendar_query_create Fail!\n");
103                 ret = _convert_service_error_to_common_error(service_ret);
104                 goto DACI_FINISH;
105         }
106         service_ret = calendar_filter_create(_calendar_todo._uri, &filter);
107         if (service_ret != CALENDAR_ERROR_NONE) {
108                 _DEBUG_ERROR("[da_task_plugIn] in calendar_filter_create Fail!\n");
109                 ret = _convert_service_error_to_common_error(service_ret);
110                 goto DACI_FINISH;
111         }
112         service_ret = calendar_filter_add_int(filter, _calendar_todo.calendar_book_id, CALENDAR_MATCH_EQUAL, folder_id);
113         if (service_ret != CALENDAR_ERROR_NONE) {
114                 _DEBUG_ERROR("[da_task_plugIn] in calendar_filter_add_int Fail!\n");
115                 ret = _convert_service_error_to_common_error(service_ret);
116                 goto DACI_FINISH;
117         }
118         // end pre condition
119
120         service_ret = calendar_query_set_filter(query, filter);
121         if (service_ret != CALENDAR_ERROR_NONE) {
122                 _DEBUG_ERROR("[da_task_plugIn] in calendar_query_set_filter Fail!\n");
123                 ret = _convert_service_error_to_common_error(service_ret);
124                 goto DACI_FINISH;
125         }
126
127         ids[0] = _calendar_todo.id;
128         service_ret = calendar_query_set_projection(query, ids, 1);
129         if (service_ret != CALENDAR_ERROR_NONE) {
130                 _DEBUG_ERROR("[da_task_plugIn] in calendar_query_set_projection Fail!\n");
131                 ret = _convert_service_error_to_common_error(service_ret);
132                 goto DACI_FINISH;
133         }
134
135         service_ret = calendar_db_get_records_with_query(query, 0, 0, &list);
136         if (service_ret != CALENDAR_ERROR_NONE) {
137                 _DEBUG_ERROR("[da_task_plugIn] in calendar_db_get_records_with_query Fail!\n");
138                 ret = _convert_service_error_to_common_error(service_ret);
139                 goto DACI_FINISH;
140         }
141         service_ret = calendar_list_get_count(list, count);
142         if (service_ret != CALENDAR_ERROR_NONE) {
143                 _DEBUG_ERROR("[da_task_plugIn] in calendar_list_get_count Fail!\n");
144                 ret = _convert_service_error_to_common_error(service_ret);
145                 goto DACI_FINISH;
146         }
147         _DEBUG_INFO("count(%d)", *count);
148
149         service_ret = calendar_list_first(list);
150         if (service_ret != CALENDAR_ERROR_NONE) {
151                 _DEBUG_ERROR("[da_task_plugIn] in calendar_list_get_count Fail!\n");
152                 ret = _convert_service_error_to_common_error(service_ret);
153                 goto DACI_FINISH;
154         }
155
156  DACI_FINISH:
157         calendar_filter_destroy(filter);
158         calendar_query_destroy(query);
159
160         *item_list = list;
161         return ret;
162         _INNER_FUNC_EXIT;
163 }
164
165 static sync_agent_da_return_e _get_service_folder_list_by_account_id(int account_id, calendar_list_h * item_list, int *count)
166 {
167         _INNER_FUNC_ENTER;
168
169         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
170         int service_ret = 0;
171
172         unsigned int ids[1];
173         calendar_query_h query = NULL;
174         calendar_filter_h filter = NULL;
175         calendar_list_h list = NULL;
176
177         // start pre condition
178         service_ret = calendar_query_create(_calendar_book._uri, &query);
179         if (service_ret != CALENDAR_ERROR_NONE) {
180                 _DEBUG_ERROR("[da_task_plugIn] in calendar_query_create Fail!\n");
181                 ret = _convert_service_error_to_common_error(service_ret);
182                 goto DACI_FINISH;
183         }
184         service_ret = calendar_filter_create(_calendar_book._uri, &filter);
185         if (service_ret != CALENDAR_ERROR_NONE) {
186                 _DEBUG_ERROR("[da_task_plugIn] in calendar_filter_create Fail!\n");
187                 ret = _convert_service_error_to_common_error(service_ret);
188                 goto DACI_FINISH;
189         }
190         service_ret = calendar_filter_add_int(filter, _calendar_book.account_id, CALENDAR_MATCH_EQUAL, account_id);
191         if (service_ret != CALENDAR_ERROR_NONE) {
192                 _DEBUG_ERROR("[da_task_plugIn] in calendar_filter_add_int Fail!\n");
193                 ret = _convert_service_error_to_common_error(service_ret);
194                 goto DACI_FINISH;
195         }
196         // end pre condition
197
198         service_ret = calendar_query_set_filter(query, filter);
199         if (service_ret != CALENDAR_ERROR_NONE) {
200                 _DEBUG_ERROR("[da_task_plugIn] in calendar_query_set_filter Fail!\n");
201                 ret = _convert_service_error_to_common_error(service_ret);
202                 goto DACI_FINISH;
203         }
204
205         ids[0] = _calendar_book.id;
206         service_ret = calendar_query_set_projection(query, ids, 1);
207         if (service_ret != CALENDAR_ERROR_NONE) {
208                 _DEBUG_ERROR("[da_task_plugIn] in calendar_query_set_projection Fail!\n");
209                 ret = _convert_service_error_to_common_error(service_ret);
210                 goto DACI_FINISH;
211         }
212
213         service_ret = calendar_db_get_records_with_query(query, 0, 0, &list);
214         if (service_ret != CALENDAR_ERROR_NONE) {
215                 _DEBUG_ERROR("[da_task_plugIn] in calendar_db_get_records_with_query Fail!\n");
216                 ret = _convert_service_error_to_common_error(service_ret);
217                 goto DACI_FINISH;
218         }
219         service_ret = calendar_list_get_count(list, count);
220         if (service_ret != CALENDAR_ERROR_NONE) {
221                 _DEBUG_ERROR("[da_task_plugIn] in calendar_list_get_count Fail!\n");
222                 ret = _convert_service_error_to_common_error(service_ret);
223                 goto DACI_FINISH;
224         }
225         _DEBUG_INFO("count(%d)", *count);
226
227         service_ret = calendar_list_first(list);
228         if (service_ret != CALENDAR_ERROR_NONE) {
229                 _DEBUG_ERROR("[da_task_plugIn] in calendar_list_get_count Fail!\n");
230                 ret = _convert_service_error_to_common_error(service_ret);
231                 goto DACI_FINISH;
232         }
233
234  DACI_FINISH:
235         calendar_filter_destroy(filter);
236         calendar_query_destroy(query);
237
238         *item_list = list;
239         return ret;
240         _INNER_FUNC_EXIT;
241 }
242
243 EXPORT_API sync_agent_da_return_e sync_agent_plugin_open_service(void)
244 {
245         _EXTERN_FUNC_ENTER;
246
247         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
248         //int err = calendar_connect_on_thread();
249         int err = calendar_connect();
250         if (err != CALENDAR_ERROR_NONE) {
251                 _DEBUG_ERROR("[da_task_plugIn] calendar_connect Fail!\n");
252                 ret = _convert_service_error_to_common_error(err);
253         } else {
254                 _DEBUG_INFO("[da_task_plugIn] calendar_connect Success!\n");
255         }
256
257         _EXTERN_FUNC_EXIT;
258
259         return ret;
260 }
261
262 EXPORT_API sync_agent_da_return_e sync_agent_plugin_close_service(void)
263 {
264         _EXTERN_FUNC_ENTER;
265
266         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
267         int err = calendar_disconnect();
268         //int err = calendar_disconnect_on_thread();
269         if (err != CALENDAR_ERROR_NONE) {
270                 _DEBUG_ERROR("[da_task_plugIn] calendar_disconnect Fail!\n");
271                 ret = _convert_service_error_to_common_error(err);
272         } else {
273                 _DEBUG_INFO("[da_task_plugIn] calendar_disconnect Success!\n");
274         }
275
276         _EXTERN_FUNC_EXIT;
277
278         return ret;
279 }
280
281 EXPORT_API sync_agent_da_return_e sync_agent_plugin_begin_transaction(void)
282 {
283         _EXTERN_FUNC_ENTER;
284
285         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
286         _DEBUG_INFO("No need to care about DB trasaction while using Calendar Service !!!");
287
288         _EXTERN_FUNC_EXIT;
289         return ret;
290 }
291
292 EXPORT_API int sync_agent_plugin_end_transaction(int is_success)
293 {
294         _EXTERN_FUNC_ENTER;
295
296         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
297         _DEBUG_INFO("No need to care about DB trasaction while using Calendar Service !!!");
298
299         _EXTERN_FUNC_EXIT;
300         return ret;
301 }
302
303 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_item(int account_id, char *folder_id, void *data, char **item_id)
304 {
305         _EXTERN_FUNC_ENTER;
306
307         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
308
309         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
310         int service_ret = 0;
311
312         calendar_record_h item = (calendar_record_h) data;
313
314         int un_type = 0;
315         memcpy(&un_type, item, 4);
316         int int_folder_id = atoi(folder_id);
317         _DEBUG_INFO("[da_task_plugIn] calendar type %d, in %d, folder id [%d]", un_type, item, int_folder_id);
318
319         /* setting folder id */
320         service_ret = calendar_record_set_int(item, _calendar_todo.calendar_book_id, int_folder_id);
321         if (service_ret != CALENDAR_ERROR_NONE) {
322                 _DEBUG_ERROR("calendar_record_set_int() Fail! : err = %d", service_ret);
323                 ret = SYNC_AGENT_DA_ERRORS;
324                 goto DACI_FINISH;
325         }
326         _DEBUG_INFO("[da_task_plugIn] calendar_svc_struct_set_int(folder %d) return [%d]!\n", atoi(folder_id), service_ret);
327
328         /* add item */
329         int slp_cal_info_id = 0;
330         service_ret = calendar_db_insert_record(item, &slp_cal_info_id);
331         if (service_ret != CALENDAR_ERROR_NONE) {
332                 _DEBUG_ERROR("calendar_db_insert_record() Fail! : err = %d", service_ret);
333                 ret = _convert_service_error_to_common_error(service_ret);
334                 *item_id = 0;
335                 goto DACI_FINISH;
336         } else {
337                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_insert() Success!");
338                 _DEBUG_INFO("calendar_svc_insert() Success!: id = %d", slp_cal_info_id);
339                 *item_id = g_strdup_printf("%d", slp_cal_info_id);
340                 is_storage_changed = 1;
341         }
342  DACI_FINISH:
343         service_ret = calendar_record_destroy(item, false);
344         if (service_ret != CALENDAR_ERROR_NONE) {
345                 _DEBUG_ERROR("calendar_record_destroy() Fail! : err = %d", service_ret);
346         }
347         item = NULL;
348         _EXTERN_FUNC_EXIT;
349         return ret;
350 }
351
352 EXPORT_API sync_agent_da_return_e sync_agent_plugin_update_item(int account_id, char *folder_id, char *item_id, void *data)
353 {
354         _EXTERN_FUNC_ENTER;
355
356         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
357         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
358
359         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
360         int err = 0;
361         calendar_record_h item = (calendar_record_h) data;
362         int int_folder_id = atoi(folder_id);
363
364         /* setting folder id */
365         int service_ret = calendar_record_set_int(item, _calendar_todo.calendar_book_id, int_folder_id);
366         if (service_ret != CALENDAR_ERROR_NONE) {
367                 _DEBUG_ERROR("calendar_record_set_int() Fail! : err = %d", service_ret);
368                 ret = SYNC_AGENT_DA_ERRORS;
369                 goto DACI_FINISH;
370         }
371
372         /*  update item */
373         err = calendar_db_update_record(item);
374         if (err != CALENDAR_ERROR_NONE) {
375                 _DEBUG_ERROR("[da_task_plugIn] calendar_svc_update() Fail!\n");
376                 ret = _convert_service_error_to_common_error(err);
377         } else {
378                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_update() Success!");
379                 is_storage_changed = 1;
380         }
381
382         /*  memory free */
383  DACI_FINISH:
384         /*  memory free */
385         if (item != NULL) {
386                 calendar_record_destroy(item, false);
387                 if (err != CALENDAR_ERROR_NONE) {
388                         _DEBUG_ERROR("calendar_record_destroy() Fail! : err = %d", err);
389                 }
390                 item = NULL;
391         }
392         _EXTERN_FUNC_EXIT;
393
394         return ret;
395 }
396
397 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_item(int account_id, char *folder_id, char *item_id)
398 {
399         _EXTERN_FUNC_ENTER;
400
401         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
402
403         /*  delete item  */
404         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
405         int err = calendar_db_delete_record(_calendar_todo._uri, atoi(item_id));
406
407         if (err != CALENDAR_ERROR_NONE) {
408                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_delete() Fail!");
409                 ret = _convert_service_error_to_common_error(err);
410         } else {
411                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_delete() Success!");
412                 is_storage_changed = 1;
413         }
414
415         _EXTERN_FUNC_EXIT;
416
417         return ret;
418 }
419
420 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_all_items(int account_id)
421 {
422         _EXTERN_FUNC_ENTER;
423
424         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
425         int err = 1;
426
427         int folder_id_list_cnt = 0;
428         int *folder_type_list;
429         char **folder_id_list = 0;
430         int service_ret = 0;
431
432         int item_id = 0;
433         calendar_list_h event_list = NULL;
434
435         /* get folder id list for account id */
436         folder_id_list = sync_agent_plugin_get_folder_id_list(account_id, &folder_id_list_cnt, &folder_type_list);
437         if (folder_id_list == NULL || folder_id_list_cnt == 0) {
438                 _DEBUG_INFO("[da_task_plugIn] folder id list cnt : %d", folder_id_list_cnt);
439                 if (folder_type_list != NULL) {
440                         cfree(folder_type_list);
441                         folder_type_list = NULL;
442                 }
443                 return SYNC_AGENT_DA_ERR_NO_DATA;
444         }
445
446         int folder_iter = 0;
447         for (; folder_iter < folder_id_list_cnt; folder_iter++) {
448                 int int_folder_id = atoi(folder_id_list[folder_iter]);
449                 _DEBUG_INFO("[da_task_plugIn] folder id : %d\n", int_folder_id);
450                 int event_count = 0;
451                 ret = _get_service_event_list_by_folder_id(int_folder_id, &event_list, &event_count);
452                 if (ret != SYNC_AGENT_DA_SUCCESS) {
453                         _DEBUG_ERROR("_get_service_folder_list_by_account_id failed !!!");
454                         continue;
455                 } else if (event_count == 0) {
456                         _DEBUG_INFO("_get_service_folder_list_by_account_id no list");
457                         continue;
458                 }
459
460                 int event_list_count = 0;
461                 service_ret = calendar_list_get_count(event_list, &event_list_count);
462                 if (service_ret != CALENDAR_ERROR_NONE) {
463                         _DEBUG_ERROR("calendar_list_get_count() Fail! : err = %d", service_ret);
464                         continue;
465                 }
466                 _DEBUG_TRACE("recode list count = %d", event_list_count);
467                 if (event_list_count == 0) {
468                         continue;
469                 }
470
471                 do {
472                         calendar_record_h event = NULL;
473
474                         item_id = -1;
475                         event = NULL;
476                         service_ret = calendar_list_get_current_record_p(event_list, &event);
477                         if (service_ret != CALENDAR_ERROR_NONE) {
478                                 _DEBUG_ERROR("[da_task_plugIn] in calendar_list_get_current_record_p Fail!\n");
479                                 ret = _convert_service_error_to_common_error(service_ret);
480                                 continue;
481                         }
482                         service_ret = calendar_record_get_int(event, _calendar_todo.id, &item_id);
483                         if (service_ret != CALENDAR_ERROR_NONE) {
484                                 _DEBUG_ERROR("[da_task_plugIn] in calendar_record_get_int Fail!\n");
485                                 ret = _convert_service_error_to_common_error(service_ret);
486                                 continue;
487                         }
488                         _DEBUG_INFO("[da_task_plugIn] item id : %d\n", item_id);
489
490                         err = calendar_db_delete_record(_calendar_todo._uri, item_id);
491                         if (err != CALENDAR_ERROR_NONE) {
492                                 _DEBUG_ERROR("[da_task_plugIn] calendar_svc_delete(%d) Fail!\n", item_id);
493                                 ret = _convert_service_error_to_common_error(err);
494                                 goto DACI_FINISH;
495                         } else {
496                                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_delete(%d) Success!\n", item_id);
497                                 is_storage_changed = 1;
498                         }
499
500                 } while (calendar_list_next(event_list) != CALENDAR_ERROR_NO_DATA);
501
502                 if (event_list != NULL) {
503                         err = calendar_list_destroy(event_list, true);
504                         if (err != CALENDAR_ERROR_NONE) {
505                                 _DEBUG_ERROR("calendar_list_destroy() Fail! : err = %d", err);
506                         }
507                         event_list = NULL;
508                 }
509         }
510
511  DACI_FINISH:
512         if (event_list != NULL) {
513                 err = calendar_list_destroy(event_list, true);
514                 if (err != CALENDAR_ERROR_NONE) {
515                         _DEBUG_ERROR("calendar_list_destroy() Fail! : err = %d", err);
516                 }
517                 event_list = NULL;
518         }
519         if (folder_id_list != NULL) {
520                 int cnt = 0;
521                 for (cnt = 0; cnt < folder_id_list_cnt; cnt++) {
522                         if (folder_id_list[cnt] != NULL) {
523                                 free(folder_id_list[cnt]);
524                                 _DEBUG_INFO("[da_task_plugIn] folder id free !! \n");
525                         }
526                 }
527                 cfree(folder_id_list);
528                 _DEBUG_INFO("[da_task_plugIn] folder id list free !! \n");
529         }
530         if (folder_type_list != NULL) {
531                 cfree(folder_type_list);
532                 folder_type_list = NULL;
533         }
534
535         _EXTERN_FUNC_EXIT;
536         return ret;
537 }
538
539 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_item(int account_id, char *folder_id, char *item_id, void **data)
540 {
541         _EXTERN_FUNC_ENTER;
542
543         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
544
545         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
546         int err = 0;
547         calendar_record_h item = 0;
548
549         /* get item */
550         err = calendar_db_get_record(_calendar_todo._uri, atoi(item_id), &item);
551         if (err != CALENDAR_ERROR_NONE) {
552                 _DEBUG_INFO("[da_task_plugIn] calendar_db_get_record() Fail!\n");
553                 ret = _convert_service_error_to_common_error(err);
554                 *data = 0;
555         } else {
556                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_get() Success!\n");
557                 *data = (void *)item;
558         }
559
560         _EXTERN_FUNC_EXIT;
561
562         return ret;
563 }
564
565 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_folder(int account_id, char *folder_name, int folder_type, char **folder_id)
566 {
567         _EXTERN_FUNC_ENTER;
568
569         retvm_if(folder_name == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_name is NULL. FAIL !!!");
570
571         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
572         int err = 0;
573         calendar_record_h folder = 0;
574
575         /* new service struct */
576         calendar_record_create(_calendar_book._uri, &folder);
577         if (folder == NULL) {
578                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_struct_new() Fail!\n");
579                 *folder_id = 0;
580                 return SYNC_AGENT_DA_ERRORS;
581         }
582
583         /* setting folder data */
584         int cal_ret = calendar_record_set_int(folder, _calendar_book.account_id, account_id);
585         if (cal_ret != CALENDAR_ERROR_NONE) {
586                 ret = SYNC_AGENT_DA_ERRORS;
587                 goto DACI_FINISH;
588         }
589         cal_ret = calendar_record_set_int(folder, _calendar_book.visibility, 1);
590         if (cal_ret != CALENDAR_ERROR_NONE) {
591                 ret = SYNC_AGENT_DA_ERRORS;
592                 goto DACI_FINISH;
593         }
594         cal_ret = calendar_record_set_str(folder, _calendar_book.name, folder_name);
595         if (cal_ret != CALENDAR_ERROR_NONE) {
596                 ret = SYNC_AGENT_DA_ERRORS;
597                 goto DACI_FINISH;
598         }
599
600         /* add folder */
601         int service_folder_item_id = 0;
602         cal_ret = calendar_db_insert_record(folder, &service_folder_item_id);
603         if (cal_ret != CALENDAR_ERROR_NONE) {
604                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_insert() Fail!\n");
605                 ret = _convert_service_error_to_common_error(cal_ret);
606                 *folder_id = 0;
607         } else {
608                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_insert() Success!");
609                 *folder_id = g_strdup_printf("%d", service_folder_item_id);
610                 is_storage_changed = 1;
611         }
612  DACI_FINISH:
613         /*  memory free */
614         if (folder != NULL) {
615                 calendar_record_destroy(folder, false);
616                 if (err != CALENDAR_ERROR_NONE) {
617                         _DEBUG_ERROR("calendar_record_destroy() Fail! : err = %d", err);
618                 }
619                 folder = NULL;
620         }
621         _EXTERN_FUNC_EXIT;
622
623         return ret;
624 }
625
626 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_folder(int account_id, char *folder_id)
627 {
628         _EXTERN_FUNC_ENTER;
629
630         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
631
632         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
633         int err = 0;
634
635         /* delete folder */
636         err = calendar_db_delete_record(_calendar_book._uri, atoi(folder_id));
637         if (err != CALENDAR_ERROR_NONE) {
638                 _DEBUG_INFO("[da_task_plugIn] calendar_db_delete_record() Fail, err[%d]", err);
639                 ret = _convert_service_error_to_common_error(err);
640         } else {
641                 _DEBUG_INFO("[da_task_plugIn] calendar_svc_delete() Success!");
642                 calendar_db_clean_after_sync(atoi(folder_id), 0);
643                 is_storage_changed = 1;
644         }
645
646         _EXTERN_FUNC_EXIT;
647
648         return ret;
649 }
650
651 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_folder(int account_id, char *folder_id, char **out_folder_name, int *out_folder_type)
652 {
653         _EXTERN_FUNC_ENTER;
654
655         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
656
657         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
658         int err = 0;
659         calendar_record_h folder = 0;
660
661         /* get item */
662         int task_folder_id = atoi(folder_id);
663         _DEBUG_INFO("[da_task_plugIn] task_folder_id : %d\n", task_folder_id);
664         err = calendar_db_get_record(_calendar_book._uri, task_folder_id, &folder);
665         if (err != CALENDAR_ERROR_NONE) {
666                 _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_addressbook() Fail!\n");
667                 ret = _convert_service_error_to_common_error(err);
668                 *out_folder_name = 0;
669                 *out_folder_type = -1;
670         } else {
671                 _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_addressbook() Success!\n");
672                 *out_folder_name = 0;
673                 calendar_record_get_str(folder, _calendar_book.name, out_folder_name);
674                 *out_folder_type = DEFAULT_CALENDAR_FOLDER;
675         }
676
677         /*  memory free */
678         if (folder != NULL) {
679                 calendar_record_destroy(folder, false);
680                 if (err != CALENDAR_ERROR_NONE) {
681                         _DEBUG_ERROR("calendar_record_destroy() Fail! : err = %d", err);
682                 }
683                 folder = NULL;
684         }
685         _EXTERN_FUNC_EXIT;
686
687         return ret;
688 }
689
690 EXPORT_API sync_agent_da_return_e sync_agent_plugin_execute(int account_ID, const char *execute_key, void *execute_values, void **result)
691 {
692         _EXTERN_FUNC_ENTER;
693
694         _DEBUG_INFO("[da_task_plugIn] service is not supported execute. \n");
695         *result = 0;
696
697         _EXTERN_FUNC_EXIT;
698
699         return SYNC_AGENT_DA_SUCCESS;
700 }
701
702 EXPORT_API int sync_agent_plugin_get_used_item_count()
703 {
704         _EXTERN_FUNC_ENTER;
705
706         int ret = 0;
707         int used_cnt = 0;
708         int err = calendar_db_get_count(_calendar_todo._uri, &used_cnt);
709         if (err != CALENDAR_ERROR_NONE) {
710                 _DEBUG_INFO("calendar_db_get_count() Fail\n");
711         } else {
712                 _DEBUG_INFO("calendar_db_get_count() Success\n");
713                 _DEBUG_INFO("used_count = %d\n", used_cnt);
714                 ret = used_cnt;
715         }
716
717         _EXTERN_FUNC_EXIT;
718
719         return ret;
720 }
721
722 EXPORT_API int sync_agent_plugin_get_used_item_count_for_folder(int account_id, char *folder_id)
723 {
724         _EXTERN_FUNC_ENTER;
725
726         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
727
728         int ret = 0;
729
730         calendar_filter_h filter = NULL;
731         calendar_query_h query = NULL;
732         ret = calendar_query_create(_calendar_todo._uri, &query);
733         // end pre condition
734         ret = calendar_filter_create(_calendar_todo._uri, &filter);
735         ret = calendar_filter_add_int(filter, _calendar_todo.calendar_book_id, CALENDAR_MATCH_EQUAL, atoi(folder_id));
736         // end pre condition
737
738         ret = calendar_query_set_filter(query, filter);
739
740         int used_cnt = 0;
741         ret = calendar_db_get_count_with_query(query, &used_cnt);
742         if (ret != CALENDAR_ERROR_NONE) {
743                 _DEBUG_ERROR("[da_task_plugIn] calendar_db_get_count() Fail\n");
744         } else {
745                 _DEBUG_INFO("[da_task_plugIn] calendar_db_get_count() Success\n");
746                 _DEBUG_INFO("[da_task_plugIn] used_count = %d\n", used_cnt);
747         }
748
749         if (filter != NULL)
750                 calendar_filter_destroy(filter);
751         if (query != NULL)
752                 calendar_query_destroy(query);
753
754         _EXTERN_FUNC_EXIT;
755
756         return used_cnt;
757 }
758
759 EXPORT_API char **sync_agent_plugin_get_folder_id_list(int account_id, int *folder_count, int **folder_type_list)
760 {
761         _EXTERN_FUNC_ENTER;
762
763         char **folder_id_list = NULL;
764         int count = 0;
765         calendar_list_h service_folder_list = 0;
766         calendar_record_h folder = NULL;
767         int err = 0;
768         int i = 0;
769         *folder_count = 0;
770
771         _DEBUG_INFO("account_id [%d]", account_id);
772
773         err = _get_service_folder_list_by_account_id(account_id, &service_folder_list, &count);
774         if (err != SYNC_AGENT_DA_SUCCESS || count == 0) {
775                 _DEBUG_ERROR("_get_service_folder_list_by_account_id failed !!!");
776                 return NULL;
777         }
778         if (count > 0) {
779                 folder_id_list = (char **)calloc(count, sizeof(char *));
780                 if (folder_id_list == NULL) {
781                         _DEBUG_ERROR("CALLOC failed !!!");
782                         if (service_folder_list != NULL) {
783                                 err = calendar_list_destroy(service_folder_list, true);
784                                 if (err != CALENDAR_ERROR_NONE) {
785                                         _DEBUG_ERROR("calendar_list_destroy() Fail! : err = %d", err);
786                                 }
787                                 service_folder_list = NULL;
788                         }
789                         return NULL;
790                 }
791                 *folder_type_list = (int *)calloc(count, sizeof(int));
792                 if (*folder_type_list == NULL) {
793                         _DEBUG_ERROR("CALLOC failed !!!");
794                         if (folder_id_list) {
795                                 free(folder_id_list);
796                                 folder_id_list = NULL;
797                         }
798                         if (service_folder_list != NULL) {
799                                 err = calendar_list_destroy(service_folder_list, true);
800                                 if (err != CALENDAR_ERROR_NONE) {
801                                         _DEBUG_ERROR("calendar_list_destroy() Fail! : err = %d", err);
802                                 }
803                                 service_folder_list = NULL;
804                         }
805                         return NULL;
806                 }
807
808                 /* find */
809                 do {
810                         /* get */
811                         err = calendar_list_get_current_record_p(service_folder_list, &folder);
812                         if ((err != CALENDAR_ERROR_NONE) && (folder == NULL)) {
813                                 _DEBUG_ERROR("calendar_list_get_current_record_p() Fail! : err = %d", err);
814                                 break;
815                         }
816
817                         /* check */
818                         int int_folder_id = 0;
819                         err = calendar_record_get_int(folder, _calendar_book.id, &int_folder_id);
820                         folder_id_list[i] = g_strdup_printf("%d", int_folder_id);
821                         (*folder_type_list)[i] = DEFAULT_CALENDAR_FOLDER;
822                         i++;
823                         *folder_count = i;
824
825 //                      if (folder != NULL){
826 //                              calendar_record_destroy(folder, false);
827 //                              if (err != CALENDAR_ERROR_NONE) {
828 //                                      _DEBUG_ERROR("calendar_record_destroy() Fail! : err = %d", err);
829 //                              }
830 //                              folder = NULL;
831 //                      }
832
833                 } while (calendar_list_next(service_folder_list) != CALENDAR_ERROR_NO_DATA);
834         }
835         if (service_folder_list != NULL) {
836                 err = calendar_list_destroy(service_folder_list, true);
837                 if (err != CALENDAR_ERROR_NONE) {
838                         _DEBUG_ERROR("calendar_list_destroy() Fail! : err = %d", err);
839                 }
840                 service_folder_list = NULL;
841         }
842         _EXTERN_FUNC_EXIT;
843
844         return folder_id_list;
845 }
846
847 EXPORT_API int *sync_agent_plugin_get_account_id_list(int *count)
848 {
849         _EXTERN_FUNC_ENTER;
850
851         _DEBUG_INFO("[da_task_plugIn] start!!\n");
852
853         int error_code = account_connect();
854
855         account_id_s *account_info_list = (account_id_s *) calloc(1, sizeof(account_id_s));
856         if (account_info_list == NULL) {
857                 _DEBUG_ERROR("CALLOC failed !!!");
858                 return NULL;
859         }
860         account_info_list->account_id = -1;
861         account_info_list->next = 0;
862
863         account_foreach_account_from_db(_get_account_id, account_info_list);
864
865         *count = 0;
866         account_id_s *cursor_ptr = account_info_list;
867         while (cursor_ptr != 0) {
868                 cursor_ptr = cursor_ptr->next;
869                 (*count)++;
870         }
871
872         int *id_list = 0;
873
874         if ((*count) > 0) {
875                 id_list = (int *)calloc(*count, sizeof(int));
876                 if (id_list == NULL) {
877                         _DEBUG_ERROR("CALLOC failed !!!");
878                         if (account_info_list) {
879                                 free(account_info_list);
880                                 account_info_list = NULL;
881                         }
882                         return NULL;
883                 }
884                 cursor_ptr = account_info_list;
885                 int i = 0;
886                 while (cursor_ptr != 0) {
887                         id_list[i] = cursor_ptr->account_id;
888                         cursor_ptr = cursor_ptr->next;
889                         i++;
890                 }
891         }
892
893         free(account_info_list);
894
895         error_code = account_disconnect();
896
897         _DEBUG_INFO("[da_task_plugIn] end!!\n");
898
899         _EXTERN_FUNC_EXIT;
900
901         return id_list;
902 }
903
904 EXPORT_API sync_agent_plugin_item_node_s *sync_agent_plugin_get_changed_item_for_folder_add(int account_id, const char *folder_id, int changepoint, int *changeCount)
905 {
906         _EXTERN_FUNC_ENTER;
907
908         _DEBUG_INFO("[da_task_plugIn] account_id : %d\n", account_id);
909         _DEBUG_INFO("[da_task_plugIn] folder_id : %s\n", folder_id);
910         _DEBUG_INFO("[da_task_plugIn] changepoint : %d\n", changepoint);
911         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
912         int taskTimeStamp = changepoint;
913         int current_change_point = 0;
914         int temp_folder_id = atoi(folder_id);
915         int service_ret = 0;
916
917         sync_agent_plugin_item_node_s *root_ptr = 0;
918         sync_agent_plugin_item_node_s *cursor_ptr = 0;
919         int chagned_count = 0;
920
921         calendar_list_h service_changed_list = 0;
922         calendar_record_h changed_task = NULL;
923
924         service_ret = calendar_db_get_changes_by_version(_calendar_todo._uri, temp_folder_id, taskTimeStamp, &service_changed_list, &current_change_point);
925         _DEBUG_INFO("[da_task_plugIn] calendar_db_get_changes_by_version service_ret : %d, input folder id [%d], current change point [%d]", service_ret, temp_folder_id, current_change_point);
926         if ((service_ret != CALENDAR_ERROR_NONE) && (service_changed_list == NULL)) {
927                 _DEBUG_ERROR("calendar_db_get_changes_by_version() Fail! : err = %d", service_ret);
928                 ret = _convert_service_error_to_common_error(service_ret);
929                 goto DACI_FINISH;
930         }
931
932         int list_count = 0;
933         service_ret = calendar_list_get_count(service_changed_list, &list_count);
934         if (service_ret != CALENDAR_ERROR_NONE) {
935                 _DEBUG_ERROR("[da_task_plugIn] in calendar_list_get_count Fail!\n");
936                 ret = _convert_service_error_to_common_error(service_ret);
937                 goto DACI_FINISH;
938         }
939
940         _DEBUG_INFO("[da_task_plugIn] in calendar_list_get_count [%d]!", list_count);
941         if (list_count <= 0) {
942 //              ret =SYNC_AGENT_DA_ERR_NO_DATA;
943                 goto DACI_FINISH;
944         }
945 //      service_ret = calendar_svc_iter_next(iter);
946         service_ret = calendar_list_first(service_changed_list);
947         if (service_ret != CALENDAR_ERROR_NONE) {
948                 _DEBUG_ERROR("calendar_list_first() Fail! : err = %d", service_ret);
949                 ret = SYNC_AGENT_DA_ERRORS;
950                 goto DACI_FINISH;
951         }
952
953         _DEBUG_INFO("[da_task_plugIn] calendar_svc_iter_next service_ret : %d\n", service_ret);
954
955         do {
956                 _DEBUG_INFO("while start");
957
958                 service_ret = calendar_list_get_current_record_p(service_changed_list, &changed_task);
959                 if ((service_ret != CALENDAR_ERROR_NONE) && (changed_task == NULL)) {
960                         _DEBUG_ERROR("calendar_list_get_current_record_p() Fail! : err = %d", service_ret);
961                         ret = SYNC_AGENT_DA_ERRORS;
962                         goto DACI_FINISH;
963                 }
964                 int type = 0;
965                 service_ret = calendar_record_get_int(changed_task, _calendar_updated_info.modified_status, &type);
966                 if (service_ret != CALENDAR_ERROR_NONE) {
967                         _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
968                         break;
969                 }
970
971                 _DEBUG_INFO("[da_task_plugIn] type is [%d]", type);
972                 if (type == CALENDAR_EVENT_MODIFIED_STATUS_INSERTED) {
973                         int task_id = 0;
974                         service_ret = calendar_record_get_int(changed_task, _calendar_updated_info.id, &task_id);
975                         if (service_ret != CALENDAR_ERROR_NONE) {
976                                 _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
977                                 break;
978                         }
979                         _DEBUG_INFO("[da_task_plugIn] calendar id is [%d]", task_id);
980                         char *task_id_str = g_strdup_printf("%d", task_id);
981                         _DEBUG_INFO("[da_task_plugIn] CALS_UPDATED_TYPE_INSERTED : [%s]\n", task_id_str);
982
983                         if (root_ptr == NULL) {
984                                 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
985                                 if (root_ptr == NULL) {
986                                         _DEBUG_ERROR("CALLOC failed !!!");
987                                         return NULL;
988                                 }
989                                 root_ptr->item_id = task_id_str;
990                                 root_ptr->next = 0;
991                                 cursor_ptr = root_ptr;
992                         } else {
993                                 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
994                                 if (cursor_ptr->next == NULL) {
995                                         _DEBUG_ERROR("CALLOC failed !!!");
996                                         return NULL;
997                                 }
998                                 cursor_ptr->next->item_id = task_id_str;
999                                 cursor_ptr->next->next = 0;
1000                                 cursor_ptr = cursor_ptr->next;
1001                         }
1002
1003                         chagned_count++;
1004
1005                 }
1006
1007                 /*if list can successfully free child than dont need this free */
1008 //              if (changed_task != NULL)
1009 //                      calendar_record_destroy(changed_task, true);
1010         } while (calendar_list_next(service_changed_list) != CALENDAR_ERROR_NO_DATA);
1011
1012  DACI_FINISH:
1013
1014         if (service_changed_list != NULL) {
1015                 calendar_list_destroy(service_changed_list, true);
1016                 service_changed_list = NULL;
1017         }
1018
1019         *changeCount = chagned_count;
1020
1021         _EXTERN_FUNC_EXIT;
1022
1023         return root_ptr;
1024 }
1025
1026 EXPORT_API sync_agent_plugin_item_node_s *sync_agent_plugin_get_changed_item_for_folder_delete(int account_id, const char *folder_id, int change_point, int *change_count)
1027 {
1028         _EXTERN_FUNC_ENTER;
1029
1030         _DEBUG_INFO("[da_task_plugIn] account_id : %d\n", account_id);
1031         _DEBUG_INFO("[da_task_plugIn] folder_id : %s\n", folder_id);
1032         _DEBUG_INFO("[da_task_plugIn] changepoint : %d\n", change_point);
1033         int current_change_point = 0;
1034         int task_time_stamp = change_point;
1035         int temp_folder_id = atoi(folder_id);
1036         int service_ret = 0;
1037         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1038         sync_agent_plugin_item_node_s *root_ptr = 0;
1039         sync_agent_plugin_item_node_s *cursor_ptr = 0;
1040         int chagned_count = 0;
1041
1042         calendar_list_h service_changed_list = 0;
1043         calendar_record_h changed_task = NULL;
1044
1045         service_ret = calendar_db_get_changes_by_version(_calendar_todo._uri, temp_folder_id, task_time_stamp, &service_changed_list, &current_change_point);
1046         _DEBUG_INFO("[da_task_plugIn] calendar_db_get_changes_by_version service_ret : %d, input folder id [%d], current change point [%d]", service_ret, temp_folder_id, current_change_point);
1047         if ((service_ret != CALENDAR_ERROR_NONE) && (service_changed_list == NULL)) {
1048                 _DEBUG_ERROR("calendar_db_get_changes_by_version() Fail! : err = %d", service_ret);
1049                 ret = _convert_service_error_to_common_error(service_ret);
1050                 goto DACI_FINISH;
1051         }
1052
1053         int list_count = 0;
1054         service_ret = calendar_list_get_count(service_changed_list, &list_count);
1055         if (service_ret != CALENDAR_ERROR_NONE) {
1056                 _DEBUG_ERROR("[da_task_plugIn] in calendar_list_get_count Fail!\n");
1057                 ret = _convert_service_error_to_common_error(service_ret);
1058                 goto DACI_FINISH;
1059         }
1060
1061         _DEBUG_INFO("[da_task_plugIn] in calendar_list_get_count [%d]!", list_count);
1062         if (list_count <= 0) {
1063                 goto DACI_FINISH;
1064         }
1065
1066         service_ret = calendar_list_first(service_changed_list);
1067         if (service_ret != CALENDAR_ERROR_NONE) {
1068                 _DEBUG_ERROR("calendar_list_first() Fail! : err = %d", service_ret);
1069                 ret = SYNC_AGENT_DA_ERRORS;
1070                 goto DACI_FINISH;
1071         }
1072         _DEBUG_INFO("[da_task_plugIn] service_ret : %d\n", service_ret);
1073
1074         do {
1075
1076                 service_ret = calendar_list_get_current_record_p(service_changed_list, &changed_task);
1077                 if ((service_ret != CALENDAR_ERROR_NONE) && (changed_task == NULL)) {
1078                         _DEBUG_ERROR("calendar_list_get_current_record_p() Fail! : err = %d", service_ret);
1079                         ret = SYNC_AGENT_DA_ERRORS;
1080                         goto DACI_FINISH;
1081                 }
1082
1083                 int type = 0;
1084                 service_ret = calendar_record_get_int(changed_task, _calendar_updated_info.modified_status, &type);
1085                 if (service_ret != CALENDAR_ERROR_NONE) {
1086                         _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
1087                         break;
1088                 }
1089                 if (type == CALENDAR_EVENT_MODIFIED_STATUS_DELETED) {
1090                         int task_id = 0;
1091                         service_ret = calendar_record_get_int(changed_task, _calendar_updated_info.id, &task_id);
1092                         if (service_ret != CALENDAR_ERROR_NONE) {
1093                                 _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
1094                                 break;
1095                         }
1096                         char *task_id_str = g_strdup_printf("%d", task_id);
1097                         _DEBUG_INFO("[da_task_plugIn] CALS_UPDATED_TYPE_DELETED : [%s]\n", task_id_str);
1098
1099                         if (root_ptr == NULL) {
1100                                 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1101                                 if (root_ptr == NULL) {
1102                                         _DEBUG_ERROR("CALLOC failed !!!");
1103                                         return NULL;
1104                                 }
1105                                 root_ptr->item_id = task_id_str;
1106                                 root_ptr->next = 0;
1107                                 cursor_ptr = root_ptr;
1108                         } else {
1109                                 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1110                                 if (cursor_ptr->next == NULL) {
1111                                         _DEBUG_ERROR("CALLOC failed !!!");
1112                                         return NULL;
1113                                 }
1114                                 cursor_ptr->next->item_id = task_id_str;
1115                                 cursor_ptr->next->next = 0;
1116                                 cursor_ptr = cursor_ptr->next;
1117                         }
1118
1119                         chagned_count++;
1120                 }
1121
1122                 /*if list can successfully free child than dont need this free */
1123 //              if (changed_task != NULL)
1124 //                      calendar_record_destroy(changed_task, true);
1125         } while (calendar_list_next(service_changed_list) != CALENDAR_ERROR_NO_DATA);
1126
1127  DACI_FINISH:
1128
1129         if (service_changed_list != NULL) {
1130                 calendar_list_destroy(service_changed_list, true);
1131                 service_changed_list = NULL;
1132         }
1133
1134         *change_count = chagned_count;
1135
1136         _EXTERN_FUNC_EXIT;
1137
1138         return root_ptr;
1139 }
1140
1141 EXPORT_API sync_agent_plugin_item_node_s *sync_agent_plugin_get_changed_item_for_folder_update(int account_id, const char *folder_id, int change_point, int *change_count)
1142 {
1143         _EXTERN_FUNC_ENTER;
1144
1145         _DEBUG_INFO("\n[da_task_plugIn] account_id : %d\n", account_id);
1146         _DEBUG_INFO("[da_task_plugIn] folder_id : %s\n", folder_id);
1147         _DEBUG_INFO("[da_task_plugIn] changepoint : %d\n", change_point);
1148         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1149         int task_time_stamp = change_point;
1150         int temp_folder_id = atoi(folder_id);
1151         int service_ret = 0;
1152
1153         calendar_list_h service_changed_list = 0;
1154         calendar_record_h changed_task = NULL;
1155
1156         sync_agent_plugin_item_node_s *root_ptr = 0;
1157         sync_agent_plugin_item_node_s *cursor_ptr = 0;
1158         int chagned_count = 0;
1159         int current_change_point = 0;
1160         service_ret = calendar_db_get_changes_by_version(_calendar_todo._uri, temp_folder_id, task_time_stamp, &service_changed_list, &current_change_point);
1161         _DEBUG_INFO("[da_task_plugIn] calendar_db_get_changes_by_version service_ret : %d, input folder id [%d], current change point [%d]", service_ret, temp_folder_id, current_change_point);
1162         if ((service_ret != CALENDAR_ERROR_NONE) && (service_changed_list == NULL)) {
1163                 _DEBUG_ERROR("calendar_db_get_changes_by_version() Fail! : err = %d", service_ret);
1164                 ret = _convert_service_error_to_common_error(service_ret);
1165                 goto DACI_FINISH;
1166         }
1167
1168         int list_count = 0;
1169         service_ret = calendar_list_get_count(service_changed_list, &list_count);
1170         if (service_ret != CALENDAR_ERROR_NONE) {
1171                 _DEBUG_ERROR("[da_task_plugIn] in calendar_list_get_count Fail!\n");
1172                 ret = _convert_service_error_to_common_error(service_ret);
1173                 goto DACI_FINISH;
1174         }
1175
1176         _DEBUG_INFO("[da_task_plugIn] in calendar_list_get_count [%d]!", list_count);
1177         if (list_count <= 0) {
1178                 goto DACI_FINISH;
1179         }
1180
1181         service_ret = calendar_list_first(service_changed_list);
1182         if (service_ret != CALENDAR_ERROR_NONE) {
1183                 _DEBUG_ERROR("calendar_list_first() Fail! : err = %d", service_ret);
1184                 ret = SYNC_AGENT_DA_ERRORS;
1185                 goto DACI_FINISH;
1186         }
1187
1188         _DEBUG_INFO("[da_task_plugIn] service_ret : %d\n", service_ret);
1189
1190         do {
1191                 service_ret = calendar_list_get_current_record_p(service_changed_list, &changed_task);
1192                 if ((service_ret != CALENDAR_ERROR_NONE) && (changed_task == NULL)) {
1193                         _DEBUG_ERROR("calendar_list_get_current_record_p() Fail! : err = %d", service_ret);
1194                         ret = SYNC_AGENT_DA_ERRORS;
1195                         goto DACI_FINISH;
1196                 }
1197                 int type = 0;
1198                 service_ret = calendar_record_get_int(changed_task, _calendar_updated_info.modified_status, &type);
1199                 if (service_ret != CALENDAR_ERROR_NONE) {
1200                         _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
1201                         break;
1202                 }
1203
1204                 if (type == CALENDAR_EVENT_MODIFIED_STATUS_UPDATED) {
1205                         int task_id = 0;
1206                         service_ret = calendar_record_get_int(changed_task, _calendar_updated_info.id, &task_id);
1207                         if (service_ret != CALENDAR_ERROR_NONE) {
1208                                 _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
1209                                 break;
1210                         }
1211                         char *task_id_str = g_strdup_printf("%d", task_id);
1212                         _DEBUG_INFO("[da_task_plugIn] CALS_UPDATED_TYPE_MODIFIED : [%s]\n", task_id_str);
1213
1214                         if (root_ptr == NULL) {
1215                                 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1216                                 if (root_ptr == NULL) {
1217                                         _DEBUG_ERROR("CALLOC failed !!!");
1218                                         return NULL;
1219                                 }
1220                                 root_ptr->item_id = task_id_str;
1221                                 root_ptr->next = 0;
1222                                 cursor_ptr = root_ptr;
1223                         } else {
1224                                 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1225                                 if (cursor_ptr->next == NULL) {
1226                                         _DEBUG_ERROR("CALLOC failed !!!");
1227                                         return NULL;
1228                                 }
1229                                 cursor_ptr->next->item_id = task_id_str;
1230                                 cursor_ptr->next->next = 0;
1231                                 cursor_ptr = cursor_ptr->next;
1232                         }
1233
1234                         chagned_count++;
1235                 }
1236
1237                 /*if list can successfully free child than dont need this free */
1238 //              if (changed_task != NULL)
1239 //                      calendar_record_destroy(changed_task, true);
1240         } while (calendar_list_next(service_changed_list) != CALENDAR_ERROR_NO_DATA);
1241
1242  DACI_FINISH:
1243
1244         if (service_changed_list != NULL) {
1245                 calendar_list_destroy(service_changed_list, true);
1246                 service_changed_list = NULL;
1247         }
1248
1249         *change_count = chagned_count;
1250
1251         _EXTERN_FUNC_EXIT;
1252
1253         return root_ptr;
1254 }
1255
1256 EXPORT_API int sync_agent_plugin_get_last_change_point(void)
1257 {
1258         _EXTERN_FUNC_ENTER;
1259
1260         int task_db_version = 0;
1261         int service_ret = calendar_db_get_current_version(&task_db_version);
1262         if (service_ret != CALENDAR_ERROR_NONE) {
1263                 _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
1264         }
1265         _EXTERN_FUNC_EXIT;
1266
1267         return task_db_version;
1268 }
1269
1270 EXPORT_API void sync_agent_plugin_start_listening_change_noti(void *data)
1271 {
1272         _EXTERN_FUNC_ENTER;
1273
1274         int service_ret = calendar_db_add_changed_cb(_calendar_todo._uri, _process_task_change, NULL);
1275         if (service_ret != CALENDAR_ERROR_NONE) {
1276                 _DEBUG_ERROR("calendar_db_add_changed_cb() Fail!: err = %d", service_ret);
1277         }
1278
1279         latest_task_version = sync_agent_plugin_get_last_change_point();
1280         _DEBUG_INFO("Current Latest Task Version : %d", latest_task_version);
1281
1282         pthread_t thread_process_change_id;
1283         if (pthread_create(&thread_process_change_id, 0, _rutine_task_change, 0) < 0) {
1284                 _DEBUG_ERROR("task Change Noti Handling Thread Create Error");
1285         }
1286
1287         _EXTERN_FUNC_EXIT;
1288 }
1289
1290 EXPORT_API void sync_agent_plugin_set_callback_add_item(sync_agent_add_item_cb_plugin callback)
1291 {
1292         _EXTERN_FUNC_ENTER;
1293
1294         callback_add_item = callback;
1295
1296         _EXTERN_FUNC_EXIT;
1297 }
1298
1299 EXPORT_API void sync_agent_plugin_set_callback_delete_item(sync_agent_del_item_cb_plugin callback)
1300 {
1301         _EXTERN_FUNC_ENTER;
1302
1303         callback_del_item = callback;
1304
1305         _EXTERN_FUNC_EXIT;
1306 }
1307
1308 EXPORT_API void sync_agent_plugin_set_callback_update_item(sync_agent_update_item_cb_plugin callback)
1309 {
1310         _EXTERN_FUNC_ENTER;
1311
1312         callback_update_item = callback;
1313
1314         _EXTERN_FUNC_EXIT;
1315 }
1316
1317 EXPORT_API void sync_agent_plugin_set_callback_get_account_id_list(sync_agent_get_account_id_list_cb_plugin callback)
1318 {
1319         _EXTERN_FUNC_ENTER;
1320
1321         callback_get_account_id_list = callback;
1322
1323         _EXTERN_FUNC_EXIT;
1324 }
1325
1326 EXPORT_API int sync_agent_plugin_get_max_item_count(int folder_type)
1327 {
1328         _EXTERN_FUNC_ENTER;
1329
1330         _EXTERN_FUNC_EXIT;
1331
1332         return MAX_ITEM_COUNT_TASK;
1333 }
1334
1335 EXPORT_API int sync_agent_plugin_get_max_field_length(int field_name, int child_field_name)
1336 {
1337         _EXTERN_FUNC_ENTER;
1338
1339         switch (field_name) {
1340         case CALENDAR_FIELD_SUMMARY:
1341                 _EXTERN_FUNC_EXIT;
1342                 return MAX_LEN_SUMMARY;
1343         case CALENDAR_FIELD_DESCRIPTION:
1344                 _EXTERN_FUNC_EXIT;
1345                 return MAX_LEN_DESCRIPTION;
1346         case CALENDAR_FIELD_LOCATOIN:
1347                 _EXTERN_FUNC_EXIT;
1348                 return MAX_LEN_LOCATION;
1349         case CALENDAR_FIELD_MEMO:
1350                 _EXTERN_FUNC_EXIT;
1351                 return MAX_LEN_MEMO;
1352         default:
1353         _EXTERN_FUNC_EXIT;
1354         return -1;
1355         }
1356 }
1357
1358 EXPORT_API int sync_agent_plugin_get_max_field_count(int field_name, int child_field_name)
1359 {
1360         _EXTERN_FUNC_ENTER;
1361         _EXTERN_FUNC_EXIT;
1362         return -1;
1363 }
1364
1365 EXPORT_API int sync_agent_plugin_get_field_value(int field_name, int child_field_name, char **str_val, int *num_val1, int *num_val2)
1366 {
1367         _EXTERN_FUNC_ENTER;
1368
1369         switch (field_name) {
1370         case CALENDAR_FIELD_YEAR:
1371                 *num_val1 = MIN_PERIOD_YEAR;
1372                 *num_val2 = MAX_PERIOD_YEAR;
1373                 _EXTERN_FUNC_EXIT;
1374                 return 1;
1375         case CALENDAR_FIELD_TYPE_MODEL:
1376                 *str_val = VAL_TYPE_MODEL;
1377                 _EXTERN_FUNC_EXIT;
1378                 return 1;
1379         case CALENDAR_FIELD_DB_INDEX:
1380                 switch (child_field_name) {
1381                 case CALENDAR_FEATURE_APPOINTMENT:
1382                         *num_val1 = VAL_DB_INDEX_APPOINTMENT;
1383                         _EXTERN_FUNC_EXIT;
1384                         return 1;
1385                 case CALENDAR_FEATURE_ANNIVERSARY:
1386                         *num_val1 = VAL_DB_INDEX_ANNIVERSARY;
1387         _EXTERN_FUNC_EXIT;
1388                         return 1;
1389                 case CALENDAR_FEATURE_HOLIDAY:
1390                         *num_val1 = VAL_DB_INDEX_HOLIDAY;
1391                         _EXTERN_FUNC_EXIT;
1392                         return 1;
1393                 case CALENDAR_FEATURE_IMPORTANT:
1394                         *num_val1 = VAL_DB_INDEX_IMPORTANT;
1395                         _EXTERN_FUNC_EXIT;
1396                         return 1;
1397                 case CALENDAR_FEATURE_PRIVATE:
1398                         *num_val1 = VAL_DB_INDEX_PRIVATE;
1399                         _EXTERN_FUNC_EXIT;
1400                         return 1;
1401                 case CALENDAR_FEATURE_MISCELLANEOUS:
1402                         *num_val1 = VAL_DB_INDEX_MISCELLANEOUS;
1403                         _EXTERN_FUNC_EXIT;
1404                         return 1;
1405                 case CALENDAR_FEATURE_TASK:
1406                         *num_val1 = VAL_DB_INDEX_TASK;
1407                         _EXTERN_FUNC_EXIT;
1408                         return 1;
1409                 case CALENDAR_FEATURE_MEMO:
1410                         *num_val1 = VAL_DB_INDEX_MEMO;
1411                         _EXTERN_FUNC_EXIT;
1412                         return 1;
1413                 default:
1414                         return -1;
1415                 }
1416         case CALENDAR_FIELD_TIMEZONE:
1417                 switch (child_field_name) {
1418                 case CALENDAR_CHILD_FIELD_BIAS:
1419                         *num_val1 = (int)VAL_TZ_BIAS;
1420                         _EXTERN_FUNC_EXIT;
1421                         return 1;
1422                 case CALENDAR_CHILD_FIELD_DAYLIGHT:
1423                         *num_val1 = (int)VAL_TZ_DAYLIGHT;
1424                         _EXTERN_FUNC_EXIT;
1425                         return 1;
1426                 case CALENDAR_CHILD_FIELD_DAYLIGHT_BIAS:
1427                         *num_val1 = (int)VAL_TZ_DAYLIGHT_BIAS;
1428                         _EXTERN_FUNC_EXIT;
1429                         return 1;
1430                 case CALENDAR_CHILD_FIELD_DAYLIGHT_BEGIN:
1431                         *str_val = VAL_TZ_DAYLIGHT_BEGIN;
1432                         _EXTERN_FUNC_EXIT;
1433                         return 1;
1434                 case CALENDAR_CHILD_FIELD_DAYLIGHT_END:
1435                         *str_val = VAL_TZ_DAYLIGHT_END;
1436                         _EXTERN_FUNC_EXIT;
1437                         return 1;
1438                 case CALENDAR_CHILD_FIELD_STANDARD_TIMENAME:
1439                         *str_val = VAL_TZ_STANDARD_TIMENAME;
1440                         _EXTERN_FUNC_EXIT;
1441                         return 1;
1442                 case CALENDAR_CHILD_FIELD_DAYLIGHT_TIMENAME:
1443                         *str_val = VAL_TZ_DAYLIGHT_TIMENAME;
1444                         _EXTERN_FUNC_EXIT;
1445                         return 1;
1446                 default:
1447                         return -1;
1448                 }
1449         default:
1450                 return -1;
1451         }
1452 }
1453
1454 EXPORT_API int sync_agent_plugin_get_is_support_feature(int feature)
1455 {
1456         _EXTERN_FUNC_ENTER;
1457
1458         switch (feature) {
1459         case CALENDAR_FEATURE_APPOINTMENT:
1460                 _EXTERN_FUNC_EXIT;
1461                 return IF_SUPPORT_APPOINTMENT;
1462         case CALENDAR_FEATURE_ANNIVERSARY:
1463                 _EXTERN_FUNC_EXIT;
1464                 return IF_SUPPORT_ANNIVERSARY;
1465         case CALENDAR_FEATURE_HOLIDAY:
1466         _EXTERN_FUNC_EXIT;
1467                 return IF_SUPPORT_HOLIDAY;
1468         case CALENDAR_FEATURE_IMPORTANT:
1469                 _EXTERN_FUNC_EXIT;
1470                 return IF_SUPPORT_IMPORTANT;
1471         case CALENDAR_FEATURE_PRIVATE:
1472                 _EXTERN_FUNC_EXIT;
1473                 return IF_SUPPORT_PRIVATE;
1474         case CALENDAR_FEATURE_MISCELLANEOUS:
1475                 _EXTERN_FUNC_EXIT;
1476                 return IF_SUPPORT_MISCELLANEOUS;
1477         case CALENDAR_FEATURE_TASK:
1478                 _EXTERN_FUNC_EXIT;
1479                 return IF_SUPPORT_TASK;
1480         case CALENDAR_FEATURE_MEMO:
1481                 _EXTERN_FUNC_EXIT;
1482                 return IF_SUPPORT_MEMO;
1483         default:
1484         return -1;
1485         }
1486 }
1487
1488 /********************************** static function *******************************************/
1489
1490 static void _set_is_noti_from_me(int set_flag)
1491 {
1492         _INNER_FUNC_ENTER;
1493
1494         if (pthread_mutex_lock(&lockx)) {
1495                 _DEBUG_ERROR("[da_task_plugIn] pthread_mutex_lock error");
1496         }
1497
1498         is_noti_from_me = set_flag;
1499
1500         if (pthread_mutex_unlock(&lockx)) {
1501                 _DEBUG_ERROR("[da_task_plugIn] pthread_mutex_unlock error");
1502         }
1503
1504         _INNER_FUNC_EXIT;
1505 }
1506
1507 static int __get_is_noti_from_me(void)
1508 {
1509         _INNER_FUNC_ENTER;
1510
1511         _INNER_FUNC_EXIT;
1512
1513         return is_noti_from_me;
1514 }
1515
1516 void _process_task_change(const char *view_uri, void *user_data)
1517 {
1518         _INNER_FUNC_ENTER;
1519
1520         _DEBUG_TRACE("[da_task_plugIn] detected task storage changed\n");
1521
1522         int from_Me = __get_is_noti_from_me();
1523         if (from_Me == 1) {
1524                 _set_is_noti_from_me(0);
1525                 _DEBUG_TRACE("[da_task_plugIn] This noti is from Me!! so will be ignored!!\n");
1526                 return;
1527         }
1528
1529         int *taskVersion = (int *)calloc(1, sizeof(int));
1530         if (taskVersion == NULL) {
1531                 _DEBUG_ERROR("CALLOC failed !!!");
1532                 return;
1533         }
1534         *taskVersion = latest_task_version;
1535         latest_task_version++;
1536
1537         _DEBUG_INFO("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
1538         if (sync_agent_async_queue_length(queue) < 1) {
1539                 sync_agent_send_msg_async_queue(queue, (void *)taskVersion);
1540                 _DEBUG_INFO("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
1541         }
1542
1543         _INNER_FUNC_EXIT;
1544 }
1545
1546 static void *_rutine_task_change(void *data)
1547 {
1548         _INNER_FUNC_ENTER;
1549
1550         _DEBUG_TRACE("[da_task_plugIn] Start __rutine_task_Change (create thread)\n");
1551
1552         /*
1553          * Create Queue
1554          */
1555         queue = sync_agent_alloc_async_queue();
1556         if (queue == NULL) {
1557                 _DEBUG_ERROR("Failed to call fw_async_queue_alloc()");
1558                 return 0;
1559         }
1560
1561         calendar_list_h folder_list = NULL;
1562         calendar_list_h record_list = NULL;
1563         int service_ret = 0;
1564
1565         // open task service
1566         sync_agent_plugin_open_service();
1567
1568         while (1) {
1569                 _DEBUG_TRACE("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
1570                 int *temp_task_version = sync_agent_receive_msg_async_queue(queue);
1571                 int task_version = *temp_task_version;
1572                 free(temp_task_version);
1573
1574                 _DEBUG_TRACE("Detected Change taskVersion : %d", task_version);
1575
1576                 int account_list_count;
1577                 int *account_list = callback_get_account_id_list(FW_TASK, &account_list_count);
1578                 if (account_list == NULL)
1579                         return 0;
1580
1581                 int i = 0;
1582                 for (; i < account_list_count; i++) {
1583                         int task_account_id = account_list[i];
1584                         _DEBUG_TRACE("[da_task_plugIn] task_account_id : %d, count : %d\n", task_account_id, account_list_count);
1585                         _DEBUG_TRACE("[da_task_plugIn] Before calendar_svc_event_get_changes : %d\n", task_version);
1586
1587                         /*get folder id */
1588                         int folder_list_count = 0;
1589                         service_ret = _get_service_folder_list_by_account_id(task_account_id, &folder_list, &folder_list_count);
1590                         if (service_ret != SYNC_AGENT_DA_SUCCESS || folder_list == NULL) {
1591                                 _DEBUG_ERROR("calendar_list_get_count() Fail! : err = %d", service_ret);
1592                                 break;
1593                         }
1594                         _DEBUG_TRACE("[da_task_plugIn] folder_list_count : %d", folder_list_count);
1595
1596                         do {
1597
1598                                 calendar_record_h folder = NULL;
1599                                 service_ret = calendar_list_get_current_record_p(folder_list, &folder);
1600                                 if ((service_ret != CALENDAR_ERROR_NONE) && (folder == NULL)) {
1601                                         _DEBUG_ERROR("calendar_list_get_current_record_p() Fail! : err = %d", service_ret);
1602                                         break;
1603                                 }
1604
1605                                 int task_folder_id = 0;
1606                                 service_ret = calendar_record_get_int(folder, _calendar_book.id, &task_folder_id);
1607                                 if (service_ret != CALENDAR_ERROR_NONE) {
1608                                         _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
1609                                         break;
1610                                 }
1611
1612                                 int current_cal_db_ver = 0;
1613                                 service_ret = calendar_db_get_changes_by_version(_calendar_todo._uri, task_folder_id, task_version, &record_list, &current_cal_db_ver);
1614                                 if (service_ret != CALENDAR_ERROR_NONE || record_list == NULL) {
1615                                         _DEBUG_ERROR("calendar_db_get_changes_by_version() Fail!: task_folder_id = %d, err = %d", task_folder_id, service_ret);
1616                                         continue;
1617                                 }
1618                                 _DEBUG_TRACE("calendar_db_get_changes_by_version() Success, task_folderId = %d", task_folder_id);
1619
1620                                 service_ret = calendar_list_first(record_list);
1621                                 if (service_ret != CALENDAR_ERROR_NONE) {
1622                                         _DEBUG_ERROR("calendar_list_first() Fail! : err = %d", service_ret);
1623                                         break;
1624                                 }
1625                                 int recode_count = 0;
1626                                 service_ret = calendar_list_get_count(record_list, &recode_count);
1627                                 if (service_ret != CALENDAR_ERROR_NONE) {
1628                                         _DEBUG_ERROR("calendar_list_get_count() Fail! : err = %d", service_ret);
1629                                         break;
1630                                 }
1631                                 _DEBUG_TRACE("recode list count = %d", recode_count);
1632                                 if (recode_count == 0) {
1633                                         continue;
1634                                 }
1635
1636                                 do {
1637                                         calendar_record_h row_event = NULL;
1638                                         service_ret = calendar_list_get_current_record_p(record_list, &row_event);
1639                                         if ((service_ret != CALENDAR_ERROR_NONE) && (row_event == NULL)) {
1640                                                 _DEBUG_ERROR("calendar_list_get_current_record_p() Fail! : err = %d", service_ret);
1641                                                 break;
1642                                         }
1643
1644                                         int task_id = 0;
1645                                         service_ret = calendar_record_get_int(row_event, _calendar_updated_info.id, &task_id);
1646                                         if (service_ret != CALENDAR_ERROR_NONE) {
1647                                                 _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
1648                                                 break;
1649                                         }
1650
1651                                         _DEBUG_TRACE("recode list count = %d", recode_count);
1652                                         char task_id_str[12];
1653                                         snprintf(task_id_str, sizeof(task_id_str), "%d", task_id);
1654
1655                                         int type = 0;
1656                                         service_ret = calendar_record_get_int(row_event, _calendar_updated_info.modified_status, &type);
1657                                         if (service_ret != CALENDAR_ERROR_NONE) {
1658                                                 _DEBUG_ERROR("calendar_record_get_int() Fail!: err = %d", service_ret);
1659                                                 break;
1660                                         }
1661                                         switch (type) {
1662                                         case CALENDAR_EVENT_MODIFIED_STATUS_INSERTED:
1663                                                 {
1664                                                         char task_folder_id_str[12];
1665                                                         snprintf(task_folder_id_str, sizeof(task_folder_id_str), "%d", task_folder_id);
1666
1667                                                         _DEBUG_TRACE("[da_task_plugIn] task Inserted!\n");
1668                                                         _DEBUG_TRACE("[da_task_plugIn] task folder id : %s\n", task_folder_id_str);
1669                                                         int result = callback_add_item(account_list[i], i, task_id_str, FW_TASK, task_folder_id_str, 0);
1670                                                         if (!result)
1671                                                                 _DEBUG_TRACE("[da_task_plugIn] Failed to call SA_EH_Add_Item() \n");
1672                                                         else
1673                                                                 _DEBUG_TRACE("[da_task_plugIn] Success to call SA_EH_Add_Item() \n");
1674
1675                                                 }
1676                                                 break;
1677                                         case CALENDAR_EVENT_MODIFIED_STATUS_UPDATED:
1678                                                 {
1679                                                         _DEBUG_TRACE("[da_task_plugIn] task Updated!\n");
1680                                                         int result = callback_update_item(account_list[i], i, task_id_str, FW_TASK);
1681                                                         if (!result)
1682                                                                 _DEBUG_TRACE("[da_task_plugIn] Failed to call callback_del_item() \n");
1683                                                 }
1684                                                 break;
1685                                         case CALENDAR_EVENT_MODIFIED_STATUS_DELETED:
1686                                                 {
1687                                                         _DEBUG_TRACE("[da_task_plugIn] task Deleted!\n");
1688                                                         int result = callback_del_item(account_list[i], i, task_id_str, FW_TASK);
1689                                                         if (!result)
1690                                                                 _DEBUG_TRACE("[da_task_plugIn] Failed to call callback_del_item() \n");
1691                                                 }
1692                                                 break;
1693                                         default:
1694                                                 _DEBUG_TRACE("[da_task_plugIn] cached Another task Change Noti!\n");
1695                                                 break;
1696                                         }
1697
1698                                         /*if list can successfully free child than dont need this free */
1699 //                                      if (row_event != NULL){
1700 //                                              calendar_record_destroy(row_event, true);
1701 //                                              row_event = NULL;
1702 //                                      }
1703
1704                                 } while (calendar_list_next(record_list) != CALENDAR_ERROR_NO_DATA);
1705
1706                                 if (record_list != NULL) {
1707                                         service_ret = calendar_list_destroy(record_list, true);
1708                                         if (service_ret != CALENDAR_ERROR_NONE) {
1709                                                 _DEBUG_ERROR("calendar_list_destroy() Fail! : err = %d", service_ret);
1710                                         }
1711                                         record_list = NULL;
1712                                 }
1713                                 /*if list can successfully free child than dont need this free */
1714 //                              if (folder != NULL){
1715 //                                      calendar_record_destroy(folder, true);
1716 //                                      folder = NULL;
1717 //                              }
1718
1719                         } while (calendar_list_next(folder_list) != CALENDAR_ERROR_NO_DATA);    /*folder iter list */
1720
1721                         _DEBUG_TRACE("folder list free");
1722                         if (folder_list != NULL) {
1723                                 service_ret = calendar_list_destroy(folder_list, true);
1724                                 if (service_ret != CALENDAR_ERROR_NONE) {
1725                                         _DEBUG_ERROR("calendar_list_destroy() Fail! : err = %d", service_ret);
1726                                 }
1727                                 folder_list = NULL;
1728                         }
1729
1730                 }               /*account loop for */
1731                 _DEBUG_TRACE("account list free");
1732                 if (account_list != NULL) {
1733                         free(account_list);
1734                 }
1735
1736         }                       /*while (1) */
1737
1738         //  close calendar service
1739         sync_agent_plugin_close_service();
1740
1741         _DEBUG_TRACE("[da_task_plugIn] End __rutine_task_Change (create thread)\n");
1742
1743         _INNER_FUNC_EXIT;
1744
1745         return 0;
1746 }
1747
1748 static sync_agent_da_return_e _convert_service_error_to_common_error(int err)
1749 {
1750         _INNER_FUNC_ENTER;
1751
1752         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1753         _DEBUG_TRACE("[da_task_plugIn] Error Code : %d\n", err);
1754
1755         switch (err) {
1756         case CALENDAR_ERROR_INVALID_PARAMETER:  /* -4 */
1757                 ret = SYNC_AGENT_DA_ERR_INVALID_CONTENT;
1758                 break;
1759
1760         default:
1761                 ret = SYNC_AGENT_DA_ERRORS;
1762                 break;
1763         }
1764
1765         _INNER_FUNC_EXIT;
1766
1767         return ret;
1768 }
1769
1770 static bool _get_account_id(account_h account, void *user_data)
1771 {
1772         _INNER_FUNC_ENTER;
1773
1774         account_id_s *account_info_list = user_data;
1775
1776         bool has_contact = FALSE;
1777
1778         account_get_capability_all(account, __is_task_id, &has_contact);
1779
1780         if (has_contact == FALSE) {
1781                 /* DO Nothing */
1782                 _DEBUG_INFO("has_contact is false");
1783         } else {
1784                 char *domain_name = NULL;
1785                 account_error_e error_code = account_get_domain_name(account, &domain_name);
1786                 if (error_code != ACCOUNT_ERROR_NONE) {
1787                         _DEBUG_ERROR("Failed to call account_get_domain_name()");
1788                         return has_contact;
1789                 }
1790
1791                 /*local service id already in id list so pass */
1792                 _DEBUG_INFO("domain_name = %s", domain_name);
1793                 if (strcmp(domain_name, "Gmail") && strcmp(domain_name, "exchange")) {
1794                         _DEBUG_TRACE("domain is [%s] is not gmail&exchange");
1795                         return has_contact;
1796                 }
1797
1798                 account_id_s *account_info = (account_id_s *) calloc(1, sizeof(account_id_s));
1799                 if (account_info == NULL) {
1800                         _DEBUG_ERROR("CALLOC failed !!!");
1801                         return FALSE;
1802                 }
1803                 account_get_account_id(account, &(account_info->account_id));
1804                 _DEBUG_TRACE("Getted accont id : %d", account_info->account_id);
1805                 account_info->next = 0;
1806
1807                 account_id_s *cursor_ptr = account_info_list;
1808                 while (cursor_ptr->next != 0) {
1809                         cursor_ptr = cursor_ptr->next;
1810                 }
1811
1812                 cursor_ptr->next = account_info;
1813         }
1814
1815         _INNER_FUNC_EXIT;
1816
1817         return has_contact;
1818 }
1819
1820 static bool __is_task_id(/*account_capability_type_e*/const char* capability_type, account_capability_state_e capability_value, void *user_data)
1821 {
1822         _INNER_FUNC_ENTER;
1823         retvm_if(capability_type == NULL, FALSE, "capability_type is NULL !!");
1824
1825         bool *has_contact = user_data;
1826         //if (capability_type == ACCOUNT_CAPABILITY_CALENDAR) {
1827         if (strncmp(capability_type, ACCOUNT_SUPPORTS_CAPABILITY_CALENDAR, strlen(capability_type)) == 0) {
1828                 *has_contact = TRUE;
1829                 _INNER_FUNC_EXIT;
1830                 return FALSE;
1831         }
1832         _INNER_FUNC_EXIT;
1833         return TRUE;
1834 }