Update change log and spec for wrt-plugins-tizen_0.4.74
[framework/web/wrt-plugins-tizen.git] / src / DataSync / DataSyncManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
19 #include "DataSyncManager.h"
20 #include "SyncInfo.h"
21 #include "SyncServiceInfo.h"
22 #include "SyncProfileInfo.h"
23
24 #include <sync_agent.h>
25 #include <sstream>
26 #include <Logger.h>
27
28 using namespace WrtDeviceApis::Commons;
29
30 namespace DeviceAPI {
31 namespace DataSync {
32
33 int DataSyncManager::m_instanceCount = 0;
34
35 DataSyncManager::DataSyncManager()
36 {
37         LoggerI("Initialize the datasync manager with count: "<<m_instanceCount);
38
39     if (0==m_instanceCount) {
40                 sync_agent_ds_error_e ds_err = sync_agent_ds_init();
41                 if (SYNC_AGENT_DS_SUCCESS!=ds_err) {
42                    LoggerE("Failed to init oma ds.");
43                 }
44         }
45     m_instanceCount++;
46 }
47
48 DataSyncManager::~DataSyncManager()
49 {
50         LoggerI("Deinitialize the datasync manager with count: "<<m_instanceCount);
51
52     m_instanceCount--;
53     if (0==m_instanceCount) {
54                 sync_agent_ds_error_e ds_err = sync_agent_ds_deinit();
55                 if (SYNC_AGENT_DS_SUCCESS!=ds_err) {
56                    LoggerE("Failed to deinit oma ds.");
57                 }
58         }
59 }
60
61 static int datasync_state_changed_cb(sync_agent_event_data_s* request, void *user_data)
62 {
63     LoggerD("DataSync session state changed.");
64
65         char *profileDirName = NULL;
66         int sync_type = 0;
67         char *progress = NULL;
68         char *error = NULL;
69
70     Try
71     {
72         OnDataSyncStateChangedPtr eventPtr(new OnDataSyncStateChanged());
73         DataSyncManager* thisDataSyncManager = (DataSyncManager*) user_data;
74
75                 LoggerD("Get state info.");
76                 sync_agent_get_event_data_param(request, &profileDirName);
77                 sync_agent_get_event_data_param(request, &sync_type);
78                 sync_agent_get_event_data_param(request, &progress);
79                 sync_agent_get_event_data_param(request, &error);
80
81                 LoggerI("profileDirName: "<<profileDirName<<", sync_type: "<<sync_type<<", progress: "<<progress<<", error: "<<error);
82
83                 if(profileDirName) {
84                 eventPtr->setProfileId(profileDirName);
85                         if(4<strnlen(profileDirName, 5)) {
86                         eventPtr->setProfileId(eventPtr->getProfileId().substr(4));
87                         }
88                 }
89
90                 if(NULL==progress) {
91                         LoggerW("Null status.");
92                         eventPtr->setSessionStatus(OnDataSyncStateChanged::UNDEFINED_STATUS);
93                 } else if(0==strncmp(progress, "DONE", 4)) {
94                         eventPtr->setSessionStatus(OnDataSyncStateChanged::COMPLETE_STATUS);
95                 } else if(0==strncmp(progress, "CANCEL", 6)) {
96                         eventPtr->setSessionStatus(OnDataSyncStateChanged::STOP_STATUS);
97                 } else if(0==strncmp(progress, "ERROR", 5)) {
98                         // Error cases should be redefined and transferred based on the error value.
99                         eventPtr->setSessionStatus(OnDataSyncStateChanged::FAIL_STATUS);
100                 } else {
101                         LoggerI("Undefined status: "<<progress);
102                         eventPtr->setSessionStatus(OnDataSyncStateChanged::UNDEFINED_STATUS);
103                 }
104
105         eventPtr->setResult(true);
106
107                 if(profileDirName) {
108                 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
109                     thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
110                 }
111                 }
112     }
113     Catch (Exception)
114     {
115         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
116     }
117
118         if(profileDirName) {
119                 g_free(profileDirName);
120         }
121         if(progress) {
122                 g_free(progress);
123         }
124         if(error) {
125                 g_free(error);
126         }
127
128         if (request != NULL) {
129                 if (request->size != NULL) {
130                         g_free(request->size);
131                 }
132                 g_free(request);
133         }
134
135         return 0;
136 }
137
138 static int datasync_progress_cb(sync_agent_event_data_s* request, void *user_data)
139 {
140     LoggerD("DataSync progress called.");
141
142         char *profileDirName = NULL;
143         int syncType = 0;
144         int uri;
145         char *progressStatus = NULL;
146         char *operationType = NULL;
147
148     Try
149     {
150         OnDataSyncStateChangedPtr eventPtr(new OnDataSyncStateChanged());
151         DataSyncManager* thisDataSyncManager = (DataSyncManager*) user_data;
152
153                 int isFromServer, totalPerOperation, syncedPerOperation, totalPerDb, syncedPerDb;
154
155                 LoggerD("Get progress info.");
156                 sync_agent_get_event_data_param(request, &profileDirName);
157                 sync_agent_get_event_data_param(request, &syncType);
158                 sync_agent_get_event_data_param(request, &uri);
159                 sync_agent_get_event_data_param(request, &progressStatus);
160                 sync_agent_get_event_data_param(request, &operationType);
161
162                 LoggerI("profileDirName: "<<profileDirName<<", syncType: "<<syncType<<", uri: "<<uri<<", progressStatus: "<<progressStatus<<", operationType "<<operationType);
163
164                 sync_agent_get_event_data_param(request, &isFromServer);
165                 sync_agent_get_event_data_param(request, &totalPerOperation);
166                 sync_agent_get_event_data_param(request, &syncedPerOperation);
167                 sync_agent_get_event_data_param(request, &totalPerDb);
168                 sync_agent_get_event_data_param(request, &syncedPerDb);
169
170                 LoggerI("isFromServer: "<<isFromServer<<", totalPerOperation: "<<totalPerOperation<<", syncedPerOperation: "<<syncedPerOperation<<", totalPerDb: "<<totalPerDb<<", syncedPerDb "<<syncedPerDb);
171
172                 if(profileDirName) {
173                 eventPtr->setProfileId(profileDirName);
174                         if(4<strnlen(profileDirName, 5)) {
175                         eventPtr->setProfileId(eventPtr->getProfileId().substr(4));
176                         }
177                 }
178
179                 eventPtr->setSessionStatus(OnDataSyncStateChanged::PROGRESS_STATUS);
180
181                 if(SYNC_AGENT_SRC_URI_CONTACT==uri) {
182                 eventPtr->setServiceType(SyncServiceInfo::CONTACT_SERVICE_TYPE);
183                 } else if(SYNC_AGENT_SRC_URI_CALENDAR==uri) {
184                 eventPtr->setServiceType(SyncServiceInfo::EVENT_SERVICE_TYPE);
185                 } else {
186                         LoggerW("Wrong service type.");
187                 eventPtr->setServiceType(SyncServiceInfo::UNDEFINED_SERVICE_TYPE);
188                 }
189
190         eventPtr->setIsFromServer(isFromServer);
191                 eventPtr->setSynedPerService(syncedPerDb);
192                 eventPtr->setTotalPerService(totalPerDb);
193
194         eventPtr->setResult(true);
195
196                 if(profileDirName) {
197                 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
198                     thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
199                 }
200                 }
201     }
202     Catch (Exception)
203     {
204         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
205     }
206
207         if(profileDirName) {
208                 g_free(profileDirName);
209         }
210         if(progressStatus) {
211                 g_free(progressStatus);
212         }
213         if(operationType) {
214                 g_free(operationType);
215         }
216
217         if (request != NULL) {
218                 if (request->size != NULL) {
219                         g_free(request->size);
220                 }
221                 g_free(request);
222         }
223
224         return 0;
225 }
226
227 static sync_agent_ds_sync_mode_e convertToPlatformSyncMode(SyncInfo::SyncMode syncMode)
228 {
229         if(SyncInfo::MANUAL_MODE==syncMode) {
230                 return SYNC_AGENT_SYNC_MODE_MANUAL;
231         } else if(SyncInfo::PERIODIC_MODE==syncMode) {
232                 return SYNC_AGENT_SYNC_MODE_PERIODIC;
233         } else if(SyncInfo::PUSH_MODE==syncMode) {
234                 return SYNC_AGENT_SYNC_MODE_PUSH;
235         } else {
236                 LoggerW("Error while converting a sync mode.");
237         }
238
239         return SYNC_AGENT_SYNC_MODE_MANUAL;
240 }
241
242 static SyncInfo::SyncMode convertToSyncMode(sync_agent_ds_sync_mode_e syncMode)
243 {
244         if(SYNC_AGENT_SYNC_MODE_MANUAL==syncMode) {
245                 return SyncInfo::MANUAL_MODE;
246         } else if(SYNC_AGENT_SYNC_MODE_PERIODIC==syncMode) {
247                 return SyncInfo::PERIODIC_MODE;
248         } else if(SYNC_AGENT_SYNC_MODE_PUSH==syncMode) {
249                 return SyncInfo::PUSH_MODE;
250         } else {
251                 LoggerW("Error while converting a sync mode.");
252         }
253
254         return SyncInfo::UNDEFINED_MODE;
255 }
256
257 static sync_agent_ds_sync_type_e convertToPlatformSyncType(SyncInfo::SyncType syncType)
258 {
259         if(SyncInfo::TWO_WAY_TYPE==syncType) {
260                 return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
261         } else if(SyncInfo::SLOW_TYPE==syncType) {
262                 return SYNC_AGENT_SYNC_TYPE_FULL_SYNC;
263         } else if(SyncInfo::ONE_WAY_FROM_CLIENT_TYPE==syncType) {
264                 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER;
265         } else if(SyncInfo::REFRESH_FROM_CLIENT_TYPE==syncType) {
266                 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE;
267         } else if(SyncInfo::ONE_WAY_FROM_SERVER_TYPE==syncType) {
268                 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE;
269         } else if(SyncInfo::REFRESH_FROM_SERVER_TYPE==syncType) {
270                 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER;
271         } else {
272                 LoggerW("Error while converting a sync type.");
273         }
274
275         return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
276 }
277
278 static SyncInfo::SyncType convertToSyncType(sync_agent_ds_sync_type_e syncType)
279 {
280         if(SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH==syncType) {
281                 return SyncInfo::TWO_WAY_TYPE;
282         } else if(SYNC_AGENT_SYNC_TYPE_FULL_SYNC==syncType) {
283                 return SyncInfo::SLOW_TYPE;
284         } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER==syncType) {
285                 return SyncInfo::ONE_WAY_FROM_CLIENT_TYPE;
286         } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE==syncType) {
287                 return SyncInfo::REFRESH_FROM_CLIENT_TYPE;
288         } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE==syncType) {
289                 return SyncInfo::ONE_WAY_FROM_SERVER_TYPE;
290         } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER==syncType) {
291                 return SyncInfo::REFRESH_FROM_SERVER_TYPE;
292         } else {
293                 LoggerW("Error while converting a sync type.");
294         }
295
296         return SyncInfo::UNDEFINED_TYPE;
297 }
298
299 static sync_agent_ds_sync_interval_e convertToPlatformSyncInterval(SyncInfo::SyncInterval syncInterval)
300 {
301         if(SyncInfo::INTERVAL_5_MINUTES==syncInterval) {
302                 return SYNC_AGENT_SYNC_INTERVAL_5_MINUTES;
303         } else if(SyncInfo::INTERVAL_15_MINUTES==syncInterval) {
304                 return SYNC_AGENT_SYNC_INTERVAL_15_MINUTES;
305         } else if(SyncInfo::INTERVAL_1_HOUR==syncInterval) {
306                 return SYNC_AGENT_SYNC_INTERVAL_1_HOUR;
307         } else if(SyncInfo::INTERVAL_4_HOURS==syncInterval) {
308                 return SYNC_AGENT_SYNC_INTERVAL_4_HOURS;
309         } else if(SyncInfo::INTERVAL_12_HOURS==syncInterval) {
310                 return SYNC_AGENT_SYNC_INTERVAL_12_HOURS;
311         } else if(SyncInfo::INTERVAL_1_DAY==syncInterval) {
312                 return SYNC_AGENT_SYNC_INTERVAL_1_DAY;
313         } else if(SyncInfo::INTERVAL_1_WEEK==syncInterval) {
314                 return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
315         } else if(SyncInfo::INTERVAL_1_MONTH==syncInterval) {
316                 return SYNC_AGENT_SYNC_INTERVAL_1_MONTH;
317         } else if(SyncInfo::INTERVAL_UNDEFINED==syncInterval) {
318                 return SYNC_AGENT_SYNC_INTERVAL_NONE;
319         } else {
320                 LoggerW("Error while converting a JS sync interval.");
321         }
322
323         return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
324 }
325
326 static SyncInfo::SyncInterval convertToSyncInterval(sync_agent_ds_sync_interval_e syncInterval)
327 {
328         if(SYNC_AGENT_SYNC_INTERVAL_5_MINUTES==syncInterval) {
329                 return SyncInfo::INTERVAL_5_MINUTES;
330         } else if(SYNC_AGENT_SYNC_INTERVAL_15_MINUTES==syncInterval) {
331                 return SyncInfo::INTERVAL_15_MINUTES;
332         } else if(SYNC_AGENT_SYNC_INTERVAL_1_HOUR==syncInterval) {
333                 return SyncInfo::INTERVAL_1_HOUR;
334         } else if(SYNC_AGENT_SYNC_INTERVAL_4_HOURS==syncInterval) {
335                 return SyncInfo::INTERVAL_4_HOURS;
336         } else if(SYNC_AGENT_SYNC_INTERVAL_12_HOURS==syncInterval) {
337                 return SyncInfo::INTERVAL_12_HOURS;
338         } else if(SYNC_AGENT_SYNC_INTERVAL_1_DAY==syncInterval) {
339                 return SyncInfo::INTERVAL_1_DAY;
340         } else if(SYNC_AGENT_SYNC_INTERVAL_1_WEEK==syncInterval) {
341                 return SyncInfo::INTERVAL_1_WEEK;
342         } else if(SYNC_AGENT_SYNC_INTERVAL_1_MONTH==syncInterval) {
343                 return SyncInfo::INTERVAL_1_MONTH;
344         } else if(SYNC_AGENT_SYNC_INTERVAL_NONE==syncInterval) {
345                 return SyncInfo::INTERVAL_UNDEFINED;
346         } else {
347                 LoggerW("Error while converting a platform sync interval.");
348         }
349
350         return SyncInfo::INTERVAL_UNDEFINED;
351 }
352
353 static sync_agent_ds_service_type_e convertToPlatformSyncServiceType(SyncServiceInfo::SyncServiceType serviceType)
354 {
355         if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
356                 return SYNC_AGENT_CONTACT;
357         } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
358                 return SYNC_AGENT_CALENDAR;
359         } else {
360                 LoggerW("Error while converting a sync service type.");
361         }
362
363         return SYNC_AGENT_CONTACT;
364 }
365
366 static SyncServiceInfo::SyncServiceType convertToSyncServiceType(sync_agent_ds_service_type_e serviceType)
367 {
368         if(SYNC_AGENT_CONTACT==serviceType) {
369                 return SyncServiceInfo::CONTACT_SERVICE_TYPE;
370         } else if(SYNC_AGENT_CALENDAR==serviceType) {
371                 return SyncServiceInfo::EVENT_SERVICE_TYPE;
372         } else {
373                 LoggerW("Error while converting a sync service type.");
374         }
375
376         return SyncServiceInfo::UNDEFINED_SERVICE_TYPE;
377 }
378
379 static sync_agent_ds_src_uri_e convertToPlatformSourceUri(SyncServiceInfo::SyncServiceType serviceType)
380 {
381         if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
382                 return SYNC_AGENT_SRC_URI_CONTACT;
383         } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
384                 return SYNC_AGENT_SRC_URI_CALENDAR;
385         } else {
386                 LoggerW("Error while converting a sync service.");
387         }
388
389         return SYNC_AGENT_SRC_URI_CONTACT;
390 }
391
392 static SyncStatistics::SyncStatus convertToSyncStatus(char* status)
393 {
394         if(0==strncmp(status, "success", 7)) {
395                 return SyncStatistics::SUCCESS_STATUS;
396         } else if(0==strncmp(status, "stop", 4)) {
397                 return SyncStatistics::STOP_STATUS;
398         } else if(0==strncmp(status, "fail", 4)) {
399                 return SyncStatistics::FAIL_STATUS;
400         } else if(0==strncmp(status, "No", 2)) {
401                 return SyncStatistics::NONE_STATUS;
402         } else {
403                 LoggerW("Error while converting a sync status.");
404         }
405
406         return SyncStatistics::NONE_STATUS;
407 }
408
409 void DataSyncManager::OnRequestReceived(const IEventAddProfilePtr &event)
410 {
411         ds_profile_h profile_h = NULL;
412
413     Try
414     {
415         // Check if the quota is full first.
416                 GList *profile_list = NULL;
417                 GList *iter = NULL;
418
419         sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
420
421                 ret = sync_agent_ds_get_all_profile(&profile_list);
422                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
423                         ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
424                 }
425
426                 int numProfiles = g_list_length(profile_list);
427                 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
428                    sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
429                 }
430                 if(profile_list) {
431                         g_list_free(profile_list);
432                 }
433                 LoggerD("numProfiles: "<<numProfiles);
434                 if(MAX_PROFILES_NUM==numProfiles) {
435                         ThrowMsg(OutOfRangeException, "There are already maximum number of profiles!");
436                 }
437
438         SyncProfileInfoPtr profile;
439         profile = event->getProfile();
440         if (!profile) {
441             ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
442         }
443
444                 ret = sync_agent_ds_create_profile_info(&profile_h);
445                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
446                         ThrowMsg(PlatformException, "Platform error while creating a profile: "<<ret);
447                 }
448
449                 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
450                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
451                         ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
452                 }
453
454                 ret = sync_agent_ds_set_server_info(profile_h, (char*)(profile->getSyncInfo()->getUrl().c_str()),  (char*)(profile->getSyncInfo()->getId().c_str()),  (char*)(profile->getSyncInfo()->getPassword().c_str()));
455                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
456                         ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
457                 }
458
459                 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
460                 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
461                 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
462                 LoggerD("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
463
464                 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
465                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
466                         ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
467                 }
468
469                 // Set the sync categories.
470                 SyncServiceInfoListPtr categories = profile->getServiceInfo();
471                 sync_agent_ds_service_type_e serviceType;
472                 sync_agent_ds_src_uri_e srcURI;
473                 std::string tgtURI, id, password;
474                 bool enable;
475                 for(unsigned int i=0; categories->size()>i; i++) {
476                         serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
477                         tgtURI = categories->at(i)->getServerDatabaseUri();
478                         srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
479                         id = categories->at(i)->getId();
480                         password = categories->at(i)->getPassword();
481                         enable = categories->at(i)->getEnable();
482
483                         LoggerI("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<", enable: "<<enable<<" for index: "<<i);
484
485                         ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
486                                         0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
487                         if (SYNC_AGENT_DS_SUCCESS!=ret) {
488                                 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
489                         }
490                 }
491
492                 int profileId;
493                 ret = sync_agent_ds_add_profile(profile_h, &profileId);
494                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
495                         ThrowMsg(PlatformException, "Platform error while adding a profile: "<<ret);
496                 }
497
498         LoggerD("profileId from platform: "<<profileId);
499
500                 char* profileName = NULL;
501                 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
502                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
503                         ThrowMsg(PlatformException, "Platform error while getting a profile name: "<<ret);
504                 }
505
506         LoggerD("profileName: "<<profileName<<", profileId: "<<profileId);
507
508                 std::stringstream ss;
509                 ss<<profileId;
510                 profile->setProfileId(ss.str());
511
512                 if(profileName) {
513                         free(profileName);
514                 }
515
516         event->setResult(true);
517     }
518     Catch(OutOfRangeException)
519     {
520                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
521         event->setResult(false);
522         event->setExceptionCode(ExceptionCodes::OutOfRangeException);
523     }
524     Catch(Exception)
525     {
526                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
527         event->setResult(false);
528         event->setExceptionCode(ExceptionCodes::PlatformException);
529     }
530
531         if(profile_h) {
532                 sync_agent_ds_free_profile_info(profile_h);
533         }
534 }
535
536 void DataSyncManager::OnRequestReceived(const IEventUpdateProfilePtr &event)
537 {
538         ds_profile_h profile_h = NULL;
539
540     Try
541     {
542         SyncProfileInfoPtr profile;
543         profile = event->getProfile();
544         if (!profile) {
545             ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
546         }
547
548         sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
549
550                 int profileId = -1;
551                 std::stringstream ss(profile->getProfileId());
552                 ss>>profileId;
553         LoggerD("profileId: "<<profileId);
554                 ret = sync_agent_ds_get_profile(profileId, &profile_h);
555                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
556                         ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
557                 }
558
559                 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
560                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
561                         ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
562                 }
563
564                 ret = sync_agent_ds_set_server_info(profile_h, (char*)(profile->getSyncInfo()->getUrl().c_str()),  (char*)(profile->getSyncInfo()->getId().c_str()),  (char*)(profile->getSyncInfo()->getPassword().c_str()));
565                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
566                         ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
567                 }
568
569                 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
570                 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
571                 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
572                 LoggerD("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
573
574                 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
575                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
576                         ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
577                 }
578
579                 // Set the sync categories.
580                 SyncServiceInfoListPtr categories = profile->getServiceInfo();
581                 sync_agent_ds_service_type_e serviceType;
582                 sync_agent_ds_src_uri_e srcURI;
583                 std::string tgtURI, id, password;
584                 bool enable;
585                 for(unsigned int i=0; categories->size()<i; i++) {
586                         serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
587                         tgtURI = categories->at(i)->getServerDatabaseUri();
588                         srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
589                         id = categories->at(i)->getId();
590                         password = categories->at(i)->getPassword();
591                         enable = categories->at(i)->getEnable();
592
593                         LoggerD("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<" for index: "<<i);
594
595                         ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
596                                         0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
597                         if (SYNC_AGENT_DS_SUCCESS!=ret) {
598                                 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
599                         }
600                 }
601
602                 ret = sync_agent_ds_update_profile(profile_h);
603                 if (SYNC_AGENT_DS_SUCCESS!=ret && SYNC_AGENT_DS_SYNCHRONISING !=ret) {
604                         ThrowMsg(NotFoundException, "Platform error while updating a profile: "<<ret);
605                 }
606
607         event->setResult(true);
608     }
609     Catch(NotFoundException)
610     {
611         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
612         event->setResult(false);
613         event->setExceptionCode(ExceptionCodes::NotFoundException);
614     }
615     Catch(Exception)
616     {
617                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
618         event->setResult(false);
619         event->setExceptionCode(ExceptionCodes::PlatformException);
620     }
621
622         if(profile_h) {
623                 sync_agent_ds_free_profile_info(profile_h);
624         }
625 }
626
627 void DataSyncManager::OnRequestReceived(const IEventRemoveProfilePtr &event)
628 {
629         ds_profile_h profile_h = NULL;
630
631     Try
632     {
633         sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
634
635                 int profileId = -1;
636                 std::stringstream ss(event->getProfileId());
637                 ss>>profileId;
638         LoggerD("profileId: "<<profileId);
639                 ret = sync_agent_ds_get_profile(profileId, &profile_h);
640                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
641                         ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
642                 }
643
644                 ret = sync_agent_ds_delete_profile(profile_h);
645                 if (SYNC_AGENT_DS_SUCCESS!=ret && SYNC_AGENT_DS_SYNCHRONISING !=ret) {
646                         ThrowMsg(PlatformException, "Platform error while deleting a profile: "<<ret);
647                 }
648
649         event->setResult(true);
650     }
651     Catch(NotFoundException)
652     {
653         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
654         event->setResult(false);
655         event->setExceptionCode(ExceptionCodes::NotFoundException);
656     }
657     Catch(Exception)
658     {
659                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
660         event->setResult(false);
661         event->setExceptionCode(ExceptionCodes::PlatformException);
662     }
663
664         if(profile_h) {
665                 sync_agent_ds_free_profile_info(profile_h);
666         }
667 }
668
669 void DataSyncManager::OnRequestReceived(const IEventGetMaxProfilesNumPtr &event)
670 {
671     Try
672     {
673         LoggerD("Return maximum number of supported profiles: "<<MAX_PROFILES_NUM);
674
675                 event->setNumMaxProfiles(MAX_PROFILES_NUM);
676
677         event->setResult(true);
678     }
679     Catch(Exception)
680     {
681                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
682         event->setResult(false);
683         event->setExceptionCode(ExceptionCodes::PlatformException);
684     }
685 }
686
687 void DataSyncManager::OnRequestReceived(const IEventGetProfilesNumPtr &event)
688 {
689         GList *profile_list = NULL;
690         GList *iter = NULL;
691
692     Try
693     {
694         sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
695
696                 ret = sync_agent_ds_get_all_profile(&profile_list);
697                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
698                         ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
699                 }
700
701                 int numProfiles=0;
702                 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
703                    sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
704                    numProfiles++;
705                    LoggerD("Free sync_agent_ds_profile_info for index: "<<numProfiles);
706                 }
707
708                 LoggerD("numProfiles: "<<numProfiles);
709
710                 event->setNumProfiles(numProfiles);
711
712         event->setResult(true);
713     }
714     Catch(Exception)
715     {
716                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
717         event->setResult(false);
718         event->setExceptionCode(ExceptionCodes::PlatformException);
719     }
720 }
721
722 void DataSyncManager::OnRequestReceived(const IEventGetProfilePtr &event)
723 {
724         ds_profile_h profile_h = NULL;
725
726     Try
727     {
728         sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
729
730                 int profileId = -1;
731                 std::stringstream ss(event->getProfileId());
732                 ss>>profileId;
733         LoggerD("profileId: "<<profileId);
734                 ret = sync_agent_ds_get_profile(profileId, &profile_h);
735                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
736                         ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
737                 }
738
739                 SyncProfileInfoPtr profile( new SyncProfileInfo() );
740
741                 profile->setProfileId(event->getProfileId());
742
743                 char *profileName = NULL;
744                 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
745                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
746                         ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
747                 }
748                 profile->setProfileName(profileName);
749
750                 sync_agent_ds_server_info server_info = { NULL };
751                 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
752                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
753                         ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
754                 }
755                 profile->getSyncInfo()->setUrl(server_info.addr);
756                 profile->getSyncInfo()->setId(server_info.id);
757                 profile->getSyncInfo()->setPassword(server_info.password);
758
759                 sync_agent_ds_sync_info sync_info;
760                 ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
761                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
762                         ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
763                 }
764                 profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
765                 profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
766                 profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
767
768                 LoggerD("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
769
770                 GList *category_list = NULL;
771                 sync_agent_ds_service_info *category_info = NULL;
772                 ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
773                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
774                         ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
775                 }
776                 int category_count = g_list_length(category_list);
777                 LoggerD("category_count: "<<category_count);
778                 while(category_count--) {
779                         category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
780                         if(SYNC_AGENT_CALENDAR<category_info->service_type) {
781                                 LoggerD("Skip unsupported sync service type: "<<category_info->service_type);
782                                 continue;
783                         }
784
785                         SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
786                         serviceInfo->setEnable(category_info->enabled);
787                         if(category_info->id) {
788                                 serviceInfo->setId(category_info->id);
789                         }
790                         if(category_info->password) {
791                                 serviceInfo->setPassword(category_info->password);
792                         }
793                         serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
794                         if(category_info->tgt_uri) {
795                                 serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
796                         }
797
798                         LoggerD("Service type: "<<serviceInfo->getSyncServiceType());
799                         profile->getServiceInfo()->push_back(serviceInfo);
800                 }
801                 if(category_list) {
802                         g_list_free(category_list);
803                 }
804
805                 event->setProfile(profile);
806
807         event->setResult(true);
808     }
809     Catch(NotFoundException)
810     {
811         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
812         event->setResult(false);
813         event->setExceptionCode(ExceptionCodes::NotFoundException);
814     }
815     Catch(Exception)
816     {
817                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
818         event->setResult(false);
819         event->setExceptionCode(ExceptionCodes::PlatformException);
820     }
821
822         if(profile_h) {
823                 sync_agent_ds_free_profile_info(profile_h);
824         }
825 }
826
827 void DataSyncManager::OnRequestReceived(const IEventGetAllProfilesPtr &event)
828 {
829         GList *profile_list = NULL;
830         GList *iter = NULL;
831
832     Try
833     {
834                 ds_profile_h profile_h = NULL;
835         sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
836
837                 ret = sync_agent_ds_get_all_profile(&profile_list);
838                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
839                         ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
840                 }
841
842                 LoggerD("Number of profiles: "<< g_list_length(profile_list));
843                 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
844                         profile_h = (ds_profile_h) iter->data;
845                         SyncProfileInfoPtr profile( new SyncProfileInfo() );
846
847                         int profileId;
848                         ret = sync_agent_ds_get_profile_id(profile_h, &profileId);
849                         if (SYNC_AGENT_DS_SUCCESS!=ret) {
850                                 ThrowMsg(PlatformException, "Platform error while gettting a profile id: "<<ret);
851                         }
852
853                         std::stringstream ss;
854                         ss<<profileId;
855                         profile->setProfileId(ss.str());
856
857                         LoggerD("Processing a profile with id: "<<profile->getProfileId());
858
859                         char *profileName = NULL;
860                         ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
861                         if (SYNC_AGENT_DS_SUCCESS!=ret) {
862                                 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
863                         }
864                         profile->setProfileName(profileName);
865
866                         sync_agent_ds_server_info server_info = { NULL };
867                         ret = sync_agent_ds_get_server_info(profile_h, &server_info);
868                         if (SYNC_AGENT_DS_SUCCESS!=ret) {
869                                 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
870                         }
871                         profile->getSyncInfo()->setUrl(server_info.addr);
872                         profile->getSyncInfo()->setId(server_info.id);
873                         profile->getSyncInfo()->setPassword(server_info.password);
874
875                         sync_agent_ds_sync_info sync_info;
876                         ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
877                         if (SYNC_AGENT_DS_SUCCESS!=ret) {
878                                 ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
879                         }
880                         profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
881                         profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
882                         profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
883
884                         LoggerD("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
885
886                         GList *category_list = NULL;
887                         sync_agent_ds_service_info *category_info = NULL;
888                         ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
889                         if (SYNC_AGENT_DS_SUCCESS!=ret) {
890                                 ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
891                         }
892                         int category_count = g_list_length(category_list);
893                         LoggerD("category_count: "<<category_count);
894                         while(category_count--) {
895                                 category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
896                                 if(SYNC_AGENT_CALENDAR<category_info->service_type) {
897                                         LoggerD("Skip unsupported sync service type: "<<category_info->service_type);
898                                         continue;
899                                 }
900
901                                 SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
902                                 serviceInfo->setEnable(category_info->enabled);
903                                 if(category_info->id) {
904                                         serviceInfo->setId(category_info->id);
905                                 }
906                                 if(category_info->password) {
907                                         serviceInfo->setPassword(category_info->password);
908                                 }
909                                 serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
910                                 if(category_info->tgt_uri) {
911                                         serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
912                                 }
913
914                                 LoggerD("Service type: "<<serviceInfo->getSyncServiceType());
915                                 profile->getServiceInfo()->push_back(serviceInfo);
916                         }
917                         if(category_list) {
918                                 g_list_free(category_list);
919                         }
920
921                         LoggerD("Adding a profile to the list.");
922                         event->getProfiles()->push_back(profile);
923                 }
924
925         event->setResult(true);
926     }
927     Catch(Exception)
928     {
929                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
930         event->setResult(false);
931         event->setExceptionCode(ExceptionCodes::PlatformException);
932     }
933
934         LoggerD("Free profiles list.");
935         for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
936                 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
937         }
938         if(profile_list) {
939                 g_list_free(profile_list);
940         }
941 }
942
943 void DataSyncManager::OnRequestReceived(const IEventStartSyncPtr &event)
944 {
945         ds_profile_h profile_h = NULL;
946
947     Try
948     {
949         sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
950                 sync_agent_event_error_e err= SYNC_AGENT_EVENT_FAIL;
951
952                 int profileId = -1;
953                 std::stringstream ss(event->getProfileId());
954                 ss>>profileId;
955         LoggerD("profileId: "<<profileId);
956                 ret = sync_agent_ds_get_profile(profileId, &profile_h);
957                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
958                         ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
959                 }
960
961         err = sync_agent_set_noti_callback(1, datasync_state_changed_cb, this);
962         if (SYNC_AGENT_EVENT_SUCCESS != err) {
963             ThrowMsg(PlatformException, "Platform error while setting state changed cb: " << err);
964         }
965
966         err = sync_agent_set_noti_callback(2, datasync_progress_cb, this);
967         if (SYNC_AGENT_EVENT_SUCCESS != err) {
968             ThrowMsg(PlatformException, "Platform error while setting progress cb: " << err);
969         }
970
971                 ret = sync_agent_ds_start_sync(profile_h);
972                 if (SYNC_AGENT_DS_SUCCESS!=ret && SYNC_AGENT_DS_SYNCHRONISING !=ret) {
973                         ThrowMsg(PlatformException, "Platform error while starting a profile: "<<ret);
974                 }
975
976         if(event->getEmitter()) {
977             LoggerD("Attaching the emitter with profileId: "<<event->getProfileId());
978             m_changeEmitters[std::string("Sync") + event->getProfileId()] = event->getEmitter();
979         }
980
981         event->setResult(true);
982     }
983     Catch(NotFoundException)
984     {
985         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
986         event->setResult(false);
987         event->setExceptionCode(ExceptionCodes::NotFoundException);
988     }
989     Catch(Exception)
990     {
991                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
992         event->setResult(false);
993         event->setExceptionCode(ExceptionCodes::PlatformException);
994     }
995
996         if(profile_h) {
997                 sync_agent_ds_free_profile_info(profile_h);
998         }
999 }
1000
1001 void DataSyncManager::OnRequestReceived(const IEventStopSyncPtr &event)
1002 {
1003         ds_profile_h profile_h = NULL;
1004
1005     Try
1006     {
1007         sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
1008
1009                 int profileId = -1;
1010                 std::stringstream ss(event->getProfileId());
1011                 ss>>profileId;
1012         LoggerD("profileId: "<<profileId);
1013                 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1014                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1015                         ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1016                 }
1017
1018                 ret = sync_agent_ds_stop_sync(profile_h);
1019                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1020                         ThrowMsg(PlatformException, "Platform error while stopping a profile: "<<ret);
1021                 }
1022
1023         event->setResult(true);
1024     }
1025     Catch(NotFoundException)
1026     {
1027         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1028         event->setResult(false);
1029         event->setExceptionCode(ExceptionCodes::NotFoundException);
1030     }
1031     Catch(Exception)
1032     {
1033                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1034         event->setResult(false);
1035         event->setExceptionCode(ExceptionCodes::PlatformException);
1036     }
1037
1038         if(profile_h) {
1039                 sync_agent_ds_free_profile_info(profile_h);
1040         }
1041
1042 }
1043
1044 void DataSyncManager::OnRequestReceived(const IEventGetLastSyncStatisticsPtr &event)
1045 {
1046         ds_profile_h profile_h = NULL;
1047
1048     Try
1049     {
1050         sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
1051
1052                 int profileId = -1;
1053                 std::stringstream ss(event->getProfileId());
1054                 ss>>profileId;
1055         LoggerD("profileId: "<<profileId);
1056                 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1057                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1058                         ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1059                 }
1060
1061                 SyncStatisticsListPtr statisticsList( new SyncStatisticsList() );
1062
1063                 GList *statistics_list = NULL;
1064                 ret = sync_agent_ds_get_sync_statistics(profile_h, &statistics_list);
1065                 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1066                         ThrowMsg(PlatformException, "Platform error while gettting sync statistics: "<<ret);
1067                 }
1068
1069                 int statistics_count = g_list_length(statistics_list);
1070                 LoggerD("statistics_count: "<<statistics_count);
1071                 sync_agent_ds_statistics_info *statistics = NULL;
1072                 for (int i = 0; i < statistics_count; i++) {
1073                         statistics = (sync_agent_ds_statistics_info *) g_list_nth_data(statistics_list, i);
1074
1075                         SyncStatisticsPtr statisticsPtr( new SyncStatistics() );
1076
1077                         if(0==i) {
1078                                 LoggerD("Statistics for contact.");
1079                                 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CONTACT));
1080                         } else if(1==i) {
1081                                 LoggerD("Statistics for event.");
1082                                 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CALENDAR));
1083                         } else {
1084                                 LoggerW("Unsupported category for statistics: "<<i);
1085                                 continue;
1086                         }
1087
1088                         LoggerD("dbsynced: "<<statistics->dbsynced);
1089                         if (statistics->dbsynced) {
1090                                 statisticsPtr->setSyncStatus(convertToSyncStatus(statistics->dbsynced));
1091                                 statisticsPtr->setClientToServerTotal(statistics->client2server_total);
1092                                 statisticsPtr->setClientToServerAdded(statistics->client2server_nrofadd);
1093                                 statisticsPtr->setClientToServerUpdated(statistics->client2server_nrofreplace);
1094                                 statisticsPtr->setClientToServerRemoved(statistics->client2server_nrofdelete);
1095                                 statisticsPtr->setServerToClientTotal(statistics->server2client_total);
1096                                 statisticsPtr->setServerToClientAdded(statistics->server2client_nrofadd);
1097                                 statisticsPtr->setServerToClientUpdated(statistics->server2client_nrofreplace);
1098                                 statisticsPtr->setServerToClientRemoved(statistics->server2client_nrofdelete);
1099
1100                                 statisticsPtr->setLastSyncTime((long long int) (statistics->last_session_time));
1101                         }
1102
1103                         LoggerD("ClientToServerTotal: "<<statisticsPtr->getClientToServerTotal()<<", ServerToClientTotal: "<<statisticsPtr->getServerToClientTotal());
1104
1105                         statisticsList->push_back(statisticsPtr);
1106                 }
1107                 if(statistics_list) {
1108                         g_list_free(statistics_list);
1109                 }
1110
1111                 event->setSyncStatictics(statisticsList);
1112
1113         event->setResult(true);
1114     }
1115     Catch(NotFoundException)
1116     {
1117         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1118         event->setResult(false);
1119         event->setExceptionCode(ExceptionCodes::NotFoundException);
1120     }
1121     Catch(Exception)
1122     {
1123                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1124         event->setResult(false);
1125         event->setExceptionCode(ExceptionCodes::PlatformException);
1126     }
1127
1128         if(profile_h) {
1129                 sync_agent_ds_free_profile_info(profile_h);
1130         }
1131 }
1132
1133 }
1134 }