upload tizen1.0 source
[pkgs/o/oma-ds-service.git] / src / ServiceEngine / SE_Sync.c
1 /*
2  * oma-ds-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JuHak Park <juhaki.park@samsung.com>,
7  *          JuneHyuk Lee <junhyuk7.lee@samsung.com>,
8  *          SunBong Ha <sunbong.ha@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24
25
26
27 /*
28  * For any sort of issue you concern as to this software,
29  * you may use following point of contact.
30  * All resources contributed on this software
31  * are orinigally written by S-Core Inc., a member of Samsung Group.
32  *
33  * SeongWon Shim <seongwon.shim@samsung.com>
34  */
35
36 /**
37  *   @SE_Sync.c
38  *   @version                                                                   0.1
39  *   @brief                                                                             This file is the source file of implementation of functions which process sync request, auto configure etc..
40  */
41
42
43 #include <sys/time.h>
44 #include "agent-framework/DACI/DACI_Agent_Handler_Manager.h"
45 #include "agent-framework/DACI/DACI_Agent_Mgr.h"
46 #include "agent-framework/DACI/DACI_Service.h"
47 #include "agent-framework/DACI/DACI_Luid.h"
48 #include "agent-framework/DACI/DACI_ChangeLog.h"
49 #include "agent-framework/SyncAgent_Info.h"
50 #include "agent-framework/Utility/fw_log.h"
51 #include "agent-framework/EngineController/task_process.h"
52 #include "agent-framework/Utility/fw_time.h"
53 #include "agent-framework/Device/EXT_DCI_DevExecutor.h"
54 #include "ServiceEngine/SE_Sync.h"
55 #include "ServiceEngine/SE_Storage.h"
56 #include "ServiceEngine/SE_Common.h"
57 #include "ServiceEngine/SE_Notification.h"
58 #include "ServiceAdapter/SA_Common_Interface.h"
59 #include "Common/Common_Util.h"
60 #include "Common/Common_Define_Internal.h"
61
62
63 #define LOG_TAG "OMA_DS_SE"
64
65 static SE_ErrorType __session_process(int accountId, SyncProgress process, SyncError error);
66 static SE_ErrorType __process_update(int accountId, SyncProgressStatus progressStatus,
67                 OperationType operationType, int content_type, bool isFromServer, bool needToSave, SyncResult *syncResult);
68 static SE_ErrorType __write_sync_data(int accountId, AlertType alertType, SyncSessionResult syncSessionResult, int lastSessionTime);
69 static SE_ErrorType __write_sync_resource_info(int accountId,  AlertType alertType, int content_type, int lastSessionTime);
70
71 static SE_ErrorType __init_datastore_info_array(int accountId);
72 static SE_ErrorType __set_datastore_info_array(int accountId, char *config_key, CONTENT_TYPE content_type);
73 static SE_ErrorType __generate_datastore_info(int accountId,  CONTENT_TYPE content_type, Datastore **pDatastore);
74 static SE_ErrorType __init_datastore_info(int accountId,  CONTENT_TYPE content_type, Datastore **pDatastore);
75 static SE_ErrorType __set_datastore_config(int accountId, int content_type, Datastore **pDatastore);
76
77 static SE_ErrorType __on_synchronising_account(int accountId);
78 static SE_ErrorType __off_synchronising_account(int accountId);
79
80 static SE_ErrorType __assemble_changeddatastores(int accountId, SyncObj **pSyncObj);
81 static SE_ErrorType __prepare_pre_sync(int accountId, char *syncMode, SanPackage *pSanPackage);
82 static SE_ErrorType __set_config_based_on_syncMode(int accountId, char *syncMode, SanPackage *pSanPackage);
83 static SE_ErrorType __execute_pre_sync(int accountId, PreSyncReturnObj *pPreSyncReturnObj, int sessionTime);
84 static SE_ErrorType __execute_pre_sync_set_server_id(int accountId, char *devID);
85 static SE_ErrorType __execute_pre_sync_datastore(int accountId, GList *datastoreInfo, int sessionTime);
86 static SE_ErrorType __execute_sync(int accountId, SyncObj **pSyncObj, SyncReturnObj **SyncReturnObj);
87 static SE_ErrorType __execute_sync_arrange_changelog(int accountId, GList *status);
88 static SE_ErrorType __execute_sync_status(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj);
89 static SE_ErrorType __execute_sync_change(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj);
90 static SE_ErrorType __update_sync_result(int accountId);
91
92 static CommandResult __convert_return_status(DACI_RETURN da_err);
93 static char *__convert_cttype(int itemTypeId);
94 static int __convert_synctype_value(char *syncType_str);
95
96 static inline long myclock()
97 {
98         struct timeval tv;
99         gettimeofday(&tv, 0);
100         return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
101 }
102
103 static SE_ErrorType __session_process(int accountId, SyncProgress process, SyncError error)
104 {
105         FW_LOGV("start");
106
107         char *profileDirName = NULL;
108         SE_ErrorType err = SE_INTERNAL_OK;
109         bool result;
110
111         result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
112         if (result == false) {
113                 FW_LOGE("failed in get_Config");
114                 err = SE_INTERNAL_DA_ERROR;
115                 goto error;
116         }
117
118         if (profileDirName != NULL ){
119                 err = session_process(profileDirName, process, error);
120                 if (err != SE_INTERNAL_OK) {
121                         FW_LOGE("failed to send noti");
122                         goto error;
123                 }
124         }
125
126         FW_LOGV("end");
127
128 error:
129         if (profileDirName)
130                 free(profileDirName);
131
132         return err;
133 }
134
135 static SE_ErrorType __process_update(int accountId, SyncProgressStatus progressStatus,
136                 OperationType operationType, int content_type, bool isFromServer, bool needToSave, SyncResult *syncResult)
137 {
138         FW_LOGV("start with accountId =%d, progressStatus = %d, operationType = %d, content_type = %d, isFromServer = %d, needToSave = %d ",
139                                         accountId, progressStatus, operationType, content_type, isFromServer, needToSave);
140         FW_LOGV("numberOfChange = %d, received_count = %d,  syncCount = %d" ,
141                         syncResult->numberOfChange, syncResult->received_count, syncResult->add_count + syncResult->replace_count + syncResult->delete_count);
142
143         SE_ErrorType err = SE_INTERNAL_OK;
144         char *profileDirName = NULL;
145         char *profile = NULL;
146         char *progressStatus_str = NULL;
147         char *operationType_str = NULL;
148         char *uri = NULL;
149
150         bool result;
151         result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
152         if (result == false) {
153                 FW_LOGE("failed in get_Config");
154                 err = SE_INTERNAL_DA_ERROR;
155                 goto error;
156         }
157
158         /* do not need to send update noti to ui */
159         if(profileDirName == NULL){
160                 FW_LOGV("profileDirName is NULL");
161                 goto error;
162         }
163
164         if (needToSave == true) {
165                 err = write_sync_statistics(accountId, content_type, isFromServer, syncResult);
166                 if (err != SE_INTERNAL_OK) {
167                         FW_LOGE("failed in writeSyncStatistics");
168                         goto error;
169                 }
170
171                 syncResult->sessionResult = SYNC_SESSION_SUCCEEDED;
172         }
173
174         char *progress = NULL;
175         switch (progressStatus) {
176         case SYNC_PROGRESS_NONE:
177                 progress = DEFINE_PROGRESS_NONE;
178                 break;
179         case SYNC_PROGRESS_SUCCESS:
180                 progress = DEFINE_PROGRESS_SUCCESS;
181                 break;
182         case SYNC_FAILED_DB:
183                 progress = DEFINE_FAILED_DB;
184                 break;
185         case SYNC_FAILED_DB_FORBIDDEN:
186                 progress = DEFINE_FAILED_DB_FORBIDDEN;
187                 break;
188         case SYNC_FAILED_DB_ITEM:
189                 progress = DEFINE_FAILED_DB_ITEM;
190                 break;
191         case SYNC_FAILED_DB_CONFIG:
192                 progress = DEFINE_FAILED_DB_CONFIG;
193                 break;
194         case SYNC_FAILED_DB_DEVICEFULL:
195                 progress = DEFINE_FAILED_DB_DEVICEFULL;
196                 break;
197         default:
198                 break;
199         }
200
201         char *operation = NULL;
202         switch (operationType) {
203         case OPERATION_NOOP:
204                 operation = DEFINE_NOOP;
205                 break;
206         case OPERATION_ADD:
207                 operation = DEFINE_ADD;
208                 break;
209         case OPERATION_DELETE:
210                 operation = DEFINE_DELETE;
211                 break;
212         case OPERATION_MOVE:
213                 operation = DEFINE_MOVE;
214                 break;
215         case OPERATION_COPY:
216                 operation = DEFINE_COPY;
217                 break;
218         case OPERATION_REPLACE:
219                 operation = DEFINE_REPLACE;
220                 break;
221         default:
222                 break;
223         }
224
225         if (progress == NULL || operation == NULL) {
226                 err = SE_INTERNAL_NOT_DEFINED;
227                 goto error;
228         }
229
230         if (profileDirName != NULL)
231                 profile = strdup(profileDirName);
232
233         progressStatus_str = strdup(progress);
234         operationType_str = strdup(operation);
235
236         if (datastoreinfo_per_content_type[content_type]->source != NULL)
237                 uri = strdup(datastoreinfo_per_content_type[content_type]->source);
238
239         err = send_noti_process_update(profile, uri , progressStatus_str, operationType_str,
240                         isFromServer, 0, 0, syncResult->numberOfChange,
241                         syncResult->add_count + syncResult->replace_count + syncResult->delete_count);
242         if (err != SE_INTERNAL_OK) {
243                 FW_LOGE("failed in send_noti_process_update");
244                 goto error;
245         }
246
247         FW_LOGV("end");
248
249 error:
250
251         if (profileDirName != NULL)
252                 free(profileDirName);
253
254         if (profile != NULL)
255                 free(profile);
256
257         if (progressStatus_str != NULL)
258                 free(progressStatus_str);
259
260         if (operationType_str != NULL)
261                 free(operationType_str);
262
263         if (uri != NULL)
264                 free(uri);
265
266         return err;
267 }
268
269 static SE_ErrorType __write_sync_data(int accountId, AlertType alertType, SyncSessionResult syncSessionResult, int lastSessionTime)
270 {
271         FW_LOGV("start");
272
273         SE_ErrorType err = SE_INTERNAL_OK;
274
275         err = write_profile_data(accountId, alertType, syncSessionResult,  lastSessionTime);
276         if (err != SE_INTERNAL_OK) {
277                 FW_LOGE("failed in writeProfileData");
278                 goto error;
279         }
280
281         FW_LOGV("end");
282
283 error:
284
285         return err;
286 }
287
288
289 static SE_ErrorType __write_sync_resource_info(int accountId,  AlertType alertType, int content_type , int lastSessionTime)
290 {
291         FW_LOGV("start");
292
293         SE_ErrorType err = SE_INTERNAL_OK;
294
295         err = write_sync_resource_info(accountId, alertType, content_type , lastSessionTime,
296                         datastoreinfo_per_content_type[content_type]->clientSyncResult, datastoreinfo_per_content_type[content_type]->serverSyncResult);
297         if (err != SE_INTERNAL_OK) {
298                 FW_LOGE("failed in write_sync_resource_info");
299                 goto error;
300         }
301
302         FW_LOGV("end");
303
304 error:
305
306         return err;
307 }
308
309 static SE_ErrorType __init_datastore_info_array(int accountId)
310 {
311         FW_LOGV("start");
312
313         SE_ErrorType err = SE_INTERNAL_OK;
314
315         err = __set_datastore_info_array(accountId, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, TYPE_CONTACT);
316         if (err != SE_INTERNAL_OK) {
317                 FW_LOGE("failed in set_datastoreinfo_array");
318                 goto error;
319         }
320
321         err = __set_datastore_info_array(accountId, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, TYPE_CALENDAR);
322         if (err != SE_INTERNAL_OK) {
323                 FW_LOGE("failed in set_datastoreinfo_array");
324                 goto error;
325         }
326
327         err = __set_datastore_info_array(accountId, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, TYPE_MEMO);
328         if (err != SE_INTERNAL_OK) {
329                 FW_LOGE("failed in set_datastoreinfo_array");
330                 goto error;
331         }
332
333         err = __set_datastore_info_array(accountId, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, TYPE_CALLLOG);
334         if (err != SE_INTERNAL_OK) {
335                 FW_LOGE("failed in set_datastoreinfo_array");
336                 goto error;
337         }
338
339
340         FW_LOGV("end");
341
342 error:
343
344         return err;
345 }
346
347 static SE_ErrorType __set_datastore_info_array(int accountId, char *config_key, CONTENT_TYPE content_type)
348 {
349         FW_LOGV("start");
350
351         SE_ErrorType err = SE_INTERNAL_OK;
352
353         Datastore *pDatastoreInfo = NULL;
354         bool result;
355         char *value = NULL;
356
357         result = get_config(accountId, config_key, &value);
358         if (result == true) {
359                 if (strcmp(value, "1") == 0)
360                         err = __generate_datastore_info(accountId, content_type, &pDatastoreInfo);
361                 else
362                         err = __init_datastore_info(accountId, content_type, &pDatastoreInfo);
363         } else
364                 err = __init_datastore_info(accountId, content_type , &pDatastoreInfo);
365
366         if (err != SE_INTERNAL_OK) {
367                 FW_LOGE("failed to create or init datastore_info");
368                 goto error;
369         }
370
371         datastoreinfo_per_content_type[content_type] = pDatastoreInfo;
372
373         FW_LOGV("end");
374
375 error:
376
377         if (value)
378                 free(value);
379
380         return err;
381 }
382
383 static SE_ErrorType __generate_datastore_info(int accountId,  CONTENT_TYPE content_type , Datastore **pDatastore)
384 {
385         FW_LOGV("start content_type=[%d]\n",  content_type);
386
387         SE_ErrorType err = SE_INTERNAL_OK;
388         char *clientLastAnchor = NULL;
389         char *clientNextAnchor = NULL;
390         char *serverLastAnchor = NULL;
391
392         daci_last_anchor *last_anchor_info  = NULL;
393         DACI_RETURN da_err;
394
395         SyncResult *pSyncResult = NULL;
396         Datastore *datastore = NULL;
397         err = __set_datastore_config(accountId, content_type, &datastore);
398         if (err != SE_INTERNAL_OK) {
399                 FW_LOGE("failed in set_datastore_config");
400                 goto error;
401         }
402
403         /* Must get anchor per datastore for synchronization (contact, calendar, memo)
404          *              daci_last_anchor *DACI_Get_Last_Anchor_By_Account_Id(int account_id, int *count);
405          * */
406         int count = 0;
407         last_anchor_info = DACI_Get_Last_Anchor_By_Item_Type_Id(accountId, datastore->datastore_id, &count);
408
409         if (last_anchor_info != NULL) {
410                 clientLastAnchor = last_anchor_info[0].last_anchor_client;
411                 serverLastAnchor = last_anchor_info[0].last_anchor_server;
412         } else{
413                 daci_last_anchor lastAnchor_daci;
414
415                 lastAnchor_daci.account_id = accountId;
416                 lastAnchor_daci.data_store_id = datastore->datastore_id;
417                 lastAnchor_daci.last_anchor_server = NULL;
418                 lastAnchor_daci.last_anchor_client = NULL;
419                 lastAnchor_daci.access_name = "Engine";
420
421                 da_err = DACI_Add_Last_Anchor(&lastAnchor_daci);
422                 if (da_err != DACI_SUCCESS) {
423                         FW_LOGE("DACI_Add_Last_Anchor is failed %d", da_err);
424                         err = SE_INTERNAL_DA_ERROR;
425                         goto error;
426                 }
427         }
428
429         clientNextAnchor = g_strdup_printf("%ld", time(NULL));
430
431         FW_LOGV("clientLastAnchor = %s\n",  clientLastAnchor);
432         FW_LOGV("serverLastAnchor = %s\n",  serverLastAnchor);
433         FW_LOGV("clientNextAnchor = %s\n",  clientNextAnchor);
434
435         /*slow sync if managed anchor info not exist.. (first sync)*/
436         if (clientLastAnchor == NULL)
437                 set_datastore_client_anchor(datastore, clientNextAnchor, clientNextAnchor);
438         else
439                 set_datastore_client_anchor(datastore, clientLastAnchor, clientNextAnchor);
440
441         set_datastore_server_anchor(datastore, serverLastAnchor, NULL);
442
443         pSyncResult = create_sync_result();
444         if (pSyncResult == NULL) {
445                 FW_LOGE("create_syncResult is failed");
446                 err = SE_INTERNAL_NO_MEMORY;
447                 goto error;
448         }
449         datastore->clientSyncResult = pSyncResult;
450
451         pSyncResult = create_sync_result();
452         if (pSyncResult == NULL) {
453                 FW_LOGE("create_syncResult is failed");
454                 err = SE_INTERNAL_NO_MEMORY;
455                 goto error;
456         }
457         datastore->serverSyncResult  = pSyncResult;
458
459         *pDatastore = datastore;
460
461         FW_LOGV("end");
462
463 error:
464
465         if (clientNextAnchor  != NULL)
466                 free(clientNextAnchor);
467
468         if (last_anchor_info != NULL)
469                 DACI_Free_Memory_Last_Anchor(last_anchor_info, count);
470
471         if (err != SE_INTERNAL_OK) {
472                 if (datastore != NULL) {
473                         free_datastore(datastore);
474                         datastore = NULL;
475                 }
476         }
477
478         return err;
479 }
480
481 static SE_ErrorType __init_datastore_info(int accountId,  CONTENT_TYPE content_type ,  Datastore **pDatastore)
482 {
483         FW_LOGV("start");
484
485         SE_ErrorType err = SE_INTERNAL_OK;
486         char *sourceDatastore = NULL;
487         char key[128];
488         Datastore *datastore = NULL;
489         SyncResult *pSyncResult = NULL;
490
491         bool result;
492         switch (content_type) {
493                 case TYPE_CONTACT:
494                         sprintf(key, "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
495                         break;
496                 case TYPE_CALENDAR:
497                         sprintf(key, "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
498                         break;
499                 case TYPE_MEMO:
500                         sprintf(key, "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
501                         break;
502                 case TYPE_CALLLOG:
503                         sprintf(key, "%s_%s", DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
504                         break;
505                 default:
506                         FW_LOGV("unknown content type = %d", content_type);
507                         goto error;
508         }
509
510         result = get_config(accountId, key, &sourceDatastore);
511         if (result == false) {
512                 FW_LOGE("failed in get_Config");
513                 err = SE_INTERNAL_DA_ERROR;
514                 goto error;
515         }
516
517         datastore = create_datastore(NULL, sourceDatastore);
518         if (datastore == NULL) {
519                 FW_LOGE("failed to create_Datastore");
520                 err = SE_INTERNAL_NO_MEMORY;
521                 goto error;
522         }
523
524         pSyncResult = create_sync_result();
525         if (pSyncResult == NULL) {
526                 FW_LOGE("create_syncResult is failed");
527                 err = SE_INTERNAL_NO_MEMORY;
528                 goto error;
529         }
530         datastore->clientSyncResult = pSyncResult;
531
532         pSyncResult = create_sync_result();
533         if (pSyncResult == NULL) {
534                 FW_LOGE("create_syncResult is failed");
535                 err = SE_INTERNAL_NO_MEMORY;
536                 goto error;
537         }
538         datastore->serverSyncResult  = pSyncResult;
539
540         *pDatastore = datastore;
541
542         FW_LOGV("end");
543
544 error:
545         if (sourceDatastore != NULL)
546                 free(sourceDatastore);
547
548         if (err != SE_INTERNAL_OK) {
549                 if (datastore != NULL) {
550                         free_datastore(datastore);
551                         datastore = NULL;
552                 }
553         }
554
555         return err;
556 }
557
558 static SE_ErrorType __set_datastore_config(int accountId, int content_type, Datastore **pDatastore)
559 {
560         FW_LOGV("start");
561
562         Datastore *datastore = NULL;
563         SE_ErrorType err = SE_INTERNAL_OK;
564
565         char *contentType = NULL;
566
567         char sourceDatastore_key[128];
568         char targetDatastore_key[128];
569         char id_key[128];
570         char pw_key[128];
571
572         char *sourceDatastore = NULL;
573         char *targetDatastore = NULL;
574         char *sync_type = NULL;
575         char *id = NULL;
576         char *pw = NULL;
577
578         int folder_type_id = 0;
579         switch (content_type) {
580                 case TYPE_CONTACT:
581                 {
582                         folder_type_id = 0;
583                         contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CONTACTS;
584                 }
585                         break;
586                 case TYPE_CALENDAR:
587                 {
588                         folder_type_id = 0;
589                         contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALENDAR;
590                 }
591                         break;
592                 case TYPE_MEMO:
593                 {
594                         folder_type_id = 0;
595                         contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_MEMO;
596                 }
597                         break;
598                 case TYPE_CALLLOG:
599                 {
600                         folder_type_id = 0;
601                         contentType = DEFINE_CONFIG_KEY_PROFILE_CATEGORY_CALLLOG;
602                 }
603                         break;
604                 default:
605                         FW_LOGV("unknown content type = %d", content_type);
606                         err = SE_INTERNAL_ERROR;
607                         goto error;
608         }
609
610         sprintf(sourceDatastore_key, "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_SOURCE);
611         sprintf(targetDatastore_key, "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_TARGET);
612         sprintf(id_key, "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_ID);
613         sprintf(pw_key, "%s_%s", contentType, DEFINE_CONFIG_KEY_PROFILE_CATEGORY_PASSWORD);
614
615         bool result;
616         result = get_config(accountId, sourceDatastore_key, &sourceDatastore);
617         if (result == false) {
618                 FW_LOGE("failed in get_Config");
619                 err = SE_INTERNAL_DA_ERROR;
620                 goto error;
621         }
622
623         result = get_config(accountId, targetDatastore_key, &targetDatastore);
624         if (result == false) {
625                 FW_LOGE("failed in get_Config");
626                 err = SE_INTERNAL_DA_ERROR;
627                 goto error;
628         }
629
630         result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_SYNC_TYPE, &sync_type);
631         if (result == false) {
632                 FW_LOGE("failed in get_Config");
633                 err = SE_INTERNAL_DA_ERROR;
634                 goto error;
635         }
636
637         syncType = __convert_synctype_value(sync_type);
638
639         result = get_config(accountId, id_key, &id);
640         if (result == false) {
641                 FW_LOGE("failed in get_Config");
642                 err = SE_INTERNAL_DA_ERROR;
643                 goto error;
644         }
645
646         result = get_config(accountId, pw_key, &pw);
647         if (result == false) {
648                 FW_LOGE("failed in get_Config");
649                 err = SE_INTERNAL_DA_ERROR;
650                 goto error;
651         }
652
653         FW_LOGV("get_Config result  sourceDatastore= %s ",  sourceDatastore);
654         FW_LOGV("get_Config result  targetDatastore= %s ",  targetDatastore);
655         FW_LOGV("get_Config result  alertType= %s ",  sync_type);
656         FW_LOGV("get_Config result  id= %s ",  id);
657         FW_LOGV("get_Config result  pw= %s ",  pw);
658
659         datastore = create_datastore(targetDatastore, sourceDatastore);
660         if (datastore == NULL) {
661                 FW_LOGE("datastore is NULL");
662                 goto error;
663         }
664
665         set_datastore_contenttype_info(datastore, content_type, folder_type_id);
666         set_datastore_account_info(datastore, id, pw);
667         set_datastore_client_sync_type(datastore, syncType);
668
669         *pDatastore = datastore;
670
671         FW_LOGV("End");
672
673 error:
674
675         if (sourceDatastore != NULL)
676                 free(sourceDatastore);
677
678         if (targetDatastore != NULL)
679                 free(targetDatastore);
680
681         if (sync_type != NULL)
682                 free(sync_type);
683
684         if (id != NULL)
685                 free(id);
686
687         if (pw != NULL)
688                 free(pw);
689
690         if (err != SE_INTERNAL_OK) {
691                 if (datastore != NULL) {
692                         free_datastore(datastore);
693                         datastore = NULL;
694                 }
695         }
696
697         return err;
698 }
699
700 static SE_ErrorType __on_synchronising_account(int accountId)
701 {
702         FW_LOGV("start ");
703
704         SE_ErrorType err = SE_INTERNAL_OK;
705
706         bool result = set_config_str(accountId, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "1", "string", "SE");
707         if (result == false) {
708                 FW_LOGE("failed in set_Config");
709                 err = SE_INTERNAL_DA_ERROR;
710                 goto error;
711         }
712
713         FW_LOGV("end ");
714         return err;
715
716 error:
717
718         FW_LOGE("Error :%d ", err);
719         return err;
720 }
721
722 static SE_ErrorType __off_synchronising_account(int accountId)
723 {
724         FW_LOGV("start ");
725
726         SE_ErrorType err = SE_INTERNAL_OK;
727
728         bool result = set_config_str(accountId, DEFINE_CONFIG_KEY_PROFILE_SYNCHRONISING, "0", "string", "SE");
729         if (result == false) {
730                 FW_LOGE("failed in set_Config");
731                 err = SE_INTERNAL_DA_ERROR;
732                 goto error;
733         }
734
735         FW_LOGV("end ");
736         return err;
737
738 error:
739
740         FW_LOGE("Error :%d ", err);
741         return err;
742 }
743
744 static SE_ErrorType __assemble_changeddatastores(int accountId, SyncObj **pSyncObj)
745 {
746         FW_LOGV("start");
747         SE_ErrorType err = SE_INTERNAL_OK;
748         ChangedDatastore *pChangedDatastore = NULL;
749         DACI_RETURN da_err = DACI_SUCCESS;
750
751         int content_type;
752         for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++)       {
753                 int count = 0;
754                 if (!datastoreinfo_per_content_type[content_type]->clientSyncType) continue;
755
756                 if (datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_SLOW_SYNC
757                                 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_CLIENT
758                                 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_CLIENT_BY_SERVER) {
759
760                         if( content_type == TYPE_CALLLOG ){
761                                 FW_LOGV("refresh_Item_Tbl_From_Service for call log");
762                                 refresh_Item_Tbl_From_Service(accountId, datastoreinfo_per_content_type[content_type]->datastore_id);
763                         }
764
765                         /*all item using accountId*/
766                         daci_item *pDaci_item = DACI_Get_item_By_Account_Id(accountId, datastoreinfo_per_content_type[content_type]->datastore_id, &count);
767
768                         pChangedDatastore = create_changeddatastore(datastoreinfo_per_content_type[content_type]->source,
769                                                                                                                                                 datastoreinfo_per_content_type[content_type]->target, 1 , count);
770                         if (pChangedDatastore == NULL) {
771                                 DACI_Free_Memory_Item(pDaci_item, count);
772                                 FW_LOGE("failed in create_ChangedDatastore");
773                                 err = SE_INTERNAL_NO_MEMORY;
774                                 goto error;
775                         }
776
777                         if (count > 0) {
778                                 int i;
779                                 for (i = 0; i < count; i++) {
780                                         char *cttype = __convert_cttype(pDaci_item[i].data_store_id);
781                                         ChangedItem *pChanged = create_changeditem(CHANGE_ADD, pDaci_item[i].item_id);
782                                         if (pChanged == NULL) {
783
784                                                 DACI_Free_Memory_Item(pDaci_item, count);
785                                                 FW_LOGE("failed in create_ChangedDatastore");
786                                                 err = SE_INTERNAL_NO_MEMORY;
787                                                 goto error;
788                                         }
789
790                                         set_changeditem_contenttype(pChanged, cttype);
791                                         set_changedItem_indexofdatastore(pChanged, content_type);
792
793                                         add_changeddatastore_changeditem(pChangedDatastore, pChanged);
794                                 }
795
796                                 DACI_Free_Memory_Item(pDaci_item, count);
797                         }
798                 } else if (datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_TWO_WAY
799                                 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_TWO_WAY_BY_SERVER
800                                 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_ONE_WAY_FROM_CLIENT
801                                 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
802                         FW_LOGV("server sync type is ALERT_TYPE [%d]",   datastoreinfo_per_content_type[content_type]->serverSyncType);
803
804                         /*from changelog*/
805                         daci_item_info *pDaci_item_info = DACI_Get_Item_Info_By_Item_Type_Id(accountId,
806                                                                                                                 datastoreinfo_per_content_type[content_type]->datastore_id, &count);
807
808                         pChangedDatastore = create_changeddatastore(datastoreinfo_per_content_type[content_type]->source,
809                                                                                                                 datastoreinfo_per_content_type[content_type]->target, 1, count);
810                         if (pChangedDatastore == NULL) {
811                                 DACI_Free_Memory_Item_Info(pDaci_item_info, count);
812                                 FW_LOGE("failed in create_ChangedDatastore");
813                                 err = SE_INTERNAL_NO_MEMORY;
814                                 goto error;
815                         }
816
817                         /*gathering changelog info*/
818                         if (count > 0) {
819                                 /*begin agent db transaction*/
820                                 DACI_Begin_Transaction();
821
822                                 char *cttype = __convert_cttype(datastoreinfo_per_content_type[content_type]->datastore_id);
823
824                                 int i;
825                                 for (i = 0; i < count; i++) {
826                                         ChangedItem *pChanged = create_changeditem(pDaci_item_info[i].operationId - 300, pDaci_item_info[i].itemId);
827                                         if (pChanged == NULL) {
828                                                 DACI_Free_Memory_Item_Info(pDaci_item_info, count);
829                                                 DACI_End_Transaction(TRANSACTION_ROLLBACK);
830                                                 FW_LOGE("failed in create_ChangedDatastore");
831                                                 err = SE_INTERNAL_NO_MEMORY;
832                                                 goto error;
833                                         }
834
835                                         set_changeditem_contenttype(pChanged, cttype);
836                                         set_changedItem_indexofdatastore(pChanged, content_type);
837                                         add_changeddatastore_changeditem(pChangedDatastore, pChanged);
838                                 }
839
840                                 int *itemTypeIdList = (int *)calloc(1, sizeof(int));
841                                 itemTypeIdList[0] = datastoreinfo_per_content_type[content_type]->datastore_id;
842
843                                 da_err = DACI_Set_Item_Changelog_Wait_Status(accountId, NULL, 0, itemTypeIdList, 1);
844                                 DACI_Free_Memory_Item_Info(pDaci_item_info, count);
845                                 if (itemTypeIdList != NULL)
846                                         free(itemTypeIdList);
847                                 if (da_err != DACI_SUCCESS) {
848                                         DACI_End_Transaction(TRANSACTION_ROLLBACK);
849                                         FW_LOGE("failed in DACI_Set_Item_Changelog_Wait_Status");
850                                         err = SE_INTERNAL_DA_ERROR;
851                                         goto error;
852                                 }
853
854                                 DACI_End_Transaction(TRANSACTION_COMMIT);
855                         }
856                 } else if (datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_ONE_WAY_FROM_SERVER
857                                 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_ONE_WAY_FROM_SERVER_BY_SERVER
858                                 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_SERVER
859                                 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
860                         /*MUST create empty changedDatastore..*/
861                         pChangedDatastore = create_changeddatastore(datastoreinfo_per_content_type[content_type]->source,
862                                                                                                                                                         datastoreinfo_per_content_type[content_type]->target, 1, 0);
863                         if (pChangedDatastore == NULL) {
864                                 FW_LOGE("pChangedDatastore is NULL");
865                                 err = SE_INTERNAL_NO_MEMORY;
866                                 goto error;
867                         }
868
869                         if (datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_SERVER
870                                 || datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
871
872                                 /*FW_LOGV("refresh_Item_Tbl_From_Service");
873                                 refresh_Item_Tbl_From_Service(accountId, datastoreinfo_per_content_type[content_type]->plugin_type);*/
874
875                                 /*  Delete All item (include changelog), before adapting server item data…*/
876                                 FW_LOGV("DACI_Service_Begin = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
877                                 DACI_Service_Begin(datastoreinfo_per_content_type[content_type]->datastore_id);
878                                 DACI_Begin_Transaction();
879
880                                 /*delete service item data*/
881                                 da_err = DACI_Service_Delete_All_Items(datastoreinfo_per_content_type[content_type]->datastore_id, accountId);
882                                 if (da_err != DACI_SUCCESS) {
883
884                                         DACI_Service_End(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
885                                         DACI_End_Transaction(TRANSACTION_ROLLBACK);
886
887                                         FW_LOGE("failed in DACI_Service_Delete_All_Items");
888                                         err = SE_INTERNAL_DA_ERROR;
889                                         goto error;
890                                 }
891
892                                 int item_cnt;
893                                 FW_LOGV("DACI_Get_item_By_Account_Id");
894                                 /*get all item by account id*/
895                                 daci_item *pDaci_item = DACI_Get_item_By_Account_Id(accountId, datastoreinfo_per_content_type[content_type]->datastore_id, &item_cnt);
896                                 FW_LOGV("item_cnt = %d", item_cnt);
897
898                                 int j;
899                                 int acc_cnt;
900                                 for (j = 0; j < item_cnt ; j++) {
901                                         daci_item *pDaci_item_from_other_account = DACI_Get_item_Mapping_Service_Id(accountId, pDaci_item[j].item_id, &acc_cnt);
902                                         FW_LOGV("acc_cnt = %d", acc_cnt);
903
904                                         int i;
905                                         for (i = 0; i < acc_cnt; i++)
906                                                 ChangeLog_Delete_Item(pDaci_item_from_other_account[i].account_id, pDaci_item_from_other_account[i].item_id,
907                                                                                                                                                                                                                         datastoreinfo_per_content_type[content_type]->datastore_id);
908
909                                         DACI_Free_Memory_Item(pDaci_item_from_other_account, acc_cnt);
910                                 }
911
912                                 /*delete item from item_tbl*/
913                                 FW_LOGV("DACI_Delete_Item_By_Item_Type_Id = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
914                                 da_err = DACI_Delete_Item_By_Item_Type_Id(accountId, datastoreinfo_per_content_type[content_type]->datastore_id);
915                                 if (da_err != DACI_SUCCESS) {
916
917                                         DACI_Free_Memory_Item(pDaci_item, item_cnt);
918
919                                         DACI_Service_End(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
920                                         DACI_End_Transaction(TRANSACTION_ROLLBACK);
921
922                                         FW_LOGE("failed in DACI_Delete_Item_By_Item_Type_Id");
923                                         err = SE_INTERNAL_DA_ERROR;
924                                         goto error;
925                                 }
926
927                                 /*delete changelog data bu account id*/
928                                 FW_LOGV("DACI_Delete_Item_Changelog_By_Item_Type_Id = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
929                                 da_err = DACI_Delete_Item_Changelog_By_Item_Type_Id(accountId, datastoreinfo_per_content_type[content_type]->datastore_id);
930                                 if (da_err != DACI_SUCCESS) {
931
932                                         DACI_Free_Memory_Item(pDaci_item, item_cnt);
933
934                                         DACI_Service_End(datastoreinfo_per_content_type[content_type]->datastore_id, 0);
935                                         DACI_End_Transaction(TRANSACTION_ROLLBACK);
936
937                                         FW_LOGE("failed in DACI_Delete_Item_Changelog_By_Item_Type_Id");
938                                         err = SE_INTERNAL_DA_ERROR;
939                                         goto error;
940                                 }
941
942                                 FW_LOGV("DACI_Service_End = %d", datastoreinfo_per_content_type[content_type]->datastore_id);
943                                 DACI_Service_End(datastoreinfo_per_content_type[content_type]->datastore_id, 1);
944                                 DACI_End_Transaction(TRANSACTION_COMMIT);
945
946                                 DACI_Free_Memory_Item(pDaci_item, item_cnt);
947                         }
948                 }
949                 if (datastoreinfo_per_content_type[content_type]->clientSyncType) {
950                         (*pSyncObj)->changedDatastore =  g_list_append((*pSyncObj)->changedDatastore, pChangedDatastore);
951
952                         set_numberofchange(datastoreinfo_per_content_type[content_type]->clientSyncResult, pChangedDatastore->numberOfChanges);
953
954                         OperationType operationType;
955                         if (datastoreinfo_per_content_type[content_type]->clientSyncResult->numberOfChange == 0)
956                                 operationType = OPERATION_NOOP;
957                         else
958                                 operationType = OPERATION_ADD;
959                         err = __process_update(accountId,  SYNC_PROGRESS_SUCCESS, operationType, content_type, false, false,
960                                                                                                                                                                 datastoreinfo_per_content_type[content_type]->clientSyncResult);
961                         if (err != SE_INTERNAL_OK) {
962                                 FW_LOGE("Failed in process_update");
963                                 goto error;
964                         }
965                 }
966         }
967
968         FW_LOGV("end");
969
970         return err;
971
972 error:
973
974         if (pChangedDatastore != NULL) {
975                 free_changeddatastore(pChangedDatastore);
976                 pChangedDatastore = NULL;
977         }
978
979         return err;
980 }
981
982 static SE_ErrorType __prepare_pre_sync(int accountId, char *syncMode, SanPackage *pSanPackage)
983 {
984         FW_LOGV("start");
985         SE_ErrorType err = SE_INTERNAL_OK;
986
987         /*set synchronising flag into agent DB*/
988         err =  __on_synchronising_account(accountId);
989         if (err != SE_INTERNAL_OK) {
990                 FW_LOGE("failed in set_synchronising_account");
991                 goto error;
992         }
993
994         /*init datastore_info_array*/
995         err = __init_datastore_info_array(accountId);
996         if (err != SE_INTERNAL_OK) {
997                 FW_LOGE("failed in init_datastoreInfo_array");
998                 goto error;
999         }
1000
1001         /*set config based on sync mode*/
1002         err = __set_config_based_on_syncMode(accountId, syncMode, pSanPackage);
1003         if (err != SE_INTERNAL_OK) {
1004                 FW_LOGE("failed in set_synchronising_account");
1005                 goto error;
1006         }
1007
1008         FW_LOGV("end");
1009         return err;
1010
1011 error:
1012         FW_LOGE("err = %d", err);
1013         return err;
1014 }
1015
1016 static SE_ErrorType __set_config_based_on_syncMode(int accountId, char *syncMode, SanPackage *pSanPackage)
1017 {
1018         FW_LOGV("start");
1019         SE_ErrorType err = SE_INTERNAL_OK;
1020         int content_type;
1021         bool result;
1022
1023         if (strcmp(syncMode, DEFINE_SYNC_MODE_MANUAL) != 0) {
1024                 char *profileDirName = NULL;
1025
1026                 result = get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
1027                 if (result == false) {
1028                         FW_LOGE("failed in get_Config");
1029                         err = SE_INTERNAL_DA_ERROR;
1030                         goto error;
1031                 }
1032
1033                 if (profileDirName != NULL) {
1034                         err = send_noti_sync_status(profileDirName);
1035                         if (profileDirName != NULL)
1036                                 free(profileDirName);
1037
1038                         if (err != SE_INTERNAL_OK) {
1039                                 FW_LOGE("failed in send_noti_sync_status");
1040                                 goto error;
1041                         }
1042                 }
1043         }
1044
1045         if (strcmp(syncMode, DEFINE_SYNC_MODE_PUSH) == 0) {
1046                 if (pSanPackage != NULL) {
1047                         int count = pSanPackage->cntSyncAlerts;
1048                         FW_LOGV("count = %d", count);
1049                         int i;
1050                         for (i = 0 ; i < count ; i++) {
1051                                 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1052
1053                                         if (datastoreinfo_per_content_type[content_type]->target == NULL) continue;
1054
1055                                         if (strcmp(pSanPackage->syncAlerts[i].serverURI, datastoreinfo_per_content_type[content_type]->target) == 0) {
1056                                                 datastoreinfo_per_content_type[content_type]->clientSyncType = pSanPackage->syncAlerts[i].syncType;
1057                                                 FW_LOGV("pSanPackage->syncAlerts[i].syncType = %d", pSanPackage->syncAlerts[i].syncType);
1058                                         } else
1059                                                 datastoreinfo_per_content_type[content_type]->clientSyncType = ALERT_UNKNOWN;
1060
1061                                 }
1062                         }
1063                 }
1064         }
1065
1066         FW_LOGV("end");
1067         return err;
1068
1069 error:
1070         FW_LOGE("err = %d", err);
1071         return err;
1072 }
1073
1074
1075 static SE_ErrorType  __execute_pre_sync(int accountId, PreSyncReturnObj *pPreSyncReturnObj, int sessionTime)
1076 {
1077         FW_LOGV("start");
1078         SE_ErrorType err = SE_INTERNAL_OK;
1079
1080         /*if there is no alert command from server check it and goto fail_part*/
1081         if (g_list_length(pPreSyncReturnObj->datastoreInfo) == 0) {
1082                 FW_LOGV("alert command list from server is empty");
1083                 err = SE_INTERNAL_MISCONFIGURATION;
1084                 goto error;
1085         }
1086
1087         err = __execute_pre_sync_set_server_id(accountId, pPreSyncReturnObj->devID);
1088         if (err != SE_INTERNAL_OK) {
1089                 FW_LOGE("failed in __set_server_id");
1090                 goto error;
1091         }
1092
1093         err = __execute_pre_sync_datastore(accountId, pPreSyncReturnObj->datastoreInfo, sessionTime);
1094         if (err != SE_INTERNAL_OK) {
1095                 FW_LOGE("failed in __process_datastore");
1096                 goto error;
1097         }
1098
1099         FW_LOGV("end");
1100         return err;
1101
1102 error:
1103         FW_LOGE("err = %d", err);
1104         return err;
1105
1106 }
1107
1108 /*FIXME : set Server_id for SAN*/
1109 static SE_ErrorType __execute_pre_sync_set_server_id(int accountId, char *devID)
1110 {
1111         FW_LOGV("start");
1112         SE_ErrorType err = SE_INTERNAL_OK;
1113         bool result;
1114         char *value = NULL;
1115         result =  get_config(accountId, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &value);
1116         if (result == true) {
1117                 if (value != NULL) {
1118                         if (strcmp(value, devID) == 0) {
1119                                 /* OK*/
1120                         } else{
1121                                 result = set_config_str(accountId, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, devID, "string", "SE");
1122                                 if (result == false) {
1123                                         FW_LOGE("failed in set_Config");
1124                                         err = SE_INTERNAL_DA_ERROR;
1125                                         goto error;
1126                                 }
1127                         }
1128                 } else{
1129                         result = set_config_str(accountId, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, devID, "string", "SE");
1130                         if (result == false) {
1131                                 FW_LOGE("failed in set_Config");
1132                                 err = SE_INTERNAL_DA_ERROR;
1133                                 goto error;
1134                         }
1135                 }
1136         } else{
1137                 FW_LOGE("failed in get_Config");
1138                 err = SE_INTERNAL_DA_ERROR;
1139                 goto error;
1140         }
1141
1142 error:
1143
1144         if (value != NULL) {
1145                 free(value);
1146                 value = NULL;
1147         }
1148         FW_LOGV("end");
1149
1150         return err;
1151 }
1152
1153 static SE_ErrorType __execute_pre_sync_datastore(int accountId, GList *datastoreInfo, int sessionTime)
1154 {
1155         FW_LOGV("start");
1156         SE_ErrorType err = SE_INTERNAL_OK;
1157
1158         int content_type;
1159         GList *serverDatastore_iter = NULL;
1160         DatastoreInfo *serverDatastoreInfo = NULL;
1161         bool existInServerDatastore;
1162         for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1163                 existInServerDatastore =  false;
1164
1165                 if (datastoreinfo_per_content_type[content_type]->clientSyncType) {
1166
1167                         for (serverDatastore_iter = datastoreInfo; serverDatastore_iter != NULL;  serverDatastore_iter = g_list_next(serverDatastore_iter)) {
1168                                         serverDatastoreInfo = serverDatastore_iter->data;
1169
1170                                 if (strcmp(serverDatastoreInfo->source, datastoreinfo_per_content_type[content_type]->target) == 0) {
1171                                         existInServerDatastore = true;
1172
1173                                         datastoreinfo_per_content_type[content_type]->serverSyncType = serverDatastoreInfo->syncType;
1174                                         datastoreinfo_per_content_type[content_type]->nextAnchorServer = strdup(serverDatastoreInfo->nextAnchor);
1175
1176                                         if (datastoreinfo_per_content_type[content_type]->lastAnchorServer) {
1177                                                 /*if (strcmp(datastoreinfo_per_content_type[content_type]->lastAnchorServer, serverDatastoreInfo->lastAnchor) != 0) {
1178                                                         free(datastoreinfo_per_content_type[content_type]->lastAnchorServer);
1179                                                         datastoreinfo_per_content_type[content_type]->lastAnchorServer = NULL;
1180                                                         datastoreinfo_per_content_type[content_type]->serverSyncType == ALERT_SLOW_SYNC;
1181                                                 }*/
1182                                         } else{
1183                                                 /* When first synchronize, lastAnchorServer is NULL...
1184                                                 if (datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_SLOW_SYNC
1185                                                                         && datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_REFRESH_FROM_CLIENT
1186                                                                         && datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_REFRESH_FROM_CLIENT_BY_SERVER
1187                                                                         && datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_REFRESH_FROM_SERVER
1188                                                                         && datastoreinfo_per_content_type[content_type]->serverSyncType != ALERT_REFRESH_FROM_SERVER_BY_SERVER) {
1189                                                         FW_LOGV("refresh_Item_Tbl_From_Service");
1190                                                         refresh_Item_Tbl_From_Service(accountId, datastoreinfo_per_content_type[content_type]->plugin_type);
1191                                                 }*/
1192                                         }
1193                                         break;
1194                                 }
1195                         }
1196
1197                         if (!existInServerDatastore) {
1198                                 /* datastore config is wrong this datastore is not going to contain sync process from now*/
1199                                 err = __process_update(accountId,  SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, false, true,
1200                                                                                                                 datastoreinfo_per_content_type[content_type]->clientSyncResult);
1201                                 if (err != SE_INTERNAL_OK) {
1202                                         FW_LOGE("failed to process_update");
1203                                         goto error;
1204                                 }
1205
1206                                 err = __process_update(accountId,  SYNC_FAILED_DB_CONFIG, OPERATION_NOOP, content_type, true, true,
1207                                                                                                                 datastoreinfo_per_content_type[content_type]->serverSyncResult);
1208                                 if (err != SE_INTERNAL_OK) {
1209                                         FW_LOGE("failed to process_update");
1210                                         goto error;
1211                                 }
1212
1213                                 datastoreinfo_per_content_type[content_type]->clientSyncResult->sessionResult = SYNC_SESSION_FAILED;
1214                                 err = __write_sync_resource_info(accountId, syncType, content_type , sessionTime);
1215                                 if (err != SE_INTERNAL_OK) {
1216                                         FW_LOGE("failed to write_SyncResourceInfo");
1217                                         goto error;
1218                                 }
1219
1220                                 free_datastore(datastoreinfo_per_content_type[content_type]);
1221                                 datastoreinfo_per_content_type[content_type] = NULL;
1222
1223                                 Datastore *pDatastore = NULL;
1224                                 err = __init_datastore_info(accountId, content_type, &pDatastore);
1225                                 if (err != SE_INTERNAL_OK) {
1226                                         FW_LOGE("failed to init_datastore_info");
1227                                         goto error;
1228                                 }
1229
1230                                 datastoreinfo_per_content_type[content_type] = pDatastore;
1231                         }
1232                 }
1233         }
1234
1235 error:
1236         FW_LOGV("end");
1237
1238         return err;
1239 }
1240
1241 static SE_ErrorType __execute_sync(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj)
1242 {
1243         FW_LOGV("start");
1244         SE_ErrorType err = SE_INTERNAL_OK;
1245
1246         if (syncType == ALERT_TWO_WAY || syncType == ALERT_TWO_WAY_BY_SERVER ||
1247                         syncType == ALERT_ONE_WAY_FROM_CLIENT || syncType == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
1248
1249                 err = __execute_sync_arrange_changelog(accountId , (*pSyncReturnObj)->status);
1250                 if (err != SE_INTERNAL_OK) {
1251                         FW_LOGE("failed to process_update");
1252                         goto error;
1253                 }
1254         }
1255
1256         err = __execute_sync_status(accountId, pSyncObj, pSyncReturnObj);
1257         if (err != SE_INTERNAL_OK) {
1258                 FW_LOGE("failed in __execute_sync_status");
1259                 goto error;
1260         }
1261
1262         err = __execute_sync_change(accountId, pSyncObj, pSyncReturnObj);
1263         if (err != SE_INTERNAL_OK) {
1264                 FW_LOGE("failed in __execute_sync_status");
1265                 goto error;
1266         }
1267
1268         FW_LOGV("end");
1269         return err;
1270
1271 error:
1272         FW_LOGE("err = %d", err);
1273         return err;
1274 }
1275
1276 static SE_ErrorType __execute_sync_arrange_changelog(int accountId, GList *status)
1277 {
1278         FW_LOGV("start");
1279         SE_ErrorType err = SE_INTERNAL_OK;
1280         DACI_RETURN da_err;
1281
1282         int i = 0;
1283         int count = 0;
1284         int list_length = g_list_length(status);
1285         char **itemIdList = (char **)calloc(list_length, sizeof(char *));
1286         if (itemIdList == NULL) {
1287                 FW_LOGE("Failed to alloc memory");
1288                 err = SE_INTERNAL_NO_MEMORY;
1289                 goto error;
1290         }
1291
1292         GList *appliedStatus_iter = NULL;
1293         AppliedStatus *pAppliedStatus = NULL;
1294         for (appliedStatus_iter = status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) {
1295                 pAppliedStatus = appliedStatus_iter->data;
1296
1297                 switch (pAppliedStatus->status) {
1298                         case 200:
1299                         case 201:
1300                         case 202:
1301                         case 203:
1302                         case 204:
1303                         case 205:
1304                         case 207:
1305                         case 208:
1306                         case 209:
1307                         case 210:
1308                         case 211:
1309                         {
1310                                 /*clean up change log*/
1311                                 itemIdList[count++] = strdup(pAppliedStatus->luid);
1312
1313                                 if (syncType == ALERT_TWO_WAY || syncType == ALERT_TWO_WAY_BY_SERVER ||
1314                                                 syncType == ALERT_ONE_WAY_FROM_CLIENT || syncType == ALERT_ONE_WAY_FROM_CLIENT_BY_SERVER) {
1315
1316                                         if (pAppliedStatus->changeType == CHANGE_DELETE) {
1317                                                 da_err = DACI_Delete_Item_By_Item_Id(pAppliedStatus->luid);
1318                                                 if (da_err != DACI_SUCCESS) {
1319                                                         FW_LOGE("failed in DACI_Delete_Item_By_Item_Id");
1320                                                         err = SE_INTERNAL_DA_ERROR;
1321                                                         goto error;
1322                                                 }
1323                                         }
1324                                 }
1325                                 break;
1326                         }
1327                         case 206:
1328                         case 420:
1329                                 break;
1330                         case 400:
1331                         case 500:
1332                         {
1333                                 daci_item_changelog get_changelog;
1334                                 get_changelog.item_id = pAppliedStatus->luid;
1335
1336                                 da_err = DACI_Get_Item_Changelog_By_Item_Id(accountId, &get_changelog);
1337
1338                                 if (da_err == DACI_ERR_NO_DATA) {
1339                                         FW_LOGV("%s item does not exist in ChangeLog tbl", pAppliedStatus->luid);
1340                                         break;
1341                                 } else if (da_err != DACI_SUCCESS) {
1342
1343                                         FW_LOGE("failed in DACI_Get_Item_Changelog_By_Item_Id");
1344                                         err = SE_INTERNAL_DA_ERROR;
1345                                         goto error;
1346                                 }
1347
1348                                 if (get_changelog.status != NULL) {
1349                                         if (strcmp(get_changelog.status, "SYNC_ERROR") == 0)
1350                                                 itemIdList[count++] = strdup(pAppliedStatus->luid);
1351                                         else{
1352                                                 daci_item_changelog set_changelog;
1353
1354                                                 set_changelog.item_id = pAppliedStatus->luid;
1355                                                 set_changelog.status = "SYNC_ERROR";
1356                                                 set_changelog.access_name = "SE";
1357
1358
1359                                                 da_err =  DACI_Set_Item_Changelog_Sync_Status(accountId, &set_changelog);
1360
1361                                                 if (get_changelog.status != NULL)
1362                                                         free(get_changelog.status);
1363
1364                                                 if (get_changelog.access_name != NULL)
1365                                                         free(get_changelog.access_name);
1366                                                 if (da_err != DACI_SUCCESS) {
1367                                                         FW_LOGE("failed in DACI_Delete_Item_By_Item_Id");
1368                                                         err = SE_INTERNAL_DA_ERROR;
1369                                                         goto error;
1370                                                 }
1371                                         }
1372                                 }
1373                                 break;
1374                         }
1375                 }
1376         }
1377
1378         if (list_length >= 1) {
1379                 FW_LOGV("before DACI_Delete_Item_Changelog_By_Item_Id_List");
1380                 FW_LOGV("count = %d",  count);
1381                 da_err = DACI_Delete_Item_Changelog_By_Item_Id_List(accountId, itemIdList, count);
1382                 if (da_err != DACI_SUCCESS) {
1383                         FW_LOGE("failed in DACI_Delete_Item_Changelog_By_Item_Id_List");
1384                         err = SE_INTERNAL_DA_ERROR;
1385                         goto error;
1386                 }
1387         }
1388
1389         FW_LOGV("end");
1390
1391 error:
1392
1393         /*free itemIdList*/
1394         if (itemIdList != NULL) {
1395                 for (i = 0; i < list_length; i++) {
1396                         if (itemIdList[i])
1397                                 free(itemIdList[i]);
1398                 }
1399                 free(itemIdList);
1400         }
1401
1402         return err;
1403 }
1404
1405 static SE_ErrorType __execute_sync_status(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj)
1406 {
1407         FW_LOGV("start");
1408         SE_ErrorType err = SE_INTERNAL_OK;
1409
1410         int content_type;
1411         bool existEqualItem;
1412         bool needToSave;
1413         SyncResult *pClientSyncResult;
1414         GList *changedDatastore_iter = NULL;
1415         ChangedDatastore *pSyncChangedDatastore = NULL;
1416         for (changedDatastore_iter = (*pSyncObj)->changedDatastore; changedDatastore_iter != NULL;) {
1417                 pSyncChangedDatastore = (ChangedDatastore *)changedDatastore_iter->data;
1418                 FW_LOGV("pChangedDatastore = %p", pSyncChangedDatastore);
1419
1420                 int datastoreContentType = 0;
1421                 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1422                         if (strcmp(pSyncChangedDatastore->source, datastoreinfo_per_content_type[content_type]->source) == 0) {
1423                                 datastoreContentType = content_type;
1424                                 break;
1425                         }
1426                 }
1427
1428                 GList *changedItem_iter = NULL;
1429                 ChangedItem *changedItem = NULL;
1430                 for (changedItem_iter = pSyncChangedDatastore->sentItem; changedItem_iter != NULL;) {
1431                         changedItem = changedItem_iter->data;
1432
1433                         existEqualItem = false;
1434                         GList *appliedStatus_iter = NULL;
1435                         AppliedStatus *pAppliedStatus = NULL;
1436                         for (appliedStatus_iter = (*pSyncReturnObj)->status; appliedStatus_iter != NULL; appliedStatus_iter = g_list_next(appliedStatus_iter)) {
1437                                 pAppliedStatus = appliedStatus_iter->data;
1438
1439                                 if (strcmp(pAppliedStatus->luid, changedItem->luid) == 0) {
1440                                         add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult, 1);
1441
1442                                         if (pAppliedStatus->status >= 200  && pAppliedStatus->status <= 211) {
1443                                                 switch (changedItem->changeType) {
1444                                                 case CHANGE_ADD:
1445                                                         add_add_count(datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult, 1);
1446                                                         break;
1447                                                 case CHANGE_REPLACE:
1448                                                         add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult, 1);
1449                                                         break;
1450                                                 case CHANGE_DELETE:
1451                                                         add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult, 1);
1452                                                         break;
1453                                                 default:
1454                                                         break;
1455                                                 }
1456                                         } else{
1457                                                 /* if status is not success count it is failed*/
1458                                         }
1459
1460                                         (*pSyncReturnObj)->status = g_list_remove((*pSyncReturnObj)->status, pAppliedStatus);
1461                                         free_appliedstatus(pAppliedStatus);
1462                                         pAppliedStatus = NULL;
1463
1464                                         changedItem_iter = g_list_next(changedItem_iter);
1465
1466                                         pSyncChangedDatastore->sentItem = g_list_remove(pSyncChangedDatastore->sentItem, changedItem);
1467                                         free_changeditem(changedItem);
1468                                         changedItem = NULL;
1469
1470                                         existEqualItem = true;
1471                                         break;
1472                                 }
1473                         }
1474                         if (existEqualItem == false)
1475                                 changedItem_iter = g_list_next(changedItem_iter);
1476                 }
1477
1478                 changedDatastore_iter =  g_list_next(changedDatastore_iter);
1479
1480                 needToSave = false;
1481                 pClientSyncResult = datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult;
1482                 if (pClientSyncResult->numberOfChange == pClientSyncResult->received_count) {
1483                         needToSave = true;
1484                         /*remove datastore from list and
1485                         free current ChangedDatastore it does not need anymore because all item in datastore has been sent and receive status*/
1486                          (*pSyncObj)->changedDatastore = g_list_remove((*pSyncObj)->changedDatastore, pSyncChangedDatastore);
1487                         free_changeddatastore(pSyncChangedDatastore);
1488                         pSyncChangedDatastore = NULL;
1489                 }
1490
1491                 OperationType operationType;
1492                 if (datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult->numberOfChange == 0)
1493                         operationType = OPERATION_NOOP;
1494                 else
1495                         operationType = OPERATION_ADD;
1496                 err = __process_update(accountId, SYNC_PROGRESS_SUCCESS, operationType, datastoreContentType, false,  needToSave,
1497                                                                                         datastoreinfo_per_content_type[datastoreContentType]->clientSyncResult);
1498                 if (err != SE_INTERNAL_OK) {
1499                         FW_LOGE("failed in process_update");
1500                         goto error;
1501                 }
1502         }
1503
1504         FW_LOGV("end");
1505         return err;
1506
1507 error:
1508         FW_LOGE("err = %d", err);
1509         return err;
1510 }
1511
1512 static SE_ErrorType __execute_sync_change(int accountId, SyncObj **pSyncObj, SyncReturnObj **pSyncReturnObj)
1513 {
1514         FW_LOGV("start");
1515         SE_ErrorType err = SE_INTERNAL_OK;
1516         DACI_RETURN da_err = DACI_SUCCESS;
1517         int content_type;
1518
1519         /* process command that from server sended to client(sync, add, replace etc...)*/
1520         ChangedDatastore *pSyncReturnChangedDatastore = NULL;
1521         SendingStatus *sendingStatus = NULL;
1522         GList *iter = NULL;
1523         for (iter = (*pSyncReturnObj)->changedDatastore; iter != NULL;) {
1524                 pSyncReturnChangedDatastore = (ChangedDatastore *)iter->data;
1525
1526                 int datastoreContentType = 0;
1527                 for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1528                         if (strcmp(pSyncReturnChangedDatastore->target, datastoreinfo_per_content_type[content_type]->source) == 0) {
1529                                 datastoreContentType = content_type;
1530                                 break;
1531                         }
1532                 }
1533
1534                 /*noti to UI numberOfChanges from server*/
1535                 if (pSyncReturnChangedDatastore->hasNumberOfChanges) {
1536                         set_numberofchange(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, pSyncReturnChangedDatastore->numberOfChanges);
1537
1538                         OperationType operationType;
1539                         if (datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult->numberOfChange == 0)
1540                                 operationType = OPERATION_NOOP;
1541                         else
1542                                 operationType = OPERATION_ADD;
1543                         err = __process_update(accountId,  SYNC_PROGRESS_SUCCESS, operationType, datastoreContentType, true, false,
1544                                                                                                 datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult);
1545                         if (err != SE_INTERNAL_OK) {
1546                                 FW_LOGE("failed in process_update");
1547                                 goto error;
1548                         }
1549                 }
1550
1551                 sendingStatus = create_sendingstatus(pSyncReturnChangedDatastore->target, pSyncReturnChangedDatastore->source);
1552                 if (sendingStatus == NULL) {
1553                         err = SE_INTERNAL_NO_MEMORY;
1554                         FW_LOGE("failed to alloc memory");
1555                         goto error;
1556                 }
1557
1558                 SyncResult *tempServerSyncResult = NULL;
1559                 int changeItemCount = g_list_length(pSyncReturnChangedDatastore->changeItem);
1560                 if (changeItemCount > 0) {
1561                         /*begin transaction*/
1562                         FW_LOGV("DACI_Service_Begin with datastoreContentType = %d", datastoreContentType);
1563                         DACI_Service_Begin(datastoreinfo_per_content_type[datastoreContentType]->datastore_id);
1564                         DACI_Begin_Transaction();
1565
1566                         /*back up syncresult structure for rollback case*/
1567                         tempServerSyncResult = dup_syncresult(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult);
1568                 }
1569
1570                 /*get folderId*/
1571                 char *folderId = NULL;
1572                 daci_id_list *folder_id_list = DACI_Get_Folder_Folder_Id_By_Folder_Type_Id(accountId,
1573                                                                                                                 datastoreinfo_per_content_type[datastoreContentType]->datastore_id,
1574                                                                                                                 datastoreinfo_per_content_type[datastoreContentType]->folder_type_id);
1575                 if (folder_id_list != NULL) {
1576                         folderId = folder_id_list->id[0];
1577                 }
1578
1579                 bool needToSave = false;
1580                 SyncResult *pServerSyncResult = NULL;
1581                 GList *changedIter = NULL;
1582                 ChangedItem *changedItem = NULL;
1583                 AppliedStatus *appliedStatus = NULL;
1584
1585                 for (changedIter = pSyncReturnChangedDatastore->changeItem; changedIter != NULL; changedIter = g_list_next(changedIter)) {
1586                         changedItem = (changedIter->data);
1587
1588                         FW_LOGV("changedItem->changeType : %d", changedItem->changeType);
1589                         FW_LOGV("changedItem->luid : %s", changedItem->luid);
1590                         FW_LOGV("changedItem->content_type : %s", changedItem->content_type);
1591                         FW_LOGV("changedItem->indexOfDatastore : %d", changedItem->indexOfDatastore);
1592                         FW_LOGV("changedItem->data : %s", changedItem->data);
1593
1594                         add_receive_count(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, 1);
1595
1596                         bool da_fail = false;
1597                         CommandResult returnResult = COMMAND_RESULT_INIT;
1598                         switch (changedItem->changeType) {
1599                                 case CHANGE_ADD:
1600                                 {
1601                                         /* add to service DB*/
1602                                         if (changedItem->data != NULL) {
1603                                                 char *service_id = NULL;
1604                                                 da_err = DACI_Service_Add_Item(datastoreinfo_per_content_type[datastoreContentType]->datastore_id,
1605                                                                                                                                                         accountId, folderId, (const void *)(changedItem->data), &service_id);
1606                                                 FW_LOGV("da_err = %d", da_err);
1607
1608                                                 if (da_err == DACI_SUCCESS) {
1609
1610                                                         daci_item daci_item_s;
1611                                                         daci_item_s.account_id = accountId;
1612                                                         daci_item_s.item_id = changedItem->luid;
1613                                                         daci_item_s.folder_id = folderId;
1614                                                         daci_item_s.data_store_id = datastoreinfo_per_content_type[datastoreContentType]->datastore_id;
1615                                                         daci_item_s.service_id = service_id;
1616                                                         daci_item_s.access_name = "Engine";
1617
1618                                                         da_err = DACI_Add_Item(&daci_item_s, 1);
1619                                                         FW_LOGV("da_err = %d", da_err);
1620                                                         if (da_err == DACI_SUCCESS) {
1621                                                                 int acc_cnt;
1622                                                                 daci_folder *pDaci_folder = DACI_Get_Folder_Mapping_Service_Id(accountId, folderId,  &acc_cnt);
1623                                                                 FW_LOGV("acc_cnt = %d", acc_cnt);
1624
1625                                                                 int i;
1626                                                                 bool success = true;
1627
1628                                                                 char **fw_item_id_list = DACI_Generate_Item_Luid(1, acc_cnt);
1629                                                                 for (i = 0; i < acc_cnt; i++) {
1630                                                                         da_err = ChangeLog_Add_Item(pDaci_folder[i].account_id, fw_item_id_list[i], service_id,
1631                                                                                                 datastoreinfo_per_content_type[content_type]->datastore_id, pDaci_folder[i].folder_id);
1632                                                                         free(fw_item_id_list[i]);
1633
1634                                                                         FW_LOGV("da_err = %d", da_err);
1635
1636                                                                         if (da_err != DACI_SUCCESS) {
1637                                                                                 success = false;
1638                                                                                 break;
1639                                                                         }
1640                                                                 }
1641                                                                 if (fw_item_id_list != NULL) {
1642                                                                         free(fw_item_id_list);
1643                                                                 }
1644
1645                                                                 if (success == true) {
1646                                                                         returnResult = COMMAND_RESULT_ADDED;
1647                                                                         add_add_count(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, 1);
1648                                                                 } else{
1649                                                                         /*this case just fail do rollback and goto fail*/
1650                                                                         returnResult = __convert_return_status(da_err);
1651                                                                         da_fail = true;
1652                                                                 }
1653                                                                 DACI_Free_Memory_Folder(pDaci_folder, acc_cnt);
1654                                                         } else{
1655                                                                 /*this case just fail do rollback and goto fail*/
1656                                                                 returnResult = __convert_return_status(da_err);
1657                                                                 da_fail = true;
1658                                                         }
1659                                                 } else  if (da_err ==  DACI_ERR_NOT_SUPPORTED || da_err == DACI_ERR_INVALID_CONTENT || da_err == DACI_ERR_ALREADY_EXIST) {
1660                                                         /*just return error to server*/
1661                                                         returnResult = __convert_return_status(da_err);
1662                                                 } else{
1663                                                         /*this case just fail do rollback and goto fail*/
1664                                                         returnResult = __convert_return_status(da_err);
1665                                                         da_fail = true;
1666                                                 }
1667
1668                                                 if (service_id != NULL)
1669                                                         free(service_id);
1670                                         } else
1671                                                 returnResult = COMMAND_RESULT_COMMAND_FAIL;
1672
1673                                         break;
1674                                 }
1675                                 case CHANGE_REPLACE:
1676                                 {
1677                                         if (changedItem->data != NULL) {
1678                                                 /* update from service DB*/
1679                                                 da_err =  DACI_Service_Update_Item(datastoreinfo_per_content_type[datastoreContentType]->datastore_id,
1680                                                                                                                                                 accountId, folderId, changedItem->luid, (const void *)(changedItem->data));
1681                                                 FW_LOGV("da_err = %d", da_err);
1682                                                 if (da_err == DACI_SUCCESS) {
1683
1684                                                         int acc_cnt;
1685                                                         daci_item *pDaci_item_from_other_account = DACI_Get_item_Mapping_Service_Id(accountId, changedItem->luid, &acc_cnt);
1686
1687                                                         int i;
1688                                                         bool success = true;
1689                                                         for (i = 0; i < acc_cnt; i++) {
1690                                                                 da_err = ChangeLog_Update_Item(pDaci_item_from_other_account[i].account_id, pDaci_item_from_other_account[i].item_id,
1691                                                                                                                                                         datastoreinfo_per_content_type[content_type]->datastore_id);
1692                                                                 if (da_err != DACI_SUCCESS) {
1693                                                                         success = false;
1694                                                                         break;
1695                                                                 }
1696                                                         }
1697
1698                                                         if (success == true) {
1699                                                                 returnResult = COMMAND_RESULT_OK;
1700                                                                 add_replace_count(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, 1);
1701                                                         } else{
1702                                                                 /*this case just fail do rollback and goto fail*/
1703                                                                 returnResult = __convert_return_status(da_err);
1704                                                                 da_fail = true;
1705                                                         }
1706                                                         DACI_Free_Memory_Item(pDaci_item_from_other_account, acc_cnt);
1707                                                 } else if (da_err ==  DACI_ERR_NOT_SUPPORTED || da_err == DACI_ERR_INVALID_CONTENT) {
1708                                                         /*just return error to server*/
1709                                                         returnResult = __convert_return_status(da_err);
1710                                                 } else{
1711                                                         /*this case just fail do rollback and goto fail*/
1712                                                         returnResult = __convert_return_status(da_err);
1713                                                         da_fail = true;
1714                                                 }
1715                                         } else
1716                                                 returnResult = COMMAND_RESULT_COMMAND_FAIL;
1717
1718                                         break;
1719                                 }
1720                                 case CHANGE_DELETE:
1721                                 {
1722                                         /* delete from service DB*/
1723                                         da_err =  DACI_Service_Delete_Item(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, accountId, folderId, changedItem->luid);
1724                                         FW_LOGV("[DACI_Service_Delete_Item] result : %d", da_err);
1725                                         if (da_err == DACI_SUCCESS) {
1726                                                 int acc_cnt;
1727                                                 daci_item *pDaci_item_from_other_account = DACI_Get_item_Mapping_Service_Id(accountId, changedItem->luid, &acc_cnt);
1728
1729                                                 int i;
1730                                                 bool success = true;
1731                                                 for (i = 0; i < acc_cnt; i++) {
1732                                                         da_err = ChangeLog_Delete_Item(pDaci_item_from_other_account[i].account_id, pDaci_item_from_other_account[i].item_id,
1733                                                                                                                                                 datastoreinfo_per_content_type[content_type]->datastore_id);
1734                                                         if (da_err != DACI_SUCCESS) {
1735                                                                 success = false;
1736                                                                 break;
1737                                                         }
1738                                                 }
1739
1740                                                 DACI_Free_Memory_Item(pDaci_item_from_other_account, acc_cnt);
1741                                                 if (success == true) {
1742                                                         da_err =  DACI_Delete_Item_By_Item_Id(changedItem->luid);
1743                                                         if (da_err == DACI_SUCCESS) {
1744                                                                 returnResult = COMMAND_RESULT_DELETE_WITHOUT_ARCHIVE;
1745                                                                 add_delete_count(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult, 1);
1746                                                         } else {
1747                                                                 /*this case just fail do rollback and goto fail*/
1748                                                                 returnResult = __convert_return_status(da_err);
1749                                                                 da_fail = true;
1750                                                         }
1751                                                 } else {
1752                                                         /*this case just fail do rollback and goto fail*/
1753                                                         returnResult = __convert_return_status(da_err);
1754                                                         da_fail = true;
1755                                                 }
1756                                         } else if (da_err == DACI_ERR_NO_DATA || da_err == DACI_ERR_SUB_DATA_EXIST || da_err == DACI_ERR_INVALID_CONTENT) {
1757                                                 /*just return error to server*/
1758                                                 returnResult = __convert_return_status(da_err);
1759                                         } else {
1760                                                 /*this case just fail do rollback and goto fail*/
1761                                                 returnResult = __convert_return_status(da_err);
1762                                                 da_fail = true;
1763                                         }
1764
1765                                         break;
1766                                 }
1767                                 default:
1768                                         break;
1769                         }
1770
1771                         if (da_fail == true) {
1772
1773                                 if (folderId != NULL)
1774                                         free(folderId);
1775
1776                                 /*replace syncresult when rollback happened*/
1777                                 if (datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult != NULL)
1778                                         free(datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult);
1779
1780                                 datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult = tempServerSyncResult;
1781
1782                                 FW_LOGV("Transaction_Rollback");
1783                                 FW_LOGV("DACI_Service_End with datastoreContentType = %d", datastoreContentType);
1784                                 DACI_Service_End(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 0);
1785                                 DACI_End_Transaction(TRANSACTION_ROLLBACK);
1786
1787                                 err = SE_INTERNAL_DA_ERROR;
1788                                 FW_LOGE("failed in DA");
1789                                 goto error;
1790                         }
1791
1792                         appliedStatus = create_appliedstatus(changedItem->luid, changedItem->changeType, returnResult);
1793                         if (appliedStatus == NULL) {
1794                                 err = SE_INTERNAL_NO_MEMORY;
1795                                 FW_LOGE("failed to alloc memory");
1796                                 goto error;
1797                         }
1798                         add_sendingstatus_appliedstatus(sendingStatus, appliedStatus);
1799                 }
1800
1801                 if (folderId != NULL)
1802                         free(folderId);
1803
1804                 if (changeItemCount > 0) {
1805
1806                         if (tempServerSyncResult != NULL)
1807                                 free(tempServerSyncResult);
1808
1809                         /*end transaction*/
1810                         FW_LOGV("DACI_Service_End with datastoreContentType = %d", datastoreContentType);
1811                         DACI_Service_End(datastoreinfo_per_content_type[datastoreContentType]->datastore_id, 1);
1812                         DACI_End_Transaction(TRANSACTION_COMMIT);
1813                 }
1814
1815                 needToSave = false;
1816                 pServerSyncResult = datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult;
1817                 if (pServerSyncResult->numberOfChange == pServerSyncResult->received_count)
1818                         needToSave = true;
1819
1820                 OperationType operationType;
1821                 if (datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult->numberOfChange == 0)
1822                         operationType = OPERATION_NOOP;
1823                 else
1824                         operationType = OPERATION_ADD;
1825                 err = __process_update(accountId, SYNC_PROGRESS_SUCCESS, operationType, datastoreContentType, true, needToSave,
1826                                                                                                 datastoreinfo_per_content_type[datastoreContentType]->serverSyncResult);
1827                 if (err != SE_INTERNAL_OK) {
1828                         FW_LOGE("failed in process_update");
1829                         goto error;
1830                 }
1831
1832                 (*pSyncObj)->sendingStatus = g_list_append((*pSyncObj)->sendingStatus, sendingStatus);
1833
1834                 iter = g_list_next(iter);
1835
1836                 (*pSyncReturnObj)->changedDatastore = g_list_remove((*pSyncReturnObj)->changedDatastore, pSyncReturnChangedDatastore);
1837                 free_changeddatastore(pSyncReturnChangedDatastore);
1838                 pSyncReturnChangedDatastore = NULL;
1839         }
1840
1841         FW_LOGV("end");
1842         return err;
1843
1844 error:
1845         FW_LOGE("err = %d", err);
1846         return err;
1847 }
1848
1849 static SE_ErrorType __update_sync_result(int accountId)
1850 {
1851         FW_LOGV("start");
1852         SE_ErrorType err = SE_INTERNAL_OK;
1853         DACI_RETURN da_err = DACI_SUCCESS;
1854         int content_type;
1855
1856         daci_last_anchor next_anchor_info;
1857         for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
1858                 if (!datastoreinfo_per_content_type[content_type]->clientSyncType) continue;
1859
1860                 next_anchor_info.access_name = "Engine";
1861                 next_anchor_info.account_id = accountId;
1862                 next_anchor_info.data_store_id = datastoreinfo_per_content_type[content_type]->datastore_id;
1863                 next_anchor_info.last_anchor_client = datastoreinfo_per_content_type[content_type]->nextAnchorClient;
1864                 next_anchor_info.last_anchor_server = datastoreinfo_per_content_type[content_type]->nextAnchorServer;
1865
1866                 da_err = DACI_Set_Last_Anchor(&next_anchor_info);
1867                 if (da_err != DACI_SUCCESS)
1868                         FW_LOGE("failed in DACI_Set_Last_Anchor");
1869
1870                 da_err =  set_ChangePoint_Service(accountId, datastoreinfo_per_content_type[content_type]->datastore_id);
1871                 if (da_err != DACI_SUCCESS)
1872                         FW_LOGE("failed in set_ChangePoint_Service");
1873
1874         }
1875
1876         FW_LOGV("end");
1877         return err;
1878 }
1879
1880 static CommandResult __convert_return_status(DACI_RETURN da_err)
1881 {
1882         CommandResult returnResult = COMMAND_RESULT_INIT;
1883         switch (da_err) {
1884         case 1: /*DACI_SUCCESS*/
1885                 /*never comes this case
1886                 break;*/
1887         case -813: /*DACI_ERR_ALREADY_EXIST*/
1888                 returnResult = COMMAND_RESULT_ALREADY_EXIST;            /*[A] Already exists exception*/
1889                 break;
1890         case -815: /*DACI_ERR_MEMORY_FULL*/              /* oma : 420 (device full exception) */
1891                 returnResult = COMMAND_RESULT_DEVICE_FULL;                      /*[AR] Device full exception*/
1892                 break;
1893         case -820: /*DACI_ERR_NOT_SUPPORTED*/            /* oma : 415 (unsupported media type or format exception) */
1894                 returnResult = COMMAND_RESULT_UNSUPPORTED_TYPE; /*[AR] Unsupported media type or format exception*/
1895                 break;
1896         case -819: /*DACI_ERR_NO_DATA*/                  /* kies : not found, oma : 211 (item not deleted exception) */
1897                 returnResult = COMMAND_RESULT_NOT_EXIST;                        /*[D] Item not deleted exception*/
1898                 break;
1899         case -800: /*DACI_ERRORS*/
1900         case -801: /*DACI_NOT_FOUND_PLUG_IN*/
1901         case -802: /*DACI_ERR_OPEN_FAILED*/
1902         case -803: /*DACI_ERR_CLOSE_FAILED*/
1903         case -804: /*DACI_ERR_TRANSACTION_FAILED*/
1904         case -805: /*DACI_ERR_CREATE_TABLE_FAILED*/
1905         case -806: /*DACI_ERR_DROP_TABLE_FAILED*/
1906         case -807: /*DACI_ERR_QUERY_FAILED*/
1907         case -808: /*DACI_ERR_NOT_OPENED*/
1908         case -809: /*DACI_ERR_ACCOUNT_FULL*/
1909         case -810: /*DACI_ERR_DELETE_LAST_ACCOUNT*/
1910         case -811: /*DACI_ERR_PRIMARY_KEY_NOT_UNIQUE*/
1911         case -812: /*DACI_ERR_DB_HANDLER_MGR*/
1912         case -814: /*DACI_ERR_INVALID_CONTENT*/
1913         case -816: /*DACI_ERR_SUB_DATA_EXIST*/                   /* oma : 427 (item not empty) */
1914         case -817: /*DACI_ERR_LOCKED*/                /* kies : cannot access */
1915         case -818: /*DACI_ERR_MORE_DATA*/
1916         case -821:/*DACI_ERR_NOT_EXECUTE*/              /* kies : ex) sms send command, no network service */
1917         default:
1918                 returnResult = COMMAND_RESULT_COMMAND_FAIL;     /*[ARD] Command failed exception*/
1919                 break;
1920         }
1921
1922         return returnResult;
1923 }
1924
1925 static char *__convert_cttype(int datastore_id)
1926 {
1927         char *cttype = NULL;
1928         /*FIXME : check type and version (contact : vCard2.1 , calendar : vCalendar 1.0 , memo : plain text) of real item data..*/
1929         switch (datastore_id) {
1930         case TYPE_CONTACT: /*contact (vCard2.1)*/
1931                 cttype = ELEMENT_TEXT_VCARD;
1932                 break;
1933         case TYPE_CALENDAR: /*calendar (vCalendar2.0)*/
1934                 cttype = ELEMENT_TEXT_VCAL;
1935                 break;
1936         case TYPE_MEMO: /*note(Memo)*/
1937                 cttype = ELEMENT_TEXT_PLAIN;
1938                 break;
1939         default:
1940                 break;
1941         }
1942
1943         return cttype;
1944 }
1945
1946 static int __convert_synctype_value(char *syncType_str)
1947 {
1948         int syncType_value;
1949
1950         if (strcmp(syncType_str, DEFINE_ALERT_SLOW_SYNC_STR) == 0)
1951                 syncType_value = ALERT_SLOW_SYNC ;
1952         else if (strcmp(syncType_str, DEFINE_ALERT_TWO_WAY_STR) == 0)
1953                 syncType_value = ALERT_TWO_WAY ;
1954         else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) == 0)
1955                 syncType_value =  ALERT_ONE_WAY_FROM_CLIENT;
1956         else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) == 0)
1957                 syncType_value = ALERT_ONE_WAY_FROM_SERVER;
1958         else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) == 0)
1959                 syncType_value = ALERT_REFRESH_FROM_SERVER;
1960         else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) == 0)
1961                 syncType_value = ALERT_REFRESH_FROM_CLIENT;
1962         else
1963                 syncType_value = ALERT_UNKNOWN;
1964
1965         return syncType_value;
1966 }
1967
1968 bool synchronize(int accountId, char *syncMode, SanPackage *pSanPackage)
1969 {
1970         FW_LOGV("accountID = %d", accountId);
1971         FW_LOGV("syncMode = %s", syncMode);
1972
1973         /*FIXME*/
1974         remove(OMA_DS_MSG_PATH);
1975
1976         /*FIXME*/
1977         long t, dt;
1978         t = myclock();
1979
1980         SE_ErrorType err = SE_INTERNAL_OK;
1981         Common_ErrorType errorCode = COMMON_OK;
1982         SyncProgress process = PROGRESS_NONE;
1983         SyncError error = ERROR_NONE;
1984
1985         int sessionTime = FW_Convert_Seconds_To_UTC(time(NULL));                /*lastSessionTime for resultView;*/
1986         FW_LOGV("sessionTime = %d", sessionTime);
1987
1988         PreSyncReturnObj *pPreSyncReturnObj = NULL;
1989         SyncObj *pSyncObj = NULL;
1990         SyncReturnObj *pSyncReturnObj = NULL;
1991
1992         bool cancel_flag = false;
1993         int content_type;
1994         char *session_id = NULL;
1995         DCI_RETURN dci_result = DCI_SUCCESS;
1996         DACI_RETURN da_err = DACI_SUCCESS;
1997
1998         dci_result =  DCI_PM_Lock_State(DEFINE_PLATFORM, LCD_OFF , STAY_CUR_STATE, 0);
1999         if (dci_result != DCI_SUCCESS) {
2000                 FW_LOGE("failed in DCI_PM_Lock_State");
2001         }
2002
2003         da_err = DACI_Open_Agent();
2004         if (da_err != DACI_SUCCESS) {
2005                 FW_LOGE("failed in DACI_Open_Agent");
2006                 err = SE_INTERNAL_DA_ERROR;
2007                 goto fail_part;
2008         }
2009
2010         /*prepare for sending pre sync*/
2011         err = __prepare_pre_sync(accountId, syncMode, pSanPackage);
2012         if (err != SE_INTERNAL_OK) {
2013                 FW_LOGE("failed in __prepare_pre_sync");
2014                 goto fail_part;
2015         }
2016
2017         cancel_flag = task_process_check_cancel_flag();
2018         if (cancel_flag) {
2019                 err = SE_INTERNAL_CANCEL;
2020                 goto cancel_part;
2021         }
2022
2023         pPreSyncReturnObj = (PreSyncReturnObj *)calloc(1, sizeof(PreSyncReturnObj));
2024         if (pPreSyncReturnObj == NULL) {
2025                 FW_LOGE("failed to alloc memory");
2026                 err = SE_INTERNAL_NO_MEMORY;
2027                 goto fail_part;
2028         }
2029
2030         /*pkg 1 */
2031         if (strcmp(syncMode, DEFINE_SYNC_MODE_PUSH) == 0) {
2032                 session_id = g_strdup_printf("%u", pSanPackage->sessionID);             /*freed in pre_sync*/
2033                 errorCode = pre_sync(TRANSPORT_TYPE, accountId, session_id, (void **)&pPreSyncReturnObj);
2034         } else
2035                 errorCode = pre_sync(TRANSPORT_TYPE, accountId, NULL, (void **)&pPreSyncReturnObj);
2036
2037         cancel_flag = task_process_check_cancel_flag();
2038         if (cancel_flag) {
2039                 err = SE_INTERNAL_CANCEL;
2040                 goto cancel_part;
2041         }
2042
2043         FW_LOGV("pre_sync errorCode =[%d]",  errorCode);
2044         if (errorCode != COMMON_OK) {
2045                 err = SE_INTERNAL_SA_ERROR;
2046                 goto fail_part;
2047         }
2048
2049         /*execute pre_sync return*/
2050         err = __execute_pre_sync(accountId, pPreSyncReturnObj, sessionTime);
2051         if (err != SE_INTERNAL_OK) {
2052                 FW_LOGE("failed in __process_pre_sync");
2053                 goto fail_part;
2054         }
2055
2056         __session_process(accountId, PROGRESS_AUTHENTICATED, ERROR_NONE);
2057
2058         cancel_flag = task_process_check_cancel_flag();
2059         if (cancel_flag) {
2060                 err = SE_INTERNAL_CANCEL;
2061                 goto cancel_part;
2062         }
2063
2064         /* pkg 3 */
2065         FW_LOGV("pre_sync end pkg3 start");
2066         char *msg = NULL;
2067         unsigned int msg_size;
2068         char *recvMsg = NULL;
2069         unsigned  int recvMsg_size;
2070
2071         pSyncObj = (SyncObj *)calloc(1, sizeof(SyncObj));
2072         if (pSyncObj == NULL) {
2073                 FW_LOGE("failed to alloc memory");
2074                 err = SE_INTERNAL_NO_MEMORY;
2075                 goto fail_part;
2076         }
2077
2078         pSyncReturnObj = (SyncReturnObj *)calloc(1, sizeof(SyncReturnObj));
2079         if (pSyncReturnObj == NULL) {
2080                 FW_LOGE("failed to alloc memory");
2081                 err = SE_INTERNAL_NO_MEMORY;
2082                 goto fail_part;
2083         }
2084
2085         err = __assemble_changeddatastores(accountId, &pSyncObj);
2086         if (err != SE_INTERNAL_OK) {
2087                 FW_LOGE("failed in __assemble_changeddatastore");
2088                 goto fail_part;
2089         }
2090
2091         cancel_flag = task_process_check_cancel_flag();
2092         if (cancel_flag) {
2093                 err = SE_INTERNAL_CANCEL;
2094                 goto cancel_part;
2095         }
2096
2097         int isFinish = 0;
2098         while (!isFinish) {
2099
2100                 errorCode = generate_msg((void **)&pSyncObj, &msg, &msg_size);
2101                 if (errorCode != COMMON_OK) {
2102                         FW_LOGE("Failed in generate_Msg");
2103                         err = SE_INTERNAL_SA_ERROR;
2104                         goto fail_part;
2105                 }
2106
2107                 cancel_flag = task_process_check_cancel_flag();
2108                 if (cancel_flag) {
2109                         err = SE_INTERNAL_CANCEL;
2110                         goto cancel_part;
2111                 }
2112
2113                 errorCode = exchange_msg(TRANSPORT_TYPE,  msg, msg_size,  &recvMsg, &recvMsg_size);
2114                 if (errorCode != COMMON_OK) {
2115                         FW_LOGE("Failed in exchange_Msg");
2116                         err = SE_INTERNAL_SA_ERROR;
2117                         goto fail_part;
2118                 }
2119
2120                 cancel_flag = task_process_check_cancel_flag();
2121                 if (cancel_flag) {
2122                         err = SE_INTERNAL_CANCEL;
2123                         goto cancel_part;
2124                 }
2125
2126                 errorCode = process_recv_msg(recvMsg, recvMsg_size, (void **)&pSyncReturnObj, &isFinish);
2127                 if (errorCode != COMMON_OK) {
2128                         FW_LOGE("Failed in processRecv_Msg");
2129                         err = SE_INTERNAL_SA_ERROR;
2130                         goto fail_part;
2131                 }
2132
2133                 cancel_flag = task_process_check_cancel_flag();
2134                 if (cancel_flag) {
2135                         err = SE_INTERNAL_CANCEL;
2136                         goto cancel_part;
2137                 }
2138
2139                 err = __execute_sync(accountId, &pSyncObj, &pSyncReturnObj);
2140                 if (err != SE_INTERNAL_OK) {
2141                         FW_LOGE("failed in __execute_sync");
2142                         goto fail_part;
2143                 }
2144
2145                 free_changeddatastores(pSyncReturnObj->changedDatastore);
2146                 pSyncReturnObj->changedDatastore = NULL;
2147
2148                 free_appliedstatuses(pSyncReturnObj->status);
2149                 pSyncReturnObj->status = NULL;
2150
2151                 cancel_flag = task_process_check_cancel_flag();
2152                 if (cancel_flag) {
2153                         err = SE_INTERNAL_CANCEL;
2154                         goto cancel_part;
2155                 }
2156         }
2157
2158         err = __update_sync_result(accountId);
2159         if (err != SE_INTERNAL_OK) {
2160                 FW_LOGE("failed in __update_anchor");
2161                 goto fail_part;
2162         }
2163
2164         __write_sync_data(accountId, syncType, SYNC_SESSION_SUCCEEDED, sessionTime);
2165
2166         goto return_part;
2167
2168 cancel_part:
2169
2170         /*clean up for SA unusual end sync process case*/
2171         clean_up_SA();
2172
2173         for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
2174                 if (datastoreinfo_per_content_type[content_type]->clientSyncType)
2175                         datastoreinfo_per_content_type[content_type]->clientSyncResult->sessionResult = SYNC_SESSION_STOPPED;
2176         }
2177
2178         __write_sync_data(accountId, syncType, SYNC_SESSION_STOPPED, sessionTime);
2179
2180         goto return_part;
2181
2182 fail_part:
2183
2184         /*clean up for SA unusual end sync process case*/
2185         clean_up_SA();
2186
2187         for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
2188                 if (datastoreinfo_per_content_type[content_type]->clientSyncType)
2189                         datastoreinfo_per_content_type[content_type]->clientSyncResult->sessionResult = SYNC_SESSION_FAILED;
2190         }
2191
2192         __write_sync_data(accountId, syncType, SYNC_SESSION_FAILED, sessionTime);
2193
2194 return_part:
2195
2196         if (err == SE_INTERNAL_SA_ERROR)
2197                 convert_common_errorcode(errorCode, &process, &error);
2198         else
2199                 convert_engine_errorcode(err, &process, &error);
2200
2201          __off_synchronising_account(accountId);
2202
2203         __session_process(accountId, process, error);
2204         FW_LOGV("process = %d", process);
2205         FW_LOGV("error = %d", error);
2206
2207         DACI_Close_Agent();
2208
2209         dci_result =  DCI_PM_Unlock_State(DEFINE_PLATFORM, LCD_OFF , RESET_TIMER);
2210         if (dci_result != DCI_SUCCESS) {
2211                 FW_LOGE("failed in DCI_PM_Unlock_State");
2212         }
2213
2214         if (pPreSyncReturnObj != NULL) {
2215                 free_presyncreturnobj(pPreSyncReturnObj);
2216                 pPreSyncReturnObj = NULL;
2217         }
2218
2219         if (pSyncObj != NULL) {
2220                 free_syncobj(pSyncObj);
2221                 pSyncObj = NULL;
2222         }
2223
2224         if (pSyncReturnObj != NULL) {
2225                 free_syncreturnobj(pSyncReturnObj);
2226                 pSyncReturnObj = NULL;
2227         }
2228
2229         for (content_type = 0; content_type < TYPE_CONTENT_COUNT; content_type++) {
2230                 free_datastore(datastoreinfo_per_content_type[content_type]);
2231                 datastoreinfo_per_content_type[content_type] = NULL;
2232         }
2233
2234         /*FIXME*/
2235         dt = myclock()-t;
2236         FW_LOGE("\n=============TOTAL---- % d . % d sec\n", dt / 1000, dt % 1000);
2237
2238         if (err != SE_INTERNAL_OK)
2239                 return false;
2240         else
2241                 return true;
2242 }
2243
2244 void convert_common_errorcode(Common_ErrorType errorCode, SyncProgress *process, SyncError *error)
2245 {
2246         switch (errorCode) {
2247         case COMMON_OK:                 /*ERROR_INTERNAL_OK*/
2248         {
2249                 /* Do nothing : Error None
2250                 * pre_sync : PROGRESS_AUTHENTICATED
2251                 * sync end : PROGRESS_DONE
2252                                                 process = PROGRESS_AUTHENTICATED;
2253                                                 process = PROGRESS_DONE;
2254                 */
2255                 *process = PROGRESS_DONE;
2256                 *error = ERROR_NONE;
2257         }
2258                 break;
2259         case COMMON_MISCONFIGURATION:                                   /*ERROR_INTERNAL_MISCONFIGURATION : need configure infomation (account_id, id, pw, server_url...)*/
2260         {
2261                 *process = PROGRESS_ERROR;
2262                 *error = ERROR_SYNCHDR;
2263         }
2264                 break;
2265         case COMMON_AUTHENTICATION_ERROR: /*ERROR_AUTH_REQUIRED, ERROR_AUTH_REJECTED*/
2266         {
2267                 *process = PROGRESS_ERROR;
2268                 *error = ERROR_AUTHENTICATE;
2269         }
2270                 break;
2271         case COMMON_NOT_FOUND:                                          /*ERROR_NOT_FOUND (ERROR_INTERNAL ??)*/
2272         {
2273                 *process = PROGRESS_ERROR;
2274                 *error = ERROR_SYNCHDR;
2275         }
2276                 break;
2277         case COMMON_NO_MEMORY:                                  /*ERROR_INTERNAL_NO_MEMORY*/
2278         {
2279                 *process = PROGRESS_ERROR;
2280                 *error = ERROR_MEMORY_FULL;
2281         }
2282                 break;
2283         case COMMON_INTERNAL_ERROR:                             /*ERROR_INTERNAL_NOT_DEFINED || ERROR_INTERNAL_BINDER_ERROR*/
2284         {
2285                 *process = PROGRESS_ERROR;
2286                 *error = ERROR_INTERNAL;
2287         }
2288                 break;
2289         case COMMON_SERVER_ERROR:                                       /*ERROR_GENERIC || ERROR_SERVER_FAILURE*/
2290         {
2291                 *process = PROGRESS_ERROR;
2292                 *error = ERROR_SERVER;
2293         }
2294                 break;
2295         case COMMON_CONNECTION_ERROR:                   /*ERROR_INTERNAL_CONNECTION_ERROR*/
2296         {
2297                 *process = PROGRESS_ERROR;
2298                 *error = ERROR_CONNECTION;
2299         }
2300                 break;
2301         case COMMON_AUTOCONFIG_NOT_SUPPORT_BY_SERVER:                           /*ERROR_INTERNAL_AUTOCONFIG_NOT_SUPPORT_BY_SERVER*/
2302         {
2303                 *process = PROGRESS_ERROR;
2304                 *error = ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2305         }
2306                 break;
2307         default:
2308         {
2309                 *process = PROGRESS_ERROR;
2310                 *error = ERROR_INTERNAL;                                                /*?? unknown error*/
2311         }
2312                 break;
2313         }
2314 }
2315
2316 void convert_engine_errorcode(SE_ErrorType err, SyncProgress *process, SyncError *error)
2317 {
2318         switch (err) {
2319         case ERROR_UNKNOWN:
2320                 *process = PROGRESS_ERROR;
2321                 *error = ERROR_INTERNAL;
2322                 break;
2323         case SE_INTERNAL_CANCEL:
2324         case SE_INTERNAL_OK:
2325                 *process = PROGRESS_DONE;
2326                 *error = ERROR_NONE;
2327                 break;
2328         case SE_INTERNAL_ERROR:
2329                 *process = PROGRESS_ERROR;
2330                 *error = ERROR_INTERNAL;
2331                 break;
2332         case SE_INTERNAL_NO_MEMORY:
2333                 *process = PROGRESS_ERROR;
2334                 *error = ERROR_MEMORY_FULL;
2335                 break;
2336         case SE_INTERNAL_NOT_DEFINED:
2337                 *process = PROGRESS_ERROR;
2338                 *error = ERROR_INTERNAL;
2339                 break;
2340         case SE_INTERNAL_DA_ERROR:
2341                 *process = PROGRESS_ERROR;
2342                 *error = ERROR_DB;
2343                 break;
2344         case SE_INTERNAL_EVENT_ERROR:
2345                 *process = PROGRESS_ERROR;
2346                 *error = ERROR_INTERNAL;
2347                 break;
2348         case SE_INTERNAL_ENGINE_CONTROLER_ERROR:
2349                 *process = PROGRESS_ERROR;
2350                 *error = ERROR_INTERNAL;
2351                 break;
2352         case SE_INTERNAL_SCHEDULER_ERROR:
2353                 *process = PROGRESS_ERROR;
2354                 *error = ERROR_INTERNAL;
2355                 break;
2356         case SE_INTERNAL_SA_ERROR:
2357                 break;
2358         case SE_INTERNAL_MISCONFIGURATION:
2359                 *process = PROGRESS_ERROR;
2360                 *error = ERROR_SYNCHDR;
2361                 break;
2362         }
2363 }
2364
2365 SE_ErrorType session_process(char *profileDirName, SyncProgress process, SyncError error)
2366 {
2367         FW_LOGV("start ");
2368         FW_LOGV("profileDirName = %s", profileDirName);
2369         FW_LOGV("process = %d", process);
2370         FW_LOGV("error = %d", error);
2371
2372         SE_ErrorType err = SE_INTERNAL_OK;
2373
2374         char *syncProcess = NULL;
2375         char *syncError = NULL;
2376         char *profile = NULL;
2377         char *syncProcess_str = NULL;
2378         char *syncError_str = NULL;
2379
2380         switch (process) {
2381         case PROGRESS_NONE:
2382                 syncProcess = DEFINE_SYNC_PROGRESS_NONE;
2383                 break;
2384         case PROGRESS_INIT:
2385                 syncProcess = DEFINE_SYNC_INIT;
2386                 break;
2387         case PROGRESS_CONNECTING:
2388                 syncProcess = DEFINE_SYNC_CONNECTING;
2389                 break;
2390         case PROGRESS_AUTHENTICATED:
2391                 syncProcess = DEFINE_SYNC_AUTHENTICATED;
2392                 break;
2393         case PROGRESS_DONE:
2394                 syncProcess = DEFINE_SYNC_DONE;
2395                 break;
2396         case PROGRESS_ERROR:
2397                 syncProcess = DEFINE_SYNC_ERROR;
2398                 break;
2399         default:
2400                 break;
2401         }
2402
2403         switch (error) {
2404         case ERROR_NONE:
2405                 syncError = DEFINE_ERROR_NONE;
2406                 break;
2407         case ERROR_CONNECTION:
2408                 syncError = DEFINE_ERROR_CONNECTION;
2409                 break;
2410         case ERROR_SYNCHDR:
2411                 syncError = DEFINE_ERROR_SYNCHDR;
2412                 break;
2413         case ERROR_INTERNAL:
2414                 syncError = DEFINE_ERROR_INTERNAL;
2415                 break;
2416         case ERROR_SUSPENDED:
2417                 syncError = DEFINE_ERROR_SUSPENDED;
2418                 break;
2419         case ERROR_DB:
2420                 syncError = DEFINE_ERROR_DB;
2421                 break;
2422         case ERROR_ABORT:
2423                 syncError = DEFINE_ERROR_ABORT;
2424                 break;
2425         case ERROR_SERVER:
2426                 syncError = DEFINE_ERROR_SERVER;
2427                 break;
2428         case ERROR_MEMORY_FULL:
2429                 syncError = DEFINE_ERROR_MEMORY_FULL;
2430                 break;
2431         case ERROR_AUTHENTICATE:
2432                 syncError = DEFINE_ERROR_AUTHENTICATE;
2433                 break;
2434         case ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER:
2435                 syncError = DEFINE_ERROR_AUTOCONFIG_NOT_SUPPORT_BY_SERVER;
2436                 break;
2437         default:
2438                 break;
2439         }
2440
2441         if (syncProcess == NULL || syncError == NULL) {
2442                 err = SE_INTERNAL_NOT_DEFINED;
2443                 goto error;
2444         }
2445
2446         if (profileDirName != NULL )
2447                 profile = strdup(profileDirName);
2448
2449         if (syncProcess != NULL )
2450                 syncProcess_str = strdup(syncProcess);
2451
2452         if (syncError != NULL )
2453                 syncError_str = strdup(syncError);
2454
2455         err = send_noti_session_process(profile, syncProcess_str, syncError_str);
2456         if (err != SE_INTERNAL_OK) {
2457                 FW_LOGE("failed in send_noti_session_process");
2458                 goto error;
2459         }
2460
2461
2462         FW_LOGV("end");
2463
2464 error:
2465         if (profile)
2466                 free(profile);
2467         if (syncProcess_str)
2468                 free(syncProcess_str);
2469         if (syncError_str)
2470                 free(syncError_str);
2471
2472         return err;
2473 }
2474
2475 SE_ErrorType reset_synchronizing_profiles()
2476 {
2477         FW_LOGV("start");
2478         int *account_list = 0;
2479         int account_list_count = 0;
2480         SE_ErrorType err = SE_INTERNAL_OK;
2481
2482         DACI_RETURN da_err = DACI_Open_Agent();
2483         if (da_err != DACI_SUCCESS) {
2484                 FW_LOGE("failed in DACI_Open_Agent");
2485                 err = SE_INTERNAL_DA_ERROR;
2486                 goto error;
2487         }
2488
2489         account_list = DACI_Get_Account_Account_Id_List(&account_list_count);
2490         FW_LOGV("account_list_count = %d", account_list_count);
2491
2492         int i;
2493         for (i = 0; i < account_list_count; i++) {
2494                 FW_LOGV("account = %d", account_list[i]);
2495                 __off_synchronising_account(account_list[i]);
2496
2497                 FW_LOGV("construct_itemTbl From service start");
2498
2499                 /*construct_itemTbl From service*/
2500                 da_err = construct_ItemTbl_From_Service(account_list[i], TYPE_CONTACT);
2501                 if (da_err != DACI_SUCCESS)
2502                         FW_LOGE("failed in construct_ItemTbl_From_Service(TYPE_CONTACT)");
2503
2504                 da_err = construct_ItemTbl_From_Service(account_list[i], TYPE_CALENDAR);
2505                 if (da_err != DACI_SUCCESS)
2506                         FW_LOGE("failed in construct_ItemTbl_From_Service(TYPE_CALENDAR)");
2507
2508                 da_err = construct_ItemTbl_From_Service(account_list[i], TYPE_MEMO);
2509                 if (da_err != DACI_SUCCESS)
2510                         FW_LOGE("failed in construct_ItemTbl_From_Service(TYPE_MEMO)");
2511
2512                 FW_LOGV("construct_itemTbl From service end");
2513         }
2514
2515         DACI_Close_Agent();
2516         FW_LOGV("end");
2517
2518 error:
2519
2520         if (account_list_count > 0)
2521                 free(account_list);
2522
2523         return err;
2524 }
2525
2526 bool refresh_from_service_all(int accountId)
2527 {
2528         FW_LOGV("start");
2529         SE_ErrorType  err = SE_INTERNAL_OK;
2530
2531         DACI_RETURN da_err = DACI_Open_Agent();
2532         if (da_err != DACI_SUCCESS) {
2533                 err = SE_INTERNAL_DA_ERROR;
2534                 goto error;
2535         }
2536
2537         FW_LOGV("refresh_Item_Tbl_From_Service(%d, %d);", accountId, TYPE_CONTACT);
2538         refresh_Item_Tbl_From_Service(accountId, TYPE_CONTACT);
2539
2540         FW_LOGV("refresh_Item_Tbl_From_Service(%d, %d);", accountId, FW_CALENDAR);
2541         refresh_Item_Tbl_From_Service(accountId, TYPE_CALENDAR);
2542
2543         FW_LOGV("refresh_Item_Tbl_From_Service(%d, %d);", accountId, FW_MEMO);
2544         refresh_Item_Tbl_From_Service(accountId, TYPE_MEMO);
2545
2546 error:
2547
2548         DACI_Close_Agent();
2549
2550         if (err != SE_INTERNAL_OK)
2551                 return false;
2552         else
2553                 return true;
2554 }
2555
2556 /*FIXME cancel request to NA */
2557 /*SE_ErrorType cancel_sync_request()
2558 {
2559         FW_LOGV("start");
2560         SE_ErrorType err = SE_INTERNAL_OK;
2561
2562         cancel_connection_sync_request(TRANSPORT_TYPE);
2563
2564         FW_LOGV("end");
2565
2566         return err;
2567 }*/