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 LoggerD("id: "<<profile->getProfileId());
540 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
543 std::stringstream ss(profile->getProfileId());
545 ret = sync_agent_ds_get_profile(profileId, &profile_h);
546 if (SYNC_AGENT_DS_SUCCESS!=ret) {
547 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
550 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
551 if (SYNC_AGENT_DS_SUCCESS!=ret) {
552 ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
555 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()));
556 if (SYNC_AGENT_DS_SUCCESS!=ret) {
557 ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
560 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
561 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
562 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
563 LoggerD("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
565 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
566 if (SYNC_AGENT_DS_SUCCESS!=ret) {
567 ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
570 // Set the sync categories.
571 SyncServiceInfoListPtr categories = profile->getServiceInfo();
572 sync_agent_ds_service_type_e serviceType;
573 sync_agent_ds_src_uri_e srcURI;
574 std::string tgtURI, id, password;
576 for(unsigned int i=0; categories->size()<i; i++) {
577 serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
578 tgtURI = categories->at(i)->getServerDatabaseUri();
579 srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
580 id = categories->at(i)->getId();
581 password = categories->at(i)->getPassword();
582 enable = categories->at(i)->getEnable();
584 LoggerD("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<", id: "<<id<<" for index: "<<i);
586 ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
587 0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
588 if (SYNC_AGENT_DS_SUCCESS!=ret) {
589 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
593 ret = sync_agent_ds_update_profile(profile_h);
594 if (SYNC_AGENT_DS_SUCCESS!=ret) {
595 ThrowMsg(NotFoundException, "Platform error while updating a profile: "<<ret);
598 event->setResult(true);
600 Catch(NotFoundException)
602 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
603 event->setResult(false);
604 event->setExceptionCode(ExceptionCodes::NotFoundException);
608 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
609 event->setResult(false);
610 event->setExceptionCode(ExceptionCodes::PlatformException);
614 sync_agent_ds_free_profile_info(profile_h);
618 void DataSyncManager::OnRequestReceived(const IEventRemoveProfilePtr &event)
620 ds_profile_h profile_h = NULL;
624 LoggerD("id: "<<event->getProfileId());
626 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
629 std::stringstream ss(event->getProfileId());
631 ret = sync_agent_ds_get_profile(profileId, &profile_h);
632 if (SYNC_AGENT_DS_SUCCESS!=ret) {
633 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
636 ret = sync_agent_ds_delete_profile(profile_h);
637 if (SYNC_AGENT_DS_SUCCESS!=ret) {
638 ThrowMsg(PlatformException, "Platform error while deleting a profile: "<<ret);
641 event->setResult(true);
643 Catch(NotFoundException)
645 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
646 event->setResult(false);
647 event->setExceptionCode(ExceptionCodes::NotFoundException);
651 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
652 event->setResult(false);
653 event->setExceptionCode(ExceptionCodes::PlatformException);
657 sync_agent_ds_free_profile_info(profile_h);
661 void DataSyncManager::OnRequestReceived(const IEventGetMaxProfilesNumPtr &event)
665 LoggerD("Return maximum number of supported profiles: "<<MAX_PROFILES_NUM);
667 event->setNumMaxProfiles(MAX_PROFILES_NUM);
669 event->setResult(true);
673 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
674 event->setResult(false);
675 event->setExceptionCode(ExceptionCodes::PlatformException);
679 void DataSyncManager::OnRequestReceived(const IEventGetProfilesNumPtr &event)
681 GList *profile_list = NULL;
686 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
688 ret = sync_agent_ds_get_all_profile(&profile_list);
689 if (SYNC_AGENT_DS_SUCCESS!=ret) {
690 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
694 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
695 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
696 LoggerD("Free sync_agent_ds_profile_info for index: "<<numProfiles++);
699 LoggerD("numProfiles: "<<numProfiles);
701 event->setNumProfiles(numProfiles);
703 event->setResult(true);
707 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
708 event->setResult(false);
709 event->setExceptionCode(ExceptionCodes::PlatformException);
713 void DataSyncManager::OnRequestReceived(const IEventGetProfilePtr &event)
715 ds_profile_h profile_h = NULL;
719 LoggerD("id: "<<event->getProfileId());
721 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
724 std::stringstream ss(event->getProfileId());
726 ret = sync_agent_ds_get_profile(profileId, &profile_h);
727 if (SYNC_AGENT_DS_SUCCESS!=ret) {
728 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
731 SyncProfileInfoPtr profile( new SyncProfileInfo() );
733 profile->setProfileId(event->getProfileId());
735 char *profileName = NULL;
736 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
737 if (SYNC_AGENT_DS_SUCCESS!=ret) {
738 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
740 profile->setProfileName(profileName);
742 sync_agent_ds_server_info server_info = { NULL };
743 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
744 if (SYNC_AGENT_DS_SUCCESS!=ret) {
745 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
747 profile->getSyncInfo()->setUrl(server_info.addr);
748 profile->getSyncInfo()->setId(server_info.id);
749 profile->getSyncInfo()->setPassword(server_info.password);
751 sync_agent_ds_sync_info sync_info;
752 ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
753 if (SYNC_AGENT_DS_SUCCESS!=ret) {
754 ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
756 profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
757 profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
758 profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
760 LoggerD("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
762 GList *category_list = NULL;
763 sync_agent_ds_service_info *category_info = NULL;
764 ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
765 if (SYNC_AGENT_DS_SUCCESS!=ret) {
766 ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
768 int category_count = g_list_length(category_list);
769 LoggerD("category_count: "<<category_count);
770 while(category_count--) {
771 category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
772 if(SYNC_AGENT_CALENDAR<category_info->service_type) {
773 LoggerD("Skip unsupported sync service type: "<<category_info->service_type);
777 SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
778 serviceInfo->setEnable(category_info->enabled);
779 if(category_info->id) {
780 serviceInfo->setId(category_info->id);
782 if(category_info->password) {
783 serviceInfo->setPassword(category_info->password);
785 serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
786 if(category_info->tgt_uri) {
787 serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
790 LoggerD("Service type: "<<serviceInfo->getSyncServiceType());
791 profile->getServiceInfo()->push_back(serviceInfo);
794 g_list_free(category_list);
797 event->setProfile(profile);
799 event->setResult(true);
801 Catch(NotFoundException)
803 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
804 event->setResult(false);
805 event->setExceptionCode(ExceptionCodes::NotFoundException);
809 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
810 event->setResult(false);
811 event->setExceptionCode(ExceptionCodes::PlatformException);
815 sync_agent_ds_free_profile_info(profile_h);
819 void DataSyncManager::OnRequestReceived(const IEventGetAllProfilesPtr &event)
821 GList *profile_list = NULL;
826 ds_profile_h profile_h = NULL;
827 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
829 ret = sync_agent_ds_get_all_profile(&profile_list);
830 if (SYNC_AGENT_DS_SUCCESS!=ret) {
831 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
834 LoggerD("Number of profiles: "<< g_list_length(profile_list));
835 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
836 profile_h = (ds_profile_h) iter->data;
837 SyncProfileInfoPtr profile( new SyncProfileInfo() );
840 ret = sync_agent_ds_get_profile_id(profile_h, &profileId);
841 if (SYNC_AGENT_DS_SUCCESS!=ret) {
842 ThrowMsg(PlatformException, "Platform error while gettting a profile id: "<<ret);
845 std::stringstream ss;
847 profile->setProfileId(ss.str());
849 LoggerD("Processing a profile with id: "<<profile->getProfileId());
851 char *profileName = NULL;
852 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
853 if (SYNC_AGENT_DS_SUCCESS!=ret) {
854 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
856 profile->setProfileName(profileName);
858 sync_agent_ds_server_info server_info = { NULL };
859 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
860 if (SYNC_AGENT_DS_SUCCESS!=ret) {
861 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
863 profile->getSyncInfo()->setUrl(server_info.addr);
864 profile->getSyncInfo()->setId(server_info.id);
865 profile->getSyncInfo()->setPassword(server_info.password);
867 sync_agent_ds_sync_info sync_info;
868 ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
869 if (SYNC_AGENT_DS_SUCCESS!=ret) {
870 ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
872 profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
873 profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
874 profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
876 LoggerD("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
878 GList *category_list = NULL;
879 sync_agent_ds_service_info *category_info = NULL;
880 ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
881 if (SYNC_AGENT_DS_SUCCESS!=ret) {
882 ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
884 int category_count = g_list_length(category_list);
885 LoggerD("category_count: "<<category_count);
886 while(category_count--) {
887 category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
888 if(SYNC_AGENT_CALENDAR<category_info->service_type) {
889 LoggerD("Skip unsupported sync service type: "<<category_info->service_type);
893 SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
894 serviceInfo->setEnable(category_info->enabled);
895 if(category_info->id) {
896 serviceInfo->setId(category_info->id);
898 if(category_info->password) {
899 serviceInfo->setPassword(category_info->password);
901 serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
902 if(category_info->tgt_uri) {
903 serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
906 LoggerD("Service type: "<<serviceInfo->getSyncServiceType());
907 profile->getServiceInfo()->push_back(serviceInfo);
910 g_list_free(category_list);
913 LoggerD("Adding a profile to the list.");
914 event->getProfiles()->push_back(profile);
917 event->setResult(true);
921 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
922 event->setResult(false);
923 event->setExceptionCode(ExceptionCodes::PlatformException);
926 LoggerD("Free profiles list.");
927 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
928 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
931 g_list_free(profile_list);
935 void DataSyncManager::OnRequestReceived(const IEventStartSyncPtr &event)
937 ds_profile_h profile_h = NULL;
941 LoggerD("id: "<<event->getProfileId());
943 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
944 sync_agent_event_error_e err= SYNC_AGENT_EVENT_FAIL;
947 std::stringstream ss(event->getProfileId());
949 ret = sync_agent_ds_get_profile(profileId, &profile_h);
950 if (SYNC_AGENT_DS_SUCCESS!=ret) {
951 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
954 err = sync_agent_set_noti_callback(1, datasync_state_changed_cb, this);
955 if (SYNC_AGENT_EVENT_SUCCESS != err) {
956 ThrowMsg(PlatformException, "Platform error while setting state changed cb: " << err);
959 err = sync_agent_set_noti_callback(2, datasync_progress_cb, this);
960 if (SYNC_AGENT_EVENT_SUCCESS != err) {
961 ThrowMsg(PlatformException, "Platform error while setting progress cb: " << err);
964 ret = sync_agent_ds_start_sync(profile_h);
965 if (SYNC_AGENT_DS_SUCCESS!=ret) {
966 ThrowMsg(PlatformException, "Platform error while starting a profile: "<<ret);
969 if(event->getEmitter()) {
970 LoggerD("Attaching the emitter with profileId: "<<event->getProfileId());
971 m_changeEmitters[std::string("Sync") + event->getProfileId()] = event->getEmitter();
974 event->setResult(true);
976 Catch(NotFoundException)
978 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
979 event->setResult(false);
980 event->setExceptionCode(ExceptionCodes::NotFoundException);
984 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
985 event->setResult(false);
986 event->setExceptionCode(ExceptionCodes::PlatformException);
990 sync_agent_ds_free_profile_info(profile_h);
994 void DataSyncManager::OnRequestReceived(const IEventStopSyncPtr &event)
996 ds_profile_h profile_h = NULL;
1000 LoggerD("id: "<<event->getProfileId());
1002 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
1005 std::stringstream ss(event->getProfileId());
1007 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1008 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1009 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1012 ret = sync_agent_ds_stop_sync(profile_h);
1013 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1014 ThrowMsg(PlatformException, "Platform error while stopping a profile: "<<ret);
1017 event->setResult(true);
1019 Catch(NotFoundException)
1021 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1022 event->setResult(false);
1023 event->setExceptionCode(ExceptionCodes::NotFoundException);
1027 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1028 event->setResult(false);
1029 event->setExceptionCode(ExceptionCodes::PlatformException);
1033 sync_agent_ds_free_profile_info(profile_h);
1038 void DataSyncManager::OnRequestReceived(const IEventGetLastSyncStatisticsPtr &event)
1040 ds_profile_h profile_h = NULL;
1044 LoggerD("id: "<<event->getProfileId());
1046 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
1049 std::stringstream ss(event->getProfileId());
1051 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1052 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1053 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1056 SyncStatisticsListPtr statisticsList( new SyncStatisticsList() );
1058 GList *statistics_list = NULL;
1059 ret = sync_agent_ds_get_sync_statistics(profile_h, &statistics_list);
1060 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1061 ThrowMsg(PlatformException, "Platform error while gettting sync statistics: "<<ret);
1064 int statistics_count = g_list_length(statistics_list);
1065 LoggerD("statistics_count: "<<statistics_count);
1066 sync_agent_ds_statistics_info *statistics = NULL;
1067 for (int i = 0; i < statistics_count; i++) {
1068 statistics = (sync_agent_ds_statistics_info *) g_list_nth_data(statistics_list, i);
1070 SyncStatisticsPtr statisticsPtr( new SyncStatistics() );
1073 LoggerD("Statistics for contact.");
1074 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CONTACT));
1076 LoggerD("Statistics for event.");
1077 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CALENDAR));
1079 LoggerW("Unsupported category for statistics: "<<i);
1083 LoggerD("dbsynced: "<<statistics->dbsynced);
1084 statisticsPtr->setSyncStatus(convertToSyncStatus(statistics->dbsynced));
1085 statisticsPtr->setClientToServerTotal(statistics->client2server_total);
1086 statisticsPtr->setClientToServerAdded(statistics->client2server_nrofadd);
1087 statisticsPtr->setClientToServerUpdated(statistics->client2server_nrofreplace);
1088 statisticsPtr->setClientToServerRemoved(statistics->client2server_nrofdelete);
1089 statisticsPtr->setServerToClientTotal(statistics->server2client_total);
1090 statisticsPtr->setServerToClientAdded(statistics->server2client_nrofadd);
1091 statisticsPtr->setServerToClientUpdated(statistics->server2client_nrofreplace);
1092 statisticsPtr->setServerToClientRemoved(statistics->server2client_nrofdelete);
1094 LoggerD("ClientToServerTotal: "<<statisticsPtr->getClientToServerTotal()<<", ServerToClientTotal: "<<statisticsPtr->getServerToClientTotal());
1096 statisticsList->push_back(statisticsPtr);
1098 if(statistics_list) {
1099 g_list_free(statistics_list);
1102 event->setSyncStatictics(statisticsList);
1104 event->setResult(true);
1106 Catch(NotFoundException)
1108 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1109 event->setResult(false);
1110 event->setExceptionCode(ExceptionCodes::NotFoundException);
1114 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1115 event->setResult(false);
1116 event->setExceptionCode(ExceptionCodes::PlatformException);
1120 sync_agent_ds_free_profile_info(profile_h);