Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / src / agent / service-engine / se_sync.c
1 /*
2  * oma-ds-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 /**
19  *   @SE_Sync.c
20  *   @version                                                                   0.1
21  *   @brief                                                                             This file is the source file of implementation of functions which process sync request, auto configure etc..
22  */
23
24 #include <sys/time.h>
25
26 #include <sync_agent.h>
27 #include <plugin/plugin_slp_device.h>
28
29 #include "service-engine/se_sync.h"
30 #include "service-engine/se_storage.h"
31 #include "service-engine/se_common.h"
32 #include "service-engine/se_notification.h"
33 #include "service-adapter/sa_common_interface.h"
34 #include "common/common_util.h"
35 #include "common/common_define_internal.h"
36
37 #ifndef OMADS_AGENT_LOG
38 #undef LOG_TAG
39 #define LOG_TAG "OMA_DS_SE"
40 #endif
41
42 static se_error_type_e _session_process(int account_id, alert_type_e server_sync_type, sync_progress_e process, sync_error_e error);
43 static se_error_type_e __process_update(int account_id, alert_type_e server_sync_type, sync_progress_status_e progress_status, operation_type_e operation_type, int content_type, bool is_from_server, bool need_to_save, sync_result_s * sync_result);
44 static se_error_type_e _write_sync_data(int account_id, alert_type_e alert_type, sync_session_result_e sync_session_result, int last_session_time, int only_from_client);
45 static se_error_type_e ___write_sync_resource_info(int account_id, int content_type, int last_session_time, int only_from_client, sync_result_s * client_sync_result, sync_result_s * server_sync_result);
46
47 static se_error_type_e __init_datastore_info_array(int account_id);
48 static se_error_type_e ___set_datastore_info_array(int account_id, char *config_key, service_type_e content_type);
49 static se_error_type_e ___generate_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore);
50 static se_error_type_e __init_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore);
51 static se_error_type_e ___set_datastore_config(int account_id, int content_type, datastore_s ** datastore);
52 static se_error_type_e __get_sync_type(int account_id, alert_type_e * sync_type);
53
54 static se_error_type_e __on_synchronising_account(int account_id);
55 static se_error_type_e _off_synchronising_account(int account_id);
56
57 static se_error_type_e __on_resume_flag(int account_id);
58 static se_error_type_e _off_resume_flag(int account_id);
59
60 static se_error_type_e _assemble_changed_datastores(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj);
61 static se_error_type_e _prepare_pre_sync(int account_id, char *sync_mode, san_package_s * san_package, alert_type_e * sync_type);
62 static se_error_type_e __set_config_based_on_sync_mode(int account_id, char *sync_mode, san_package_s * san_package);
63 static se_error_type_e _execute_pre_sync(int account_id, int session_time, pre_sync_return_obj_s * pre_sync_return_obj, alert_type_e * server_sync_type);
64 static se_error_type_e __execute_pre_sync_set_server_id(int account_id, char *dev_id);
65 static se_error_type_e __execute_pre_sync_datastore(int account_id, int session_time, GList * datastore_info, alert_type_e * server_sync_type);
66 static se_error_type_e _execute_sync(int account_id, alert_type_e client_sync_type, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj);
67 static se_error_type_e __execute_sync_arrange_changelog(int account_id, alert_type_e sync_type, GList * status);
68 static se_error_type_e __execute_sync_status(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj);
69 static se_error_type_e __execute_sync_change(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj);
70 static se_error_type_e _update_sync_result(int account_id);
71
72 static command_result_e ___convert_return_status(sync_agent_da_return_e da_err);
73 static char *__convert_cttype_str(int itemTypeId);
74 static int ___convert_sync_type_value(char *sync_type_str);
75 static char *__convert_sync_type_str(alert_type_e sync_type);
76 static char *___convert_sync_progress_status_str(sync_progress_status_e progress_status);
77 static char *___convert_operation_type_str(operation_type_e operation_type);
78 static char *_convert_sync_progress_str(sync_progress_e process);
79 static char *_convert_sync_error_str(sync_error_e error);
80 static se_error_type_e _check_low_battery();
81 static se_error_type_e _open_services();
82 static se_error_type_e _close_services();
83
84 static inline long myclock()
85 {
86         struct timeval tv;
87         gettimeofday(&tv, 0);
88         return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
89 }
90
91 static se_error_type_e _session_process(int account_id, alert_type_e server_sync_type, sync_progress_e process, sync_error_e error)
92 {
93         _INNER_FUNC_ENTER;
94
95         char *profileDirName = NULL;
96         se_error_type_e err = SE_INTERNAL_OK;
97         bool result;
98
99         result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
100         if (result == false) {
101                 _DEBUG_ERROR("failed in get_Config");
102                 err = SE_INTERNAL_DA_ERROR;
103                 goto error;
104         }
105
106         if (profileDirName != NULL) {
107                 err = session_process(profileDirName, server_sync_type, process, error);
108                 if (err != SE_INTERNAL_OK) {
109                         _DEBUG_ERROR("failed to send noti");
110                         goto error;
111                 }
112         }
113
114  error:
115         if (profileDirName != NULL)
116                 free(profileDirName);
117
118         _INNER_FUNC_EXIT;
119
120         return err;
121 }
122
123 static se_error_type_e __process_update(int account_id, alert_type_e server_sync_type, sync_progress_status_e progress_status, operation_type_e operation_type, int content_type, bool is_from_server, bool need_to_save, sync_result_s * sync_result)
124 {
125         _INNER_FUNC_ENTER;
126
127         _DEBUG_VERBOSE("account_id =%d, progress_status = %d, operation_type = %d, content_type = %d, isFromServer = %d, needToSave = %d ", account_id, progress_status, operation_type, content_type, is_from_server, need_to_save);
128         _DEBUG_VERBOSE("numberOfChange = %d, received_count = %d,  syncCount = %d", sync_result->number_of_change, sync_result->received_count, sync_result->add_count + sync_result->replace_count + sync_result->delete_count);
129
130         se_error_type_e err = SE_INTERNAL_OK;
131         char *profileDirName = NULL;
132         char *uri = NULL;
133         char *sync_type = NULL;
134         char *operation = NULL;
135         char *progress = NULL;
136
137         bool result;
138         result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
139         if (result == false) {
140                 _DEBUG_ERROR("failed in get_Config");
141                 err = SE_INTERNAL_DA_ERROR;
142                 goto error;
143         }
144
145         /* do not need to send update noti to ui */
146         if (profileDirName == NULL) {
147                 _DEBUG_VERBOSE("profileDirName is NULL");
148                 goto error;
149         }
150
151         if (need_to_save == true) {
152                 err = write_sync_statistics(account_id, content_type, is_from_server, sync_result);
153                 if (err != SE_INTERNAL_OK) {
154                         _DEBUG_ERROR("failed in writeSyncStatistics");
155                         goto error;
156                 }
157         }
158
159         sync_type = __convert_sync_type_str(server_sync_type);
160
161         progress = ___convert_sync_progress_status_str(progress_status);
162
163         operation = ___convert_operation_type_str(operation_type);
164
165         if (sync_type == NULL || progress == NULL || operation == NULL) {
166                 err = SE_INTERNAL_NOT_DEFINED;
167                 goto error;
168         }
169
170         if (datastoreinfo_per_content_type[content_type] != NULL) {
171                 if (datastoreinfo_per_content_type[content_type]->source != NULL)
172                         uri = strdup(datastoreinfo_per_content_type[content_type]->source);
173         }
174
175         err = send_noti_process_update(profileDirName, sync_type, uri, progress, operation, is_from_server, 0, 0, sync_result->number_of_change, sync_result->add_count + sync_result->replace_count + sync_result->delete_count);
176         if (err != SE_INTERNAL_OK) {
177                 _DEBUG_ERROR("failed in send_noti_process_update");
178                 goto error;
179         }
180
181  error:
182
183         if (profileDirName != NULL)
184                 free(profileDirName);
185
186         if (uri != NULL)
187                 free(uri);
188
189         _INNER_FUNC_EXIT;
190         return err;
191 }
192
193 static se_error_type_e _write_sync_data(int account_id, alert_type_e alert_type, sync_session_result_e sync_session_result, int last_session_time, int only_from_client)
194 {
195         _INNER_FUNC_ENTER;
196
197         se_error_type_e err = SE_INTERNAL_OK;
198
199         err = write_profile_data(account_id, alert_type, sync_session_result, last_session_time, only_from_client);
200         if (err != SE_INTERNAL_OK) {
201                 _DEBUG_ERROR("failed in writeProfileData");
202                 goto error;
203         }
204
205  error:
206
207         _INNER_FUNC_EXIT;
208         return err;
209 }
210
211 static se_error_type_e ___write_sync_resource_info(int account_id, int content_type, int last_session_time, int only_from_client, sync_result_s * client_sync_result, sync_result_s * server_sync_result)
212 {
213         _INNER_FUNC_ENTER;
214
215         se_error_type_e err = SE_INTERNAL_OK;
216
217         err = write_sync_resource_info(account_id, content_type, last_session_time, only_from_client, client_sync_result, server_sync_result);
218         if (err != SE_INTERNAL_OK) {
219                 _DEBUG_ERROR("failed in write_sync_resource_info");
220                 goto error;
221         }
222
223  error:
224
225         _INNER_FUNC_EXIT;
226         return err;
227 }
228
229 static se_error_type_e __init_datastore_info_array(int account_id)
230 {
231         _INNER_FUNC_ENTER;
232
233         se_error_type_e err = SE_INTERNAL_OK;
234
235         err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, TYPE_CONTACT);
236         if (err != SE_INTERNAL_OK) {
237                 _DEBUG_ERROR("failed in set_datastoreinfo_array");
238                 goto error;
239         }
240
241         err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, TYPE_CALENDAR);
242         if (err != SE_INTERNAL_OK) {
243                 _DEBUG_ERROR("failed in set_datastoreinfo_array");
244                 goto error;
245         }
246
247         err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, TYPE_MEMO);
248         if (err != SE_INTERNAL_OK) {
249                 _DEBUG_ERROR("failed in set_datastoreinfo_array");
250                 goto error;
251         }
252
253         err = ___set_datastore_info_array(account_id, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, TYPE_CALLLOG);
254         if (err != SE_INTERNAL_OK) {
255                 _DEBUG_ERROR("failed in set_datastoreinfo_array");
256                 goto error;
257         }
258
259  error:
260
261         _INNER_FUNC_EXIT;
262         return err;
263 }
264
265 static se_error_type_e ___set_datastore_info_array(int account_id, char *config_key, service_type_e content_type)
266 {
267         _INNER_FUNC_ENTER;
268
269         se_error_type_e err = SE_INTERNAL_OK;
270
271         datastore_s *datastore_info = NULL;
272         bool result;
273         char *value = NULL;
274
275         result = get_config(account_id, config_key, &value);
276         if (result == true) {
277                 if (strcmp(value, "1") == 0) {
278                         err = ___generate_datastore_info(account_id, content_type, &datastore_info);
279                         if (err != SE_INTERNAL_OK) {
280                                 _DEBUG_ERROR("failed to create or init datastore_info");
281                                 datastoreinfo_per_content_type[content_type] = NULL;
282                                 goto error;
283                         }
284                         datastoreinfo_per_content_type[content_type] = datastore_info;
285                 }
286
287         } else
288                 datastoreinfo_per_content_type[content_type] = NULL;
289
290  error:
291
292         if (value != NULL)
293                 free(value);
294
295         _INNER_FUNC_EXIT;
296         return err;
297 }
298
299 static se_error_type_e ___generate_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore)
300 {
301         _INNER_FUNC_ENTER;
302         _DEBUG_VERBOSE("content_type=[%d]\n", content_type);
303
304         se_error_type_e err = SE_INTERNAL_OK;
305         char *clientLastAnchor = NULL;
306         char *clientNextAnchor = NULL;
307         char *serverLastAnchor = NULL;
308
309         sync_agent_da_return_e da_err = SYNC_AGENT_DA_ERRORS;
310
311         sync_result_s *pSyncResult = NULL;
312         datastore_s *temp_datastore = NULL;
313         err = ___set_datastore_config(account_id, content_type, &temp_datastore);
314         if (err != SE_INTERNAL_OK) {
315                 _DEBUG_ERROR("failed in set_datastore_config");
316                 goto error;
317         }
318
319         GList *anchor_list = NULL;
320         sync_agent_da_get_last_anchor_list_query_s query;
321         query.option = SYNC_AGENT_DA_GET_LAST_ANCHOR_LIST_OPTION_ITEM_TYPE_ID;
322         query.account_id = account_id;
323         query.item_type_id = temp_datastore->datastore_id;
324
325         da_err = sync_agent_get_last_anchor_list(&query, &anchor_list);
326         if (da_err != SYNC_AGENT_DA_SUCCESS) {
327                 _DEBUG_ERROR("failed in sync_agent_get_last_anchor_list !!");
328                 goto error;
329         }
330
331         if (g_list_length(anchor_list) > 0) {
332                 GList *last_anchor_info = g_list_nth(anchor_list, g_list_length(anchor_list) - 1);
333                 clientLastAnchor = ((sync_agent_da_last_anchor_s *) (last_anchor_info->data))->last_anchor_client;
334                 serverLastAnchor = ((sync_agent_da_last_anchor_s *) (last_anchor_info->data))->last_anchor_server;
335         } else {
336                 sync_agent_da_last_anchor_s lastAnchor_daci;
337
338                 lastAnchor_daci.account_id = account_id;
339                 lastAnchor_daci.data_store_id = temp_datastore->datastore_id;
340                 lastAnchor_daci.last_anchor_server = NULL;
341                 lastAnchor_daci.last_anchor_client = NULL;
342                 lastAnchor_daci.access_name = "Engine";
343
344                 da_err = sync_agent_add_last_anchor(&lastAnchor_daci);
345                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
346                         _DEBUG_ERROR("da_add_last_anchor_internal is failed %d", da_err);
347                         err = SE_INTERNAL_DA_ERROR;
348                         goto error;
349                 }
350         }
351
352         clientNextAnchor = g_strdup_printf("%ld", time(NULL));
353
354         _DEBUG_VERBOSE("clientLastAnchor = %s\n", clientLastAnchor);
355         _DEBUG_VERBOSE("serverLastAnchor = %s\n", serverLastAnchor);
356         _DEBUG_VERBOSE("clientNextAnchor = %s\n", clientNextAnchor);
357
358         /*slow sync if managed anchor info not exist.. (first sync) */
359         if (clientLastAnchor == NULL)
360                 set_datastore_client_anchor(temp_datastore, clientNextAnchor, clientNextAnchor);
361         else
362                 set_datastore_client_anchor(temp_datastore, clientLastAnchor, clientNextAnchor);
363
364         set_datastore_server_anchor(temp_datastore, serverLastAnchor, NULL);
365
366         pSyncResult = create_sync_result();
367         if (pSyncResult == NULL) {
368                 _DEBUG_ERROR("create_syncResult is failed");
369                 err = SE_INTERNAL_NO_MEMORY;
370                 goto error;
371         }
372         temp_datastore->client_sync_result = pSyncResult;
373
374         pSyncResult = create_sync_result();
375         if (pSyncResult == NULL) {
376                 _DEBUG_ERROR("create_syncResult is failed");
377                 err = SE_INTERNAL_NO_MEMORY;
378                 goto error;
379         }
380         temp_datastore->server_sync_result = pSyncResult;
381
382         *datastore = temp_datastore;
383         temp_datastore = NULL;
384
385  error:
386
387         if (clientNextAnchor != NULL)
388                 free(clientNextAnchor);
389
390         if (temp_datastore != NULL)
391                 free_datastore(temp_datastore);
392
393         _INNER_FUNC_EXIT;
394         return err;
395 }
396
397 static se_error_type_e __init_datastore_info(int account_id, service_type_e content_type, datastore_s ** datastore)
398 {
399         _INNER_FUNC_ENTER;
400
401         se_error_type_e err = SE_INTERNAL_OK;
402         char *sourceDatastore = NULL;
403         char key[128];
404         datastore_s *temp_datastore = NULL;
405         sync_result_s *pSyncResult = NULL;
406
407         bool result;
408         switch (content_type) {
409         case TYPE_CONTACT:
410                 snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
411                 break;
412         case TYPE_CALENDAR:
413                 snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
414                 break;
415         case TYPE_MEMO:
416                 snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
417                 break;
418         case TYPE_CALLLOG:
419                 snprintf(key, sizeof(key), "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
420                 break;
421         default:
422                 _DEBUG_VERBOSE("unknown content type = %d", content_type);
423                 goto error;
424         }
425
426         _DEBUG_ERROR("content_type = %d", content_type);
427         _DEBUG_ERROR("key = %s", key);
428         result = get_config(account_id, key, &sourceDatastore);
429         if (result == false) {
430                 _DEBUG_ERROR("failed in get_Config");
431                 err = SE_INTERNAL_DA_ERROR;
432                 goto error;
433         }
434
435         temp_datastore = create_datastore(NULL, sourceDatastore);
436         if (temp_datastore == NULL) {
437                 _DEBUG_ERROR("failed to create_Datastore");
438                 err = SE_INTERNAL_NO_MEMORY;
439                 goto error;
440         }
441
442         pSyncResult = create_sync_result();
443         if (pSyncResult == NULL) {
444                 _DEBUG_ERROR("create_syncResult is failed");
445                 err = SE_INTERNAL_NO_MEMORY;
446                 goto error;
447         }
448         temp_datastore->client_sync_result = pSyncResult;
449
450         pSyncResult = create_sync_result();
451         if (pSyncResult == NULL) {
452                 _DEBUG_ERROR("create_syncResult is failed");
453                 err = SE_INTERNAL_NO_MEMORY;
454                 goto error;
455         }
456         temp_datastore->server_sync_result = pSyncResult;
457
458         *datastore = temp_datastore;
459         temp_datastore = NULL;
460
461  error:
462         if (sourceDatastore != NULL)
463                 free(sourceDatastore);
464
465         if (temp_datastore != NULL)
466                 free_datastore(temp_datastore);
467
468         _INNER_FUNC_EXIT;
469         return err;
470 }
471
472 static se_error_type_e ___set_datastore_config(int account_id, int content_type, datastore_s ** datastore)
473 {
474         _INNER_FUNC_ENTER;
475
476         datastore_s *temp_datastore = NULL;
477         se_error_type_e err = SE_INTERNAL_OK;
478         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
479
480         char *contentType = NULL;
481
482         char sourceDatastore_key[128];
483         char targetDatastore_key[128];
484         char id_key[128];
485         char pw_key[128];
486
487         char *sourceDatastore = NULL;
488         char *targetDatastore = NULL;
489         char *sync_type = NULL;
490         char *id = NULL;
491         char *pw = NULL;
492
493         alert_type_e alert_type = ALERT_UNKNOWN;
494
495         GList *config_list = NULL;
496         GList *iter = NULL;
497         sync_agent_da_config_s *config_data = NULL;
498
499         int folder_type_id = 0;
500         switch (content_type) {
501         case TYPE_CONTACT:
502                 {
503                         folder_type_id = 0;
504                         contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS;
505                 }
506                 break;
507         case TYPE_CALENDAR:
508                 {
509                         folder_type_id = 0;
510                         contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR;
511                 }
512                 break;
513         case TYPE_MEMO:
514                 {
515                         folder_type_id = 0;
516                         contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO;
517                 }
518                 break;
519         case TYPE_CALLLOG:
520                 {
521                         folder_type_id = 0;
522                         contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG;
523                 }
524                 break;
525         default:
526                 _DEBUG_VERBOSE("unknown content type = %d", content_type);
527                 err = SE_INTERNAL_ERROR;
528                 goto error;
529         }
530
531         snprintf(sourceDatastore_key, sizeof(sourceDatastore_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
532         snprintf(targetDatastore_key, sizeof(targetDatastore_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_TARGET);
533         snprintf(id_key, sizeof(id_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_ID);
534         snprintf(pw_key, sizeof(id_key), "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_PASSWORD);
535
536         da_err = sync_agent_get_config_list(account_id, &config_list);
537         if (da_err != SYNC_AGENT_DA_SUCCESS) {
538                 err = SE_INTERNAL_DA_ERROR;
539                 goto error;
540         }
541
542         for (iter = config_list; iter != NULL; iter = g_list_next(iter)) {
543                 config_data = (sync_agent_da_config_s *) iter->data;
544
545                 if (config_data != NULL) {
546                         if (config_data->key != NULL) {
547                                 if (strcmp(config_data->key, sourceDatastore_key) == 0) {
548                                         if (config_data->value != NULL)
549                                                 sourceDatastore = strdup(config_data->value);
550                                 }
551
552                                 if (strcmp(config_data->key, targetDatastore_key) == 0) {
553                                         if (config_data->value != NULL)
554                                                 targetDatastore = strdup(config_data->value);
555                                 }
556
557                                 if (strcmp(config_data->key, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE) == 0) {
558                                         if (config_data->value != NULL)
559                                                 sync_type = strdup(config_data->value);
560                                 }
561
562                                 if (strcmp(config_data->key, id_key) == 0) {
563                                         if (config_data->value != NULL)
564                                                 id = strdup(config_data->value);
565                                 }
566
567                                 if (strcmp(config_data->key, pw_key) == 0) {
568                                         if (config_data->value != NULL)
569                                                 pw = strdup(config_data->value);
570                                 }
571                         }
572                 }
573         }
574
575         if (sync_type != NULL) {
576                 alert_type = ___convert_sync_type_value(sync_type);
577         } else {
578                 _DEBUG_ERROR("sync_type is NULL !");
579                 err = SE_INTERNAL_ERROR;
580                 goto error;
581         }
582
583         _DEBUG_VERBOSE("get_Config result  sourceDatastore= %s ", sourceDatastore);
584         _DEBUG_VERBOSE("get_Config result  targetDatastore= %s ", targetDatastore);
585         _DEBUG_VERBOSE("get_Config result  alert_type= %s ", sync_type);
586         _DEBUG_VERBOSE("get_Config result  id= %s ", id);
587         _DEBUG_VERBOSE("get_Config result  pw= %s ", pw);
588
589         temp_datastore = create_datastore(targetDatastore, sourceDatastore);
590         if (temp_datastore == NULL) {
591                 _DEBUG_ERROR("temp_datastore is NULL");
592                 err = SE_INTERNAL_ERROR;
593                 goto error;
594         }
595
596         set_datastore_content_type_info(temp_datastore, content_type, folder_type_id);
597         set_datastore_account_info(temp_datastore, id, pw);
598         set_datastore_client_sync_type(temp_datastore, alert_type);
599
600         *datastore = temp_datastore;
601         temp_datastore = NULL;
602
603  error:
604
605         if (sourceDatastore != NULL)
606                 free(sourceDatastore);
607
608         if (targetDatastore != NULL)
609                 free(targetDatastore);
610
611         if (sync_type != NULL)
612                 free(sync_type);
613
614         if (id != NULL)
615                 free(id);
616
617         if (pw != NULL)
618                 free(pw);
619
620         _INNER_FUNC_EXIT;
621         return err;
622 }
623
624 static se_error_type_e __get_sync_type(int account_id, alert_type_e * alert_type)
625 {
626         _INNER_FUNC_ENTER;
627
628         se_error_type_e err = SE_INTERNAL_OK;
629         char *sync_type = NULL;
630
631         bool result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_CLIENT_SYNC_TYPE, &sync_type);
632         if (result == false) {
633                 _DEBUG_ERROR("failed in get_Config");
634                 err = SE_INTERNAL_DA_ERROR;
635                 goto error;
636         }
637
638         *alert_type = ___convert_sync_type_value(sync_type);
639
640  error:
641
642         if (sync_type != NULL)
643                 free(sync_type);
644
645         _INNER_FUNC_EXIT;
646         return err;
647 }
648
649 static se_error_type_e __on_synchronising_account(int account_id)
650 {
651         _INNER_FUNC_ENTER;
652
653         se_error_type_e err = SE_INTERNAL_OK;
654
655         bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "1", "string", "SE");
656         if (result == false) {
657                 _DEBUG_ERROR("failed in set_Config");
658                 err = SE_INTERNAL_DA_ERROR;
659                 goto error;
660         }
661
662  error:
663
664         _INNER_FUNC_EXIT;
665         return err;
666 }
667
668 static se_error_type_e _off_synchronising_account(int account_id)
669 {
670         _INNER_FUNC_ENTER;
671
672         se_error_type_e err = SE_INTERNAL_OK;
673
674         bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "0", "string", "SE");
675         if (result == false) {
676                 _DEBUG_ERROR("failed in set_Config");
677                 err = SE_INTERNAL_DA_ERROR;
678                 goto error;
679         }
680
681  error:
682
683         _INNER_FUNC_EXIT;
684         return err;
685 }
686
687 static se_error_type_e __on_resume_flag(int account_id)
688 {
689         _INNER_FUNC_ENTER;
690
691         se_error_type_e err = SE_INTERNAL_OK;
692
693         bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, "1", "string", "SE");
694         if (result == false) {
695                 _DEBUG_ERROR("failed in set_Config");
696                 err = SE_INTERNAL_DA_ERROR;
697                 goto error;
698         }
699
700  error:
701
702         _INNER_FUNC_EXIT;
703         return err;
704 }
705
706 static se_error_type_e _off_resume_flag(int account_id)
707 {
708         _INNER_FUNC_ENTER;
709
710         se_error_type_e err = SE_INTERNAL_OK;
711
712         bool result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_RESUME, "0", "string", "SE");
713         if (result == false) {
714                 _DEBUG_ERROR("failed in set_Config");
715                 err = SE_INTERNAL_DA_ERROR;
716                 goto error;
717         }
718
719  error:
720
721         _INNER_FUNC_EXIT;
722         return err;
723 }
724
725 static se_error_type_e _assemble_changed_datastores(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj)
726 {
727         _INNER_FUNC_ENTER;
728
729         se_error_type_e err = SE_INTERNAL_OK;
730         changed_datastore_s *pChangedDatastore = NULL;
731         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
732
733         int content_type;
734         for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
735                 if (datastoreinfo_per_content_type[content_type] != NULL) {
736                         if (!datastoreinfo_per_content_type[content_type]->client_sync_type)
737                                 continue;
738
739                         if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_SLOW_SYNC
740                             || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_CLIENT || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_CLIENT_BY_SERVER) {
741
742                                 if (content_type == TYPE_CALLLOG) {
743                                         _DEBUG_TRACE("sync_agent_refresh_item_tbl_from_service for call log");
744                                         sync_agent_refresh_item_tbl_from_service(account_id, datastoreinfo_per_content_type[content_type]->datastore_id);
745                                 }
746
747                                 GList *item_list = NULL;
748                                 sync_agent_da_get_item_list_query_s query;
749                                 query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_ACCOUNT_ID;
750                                 query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;
751
752                                 da_err = sync_agent_get_item_list(account_id, &query, &item_list);
753                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
754                                         _DEBUG_ERROR("failed in sync_agent_get_item_list !!");
755                                 }
756
757                                 pChangedDatastore = create_changed_datastore(datastoreinfo_per_content_type[content_type]->source, datastoreinfo_per_content_type[content_type]->target, 1, g_list_length(item_list));
758
759                                 if (pChangedDatastore == NULL) {
760                                         sync_agent_free_item_list(item_list);
761                                         _DEBUG_ERROR("failed in create_ChangedDatastore");
762                                         err = SE_INTERNAL_NO_MEMORY;
763                                         goto error;
764                                 }
765
766                                 if (g_list_length(item_list) > 0) {
767                                         GList *iter = NULL;
768                                         sync_agent_da_item_s *iter_data;
769                                         for (iter = item_list; iter != NULL; iter = g_list_next(iter)) {
770                                                 iter_data = NULL;
771                                                 iter_data = (sync_agent_da_item_s *) (iter->data);
772                                                 if (iter_data != NULL) {
773                                                         char *cttype = __convert_cttype_str(iter_data->data_store_id);
774                                                         changed_item_s *pChanged = create_changed_item(CHANGE_ADD, iter_data->item_id);
775                                                         if (pChanged == NULL) {
776                                                                 sync_agent_free_item_list(item_list);
777                                                                 _DEBUG_ERROR("failed in create_ChangedDatastore");
778                                                                 err = SE_INTERNAL_NO_MEMORY;
779                                                                 goto error;
780                                                         }
781
782                                                         set_changed_item_content_type(pChanged, cttype);
783                                                         set_changed_item_index_of_datastore(pChanged, content_type);
784
785                                                         add_changed_datastore_changed_item(pChangedDatastore, pChanged);
786                                                 } else {
787                                                         _DEBUG_ERROR("iter_data is NULL !!");
788                                                         sync_agent_free_item_list(item_list);
789                                                         err = SE_INTERNAL_NO_MEMORY;
790                                                         goto error;
791                                                 }
792                                         }
793
794                                         sync_agent_free_item_list(item_list);
795                                 }
796
797                                 GList *item_id_list = NULL;
798
799                                 sync_agent_da_get_item_id_list_query_s item_id_query = {0,};
800                                 item_id_query.option = SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_OPERATION_ID_N_ITEM_TYPE_ID;
801                                 item_id_query.account_id = account_id;
802                                 item_id_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;
803                                 item_id_query.operation_id = SYNC_AGENT_DA_CHANGE_OPERATION_DELETE;
804
805                                 da_err = sync_agent_get_item_id_list(&item_id_query, &item_id_list);
806                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
807                                         _DEBUG_ERROR("failed in sync_agent_get_item_id_list !!");
808                                         err = SE_INTERNAL_DA_ERROR;
809                                         goto error;
810                                 }
811
812                                 GList *item_id_iter = NULL;
813                                 char *item_id;
814
815                                 if (g_list_length(item_id_list) > 0) {
816                                         for (item_id_iter = item_id_list; item_id_iter != NULL; item_id_iter = g_list_next(item_id_iter)) {
817                                                 item_id = NULL;
818                                                 item_id = (char *)(item_id_iter->data);
819
820                                                 da_err = sync_agent_delete_item(item_id, 1);
821                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
822                                                         _DEBUG_ERROR("failed in sync_agent_delete_item !!");
823                                                         sync_agent_free_item_id_list(item_id_list);
824                                                         err = SE_INTERNAL_DA_ERROR;
825                                                         goto error;
826                                                 }
827                                         }
828                                 }
829
830                                 sync_agent_free_item_id_list(item_id_list);
831
832                                 /*delete changelog data by account id and datastore id */
833                                 sync_agent_da_delete_changelog_query_s delete_ch_query = {0,};
834                                 delete_ch_query.option = SYNC_AGENT_DA_DELETE_CHANGELOG_OPTION_ITEM_TYPE_ID;
835                                 delete_ch_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;
836
837                                 da_err = sync_agent_delete_changelog(account_id, &delete_ch_query);
838                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
839                                         _DEBUG_ERROR("failed in sync_agent_delete_changelog !!");
840                                         err = SE_INTERNAL_DA_ERROR;
841                                         goto error;
842                                 }
843                         } else if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_TWO_WAY
844                                    || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_TWO_WAY_BY_SERVER
845                                    || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_CLIENT || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
846                                 _DEBUG_TRACE("server sync type is ALERT_TYPE [%d]", datastoreinfo_per_content_type[content_type]->server_sync_type);
847
848                                 /*on resume flag into agent DB */
849                                 err = __on_resume_flag(account_id);
850                                 if (err != SE_INTERNAL_OK) {
851                                         _DEBUG_ERROR("failed in __on_resume_flag");
852                                         goto error;
853                                 }
854
855                                 if (content_type == TYPE_CALLLOG) {
856                                         _DEBUG_TRACE("sync_agent_construct_item_tbl_from_service for call log");
857                                         sync_agent_construct_item_tbl_from_service(account_id, content_type);
858                                 }
859
860                                 /*from changelog */
861                                 GList *item_info_list = NULL;
862                                 sync_agent_da_get_changelog_list_query_s get_ch_query;
863                                 get_ch_query.option = SYNC_AGENT_DA_GET_CHANGELOG_LIST_OPTION_ITEM_TYPE_ID;
864                                 get_ch_query.account_id = account_id;
865                                 get_ch_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;
866
867                                 da_err = sync_agent_get_changelog_list(&get_ch_query, &item_info_list);
868                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
869                                         _DEBUG_ERROR("failed in sync_agent_get_changelog_list !!");
870                                         err = SE_INTERNAL_NO_MEMORY;
871                                         goto error;
872                                 }
873
874                                 pChangedDatastore = create_changed_datastore(datastoreinfo_per_content_type[content_type]->source, datastoreinfo_per_content_type[content_type]->target, 1, g_list_length(item_info_list));
875
876                                 if (pChangedDatastore == NULL) {
877                                         sync_agent_free_changelog_list(item_info_list);
878                                         _DEBUG_ERROR("failed in create_ChangedDatastore");
879                                         err = SE_INTERNAL_NO_MEMORY;
880                                         goto error;
881                                 }
882
883                                 /*gathering changelog info */
884                                 if (g_list_length(item_info_list) > 0) {
885                                         char *cttype = __convert_cttype_str(datastoreinfo_per_content_type[content_type]->datastore_id);
886
887                                         GList *item_info_iter = NULL;
888                                         sync_agent_da_item_changelog_s *iter_data;
889
890                                         for (item_info_iter = item_info_list; item_info_iter != NULL; item_info_iter = g_list_next(item_info_iter)) {
891                                                 iter_data = NULL;
892                                                 iter_data = (sync_agent_da_item_changelog_s *) (item_info_iter->data);
893
894                                                 if (iter_data != NULL) {
895                                                         changed_item_s *pChanged = create_changed_item(iter_data->operation_id - 300, iter_data->item_id);
896                                                         if (pChanged == NULL) {
897                                                                 sync_agent_free_changelog_list(item_info_list);
898                                                                 _DEBUG_ERROR("failed in create_ChangedDatastore");
899                                                                 err = SE_INTERNAL_NO_MEMORY;
900                                                                 goto error;
901                                                         }
902
903                                                         set_changed_item_content_type(pChanged, cttype);
904                                                         set_changed_item_index_of_datastore(pChanged, content_type);
905                                                         add_changed_datastore_changed_item(pChangedDatastore, pChanged);
906                                                 } else {
907                                                         sync_agent_free_changelog_list(item_info_list);
908                                                         _DEBUG_ERROR("iter_data is NULL !!");
909                                                         err = SE_INTERNAL_NO_MEMORY;
910                                                         goto error;
911                                                 }
912                                         }
913
914                                         int *itemTypeIdList = (int *)calloc(1, sizeof(int));
915                                         if (itemTypeIdList == NULL) {
916                                                 _DEBUG_ERROR("failed in da_set_item_changelog_wait_status_internal");
917                                                 err = SE_INTERNAL_NO_MEMORY;
918                                                 goto error;
919                                         }
920
921                                         itemTypeIdList[0] = datastoreinfo_per_content_type[content_type]->datastore_id;
922
923                                         sync_agent_da_update_changelog_query_s query;
924                                         query.option = SYNC_AGENT_DA_UPDATE_CHANGELOG_OPTION_WAIT_STATUS;
925                                         query.folder_id_list = NULL;
926                                         query.folder_id_count = 0;
927                                         query.item_type_id_list = itemTypeIdList;
928                                         query.item_type_id_count = 1;
929
930                                         da_err = sync_agent_update_changelog(account_id, &query);
931
932                                         sync_agent_free_changelog_list(item_info_list);
933
934                                         if (itemTypeIdList != NULL)
935                                                 free(itemTypeIdList);
936                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
937                                                 _DEBUG_ERROR("failed in sync_agent_update_changelog !!");
938                                                 err = SE_INTERNAL_DA_ERROR;
939                                                 goto error;
940                                         }
941                                 }
942                         } else if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_SERVER
943                                    || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_SERVER_BY_SERVER
944                                    || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
945
946                                 if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_ONE_WAY_FROM_SERVER) {
947                                         /*on resume flag into agent DB */
948                                         err = __on_resume_flag(account_id);
949                                         if (err != SE_INTERNAL_OK) {
950                                                 _DEBUG_ERROR("failed in __on_resume_flag");
951                                                 goto error;
952                                         }
953                                 }
954
955                                 /*MUST create empty changedDatastore.. */
956                                 pChangedDatastore = create_changed_datastore(datastoreinfo_per_content_type[content_type]->source, datastoreinfo_per_content_type[content_type]->target, 1, 0);
957                                 if (pChangedDatastore == NULL) {
958                                         _DEBUG_ERROR("pChangedDatastore is NULL");
959                                         err = SE_INTERNAL_NO_MEMORY;
960                                         goto error;
961                                 }
962
963                                 if (datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER || datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
964
965                                         /*_DEBUG_TRACE("sync_agent_refresh_item_tbl_from_service");
966                                         sync_agent_refresh_item_tbl_from_service(account_id, datastoreinfo_per_content_type[content_type]->plugin_type);*/
967
968                                         /*  Delete All item (include changelog), before adapting server item data… */
969                                         _DEBUG_TRACE("sync_agent_begin_service = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
970                                         sync_agent_begin_service(datastoreinfo_per_content_type[content_type]->datastore_id);
971                                         sync_agent_begin_transaction();
972
973                                         /*delete service item data */
974                                         sync_agent_da_delete_service_item_query_s query;
975                                         query.content_type = datastoreinfo_per_content_type[content_type]->datastore_id;
976                                         query.account_id = account_id;
977
978                                         da_err = sync_agent_query_delete_service_items(&query);
979                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
980
981                                                 sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
982                                                 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);
983
984                                                 _DEBUG_ERROR("failed in sync_agent_query_delete_service_items()");
985                                                 err = SE_INTERNAL_DA_ERROR;
986                                                 goto error;
987                                         }
988
989                                         _DEBUG_TRACE("da_get_item_by_account_id_internal");
990                                         /*get all item by account id */
991                                         GList *item_list = NULL;
992                                         sync_agent_da_get_item_list_query_s get_item_query;
993                                         get_item_query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_ACCOUNT_ID;
994                                         get_item_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;
995
996                                         da_err = sync_agent_get_item_list(account_id, &get_item_query, &item_list);
997                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
998                                                 _DEBUG_ERROR("failed in sync_agent_get_item_list !!");
999                                         }
1000                                         _DEBUG_TRACE("item_cnt = %d", g_list_length(item_list));
1001
1002                                         GList *item_iter = NULL;
1003                                         sync_agent_da_item_s *item_iter_data;
1004                                         for (item_iter = item_list; item_iter != NULL; item_iter = g_list_next(item_iter)) {
1005                                                 item_iter_data = NULL;
1006                                                 item_iter_data = (sync_agent_da_item_s *) (item_iter->data);
1007
1008                                                 GList *mapping_list = NULL;
1009                                                 sync_agent_da_get_item_list_query_s mapping_query;
1010                                                 mapping_query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING;
1011                                                 mapping_query.item_id = item_iter_data->item_id;
1012
1013                                                 da_err = sync_agent_get_item_list(account_id, &mapping_query, &mapping_list);
1014                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1015                                                         _DEBUG_ERROR("failed in sync_agent_get_item_list !!");
1016                                                 }
1017
1018                                                 _DEBUG_TRACE("acc_cnt = %d", g_list_length(mapping_list));
1019
1020                                                 GList *mapping_iter = NULL;
1021                                                 sync_agent_da_item_s *mapping_iter_data;
1022
1023                                                 for (mapping_iter = mapping_list; mapping_iter != NULL; mapping_iter = g_list_next(mapping_iter)) {
1024                                                         mapping_iter_data = NULL;
1025                                                         mapping_iter_data = (sync_agent_da_item_s *) (mapping_iter->data);
1026                                                         da_err = sync_agent_delete_item(mapping_iter_data->item_id, 1);
1027                                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
1028                                                                 _DEBUG_ERROR("failed in sync_agent_delete_item !!");
1029                                                         }
1030                                                 }
1031
1032                                                 sync_agent_free_item_list(mapping_list);
1033                                         }
1034
1035                                         /*delete changelog data by account id and datastore id */
1036                                         _DEBUG_TRACE("da_delete_item_changelog_by_item_type_id_internal = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
1037
1038                                         sync_agent_da_delete_changelog_query_s delete_ch_query;
1039                                         delete_ch_query.option = SYNC_AGENT_DA_DELETE_CHANGELOG_OPTION_ITEM_TYPE_ID;
1040                                         delete_ch_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;
1041
1042                                         da_err = sync_agent_delete_changelog(account_id, &delete_ch_query);
1043                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
1044
1045                                                 sync_agent_free_item_list(item_list);
1046
1047                                                 sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
1048                                                 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);
1049
1050                                                 _DEBUG_ERROR("failed in sync_agent_delete_changelog !!");
1051                                                 err = SE_INTERNAL_DA_ERROR;
1052                                                 goto error;
1053                                         }
1054
1055                                         /*delete item from item_tbl */
1056                                         _DEBUG_TRACE("da_delete_item_by_item_type_id_internal = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
1057                                         sync_agent_da_delete_item_query_s delete_item_query;
1058                                         delete_item_query.option = SYNC_AGENT_DA_DELETE_ITEM_OPTION_ITEM_TYPE_ID;
1059                                         delete_item_query.account_id = account_id;
1060                                         delete_item_query.item_type_id = datastoreinfo_per_content_type[content_type]->datastore_id;
1061
1062                                         da_err = sync_agent_query_delete_item(&delete_item_query);
1063                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
1064                                                 sync_agent_free_item_list(item_list);
1065
1066                                                 sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
1067                                                 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);
1068
1069                                                 _DEBUG_ERROR("failed in sync_agent_query_delete_item !!");
1070                                                 err = SE_INTERNAL_DA_ERROR;
1071                                                 goto error;
1072                                         }
1073
1074                                         _DEBUG_TRACE("sync_agent_end_service = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
1075                                         sync_agent_end_service(datastoreinfo_per_content_type[content_type]->datastore_id, 1);
1076                                         sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
1077                                         sync_agent_free_item_list(item_list);
1078                                 }
1079                         }
1080                         if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
1081                                 (*sync_obj)->changed_datastore = g_list_append((*sync_obj)->changed_datastore, pChangedDatastore);
1082
1083                                 /* for prevent */
1084                                 if (pChangedDatastore != NULL) {
1085                                         set_number_of_change(datastoreinfo_per_content_type[content_type]->client_sync_result, pChangedDatastore->number_of_changes);
1086                                 } else {
1087                                         _DEBUG_ERROR("pChangedDatastore is NULL !");
1088                                 }
1089
1090                                 operation_type_e operation_type;
1091                                 if (datastoreinfo_per_content_type[content_type]->client_sync_result->number_of_change == 0)
1092                                         operation_type = OPERATION_NOOP;
1093                                 else
1094                                         operation_type = OPERATION_ADD;
1095                                 err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, content_type, false, false, datastoreinfo_per_content_type[content_type]->client_sync_result);
1096                                 if (err != SE_INTERNAL_OK) {
1097                                         _DEBUG_ERROR("Failed in process_update");
1098                                         goto error;
1099                                 }
1100                         }
1101                 }
1102         }
1103
1104         _INNER_FUNC_EXIT;
1105         return err;
1106
1107  error:
1108
1109         if (pChangedDatastore != NULL) {
1110                 free_changed_datastore(pChangedDatastore);
1111                 pChangedDatastore = NULL;
1112         }
1113
1114         _INNER_FUNC_EXIT;
1115         return err;
1116 }
1117
1118 static se_error_type_e _prepare_pre_sync(int account_id, char *sync_mode, san_package_s * san_package, alert_type_e * sync_type)
1119 {
1120         _INNER_FUNC_ENTER;
1121
1122         se_error_type_e err = SE_INTERNAL_OK;
1123
1124         /*set synchronising flag into agent DB */
1125         err = __on_synchronising_account(account_id);
1126         if (err != SE_INTERNAL_OK) {
1127                 _DEBUG_ERROR("failed in __on_synchronising_account");
1128                 goto error;
1129         }
1130
1131         /*get sync type */
1132         err = __get_sync_type(account_id, sync_type);
1133         if (err != SE_INTERNAL_OK) {
1134                 _DEBUG_ERROR("failed in __get_sync_type");
1135                 goto error;
1136         }
1137
1138         /*init datastore_info_array */
1139         err = __init_datastore_info_array(account_id);
1140         if (err != SE_INTERNAL_OK) {
1141                 _DEBUG_ERROR("failed in __init_datastore_info_array");
1142                 goto error;
1143         }
1144
1145         /*set config based on sync mode */
1146         err = __set_config_based_on_sync_mode(account_id, sync_mode, san_package);
1147         if (err != SE_INTERNAL_OK) {
1148                 _DEBUG_ERROR("failed in __set_config_based_on_sync_mode");
1149                 goto error;
1150         }
1151
1152  error:
1153
1154         _INNER_FUNC_EXIT;
1155         return err;
1156 }
1157
1158 static se_error_type_e __set_config_based_on_sync_mode(int account_id, char *sync_mode, san_package_s * san_package)
1159 {
1160         _INNER_FUNC_ENTER;
1161
1162         se_error_type_e err = SE_INTERNAL_OK;
1163         int content_type;
1164
1165         if (strcmp(sync_mode, DEFINE_SYNC_MODE_PUSH) == 0) {
1166                 if (san_package != NULL) {
1167                         int count = san_package->cnt_sync_alerts;
1168                         _DEBUG_TRACE("count = %d", count);
1169                         int i;
1170                         for (i = 0; i < count; i++) {
1171                                 for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
1172
1173                                         if (datastoreinfo_per_content_type[content_type] != NULL) {
1174                                                 _DEBUG_TRACE("datastoreinfo_per_content_type[content_type]->target = %s", datastoreinfo_per_content_type[content_type]->target);
1175                                                 if (datastoreinfo_per_content_type[content_type]->target == NULL)
1176                                                         continue;
1177
1178                                                 if (strcmp(san_package->sync_alerts[i].server_uri, datastoreinfo_per_content_type[content_type]->target) == 0) {
1179                                                         datastoreinfo_per_content_type[content_type]->client_sync_type = san_package->sync_alerts[i].sync_type;
1180                                                         _DEBUG_TRACE("san_package->syncAlerts[i].sync_type = %d", san_package->sync_alerts[i].sync_type);
1181                                                 } else
1182                                                         datastoreinfo_per_content_type[content_type]->client_sync_type = ALERT_UNKNOWN;
1183                                         }
1184                                 }
1185                         }
1186                 }
1187         }
1188
1189         _INNER_FUNC_EXIT;
1190         return err;
1191 }
1192
1193 static se_error_type_e _execute_pre_sync(int account_id, int session_time, pre_sync_return_obj_s * pre_sync_return_obj, alert_type_e * server_sync_type)
1194 {
1195         _INNER_FUNC_ENTER;
1196
1197         se_error_type_e err = SE_INTERNAL_OK;
1198
1199         /*if there is no alert command from server check it and goto fail_part */
1200         if (g_list_length(pre_sync_return_obj->datastore_info) == 0) {
1201                 _DEBUG_TRACE("alert command list from server is empty");
1202                 err = SE_INTERNAL_MISCONFIGURATION;
1203                 goto error;
1204         }
1205
1206         err = __execute_pre_sync_set_server_id(account_id, pre_sync_return_obj->dev_id);
1207         if (err != SE_INTERNAL_OK) {
1208                 _DEBUG_ERROR("failed in __set_server_id");
1209                 goto error;
1210         }
1211
1212         err = __execute_pre_sync_datastore(account_id, session_time, pre_sync_return_obj->datastore_info, server_sync_type);
1213         if (err != SE_INTERNAL_OK) {
1214                 _DEBUG_ERROR("failed in __process_datastore");
1215                 goto error;
1216         }
1217
1218  error:
1219
1220         _INNER_FUNC_EXIT;
1221         return err;
1222
1223 }
1224
1225 static se_error_type_e __execute_pre_sync_set_server_id(int account_id, char *dev_id)
1226 {
1227         _INNER_FUNC_ENTER;
1228
1229         se_error_type_e err = SE_INTERNAL_OK;
1230         bool result;
1231         char *value = NULL;
1232
1233         if (dev_id != NULL) {
1234                 result = get_config(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &value);
1235                 if (result == true) {
1236                         if (value != NULL) {
1237                                 if (strcmp(value, dev_id) == 0) {
1238                                         /* OK */
1239                                 } else {
1240                                         result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, dev_id, "string", "SE");
1241                                         if (result == false) {
1242                                                 _DEBUG_ERROR("failed in set_Config");
1243                                                 err = SE_INTERNAL_DA_ERROR;
1244                                                 goto error;
1245                                         }
1246                                 }
1247                         } else {
1248                                 result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, dev_id, "string", "SE");
1249                                 if (result == false) {
1250                                         _DEBUG_ERROR("failed in set_Config");
1251                                         err = SE_INTERNAL_DA_ERROR;
1252                                         goto error;
1253                                 }
1254                         }
1255                 } else {
1256                         _DEBUG_ERROR("failed in get_Config");
1257                         err = SE_INTERNAL_DA_ERROR;
1258                         goto error;
1259                 }
1260         }
1261  error:
1262
1263         if (value != NULL) {
1264                 free(value);
1265                 value = NULL;
1266         }
1267
1268         _INNER_FUNC_EXIT;
1269         return err;
1270 }
1271
1272 static se_error_type_e __execute_pre_sync_datastore(int account_id, int session_time, GList * datastore_info, alert_type_e * server_sync_type)
1273 {
1274         _INNER_FUNC_ENTER;
1275
1276         se_error_type_e err = SE_INTERNAL_OK;
1277         int result;
1278
1279         bool changeSyncType = false;
1280         alert_type_e sync_type = ALERT_UNKNOWN;
1281
1282         int content_type;
1283         GList *serverDatastore_iter = NULL;
1284         datastore_info_s *serverDatastoreInfo = NULL;
1285         bool existInServerDatastore;
1286         for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
1287                 existInServerDatastore = false;
1288
1289                 if (datastoreinfo_per_content_type[content_type] != NULL) {
1290                         if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
1291                                 _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->client_sync_type = %d", content_type, datastoreinfo_per_content_type[content_type]->client_sync_type);
1292
1293                                 for (serverDatastore_iter = datastore_info; serverDatastore_iter != NULL; serverDatastore_iter = g_list_next(serverDatastore_iter)) {
1294                                         serverDatastoreInfo = serverDatastore_iter->data;
1295
1296                                         if (strcmp(serverDatastoreInfo->source, datastoreinfo_per_content_type[content_type]->target) == 0) {
1297                                                 existInServerDatastore = true;
1298
1299                                                 if (serverDatastoreInfo->next_anchor != NULL)
1300                                                         datastoreinfo_per_content_type[content_type]->next_anchor_server = strdup(serverDatastoreInfo->next_anchor);
1301
1302                                                 datastoreinfo_per_content_type[content_type]->server_sync_type = serverDatastoreInfo->sync_type;
1303
1304                                                 if (datastoreinfo_per_content_type[content_type]->client_sync_type != datastoreinfo_per_content_type[content_type]->server_sync_type) {
1305                                                         changeSyncType = true;
1306                                                         sync_type = datastoreinfo_per_content_type[content_type]->server_sync_type;
1307                                                 }
1308
1309                                                 _DEBUG_VERBOSE("datastoreinfo_per_content_type[%d]->server_sync_type = %d", content_type, datastoreinfo_per_content_type[content_type]->server_sync_type);
1310
1311                                                 if (datastoreinfo_per_content_type[content_type]->last_anchor_server) {
1312                                                         /*if (strcmp(datastoreinfo_per_content_type[content_type]->lastAnchorServer, serverDatastoreInfo->lastAnchor) != 0) {
1313                                                            free(datastoreinfo_per_content_type[content_type]->lastAnchorServer);
1314                                                            datastoreinfo_per_content_type[content_type]->lastAnchorServer = NULL;
1315                                                            datastoreinfo_per_content_type[content_type]->server_sync_type == ALERT_SLOW_SYNC;
1316                                                            } */
1317                                                 } else {
1318                                                         /* When first synchronize, lastAnchorServer is NULL...
1319                                                            if (datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_SLOW_SYNC
1320                                                            && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_CLIENT
1321                                                            && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_CLIENT_BY_SERVER
1322                                                            && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_SERVER
1323                                                            && datastoreinfo_per_content_type[content_type]->server_sync_type != ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
1324                                                            _DEBUG_VERBOSE("sync_agent_refresh_item_tbl_from_service");
1325                                                            sync_agent_refresh_item_tbl_from_service(account_id, datastoreinfo_per_content_type[content_type]->plugin_type);
1326                                                            } */
1327                                                 }
1328                                                 break;
1329                                         }
1330                                 }
1331
1332                                 if (!existInServerDatastore) {
1333                                         /* datastore config is wrong this datastore is not going to contain sync process from now */
1334                                         err = __process_update(account_id, ALERT_UNKNOWN, SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, false, true, datastoreinfo_per_content_type[content_type]->client_sync_result);
1335                                         if (err != SE_INTERNAL_OK) {
1336                                                 _DEBUG_ERROR("failed to process_update");
1337                                                 goto error;
1338                                         }
1339
1340                                         err = __process_update(account_id, ALERT_UNKNOWN, SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, true, true, datastoreinfo_per_content_type[content_type]->server_sync_result);
1341                                         if (err != SE_INTERNAL_OK) {
1342                                                 _DEBUG_ERROR("failed to process_update");
1343                                                 goto error;
1344                                         }
1345
1346                                         /* set fail into result (both client and server) */
1347                                         if (datastoreinfo_per_content_type[content_type]->client_sync_result != NULL)
1348                                                 datastoreinfo_per_content_type[content_type]->client_sync_result->session_result = SYNC_SESSION_FAILED;
1349                                         if (datastoreinfo_per_content_type[content_type]->server_sync_result != NULL)
1350                                                 datastoreinfo_per_content_type[content_type]->server_sync_result->session_result = SYNC_SESSION_FAILED;
1351
1352                                         err = ___write_sync_resource_info(account_id, content_type, session_time, 0, datastoreinfo_per_content_type[content_type]->client_sync_result, datastoreinfo_per_content_type[content_type]->server_sync_result);
1353                                         if (err != SE_INTERNAL_OK) {
1354                                                 _DEBUG_ERROR("failed to write_SyncResourceInfo");
1355                                                 goto error;
1356                                         }
1357
1358                                         free_datastore(datastoreinfo_per_content_type[content_type]);
1359                                         datastoreinfo_per_content_type[content_type] = NULL;
1360
1361                                         datastore_s *datastore = NULL;
1362                                         err = __init_datastore_info(account_id, content_type, &datastore);
1363                                         if (err != SE_INTERNAL_OK) {
1364                                                 _DEBUG_ERROR("failed to init_datastore_info");
1365                                                 goto error;
1366                                         }
1367
1368                                         datastoreinfo_per_content_type[content_type] = datastore;
1369                                 }
1370                         }
1371                 }
1372         }
1373
1374         if (changeSyncType) {
1375                 *server_sync_type = sync_type;
1376                 result = set_config_str(account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_SYNC_TYPE, __convert_sync_type_str(sync_type), "string", "SE");
1377                 if (result == false) {
1378                         _DEBUG_ERROR("failed in set_config");
1379                         err = SE_INTERNAL_DA_ERROR;
1380                         goto error;
1381                 }
1382         }
1383
1384  error:
1385
1386         _INNER_FUNC_EXIT;
1387         return err;
1388 }
1389
1390 static se_error_type_e _execute_sync(int account_id, alert_type_e client_sync_type, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj)
1391 {
1392         _INNER_FUNC_ENTER;
1393
1394         se_error_type_e err = SE_INTERNAL_OK;
1395
1396         if (client_sync_type == ALERT_TWO_WAY || client_sync_type == ALERT_TWO_WAY_BY_SERVER || client_sync_type == ALERT_ONE_WAY_FROM_CLIENT || client_sync_type == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
1397
1398                 err = __execute_sync_arrange_changelog(account_id, client_sync_type, (*sync_return_obj)->status);
1399                 if (err != SE_INTERNAL_OK) {
1400                         _DEBUG_ERROR("failed to process_update");
1401                         goto error;
1402                 }
1403         }
1404
1405         err = __execute_sync_status(account_id, server_sync_type, sync_obj, sync_return_obj);
1406         if (err != SE_INTERNAL_OK) {
1407                 _DEBUG_ERROR("failed in __execute_sync_status");
1408                 goto error;
1409         }
1410
1411         err = __execute_sync_change(account_id, server_sync_type, sync_obj, sync_return_obj);
1412         if (err != SE_INTERNAL_OK) {
1413                 _DEBUG_ERROR("failed in __execute_sync_change");
1414                 goto error;
1415         }
1416
1417  error:
1418
1419         _INNER_FUNC_EXIT;
1420         return err;
1421 }
1422
1423 static se_error_type_e __execute_sync_arrange_changelog(int account_id, alert_type_e sync_type, GList * status)
1424 {
1425         _INNER_FUNC_ENTER;
1426
1427         se_error_type_e err = SE_INTERNAL_OK;
1428         sync_agent_da_return_e da_err;
1429
1430         int i = 0;
1431         int count = 0;
1432         int list_length = g_list_length(status);
1433         char **itemIdList = (char **)calloc(list_length, sizeof(char *));
1434         if (itemIdList == NULL) {
1435                 _DEBUG_ERROR("Failed to alloc memory");
1436                 err = SE_INTERNAL_NO_MEMORY;
1437                 goto error;
1438         }
1439
1440         GList *appliedStatus_iter = NULL;
1441         applied_status_s *pAppliedStatus = NULL;
1442         for (appliedStatus_iter = status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) {
1443                 pAppliedStatus = appliedStatus_iter->data;
1444
1445                 switch (pAppliedStatus->status) {
1446                 case 200:
1447                 case 201:
1448                 case 202:
1449                 case 203:
1450                 case 204:
1451                 case 205:
1452                 case 207:
1453                 case 208:
1454                 case 209:
1455                 case 210:
1456                 case 211:
1457                         {
1458                                 /*clean up change log */
1459                                 if (pAppliedStatus->luid != NULL)
1460                                         itemIdList[count++] = strdup(pAppliedStatus->luid);
1461
1462                                 if (sync_type == ALERT_TWO_WAY || sync_type == ALERT_TWO_WAY_BY_SERVER || sync_type == ALERT_ONE_WAY_FROM_CLIENT || sync_type == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
1463
1464                                         if (pAppliedStatus->change_type == CHANGE_DELETE) {
1465                                                 da_err = sync_agent_delete_item(pAppliedStatus->luid, 0);
1466                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1467                                                         _DEBUG_ERROR("failed in da_delete_item_by_item_id_internal");
1468                                                         err = SE_INTERNAL_DA_ERROR;
1469                                                         goto error;
1470                                                 }
1471                                         }
1472                                 }
1473                                 break;
1474                         }
1475                 case 206:
1476                 case 420:
1477                         break;
1478                 case 400:
1479                 case 500:
1480                         {
1481                                 sync_agent_da_item_changelog_s *get_changelog = NULL;
1482                                 da_err = sync_agent_create_changelog(&get_changelog);
1483                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1484                                         _DEBUG_ERROR("failed in sync_agent_create_changelog !!");
1485                                         err = SE_INTERNAL_DA_ERROR;
1486                                         goto error;
1487                                 }
1488
1489                                 da_err = sync_agent_get_changelog(account_id, pAppliedStatus->luid, &get_changelog);
1490                                 if (da_err == SYNC_AGENT_DA_ERR_NO_DATA) {
1491                                         _DEBUG_VERBOSE("%s item does not exist in ChangeLog tbl", pAppliedStatus->luid);
1492                                         sync_agent_free_changelog(get_changelog);
1493                                         break;
1494                                 } else if (da_err != SYNC_AGENT_DA_SUCCESS) {
1495                                         _DEBUG_ERROR("failed in sync_agent_get_changelog !!");
1496                                         sync_agent_free_changelog(get_changelog);
1497                                         err = SE_INTERNAL_DA_ERROR;
1498                                         goto error;
1499                                 }
1500
1501                                 if (get_changelog->status != NULL) {
1502                                         if (strcmp(get_changelog->status, "SYNC_ERROR") == 0) {
1503                                                 if (pAppliedStatus->luid != NULL)
1504                                                         itemIdList[count++] = strdup(pAppliedStatus->luid);
1505                                         } else {
1506                                                 sync_agent_da_item_changelog_s *set_changelog = NULL;
1507
1508                                                 da_err = sync_agent_create_changelog(&set_changelog);
1509                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1510                                                         _DEBUG_ERROR("failed in sync_agent_create_changelog !!");
1511                                                         sync_agent_free_changelog(get_changelog);
1512                                                         err = SE_INTERNAL_DA_ERROR;
1513                                                         goto error;
1514                                                 }
1515
1516                                                 set_changelog->item_id = strdup(pAppliedStatus->luid);
1517                                                 set_changelog->status = strdup("SYNC_ERROR");
1518                                                 set_changelog->access_name = strdup("SE");
1519
1520                                                 sync_agent_da_update_changelog_query_s update_ch_query;
1521                                                 update_ch_query.option = SYNC_AGENT_DA_UPDATE_CHANGELOG_OPTION_SYNC_STATUS;
1522                                                 update_ch_query.update_item = set_changelog;
1523
1524                                                 da_err = sync_agent_update_changelog(account_id, &update_ch_query);
1525                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1526                                                         _DEBUG_ERROR("failed in sync_agent_update_changelog !!");
1527                                                         sync_agent_free_changelog(get_changelog);
1528                                                         sync_agent_free_changelog(set_changelog);
1529                                                         err = SE_INTERNAL_DA_ERROR;
1530                                                         goto error;
1531                                                 }
1532
1533                                                 sync_agent_free_changelog(set_changelog);
1534                                         }
1535                                 }
1536
1537                                 sync_agent_free_changelog(get_changelog);
1538                                 break;
1539                         }
1540                 }
1541         }
1542
1543         if (list_length >= 1) {
1544                 _DEBUG_TRACE("before da_delete_item_changelog_by_item_id_list_internal");
1545                 _DEBUG_TRACE("count = %d", count);
1546
1547                 sync_agent_da_delete_changelog_query_s query;
1548                 query.option = SYNC_AGENT_DA_DELETE_CHANGELOG_OPTION_ITEM_ID_LIST;
1549                 query.item_id_list = itemIdList;
1550                 query.count = count;
1551
1552                 da_err = sync_agent_delete_changelog(account_id, &query);
1553                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1554                         _DEBUG_ERROR("failed in sync_agent_delete_changelog !!");
1555                         err = SE_INTERNAL_DA_ERROR;
1556                         goto error;
1557                 }
1558         }
1559
1560  error:
1561
1562         /*free itemIdList */
1563         if (itemIdList != NULL) {
1564                 for (i = 0; i < list_length; i++) {
1565                         if (itemIdList[i] != NULL)
1566                                 free(itemIdList[i]);
1567                 }
1568                 free(itemIdList);
1569         }
1570
1571         _INNER_FUNC_EXIT;
1572         return err;
1573 }
1574
1575 static se_error_type_e __execute_sync_status(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj)
1576 {
1577         _INNER_FUNC_ENTER;
1578
1579         se_error_type_e err = SE_INTERNAL_OK;
1580
1581         int content_type;
1582         bool existEqualItem;
1583         bool needToSave;
1584         sync_result_s *pClientSyncResult;
1585         GList *changedDatastore_iter = NULL;
1586         changed_datastore_s *pSyncChangedDatastore = NULL;
1587         for (changedDatastore_iter = (*sync_obj)->changed_datastore; changedDatastore_iter != NULL;) {
1588                 pSyncChangedDatastore = (changed_datastore_s *) changedDatastore_iter->data;
1589                 _DEBUG_VERBOSE("pChangedDatastore = %p", pSyncChangedDatastore);
1590
1591                 int datastoreContentType = 0;
1592                 for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
1593                         if (datastoreinfo_per_content_type[content_type] != NULL) {
1594                                 if (strcmp(pSyncChangedDatastore->source, datastoreinfo_per_content_type[content_type]->source) == 0) {
1595                                         datastoreContentType = content_type;
1596                                         break;
1597                                 }
1598                         }
1599                 }
1600
1601                 GList *changedItem_iter = NULL;
1602                 changed_item_s *changedItem = NULL;
1603                 for (changedItem_iter = pSyncChangedDatastore->sent_item; changedItem_iter != NULL;) {
1604                         changedItem = changedItem_iter->data;
1605
1606                         existEqualItem = false;
1607                         GList *appliedStatus_iter = NULL;
1608                         applied_status_s *pAppliedStatus = NULL;
1609                         for (appliedStatus_iter = (*sync_return_obj)->status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) {
1610                                 pAppliedStatus = appliedStatus_iter->data;
1611
1612                                 if (strcmp(pAppliedStatus->luid, changedItem->luid) == 0) {
1613                                         add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1);
1614
1615                                         if (pAppliedStatus->status >= 200 && pAppliedStatus->status <= 210) {
1616                                                 switch (changedItem->change_type) {
1617                                                 case CHANGE_ADD:
1618                                                         add_add_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1);
1619                                                         break;
1620                                                 case CHANGE_REPLACE:
1621                                                         add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1);
1622                                                         break;
1623                                                 case CHANGE_DELETE:
1624                                                         add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->client_sync_result, 1);
1625                                                         break;
1626                                                 default:
1627                                                         break;
1628                                                 }
1629                                         } else {
1630                                                 /* if status is not success count it is failed */
1631                                         }
1632
1633                                         (*sync_return_obj)->status = g_list_remove((*sync_return_obj)->status, pAppliedStatus);
1634                                         free_applied_status(pAppliedStatus);
1635                                         pAppliedStatus = NULL;
1636
1637                                         changedItem_iter = g_list_next(changedItem_iter);
1638
1639                                         pSyncChangedDatastore->sent_item = g_list_remove(pSyncChangedDatastore->sent_item, changedItem);
1640                                         free_changed_item(changedItem);
1641                                         changedItem = NULL;
1642
1643                                         existEqualItem = true;
1644                                         break;
1645                                 }
1646                         }
1647                         if (existEqualItem == false)
1648                                 changedItem_iter = g_list_next(changedItem_iter);
1649                 }
1650
1651                 changedDatastore_iter = g_list_next(changedDatastore_iter);
1652
1653                 needToSave = false;
1654                 pClientSyncResult = datastoreinfo_per_content_type[datastoreContentType]->client_sync_result;
1655                 if (pClientSyncResult->number_of_change == pClientSyncResult->received_count) {
1656                         needToSave = true;
1657
1658                         if (datastoreinfo_per_content_type[datastoreContentType]->client_sync_result != NULL)
1659                                 datastoreinfo_per_content_type[datastoreContentType]->client_sync_result->session_result = SYNC_SESSION_SUCCEEDED;
1660
1661                         /*remove datastore from list and
1662                            free current ChangedDatastore it does not need anymore because all item in datastore has been sent and receive status */
1663                         (*sync_obj)->changed_datastore = g_list_remove((*sync_obj)->changed_datastore, pSyncChangedDatastore);
1664                         free_changed_datastore(pSyncChangedDatastore);
1665                         pSyncChangedDatastore = NULL;
1666                 }
1667
1668                 operation_type_e operation_type;
1669                 if (datastoreinfo_per_content_type[datastoreContentType]->client_sync_result->number_of_change == 0)
1670                         operation_type = OPERATION_NOOP;
1671                 else
1672                         operation_type = OPERATION_ADD;
1673                 err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, datastoreContentType, false, needToSave, datastoreinfo_per_content_type[datastoreContentType]->client_sync_result);
1674                 if (err != SE_INTERNAL_OK) {
1675                         _DEBUG_ERROR("failed in process_update");
1676                         goto error;
1677                 }
1678         }
1679
1680  error:
1681
1682         _INNER_FUNC_EXIT;
1683         return err;
1684 }
1685
1686 static se_error_type_e __execute_sync_change(int account_id, alert_type_e server_sync_type, sync_obj_s ** sync_obj, sync_return_obj_s ** sync_return_obj)
1687 {
1688         _INNER_FUNC_ENTER;
1689
1690         se_error_type_e err = SE_INTERNAL_OK;
1691         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
1692         sync_agent_da_service_item_s *service_item = NULL;
1693         sync_agent_da_item_s *fw_item = NULL;
1694         int content_type;
1695         GList *f_id_list = NULL;
1696         sync_result_s *tempServerSyncResult = NULL;
1697
1698         /* process command that from server sended to client(sync, add, replace etc...) */
1699         changed_datastore_s *pSyncReturnChangedDatastore = NULL;
1700         sending_status_s *sendingStatus = NULL;
1701         GList *iter = NULL;
1702         for (iter = (*sync_return_obj)->changed_datastore; iter != NULL;) {
1703                 pSyncReturnChangedDatastore = (changed_datastore_s *) iter->data;
1704
1705                 int datastoreContentType = 0;
1706                 for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
1707                         if (datastoreinfo_per_content_type[content_type] != NULL) {
1708                                 if (strcmp(pSyncReturnChangedDatastore->target, datastoreinfo_per_content_type[content_type]->source) == 0) {
1709                                         datastoreContentType = content_type;
1710                                         break;
1711                                 }
1712                         }
1713                 }
1714
1715                 /*noti to UI numberOfChanges from server */
1716                 if (pSyncReturnChangedDatastore->has_number_of_changes) {
1717                         set_number_of_change(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, pSyncReturnChangedDatastore->number_of_changes);
1718
1719                         operation_type_e operation_type;
1720                         if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result->number_of_change == 0)
1721                                 operation_type = OPERATION_NOOP;
1722                         else
1723                                 operation_type = OPERATION_ADD;
1724                         err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, datastoreContentType, true, false, datastoreinfo_per_content_type[datastoreContentType]->server_sync_result);
1725                         if (err != SE_INTERNAL_OK) {
1726                                 _DEBUG_ERROR("failed in process_update");
1727                                 goto error;
1728                         }
1729                 }
1730
1731                 sendingStatus = create_sending_status(pSyncReturnChangedDatastore->target, pSyncReturnChangedDatastore->source);
1732                 if (sendingStatus == NULL) {
1733                         err = SE_INTERNAL_NO_MEMORY;
1734                         _DEBUG_ERROR("failed to alloc memory");
1735                         goto error;
1736                 }
1737
1738                 /*get folderId */
1739                 char *folderId = NULL;
1740                 sync_agent_da_get_folder_id_list_query_s query;
1741                 query.option = SYNC_AGENT_DA_GET_FOLDER_ID_OPTION_FOLDER_TYPE_ID;
1742                 query.account_id = account_id;
1743                 query.item_type_id = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
1744                 query.folder_type_id = datastoreinfo_per_content_type[datastoreContentType]->folder_type_id;
1745
1746                 da_err = sync_agent_get_folder_id_list(&query, &f_id_list);
1747                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1748                         _DEBUG_ERROR("sync_agent_get_folder_id_list_inferface() failed !!");
1749                         goto error;
1750                 }
1751
1752                 if (g_list_length(f_id_list) > 0) {
1753                         GList *iter = g_list_nth(f_id_list, 0);
1754                         folderId = (char *)(iter->data);
1755                 } else {
1756                         _DEBUG_ERROR("failed to get folderId");
1757                         err = SE_INTERNAL_DA_ERROR;
1758                         goto error;
1759                 }
1760
1761                 int changeItemCount = g_list_length(pSyncReturnChangedDatastore->change_item);
1762                 if (changeItemCount > 0) {
1763                         /*begin transaction */
1764                         _DEBUG_VERBOSE("sync_agent_begin_service with datastoreContentType = %d", datastoreContentType);
1765 //                      sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);
1766                         sync_agent_begin_transaction();
1767
1768                         /*back up syncresult structure for rollback case */
1769                         tempServerSyncResult = dup_sync_result(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result);
1770                 }
1771
1772                 bool needToSave = false;
1773                 sync_result_s *pServerSyncResult = NULL;
1774                 GList *changedIter = NULL;
1775                 changed_item_s *changedItem = NULL;
1776                 applied_status_s *appliedStatus = NULL;
1777
1778                 for (changedIter = pSyncReturnChangedDatastore->change_item; changedIter != NULL; changedIter = g_list_next(changedIter)) {
1779                         changedItem = (changedIter->data);
1780
1781                         _DEBUG_VERBOSE("changedItem->changeType : %d", changedItem->change_type);
1782                         _DEBUG_VERBOSE("changedItem->luid : %s", changedItem->luid);
1783                         _DEBUG_VERBOSE("changedItem->content_type : %s", changedItem->content_type);
1784                         _DEBUG_VERBOSE("changedItem->indexOfDatastore : %d", changedItem->index_of_datastore);
1785                         _DEBUG_VERBOSE("changedItem->data : %s", changedItem->data);
1786
1787                         add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1);
1788
1789                         bool da_fail = false;
1790                         command_result_e returnResult = COMMAND_RESULT_INIT;
1791                         switch (changedItem->change_type) {
1792                         case CHANGE_ADD:
1793                                 {
1794                                         /* add to service DB */
1795                                         if (changedItem->data != NULL) {
1796                                                 char *fw_id = NULL;
1797
1798                                                 da_err = sync_agent_create_service_item(&service_item);
1799                                                 if ((da_err != SYNC_AGENT_DA_SUCCESS) || (service_item == NULL)) {
1800                                                         _DEBUG_ERROR("failed in sync_agent_create_service_item() = %d", da_err);
1801                                                         returnResult = ___convert_return_status(da_err);
1802                                                         da_fail = true;
1803                                                         break;
1804                                                 }
1805
1806                                                 service_item->item_id = g_strdup(changedItem->luid);
1807                                                 service_item->content_type = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
1808                                                 service_item->account_id = account_id;
1809                                                 service_item->folder_id = g_strdup(folderId);
1810                                                 service_item->access_name = g_strdup("Engine");
1811                                                 service_item->data = g_strdup(changedItem->data);
1812                                                 sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);
1813
1814                                                 da_err = sync_agent_add_service_item(service_item, &fw_id, false);
1815
1816                                                 sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);
1817
1818                                                 _DEBUG_VERBOSE("da_err = %d", da_err);
1819
1820                                                 if (da_err == SYNC_AGENT_DA_SUCCESS) {
1821                                                         int acc_cnt;
1822                                                         bool success = true;
1823                                                         char *service_id = sync_agent_get_service_item_id(fw_id);
1824
1825                                                         GList *folder_list = NULL;
1826                                                         sync_agent_da_get_folder_list_query_s query;
1827                                                         query.option = SYNC_AGENT_DA_GET_FOLDER_LIST_OPTION_SERVICE_ID_MAPPING;
1828                                                         query.account_id = account_id;
1829                                                         query.folder_id = folderId;
1830                                                         da_err = sync_agent_get_folder_list(&query, &folder_list);
1831                                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
1832                                                                 _DEBUG_ERROR("sync_agent_get_folder_list() failed !!");
1833                                                                 success = false;
1834                                                         }
1835
1836                                                         acc_cnt = g_list_length(folder_list);
1837
1838                                                         _DEBUG_VERBOSE("acc_cnt = %d", acc_cnt);
1839                                                         if (acc_cnt > 0) {
1840                                                                 char **fw_item_id_list = sync_agent_generate_item_luid(1, acc_cnt);
1841                                                                 char **id_list = (char **)calloc(acc_cnt, sizeof(char *));
1842                                                                 if (id_list == NULL) {
1843                                                                         _DEBUG_ERROR("CALLOC failed !!!");
1844                                                                         success = false;
1845                                                                         break;
1846                                                                 }
1847
1848                                                                 if (fw_item_id_list == NULL) {
1849                                                                         _DEBUG_ERROR("failed in sync_agent_generate_item_luid");
1850                                                                         da_err = SYNC_AGENT_DA_ERRORS;
1851                                                                         success = false;
1852                                                                 } else {
1853                                                                         int i = 0;
1854                                                                         GList *iter = NULL;
1855                                                                         sync_agent_da_folder_s *iter_data;
1856
1857                                                                         for (iter = folder_list; iter != NULL; iter = g_list_next(iter)) {
1858                                                                                 iter_data = NULL;
1859                                                                                 iter_data = (sync_agent_da_folder_s *) (iter->data);
1860
1861                                                                                 da_err = sync_agent_create_item(&fw_item);
1862                                                                                 if ((da_err != SYNC_AGENT_DA_SUCCESS) || (fw_item == NULL)) {
1863                                                                                         _DEBUG_ERROR("failed in sync_agent_create_item() = %d", da_err);
1864                                                                                         success = false;
1865                                                                                         break;
1866                                                                                 }
1867
1868                                                                                 fw_item->item_id = g_strdup(fw_item_id_list[i]);
1869                                                                                 fw_item->data_store_id = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
1870                                                                                 fw_item->account_id = iter_data->account_id;
1871                                                                                 fw_item->folder_id = g_strdup(iter_data->folder_id);
1872                                                                                 fw_item->service_id = g_strdup(service_id);
1873                                                                                 fw_item->access_name = g_strdup("DACI_ChangeLog");
1874
1875                                                                                 da_err = sync_agent_add_item(fw_item, &(id_list[i]), true);
1876                                                                                 if (fw_item != NULL)
1877                                                                                         sync_agent_free_item(fw_item);
1878                                                                                 free(fw_item_id_list[i]);
1879                                                                                 free(id_list[i]);
1880
1881                                                                                 i++;
1882
1883                                                                                 _DEBUG_VERBOSE("da_err = %d", da_err);
1884
1885                                                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1886                                                                                         success = false;
1887                                                                                         break;
1888                                                                                 }
1889                                                                         }
1890                                                                         if (fw_item_id_list != NULL) {
1891                                                                                 free(fw_item_id_list);
1892                                                                         }
1893                                                                         if (id_list != NULL) {
1894                                                                                 free(id_list);
1895                                                                         }
1896                                                                 }
1897                                                         }
1898
1899                                                         if (success == true) {
1900                                                                 returnResult = COMMAND_RESULT_ADDED;
1901                                                                 add_add_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1);
1902                                                         } else {
1903                                                                 /*this case just fail do rollback and goto fail */
1904                                                                 returnResult = ___convert_return_status(da_err);
1905                                                                 da_fail = true;
1906                                                         }
1907                                                         sync_agent_free_folder_list(folder_list);
1908
1909                                                         if (service_id != NULL)
1910                                                                 free(service_id);
1911
1912                                                 } else if (da_err == SYNC_AGENT_DA_ERR_NOT_SUPPORTED || da_err == SYNC_AGENT_DA_ERR_INVALID_CONTENT || da_err == SYNC_AGENT_DA_ERR_ALREADY_EXIST) {
1913                                                         /*just return error to server */
1914                                                         returnResult = ___convert_return_status(da_err);
1915                                                 } else {
1916                                                         /*this case just fail do rollback and goto fail */
1917                                                         returnResult = ___convert_return_status(da_err);
1918                                                         da_fail = true;
1919                                                 }
1920
1921                                                 if (service_item != NULL)
1922                                                         sync_agent_free_service_item(service_item);
1923
1924                                                 if (fw_id != NULL)
1925                                                         free(fw_id);
1926                                         } else
1927                                                 returnResult = COMMAND_RESULT_COMMAND_FAIL;
1928
1929                                         break;
1930                                 }
1931                         case CHANGE_REPLACE:
1932                                 {
1933                                         if (changedItem->data != NULL) {
1934                                                 /* update from service DB */
1935                                                 da_err = sync_agent_create_service_item(&service_item);
1936                                                 if (da_err != SYNC_AGENT_DA_SUCCESS || (service_item == NULL)) {
1937                                                         _DEBUG_ERROR("failed in sync_agent_create_service_item() = %d", da_err);
1938                                                         returnResult = ___convert_return_status(da_err);
1939                                                         da_fail = true;
1940                                                         break;
1941                                                 }
1942
1943                                                 service_item->item_id = g_strdup(changedItem->luid);
1944                                                 service_item->content_type = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
1945                                                 service_item->account_id = account_id;
1946                                                 service_item->folder_id = g_strdup(folderId);
1947                                                 service_item->access_name = g_strdup("Engine");
1948                                                 service_item->data = (const void *)g_strdup(changedItem->data);
1949
1950                                                 sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);
1951
1952                                                 da_err = sync_agent_update_service_item(service_item, changedItem->luid, false);
1953
1954                                                 sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);
1955
1956                                                 _DEBUG_VERBOSE("da_err = %d", da_err);
1957                                                 if (da_err == SYNC_AGENT_DA_SUCCESS) {
1958
1959                                                         GList *item_list = NULL;
1960                                                         sync_agent_da_get_item_list_query_s query;
1961                                                         query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING;
1962                                                         query.item_id = changedItem->luid;
1963
1964                                                         da_err = sync_agent_get_item_list(account_id, &query, &item_list);
1965                                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
1966                                                                 _DEBUG_ERROR("failed in sync_agent_get_item_list !!");
1967                                                         }
1968
1969                                                         bool success = true;
1970                                                         GList *iter = NULL;
1971                                                         sync_agent_da_item_s *iter_data;
1972
1973                                                         for (iter = item_list; iter != NULL; iter = g_list_next(iter)) {
1974                                                                 iter_data = NULL;
1975                                                                 iter_data = (sync_agent_da_item_s *) (iter->data);
1976
1977                                                                 da_err = sync_agent_update_item(iter_data->item_id, 1);
1978                                                                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
1979                                                                         success = false;
1980                                                                         break;
1981                                                                 }
1982                                                         }
1983
1984                                                         if (success == true) {
1985                                                                 returnResult = COMMAND_RESULT_OK;
1986                                                                 add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1);
1987                                                         } else {
1988                                                                 /*this case just fail do rollback and goto fail */
1989                                                                 returnResult = ___convert_return_status(da_err);
1990                                                                 da_fail = true;
1991                                                         }
1992
1993                                                         sync_agent_free_item_list(item_list);
1994                                                 } else if (da_err == SYNC_AGENT_DA_ERR_NOT_SUPPORTED || da_err == SYNC_AGENT_DA_ERR_INVALID_CONTENT) {
1995                                                         /*just return error to server */
1996                                                         returnResult = ___convert_return_status(da_err);
1997                                                 } else {
1998                                                         /*this case just fail do rollback and goto fail */
1999                                                         returnResult = ___convert_return_status(da_err);
2000                                                         da_fail = true;
2001                                                 }
2002
2003                                                 if (service_item != NULL)
2004                                                         sync_agent_free_service_item(service_item);
2005                                         } else
2006                                                 returnResult = COMMAND_RESULT_COMMAND_FAIL;
2007
2008                                         break;
2009                                 }
2010                         case CHANGE_DELETE:
2011                                 {
2012                                         /* delete from service DB */
2013                                         GList *item_list = NULL;
2014                                         sync_agent_da_get_item_list_query_s query;
2015                                         query.option = SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING;
2016                                         query.item_id = changedItem->luid;
2017
2018                                         da_err = sync_agent_get_item_list(account_id, &query, &item_list);
2019                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
2020                                                 _DEBUG_ERROR("failed in sync_agent_get_item_list !!");
2021                                         }
2022
2023                                         sync_agent_begin_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);
2024
2025                                         da_err = sync_agent_delete_service_item(changedItem->luid, false);
2026
2027                                         sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);
2028
2029                                         _DEBUG_VERBOSE("[sync_agent_delete_service_item] result : %d", da_err);
2030                                         if (da_err == SYNC_AGENT_DA_SUCCESS) {
2031                                                 bool success = true;
2032                                                 GList *iter = NULL;
2033                                                 sync_agent_da_item_s *iter_data;
2034
2035                                                 for (iter = item_list; iter != NULL; iter = g_list_next(iter)) {
2036                                                         iter_data = NULL;
2037                                                         iter_data = (sync_agent_da_item_s *) (iter->data);
2038
2039                                                         da_err = sync_agent_delete_item(iter_data->item_id, true);
2040                                                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
2041                                                                 success = false;
2042                                                                 break;
2043                                                         }
2044                                                 }
2045
2046                                                 if (success == true) {
2047                                                         returnResult = COMMAND_RESULT_DELETE_WITHOUT_ARCHIVE;
2048                                                         add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result, 1);
2049                                                 } else {
2050                                                         /*this case just fail do rollback and goto fail */
2051                                                         returnResult = ___convert_return_status(da_err);
2052                                                         da_fail = true;
2053                                                 }
2054                                         } else if (da_err == SYNC_AGENT_DA_ERR_NO_DATA || da_err == SYNC_AGENT_DA_ERR_SUB_DATA_EXIST || da_err == SYNC_AGENT_DA_ERR_INVALID_CONTENT) {
2055                                                 /*just return error to server */
2056                                                 returnResult = ___convert_return_status(da_err);
2057                                         } else {
2058                                                 /*this case just fail do rollback and goto fail */
2059                                                 returnResult = ___convert_return_status(da_err);
2060                                                 da_fail = true;
2061                                         }
2062                                         sync_agent_free_item_list(item_list);
2063                                         break;
2064                                 }
2065                         default:
2066                                 break;
2067                         }
2068
2069                         if (da_fail == true) {
2070                                 /*replace syncresult when rollback happened */
2071                                 if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result != NULL)
2072                                         free(datastoreinfo_per_content_type[datastoreContentType]->server_sync_result);
2073
2074                                 datastoreinfo_per_content_type[datastoreContentType]->server_sync_result = tempServerSyncResult;
2075                                 tempServerSyncResult = NULL;
2076
2077                                 _DEBUG_VERBOSE("Transaction_Rollback");
2078                                 _DEBUG_VERBOSE("sync_agent_end_service with datastoreContentType = %d", datastoreContentType);
2079 //                              sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 0);
2080                                 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);
2081
2082                                 err = SE_INTERNAL_DA_ERROR;
2083                                 _DEBUG_ERROR("failed in DA");
2084                                 goto error;
2085                         }
2086
2087                         appliedStatus = create_applied_status(changedItem->luid, changedItem->change_type, returnResult);
2088                         if (appliedStatus == NULL) {
2089                                 _DEBUG_VERBOSE("Transaction_Rollback");
2090                                 _DEBUG_VERBOSE("sync_agent_end_service with datastoreContentType = %d", datastoreContentType);
2091 //                              sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 0);
2092                                 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_ROLLBACK);
2093
2094                                 err = SE_INTERNAL_NO_MEMORY;
2095                                 _DEBUG_ERROR("failed to alloc memory");
2096                                 goto error;
2097                         }
2098                         add_sending_status_applied_status(sendingStatus, appliedStatus);
2099                 }
2100
2101                 if (changeItemCount > 0) {
2102                         /*end transaction */
2103                         _DEBUG_VERBOSE("sync_agent_end_service with datastoreContentType = %d", datastoreContentType);
2104 //                      sync_agent_end_service(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);
2105                         sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
2106                 }
2107
2108                 /* Do not need to send update event to ui when changeItemCount less than 0 */
2109                 /* send update event about add, delete, replace count during this sync command */
2110                 if (changeItemCount >= 0) {
2111                         needToSave = false;
2112                         pServerSyncResult = datastoreinfo_per_content_type[datastoreContentType]->server_sync_result;
2113                         if (pServerSyncResult->number_of_change == pServerSyncResult->received_count) {
2114                                 needToSave = true;
2115
2116                                 if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result != NULL)
2117                                         datastoreinfo_per_content_type[datastoreContentType]->server_sync_result->session_result = SYNC_SESSION_SUCCEEDED;
2118                         }
2119
2120                         operation_type_e operation_type;
2121                         if (datastoreinfo_per_content_type[datastoreContentType]->server_sync_result->number_of_change == 0)
2122                                 operation_type = OPERATION_NOOP;
2123                         else
2124                                 operation_type = OPERATION_ADD;
2125                         err = __process_update(account_id, server_sync_type, SYNC_PROGRESS_SUCCESS, operation_type, datastoreContentType, true, needToSave, datastoreinfo_per_content_type[datastoreContentType]->server_sync_result);
2126                         if (err != SE_INTERNAL_OK) {
2127                                 _DEBUG_ERROR("failed in process_update");
2128                                 goto error;
2129                         }
2130                 }
2131
2132                 (*sync_obj)->sending_status = g_list_append((*sync_obj)->sending_status, sendingStatus);
2133                 sendingStatus = NULL;
2134
2135                 iter = g_list_next(iter);
2136
2137                 (*sync_return_obj)->changed_datastore = g_list_remove((*sync_return_obj)->changed_datastore, pSyncReturnChangedDatastore);
2138                 free_changed_datastore(pSyncReturnChangedDatastore);
2139                 pSyncReturnChangedDatastore = NULL;
2140
2141                 if (tempServerSyncResult != NULL) {
2142                         free(tempServerSyncResult);
2143                         tempServerSyncResult = NULL;
2144                 }
2145         }
2146
2147  error:
2148         sync_agent_free_folder_id_list(f_id_list);
2149
2150         if (tempServerSyncResult != NULL)
2151                 free(tempServerSyncResult);
2152
2153         /* for prevent */
2154         free_sending_status(sendingStatus);
2155
2156         _INNER_FUNC_EXIT;
2157         return err;
2158 }
2159
2160 static se_error_type_e _update_sync_result(int account_id)
2161 {
2162         _INNER_FUNC_ENTER;
2163
2164         se_error_type_e err = SE_INTERNAL_OK;
2165         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
2166         int content_type;
2167
2168         sync_agent_da_last_anchor_s next_anchor_info;
2169         for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
2170                 if (datastoreinfo_per_content_type[content_type] != NULL) {
2171                         if (!datastoreinfo_per_content_type[content_type]->client_sync_type)
2172                                 continue;
2173
2174                         next_anchor_info.access_name = "Engine";
2175                         next_anchor_info.account_id = account_id;
2176                         next_anchor_info.data_store_id = datastoreinfo_per_content_type[content_type]->datastore_id;
2177                         next_anchor_info.last_anchor_client = datastoreinfo_per_content_type[content_type]->next_anchor_client;
2178                         next_anchor_info.last_anchor_server = datastoreinfo_per_content_type[content_type]->next_anchor_server;
2179
2180                         da_err = sync_agent_update_last_anchor(&next_anchor_info);
2181                         if (da_err != SYNC_AGENT_DA_SUCCESS)
2182                                 _DEBUG_ERROR("failed in da_set_last_anchor_internal");
2183
2184                         da_err = sync_agent_set_service_change_point(account_id, datastoreinfo_per_content_type[content_type]->datastore_id);
2185                         if (da_err != SYNC_AGENT_DA_SUCCESS)
2186                                 _DEBUG_ERROR("failed in sync_agent_set_service_change_point");
2187                 }
2188         }
2189
2190         _INNER_FUNC_EXIT;
2191         return err;
2192 }
2193
2194 static command_result_e ___convert_return_status(sync_agent_da_return_e da_err)
2195 {
2196         _INNER_FUNC_ENTER;
2197
2198         command_result_e returnResult = COMMAND_RESULT_INIT;
2199         switch (da_err) {
2200         case 1:         /*SYNC_AGENT_DA_SUCCESS */
2201                 /*never comes this case
2202                    break; */
2203         case -813:              /*SYNC_AGENT_DA_ERR_ALREADY_EXIST */
2204                 returnResult = COMMAND_RESULT_ALREADY_EXIST;    /*[A] Already exists exception */
2205                 break;
2206         case -815:              /*SYNC_AGENT_DA_ERR_MEMORY_FULL *//* oma : 420 (device full exception) */
2207                 returnResult = COMMAND_RESULT_DEVICE_FULL;      /*[AR] Device full exception */
2208                 break;
2209         case -820:              /*SYNC_AGENT_DA_ERR_NOT_SUPPORTED *//* oma : 415 (unsupported media type or format exception) */
2210                 returnResult = COMMAND_RESULT_UNSUPPORTED_TYPE; /*[AR] Unsupported media type or format exception */
2211                 break;
2212         case -819:              /*SYNC_AGENT_DA_ERR_NO_DATA *//* kies : not found, oma : 211 (item not deleted exception) */
2213                 returnResult = COMMAND_RESULT_NOT_EXIST;        /*[D] Item not deleted exception */
2214                 break;
2215         case -800:              /*SYNC_AGENT_DA_ERRORS */
2216         case -801:              /*SYNC_AGENT_DA_NOT_FOUND_PLUG_IN */
2217         case -802:              /*SYNC_AGENT_DA_ERR_OPEN_FAILED */
2218         case -803:              /*SYNC_AGENT_DA_ERR_CLOSE_FAILED */
2219         case -804:              /*SYNC_AGENT_DA_ERR_TRANSACTION_FAILED */
2220         case -805:              /*SYNC_AGENT_DA_ERR_CREATE_TABLE_FAILED */
2221         case -806:              /*SYNC_AGENT_DA_ERR_DROP_TABLE_FAILED */
2222         case -807:              /*SYNC_AGENT_DA_ERR_QUERY_FAILED */
2223         case -808:              /*SYNC_AGENT_DA_ERR_NOT_OPENED */
2224         case -809:              /*SYNC_AGENT_DA_ERR_ACCOUNT_FULL */
2225         case -810:              /*SYNC_AGENT_DA_ERR_DELETE_LAST_ACCOUNT */
2226         case -811:              /*SYNC_AGENT_DA_ERR_PRIMARY_KEY_NOT_UNIQUE */
2227         case -812:              /*SYNC_AGENT_DA_ERR_DB_HANDLER_MGR */
2228         case -814:              /*SYNC_AGENT_DA_ERR_INVALID_CONTENT */
2229         case -816:              /*SYNC_AGENT_DA_ERR_SUB_DATA_EXIST *//* oma : 427 (item not empty) */
2230         case -817:              /*SYNC_AGENT_DA_ERR_LOCKED *//* kies : cannot access */
2231         case -818:              /*SYNC_AGENT_DA_ERR_MORE_DATA */
2232         case -821:              /*SYNC_AGENT_DA_ERR_NOT_EXECUTE *//* kies : ex) sms send command, no network service */
2233         default:
2234                 returnResult = COMMAND_RESULT_COMMAND_FAIL;     /*[ARD] Command failed exception */
2235                 break;
2236         }
2237
2238         _INNER_FUNC_EXIT;
2239         return returnResult;
2240 }
2241
2242 static char *__convert_cttype_str(int datastore_id)
2243 {
2244         _INNER_FUNC_ENTER;
2245
2246         char *cttype = NULL;
2247         /*FIXME : check type and version (contact : vCard2.1 , calendar : vCalendar 1.0 , memo : plain text) of real item data.. */
2248         switch (datastore_id) {
2249         case TYPE_CONTACT:      /*contact (vCard2.1) */
2250                 cttype = ELEMENT_TEXT_VCARD;
2251                 break;
2252         case TYPE_CALENDAR:     /*calendar (vCalendar2.0) */
2253                 cttype = ELEMENT_TEXT_VCAL;
2254                 break;
2255         case TYPE_MEMO: /*note(Memo) */
2256                 cttype = ELEMENT_TEXT_PLAIN;
2257                 break;
2258         case TYPE_CALLLOG:
2259                 cttype = ELEMENT_TEXT_XCALLLOG;
2260                 break;
2261         default:
2262                 break;
2263         }
2264
2265         _INNER_FUNC_EXIT;
2266         return cttype;
2267 }
2268
2269 static int ___convert_sync_type_value(char *sync_type_str)
2270 {
2271         _INNER_FUNC_ENTER;
2272
2273         int sync_type_value;
2274
2275         if (strcmp(sync_type_str, DEFINE_ALERT_SLOW_SYNC_STR) == 0)
2276                 sync_type_value = ALERT_SLOW_SYNC;
2277         else if (strcmp(sync_type_str, DEFINE_ALERT_TWO_WAY_STR) == 0)
2278                 sync_type_value = ALERT_TWO_WAY;
2279         else if (strcmp(sync_type_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) == 0)
2280                 sync_type_value = ALERT_ONE_WAY_FROM_CLIENT;
2281         else if (strcmp(sync_type_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) == 0)
2282                 sync_type_value = ALERT_ONE_WAY_FROM_SERVER;
2283         else if (strcmp(sync_type_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) == 0)
2284                 sync_type_value = ALERT_REFRESH_FROM_SERVER;
2285         else if (strcmp(sync_type_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) == 0)
2286                 sync_type_value = ALERT_REFRESH_FROM_CLIENT;
2287         else
2288                 sync_type_value = ALERT_UNKNOWN;
2289
2290         _INNER_FUNC_EXIT;
2291         return sync_type_value;
2292 }
2293
2294 static char *__convert_sync_type_str(alert_type_e sync_type)
2295 {
2296         _INNER_FUNC_ENTER;
2297
2298         char *sync_Type = NULL;
2299
2300         switch (sync_type) {
2301         case ALERT_TWO_WAY:
2302                 sync_Type = DEFINE_ALERT_TWO_WAY_STR;
2303                 break;
2304         case ALERT_SLOW_SYNC:
2305                 sync_Type = DEFINE_ALERT_SLOW_SYNC_STR;
2306                 break;
2307         case ALERT_ONE_WAY_FROM_CLIENT:
2308                 sync_Type = DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR;
2309                 break;
2310         case ALERT_REFRESH_FROM_CLIENT:
2311                 sync_Type = DEFINE_ALERT_REFRESH_FROM_CLIENT_STR;
2312                 break;
2313         case ALERT_ONE_WAY_FROM_SERVER:
2314                 sync_Type = DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR;
2315                 break;
2316         case ALERT_REFRESH_FROM_SERVER:
2317                 sync_Type = DEFINE_ALERT_REFRESH_FROM_SERVER_STR;
2318                 break;
2319         default:
2320                 sync_Type = DEFINE_ALERT_UNKNOWN_STR;
2321                 break;
2322         }
2323
2324         _INNER_FUNC_EXIT;
2325         return sync_Type;
2326 }
2327
2328 static char *___convert_sync_progress_status_str(sync_progress_status_e progress_status)
2329 {
2330         _INNER_FUNC_ENTER;
2331
2332         char *progress = NULL;
2333
2334         switch (progress_status) {
2335         case SYNC_PROGRESS_NONE:
2336                 progress = DEFINE_PROGRESS_NONE;
2337                 break;
2338         case SYNC_PROGRESS_SUCCESS:
2339                 progress = DEFINE_PROGRESS_SUCCESS;
2340                 break;
2341         case SYNC_FAILED_DB:
2342                 progress = DEFINE_FAILED_DB;
2343                 break;
2344         case SYNC_FAILED_DB_FORBIDDEN:
2345                 progress = DEFINE_FAILED_DB_FORBIDDEN;
2346                 break;
2347         case SYNC_FAILED_DB_ITEM:
2348                 progress = DEFINE_FAILED_DB_ITEM;
2349                 break;
2350         case SYNC_FAILED_DB_CONFIG:
2351                 progress = DEFINE_FAILED_DB_CONFIG;
2352                 break;
2353         case SYNC_FAILED_DB_DEVICEFULL:
2354                 progress = DEFINE_FAILED_DB_DEVICEFULL;
2355                 break;
2356         default:
2357                 break;
2358         }
2359
2360         _INNER_FUNC_EXIT;
2361         return progress;
2362 }
2363
2364 static char *___convert_operation_type_str(operation_type_e operation_type)
2365 {
2366         _INNER_FUNC_ENTER;
2367
2368         char *operation = NULL;
2369
2370         switch (operation_type) {
2371         case OPERATION_NOOP:
2372                 operation = DEFINE_NOOP;
2373                 break;
2374         case OPERATION_ADD:
2375                 operation = DEFINE_ADD;
2376                 break;
2377         case OPERATION_DELETE:
2378                 operation = DEFINE_DELETE;
2379                 break;
2380         case OPERATION_MOVE:
2381                 operation = DEFINE_MOVE;
2382                 break;
2383         case OPERATION_COPY:
2384                 operation = DEFINE_COPY;
2385                 break;
2386         case OPERATION_REPLACE:
2387                 operation = DEFINE_REPLACE;
2388                 break;
2389         default:
2390                 break;
2391         }
2392
2393         _INNER_FUNC_EXIT;
2394         return operation;
2395 }
2396
2397 static char *_convert_sync_progress_str(sync_progress_e process)
2398 {
2399         _INNER_FUNC_ENTER;
2400
2401         char *syncProcess = NULL;
2402
2403         switch (process) {
2404         case PROGRESS_NONE:
2405                 syncProcess = DEFINE_SYNC_PROGRESS_NONE;
2406                 break;
2407         case PROGRESS_INIT:
2408                 syncProcess = DEFINE_SYNC_INIT;
2409                 break;
2410         case PROGRESS_CONNECTING:
2411                 syncProcess = DEFINE_SYNC_CONNECTING;
2412                 break;
2413         case PROGRESS_AUTHENTICATED:
2414                 syncProcess = DEFINE_SYNC_AUTHENTICATED;
2415                 break;
2416         case PROGRESS_DONE:
2417                 syncProcess = DEFINE_SYNC_DONE;
2418                 break;
2419         case PROGRESS_ERROR:
2420                 syncProcess = DEFINE_SYNC_ERROR;
2421                 break;
2422         default:
2423                 break;
2424         }
2425
2426         _INNER_FUNC_EXIT;
2427         return syncProcess;
2428 }
2429
2430 static char *_convert_sync_error_str(sync_error_e error)
2431 {
2432         _INNER_FUNC_ENTER;
2433
2434         char *syncError = NULL;
2435
2436         switch (error) {
2437         case ERROR_NONE:
2438                 syncError = DEFINE_ERROR_NONE;
2439                 break;
2440         case ERROR_CONNECTION:
2441                 syncError = DEFINE_ERROR_CONNECTION;
2442                 break;
2443         case ERROR_SYNCHDR:
2444                 syncError = DEFINE_ERROR_SYNCHDR;
2445                 break;
2446         case ERROR_INTERNAL:
2447                 syncError = DEFINE_ERROR_INTERNAL;
2448                 break;
2449         case ERROR_SUSPENDED:
2450                 syncError = DEFINE_ERROR_SUSPENDED;
2451                 break;
2452         case ERROR_DB:
2453                 syncError = DEFINE_ERROR_DB;
2454                 break;
2455         case ERROR_ABORT:
2456                 syncError = DEFINE_ERROR_ABORT;
2457                 break;
2458         case ERROR_SERVER:
2459                 syncError = DEFINE_ERROR_SERVER;
2460                 break;
2461         case ERROR_MEMORY_FULL:
2462                 syncError = DEFINE_ERROR_MEMORY_FULL;
2463                 break;
2464         case ERROR_AUTHENTICATE:
2465                 syncError = DEFINE_ERROR_AUTHENTICATE;
2466                 break;
2467         case ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER:
2468                 syncError = DEFINE_ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2469                 break;
2470         case ERROR_LOW_BATTERY:
2471                 syncError = DEFINE_ERROR_LOW_BATTERY;
2472                 break;
2473         default:
2474                 break;
2475         }
2476
2477         _INNER_FUNC_EXIT;
2478         return syncError;
2479 }
2480
2481 static se_error_type_e _check_low_battery()
2482 {
2483         _INNER_FUNC_ENTER;
2484
2485         se_error_type_e err = SE_INTERNAL_OK;
2486         sync_agent_dev_return_e dci_err = SYNC_AGENT_DEV_RETURN_SUCCESS;
2487         char *battery_level = NULL;
2488         int int_battery_level = 0;
2489
2490         dci_err = sync_agent_get_devinfo(1, "Battery", &battery_level);
2491         if (dci_err != SYNC_AGENT_DEV_RETURN_SUCCESS) {
2492                 _DEBUG_ERROR("failed in sync_agent_get_devinfo = %d", dci_err);
2493                 err = SE_INTERNAL_DA_ERROR;
2494                 goto error;
2495         }
2496
2497         _DEBUG_TRACE("battery_level =%s", battery_level);
2498
2499         if (battery_level != NULL) {
2500                 int_battery_level = atoi(battery_level);
2501
2502                 if (int_battery_level < LOW_BATTERY_LEVEL) {
2503                         _DEBUG_ERROR("LOW Battery = %d", int_battery_level);
2504                         err = SE_INTERNAL_LOW_BATTERY;
2505                         goto error;
2506                 }
2507         }
2508
2509  error:
2510
2511         if (battery_level != NULL)
2512                 free(battery_level);
2513
2514         _INNER_FUNC_EXIT;
2515         return err;
2516 }
2517
2518 static se_error_type_e _open_services()
2519 {
2520         _EXTERN_FUNC_ENTER;
2521
2522         se_error_type_e err = SE_INTERNAL_OK;
2523         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
2524
2525         int content_type;
2526         for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
2527                 if (datastoreinfo_per_content_type[content_type] != NULL) {
2528                         if (!datastoreinfo_per_content_type[content_type]->client_sync_type)
2529                                 continue;
2530
2531                         da_err = sync_agent_open_service(content_type);
2532                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
2533                                 _DEBUG_ERROR("sync_agent_open_service(%d) is failed", content_type);
2534                                 err = SE_INTERNAL_DA_ERROR;
2535                                 break;
2536                         }
2537                 }
2538         }
2539
2540         _INNER_FUNC_EXIT;
2541         return err;
2542 }
2543
2544 static se_error_type_e _close_services()
2545 {
2546         _EXTERN_FUNC_ENTER;
2547
2548         se_error_type_e err = SE_INTERNAL_OK;
2549         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
2550
2551         int content_type;
2552         for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
2553                 if (datastoreinfo_per_content_type[content_type] != NULL) {
2554                         if (!datastoreinfo_per_content_type[content_type]->client_sync_type)
2555                                 continue;
2556
2557                         da_err = sync_agent_close_service(content_type);
2558                         if (da_err != SYNC_AGENT_DA_SUCCESS)
2559                                 _DEBUG_ERROR("sync_agent_close_service(%d) is failed", content_type);
2560                 }
2561         }
2562
2563         _INNER_FUNC_EXIT;
2564         return err;
2565 }
2566
2567 bool synchronize(int account_id, char *sync_mode, san_package_s * san_package)
2568 {
2569         _EXTERN_FUNC_ENTER;
2570
2571         _DEBUG_INFO("accountID = %d", account_id);
2572         _DEBUG_INFO("sync_mode = %s", sync_mode);
2573
2574         /*FIXME remove msg file */
2575         int ret_val = remove(OMA_DS_MSG_PATH);
2576         if (ret_val < 0) {
2577                 _DEBUG_ERROR("failed in synchronize - remove()");
2578         }
2579
2580         /*FIXME time check */
2581         long t, dt;
2582         t = myclock();
2583
2584         se_error_type_e err = SE_INTERNAL_OK;
2585         sync_agent_dev_return_e dci_result = SYNC_AGENT_DEV_RETURN_SUCCESS;
2586         sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;
2587
2588         common_error_type_e errorCode = COMMON_OK;
2589         sync_progress_e process = PROGRESS_NONE;
2590         sync_error_e error = ERROR_NONE;
2591
2592         alert_type_e client_sync_type = ALERT_UNKNOWN;
2593         alert_type_e server_sync_type = ALERT_UNKNOWN;
2594         int only_from_client = 0;
2595
2596         int session_time = sync_agent_convert_seconds_to_utc(time(NULL));       /*lastSessionTime for resultView; */
2597         _DEBUG_INFO("session_time = %d", session_time);
2598
2599         pre_sync_return_obj_s *pre_sync_return_obj = NULL;
2600         sync_obj_s *sync_obj = NULL;
2601         sync_return_obj_s *sync_return_obj = NULL;
2602
2603         bool cancel_flag = false;
2604         int content_type;
2605         char *session_id = NULL;
2606
2607         char *access_name = NULL;
2608         bool server_flag = false;
2609
2610         char *msg = NULL;
2611         unsigned int msg_size;
2612         char *recvMsg = NULL;
2613         unsigned int recvMsg_size;
2614
2615         sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
2616         sync_agent_fw_account_s *fw_account = NULL;
2617
2618         dci_result = sync_agent_execute_dev_function(DEFINE_PLATFORM, "pm_lock", 3, LCD_OFF, STAY_CUR_STATE, 0);
2619         if (dci_result != SYNC_AGENT_DEV_RETURN_SUCCESS) {
2620                 _DEBUG_ERROR("failed in sync_agent_execute_dev_function lock = %d", dci_result);
2621         }
2622
2623         /* low battery check */
2624         err = _check_low_battery();
2625         if (err != SE_INTERNAL_OK) {
2626                 _DEBUG_ERROR("check_low_battery = %d", err);
2627                 goto fail_part;
2628         }
2629
2630         da_err = sync_agent_open_agent();
2631         if (da_err != SYNC_AGENT_DA_SUCCESS) {
2632                 _DEBUG_ERROR("failed in sync_agent_open_agent = %d", da_err);
2633                 err = SE_INTERNAL_DA_ERROR;
2634                 goto fail_part;
2635         }
2636
2637         /* prepare for sending pre sync */
2638         err = _prepare_pre_sync(account_id, sync_mode, san_package, &client_sync_type);
2639         if (err != SE_INTERNAL_OK) {
2640                 _DEBUG_ERROR("failed in __prepare_pre_sync = %d", err);
2641                 goto fail_part;
2642         }
2643
2644         /* check cancel flag */
2645         cancel_flag = sync_agent_check_cancel_flag();
2646         if (cancel_flag) {
2647                 err = SE_INTERNAL_CANCEL;
2648                 _DEBUG_INFO("cancle flag is on");
2649                 goto cancel_part;
2650         }
2651
2652         pre_sync_return_obj = (pre_sync_return_obj_s *) calloc(1, sizeof(pre_sync_return_obj_s));
2653         if (pre_sync_return_obj == NULL) {
2654                 _DEBUG_ERROR("failed to alloc memory");
2655                 err = SE_INTERNAL_NO_MEMORY;
2656                 goto fail_part;
2657         }
2658
2659         acc_err = sync_agent_create_fw_account(&fw_account);
2660         if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
2661                 _DEBUG_ERROR("failed in sync_agent_create_fw_account");
2662                 err = SE_INTERNAL_NO_MEMORY;
2663                 goto fail_part;
2664         }
2665
2666         acc_err = sync_agent_get_fw_account(account_id, &fw_account);
2667         if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
2668                 _DEBUG_ERROR("failed in sync_agent_update_fw_account");
2669                 err = SE_INTERNAL_ERROR;
2670                 goto fail_part;
2671         }
2672
2673         access_name = strdup(fw_account->access_name);
2674         if (strcmp(access_name, "DIVE") == 0) {
2675                 server_flag = true;
2676         }
2677
2678         /*pkg 1 */
2679         if (strcmp(sync_mode, DEFINE_SYNC_MODE_PUSH) == 0) {
2680                 _DEBUG_INFO("sessionID = %d", san_package->session_id);
2681                 session_id = g_strdup_printf("%u", san_package->session_id);    /*freed in pre_sync */
2682                 errorCode = pre_sync(TRANSPORT_TYPE, account_id, session_id, server_flag, (void **)&pre_sync_return_obj);
2683         } else
2684                 errorCode = pre_sync(TRANSPORT_TYPE, account_id, NULL, server_flag, (void **)&pre_sync_return_obj);
2685
2686         /* check cancel flag */
2687         cancel_flag = sync_agent_check_cancel_flag();
2688         if (cancel_flag) {
2689                 err = SE_INTERNAL_CANCEL;
2690                 _DEBUG_INFO("cancle flag is on");
2691                 goto cancel_part;
2692         }
2693
2694         _DEBUG_INFO("pre_sync errorCode =[%d]", errorCode);
2695         if (errorCode != COMMON_OK) {
2696                 err = SE_INTERNAL_SA_ERROR;
2697                 goto fail_part;
2698         }
2699
2700         /*execute pre_sync return */
2701         server_sync_type = client_sync_type;
2702         err = _execute_pre_sync(account_id, session_time, pre_sync_return_obj, &server_sync_type);
2703         if (err != SE_INTERNAL_OK) {
2704                 _DEBUG_ERROR("failed in __process_pre_sync = %d", err);
2705                 goto fail_part;
2706         }
2707
2708         if (server_sync_type == ALERT_REFRESH_FROM_CLIENT || server_sync_type == ALERT_ONE_WAY_FROM_CLIENT)
2709                 only_from_client = 1;
2710
2711         _DEBUG_INFO("client_sync_type = %d", client_sync_type);
2712         _DEBUG_INFO("server_sync_type = %d", server_sync_type);
2713         _DEBUG_INFO("only_from_client = %d", only_from_client);
2714
2715         _session_process(account_id, server_sync_type, PROGRESS_AUTHENTICATED, ERROR_NONE);
2716
2717         /* check cancel flag */
2718         cancel_flag = sync_agent_check_cancel_flag();
2719         if (cancel_flag) {
2720                 err = SE_INTERNAL_CANCEL;
2721                 _DEBUG_INFO("cancle flag is on");
2722                 goto cancel_part;
2723         }
2724
2725         err = _open_services();
2726         if (err != SE_INTERNAL_OK) {
2727                 _DEBUG_ERROR("failed in __prepare_pre_sync = %d", err);
2728                 goto fail_part;
2729         }
2730
2731         /* pkg 3 */
2732         _DEBUG_INFO("pre_sync end pkg3 start");
2733         sync_obj = (sync_obj_s *) calloc(1, sizeof(sync_obj_s));
2734         if (sync_obj == NULL) {
2735                 _DEBUG_ERROR("failed to alloc memory");
2736                 err = SE_INTERNAL_NO_MEMORY;
2737                 goto fail_part;
2738         }
2739
2740         sync_return_obj = (sync_return_obj_s *) calloc(1, sizeof(sync_return_obj_s));
2741         if (sync_return_obj == NULL) {
2742                 _DEBUG_ERROR("failed to alloc memory");
2743                 err = SE_INTERNAL_NO_MEMORY;
2744                 goto fail_part;
2745         }
2746
2747         err = _assemble_changed_datastores(account_id, server_sync_type, &sync_obj);
2748         if (err != SE_INTERNAL_OK) {
2749                 _DEBUG_ERROR("failed in __assemble_changeddatastore = %d", err);
2750                 goto fail_part;
2751         }
2752
2753         /* check cancel flag */
2754         cancel_flag = sync_agent_check_cancel_flag();
2755         if (cancel_flag) {
2756                 err = SE_INTERNAL_CANCEL;
2757                 _DEBUG_INFO("cancle flag is on");
2758                 goto cancel_part;
2759         }
2760
2761         int isFinish = 0;
2762         while (!isFinish) {
2763
2764                 errorCode = generate_msg((void **)&sync_obj, server_flag, &msg, &msg_size);
2765                 if (errorCode != COMMON_OK) {
2766                         _DEBUG_ERROR("Failed in generate_Msg = %d", errorCode);
2767                         err = SE_INTERNAL_SA_ERROR;
2768                         goto fail_part;
2769                 }
2770
2771                 errorCode = exchange_msg(TRANSPORT_TYPE, msg, msg_size, &recvMsg, &recvMsg_size);
2772                 if (errorCode != COMMON_OK) {
2773                         _DEBUG_ERROR("Failed in exchange_Msg = %d", errorCode);
2774                         if (errorCode == COMMON_CANCEL) {
2775                                 bool cancel_status = check_cancel_status();
2776                                 if (cancel_status) {
2777                                         err = SE_INTERNAL_SUSPEND;
2778                                         _DEBUG_INFO("cancle flag is on");
2779                                         goto suspend_part;
2780                                 }
2781                         } else {
2782                                 err = SE_INTERNAL_SA_ERROR;
2783                                 goto fail_part;
2784                         }
2785                 }
2786
2787                 /* check cancel flag */
2788                 cancel_flag = sync_agent_check_cancel_flag();
2789                 if (cancel_flag) {
2790                         bool cancel_status = check_cancel_status();
2791                         if (cancel_status) {
2792                                 err = SE_INTERNAL_SUSPEND;
2793                                 _DEBUG_INFO("cancle flag is on");
2794                                 goto suspend_part;
2795                         }
2796                 }
2797
2798                 errorCode = process_recv_msg(recvMsg, recvMsg_size, only_from_client, (void **)&sync_return_obj, &isFinish);
2799                 if (errorCode != COMMON_OK) {
2800                         _DEBUG_ERROR("Failed in processRecv_Msg = %d", errorCode);
2801                         err = SE_INTERNAL_SA_ERROR;
2802                         goto fail_part;
2803                 }
2804
2805                 err = _execute_sync(account_id, client_sync_type, server_sync_type, &sync_obj, &sync_return_obj);
2806                 if (err != SE_INTERNAL_OK) {
2807                         _DEBUG_ERROR("failed in __execute_sync = %d", err);
2808                         goto fail_part;
2809                 }
2810
2811                 free_changed_datastores(sync_return_obj->changed_datastore);
2812                 sync_return_obj->changed_datastore = NULL;
2813
2814                 free_applied_statuses(sync_return_obj->status);
2815                 sync_return_obj->status = NULL;
2816
2817                 if (msg != NULL) {
2818                         free(msg);
2819                         msg = NULL;
2820                 }
2821
2822                 if (recvMsg != NULL) {
2823                         free(recvMsg);
2824                         recvMsg = NULL;
2825                 }
2826         }
2827
2828         err = _update_sync_result(account_id);
2829         if (err != SE_INTERNAL_OK) {
2830                 _DEBUG_ERROR("failed in __update_anchor = %d", err);
2831                 goto fail_part;
2832         }
2833
2834         _write_sync_data(account_id, client_sync_type, SYNC_SESSION_SUCCEEDED, session_time, only_from_client);
2835
2836         goto return_part;
2837
2838  suspend_part:
2839
2840         errorCode = suspend_sync(TRANSPORT_TYPE, account_id, server_flag);
2841         if (errorCode != COMMON_OK) {
2842                 _DEBUG_ERROR("Failed in suspend_sync = %d", errorCode);
2843                 err = SE_INTERNAL_SA_ERROR;
2844                 if (errorCode == COMMON_SUSPEND_FAIL)
2845                         _off_resume_flag(account_id);
2846
2847                 goto fail_part;
2848         }
2849
2850  cancel_part:
2851
2852         /*clean up for SA unusual end sync process case */
2853         clean_up_sa();
2854
2855         for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
2856                 if (datastoreinfo_per_content_type[content_type] != NULL) {
2857                         if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
2858                                 if (datastoreinfo_per_content_type[content_type]->client_sync_result != NULL)
2859                                         datastoreinfo_per_content_type[content_type]->client_sync_result->session_result = SYNC_SESSION_STOPPED;
2860
2861                                 if (datastoreinfo_per_content_type[content_type]->server_sync_result != NULL)
2862                                         datastoreinfo_per_content_type[content_type]->server_sync_result->session_result = SYNC_SESSION_STOPPED;
2863
2864                         }
2865                 }
2866         }
2867
2868         _write_sync_data(account_id, client_sync_type, SYNC_SESSION_STOPPED, session_time, only_from_client);
2869
2870         goto return_part;
2871
2872  fail_part:
2873
2874         /*clean up for SA unusual end sync process case */
2875         clean_up_sa();
2876
2877         for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
2878                 if (datastoreinfo_per_content_type[content_type] != NULL) {
2879                         if (datastoreinfo_per_content_type[content_type]->client_sync_type) {
2880                                 if (datastoreinfo_per_content_type[content_type]->client_sync_result != NULL)
2881                                         datastoreinfo_per_content_type[content_type]->client_sync_result->session_result = SYNC_SESSION_FAILED;
2882
2883                                 if (datastoreinfo_per_content_type[content_type]->server_sync_result != NULL)
2884                                         datastoreinfo_per_content_type[content_type]->server_sync_result->session_result = SYNC_SESSION_FAILED;
2885                         }
2886                 }
2887         }
2888
2889         _write_sync_data(account_id, client_sync_type, SYNC_SESSION_FAILED, session_time, only_from_client);
2890
2891  return_part:
2892
2893         if (err == SE_INTERNAL_SA_ERROR)
2894                 convert_common_errorcode(errorCode, &process, &error);
2895         else
2896                 convert_engine_errorcode(err, &process, &error);
2897
2898         /* off when session finish normal or cancel case(not suspend) */
2899         if (err != SE_INTERNAL_SUSPEND)
2900                 _off_resume_flag(account_id);
2901
2902         _off_synchronising_account(account_id);
2903
2904         _session_process(account_id, server_sync_type, process, error);
2905         _DEBUG_INFO("server_sync_type = %d", server_sync_type);
2906         _DEBUG_INFO("process = %d", process);
2907         _DEBUG_INFO("error = %d", error);
2908
2909         sync_agent_close_agent();
2910         _close_services();
2911
2912         dci_result = sync_agent_execute_dev_function(DEFINE_PLATFORM, "pm_unlock", 2, LCD_OFF, RESET_TIMER);
2913         if (dci_result != SYNC_AGENT_DEV_RETURN_SUCCESS) {
2914                 _DEBUG_ERROR("failed in sync_agent_execute_dev_function unlock");
2915         }
2916
2917         sync_agent_free_fw_account(fw_account);
2918
2919         if (access_name != NULL) {
2920                 free(access_name);
2921                 access_name = NULL;
2922         }
2923
2924         if (msg != NULL) {
2925                 free(msg);
2926                 msg = NULL;
2927         }
2928
2929         if (recvMsg != NULL) {
2930                 free(recvMsg);
2931                 recvMsg = NULL;
2932         }
2933
2934         if (pre_sync_return_obj != NULL) {
2935                 free_pre_sync_return_obj(pre_sync_return_obj);
2936                 pre_sync_return_obj = NULL;
2937         }
2938
2939         if (sync_obj != NULL) {
2940                 free_sync_obj(sync_obj);
2941                 sync_obj = NULL;
2942         }
2943
2944         if (sync_return_obj != NULL) {
2945                 free_sync_return_obj(sync_return_obj);
2946                 sync_return_obj = NULL;
2947         }
2948
2949         for (content_type = 0; content_type < TYPE_SERVICE_COUNT; content_type++) {
2950                 if (datastoreinfo_per_content_type[content_type] != NULL) {
2951                         free_datastore(datastoreinfo_per_content_type[content_type]);
2952                         datastoreinfo_per_content_type[content_type] = NULL;
2953                 }
2954         }
2955
2956          /*FIXME*/ dt = myclock() - t;
2957         _DEBUG_ERROR("\n=============TOTAL---- % d . % d sec\n", dt / 1000, dt % 1000);
2958
2959         _EXTERN_FUNC_EXIT;
2960         if (err != SE_INTERNAL_OK)
2961                 return false;
2962         else
2963                 return true;
2964 }
2965
2966 void convert_common_errorcode(common_error_type_e errorCode, sync_progress_e * process, sync_error_e * error)
2967 {
2968         _EXTERN_FUNC_ENTER;
2969
2970         switch (errorCode) {
2971         case COMMON_CANCEL:
2972         case COMMON_SUSPEND_FAIL:
2973         case COMMON_OK: /*ERROR_INTERNAL_OK */
2974                 {
2975                         /* Do nothing : Error None
2976                          * pre_sync : PROGRESS_AUTHENTICATED
2977                          * sync end : PROGRESS_DONE
2978                          process = PROGRESS_AUTHENTICATED;
2979                          process = PROGRESS_DONE;
2980                          */
2981                         *process = PROGRESS_DONE;
2982                         *error = ERROR_NONE;
2983                 }
2984                 break;
2985         case COMMON_MISCONFIGURATION:   /*ERROR_INTERNAL_MISCONFIGURATION : need configure infomation (account_id, id, pw, server_url...) */
2986                 {
2987                         *process = PROGRESS_ERROR;
2988                         *error = ERROR_SYNCHDR;
2989                 }
2990                 break;
2991         case COMMON_AUTHENTICATION_ERROR:       /*ERROR_AUTH_REQUIRED, ERROR_AUTH_REJECTED */
2992                 {
2993                         *process = PROGRESS_ERROR;
2994                         *error = ERROR_AUTHENTICATE;
2995                 }
2996                 break;
2997         case COMMON_NOT_FOUND:  /*ERROR_NOT_FOUND (ERROR_INTERNAL ??) */
2998                 {
2999                         *process = PROGRESS_ERROR;
3000                         *error = ERROR_SYNCHDR;
3001                 }
3002                 break;
3003         case COMMON_NO_MEMORY:  /*ERROR_INTERNAL_NO_MEMORY */
3004                 {
3005                         *process = PROGRESS_ERROR;
3006                         *error = ERROR_MEMORY_FULL;
3007                 }
3008                 break;
3009         case COMMON_INTERNAL_ERROR:     /*ERROR_INTERNAL_NOT_DEFINED || ERROR_INTERNAL_BINDER_ERROR */
3010                 {
3011                         *process = PROGRESS_ERROR;
3012                         *error = ERROR_INTERNAL;
3013                 }
3014                 break;
3015         case COMMON_SERVER_ERROR:       /*ERROR_GENERIC || ERROR_SERVER_FAILURE */
3016                 {
3017                         *process = PROGRESS_ERROR;
3018                         *error = ERROR_SERVER;
3019                 }
3020                 break;
3021         case COMMON_CONNECTION_ERROR:   /*ERROR_INTERNAL_CONNECTION_ERROR */
3022                 {
3023                         *process = PROGRESS_ERROR;
3024                         *error = ERROR_CONNECTION;
3025                 }
3026                 break;
3027         case COMMON_AUTOCONFIG_NOT_SUPPORT_BY_SERVER:   /*ERROR_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER */
3028                 {
3029                         *process = PROGRESS_ERROR;
3030                         *error = ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
3031                 }
3032                 break;
3033         default:
3034                 {
3035                         *process = PROGRESS_ERROR;
3036                         *error = ERROR_INTERNAL;        /*?? unknown error */
3037                 }
3038                 break;
3039         }
3040
3041         _EXTERN_FUNC_EXIT;
3042 }
3043
3044 void convert_engine_errorcode(se_error_type_e err, sync_progress_e * process, sync_error_e * error)
3045 {
3046         _EXTERN_FUNC_ENTER;
3047
3048         switch (err) {
3049         case SE_INTERNAL_SUSPEND:
3050         case SE_INTERNAL_CANCEL:
3051         case SE_INTERNAL_OK:
3052                 *process = PROGRESS_DONE;
3053                 *error = ERROR_NONE;
3054                 break;
3055         case ERROR_UNKNOWN:
3056         case SE_INTERNAL_SA_ERROR:
3057         case SE_INTERNAL_SCHEDULER_ERROR:
3058         case SE_INTERNAL_ENGINE_CONTROLER_ERROR:
3059         case SE_INTERNAL_EVENT_ERROR:
3060         case SE_INTERNAL_NOT_DEFINED:
3061         case SE_INTERNAL_ERROR:
3062                 *process = PROGRESS_ERROR;
3063                 *error = ERROR_INTERNAL;
3064                 break;
3065         case SE_INTERNAL_NO_MEMORY:
3066                 *process = PROGRESS_ERROR;
3067                 *error = ERROR_MEMORY_FULL;
3068                 break;
3069         case SE_INTERNAL_DA_ERROR:
3070                 *process = PROGRESS_ERROR;
3071                 *error = ERROR_DB;
3072                 break;
3073         case SE_INTERNAL_MISCONFIGURATION:
3074                 *process = PROGRESS_ERROR;
3075                 *error = ERROR_SYNCHDR;
3076                 break;
3077         case SE_INTERNAL_LOW_BATTERY:
3078                 *process = PROGRESS_ERROR;
3079                 *error = ERROR_LOW_BATTERY;
3080                 break;
3081         }
3082
3083         _EXTERN_FUNC_EXIT;
3084 }
3085
3086 se_error_type_e session_process(char *profileDirName, alert_type_e server_sync_type, sync_progress_e process, sync_error_e error)
3087 {
3088         _EXTERN_FUNC_ENTER;
3089
3090         _DEBUG_INFO("profileDirName = %s", profileDirName);
3091         _DEBUG_INFO("process = %d", process);
3092         _DEBUG_INFO("error = %d", error);
3093
3094         se_error_type_e err = SE_INTERNAL_OK;
3095
3096         char *sync_type = NULL;
3097         char *syncProcess = NULL;
3098         char *syncError = NULL;
3099
3100         sync_type = __convert_sync_type_str(server_sync_type);
3101
3102         syncProcess = _convert_sync_progress_str(process);
3103
3104         syncError = _convert_sync_error_str(error);
3105
3106         if (sync_type == NULL || syncProcess == NULL || syncError == NULL) {
3107                 err = SE_INTERNAL_NOT_DEFINED;
3108                 goto error;
3109         }
3110
3111         err = send_noti_session_process(profileDirName, sync_type, syncProcess, syncError);
3112         if (err != SE_INTERNAL_OK) {
3113                 _DEBUG_ERROR("failed in send_noti_session_process");
3114                 goto error;
3115         }
3116
3117         _EXTERN_FUNC_EXIT;
3118
3119  error:
3120
3121         return err;
3122 }
3123
3124 se_error_type_e reset_synchronizing_profiles()
3125 {
3126         _EXTERN_FUNC_ENTER;
3127
3128         se_error_type_e err = SE_INTERNAL_OK;
3129
3130         sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
3131         sync_agent_fw_account_s *fw_account = NULL;
3132         GList *account_info_list = NULL;
3133         GList *iter = NULL;
3134
3135         sync_agent_da_return_e da_err = sync_agent_open_agent();
3136         if (da_err != SYNC_AGENT_DA_SUCCESS) {
3137                 _DEBUG_ERROR("failed in sync_agent_open_agent");
3138                 err = SE_INTERNAL_DA_ERROR;
3139                 goto error;
3140         }
3141
3142         sync_agent_fw_account_query_s query;
3143         query.query = ACCOUNT_QUERY_BY_NONE;
3144
3145         acc_err = sync_agent_query_fw_account(&query, &account_info_list);
3146         if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
3147                 _DEBUG_ERROR("sync_agent_query_fw_account is failed");
3148                 goto error;
3149         }
3150
3151         for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
3152                 fw_account = (sync_agent_fw_account_s *) iter->data;
3153
3154                 _DEBUG_INFO("account = %d", fw_account->account_id);
3155                 _off_synchronising_account(fw_account->account_id);
3156
3157                 _DEBUG_INFO("construct_itemTbl From service start");
3158
3159                 int content_type;
3160                 for (content_type = 0; content_type < TYPE_SERVICE_COUNT - 1; content_type++) {
3161
3162                         da_err = sync_agent_open_service(content_type);
3163                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
3164                                 _DEBUG_ERROR("sync_agent_open_service(%d) is failed ", content_type);
3165                                 continue;
3166                         }
3167
3168                         _DEBUG_INFO("sync_agent_construct_item_tbl_from_service(%d, %d);", fw_account->account_id, content_type);
3169                         sync_agent_construct_item_tbl_from_service(fw_account->account_id, content_type);
3170
3171                         da_err = sync_agent_close_service(content_type);
3172                         if (da_err != SYNC_AGENT_DA_SUCCESS) {
3173                                 _DEBUG_ERROR("sync_agent_close_service(%d) is failed ", content_type);
3174                                 continue;
3175                         }
3176                 }
3177
3178                 _DEBUG_INFO("construct_itemTbl From service end");
3179         }
3180
3181  error:
3182
3183         sync_agent_close_agent();
3184
3185         sync_agent_free_fw_account_list(account_info_list);
3186
3187         _EXTERN_FUNC_EXIT;
3188
3189         return err;
3190 }
3191
3192 bool refresh_from_service_all(int account_id)
3193 {
3194         _EXTERN_FUNC_ENTER;
3195
3196         se_error_type_e err = SE_INTERNAL_OK;
3197
3198         sync_agent_da_return_e da_err = sync_agent_open_agent();
3199         if (da_err != SYNC_AGENT_DA_SUCCESS) {
3200                 err = SE_INTERNAL_DA_ERROR;
3201                 goto error;
3202         }
3203
3204         int content_type;
3205         for (content_type = 0; content_type < TYPE_SERVICE_COUNT - 1; content_type++) {
3206
3207                 da_err = sync_agent_open_service(content_type);
3208                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
3209                         _DEBUG_ERROR("sync_agent_open_service(%d) is failed ", content_type);
3210                         continue;
3211                 }
3212
3213                 _DEBUG_INFO("sync_agent_refresh_item_tbl_from_service(%d, %d);", account_id, content_type);
3214                 sync_agent_refresh_item_tbl_from_service(account_id, content_type);
3215
3216                 da_err = sync_agent_close_service(content_type);
3217                 if (da_err != SYNC_AGENT_DA_SUCCESS) {
3218                         _DEBUG_ERROR("sync_agent_close_service(%d) is failed ", content_type);
3219                         continue;
3220                 }
3221         }
3222
3223  error:
3224
3225         sync_agent_close_agent();
3226
3227         _EXTERN_FUNC_EXIT;
3228
3229         if (err != SE_INTERNAL_OK)
3230                 return false;
3231         else
3232                 return true;
3233 }
3234
3235 se_error_type_e cancel_sync_request()
3236 {
3237         _EXTERN_FUNC_ENTER;
3238         se_error_type_e err = SE_INTERNAL_OK;
3239
3240         cancel_connection_sync_request(TRANSPORT_TYPE);
3241
3242         _EXTERN_FUNC_EXIT;
3243
3244         return err;
3245 }