[Internal: merge sync-agent]
[platform/core/system/sync-agent.git] / src / framework / data-adapter / interface_service_item.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 "utility/sync_util.h"
19
20 #include "plugin/data_connector_plugin.h"
21 #include "plugin/data_converter_plugin.h"
22
23 #include "account/manager.h"
24 #include "account/util_internal.h"
25
26 #include "data-adapter/agent.h"
27 #include "data-adapter/agent_handler_manager.h"
28 #include "data-adapter/agent_handler_manager_internal.h"
29
30 #include "data-adapter/interface_item.h"
31 #include "data-adapter/interface_service_item.h"
32 #include "data-adapter/service_internal.h"
33
34 #include <stdlib.h>
35
36 #ifndef EXPORT_API
37 #define EXPORT_API __attribute__ ((visibility("default")))
38 #endif
39
40 #ifndef SYNC_AGENT_LOG
41 #undef LOG_TAG
42 #define LOG_TAG "AF_DACI"
43 #endif
44
45 EXPORT_API sync_agent_da_return_e sync_agent_open_service(int content_type)
46 {
47         _EXTERN_FUNC_ENTER;
48
49         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
50         plugin_open_service_cb func_service_open = plugin_get_function_open_service(content_type);
51
52         retvm_if(func_service_open == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_open_service_cb !!");
53
54         ret = func_service_open();
55         if (ret != SYNC_AGENT_DA_SUCCESS) {
56                 _DEBUG_ERROR("[sync_agent_open_service] Fail!");
57         } else {
58                 _DEBUG_INFO("[sync_agent_open_service] Success!");
59         }
60
61         _EXTERN_FUNC_EXIT;
62
63         return ret;
64 }
65
66 EXPORT_API sync_agent_da_return_e sync_agent_close_service(int content_type)
67 {
68         _EXTERN_FUNC_ENTER;
69
70         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
71         plugin_close_service_cb func_service_close = plugin_get_function_close_service(content_type);
72
73         retvm_if(func_service_close == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_close_service_cb !!");
74
75         ret = func_service_close();
76         if (ret != SYNC_AGENT_DA_SUCCESS) {
77                 _DEBUG_ERROR("[sync_agent_close_service] Fail!");
78         } else {
79                 _DEBUG_INFO("[sync_agent_close_service] Success!");
80         }
81
82         _EXTERN_FUNC_EXIT;
83
84         return ret;
85 }
86
87 EXPORT_API sync_agent_da_return_e sync_agent_begin_service(int content_type)
88 {
89         _EXTERN_FUNC_ENTER;
90
91         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
92         plugin_begin_transaction_cb func_begin_transaction = plugin_get_function_begin_transaction(content_type);
93
94         retvm_if(func_begin_transaction == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_begin_transaction_cb !!");
95
96         ret = func_begin_transaction();
97         if (ret != SYNC_AGENT_DA_SUCCESS) {
98                 _DEBUG_ERROR("[sync_agent_begin_service] Fail!");
99         } else {
100                 _DEBUG_INFO("[sync_agent_begin_service] Success!");
101         }
102
103         _EXTERN_FUNC_EXIT;
104
105         return ret;
106 }
107
108 EXPORT_API sync_agent_da_return_e sync_agent_end_service(int content_type, int is_success)
109 {
110         _EXTERN_FUNC_ENTER;
111
112         _DEBUG_INFO("[sync_agent_end_service] start");
113
114         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
115         plugin_end_transaction_cb func_end_transaction = plugin_get_function_end_transaction(content_type);
116
117         retvm_if(func_end_transaction == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_end_transaction_cb !!");
118
119         ret = func_end_transaction(is_success);
120         if (ret != SYNC_AGENT_DA_SUCCESS) {
121                 _DEBUG_ERROR("[sync_agent_end_service] Fail!");
122         } else {
123                 _DEBUG_INFO("[sync_agent_end_service] Success!");
124         }
125
126         _EXTERN_FUNC_EXIT;
127
128         return ret;
129 }
130
131 EXPORT_API sync_agent_da_return_e sync_agent_create_service_item(sync_agent_da_service_item_s ** service_item)
132 {
133         _EXTERN_FUNC_ENTER;
134
135         *service_item = (sync_agent_da_service_item_s *) calloc(1, sizeof(sync_agent_da_service_item_s));
136         if (*service_item == NULL) {
137                 _DEBUG_ERROR("calloc failed !!");
138                 return SYNC_AGENT_DA_ERRORS;
139         }
140
141         _EXTERN_FUNC_EXIT;
142         return SYNC_AGENT_DA_SUCCESS;
143 }
144
145 EXPORT_API sync_agent_da_return_e sync_agent_create_service_folder(sync_agent_da_service_folder_s ** service_folder)
146 {
147         _EXTERN_FUNC_ENTER;
148
149         *service_folder = (sync_agent_da_service_folder_s *) calloc(1, sizeof(sync_agent_da_service_folder_s));
150         if (*service_folder == NULL) {
151                 _DEBUG_ERROR("calloc failed !!");
152                 return SYNC_AGENT_DA_ERRORS;
153         }
154
155         _EXTERN_FUNC_EXIT;
156
157         return SYNC_AGENT_DA_SUCCESS;
158 }
159
160 EXPORT_API sync_agent_da_return_e sync_agent_free_service_item(sync_agent_da_service_item_s * service_item)
161 {
162         _EXTERN_FUNC_ENTER;
163
164         if (service_item != NULL) {
165                 if (service_item->item_id != NULL)
166                         free(service_item->item_id);
167                 service_item->item_id = NULL;
168                 if (service_item->parent_id != NULL)
169                         free(service_item->parent_id);
170                 service_item->parent_id = NULL;
171                 if (service_item->folder_id != NULL)
172                         free(service_item->folder_id);
173                 service_item->folder_id = NULL;
174                 if (service_item->access_name != NULL)
175                         free(service_item->access_name);
176                 service_item->access_name = NULL;
177                 if (service_item->data != NULL)
178                         free((void *)(service_item->data));
179                 service_item->data = NULL;
180                 free(service_item);
181                 service_item = NULL;
182         } else {
183                 _DEBUG_ERROR("service_item is NULL !!");
184                 return SYNC_AGENT_DA_ERRORS;
185         }
186
187         _EXTERN_FUNC_EXIT;
188         return SYNC_AGENT_DA_SUCCESS;
189 }
190
191 EXPORT_API sync_agent_da_return_e sync_agent_free_service_folder(sync_agent_da_service_folder_s * service_folder)
192 {
193         _EXTERN_FUNC_ENTER;
194
195         if (service_folder != NULL) {
196                 if (service_folder->folder_id != NULL)
197                         free(service_folder->folder_id);
198
199                 if (service_folder->folder_name != NULL)
200                         free(service_folder->folder_name);
201
202                 free(service_folder);
203         } else {
204                 _DEBUG_ERROR("service_folder is NULL !!");
205                 return SYNC_AGENT_DA_ERRORS;
206         }
207         _EXTERN_FUNC_EXIT;
208         return SYNC_AGENT_DA_SUCCESS;
209 }
210
211 EXPORT_API sync_agent_da_return_e sync_agent_add_service_item(sync_agent_da_service_item_s * service_item, char **item_id, bool update_changelog)
212 {
213         _EXTERN_FUNC_ENTER;
214
215         retvm_if(service_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_service_item_s is NULL !!");
216
217         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
218         sync_agent_da_item_s *fw_item = NULL;
219         int service_account_id = 0;
220         char *service_item_id = NULL;
221         char *service_folder_id = NULL;
222         void *service_data = NULL;
223         plugin_converter_cb func_converter;
224         plugin_add_item_cb func_addItem;
225
226         /* 1. get service account id */
227         service_account_id = acc_get_service_account_id(service_item->content_type, service_item->account_id);
228         _DEBUG_INFO("[sync_agent_add_service_item] service_acount_id : %d", service_account_id);
229
230         /* 2. get service folder id */
231         service_folder_id = sync_agent_get_service_folder_id(service_item->folder_id);
232         _DEBUG_INFO("[sync_agent_add_service_item] service_folder_id : %s", service_folder_id);
233
234         /* 3. call plugIn dataconverter */
235         int converter_plugIn_id = plugin_get_data_converter_plugin_id(service_item->content_type);
236         func_converter = plugin_get_function_converter(converter_plugIn_id);
237         if (func_converter == NULL) {
238                 _DEBUG_ERROR("%d", __LINE__);
239                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
240                 goto DACI_FINISH;
241         }
242
243         ret = func_converter(service_item->data, &service_data);
244         if (ret != SYNC_AGENT_DA_SUCCESS) {
245                 _DEBUG_ERROR("%d", __LINE__);
246                 goto DACI_FINISH;
247         }
248
249         /* 4. call plugIn dataconnector */
250         func_addItem = plugin_get_function_add_item(service_item->content_type);
251         if (func_addItem == NULL) {
252                 _DEBUG_ERROR("%d", __LINE__);
253                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
254                 goto DACI_FINISH;
255         }
256
257         ret = func_addItem(service_account_id, service_folder_id, service_data, &service_item_id);
258         if (ret != SYNC_AGENT_DA_SUCCESS) {
259                 _DEBUG_INFO("[sync_agent_add_service_item] failed pFunc_AddItem()\n");
260                 goto DACI_FINISH;
261         } else {
262                 _DEBUG_INFO("[sync_agent_add_service_item] success pFunc_AddItem()\n");
263                 _DEBUG_INFO("[sync_agent_add_service_item] added service item id : %s", service_item_id);
264         }
265
266         ret = sync_agent_create_item(&fw_item);
267         if (ret != SYNC_AGENT_DA_SUCCESS) {
268                 _DEBUG_INFO("[sync_agent_add_service_item] failed sync_agent_create_item()\n");
269                 goto DACI_FINISH;
270         }
271
272         fw_item->data_store_id = service_item->content_type;
273         fw_item->account_id = service_item->account_id;
274         fw_item->item_id = g_strdup(service_item->item_id);
275         fw_item->folder_id = g_strdup(service_item->folder_id);
276         fw_item->service_id = g_strdup(service_item_id);
277         fw_item->parent_service_id = g_strdup(service_item->parent_id);
278         fw_item->access_name = g_strdup(service_item->access_name);
279
280         ret = sync_agent_add_item(fw_item, item_id, update_changelog);
281         if (ret != SYNC_AGENT_DA_SUCCESS) {
282                 _DEBUG_INFO("[sync_agent_add_service_item] failed sync_agent_add_item()\n");
283                 goto DACI_FINISH;
284         } else {
285                 _DEBUG_INFO("[sync_agent_add_service_item] success sync_agent_add_item()\n");
286                 _DEBUG_INFO("[sync_agent_add_service_item] added fw item id : %s", *item_id);
287         }
288
289  DACI_FINISH:
290
291         if (ret != SYNC_AGENT_DA_SUCCESS)
292                 *item_id = NULL;
293
294         /* memory free */
295         if (fw_item != NULL)
296                 sync_agent_free_item(fw_item);
297         if (service_item_id != NULL)
298                 free(service_item_id);
299         if (service_folder_id != NULL)
300                 free(service_folder_id);
301
302         _EXTERN_FUNC_EXIT;
303
304         return ret;
305 }
306
307 EXPORT_API sync_agent_da_return_e sync_agent_add_service_bulk_item(sync_agent_da_service_item_s *service_item, char **item_id, bool update_changelog)
308 {
309         _EXTERN_FUNC_ENTER;
310
311         retvm_if(service_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_service_item_s is NULL !!");
312
313         char *service_folder_id = NULL;
314         char service_item_id_tmp[10] = {0,};
315         int service_account_id = 0;
316         int count = 0;
317         int index = 0;
318         int *service_item_id = NULL;
319         int service_item_id_count = 0;
320         void *service_data = NULL;
321         sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
322         sync_agent_da_item_s *fw_item = NULL;
323         plugin_converter_cb func_converter;
324         plugin_add_bulk_item_cb func_addBulkItem;
325
326         /* 1. get service account id */
327         service_account_id = acc_get_service_account_id(service_item->content_type, service_item->account_id);
328         _DEBUG_INFO(" service_acount_id : [%d]", service_account_id);
329
330         /* 2. get service folder id */
331         service_folder_id = sync_agent_get_service_folder_id(service_item->folder_id);
332         _DEBUG_INFO(" service_folder_id : [%s]", service_folder_id);
333
334         /* 3. call plugIn dataconverter */
335         int converter_plugIn_id = plugin_get_data_converter_plugin_id(service_item->content_type);
336         func_converter = plugin_get_function_converter(converter_plugIn_id);
337         if (func_converter == NULL) {
338                 _DEBUG_ERROR(" plugin_get_function_converter() fail, func_converter is NULL");
339                 da_ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
340                 goto DACI_FINISH;
341         }
342
343         /**************** phase 1 : vcard -> ctsvc struct **********************/
344         count = g_list_length((GList *)service_item->data);
345         _DEBUG_INFO(" count of vcard list = [%d]", count);
346
347         GList *service_data_list = NULL;
348         for (index = 0; index < count; ++index) {
349                 da_ret = func_converter((char *)g_list_nth_data((GList *)service_item->data, index), &service_data);
350                 if (da_ret != SYNC_AGENT_DA_SUCCESS) {
351                         _DEBUG_ERROR(" func_converter() fail, da_ret[%d]", da_ret);
352                         goto DACI_FINISH;
353                 }
354                 /**************** phase 2 : ctsvc struct -> GList **********************/
355                 service_data_list = g_list_append(service_data_list, service_data);
356         }
357         _DEBUG_INFO(" count of ctsvc struct list = [%d]", g_list_length(service_data_list));
358
359
360         /* 4. call plugIn dataconnector */
361         func_addBulkItem = plugin_get_function_add_bulk_item(service_item->content_type);
362         if (func_addBulkItem == NULL) {
363                 _DEBUG_ERROR(" plugin_get_function_add_bulk_item() fail, func_addBulkItem is NULL");
364                 da_ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
365                 goto DACI_FINISH;
366         }
367
368         da_ret = func_addBulkItem(service_account_id, service_folder_id, service_data_list, &service_item_id, &service_item_id_count);
369         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
370                 _DEBUG_ERROR(" func_addBulkItem() fail, da_ret[%d]", da_ret);
371                 goto DACI_FINISH;
372         } else {
373                 _DEBUG_INFO("added service item id size = [%d]", service_item_id_count);
374         }
375
376         /**************** phase 4 : inserted contact DB -> insert kies DB **********************/
377         for (index = 0; index < service_item_id_count; ++index) {
378                 da_ret = sync_agent_create_item(&fw_item);
379                 if (da_ret != SYNC_AGENT_DA_SUCCESS) {
380                         _DEBUG_ERROR("sync_agent_create_item() fail, da_ret[%s]", da_ret);
381                         goto DACI_FINISH;
382                 }
383                 if (fw_item == NULL) {
384                         _DEBUG_ERROR("sync_agent_create_item() fail, fw_item is NULL");
385                         goto DACI_FINISH;
386                 }
387
388                 fw_item->data_store_id = service_item->content_type;
389                 fw_item->account_id = service_item->account_id;
390                 fw_item->item_id = g_strdup(item_id[index]);
391                 fw_item->folder_id = g_strdup(service_item->folder_id);
392                 memset(service_item_id_tmp, 0, 10);
393                 snprintf(service_item_id_tmp, 10, "%d", service_item_id[index]);
394                 fw_item->service_id = g_strdup(service_item_id_tmp);
395                 fw_item->parent_service_id = g_strdup(service_item->parent_id);
396                 _DEBUG_INFO("fw_item->parent_service_id = %s", fw_item->parent_service_id);
397                 fw_item->access_name = g_strdup(service_item->access_name);
398                 //for test
399                 //_DEBUG_INFO("fw_item->service_id[%d] = [%s], item_id[%d] = [%s], fw_item->item_id = [%s]", index, fw_item->service_id, index, item_id[index], fw_item->item_id);
400
401                 da_ret = sync_agent_add_item(fw_item, &item_id[index], update_changelog);
402                 if (da_ret != SYNC_AGENT_DA_SUCCESS) {
403                         _DEBUG_ERROR("sync_agent_add_bulk_item() fail, da_ret[%d]", da_ret);
404                         goto DACI_FINISH;
405                 } else {
406                         //for test
407                         //_DEBUG_INFO("sync_agent_add_bulk_item() success, added fw item id = [%s]", item_id[index]);
408                 }
409
410                 if (fw_item != NULL) {
411                         da_ret = sync_agent_free_item(fw_item);
412                         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
413                                 _DEBUG_ERROR("sync_agent_free_item() fail, da_ret[%d]", da_ret);
414                                 goto DACI_FINISH;
415                         }
416                         fw_item = NULL;
417                 }
418         }
419
420 DACI_FINISH:
421         _DEBUG_INFO("daci_finish");
422
423         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
424                 *item_id = NULL;
425         }
426         if (fw_item != NULL) {
427                 da_ret = sync_agent_free_item(fw_item);
428                 if (da_ret != SYNC_AGENT_DA_SUCCESS) {
429                         _DEBUG_ERROR("sync_agent_free_item() fail, da_ret[%d]", da_ret);
430                 }
431                 fw_item = NULL;
432         }
433         if (service_item_id != NULL) {
434                 free(service_item_id);
435                 service_item_id = NULL;
436         }
437         if (service_folder_id != NULL) {
438                 free(service_folder_id);
439                 service_folder_id = NULL;
440         }
441         _EXTERN_FUNC_EXIT;
442         return da_ret;
443 }
444
445 EXPORT_API sync_agent_da_return_e sync_agent_update_service_item(sync_agent_da_service_item_s * service_item, char *item_id, bool update_changelog)
446 {
447         _EXTERN_FUNC_ENTER;
448
449         retvm_if(service_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_service_item_s is NULL !!");
450         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
451
452         _DEBUG_INFO("[sync_agent_update_service_item] account_id : %d", service_item->account_id);
453         _DEBUG_INFO("[sync_agent_update_service_item] folder_id : %s", service_item->folder_id);
454         _DEBUG_INFO("[sync_agent_update_service_item] item_id : %s", item_id);
455         _DEBUG_INFO("[sync_agent_update_service_item] data : %s", (char *)service_item->data);
456
457         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
458         int service_account_id = 0;
459         char *service_folder_id = NULL;
460         char *service_item_id = NULL;
461         void *service_data = NULL;
462         void *new_service_data = NULL;
463         plugin_get_item_cb func_get_item;
464         plugin_update_item_cb func_update_item;
465         plugin_replace_converter_cb func_replace_converter;
466
467         /* 1. get service account id */
468         service_account_id = acc_get_service_account_id(service_item->content_type, service_item->account_id);
469         _DEBUG_INFO("[sync_agent_update_service_item] service_acount_id : %d", service_account_id);
470
471         /* 2. get service folder id */
472         service_folder_id = sync_agent_get_service_folder_id(service_item->folder_id);
473         _DEBUG_INFO("[sync_agent_update_service_item] service_folder_id : %s", service_folder_id);
474
475         /* 3. get service item id */
476         service_item_id = sync_agent_get_service_item_id(item_id);
477         _DEBUG_INFO("[sync_agent_update_service_item] service_item_id : %s", service_item_id);
478
479         /* 4. call plugIn dataconnector */
480         func_get_item = plugin_get_function_get_item(service_item->content_type);
481         if (func_get_item == NULL) {
482                 _DEBUG_ERROR("%d", __LINE__);
483                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
484                 goto DACI_FINISH;
485         }
486
487         ret = func_get_item(service_account_id, service_folder_id, service_item_id, &service_data);
488         if (ret != SYNC_AGENT_DA_SUCCESS) {
489                 _DEBUG_INFO("%d", __LINE__);
490                 goto DACI_FINISH;
491         }
492
493         /* 5. call plugIn dataconverter */
494         int converter_plugIn_id = plugin_get_data_converter_plugin_id(service_item->content_type);
495         func_replace_converter = plugin_get_function_replace_converter(converter_plugIn_id);
496         if (func_replace_converter == NULL) {
497                 _DEBUG_ERROR("%d", __LINE__);
498                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
499                 goto DACI_FINISH;
500         }
501
502         ret = func_replace_converter(service_data, service_item->data, &new_service_data);
503         if (ret != SYNC_AGENT_DA_SUCCESS) {
504                 _DEBUG_INFO("%d", __LINE__);
505                 goto DACI_FINISH;
506         }
507
508         /* 6. call plugIn dataconverter */
509         func_update_item = plugin_get_function_update_item(service_item->content_type);
510         if (func_update_item == NULL) {
511                 _DEBUG_ERROR("%d", __LINE__);
512                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
513                 goto DACI_FINISH;
514         }
515
516         ret = func_update_item(service_account_id, service_folder_id, service_item_id, new_service_data);
517         if (ret != SYNC_AGENT_DA_SUCCESS) {
518                 _DEBUG_INFO("[sync_agent_update_service_item] failed func_update_item()\n");
519                 goto DACI_FINISH;
520         } else {
521                 _DEBUG_INFO("[sync_agent_update_service_item] success func_update_item()\n");
522         }
523
524         ret = sync_agent_update_item(item_id, update_changelog);
525         if (ret != SYNC_AGENT_DA_SUCCESS) {
526                 _DEBUG_INFO("[sync_agent_update_service_item] failed sync_agent_update_item()\n");
527                 goto DACI_FINISH;
528         } else {
529                 _DEBUG_INFO("[sync_agent_update_service_item] success sync_agent_update_item()\n");
530                 _DEBUG_INFO("[sync_agent_update_service_item] updated fw item id : %s", item_id);
531         }
532
533  DACI_FINISH:
534
535         /* memory free */
536         if (service_folder_id != NULL)
537                 free(service_folder_id);
538
539         if (service_item_id != NULL)
540                 free(service_item_id);
541
542         _EXTERN_FUNC_EXIT;
543         return ret;
544 }
545
546 EXPORT_API sync_agent_da_return_e sync_agent_update_service_bulk_item(sync_agent_da_service_item_s *service_item, char **item_id, bool update_changelog)
547 {
548         _EXTERN_FUNC_ENTER;
549
550         retvm_if(service_item == NULL, SYNC_AGENT_DA_ERRORS, "service_item is NULL");
551         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item_id is NULL");
552
553         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
554         char *service_item_id = NULL;
555         int index = 0;
556         int count = g_list_length((GList *)service_item->data);
557         void *service_data = NULL;
558         void *new_service_data = NULL;
559         GList *service_data_list = NULL;
560         plugin_get_item_cb func_get_item;
561         plugin_replace_converter_cb func_replace_converter;
562         plugin_update_bulk_item_cb func_update_bulk_item;
563
564         /*Get service_account_id*/
565         int service_account_id = acc_get_service_account_id(service_item->content_type, service_item->account_id);
566         _DEBUG_INFO("service_account_id = [%d]", service_account_id);
567
568         /*Get service_folder_id*/
569         char *service_folder_id = sync_agent_get_service_folder_id(service_item->folder_id);
570         _DEBUG_INFO("service_folder_id = [%s]", service_folder_id);
571
572         /*Get service_item_id*/
573         for (index = 0; index < count; ++index) {
574                 service_item_id = sync_agent_get_service_item_id(item_id[index]);
575                 _DEBUG_INFO("service_item_id = [%s]", service_item_id);
576
577                 /*Get current data*/
578                 func_get_item = plugin_get_function_get_item(service_item->content_type);
579                 if (func_get_item == NULL) {
580                         _DEBUG_ERROR("func_get_item is NULL");
581                         ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
582                         goto DACI_FINISH;
583                 }
584                 ret = func_get_item(service_account_id, service_folder_id, service_item_id, &service_data);
585                 if (ret != SYNC_AGENT_DA_SUCCESS) {
586                         _DEBUG_ERROR("func_get_item() fail, ret[%d]", ret);
587                         goto DACI_FINISH;
588                 }
589
590                 /*Repleace data*/
591                 int converter_plugin_id = plugin_get_data_converter_plugin_id(service_item->content_type);
592                 func_replace_converter = plugin_get_function_replace_converter(converter_plugin_id);
593                 if (func_replace_converter == NULL) {
594                         _DEBUG_ERROR("func_replace_converter is NULL");
595                         ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
596                         goto DACI_FINISH;
597                 }
598                 ret = func_replace_converter(service_data, ((char *)g_list_nth_data((GList *)service_item->data, index)), &new_service_data);
599                 if (ret != SYNC_AGENT_DA_SUCCESS) {
600                         _DEBUG_ERROR("func_replace_converter() fail, ret[%d]", ret);
601                         goto DACI_FINISH;
602                 }
603                 service_data_list = g_list_append(service_data_list, new_service_data);
604         }
605
606         func_update_bulk_item = plugin_get_function_update_bulk_item(service_item->content_type);
607         if (func_update_bulk_item == NULL) {
608                 _DEBUG_ERROR("func_update_bulk_item is NULL");
609                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
610                 goto DACI_FINISH;
611         }
612         ret = func_update_bulk_item(service_data_list);
613         if (ret != SYNC_AGENT_DA_SUCCESS) {
614                 _DEBUG_ERROR("func_update_bulk_item() fail, ret[%d]", ret);
615         }
616
617 DACI_FINISH:
618         _DEBUG_INFO("DACI_FINISH");
619
620         if (service_folder_id != NULL) {
621                 free(service_folder_id);
622                 service_folder_id = NULL;
623         }
624
625         if (service_item_id != NULL) {
626                 free(service_item_id);
627                 service_item_id = NULL;
628         }
629
630         _EXTERN_FUNC_EXIT;
631         return ret;
632 }
633
634 EXPORT_API sync_agent_da_return_e sync_agent_add_updated_exdate_item(sync_agent_da_service_item_s * service_item, char *parent_id, char *child_id, bool update_changelog)
635 {
636         _EXTERN_FUNC_ENTER;
637
638         retvm_if(service_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_service_item_s is NULL !!");
639         retvm_if(parent_id == NULL, SYNC_AGENT_DA_ERRORS, "parent_id is NULL !!");
640
641         _DEBUG_INFO("account_id : %d", service_item->account_id);
642         _DEBUG_INFO("folder_id : %s", service_item->folder_id);
643         _DEBUG_INFO("parent_id : %s", parent_id);
644         _DEBUG_INFO("child_id : %s", child_id);
645         _DEBUG_INFO("service_item->item_id : %s", service_item->item_id);
646         _DEBUG_INFO("data : %s", (char *)service_item->data);
647
648         char *service_folder_id = NULL;
649         char *service_parent_item_id = NULL;
650         void *service_data = NULL;
651         void *new_service_data = NULL;
652         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
653         int service_account_id = 0;
654         plugin_get_item_cb func_get_item;
655         plugin_replace_converter_cb func_replace_converter;
656
657         service_account_id = acc_get_service_account_id(service_item->content_type, service_item->account_id);
658         _DEBUG_INFO("service_acount_id : %d", service_account_id);
659         service_folder_id = sync_agent_get_service_folder_id(service_item->folder_id);
660         _DEBUG_INFO("service_folder_id : %s", service_folder_id);
661         service_parent_item_id = sync_agent_get_service_item_id(parent_id);
662         _DEBUG_INFO("service_parent_item_id : %s", service_parent_item_id);
663
664         /* 4. call plugIn dataconnector */
665         func_get_item = plugin_get_function_get_item(service_item->content_type);
666         if (func_get_item == NULL) {
667                 _DEBUG_ERROR("%d", __LINE__);
668                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
669                 goto return_part;
670         }
671
672         ret = func_get_item(service_account_id, service_folder_id, service_parent_item_id, &service_data);
673         goto_if(ret != SYNC_AGENT_DA_SUCCESS, "func_get_item() failed, ret = [%d]", ret);
674
675         /* 5. checking whether the item exist or not*/
676         ret = sync_agent_is_exist_exdate_item(service_item->content_type, service_parent_item_id, (char *)service_item->data);
677         if (ret == SYNC_AGENT_DA_ERR_ALREADY_EXIST) {
678                 _DEBUG_INFO("SYNC_AGENT_DA_ERR_ALREADY_EXIST");
679                 ret = SYNC_AGENT_DA_SUCCESS;
680                 goto return_part;
681         }
682
683         /* 6. call plugIn dataconverter */
684         int converter_plugIn_id = plugin_get_data_converter_plugin_id(service_item->content_type);
685         func_replace_converter = plugin_get_function_replace_converter(converter_plugIn_id);
686         if (func_replace_converter == NULL) {
687                 _DEBUG_ERROR("%d", __LINE__);
688                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
689                 goto return_part;
690         }
691
692         ret = func_replace_converter(service_data, service_item->data, &new_service_data);
693         goto_if(ret != SYNC_AGENT_DA_SUCCESS, "func_replace_converter() failed, ret = [%d]", ret);
694
695
696         ////////////////////////////////////////  construct the parent item ////////////////////////////////
697         // All child item of parent item whose has original_event_id as service_id field should be deleted before callback_add_item()
698         // Requesting by Calendar Svc  Team (2013-03-26 tw123.kim & iamjs.kim)
699         sync_agent_construct_exdate_parent_item(service_item->content_type, service_parent_item_id);
700
701 return_part:
702         _DEBUG_INFO("return_part");
703         if (service_folder_id != NULL){
704                 free(service_folder_id);
705                 service_folder_id = NULL;
706         }
707         if (new_service_data != NULL){
708                 free(new_service_data);
709                 new_service_data = NULL;
710         }
711         _EXTERN_FUNC_EXIT;
712         return ret;
713 }
714
715 EXPORT_API sync_agent_da_return_e sync_agent_delete_service_item(char *item_id, bool update_changelog)
716 {
717         _EXTERN_FUNC_ENTER;
718
719         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
720
721         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
722         sync_agent_da_item_s *fw_item = NULL;
723         int service_account_id = 0;
724         char *service_folder_id = NULL;
725         char *service_item_id = NULL;
726         plugin_delete_item_cb func_del_item;
727
728         ret = sync_agent_create_item(&fw_item);
729         if (ret != SYNC_AGENT_DA_SUCCESS) {
730                 _DEBUG_ERROR("sync_agent_create_item() failed !!");
731                 goto DACI_FINISH;
732         }
733
734         ret = sync_agent_get_item(item_id, &fw_item);
735         if (ret != SYNC_AGENT_DA_SUCCESS) {
736                 _DEBUG_ERROR("sync_agent_get_item() failed !!");
737                 goto DACI_FINISH;
738         }
739
740         /* 1. get service account id */
741         service_account_id = acc_get_service_account_id(fw_item->data_store_id, fw_item->account_id);
742         _DEBUG_INFO("[sync_agent_delete_service_item] service_acount_id : %d", service_account_id);
743
744         /* 2. get service folder id */
745         service_folder_id = sync_agent_get_service_folder_id(fw_item->folder_id);
746         _DEBUG_INFO("[sync_agent_delete_service_item] service_folder_id : %s", service_folder_id);
747
748         /* 3. get service item id */
749         service_item_id = sync_agent_get_service_item_id(item_id);
750         _DEBUG_INFO("[sync_agent_delete_service_item] service_item_id : %s", service_item_id);
751
752         /* 4. call plugIn dataconnector */
753         func_del_item = plugin_get_function_del_item(fw_item->data_store_id);
754         if (func_del_item == NULL) {
755                 _DEBUG_ERROR("%d", __LINE__);
756                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
757                 goto DACI_FINISH;
758         }
759
760         ret = func_del_item(service_account_id, service_folder_id, service_item_id);
761         if (ret != SYNC_AGENT_DA_SUCCESS) {
762                 _DEBUG_INFO("[sync_agent_delete_service_item] failed func_del_item()\n");
763                 goto DACI_FINISH;
764         } else {
765                 _DEBUG_INFO("[sync_agent_delete_service_item] success func_del_item()\n");
766         }
767
768         ret = sync_agent_delete_item(item_id, update_changelog);
769         if (ret != SYNC_AGENT_DA_SUCCESS) {
770                 _DEBUG_INFO("[sync_agent_delete_service_item] failed sync_agent_delete_item()\n");
771                 goto DACI_FINISH;
772         } else {
773                 _DEBUG_INFO("[sync_agent_delete_service_item] success sync_agent_delete_item()\n");
774                 _DEBUG_INFO("[sync_agent_delete_service_item] deleted fw item id : %s", item_id);
775         }
776
777  DACI_FINISH:
778
779         /* memory free */
780         if (fw_item != NULL)
781                 sync_agent_free_item(fw_item);
782
783         if (service_folder_id != NULL)
784                 free(service_folder_id);
785
786         if (service_item_id != NULL)
787                 free(service_item_id);
788
789         _EXTERN_FUNC_EXIT;
790
791         return ret;
792 }
793
794 EXPORT_API sync_agent_da_return_e sync_agent_delete_service_bulk_item(char **item_id, int count, bool update_changelog)
795 {
796         _EXTERN_FUNC_ENTER;
797
798         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item_id is NULL");
799         retvm_if(count == 0, SYNC_AGENT_DA_ERRORS, "count is 0");
800
801         sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
802         sync_agent_da_item_s *fw_item = NULL;
803         plugin_delete_bulk_item_cb func_del_bulk_item;
804         char *service_item_id_tmp = NULL;
805         int index = 0;
806         int service_item_id[count];
807
808         da_ret = sync_agent_create_item(&fw_item);
809         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
810                 _DEBUG_ERROR("sync_agent_create_item() fail, da_Ret[%d]", da_ret);
811                 goto DACI_FINISH;
812         }
813
814         da_ret = sync_agent_get_item_data_store_id(*item_id, &fw_item);
815         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
816                 _DEBUG_ERROR("sync_agent_get_item_data_store_id() fail, da_ret[%d]", da_ret);
817                 goto DACI_FINISH;
818         }
819
820         memset(service_item_id, 0, count);
821         for (index = 0; index < count; ++index) {
822                 service_item_id_tmp = sync_agent_get_service_item_id(item_id[index]);
823                 service_item_id[index] = atoi(service_item_id_tmp);
824         }
825
826         func_del_bulk_item = plugin_get_function_del_bulk_item(fw_item->data_store_id);
827         if (func_del_bulk_item == NULL) {
828                 _DEBUG_ERROR("func_del_bulk_item is NULL");
829                 da_ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
830                 goto DACI_FINISH;
831         }
832
833         da_ret = func_del_bulk_item(service_item_id, count);
834         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
835                 _DEBUG_ERROR("func_del_bulk_item() fail, da_ret[%d]", da_ret);
836                 goto DACI_FINISH;
837         }
838
839         for (index = 0; index < count; ++index) {
840                 da_ret = sync_agent_delete_item(item_id[index], update_changelog);
841                 if (da_ret != SYNC_AGENT_DA_SUCCESS) {
842                         _DEBUG_ERROR("sync_agent_delete_item() fail, da_ret[%d]", da_ret);
843                         goto DACI_FINISH;
844                 } else {
845                         //for test
846                         //_DEBUG_INFO("sync_agent_delete_item() success, deleted fw_item_id = [%s]", item_id[index]);
847                 }
848         }
849
850 DACI_FINISH:
851         _DEBUG_INFO("daci_finish");
852
853         if (fw_item != NULL) {
854                 da_ret = sync_agent_free_item(fw_item);
855                 if (da_ret != SYNC_AGENT_DA_SUCCESS) {
856                         _DEBUG_ERROR("sync_agent_free_item() fail, da_ret[%d]", da_ret);
857                 }
858         }
859
860         _EXTERN_FUNC_EXIT;
861         return da_ret;
862 }
863
864 EXPORT_API sync_agent_da_return_e sync_agent_query_delete_service_items(sync_agent_da_delete_service_item_query_s * query)
865 {
866         _EXTERN_FUNC_ENTER;
867
868         retvm_if(query == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_delete_service_item_query_s is NULL !!");
869
870         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
871         int service_account_id = 0;
872         plugin_delete_all_items_cb func_del_all_items;
873
874         /* 1. get service account id */
875         service_account_id = acc_get_service_account_id(query->content_type, query->account_id);
876         _DEBUG_INFO("[sync_agent_query_delete_service_items] service_acount_id : %d", service_account_id);
877
878         /* 2. call plugIn dataconnector */
879         func_del_all_items = plugin_get_function_del_all_items(query->content_type);
880
881         retvm_if(func_del_all_items == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_delete_all_items_cb !!");
882
883         ret = func_del_all_items(service_account_id);
884         if (ret != SYNC_AGENT_DA_SUCCESS) {
885                 _DEBUG_INFO("[sync_agent_query_delete_service_items] failed func_del_all_items()\n");
886         } else {
887                 _DEBUG_INFO("[sync_agent_query_delete_service_items] success func_del_all_items()\n");
888         }
889
890         _EXTERN_FUNC_EXIT;
891         return ret;
892 }
893
894 EXPORT_API sync_agent_da_return_e sync_agent_get_service_item(char *item_id, sync_agent_da_service_item_s ** service_item)
895 {
896         _EXTERN_FUNC_ENTER;
897
898         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
899
900         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
901         sync_agent_da_item_s *fw_item = NULL;
902         int service_account_id = 0;
903         int parent_id = -1;
904         char *service_folder_id = NULL;
905         char *service_item_id = NULL;
906         void *service_data = NULL;
907         plugin_get_item_cb func_get_item;
908         plugin_reverse_converter_cb func_reverse_converter;
909
910         ret = sync_agent_create_item(&fw_item);
911         if (ret != SYNC_AGENT_DA_SUCCESS) {
912                 _DEBUG_ERROR("sync_agent_create_item() failed !!");
913                 goto DACI_FINISH;
914         }
915
916         ret = sync_agent_get_item(item_id, &fw_item);
917         if (ret != SYNC_AGENT_DA_SUCCESS) {
918                 _DEBUG_ERROR("sync_agent_get_item() failed !!");
919                 goto DACI_FINISH;
920         }
921
922         /* 1. get service account id */
923         service_account_id = acc_get_service_account_id(fw_item->data_store_id, fw_item->account_id);
924         _DEBUG_INFO("[sync_agent_get_service_item] service_acount_id : %d", service_account_id);
925
926         /* 2. get service folder id */
927         service_folder_id = sync_agent_get_service_folder_id(fw_item->folder_id);
928         _DEBUG_INFO("[sync_agent_get_service_item] service_folder_id : %s", service_folder_id);
929
930         /* 3. get service item id */
931         service_item_id = sync_agent_get_service_item_id(item_id);
932         _DEBUG_INFO("[sync_agent_get_service_item] service_item_id : %s", service_item_id);
933
934         /* 4. call plugIn dataconnector */
935         func_get_item = plugin_get_function_get_item(fw_item->data_store_id);
936         if (func_get_item == NULL) {
937                 _DEBUG_ERROR("%d", __LINE__);
938                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
939                 goto DACI_FINISH;
940         }
941
942         ret = func_get_item(service_account_id, service_folder_id, service_item_id, &service_data);
943         if (ret != SYNC_AGENT_DA_SUCCESS) {
944                 _DEBUG_ERROR("%d", __LINE__);
945                 goto DACI_FINISH;
946         }
947
948         /* 5. call plugIn dataconverter */
949         int converter_plugIn_id = plugin_get_data_converter_plugin_id(fw_item->data_store_id);
950         func_reverse_converter = plugin_get_function_reverse_converter(converter_plugIn_id);
951         if (func_reverse_converter == NULL) {
952                 _DEBUG_ERROR("%d", __LINE__);
953                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
954                 goto DACI_FINISH;
955         }
956
957         ret = func_reverse_converter(service_data, (void **)(&((*service_item)->data)));
958         if (ret != SYNC_AGENT_DA_SUCCESS) {
959                 _DEBUG_INFO("[sync_agent_get_service_item] failed func_reverse_converter()\n");
960                 goto DACI_FINISH;
961         } else {
962                 _DEBUG_INFO("[sync_agent_get_service_item] success func_reverse_converter()\n");
963         }
964
965         (*service_item)->content_type = fw_item->data_store_id;
966         (*service_item)->account_id = fw_item->account_id;
967         (*service_item)->item_id = g_strdup(fw_item->item_id);
968         (*service_item)->folder_id = g_strdup(fw_item->folder_id);
969         (*service_item)->access_name = g_strdup(fw_item->access_name);
970
971         // in case of exdate item (only Calendar), The framework id of it's parent should be alloc to service_item->parent_id
972         if (fw_item->parent_service_id != NULL) {
973                 parent_id = atoi(fw_item->parent_service_id);
974                 _DEBUG_INFO("parent_id = %d", parent_id);
975                 if (parent_id >= 0)
976                         (*service_item)->parent_id = da_get_item_id_internal((*service_item)->account_id, fw_item->parent_service_id, (*service_item)->content_type);
977         }
978
979 DACI_FINISH:
980
981         if (fw_item != NULL)
982                 sync_agent_free_item(fw_item);
983         fw_item = NULL;
984
985         if (service_folder_id != NULL)
986                 free(service_folder_id);
987         service_folder_id = NULL;
988
989         if (service_item_id != NULL)
990                 free(service_item_id);
991         service_item_id = NULL;
992
993         _EXTERN_FUNC_EXIT;
994         return ret;
995 }
996
997 EXPORT_API sync_agent_da_return_e sync_agent_get_service_folder(char *folder_id, sync_agent_da_service_folder_s ** service_folder)
998 {
999         _EXTERN_FUNC_ENTER;
1000
1001         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERRORS, "folder id is NULL !!");
1002
1003         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1004         sync_agent_da_folder_s *fw_folder = NULL;
1005         int service_account_id = 0;
1006         char *service_folder_id = NULL;
1007         plugin_get_folder_cb func_get_folder;
1008
1009         ret = sync_agent_create_folder(&fw_folder);
1010         if (ret != SYNC_AGENT_DA_SUCCESS) {
1011                 _DEBUG_ERROR("sync_agent_create_folder() failed !!");
1012                 goto DACI_FINISH;
1013         }
1014
1015         ret = sync_agent_get_folder(folder_id, &fw_folder);
1016         if (ret != SYNC_AGENT_DA_SUCCESS) {
1017                 _DEBUG_ERROR("sync_agent_get_folder() failed !!");
1018                 goto DACI_FINISH;
1019         }
1020
1021         _DEBUG_INFO("folder[%s] info => data_store_id[%d], account_id[%d]", fw_folder->folder_id, fw_folder->data_store_id, fw_folder->account_id);
1022
1023         /* 1. get service account id */
1024         service_account_id = acc_get_service_account_id(fw_folder->data_store_id, fw_folder->account_id);
1025         _DEBUG_INFO("[sync_agent_get_service_folder] service_acount_id : %d", service_account_id);
1026
1027         /* 2. get service folder id */
1028         service_folder_id = sync_agent_get_service_folder_id(folder_id);
1029         _DEBUG_INFO("[sync_agent_get_service_folder] service_folder_id : %s", service_folder_id);
1030
1031         /* 3. call plugIn dataconnector */
1032         func_get_folder = plugin_get_function_get_folder(fw_folder->data_store_id);
1033         if (func_get_folder == NULL) {
1034                 _DEBUG_ERROR("%d", __LINE__);
1035                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
1036                 goto DACI_FINISH;
1037         }
1038
1039         ret = func_get_folder(service_account_id, service_folder_id, &((*service_folder)->folder_name), &((*service_folder)->folder_type));
1040         if (ret != SYNC_AGENT_DA_SUCCESS) {
1041                 _DEBUG_INFO("[sync_agent_get_service_folder] failed func_get_folder()\n");
1042                 goto DACI_FINISH;
1043         } else {
1044                 _DEBUG_INFO("[sync_agent_get_service_folder] success func_get_folder()\n");
1045         }
1046
1047         (*service_folder)->folder_id = g_strdup(folder_id);
1048
1049  DACI_FINISH:
1050
1051         if (service_folder_id != NULL)
1052                 free(service_folder_id);
1053         if (fw_folder != NULL)
1054                 sync_agent_free_folder(fw_folder);
1055
1056         _EXTERN_FUNC_EXIT;
1057
1058         return ret;
1059 }
1060
1061 EXPORT_API sync_agent_da_return_e sync_agent_execute_service(int content_type, int account_id, const char *execute_key, void *execute_values, void **result)
1062 {
1063         _EXTERN_FUNC_ENTER;
1064
1065         retvm_if(execute_key == NULL, SYNC_AGENT_DA_ERRORS, "execute key is NULL !!");
1066         retvm_if(execute_values == NULL, SYNC_AGENT_DA_ERRORS, "execute value is NULL !!");
1067
1068         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1069         int service_account_id = 0;
1070         plugin_execute_cb func_execute;
1071
1072         /* 1. get service account id */
1073         service_account_id = acc_get_service_account_id(content_type, account_id);
1074         _DEBUG_INFO("[sync_agent_execute_service] service_acount_id : %d", service_account_id);
1075
1076         /* 2. call plugIn dataconnector */
1077         func_execute = plugin_get_function_execute(content_type);
1078
1079         retvm_if(func_execute == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_execute_cb !!");
1080
1081         ret = func_execute(service_account_id, execute_key, execute_values, result);
1082         if (ret != SYNC_AGENT_DA_SUCCESS) {
1083                 _DEBUG_INFO("[sync_agent_execute_service] failed pFunc_Execute()\n");
1084         } else {
1085                 _DEBUG_INFO("[sync_agent_execute_service] success pFunc_Execute()\n");
1086         }
1087
1088         _EXTERN_FUNC_EXIT;
1089
1090         return ret;
1091 }
1092
1093 EXPORT_API int sync_agent_get_used_service_item_count(int content_type)
1094 {
1095         _EXTERN_FUNC_ENTER;
1096
1097         plugin_get_used_item_count_cb func_get_used_count = plugin_get_function_get_used_item_count(content_type);
1098
1099         retvm_if(func_get_used_count == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_get_used_item_count_cb !!");
1100
1101         /* 1. call plugIn dataconnector */
1102         int used_count = func_get_used_count();
1103         if (used_count <= -1) {
1104                 _DEBUG_INFO("[sync_agent_get_used_service_item_count] failed pFunc_Get_Used_Count()\n");
1105         } else {
1106                 _DEBUG_INFO("[sync_agent_get_used_service_item_count] success pFunc_Get_Used_Count() : %d\n", used_count);
1107         }
1108
1109         _EXTERN_FUNC_EXIT;
1110
1111         return used_count;
1112 }
1113
1114 EXPORT_API int sync_agent_get_deleted_exdate_item_count(int content_type)
1115 {
1116         _EXTERN_FUNC_ENTER;
1117
1118         plugin_get_deleted_exdate_item_count func_get_used_count = plugin_get_function_get_deleted_exdate_item_count(content_type);
1119         retvm_if(func_get_used_count == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_get_deleted_exdate_item_count !!");
1120
1121         /* 1. call plugIn dataconnector */
1122         int used_count = func_get_used_count();
1123         if (used_count <= -1) {
1124                 _DEBUG_INFO("[sync_agent_get_deleted_exdate_item_count] failed pFunc_Get_Used_Count()\n");
1125         } else {
1126                 _DEBUG_INFO("[sync_agent_get_deleted_exdate_item_count] success pFunc_Get_Used_Count() : %d\n", used_count);
1127         }
1128
1129         _EXTERN_FUNC_EXIT;
1130         return used_count;
1131 }
1132
1133 EXPORT_API void sync_agent_construct_exdate_parent_item(int content_type, char *parent_service_id)
1134 {
1135         _EXTERN_FUNC_ENTER;
1136         retm_if(parent_service_id == NULL, "parent_service_id is NULL !!");
1137
1138         plugin_construct_exdate_parent_item func_construct_exdate_parent_item = plugin_function_construct_exdate_parent_item(content_type);
1139         goto_if(func_construct_exdate_parent_item == NULL, "cannot get plugin_construct_exdate_parent_item !!");
1140         func_construct_exdate_parent_item(parent_service_id);
1141
1142 return_part:
1143         _EXTERN_FUNC_EXIT;
1144         return;
1145 }
1146
1147
1148 EXPORT_API sync_agent_da_return_e sync_agent_is_exist_exdate_item(int content_type, const char *fw_parent_id, const char *child_vcalendar)
1149 {
1150         _EXTERN_FUNC_ENTER;
1151
1152         plugin_is_exist_exdate_item func_is_exist_exdate_item = plugin_function_is_exist_exdate_item(content_type);
1153         retvm_if(func_is_exist_exdate_item == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_is_exist_exdate_item !!");
1154
1155         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
1156         ret = func_is_exist_exdate_item(fw_parent_id, child_vcalendar);
1157
1158         _EXTERN_FUNC_EXIT;
1159         return ret;
1160 }
1161
1162 EXPORT_API int sync_agent_get_used_service_item_count_for_folder(int content_type, int account_id, char *folder_id)
1163 {
1164         _EXTERN_FUNC_ENTER;
1165
1166         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERRORS, "folder id is NULL !!");
1167
1168         int used_count = SYNC_AGENT_DA_SUCCESS;
1169         int service_account_id = 0;
1170         char *service_folder_id = NULL;
1171         plugin_get_used_count_for_folder_cb func_get_used_item_count_for_folder;
1172
1173         /* 1. get service account id */
1174         service_account_id = acc_get_service_account_id(content_type, account_id);
1175         _DEBUG_INFO("[sync_agent_get_used_service_item_count_for_folder] service_acount_id : %d", service_account_id);
1176
1177         /* 2. get service folder id */
1178         service_folder_id = sync_agent_get_service_folder_id(folder_id);
1179         _DEBUG_INFO("[sync_agent_get_used_service_item_count_for_folder] service_folder_id : %s", service_folder_id);
1180
1181         /* 3. call plugIn dataconnector */
1182         func_get_used_item_count_for_folder = plugin_get_function_get_used_count_for_folder(content_type);
1183         if (func_get_used_item_count_for_folder == NULL) {
1184                 _DEBUG_ERROR("%d", __LINE__);
1185                 used_count = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
1186                 goto DACI_FINISH;
1187         }
1188
1189         used_count = func_get_used_item_count_for_folder(service_account_id, service_folder_id);
1190         if (used_count <= -1) {
1191                 _DEBUG_INFO("[sync_agent_get_used_service_item_count_for_folder] failed pFunc_Get_Used_item_Count_For_Folder()\n");
1192         } else {
1193                 _DEBUG_INFO("[sync_agent_get_used_service_item_count_for_folder] success pFunc_Get_Used_item_Count_For_Folder() : %d\n", used_count);
1194         }
1195
1196  DACI_FINISH:
1197
1198         /* memory free */
1199         if (service_folder_id != NULL)
1200                 free(service_folder_id);
1201
1202         _EXTERN_FUNC_EXIT;
1203
1204         return used_count;
1205 }
1206
1207 EXPORT_API sync_agent_da_return_e sync_agent_backup_service_items_to_file(int content_type, int account_id, char **file_path)
1208 {
1209         _EXTERN_FUNC_ENTER;
1210
1211         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1212         int service_account_id = 0;
1213         plugin_backup_service_items_to_file_cb func_write_items_to_files;
1214
1215         /* 1. get service account id */
1216         service_account_id = acc_get_service_account_id(content_type, account_id);
1217         _DEBUG_INFO("service_acount_id : %d", service_account_id);
1218
1219         /* 3. call plugIn dataconnector */
1220         func_write_items_to_files = plugin_get_function_backup_service_items_to_file(content_type);
1221         if (func_write_items_to_files == NULL) {
1222                 _DEBUG_ERROR("%d", __LINE__);
1223                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
1224                 goto DACI_FINISH;
1225         }
1226
1227         ret = func_write_items_to_files(service_account_id, file_path);
1228         if (ret != SYNC_AGENT_DA_SUCCESS) {
1229                 _DEBUG_INFO("failed pFunc_Write_Items_To_Files()\n");
1230         } else {
1231                 _DEBUG_INFO("success pFunc_Write_Items_To_Files() : %d\n", ret);
1232         }
1233
1234  DACI_FINISH:
1235
1236         _EXTERN_FUNC_EXIT;
1237
1238         return ret;
1239 }
1240
1241 EXPORT_API sync_agent_da_return_e sync_agent_restore_service_items_from_file(int content_type, int account_id, const char *file_path)
1242 {
1243         _EXTERN_FUNC_ENTER;
1244
1245         retvm_if(file_path == NULL, SYNC_AGENT_DA_ERRORS, "file path is NULL !!");
1246
1247         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1248         int service_account_id = 0;
1249         plugin_restore_service_items_from_file_cb func_add_file_wrote_items_to_datastore;
1250
1251         /* 1. get service account id */
1252         service_account_id = acc_get_service_account_id(content_type, account_id);
1253         _DEBUG_INFO("service_acount_id : %d", service_account_id);
1254
1255         /* 3. call plugIn dataconnector */
1256         func_add_file_wrote_items_to_datastore = plugin_get_function_restore_service_items_from_file(content_type);
1257         if (func_add_file_wrote_items_to_datastore == NULL) {
1258                 _DEBUG_ERROR("%d", __LINE__);
1259                 ret = SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
1260                 goto DACI_FINISH;
1261         }
1262
1263         ret = func_add_file_wrote_items_to_datastore(service_account_id, file_path);
1264         if (ret != SYNC_AGENT_DA_SUCCESS) {
1265                 _DEBUG_INFO("failed pFunc_Add_File_Wrote_Items_To_Datastore()\n");
1266         } else {
1267                 _DEBUG_INFO("success pFunc_Add_File_Wrote_Items_To_Datastore() : %d\n", ret);
1268         }
1269
1270  DACI_FINISH:
1271
1272         _EXTERN_FUNC_EXIT;
1273
1274         return ret;
1275 }
1276
1277 EXPORT_API char *sync_agent_get_service_item_id(char *item_id)
1278 {
1279         _EXTERN_FUNC_ENTER;
1280
1281         retvm_if(item_id == NULL, NULL, "item id is NULL !!");
1282
1283         SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
1284         if (daci_handler == NULL) {
1285                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
1286                 return NULL;
1287         }
1288
1289         _EXTERN_FUNC_EXIT;
1290
1291         return da_get_item_service_id(daci_handler, item_id);
1292 }
1293
1294 EXPORT_API char *sync_agent_get_service_folder_id(char *folder_id)
1295 {
1296         _EXTERN_FUNC_ENTER;
1297
1298         retvm_if(folder_id == NULL, NULL, "folder id is NULL !!");
1299
1300         SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
1301         if (daci_handler == NULL) {
1302                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
1303                 return NULL;
1304         }
1305
1306         _EXTERN_FUNC_EXIT;
1307
1308         return da_get_folder_service_id(daci_handler, folder_id);
1309 }
1310
1311 EXPORT_API int sync_agent_get_sim_contact_info_max_name_length(int content_type)
1312 {
1313         _EXTERN_FUNC_ENTER;
1314
1315         plugin_get_info_sim_contact_max_name_length_cb func_get_info_sim_contact_max_name_length = plugin_get_function_get_info_sim_contact_max_name_length(content_type);
1316         retvm_if(func_get_info_sim_contact_max_name_length == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_get_info_sim_contact_max_name_length_cb !!");
1317
1318         int maxNameLength = func_get_info_sim_contact_max_name_length();
1319
1320         _EXTERN_FUNC_EXIT;
1321         return maxNameLength;
1322 }
1323
1324 EXPORT_API int sync_agent_get_sim_contact_info_max_number_length(int content_type)
1325 {
1326         _EXTERN_FUNC_ENTER;
1327
1328         plugin_get_info_sim_contact_max_number_length_cb func_get_info_sim_contact_max_number_length = plugin_get_function_get_info_sim_contact_max_number_length(content_type);
1329         retvm_if(func_get_info_sim_contact_max_number_length == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_get_info_sim_contact_max_number_length_cb !!");
1330
1331         int maxNumberLength = func_get_info_sim_contact_max_number_length();
1332
1333         _EXTERN_FUNC_EXIT;
1334         return maxNumberLength;
1335 }
1336
1337 EXPORT_API int sync_agent_get_sim_contact_info_max_email_length(int content_type)
1338 {
1339         _EXTERN_FUNC_ENTER;
1340
1341         plugin_get_info_sim_contact_max_email_length_cb func_get_info_sim_contact_max_email_length = plugin_get_function_get_info_sim_contact_max_email_length(content_type);
1342         retvm_if(func_get_info_sim_contact_max_email_length == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_get_info_sim_contact_max_email_length_cb !!");
1343
1344         int maxEmailLength = func_get_info_sim_contact_max_email_length();
1345
1346         _EXTERN_FUNC_EXIT;
1347         return maxEmailLength;
1348 }
1349
1350 EXPORT_API int sync_agent_get_sim_contact_empty_count(int content_type)
1351 {
1352         _EXTERN_FUNC_ENTER;
1353
1354         plugin_get_info_sim_contact_empty_count_cb func_get_info_sim_contact_empty_count = plugin_get_function_get_info_sim_contact_empty_count(content_type);
1355         retvm_if(func_get_info_sim_contact_empty_count == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_get_info_sim_contact_empty_count_cb !!");
1356
1357         int emptyCount = func_get_info_sim_contact_empty_count();
1358
1359         _EXTERN_FUNC_EXIT;
1360         return emptyCount;
1361 }
1362
1363 EXPORT_API int sync_agent_get_sim_contact_empty_number_count(int content_type)
1364 {
1365         _EXTERN_FUNC_ENTER;
1366
1367         plugin_get_info_sim_contact_empty_number_count_cb func_get_info_sim_contact_empty_number_count = plugin_get_function_get_info_sim_contact_empty_number_count(content_type);
1368         retvm_if(func_get_info_sim_contact_empty_number_count == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_get_info_sim_contact_empty_number_count_cb !!");
1369
1370         int emptyNumberCount = func_get_info_sim_contact_empty_number_count();
1371
1372         _EXTERN_FUNC_EXIT;
1373         return emptyNumberCount;
1374 }
1375
1376 EXPORT_API int sync_agent_get_sim_contact_empty_email_count(int content_type)
1377 {
1378         _EXTERN_FUNC_ENTER;
1379
1380         plugin_get_info_sim_contact_empty_email_count_cb func_get_info_sim_contact_empty_email_count = plugin_get_function_get_info_sim_contact_empty_email_count(content_type);
1381         retvm_if(func_get_info_sim_contact_empty_email_count == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_get_info_sim_contact_empty_email_count_cb !!");
1382
1383         int emptyEmailCount = func_get_info_sim_contact_empty_email_count();
1384
1385         _EXTERN_FUNC_EXIT;
1386         return emptyEmailCount;
1387 }
1388
1389 EXPORT_API int sync_agent_get_sim_contact_addressbook_id(int content_type)
1390 {
1391         _EXTERN_FUNC_ENTER;
1392
1393         plugin_get_info_sim_contact_addressbook_id_cb func_get_info_sim_contact_addressbook_id = plugin_get_function_get_info_sim_contact_addressbook_id(content_type);
1394         retvm_if(func_get_info_sim_contact_addressbook_id == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get plugin_get_info_sim_contact_addressbook_id !!");
1395
1396         int simContactAddressbookId = func_get_info_sim_contact_addressbook_id();
1397
1398         _EXTERN_FUNC_EXIT;
1399         return simContactAddressbookId;
1400 }
1401
1402 EXPORT_API sync_agent_da_return_e sync_agent_get_sim_contact_item_id(int content_type, int sim_addressbook_id, GList **item_id)
1403 {
1404         _EXTERN_FUNC_ENTER;
1405
1406         sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
1407
1408         plugin_get_info_sim_contact_item_id_cb func_get_info_sim_contact_item_id = plugin_get_function_get_info_sim_contact_item_id(content_type);
1409         retvm_if(func_get_info_sim_contact_item_id == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get func_get_info_sim_contact_item_id !!");
1410
1411         GList *item_id_list = NULL;
1412         da_ret = func_get_info_sim_contact_item_id(sim_addressbook_id, &item_id_list);
1413         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
1414                 _DEBUG_ERROR("func_get_info_sim_contact_item_id() fail, da_ret[%d]", da_ret);
1415         }
1416         *item_id = item_id_list;
1417
1418         _EXTERN_FUNC_EXIT;
1419         return da_ret;
1420 }
1421
1422 EXPORT_API sync_agent_da_return_e sync_agent_get_sim_contact_item(int content_type, int item_id, char **data)
1423 {
1424         _EXTERN_FUNC_ENTER;
1425
1426         sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
1427
1428         plugin_get_info_sim_contact_item_cb func_get_info_sim_contact_item = plugin_get_function_get_info_sim_contact_item(content_type);
1429         retvm_if(func_get_info_sim_contact_item == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get func_get_info_sim_contact_item !!");
1430
1431         char *item_data = NULL;
1432         da_ret = func_get_info_sim_contact_item(item_id, &item_data);
1433         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
1434                 _DEBUG_ERROR("func_get_info_sim_contact_item() fail, da_ret[%d]", da_ret);
1435         }
1436         *data = item_data;
1437
1438         _EXTERN_FUNC_EXIT;
1439         return da_ret;
1440 }
1441
1442 EXPORT_API sync_agent_da_return_e sync_agent_add_sim_contact_item(int content_type, int sim_addressbook_id, int **item_id, char *data)
1443 {
1444         _EXTERN_FUNC_ENTER;
1445
1446         sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
1447
1448         plugin_add_sim_contact_item_cb func_add_sim_contact_item = plugin_get_function_add_sim_contact_item(content_type);
1449         retvm_if(func_add_sim_contact_item == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get func_add_sim_contact_item !!");
1450
1451         int *item_id_tmp = NULL;
1452         da_ret = func_add_sim_contact_item(sim_addressbook_id, &item_id_tmp, data);
1453         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
1454                 _DEBUG_ERROR("func_add_sim_contact_item() fail, da_ret[%d]", da_ret);
1455         }
1456
1457         *item_id = item_id_tmp;
1458
1459         _EXTERN_FUNC_EXIT;
1460         return da_ret;
1461 }
1462
1463 EXPORT_API sync_agent_da_return_e sync_agent_write_sim_contact_item(int content_type, int item_id, char *data)
1464 {
1465         _EXTERN_FUNC_ENTER;
1466
1467         sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
1468
1469         plugin_write_sim_contact_item_cb func_write_sim_contact_item = plugin_get_function_write_sim_contact_item(content_type);
1470         retvm_if(func_write_sim_contact_item == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get func_write_sim_contact_item !!");
1471
1472         da_ret = func_write_sim_contact_item(item_id, data);
1473         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
1474                 _DEBUG_ERROR("func_write_sim_contact_item() fail, da_ret[%d]", da_ret);
1475         }
1476
1477         _EXTERN_FUNC_EXIT;
1478         return da_ret;
1479 }
1480
1481 EXPORT_API sync_agent_da_return_e sync_agent_delete_sim_contact_item(int content_type, int item_id)
1482 {
1483         _EXTERN_FUNC_ENTER;
1484
1485         sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
1486
1487         plugin_delete_sim_contact_item_cb func_delete_sim_contact_item = plugin_get_function_delete_sim_contact_item(content_type);
1488         retvm_if(func_delete_sim_contact_item == NULL, SYNC_AGENT_DA_NOT_FOUND_PLUG_IN, "cannot get func_delete_sim_contact_item !!");
1489
1490         da_ret = func_delete_sim_contact_item(item_id);
1491         if (da_ret != SYNC_AGENT_DA_SUCCESS) {
1492                 _DEBUG_ERROR("func_delete_sim_contact_item() fail, da_ret[%d]", da_ret);
1493         }
1494
1495         _EXTERN_FUNC_EXIT;
1496         return da_ret;
1497 }
1498