2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "DataSyncManager.h"
21 #include "SyncServiceInfo.h"
22 #include "SyncProfileInfo.h"
24 #include <sync_agent.h>
28 using namespace WrtDeviceApis::Commons;
33 int DataSyncManager::m_instanceCount = 0;
35 DataSyncManager::DataSyncManager()
37 LoggerI("Initialize the datasync manager with count: "<<m_instanceCount);
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.");
48 DataSyncManager::~DataSyncManager()
50 LoggerI("Deinitialize the datasync manager with count: "<<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.");
61 static int datasync_state_changed_cb(sync_agent_event_data_s* request, void *user_data)
63 LoggerD("DataSync session state changed.");
65 char *profileDirName = NULL;
67 char *progress = NULL;
72 OnDataSyncStateChangedPtr eventPtr(new OnDataSyncStateChanged());
73 DataSyncManager* thisDataSyncManager = (DataSyncManager*) user_data;
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);
81 LoggerD("profileDirName: "<<profileDirName<<", sync_type: "<<sync_type<<", progress: "<<progress<<", error: "<<error);
84 eventPtr->setProfileId(profileDirName);
88 LoggerW("Null status.");
89 eventPtr->setSessionStatus(OnDataSyncStateChanged::UNDEFINED_STATUS);
90 } else if(0==strcmp(progress, "DONE")) {
91 eventPtr->setSessionStatus(OnDataSyncStateChanged::COMPLETE_STATUS);
92 } else if(0==strcmp(progress, "CANCEL")) {
93 eventPtr->setSessionStatus(OnDataSyncStateChanged::STOP_STATUS);
94 } else if(0==strcmp(progress, "ERROR")) {
95 // Error cases should be redefined and transferred based on the error value.
96 eventPtr->setSessionStatus(OnDataSyncStateChanged::FAIL_STATUS);
98 LoggerW("Wrong status.");
99 eventPtr->setSessionStatus(OnDataSyncStateChanged::UNDEFINED_STATUS);
102 eventPtr->setResult(true);
105 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
106 thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
112 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
116 g_free(profileDirName);
125 if (request != NULL) {
126 if (request->size != NULL) {
127 g_free(request->size);
135 static int datasync_progress_cb(sync_agent_event_data_s* request, void *user_data)
137 LoggerD("DataSync progress called.");
139 char *profileDirName = NULL;
142 char *progressStatus = NULL;
143 char *operationType = NULL;
147 OnDataSyncStateChangedPtr eventPtr(new OnDataSyncStateChanged());
148 DataSyncManager* thisDataSyncManager = (DataSyncManager*) user_data;
150 int isFromServer, totalPerOperation, syncedPerOperation, totalPerDb, syncedPerDb;
152 LoggerD("Get progress info.");
153 sync_agent_get_event_data_param(request, &profileDirName);
154 sync_agent_get_event_data_param(request, &syncType);
155 sync_agent_get_event_data_param(request, &uri);
156 sync_agent_get_event_data_param(request, &progressStatus);
157 sync_agent_get_event_data_param(request, &operationType);
159 LoggerD("profileDirName: "<<profileDirName<<", syncType: "<<syncType<<", uri: "<<uri<<", progressStatus: "<<progressStatus<<", operationType "<<operationType);
161 sync_agent_get_event_data_param(request, &isFromServer);
162 sync_agent_get_event_data_param(request, &totalPerOperation);
163 sync_agent_get_event_data_param(request, &syncedPerOperation);
164 sync_agent_get_event_data_param(request, &totalPerDb);
165 sync_agent_get_event_data_param(request, &syncedPerDb);
167 LoggerD("isFromServer: "<<isFromServer<<", totalPerOperation: "<<totalPerOperation<<", syncedPerOperation: "<<syncedPerOperation<<", totalPerDb: "<<totalPerDb<<", syncedPerDb "<<syncedPerDb);
170 eventPtr->setProfileId(profileDirName);
173 eventPtr->setSessionStatus(OnDataSyncStateChanged::PROGRESS_STATUS);
175 if(SYNC_AGENT_SRC_URI_CONTACT==uri) {
176 eventPtr->setServiceType(SyncServiceInfo::CONTACT_SERVICE_TYPE);
177 } else if(SYNC_AGENT_SRC_URI_CALENDAR==uri) {
178 eventPtr->setServiceType(SyncServiceInfo::EVENT_SERVICE_TYPE);
180 LoggerW("Wrong service type.");
181 eventPtr->setServiceType(SyncServiceInfo::UNDEFINED_SERVICE_TYPE);
184 eventPtr->setIsFromServer(isFromServer);
185 eventPtr->setSynedPerService(syncedPerDb);
186 eventPtr->setTotalPerService(totalPerDb);
188 eventPtr->setResult(true);
191 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
192 thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
198 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
202 g_free(profileDirName);
205 g_free(progressStatus);
208 g_free(operationType);
211 if (request != NULL) {
212 if (request->size != NULL) {
213 g_free(request->size);
221 static sync_agent_ds_sync_mode_e convertToPlatformSyncMode(SyncInfo::SyncMode syncMode)
223 if(SyncInfo::MANUAL_MODE==syncMode) {
224 return SYNC_AGENT_SYNC_MODE_MANUAL;
225 } else if(SyncInfo::PERIODIC_MODE==syncMode) {
226 return SYNC_AGENT_SYNC_MODE_PERIODIC;
227 } else if(SyncInfo::PUSH_MODE==syncMode) {
228 return SYNC_AGENT_SYNC_MODE_PUSH;
230 LoggerW("Error while converting a sync mode.");
233 return SYNC_AGENT_SYNC_MODE_MANUAL;
236 static SyncInfo::SyncMode convertToSyncMode(sync_agent_ds_sync_mode_e syncMode)
238 if(SYNC_AGENT_SYNC_MODE_MANUAL==syncMode) {
239 return SyncInfo::MANUAL_MODE;
240 } else if(SYNC_AGENT_SYNC_MODE_PERIODIC==syncMode) {
241 return SyncInfo::PERIODIC_MODE;
242 } else if(SYNC_AGENT_SYNC_MODE_PUSH==syncMode) {
243 return SyncInfo::PUSH_MODE;
245 LoggerW("Error while converting a sync mode.");
248 return SyncInfo::UNDEFINED_MODE;
251 static sync_agent_ds_sync_type_e convertToPlatformSyncType(SyncInfo::SyncType syncType)
253 if(SyncInfo::TWO_WAY_TYPE==syncType) {
254 return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
255 } else if(SyncInfo::SLOW_TYPE==syncType) {
256 return SYNC_AGENT_SYNC_TYPE_FULL_SYNC;
257 } else if(SyncInfo::ONE_WAY_FROM_CLIENT_TYPE==syncType) {
258 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER;
259 } else if(SyncInfo::REFRESH_FROM_CLIENT_TYPE==syncType) {
260 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE;
261 } else if(SyncInfo::ONE_WAY_FROM_SERVER_TYPE==syncType) {
262 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE;
263 } else if(SyncInfo::REFRESH_FROM_SERVER_TYPE==syncType) {
264 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER;
266 LoggerW("Error while converting a sync type.");
269 return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
272 static SyncInfo::SyncType convertToSyncType(sync_agent_ds_sync_type_e syncType)
274 if(SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH==syncType) {
275 return SyncInfo::TWO_WAY_TYPE;
276 } else if(SYNC_AGENT_SYNC_TYPE_FULL_SYNC==syncType) {
277 return SyncInfo::SLOW_TYPE;
278 } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER==syncType) {
279 return SyncInfo::ONE_WAY_FROM_CLIENT_TYPE;
280 } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE==syncType) {
281 return SyncInfo::REFRESH_FROM_CLIENT_TYPE;
282 } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE==syncType) {
283 return SyncInfo::ONE_WAY_FROM_SERVER_TYPE;
284 } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER==syncType) {
285 return SyncInfo::REFRESH_FROM_SERVER_TYPE;
287 LoggerW("Error while converting a sync type.");
290 return SyncInfo::UNDEFINED_TYPE;
293 static sync_agent_ds_sync_interval_e convertToPlatformSyncInterval(SyncInfo::SyncInterval syncInterval)
295 if(SyncInfo::INTERVAL_5_MINUTES==syncInterval) {
296 return SYNC_AGENT_SYNC_INTERVAL_5_MINUTES;
297 } else if(SyncInfo::INTERVAL_15_MINUTES==syncInterval) {
298 return SYNC_AGENT_SYNC_INTERVAL_15_MINUTES;
299 } else if(SyncInfo::INTERVAL_1_HOUR==syncInterval) {
300 return SYNC_AGENT_SYNC_INTERVAL_1_HOUR;
301 } else if(SyncInfo::INTERVAL_4_HOURS==syncInterval) {
302 return SYNC_AGENT_SYNC_INTERVAL_4_HOURS;
303 } else if(SyncInfo::INTERVAL_12_HOURS==syncInterval) {
304 return SYNC_AGENT_SYNC_INTERVAL_12_HOURS;
305 } else if(SyncInfo::INTERVAL_1_DAY==syncInterval) {
306 return SYNC_AGENT_SYNC_INTERVAL_1_DAY;
307 } else if(SyncInfo::INTERVAL_1_WEEK==syncInterval) {
308 return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
309 } else if(SyncInfo::INTERVAL_1_MONTH==syncInterval) {
310 return SYNC_AGENT_SYNC_INTERVAL_1_MONTH;
312 LoggerW("Error while converting a sync interval.");
315 return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
318 static SyncInfo::SyncInterval convertToSyncInterval(sync_agent_ds_sync_interval_e syncInterval)
320 if(SYNC_AGENT_SYNC_INTERVAL_5_MINUTES==syncInterval) {
321 return SyncInfo::INTERVAL_5_MINUTES;
322 } else if(SYNC_AGENT_SYNC_INTERVAL_15_MINUTES==syncInterval) {
323 return SyncInfo::INTERVAL_15_MINUTES;
324 } else if(SYNC_AGENT_SYNC_INTERVAL_1_HOUR==syncInterval) {
325 return SyncInfo::INTERVAL_1_HOUR;
326 } else if(SYNC_AGENT_SYNC_INTERVAL_4_HOURS==syncInterval) {
327 return SyncInfo::INTERVAL_4_HOURS;
328 } else if(SYNC_AGENT_SYNC_INTERVAL_12_HOURS==syncInterval) {
329 return SyncInfo::INTERVAL_12_HOURS;
330 } else if(SYNC_AGENT_SYNC_INTERVAL_1_DAY==syncInterval) {
331 return SyncInfo::INTERVAL_1_DAY;
332 } else if(SYNC_AGENT_SYNC_INTERVAL_1_WEEK==syncInterval) {
333 return SyncInfo::INTERVAL_1_WEEK;
334 } else if(SYNC_AGENT_SYNC_INTERVAL_1_MONTH==syncInterval) {
335 return SyncInfo::INTERVAL_1_MONTH;
337 LoggerW("Error while converting a sync interval.");
340 return SyncInfo::INTERVAL_UNDEFINED;
343 static sync_agent_ds_service_type_e convertToPlatformSyncServiceType(SyncServiceInfo::SyncServiceType serviceType)
345 if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
346 return SYNC_AGENT_CONTACT;
347 } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
348 return SYNC_AGENT_CALENDAR;
350 LoggerW("Error while converting a sync service type.");
353 return SYNC_AGENT_CONTACT;
356 static SyncServiceInfo::SyncServiceType convertToSyncServiceType(sync_agent_ds_service_type_e serviceType)
358 if(SYNC_AGENT_CONTACT==serviceType) {
359 return SyncServiceInfo::CONTACT_SERVICE_TYPE;
360 } else if(SYNC_AGENT_CALENDAR==serviceType) {
361 return SyncServiceInfo::EVENT_SERVICE_TYPE;
363 LoggerW("Error while converting a sync service type.");
366 return SyncServiceInfo::UNDEFINED_SERVICE_TYPE;
369 static sync_agent_ds_src_uri_e convertToPlatformSourceUri(SyncServiceInfo::SyncServiceType serviceType)
371 if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
372 return SYNC_AGENT_SRC_URI_CONTACT;
373 } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
374 return SYNC_AGENT_SRC_URI_CALENDAR;
376 LoggerW("Error while converting a sync service.");
379 return SYNC_AGENT_SRC_URI_CONTACT;
382 static SyncStatistics::SyncStatus convertToSyncStatus(char* status)
384 if(0==strcmp(status, "success")) {
385 return SyncStatistics::SUCCESS_STATUS;
386 } else if(0==strcmp(status, "stop")) {
387 return SyncStatistics::STOP_STATUS;
388 } else if(0==strcmp(status, "fail")) {
389 return SyncStatistics::FAIL_STATUS;
390 } else if(0==strcmp(status, "No")) {
391 return SyncStatistics::NONE_STATUS;
393 LoggerW("Error while converting a sync status.");
396 return SyncStatistics::UNDEFINED_STATUS;
399 void DataSyncManager::OnRequestReceived(const IEventAddProfilePtr &event)
401 ds_profile_h profile_h = NULL;
405 // Check if the quota is full first.
406 GList *profile_list = NULL;
409 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
411 ret = sync_agent_ds_get_all_profile(&profile_list);
412 if (SYNC_AGENT_DS_SUCCESS!=ret) {
413 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
416 int numProfiles = g_list_length(profile_list);
417 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
418 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
421 g_list_free(profile_list);
423 LoggerD("numProfiles: "<<numProfiles);
424 if(MAX_PROFILES_NUM==numProfiles) {
425 ThrowMsg(OutOfRangeException, "There are already maximum number of profiles!");
428 SyncProfileInfoPtr profile;
429 profile = event->getProfile();
431 ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
434 ret = sync_agent_ds_create_profile_info(&profile_h);
435 if (SYNC_AGENT_DS_SUCCESS!=ret) {
436 ThrowMsg(PlatformException, "Platform error while creating a profile: "<<ret);
439 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
440 if (SYNC_AGENT_DS_SUCCESS!=ret) {
441 ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
444 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()));
445 if (SYNC_AGENT_DS_SUCCESS!=ret) {
446 ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
449 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
450 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
451 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
452 LoggerD("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
454 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
455 if (SYNC_AGENT_DS_SUCCESS!=ret) {
456 ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
459 // Set the sync categories.
460 SyncServiceInfoListPtr categories = profile->getServiceInfo();
461 sync_agent_ds_service_type_e serviceType;
462 sync_agent_ds_src_uri_e srcURI;
463 std::string tgtURI, id, password;
465 for(unsigned int i=0; categories->size()>i; i++) {
466 serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
467 tgtURI = categories->at(i)->getServerDatabaseUri();
468 srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
469 id = categories->at(i)->getId();
470 password = categories->at(i)->getPassword();
471 enable = categories->at(i)->getEnable();
473 LoggerD("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<", id: "<<id<<" for index: "<<i);
475 ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
476 0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
477 if (SYNC_AGENT_DS_SUCCESS!=ret) {
478 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
483 ret = sync_agent_ds_add_profile(profile_h, &profileId);
484 if (SYNC_AGENT_DS_SUCCESS!=ret) {
485 ThrowMsg(PlatformException, "Platform error while adding a profile: "<<ret);
488 LoggerD("profileId from platform: "<<profileId);
490 char* profileName = NULL;
491 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
492 if (SYNC_AGENT_DS_SUCCESS!=ret) {
493 ThrowMsg(PlatformException, "Platform error while getting a profile name: "<<ret);
496 LoggerD("profileName: "<<profileName<<", profileId: "<<profileId);
498 std::stringstream ss;
500 profile->setProfileId(ss.str());
506 event->setResult(true);
508 Catch(OutOfRangeException)
510 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
511 event->setResult(false);
512 event->setExceptionCode(ExceptionCodes::OutOfRangeException);
516 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
517 event->setResult(false);
518 event->setExceptionCode(ExceptionCodes::PlatformException);
522 sync_agent_ds_free_profile_info(profile_h);
526 void DataSyncManager::OnRequestReceived(const IEventUpdateProfilePtr &event)
528 ds_profile_h profile_h = NULL;
532 SyncProfileInfoPtr profile;
533 profile = event->getProfile();
535 ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
538 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
541 std::stringstream ss(profile->getProfileId());
543 LoggerD("profileId: "<<profileId);
544 ret = sync_agent_ds_get_profile(profileId, &profile_h);
545 if (SYNC_AGENT_DS_SUCCESS!=ret) {
546 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
549 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
550 if (SYNC_AGENT_DS_SUCCESS!=ret) {
551 ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
554 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()));
555 if (SYNC_AGENT_DS_SUCCESS!=ret) {
556 ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
559 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
560 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
561 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
562 LoggerD("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
564 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
565 if (SYNC_AGENT_DS_SUCCESS!=ret) {
566 ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
569 // Set the sync categories.
570 SyncServiceInfoListPtr categories = profile->getServiceInfo();
571 sync_agent_ds_service_type_e serviceType;
572 sync_agent_ds_src_uri_e srcURI;
573 std::string tgtURI, id, password;
575 for(unsigned int i=0; categories->size()<i; i++) {
576 serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
577 tgtURI = categories->at(i)->getServerDatabaseUri();
578 srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
579 id = categories->at(i)->getId();
580 password = categories->at(i)->getPassword();
581 enable = categories->at(i)->getEnable();
583 LoggerD("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<", id: "<<id<<" for index: "<<i);
585 ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
586 0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
587 if (SYNC_AGENT_DS_SUCCESS!=ret) {
588 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
592 ret = sync_agent_ds_update_profile(profile_h);
593 if (SYNC_AGENT_DS_SUCCESS!=ret) {
594 ThrowMsg(NotFoundException, "Platform error while updating a profile: "<<ret);
597 event->setResult(true);
599 Catch(NotFoundException)
601 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
602 event->setResult(false);
603 event->setExceptionCode(ExceptionCodes::NotFoundException);
607 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
608 event->setResult(false);
609 event->setExceptionCode(ExceptionCodes::PlatformException);
613 sync_agent_ds_free_profile_info(profile_h);
617 void DataSyncManager::OnRequestReceived(const IEventRemoveProfilePtr &event)
619 ds_profile_h profile_h = NULL;
623 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
626 std::stringstream ss(event->getProfileId());
628 LoggerD("profileId: "<<profileId);
629 ret = sync_agent_ds_get_profile(profileId, &profile_h);
630 if (SYNC_AGENT_DS_SUCCESS!=ret) {
631 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
634 ret = sync_agent_ds_delete_profile(profile_h);
635 if (SYNC_AGENT_DS_SUCCESS!=ret) {
636 ThrowMsg(PlatformException, "Platform error while deleting a profile: "<<ret);
639 event->setResult(true);
641 Catch(NotFoundException)
643 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
644 event->setResult(false);
645 event->setExceptionCode(ExceptionCodes::NotFoundException);
649 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
650 event->setResult(false);
651 event->setExceptionCode(ExceptionCodes::PlatformException);
655 sync_agent_ds_free_profile_info(profile_h);
659 void DataSyncManager::OnRequestReceived(const IEventGetMaxProfilesNumPtr &event)
663 LoggerD("Return maximum number of supported profiles: "<<MAX_PROFILES_NUM);
665 event->setNumMaxProfiles(MAX_PROFILES_NUM);
667 event->setResult(true);
671 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
672 event->setResult(false);
673 event->setExceptionCode(ExceptionCodes::PlatformException);
677 void DataSyncManager::OnRequestReceived(const IEventGetProfilesNumPtr &event)
679 GList *profile_list = NULL;
684 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
686 ret = sync_agent_ds_get_all_profile(&profile_list);
687 if (SYNC_AGENT_DS_SUCCESS!=ret) {
688 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
692 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
693 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
694 LoggerD("Free sync_agent_ds_profile_info for index: "<<numProfiles++);
697 LoggerD("numProfiles: "<<numProfiles);
699 event->setNumProfiles(numProfiles);
701 event->setResult(true);
705 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
706 event->setResult(false);
707 event->setExceptionCode(ExceptionCodes::PlatformException);
711 void DataSyncManager::OnRequestReceived(const IEventGetProfilePtr &event)
713 ds_profile_h profile_h = NULL;
717 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
720 std::stringstream ss(event->getProfileId());
722 LoggerD("profileId: "<<profileId);
723 ret = sync_agent_ds_get_profile(profileId, &profile_h);
724 if (SYNC_AGENT_DS_SUCCESS!=ret) {
725 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
728 SyncProfileInfoPtr profile( new SyncProfileInfo() );
730 profile->setProfileId(event->getProfileId());
732 char *profileName = NULL;
733 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
734 if (SYNC_AGENT_DS_SUCCESS!=ret) {
735 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
737 profile->setProfileName(profileName);
739 sync_agent_ds_server_info server_info = { NULL };
740 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
741 if (SYNC_AGENT_DS_SUCCESS!=ret) {
742 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
744 profile->getSyncInfo()->setUrl(server_info.addr);
745 profile->getSyncInfo()->setId(server_info.id);
746 profile->getSyncInfo()->setPassword(server_info.password);
748 sync_agent_ds_sync_info sync_info;
749 ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
750 if (SYNC_AGENT_DS_SUCCESS!=ret) {
751 ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
753 profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
754 profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
755 profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
757 LoggerD("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
759 GList *category_list = NULL;
760 sync_agent_ds_service_info *category_info = NULL;
761 ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
762 if (SYNC_AGENT_DS_SUCCESS!=ret) {
763 ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
765 int category_count = g_list_length(category_list);
766 LoggerD("category_count: "<<category_count);
767 while(category_count--) {
768 category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
769 if(SYNC_AGENT_CALENDAR<category_info->service_type) {
770 LoggerD("Skip unsupported sync service type: "<<category_info->service_type);
774 SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
775 serviceInfo->setEnable(category_info->enabled);
776 if(category_info->id) {
777 serviceInfo->setId(category_info->id);
779 if(category_info->password) {
780 serviceInfo->setPassword(category_info->password);
782 serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
783 if(category_info->tgt_uri) {
784 serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
787 LoggerD("Service type: "<<serviceInfo->getSyncServiceType());
788 profile->getServiceInfo()->push_back(serviceInfo);
791 g_list_free(category_list);
794 event->setProfile(profile);
796 event->setResult(true);
798 Catch(NotFoundException)
800 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
801 event->setResult(false);
802 event->setExceptionCode(ExceptionCodes::NotFoundException);
806 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
807 event->setResult(false);
808 event->setExceptionCode(ExceptionCodes::PlatformException);
812 sync_agent_ds_free_profile_info(profile_h);
816 void DataSyncManager::OnRequestReceived(const IEventGetAllProfilesPtr &event)
818 GList *profile_list = NULL;
823 ds_profile_h profile_h = NULL;
824 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
826 ret = sync_agent_ds_get_all_profile(&profile_list);
827 if (SYNC_AGENT_DS_SUCCESS!=ret) {
828 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
831 LoggerD("Number of profiles: "<< g_list_length(profile_list));
832 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
833 profile_h = (ds_profile_h) iter->data;
834 SyncProfileInfoPtr profile( new SyncProfileInfo() );
837 ret = sync_agent_ds_get_profile_id(profile_h, &profileId);
838 if (SYNC_AGENT_DS_SUCCESS!=ret) {
839 ThrowMsg(PlatformException, "Platform error while gettting a profile id: "<<ret);
842 std::stringstream ss;
844 profile->setProfileId(ss.str());
846 LoggerD("Processing a profile with id: "<<profile->getProfileId());
848 char *profileName = NULL;
849 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
850 if (SYNC_AGENT_DS_SUCCESS!=ret) {
851 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
853 profile->setProfileName(profileName);
855 sync_agent_ds_server_info server_info = { NULL };
856 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
857 if (SYNC_AGENT_DS_SUCCESS!=ret) {
858 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
860 profile->getSyncInfo()->setUrl(server_info.addr);
861 profile->getSyncInfo()->setId(server_info.id);
862 profile->getSyncInfo()->setPassword(server_info.password);
864 sync_agent_ds_sync_info sync_info;
865 ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
866 if (SYNC_AGENT_DS_SUCCESS!=ret) {
867 ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
869 profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
870 profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
871 profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
873 LoggerD("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
875 GList *category_list = NULL;
876 sync_agent_ds_service_info *category_info = NULL;
877 ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
878 if (SYNC_AGENT_DS_SUCCESS!=ret) {
879 ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
881 int category_count = g_list_length(category_list);
882 LoggerD("category_count: "<<category_count);
883 while(category_count--) {
884 category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
885 if(SYNC_AGENT_CALENDAR<category_info->service_type) {
886 LoggerD("Skip unsupported sync service type: "<<category_info->service_type);
890 SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
891 serviceInfo->setEnable(category_info->enabled);
892 if(category_info->id) {
893 serviceInfo->setId(category_info->id);
895 if(category_info->password) {
896 serviceInfo->setPassword(category_info->password);
898 serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
899 if(category_info->tgt_uri) {
900 serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
903 LoggerD("Service type: "<<serviceInfo->getSyncServiceType());
904 profile->getServiceInfo()->push_back(serviceInfo);
907 g_list_free(category_list);
910 LoggerD("Adding a profile to the list.");
911 event->getProfiles()->push_back(profile);
914 event->setResult(true);
918 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
919 event->setResult(false);
920 event->setExceptionCode(ExceptionCodes::PlatformException);
923 LoggerD("Free profiles list.");
924 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
925 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
928 g_list_free(profile_list);
932 void DataSyncManager::OnRequestReceived(const IEventStartSyncPtr &event)
934 ds_profile_h profile_h = NULL;
938 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
939 sync_agent_event_error_e err= SYNC_AGENT_EVENT_FAIL;
942 std::stringstream ss(event->getProfileId());
944 LoggerD("profileId: "<<profileId);
945 ret = sync_agent_ds_get_profile(profileId, &profile_h);
946 if (SYNC_AGENT_DS_SUCCESS!=ret) {
947 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
950 err = sync_agent_set_noti_callback(1, datasync_state_changed_cb, this);
951 if (SYNC_AGENT_EVENT_SUCCESS != err) {
952 ThrowMsg(PlatformException, "Platform error while setting state changed cb: " << err);
955 err = sync_agent_set_noti_callback(2, datasync_progress_cb, this);
956 if (SYNC_AGENT_EVENT_SUCCESS != err) {
957 ThrowMsg(PlatformException, "Platform error while setting progress cb: " << err);
960 ret = sync_agent_ds_start_sync(profile_h);
961 if (SYNC_AGENT_DS_SUCCESS!=ret) {
962 ThrowMsg(PlatformException, "Platform error while starting a profile: "<<ret);
965 if(event->getEmitter()) {
966 LoggerD("Attaching the emitter with profileId: "<<event->getProfileId());
967 m_changeEmitters[std::string("Sync") + event->getProfileId()] = event->getEmitter();
970 event->setResult(true);
972 Catch(NotFoundException)
974 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
975 event->setResult(false);
976 event->setExceptionCode(ExceptionCodes::NotFoundException);
980 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
981 event->setResult(false);
982 event->setExceptionCode(ExceptionCodes::PlatformException);
986 sync_agent_ds_free_profile_info(profile_h);
990 void DataSyncManager::OnRequestReceived(const IEventStopSyncPtr &event)
992 ds_profile_h profile_h = NULL;
996 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
999 std::stringstream ss(event->getProfileId());
1001 LoggerD("profileId: "<<profileId);
1002 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1003 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1004 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1007 ret = sync_agent_ds_stop_sync(profile_h);
1008 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1009 ThrowMsg(PlatformException, "Platform error while stopping a profile: "<<ret);
1012 event->setResult(true);
1014 Catch(NotFoundException)
1016 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1017 event->setResult(false);
1018 event->setExceptionCode(ExceptionCodes::NotFoundException);
1022 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1023 event->setResult(false);
1024 event->setExceptionCode(ExceptionCodes::PlatformException);
1028 sync_agent_ds_free_profile_info(profile_h);
1033 void DataSyncManager::OnRequestReceived(const IEventGetLastSyncStatisticsPtr &event)
1035 ds_profile_h profile_h = NULL;
1039 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
1042 std::stringstream ss(event->getProfileId());
1044 LoggerD("profileId: "<<profileId);
1045 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1046 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1047 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1050 SyncStatisticsListPtr statisticsList( new SyncStatisticsList() );
1052 GList *statistics_list = NULL;
1053 ret = sync_agent_ds_get_sync_statistics(profile_h, &statistics_list);
1054 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1055 ThrowMsg(PlatformException, "Platform error while gettting sync statistics: "<<ret);
1058 int statistics_count = g_list_length(statistics_list);
1059 LoggerD("statistics_count: "<<statistics_count);
1060 sync_agent_ds_statistics_info *statistics = NULL;
1061 for (int i = 0; i < statistics_count; i++) {
1062 statistics = (sync_agent_ds_statistics_info *) g_list_nth_data(statistics_list, i);
1064 SyncStatisticsPtr statisticsPtr( new SyncStatistics() );
1067 LoggerD("Statistics for contact.");
1068 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CONTACT));
1070 LoggerD("Statistics for event.");
1071 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CALENDAR));
1073 LoggerW("Unsupported category for statistics: "<<i);
1077 LoggerD("dbsynced: "<<statistics->dbsynced);
1078 statisticsPtr->setSyncStatus(convertToSyncStatus(statistics->dbsynced));
1079 statisticsPtr->setClientToServerTotal(statistics->client2server_total);
1080 statisticsPtr->setClientToServerAdded(statistics->client2server_nrofadd);
1081 statisticsPtr->setClientToServerUpdated(statistics->client2server_nrofreplace);
1082 statisticsPtr->setClientToServerRemoved(statistics->client2server_nrofdelete);
1083 statisticsPtr->setServerToClientTotal(statistics->server2client_total);
1084 statisticsPtr->setServerToClientAdded(statistics->server2client_nrofadd);
1085 statisticsPtr->setServerToClientUpdated(statistics->server2client_nrofreplace);
1086 statisticsPtr->setServerToClientRemoved(statistics->server2client_nrofdelete);
1088 LoggerD("ClientToServerTotal: "<<statisticsPtr->getClientToServerTotal()<<", ServerToClientTotal: "<<statisticsPtr->getServerToClientTotal());
1090 statisticsList->push_back(statisticsPtr);
1092 if(statistics_list) {
1093 g_list_free(statistics_list);
1096 event->setSyncStatictics(statisticsList);
1098 event->setResult(true);
1100 Catch(NotFoundException)
1102 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1103 event->setResult(false);
1104 event->setExceptionCode(ExceptionCodes::NotFoundException);
1108 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1109 event->setResult(false);
1110 event->setExceptionCode(ExceptionCodes::PlatformException);
1114 sync_agent_ds_free_profile_info(profile_h);