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);
81 if(0==strcmp(error, "DONE")) {
82 // Stoped and completed cases not distinguished yet.
83 eventPtr->setSessionStatus(OnDataSyncStateChanged::COMPLETE_STATUS);
84 } else if(0==strcmp(error, "ERROR")) {
85 // Error cases should be transferred.
86 eventPtr->setSessionStatus(OnDataSyncStateChanged::FAIL_STATUS);
88 LogWarning("Wrong status.");
89 eventPtr->setSessionStatus(OnDataSyncStateChanged::UNDEFINED_STATUS);
92 eventPtr->setResult(true);
95 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
96 thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
102 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
106 g_free(profileDirName);
115 if (request != NULL) {
116 if (request->size != NULL) {
117 g_free(request->size);
125 static int datasync_progress_cb(sync_agent_event_data_s* request, void *user_data)
127 LogDebug("DataSync progress called.");
129 char *profileDirName = NULL;
132 char *progressStatus = NULL;
133 char *operationType = NULL;
137 OnDataSyncStateChangedPtr eventPtr(new OnDataSyncStateChanged());
138 DataSyncManager* thisDataSyncManager = (DataSyncManager*) user_data;
140 int isFromServer, totalPerOperation, syncedPerOperation, totalPerDb, syncedPerDb;
142 LogDebug("Get progress info.");
143 sync_agent_get_event_data_param(request, &profileDirName);
144 sync_agent_get_event_data_param(request, &syncType);
145 sync_agent_get_event_data_param(request, &uri);
146 sync_agent_get_event_data_param(request, &progressStatus);
147 sync_agent_get_event_data_param(request, &operationType);
149 LogDebug("profileDirName: "<<profileDirName<<", syncType: "<<syncType<<", uri: "<<uri<<", progressStatus: "<<progressStatus<<", operationType "<<operationType);
151 sync_agent_get_event_data_param(request, &isFromServer);
152 sync_agent_get_event_data_param(request, &totalPerOperation);
153 sync_agent_get_event_data_param(request, &syncedPerOperation);
154 sync_agent_get_event_data_param(request, &totalPerDb);
155 sync_agent_get_event_data_param(request, &syncedPerDb);
157 LogDebug("isFromServer: "<<isFromServer<<", totalPerOperation: "<<totalPerOperation<<", syncedPerOperation: "<<syncedPerOperation<<", totalPerDb: "<<totalPerDb<<", syncedPerDb "<<syncedPerDb);
160 eventPtr->setProfileId(profileDirName);
163 eventPtr->setSessionStatus(OnDataSyncStateChanged::PROGRESS_STATUS);
165 if(0==strcmp(uri, "Contacts")) {
166 eventPtr->setServiceType(SyncServiceInfo::CONTACT_SERVICE_TYPE);
167 } else if(0==strcmp(uri, "Organizer")) {
168 eventPtr->setServiceType(SyncServiceInfo::EVENT_SERVICE_TYPE);
170 LogWarning("Wrong service type.");
171 eventPtr->setServiceType(SyncServiceInfo::UNDEFINED_SERVICE_TYPE);
174 eventPtr->setIsFromServer(isFromServer);
175 eventPtr->setSynedPerService(syncedPerDb);
176 eventPtr->setTotalPerService(totalPerDb);
178 eventPtr->setResult(true);
181 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
182 thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
188 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
192 g_free(profileDirName);
198 g_free(progressStatus);
201 g_free(operationType);
204 if (request != NULL) {
205 if (request->size != NULL) {
206 g_free(request->size);
214 static sync_agent_ds_sync_mode_e convertToPlatformSyncMode(SyncInfo::SyncMode syncMode)
216 if(SyncInfo::MANUAL_MODE==syncMode) {
217 return SYNC_AGENT_SYNC_MODE_MANUAL;
218 } else if(SyncInfo::PERIODIC_MODE==syncMode) {
219 return SYNC_AGENT_SYNC_MODE_PERIODIC;
220 } else if(SyncInfo::PUSH_MODE==syncMode) {
221 return SYNC_AGENT_SYNC_MODE_PUSH;
223 LogWarning("Error while converting a sync mode.");
226 return SYNC_AGENT_SYNC_MODE_MANUAL;
229 static SyncInfo::SyncMode convertToSyncMode(sync_agent_ds_sync_mode_e syncMode)
231 if(SYNC_AGENT_SYNC_MODE_MANUAL==syncMode) {
232 return SyncInfo::MANUAL_MODE;
233 } else if(SYNC_AGENT_SYNC_MODE_PERIODIC==syncMode) {
234 return SyncInfo::PERIODIC_MODE;
235 } else if(SYNC_AGENT_SYNC_MODE_PUSH==syncMode) {
236 return SyncInfo::PUSH_MODE;
238 LogWarning("Error while converting a sync mode.");
241 return SyncInfo::UNDEFINED_MODE;
244 static sync_agent_ds_sync_type_e convertToPlatformSyncType(SyncInfo::SyncType syncType)
246 if(SyncInfo::TWO_WAY_TYPE==syncType) {
247 return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
248 } else if(SyncInfo::SLOW_TYPE==syncType) {
249 return SYNC_AGENT_SYNC_TYPE_FULL_SYNC;
250 } else if(SyncInfo::ONE_WAY_FROM_CLIENT_TYPE==syncType) {
251 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER;
252 } else if(SyncInfo::REFRESH_FROM_CLIENT_TYPE==syncType) {
253 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE;
254 } else if(SyncInfo::ONE_WAY_FROM_SERVER_TYPE==syncType) {
255 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE;
256 } else if(SyncInfo::REFRESH_FROM_SERVER_TYPE==syncType) {
257 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER;
259 LogWarning("Error while converting a sync type.");
262 return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
265 static SyncInfo::SyncType convertToSyncType(sync_agent_ds_sync_type_e syncType)
267 if(SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH==syncType) {
268 return SyncInfo::TWO_WAY_TYPE;
269 } else if(SYNC_AGENT_SYNC_TYPE_FULL_SYNC==syncType) {
270 return SyncInfo::SLOW_TYPE;
271 } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER==syncType) {
272 return SyncInfo::ONE_WAY_FROM_CLIENT_TYPE;
273 } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE==syncType) {
274 return SyncInfo::REFRESH_FROM_CLIENT_TYPE;
275 } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE==syncType) {
276 return SyncInfo::ONE_WAY_FROM_SERVER_TYPE;
277 } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER==syncType) {
278 return SyncInfo::REFRESH_FROM_SERVER_TYPE;
280 LogWarning("Error while converting a sync type.");
283 return SyncInfo::UNDEFINED_TYPE;
286 static sync_agent_ds_sync_interval_e convertToPlatformSyncInterval(SyncInfo::SyncInterval syncInterval)
288 if(SyncInfo::INTERVAL_5_MINUTES==syncInterval) {
289 return SYNC_AGENT_SYNC_INTERVAL_5_MINUTES;
290 } else if(SyncInfo::INTERVAL_15_MINUTES==syncInterval) {
291 return SYNC_AGENT_SYNC_INTERVAL_15_MINUTES;
292 } else if(SyncInfo::INTERVAL_1_HOUR==syncInterval) {
293 return SYNC_AGENT_SYNC_INTERVAL_1_HOUR;
294 } else if(SyncInfo::INTERVAL_4_HOURS==syncInterval) {
295 return SYNC_AGENT_SYNC_INTERVAL_4_HOURS;
296 } else if(SyncInfo::INTERVAL_12_HOURS==syncInterval) {
297 return SYNC_AGENT_SYNC_INTERVAL_12_HOURS;
298 } else if(SyncInfo::INTERVAL_1_DAY==syncInterval) {
299 return SYNC_AGENT_SYNC_INTERVAL_1_DAY;
300 } else if(SyncInfo::INTERVAL_1_WEEK==syncInterval) {
301 return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
302 } else if(SyncInfo::INTERVAL_1_MONTH==syncInterval) {
303 return SYNC_AGENT_SYNC_INTERVAL_1_MONTH;
305 LogWarning("Error while converting a sync interval.");
308 return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
311 static SyncInfo::SyncInterval convertToSyncInterval(sync_agent_ds_sync_interval_e syncInterval)
313 if(SYNC_AGENT_SYNC_INTERVAL_5_MINUTES==syncInterval) {
314 return SyncInfo::INTERVAL_5_MINUTES;
315 } else if(SYNC_AGENT_SYNC_INTERVAL_15_MINUTES==syncInterval) {
316 return SyncInfo::INTERVAL_15_MINUTES;
317 } else if(SYNC_AGENT_SYNC_INTERVAL_1_HOUR==syncInterval) {
318 return SyncInfo::INTERVAL_1_HOUR;
319 } else if(SYNC_AGENT_SYNC_INTERVAL_4_HOURS==syncInterval) {
320 return SyncInfo::INTERVAL_4_HOURS;
321 } else if(SYNC_AGENT_SYNC_INTERVAL_12_HOURS==syncInterval) {
322 return SyncInfo::INTERVAL_12_HOURS;
323 } else if(SYNC_AGENT_SYNC_INTERVAL_1_DAY==syncInterval) {
324 return SyncInfo::INTERVAL_1_DAY;
325 } else if(SYNC_AGENT_SYNC_INTERVAL_1_WEEK==syncInterval) {
326 return SyncInfo::INTERVAL_1_WEEK;
327 } else if(SYNC_AGENT_SYNC_INTERVAL_1_MONTH==syncInterval) {
328 return SyncInfo::INTERVAL_1_MONTH;
330 LogWarning("Error while converting a sync interval.");
333 return SyncInfo::INTERVAL_UNDEFINED;
336 static sync_agent_ds_service_type_e convertToPlatformSyncServiceType(SyncServiceInfo::SyncServiceType serviceType)
338 if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
339 return SYNC_AGENT_CONTACT;
340 } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
341 return SYNC_AGENT_CALENDAR;
343 LogWarning("Error while converting a sync service type.");
346 return SYNC_AGENT_CONTACT;
349 static SyncServiceInfo::SyncServiceType convertToSyncServiceType(sync_agent_ds_service_type_e serviceType)
351 if(SYNC_AGENT_CONTACT==serviceType) {
352 return SyncServiceInfo::CONTACT_SERVICE_TYPE;
353 } else if(SYNC_AGENT_CALENDAR==serviceType) {
354 return SyncServiceInfo::EVENT_SERVICE_TYPE;
356 LogWarning("Error while converting a sync service type.");
359 return SyncServiceInfo::UNDEFINED_SERVICE_TYPE;
362 static sync_agent_ds_src_uri_e convertToPlatformSourceUri(SyncServiceInfo::SyncServiceType serviceType)
364 if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
365 return SYNC_AGENT_SRC_URI_CONTACT;
366 } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
367 return SYNC_AGENT_SRC_URI_CALENDAR;
369 LogWarning("Error while converting a sync service.");
372 return SYNC_AGENT_SRC_URI_CONTACT;
375 static SyncStatistics::SyncStatus convertToSyncStatus(char* status)
377 if(0==strcmp(status, "success")) {
378 return SyncStatistics::SUCCESS_STATUS;
379 } else if(0==strcmp(status, "stop")) {
380 return SyncStatistics::STOP_STATUS;
381 } else if(0==strcmp(status, "fail")) {
382 return SyncStatistics::FAIL_STATUS;
383 } else if(0==strcmp(status, "No")) {
384 return SyncStatistics::NONE_STATUS;
386 LogWarning("Error while converting a sync status.");
389 return SyncStatistics::UNDEFINED_STATUS;
392 void DataSyncManager::OnRequestReceived(const IEventAddProfilePtr &event)
394 ds_profile_h profile_h = NULL;
398 // Check if the quota is full first.
399 GList *profile_list = NULL;
402 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
404 ret = sync_agent_ds_get_all_profile(&profile_list);
405 if (SYNC_AGENT_DS_SUCCESS!=ret) {
406 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
409 int numProfiles = g_list_length(profile_list);
410 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
411 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
414 g_list_free(profile_list);
416 LogDebug("numProfiles: "<<numProfiles);
417 if(MAX_PROFILES_NUM==numProfiles) {
418 ThrowMsg(OutOfRangeException, "There are already maximum number of profiles!");
421 SyncProfileInfoPtr profile;
422 profile = event->getProfile();
424 ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
427 LogDebug("url: "<<profile->getSyncInfo()->getUrl());
429 ret = sync_agent_ds_create_profile_info(&profile_h);
430 if (SYNC_AGENT_DS_SUCCESS!=ret) {
431 ThrowMsg(PlatformException, "Platform error while creating a profile: "<<ret);
434 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
435 if (SYNC_AGENT_DS_SUCCESS!=ret) {
436 ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
439 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()));
440 if (SYNC_AGENT_DS_SUCCESS!=ret) {
441 ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
444 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
445 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
446 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
447 LogDebug("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
449 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
450 if (SYNC_AGENT_DS_SUCCESS!=ret) {
451 ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
454 // Set the sync categories.
455 SyncServiceInfoListPtr categories = profile->getServiceInfo();
456 sync_agent_ds_service_type_e serviceType;
457 sync_agent_ds_src_uri_e srcURI;
458 std::string tgtURI, id, password;
460 for(unsigned int i=0; categories->size()>i; i++) {
461 serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
462 tgtURI = categories->at(i)->getServerDatabaseUri();
463 srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
464 id = categories->at(i)->getId();
465 password = categories->at(i)->getPassword();
466 enable = categories->at(i)->getEnable();
468 LogDebug("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<", id: "<<id<<" for index: "<<i);
470 ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
471 0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
472 if (SYNC_AGENT_DS_SUCCESS!=ret) {
473 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
478 ret = sync_agent_ds_add_profile(profile_h, &profileId);
479 if (SYNC_AGENT_DS_SUCCESS!=ret) {
480 ThrowMsg(PlatformException, "Platform error while adding a profile: "<<ret);
483 LogDebug("profileId from platform: "<<profileId);
485 char* profileName = NULL;
486 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
487 if (SYNC_AGENT_DS_SUCCESS!=ret) {
488 ThrowMsg(PlatformException, "Platform error while getting a profile name: "<<ret);
491 LogDebug("profileName: "<<profileName<<", profileId: "<<profileId);
493 std::stringstream ss;
495 profile->setProfileId(ss.str());
501 event->setResult(true);
503 Catch(OutOfRangeException)
505 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
506 event->setResult(false);
507 event->setExceptionCode(ExceptionCodes::OutOfRangeException);
511 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
512 event->setResult(false);
513 event->setExceptionCode(ExceptionCodes::PlatformException);
517 sync_agent_ds_free_profile_info(profile_h);
521 void DataSyncManager::OnRequestReceived(const IEventUpdateProfilePtr &event)
523 ds_profile_h profile_h = NULL;
527 SyncProfileInfoPtr profile;
528 profile = event->getProfile();
530 ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
533 LogDebug("id: "<<profile->getProfileId());
535 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
538 std::stringstream ss(profile->getProfileId());
540 ret = sync_agent_ds_get_profile(profileId, &profile_h);
541 if (SYNC_AGENT_DS_SUCCESS!=ret) {
542 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
545 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
546 if (SYNC_AGENT_DS_SUCCESS!=ret) {
547 ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
550 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()));
551 if (SYNC_AGENT_DS_SUCCESS!=ret) {
552 ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
555 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
556 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
557 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
558 LogDebug("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
560 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
561 if (SYNC_AGENT_DS_SUCCESS!=ret) {
562 ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
565 // Set the sync categories.
566 SyncServiceInfoListPtr categories = profile->getServiceInfo();
567 sync_agent_ds_service_type_e serviceType;
568 sync_agent_ds_src_uri_e srcURI;
569 std::string tgtURI, id, password;
571 for(unsigned int i=0; categories->size()<i; i++) {
572 serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
573 tgtURI = categories->at(i)->getServerDatabaseUri();
574 srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
575 id = categories->at(i)->getId();
576 password = categories->at(i)->getPassword();
577 enable = categories->at(i)->getEnable();
579 LogDebug("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<", id: "<<id<<" for index: "<<i);
581 ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
582 0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
583 if (SYNC_AGENT_DS_SUCCESS!=ret) {
584 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
588 ret = sync_agent_ds_update_profile(profile_h);
589 if (SYNC_AGENT_DS_SUCCESS!=ret) {
590 ThrowMsg(NotFoundException, "Platform error while updating a profile: "<<ret);
593 event->setResult(true);
595 Catch(NotFoundException)
597 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
598 event->setResult(false);
599 event->setExceptionCode(ExceptionCodes::NotFoundException);
603 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
604 event->setResult(false);
605 event->setExceptionCode(ExceptionCodes::PlatformException);
609 sync_agent_ds_free_profile_info(profile_h);
613 void DataSyncManager::OnRequestReceived(const IEventRemoveProfilePtr &event)
615 ds_profile_h profile_h = NULL;
619 LogDebug("id: "<<event->getProfileId());
621 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
624 std::stringstream ss(event->getProfileId());
626 ret = sync_agent_ds_get_profile(profileId, &profile_h);
627 if (SYNC_AGENT_DS_SUCCESS!=ret) {
628 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
631 ret = sync_agent_ds_delete_profile(profile_h);
632 if (SYNC_AGENT_DS_SUCCESS!=ret) {
633 ThrowMsg(PlatformException, "Platform error while deleting a profile: "<<ret);
636 event->setResult(true);
638 Catch(NotFoundException)
640 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
641 event->setResult(false);
642 event->setExceptionCode(ExceptionCodes::NotFoundException);
646 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
647 event->setResult(false);
648 event->setExceptionCode(ExceptionCodes::PlatformException);
652 sync_agent_ds_free_profile_info(profile_h);
656 void DataSyncManager::OnRequestReceived(const IEventGetMaxProfilesNumPtr &event)
660 LogDebug("Return maximum number of supported profiles: "<<MAX_PROFILES_NUM);
662 event->setNumMaxProfiles(MAX_PROFILES_NUM);
664 event->setResult(true);
668 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
669 event->setResult(false);
670 event->setExceptionCode(ExceptionCodes::PlatformException);
674 void DataSyncManager::OnRequestReceived(const IEventGetProfilesNumPtr &event)
676 GList *profile_list = NULL;
681 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
683 ret = sync_agent_ds_get_all_profile(&profile_list);
684 if (SYNC_AGENT_DS_SUCCESS!=ret) {
685 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
689 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
690 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
691 LogDebug("Free sync_agent_ds_profile_info for index: "<<numProfiles++);
694 LogDebug("numProfiles: "<<numProfiles);
696 event->setNumProfiles(numProfiles);
698 event->setResult(true);
702 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
703 event->setResult(false);
704 event->setExceptionCode(ExceptionCodes::PlatformException);
708 void DataSyncManager::OnRequestReceived(const IEventGetProfilePtr &event)
710 ds_profile_h profile_h = NULL;
714 LogDebug("id: "<<event->getProfileId());
716 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
719 std::stringstream ss(event->getProfileId());
721 ret = sync_agent_ds_get_profile(profileId, &profile_h);
722 if (SYNC_AGENT_DS_SUCCESS!=ret) {
723 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
726 SyncProfileInfoPtr profile( new SyncProfileInfo() );
728 profile->setProfileId(event->getProfileId());
730 char *profileName = NULL;
731 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
732 if (SYNC_AGENT_DS_SUCCESS!=ret) {
733 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
735 profile->setProfileName(profileName);
737 sync_agent_ds_server_info server_info = { NULL };
738 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
739 if (SYNC_AGENT_DS_SUCCESS!=ret) {
740 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
742 profile->getSyncInfo()->setUrl(server_info.addr);
743 profile->getSyncInfo()->setId(server_info.id);
744 profile->getSyncInfo()->setPassword(server_info.password);
746 LogDebug("Url: "<<profile->getSyncInfo()->getUrl());
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 LogDebug("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 LogDebug("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 LogDebug("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 LogDebug("Service type: "<<serviceInfo->getSyncServiceType()<<", DB URI: "<<serviceInfo->getServerDatabaseUri());
788 profile->getServiceInfo()->push_back(serviceInfo);
791 g_list_free(category_list);
794 event->setProfile(profile);
796 event->setResult(true);
798 Catch(NotFoundException)
800 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
801 event->setResult(false);
802 event->setExceptionCode(ExceptionCodes::NotFoundException);
806 LogWarning("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 LogDebug("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 LogDebug("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 LogDebug("Url: "<<profile->getSyncInfo()->getUrl());
866 sync_agent_ds_sync_info sync_info;
867 ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
868 if (SYNC_AGENT_DS_SUCCESS!=ret) {
869 ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
871 profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
872 profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
873 profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
875 LogDebug("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
877 GList *category_list = NULL;
878 sync_agent_ds_service_info *category_info = NULL;
879 ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
880 if (SYNC_AGENT_DS_SUCCESS!=ret) {
881 ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
883 int category_count = g_list_length(category_list);
884 LogDebug("category_count: "<<category_count);
885 while(category_count--) {
886 category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
887 if(SYNC_AGENT_CALENDAR<category_info->service_type) {
888 LogDebug("Skip unsupported sync service type: "<<category_info->service_type);
892 SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
893 serviceInfo->setEnable(category_info->enabled);
894 if(category_info->id) {
895 serviceInfo->setId(category_info->id);
897 if(category_info->password) {
898 serviceInfo->setPassword(category_info->password);
900 serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
901 if(category_info->tgt_uri) {
902 serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
905 LogDebug("Service type: "<<serviceInfo->getSyncServiceType()<<", DB URI: "<<serviceInfo->getServerDatabaseUri());
906 profile->getServiceInfo()->push_back(serviceInfo);
909 g_list_free(category_list);
912 LogDebug("Adding a profile to the list.");
913 event->getProfiles()->push_back(profile);
916 event->setResult(true);
920 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
921 event->setResult(false);
922 event->setExceptionCode(ExceptionCodes::PlatformException);
925 LogDebug("Free profiles list.");
926 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
927 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
930 g_list_free(profile_list);
934 void DataSyncManager::OnRequestReceived(const IEventStartSyncPtr &event)
936 ds_profile_h profile_h = NULL;
940 LogDebug("id: "<<event->getProfileId());
942 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
943 sync_agent_event_error_e err= SYNC_AGENT_EVENT_FAIL;
946 std::stringstream ss(event->getProfileId());
948 ret = sync_agent_ds_get_profile(profileId, &profile_h);
949 if (SYNC_AGENT_DS_SUCCESS!=ret) {
950 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
953 err = sync_agent_set_noti_callback(1, datasync_state_changed_cb, this);
954 if (SYNC_AGENT_EVENT_SUCCESS != err) {
955 ThrowMsg(PlatformException, "Platform error while setting state changed cb: " << err);
958 err = sync_agent_set_noti_callback(2, datasync_progress_cb, this);
959 if (SYNC_AGENT_EVENT_SUCCESS != err) {
960 ThrowMsg(PlatformException, "Platform error while setting progress cb: " << err);
963 ret = sync_agent_ds_start_sync(profile_h);
964 if (SYNC_AGENT_DS_SUCCESS!=ret) {
965 ThrowMsg(PlatformException, "Platform error while starting a profile: "<<ret);
968 if(event->getEmitter()) {
969 LogDebug("Attaching the emitter with profileId: "<<event->getProfileId());
970 m_changeEmitters[event->getProfileId()] = event->getEmitter();
973 event->setResult(true);
975 Catch(NotFoundException)
977 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
978 event->setResult(false);
979 event->setExceptionCode(ExceptionCodes::NotFoundException);
983 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
984 event->setResult(false);
985 event->setExceptionCode(ExceptionCodes::PlatformException);
989 sync_agent_ds_free_profile_info(profile_h);
993 void DataSyncManager::OnRequestReceived(const IEventStopSyncPtr &event)
995 ds_profile_h profile_h = NULL;
999 LogDebug("id: "<<event->getProfileId());
1001 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
1004 std::stringstream ss(event->getProfileId());
1006 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1007 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1008 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1011 ret = sync_agent_ds_stop_sync(profile_h);
1012 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1013 ThrowMsg(PlatformException, "Platform error while stopping a profile: "<<ret);
1016 event->setResult(true);
1018 Catch(NotFoundException)
1020 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1021 event->setResult(false);
1022 event->setExceptionCode(ExceptionCodes::NotFoundException);
1026 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1027 event->setResult(false);
1028 event->setExceptionCode(ExceptionCodes::PlatformException);
1032 sync_agent_ds_free_profile_info(profile_h);
1037 void DataSyncManager::OnRequestReceived(const IEventGetLastSyncStatisticsPtr &event)
1039 ds_profile_h profile_h = NULL;
1043 LogDebug("id: "<<event->getProfileId());
1045 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
1048 std::stringstream ss(event->getProfileId());
1050 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1051 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1052 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1055 SyncStatisticsListPtr statisticsList( new SyncStatisticsList() );
1057 GList *statistics_list = NULL;
1058 ret = sync_agent_ds_get_sync_statistics(profile_h, &statistics_list);
1059 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1060 ThrowMsg(PlatformException, "Platform error while gettting sync statistics: "<<ret);
1063 int statistics_count = g_list_length(statistics_list);
1064 LogDebug("statistics_count: "<<statistics_count);
1065 sync_agent_ds_statistics_info *statistics = NULL;
1066 for (int i = 0; i < statistics_count; i++) {
1067 statistics = (sync_agent_ds_statistics_info *) g_list_nth_data(statistics_list, i);
1069 SyncStatisticsPtr statisticsPtr( new SyncStatistics() );
1072 LogDebug("Statistics for contact.");
1073 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CONTACT));
1075 LogDebug("Statistics for event.");
1076 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CALENDAR));
1078 LogWarning("Unsupported category for statistics: "<<i);
1082 LogDebug("dbsynced: "<<statistics->dbsynced);
1083 statisticsPtr->setSyncStatus(convertToSyncStatus(statistics->dbsynced));
1084 statisticsPtr->setClientToServerTotal(statistics->client2server_total);
1085 statisticsPtr->setClientToServerAdded(statistics->client2server_nrofadd);
1086 statisticsPtr->setClientToServerUpdated(statistics->client2server_nrofreplace);
1087 statisticsPtr->setClientToServerRemoved(statistics->client2server_nrofdelete);
1088 statisticsPtr->setServerToClientTotal(statistics->server2client_total);
1089 statisticsPtr->setServerToClientAdded(statistics->server2client_nrofadd);
1090 statisticsPtr->setServerToClientUpdated(statistics->server2client_nrofreplace);
1091 statisticsPtr->setServerToClientRemoved(statistics->server2client_nrofdelete);
1093 LogDebug("ClientToServerTotal: "<<statisticsPtr->getClientToServerTotal()<<", ServerToClientTotal: "<<statisticsPtr->getServerToClientTotal());
1095 statisticsList->push_back(statisticsPtr);
1097 if(statistics_list) {
1098 g_list_free(statistics_list);
1101 event->setSyncStatictics(statisticsList);
1103 event->setResult(true);
1105 Catch(NotFoundException)
1107 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1108 event->setResult(false);
1109 event->setExceptionCode(ExceptionCodes::NotFoundException);
1113 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1114 event->setResult(false);
1115 event->setExceptionCode(ExceptionCodes::PlatformException);
1119 sync_agent_ds_free_profile_info(profile_h);