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 <dpl/log/log.h>
26 #include <sync_agent.h>
30 using namespace WrtDeviceApis::Commons;
35 DataSyncManager::DataSyncManager()
37 LogInfo("Initialize the datasync manager.");
39 sync_agent_ds_error_e ds_err = sync_agent_ds_init();
40 if (SYNC_AGENT_DS_SUCCESS!=ds_err) {
41 LogError("Failed to init oma ds.");
45 DataSyncManager::~DataSyncManager()
47 LogInfo("Deinitialize the datasync manager.");
49 sync_agent_ds_error_e ds_err = sync_agent_ds_deinit();
50 if (SYNC_AGENT_DS_SUCCESS!=ds_err) {
51 LogError("Failed to deinit oma ds.");
55 static int datasync_state_changed_cb(sync_agent_event_data_s* request, void *user_data)
57 LogDebug("DataSync session state changed.");
59 char *profileDirName = NULL;
61 char *progress = NULL;
66 OnDataSyncStateChangedPtr eventPtr(new OnDataSyncStateChanged());
67 DataSyncManager* thisDataSyncManager = (DataSyncManager*) user_data;
69 LogDebug("Get state info.");
70 sync_agent_get_event_data_param(request, &profileDirName);
71 sync_agent_get_event_data_param(request, &sync_type);
72 sync_agent_get_event_data_param(request, &progress);
73 sync_agent_get_event_data_param(request, &error);
75 LogDebug("profileDirName: "<<profileDirName<<", sync_type: "<<sync_type<<", progress: "<<progress<<", error: "<<error);
78 eventPtr->setProfileId(profileDirName);
82 LogWarning("Null status.");
83 eventPtr->setSessionStatus(OnDataSyncStateChanged::UNDEFINED_STATUS);
84 } else if(0==strcmp(progress, "DONE")) {
85 eventPtr->setSessionStatus(OnDataSyncStateChanged::COMPLETE_STATUS);
86 } else if(0==strcmp(progress, "CANCEL")) {
87 eventPtr->setSessionStatus(OnDataSyncStateChanged::STOP_STATUS);
88 } else if(0==strcmp(progress, "ERROR")) {
89 // Error cases should be redefined and transferred based on the error value.
90 eventPtr->setSessionStatus(OnDataSyncStateChanged::FAIL_STATUS);
92 LogWarning("Wrong status.");
93 eventPtr->setSessionStatus(OnDataSyncStateChanged::UNDEFINED_STATUS);
96 eventPtr->setResult(true);
99 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
100 thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
106 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
110 g_free(profileDirName);
119 if (request != NULL) {
120 if (request->size != NULL) {
121 g_free(request->size);
129 static int datasync_progress_cb(sync_agent_event_data_s* request, void *user_data)
131 LogDebug("DataSync progress called.");
133 char *profileDirName = NULL;
136 char *progressStatus = NULL;
137 char *operationType = NULL;
141 OnDataSyncStateChangedPtr eventPtr(new OnDataSyncStateChanged());
142 DataSyncManager* thisDataSyncManager = (DataSyncManager*) user_data;
144 int isFromServer, totalPerOperation, syncedPerOperation, totalPerDb, syncedPerDb;
146 LogDebug("Get progress info.");
147 sync_agent_get_event_data_param(request, &profileDirName);
148 sync_agent_get_event_data_param(request, &syncType);
149 sync_agent_get_event_data_param(request, &uri);
150 sync_agent_get_event_data_param(request, &progressStatus);
151 sync_agent_get_event_data_param(request, &operationType);
153 LogDebug("profileDirName: "<<profileDirName<<", syncType: "<<syncType<<", uri: "<<uri<<", progressStatus: "<<progressStatus<<", operationType "<<operationType);
155 sync_agent_get_event_data_param(request, &isFromServer);
156 sync_agent_get_event_data_param(request, &totalPerOperation);
157 sync_agent_get_event_data_param(request, &syncedPerOperation);
158 sync_agent_get_event_data_param(request, &totalPerDb);
159 sync_agent_get_event_data_param(request, &syncedPerDb);
161 LogDebug("isFromServer: "<<isFromServer<<", totalPerOperation: "<<totalPerOperation<<", syncedPerOperation: "<<syncedPerOperation<<", totalPerDb: "<<totalPerDb<<", syncedPerDb "<<syncedPerDb);
164 eventPtr->setProfileId(profileDirName);
167 eventPtr->setSessionStatus(OnDataSyncStateChanged::PROGRESS_STATUS);
169 if(SYNC_AGENT_SRC_URI_CONTACT==uri) {
170 eventPtr->setServiceType(SyncServiceInfo::CONTACT_SERVICE_TYPE);
171 } else if(SYNC_AGENT_SRC_URI_CALENDAR==uri) {
172 eventPtr->setServiceType(SyncServiceInfo::EVENT_SERVICE_TYPE);
174 LogWarning("Wrong service type.");
175 eventPtr->setServiceType(SyncServiceInfo::UNDEFINED_SERVICE_TYPE);
178 eventPtr->setIsFromServer(isFromServer);
179 eventPtr->setSynedPerService(syncedPerDb);
180 eventPtr->setTotalPerService(totalPerDb);
182 eventPtr->setResult(true);
185 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
186 thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
192 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
196 g_free(profileDirName);
199 g_free(progressStatus);
202 g_free(operationType);
205 if (request != NULL) {
206 if (request->size != NULL) {
207 g_free(request->size);
215 static sync_agent_ds_sync_mode_e convertToPlatformSyncMode(SyncInfo::SyncMode syncMode)
217 if(SyncInfo::MANUAL_MODE==syncMode) {
218 return SYNC_AGENT_SYNC_MODE_MANUAL;
219 } else if(SyncInfo::PERIODIC_MODE==syncMode) {
220 return SYNC_AGENT_SYNC_MODE_PERIODIC;
221 } else if(SyncInfo::PUSH_MODE==syncMode) {
222 return SYNC_AGENT_SYNC_MODE_PUSH;
224 LogWarning("Error while converting a sync mode.");
227 return SYNC_AGENT_SYNC_MODE_MANUAL;
230 static SyncInfo::SyncMode convertToSyncMode(sync_agent_ds_sync_mode_e syncMode)
232 if(SYNC_AGENT_SYNC_MODE_MANUAL==syncMode) {
233 return SyncInfo::MANUAL_MODE;
234 } else if(SYNC_AGENT_SYNC_MODE_PERIODIC==syncMode) {
235 return SyncInfo::PERIODIC_MODE;
236 } else if(SYNC_AGENT_SYNC_MODE_PUSH==syncMode) {
237 return SyncInfo::PUSH_MODE;
239 LogWarning("Error while converting a sync mode.");
242 return SyncInfo::UNDEFINED_MODE;
245 static sync_agent_ds_sync_type_e convertToPlatformSyncType(SyncInfo::SyncType syncType)
247 if(SyncInfo::TWO_WAY_TYPE==syncType) {
248 return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
249 } else if(SyncInfo::SLOW_TYPE==syncType) {
250 return SYNC_AGENT_SYNC_TYPE_FULL_SYNC;
251 } else if(SyncInfo::ONE_WAY_FROM_CLIENT_TYPE==syncType) {
252 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER;
253 } else if(SyncInfo::REFRESH_FROM_CLIENT_TYPE==syncType) {
254 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE;
255 } else if(SyncInfo::ONE_WAY_FROM_SERVER_TYPE==syncType) {
256 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE;
257 } else if(SyncInfo::REFRESH_FROM_SERVER_TYPE==syncType) {
258 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER;
260 LogWarning("Error while converting a sync type.");
263 return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
266 static SyncInfo::SyncType convertToSyncType(sync_agent_ds_sync_type_e syncType)
268 if(SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH==syncType) {
269 return SyncInfo::TWO_WAY_TYPE;
270 } else if(SYNC_AGENT_SYNC_TYPE_FULL_SYNC==syncType) {
271 return SyncInfo::SLOW_TYPE;
272 } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER==syncType) {
273 return SyncInfo::ONE_WAY_FROM_CLIENT_TYPE;
274 } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE==syncType) {
275 return SyncInfo::REFRESH_FROM_CLIENT_TYPE;
276 } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE==syncType) {
277 return SyncInfo::ONE_WAY_FROM_SERVER_TYPE;
278 } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER==syncType) {
279 return SyncInfo::REFRESH_FROM_SERVER_TYPE;
281 LogWarning("Error while converting a sync type.");
284 return SyncInfo::UNDEFINED_TYPE;
287 static sync_agent_ds_sync_interval_e convertToPlatformSyncInterval(SyncInfo::SyncInterval syncInterval)
289 if(SyncInfo::INTERVAL_5_MINUTES==syncInterval) {
290 return SYNC_AGENT_SYNC_INTERVAL_5_MINUTES;
291 } else if(SyncInfo::INTERVAL_15_MINUTES==syncInterval) {
292 return SYNC_AGENT_SYNC_INTERVAL_15_MINUTES;
293 } else if(SyncInfo::INTERVAL_1_HOUR==syncInterval) {
294 return SYNC_AGENT_SYNC_INTERVAL_1_HOUR;
295 } else if(SyncInfo::INTERVAL_4_HOURS==syncInterval) {
296 return SYNC_AGENT_SYNC_INTERVAL_4_HOURS;
297 } else if(SyncInfo::INTERVAL_12_HOURS==syncInterval) {
298 return SYNC_AGENT_SYNC_INTERVAL_12_HOURS;
299 } else if(SyncInfo::INTERVAL_1_DAY==syncInterval) {
300 return SYNC_AGENT_SYNC_INTERVAL_1_DAY;
301 } else if(SyncInfo::INTERVAL_1_WEEK==syncInterval) {
302 return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
303 } else if(SyncInfo::INTERVAL_1_MONTH==syncInterval) {
304 return SYNC_AGENT_SYNC_INTERVAL_1_MONTH;
306 LogWarning("Error while converting a sync interval.");
309 return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
312 static SyncInfo::SyncInterval convertToSyncInterval(sync_agent_ds_sync_interval_e syncInterval)
314 if(SYNC_AGENT_SYNC_INTERVAL_5_MINUTES==syncInterval) {
315 return SyncInfo::INTERVAL_5_MINUTES;
316 } else if(SYNC_AGENT_SYNC_INTERVAL_15_MINUTES==syncInterval) {
317 return SyncInfo::INTERVAL_15_MINUTES;
318 } else if(SYNC_AGENT_SYNC_INTERVAL_1_HOUR==syncInterval) {
319 return SyncInfo::INTERVAL_1_HOUR;
320 } else if(SYNC_AGENT_SYNC_INTERVAL_4_HOURS==syncInterval) {
321 return SyncInfo::INTERVAL_4_HOURS;
322 } else if(SYNC_AGENT_SYNC_INTERVAL_12_HOURS==syncInterval) {
323 return SyncInfo::INTERVAL_12_HOURS;
324 } else if(SYNC_AGENT_SYNC_INTERVAL_1_DAY==syncInterval) {
325 return SyncInfo::INTERVAL_1_DAY;
326 } else if(SYNC_AGENT_SYNC_INTERVAL_1_WEEK==syncInterval) {
327 return SyncInfo::INTERVAL_1_WEEK;
328 } else if(SYNC_AGENT_SYNC_INTERVAL_1_MONTH==syncInterval) {
329 return SyncInfo::INTERVAL_1_MONTH;
331 LogWarning("Error while converting a sync interval.");
334 return SyncInfo::INTERVAL_UNDEFINED;
337 static sync_agent_ds_service_type_e convertToPlatformSyncServiceType(SyncServiceInfo::SyncServiceType serviceType)
339 if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
340 return SYNC_AGENT_CONTACT;
341 } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
342 return SYNC_AGENT_CALENDAR;
344 LogWarning("Error while converting a sync service type.");
347 return SYNC_AGENT_CONTACT;
350 static SyncServiceInfo::SyncServiceType convertToSyncServiceType(sync_agent_ds_service_type_e serviceType)
352 if(SYNC_AGENT_CONTACT==serviceType) {
353 return SyncServiceInfo::CONTACT_SERVICE_TYPE;
354 } else if(SYNC_AGENT_CALENDAR==serviceType) {
355 return SyncServiceInfo::EVENT_SERVICE_TYPE;
357 LogWarning("Error while converting a sync service type.");
360 return SyncServiceInfo::UNDEFINED_SERVICE_TYPE;
363 static sync_agent_ds_src_uri_e convertToPlatformSourceUri(SyncServiceInfo::SyncServiceType serviceType)
365 if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
366 return SYNC_AGENT_SRC_URI_CONTACT;
367 } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
368 return SYNC_AGENT_SRC_URI_CALENDAR;
370 LogWarning("Error while converting a sync service.");
373 return SYNC_AGENT_SRC_URI_CONTACT;
376 static SyncStatistics::SyncStatus convertToSyncStatus(char* status)
378 if(0==strcmp(status, "success")) {
379 return SyncStatistics::SUCCESS_STATUS;
380 } else if(0==strcmp(status, "stop")) {
381 return SyncStatistics::STOP_STATUS;
382 } else if(0==strcmp(status, "fail")) {
383 return SyncStatistics::FAIL_STATUS;
384 } else if(0==strcmp(status, "No")) {
385 return SyncStatistics::NONE_STATUS;
387 LogWarning("Error while converting a sync status.");
390 return SyncStatistics::UNDEFINED_STATUS;
393 void DataSyncManager::OnRequestReceived(const IEventAddProfilePtr &event)
395 ds_profile_h profile_h = NULL;
399 // Check if the quota is full first.
400 GList *profile_list = NULL;
403 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
405 ret = sync_agent_ds_get_all_profile(&profile_list);
406 if (SYNC_AGENT_DS_SUCCESS!=ret) {
407 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
410 int numProfiles = g_list_length(profile_list);
411 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
412 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
415 g_list_free(profile_list);
417 LogDebug("numProfiles: "<<numProfiles);
418 if(MAX_PROFILES_NUM==numProfiles) {
419 ThrowMsg(OutOfRangeException, "There are already maximum number of profiles!");
422 SyncProfileInfoPtr profile;
423 profile = event->getProfile();
425 ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
428 LogDebug("url: "<<profile->getSyncInfo()->getUrl());
430 ret = sync_agent_ds_create_profile_info(&profile_h);
431 if (SYNC_AGENT_DS_SUCCESS!=ret) {
432 ThrowMsg(PlatformException, "Platform error while creating a profile: "<<ret);
435 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
436 if (SYNC_AGENT_DS_SUCCESS!=ret) {
437 ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
440 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()));
441 if (SYNC_AGENT_DS_SUCCESS!=ret) {
442 ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
445 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
446 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
447 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
448 LogDebug("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
450 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
451 if (SYNC_AGENT_DS_SUCCESS!=ret) {
452 ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
455 // Set the sync categories.
456 SyncServiceInfoListPtr categories = profile->getServiceInfo();
457 sync_agent_ds_service_type_e serviceType;
458 sync_agent_ds_src_uri_e srcURI;
459 std::string tgtURI, id, password;
461 for(unsigned int i=0; categories->size()>i; i++) {
462 serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
463 tgtURI = categories->at(i)->getServerDatabaseUri();
464 srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
465 id = categories->at(i)->getId();
466 password = categories->at(i)->getPassword();
467 enable = categories->at(i)->getEnable();
469 LogDebug("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<", id: "<<id<<" for index: "<<i);
471 ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
472 0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
473 if (SYNC_AGENT_DS_SUCCESS!=ret) {
474 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
479 ret = sync_agent_ds_add_profile(profile_h, &profileId);
480 if (SYNC_AGENT_DS_SUCCESS!=ret) {
481 ThrowMsg(PlatformException, "Platform error while adding a profile: "<<ret);
484 LogDebug("profileId from platform: "<<profileId);
486 char* profileName = NULL;
487 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
488 if (SYNC_AGENT_DS_SUCCESS!=ret) {
489 ThrowMsg(PlatformException, "Platform error while getting a profile name: "<<ret);
492 LogDebug("profileName: "<<profileName<<", profileId: "<<profileId);
494 std::stringstream ss;
496 profile->setProfileId(ss.str());
502 event->setResult(true);
504 Catch(OutOfRangeException)
506 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
507 event->setResult(false);
508 event->setExceptionCode(ExceptionCodes::OutOfRangeException);
512 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
513 event->setResult(false);
514 event->setExceptionCode(ExceptionCodes::PlatformException);
518 sync_agent_ds_free_profile_info(profile_h);
522 void DataSyncManager::OnRequestReceived(const IEventUpdateProfilePtr &event)
524 ds_profile_h profile_h = NULL;
528 SyncProfileInfoPtr profile;
529 profile = event->getProfile();
531 ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
534 LogDebug("id: "<<profile->getProfileId());
536 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
539 std::stringstream ss(profile->getProfileId());
541 ret = sync_agent_ds_get_profile(profileId, &profile_h);
542 if (SYNC_AGENT_DS_SUCCESS!=ret) {
543 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
546 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
547 if (SYNC_AGENT_DS_SUCCESS!=ret) {
548 ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
551 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()));
552 if (SYNC_AGENT_DS_SUCCESS!=ret) {
553 ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
556 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
557 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
558 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
559 LogDebug("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
561 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
562 if (SYNC_AGENT_DS_SUCCESS!=ret) {
563 ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
566 // Set the sync categories.
567 SyncServiceInfoListPtr categories = profile->getServiceInfo();
568 sync_agent_ds_service_type_e serviceType;
569 sync_agent_ds_src_uri_e srcURI;
570 std::string tgtURI, id, password;
572 for(unsigned int i=0; categories->size()<i; i++) {
573 serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
574 tgtURI = categories->at(i)->getServerDatabaseUri();
575 srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
576 id = categories->at(i)->getId();
577 password = categories->at(i)->getPassword();
578 enable = categories->at(i)->getEnable();
580 LogDebug("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<", id: "<<id<<" for index: "<<i);
582 ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
583 0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
584 if (SYNC_AGENT_DS_SUCCESS!=ret) {
585 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
589 ret = sync_agent_ds_update_profile(profile_h);
590 if (SYNC_AGENT_DS_SUCCESS!=ret) {
591 ThrowMsg(NotFoundException, "Platform error while updating a profile: "<<ret);
594 event->setResult(true);
596 Catch(NotFoundException)
598 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
599 event->setResult(false);
600 event->setExceptionCode(ExceptionCodes::NotFoundException);
604 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
605 event->setResult(false);
606 event->setExceptionCode(ExceptionCodes::PlatformException);
610 sync_agent_ds_free_profile_info(profile_h);
614 void DataSyncManager::OnRequestReceived(const IEventRemoveProfilePtr &event)
616 ds_profile_h profile_h = NULL;
620 LogDebug("id: "<<event->getProfileId());
622 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
625 std::stringstream ss(event->getProfileId());
627 ret = sync_agent_ds_get_profile(profileId, &profile_h);
628 if (SYNC_AGENT_DS_SUCCESS!=ret) {
629 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
632 ret = sync_agent_ds_delete_profile(profile_h);
633 if (SYNC_AGENT_DS_SUCCESS!=ret) {
634 ThrowMsg(PlatformException, "Platform error while deleting a profile: "<<ret);
637 event->setResult(true);
639 Catch(NotFoundException)
641 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
642 event->setResult(false);
643 event->setExceptionCode(ExceptionCodes::NotFoundException);
647 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
648 event->setResult(false);
649 event->setExceptionCode(ExceptionCodes::PlatformException);
653 sync_agent_ds_free_profile_info(profile_h);
657 void DataSyncManager::OnRequestReceived(const IEventGetMaxProfilesNumPtr &event)
661 LogDebug("Return maximum number of supported profiles: "<<MAX_PROFILES_NUM);
663 event->setNumMaxProfiles(MAX_PROFILES_NUM);
665 event->setResult(true);
669 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
670 event->setResult(false);
671 event->setExceptionCode(ExceptionCodes::PlatformException);
675 void DataSyncManager::OnRequestReceived(const IEventGetProfilesNumPtr &event)
677 GList *profile_list = NULL;
682 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
684 ret = sync_agent_ds_get_all_profile(&profile_list);
685 if (SYNC_AGENT_DS_SUCCESS!=ret) {
686 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
690 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
691 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
692 LogDebug("Free sync_agent_ds_profile_info for index: "<<numProfiles++);
695 LogDebug("numProfiles: "<<numProfiles);
697 event->setNumProfiles(numProfiles);
699 event->setResult(true);
703 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
704 event->setResult(false);
705 event->setExceptionCode(ExceptionCodes::PlatformException);
709 void DataSyncManager::OnRequestReceived(const IEventGetProfilePtr &event)
711 ds_profile_h profile_h = NULL;
715 LogDebug("id: "<<event->getProfileId());
717 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
720 std::stringstream ss(event->getProfileId());
722 ret = sync_agent_ds_get_profile(profileId, &profile_h);
723 if (SYNC_AGENT_DS_SUCCESS!=ret) {
724 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
727 SyncProfileInfoPtr profile( new SyncProfileInfo() );
729 profile->setProfileId(event->getProfileId());
731 char *profileName = NULL;
732 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
733 if (SYNC_AGENT_DS_SUCCESS!=ret) {
734 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
736 profile->setProfileName(profileName);
738 sync_agent_ds_server_info server_info = { NULL };
739 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
740 if (SYNC_AGENT_DS_SUCCESS!=ret) {
741 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
743 profile->getSyncInfo()->setUrl(server_info.addr);
744 profile->getSyncInfo()->setId(server_info.id);
745 profile->getSyncInfo()->setPassword(server_info.password);
747 LogDebug("Url: "<<profile->getSyncInfo()->getUrl());
749 sync_agent_ds_sync_info sync_info;
750 ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
751 if (SYNC_AGENT_DS_SUCCESS!=ret) {
752 ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
754 profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
755 profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
756 profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
758 LogDebug("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
760 GList *category_list = NULL;
761 sync_agent_ds_service_info *category_info = NULL;
762 ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
763 if (SYNC_AGENT_DS_SUCCESS!=ret) {
764 ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
766 int category_count = g_list_length(category_list);
767 LogDebug("category_count: "<<category_count);
768 while(category_count--) {
769 category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
770 if(SYNC_AGENT_CALENDAR<category_info->service_type) {
771 LogDebug("Skip unsupported sync service type: "<<category_info->service_type);
775 SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
776 serviceInfo->setEnable(category_info->enabled);
777 if(category_info->id) {
778 serviceInfo->setId(category_info->id);
780 if(category_info->password) {
781 serviceInfo->setPassword(category_info->password);
783 serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
784 if(category_info->tgt_uri) {
785 serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
788 LogDebug("Service type: "<<serviceInfo->getSyncServiceType()<<", DB URI: "<<serviceInfo->getServerDatabaseUri());
789 profile->getServiceInfo()->push_back(serviceInfo);
792 g_list_free(category_list);
795 event->setProfile(profile);
797 event->setResult(true);
799 Catch(NotFoundException)
801 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
802 event->setResult(false);
803 event->setExceptionCode(ExceptionCodes::NotFoundException);
807 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
808 event->setResult(false);
809 event->setExceptionCode(ExceptionCodes::PlatformException);
813 sync_agent_ds_free_profile_info(profile_h);
817 void DataSyncManager::OnRequestReceived(const IEventGetAllProfilesPtr &event)
819 GList *profile_list = NULL;
824 ds_profile_h profile_h = NULL;
825 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
827 ret = sync_agent_ds_get_all_profile(&profile_list);
828 if (SYNC_AGENT_DS_SUCCESS!=ret) {
829 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
832 LogDebug("Number of profiles: "<< g_list_length(profile_list));
833 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
834 profile_h = (ds_profile_h) iter->data;
835 SyncProfileInfoPtr profile( new SyncProfileInfo() );
838 ret = sync_agent_ds_get_profile_id(profile_h, &profileId);
839 if (SYNC_AGENT_DS_SUCCESS!=ret) {
840 ThrowMsg(PlatformException, "Platform error while gettting a profile id: "<<ret);
843 std::stringstream ss;
845 profile->setProfileId(ss.str());
847 LogDebug("Processing a profile with id: "<<profile->getProfileId());
849 char *profileName = NULL;
850 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
851 if (SYNC_AGENT_DS_SUCCESS!=ret) {
852 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
854 profile->setProfileName(profileName);
856 sync_agent_ds_server_info server_info = { NULL };
857 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
858 if (SYNC_AGENT_DS_SUCCESS!=ret) {
859 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
861 profile->getSyncInfo()->setUrl(server_info.addr);
862 profile->getSyncInfo()->setId(server_info.id);
863 profile->getSyncInfo()->setPassword(server_info.password);
865 LogDebug("Url: "<<profile->getSyncInfo()->getUrl());
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 LogDebug("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 LogDebug("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 LogDebug("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 LogDebug("Service type: "<<serviceInfo->getSyncServiceType()<<", DB URI: "<<serviceInfo->getServerDatabaseUri());
907 profile->getServiceInfo()->push_back(serviceInfo);
910 g_list_free(category_list);
913 LogDebug("Adding a profile to the list.");
914 event->getProfiles()->push_back(profile);
917 event->setResult(true);
921 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
922 event->setResult(false);
923 event->setExceptionCode(ExceptionCodes::PlatformException);
926 LogDebug("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 LogDebug("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 LogDebug("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 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
979 event->setResult(false);
980 event->setExceptionCode(ExceptionCodes::NotFoundException);
984 LogWarning("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 LogDebug("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 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1022 event->setResult(false);
1023 event->setExceptionCode(ExceptionCodes::NotFoundException);
1027 LogWarning("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 LogDebug("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 LogDebug("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 LogDebug("Statistics for contact.");
1074 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CONTACT));
1076 LogDebug("Statistics for event.");
1077 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CALENDAR));
1079 LogWarning("Unsupported category for statistics: "<<i);
1083 LogDebug("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 LogDebug("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 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1109 event->setResult(false);
1110 event->setExceptionCode(ExceptionCodes::NotFoundException);
1114 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1115 event->setResult(false);
1116 event->setExceptionCode(ExceptionCodes::PlatformException);
1120 sync_agent_ds_free_profile_info(profile_h);