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 LoggerI("profileDirName: "<<profileDirName<<", sync_type: "<<sync_type<<", progress: "<<progress<<", error: "<<error);
84 eventPtr->setProfileId(profileDirName);
85 if(4<strnlen(profileDirName, 5)) {
86 eventPtr->setProfileId(eventPtr->getProfileId().substr(4));
91 LoggerW("Null status.");
92 eventPtr->setSessionStatus(OnDataSyncStateChanged::UNDEFINED_STATUS);
93 } else if(0==strncmp(progress, "DONE", 4)) {
94 eventPtr->setSessionStatus(OnDataSyncStateChanged::COMPLETE_STATUS);
95 } else if(0==strncmp(progress, "CANCEL", 6)) {
96 eventPtr->setSessionStatus(OnDataSyncStateChanged::STOP_STATUS);
97 } else if(0==strncmp(progress, "ERROR", 5)) {
98 // Error cases should be redefined and transferred based on the error value.
99 eventPtr->setSessionStatus(OnDataSyncStateChanged::FAIL_STATUS);
101 LoggerI("Undefined status: "<<progress);
102 eventPtr->setSessionStatus(OnDataSyncStateChanged::UNDEFINED_STATUS);
105 eventPtr->setResult(true);
108 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
109 thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
115 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
119 g_free(profileDirName);
128 if (request != NULL) {
129 if (request->size != NULL) {
130 g_free(request->size);
138 static int datasync_progress_cb(sync_agent_event_data_s* request, void *user_data)
140 LoggerD("DataSync progress called.");
142 char *profileDirName = NULL;
145 char *progressStatus = NULL;
146 char *operationType = NULL;
150 OnDataSyncStateChangedPtr eventPtr(new OnDataSyncStateChanged());
151 DataSyncManager* thisDataSyncManager = (DataSyncManager*) user_data;
153 int isFromServer, totalPerOperation, syncedPerOperation, totalPerDb, syncedPerDb;
155 LoggerD("Get progress info.");
156 sync_agent_get_event_data_param(request, &profileDirName);
157 sync_agent_get_event_data_param(request, &syncType);
158 sync_agent_get_event_data_param(request, &uri);
159 sync_agent_get_event_data_param(request, &progressStatus);
160 sync_agent_get_event_data_param(request, &operationType);
162 LoggerI("profileDirName: "<<profileDirName<<", syncType: "<<syncType<<", uri: "<<uri<<", progressStatus: "<<progressStatus<<", operationType "<<operationType);
164 sync_agent_get_event_data_param(request, &isFromServer);
165 sync_agent_get_event_data_param(request, &totalPerOperation);
166 sync_agent_get_event_data_param(request, &syncedPerOperation);
167 sync_agent_get_event_data_param(request, &totalPerDb);
168 sync_agent_get_event_data_param(request, &syncedPerDb);
170 LoggerI("isFromServer: "<<isFromServer<<", totalPerOperation: "<<totalPerOperation<<", syncedPerOperation: "<<syncedPerOperation<<", totalPerDb: "<<totalPerDb<<", syncedPerDb "<<syncedPerDb);
173 eventPtr->setProfileId(profileDirName);
174 if(4<strnlen(profileDirName, 5)) {
175 eventPtr->setProfileId(eventPtr->getProfileId().substr(4));
179 eventPtr->setSessionStatus(OnDataSyncStateChanged::PROGRESS_STATUS);
181 if(SYNC_AGENT_SRC_URI_CONTACT==uri) {
182 eventPtr->setServiceType(SyncServiceInfo::CONTACT_SERVICE_TYPE);
183 } else if(SYNC_AGENT_SRC_URI_CALENDAR==uri) {
184 eventPtr->setServiceType(SyncServiceInfo::EVENT_SERVICE_TYPE);
186 LoggerW("Wrong service type.");
187 eventPtr->setServiceType(SyncServiceInfo::UNDEFINED_SERVICE_TYPE);
190 eventPtr->setIsFromServer(isFromServer);
191 eventPtr->setSynedPerService(syncedPerDb);
192 eventPtr->setTotalPerService(totalPerDb);
194 eventPtr->setResult(true);
197 if (thisDataSyncManager->m_changeEmitters[profileDirName]) {
198 thisDataSyncManager->m_changeEmitters[profileDirName]->emit(eventPtr);
204 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
208 g_free(profileDirName);
211 g_free(progressStatus);
214 g_free(operationType);
217 if (request != NULL) {
218 if (request->size != NULL) {
219 g_free(request->size);
227 static sync_agent_ds_sync_mode_e convertToPlatformSyncMode(SyncInfo::SyncMode syncMode)
229 if(SyncInfo::MANUAL_MODE==syncMode) {
230 return SYNC_AGENT_SYNC_MODE_MANUAL;
231 } else if(SyncInfo::PERIODIC_MODE==syncMode) {
232 return SYNC_AGENT_SYNC_MODE_PERIODIC;
233 } else if(SyncInfo::PUSH_MODE==syncMode) {
234 return SYNC_AGENT_SYNC_MODE_PUSH;
236 LoggerW("Error while converting a sync mode.");
239 return SYNC_AGENT_SYNC_MODE_MANUAL;
242 static SyncInfo::SyncMode convertToSyncMode(sync_agent_ds_sync_mode_e syncMode)
244 if(SYNC_AGENT_SYNC_MODE_MANUAL==syncMode) {
245 return SyncInfo::MANUAL_MODE;
246 } else if(SYNC_AGENT_SYNC_MODE_PERIODIC==syncMode) {
247 return SyncInfo::PERIODIC_MODE;
248 } else if(SYNC_AGENT_SYNC_MODE_PUSH==syncMode) {
249 return SyncInfo::PUSH_MODE;
251 LoggerW("Error while converting a sync mode.");
254 return SyncInfo::UNDEFINED_MODE;
257 static sync_agent_ds_sync_type_e convertToPlatformSyncType(SyncInfo::SyncType syncType)
259 if(SyncInfo::TWO_WAY_TYPE==syncType) {
260 return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
261 } else if(SyncInfo::SLOW_TYPE==syncType) {
262 return SYNC_AGENT_SYNC_TYPE_FULL_SYNC;
263 } else if(SyncInfo::ONE_WAY_FROM_CLIENT_TYPE==syncType) {
264 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER;
265 } else if(SyncInfo::REFRESH_FROM_CLIENT_TYPE==syncType) {
266 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE;
267 } else if(SyncInfo::ONE_WAY_FROM_SERVER_TYPE==syncType) {
268 return SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE;
269 } else if(SyncInfo::REFRESH_FROM_SERVER_TYPE==syncType) {
270 return SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER;
272 LoggerW("Error while converting a sync type.");
275 return SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH;
278 static SyncInfo::SyncType convertToSyncType(sync_agent_ds_sync_type_e syncType)
280 if(SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH==syncType) {
281 return SyncInfo::TWO_WAY_TYPE;
282 } else if(SYNC_AGENT_SYNC_TYPE_FULL_SYNC==syncType) {
283 return SyncInfo::SLOW_TYPE;
284 } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER==syncType) {
285 return SyncInfo::ONE_WAY_FROM_CLIENT_TYPE;
286 } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE==syncType) {
287 return SyncInfo::REFRESH_FROM_CLIENT_TYPE;
288 } else if(SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE==syncType) {
289 return SyncInfo::ONE_WAY_FROM_SERVER_TYPE;
290 } else if(SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER==syncType) {
291 return SyncInfo::REFRESH_FROM_SERVER_TYPE;
293 LoggerW("Error while converting a sync type.");
296 return SyncInfo::UNDEFINED_TYPE;
299 static sync_agent_ds_sync_interval_e convertToPlatformSyncInterval(SyncInfo::SyncInterval syncInterval)
301 if(SyncInfo::INTERVAL_5_MINUTES==syncInterval) {
302 return SYNC_AGENT_SYNC_INTERVAL_5_MINUTES;
303 } else if(SyncInfo::INTERVAL_15_MINUTES==syncInterval) {
304 return SYNC_AGENT_SYNC_INTERVAL_15_MINUTES;
305 } else if(SyncInfo::INTERVAL_1_HOUR==syncInterval) {
306 return SYNC_AGENT_SYNC_INTERVAL_1_HOUR;
307 } else if(SyncInfo::INTERVAL_4_HOURS==syncInterval) {
308 return SYNC_AGENT_SYNC_INTERVAL_4_HOURS;
309 } else if(SyncInfo::INTERVAL_12_HOURS==syncInterval) {
310 return SYNC_AGENT_SYNC_INTERVAL_12_HOURS;
311 } else if(SyncInfo::INTERVAL_1_DAY==syncInterval) {
312 return SYNC_AGENT_SYNC_INTERVAL_1_DAY;
313 } else if(SyncInfo::INTERVAL_1_WEEK==syncInterval) {
314 return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
315 } else if(SyncInfo::INTERVAL_1_MONTH==syncInterval) {
316 return SYNC_AGENT_SYNC_INTERVAL_1_MONTH;
317 } else if(SyncInfo::INTERVAL_UNDEFINED==syncInterval) {
318 return SYNC_AGENT_SYNC_INTERVAL_NONE;
320 LoggerW("Error while converting a JS sync interval.");
323 return SYNC_AGENT_SYNC_INTERVAL_1_WEEK;
326 static SyncInfo::SyncInterval convertToSyncInterval(sync_agent_ds_sync_interval_e syncInterval)
328 if(SYNC_AGENT_SYNC_INTERVAL_5_MINUTES==syncInterval) {
329 return SyncInfo::INTERVAL_5_MINUTES;
330 } else if(SYNC_AGENT_SYNC_INTERVAL_15_MINUTES==syncInterval) {
331 return SyncInfo::INTERVAL_15_MINUTES;
332 } else if(SYNC_AGENT_SYNC_INTERVAL_1_HOUR==syncInterval) {
333 return SyncInfo::INTERVAL_1_HOUR;
334 } else if(SYNC_AGENT_SYNC_INTERVAL_4_HOURS==syncInterval) {
335 return SyncInfo::INTERVAL_4_HOURS;
336 } else if(SYNC_AGENT_SYNC_INTERVAL_12_HOURS==syncInterval) {
337 return SyncInfo::INTERVAL_12_HOURS;
338 } else if(SYNC_AGENT_SYNC_INTERVAL_1_DAY==syncInterval) {
339 return SyncInfo::INTERVAL_1_DAY;
340 } else if(SYNC_AGENT_SYNC_INTERVAL_1_WEEK==syncInterval) {
341 return SyncInfo::INTERVAL_1_WEEK;
342 } else if(SYNC_AGENT_SYNC_INTERVAL_1_MONTH==syncInterval) {
343 return SyncInfo::INTERVAL_1_MONTH;
344 } else if(SYNC_AGENT_SYNC_INTERVAL_NONE==syncInterval) {
345 return SyncInfo::INTERVAL_UNDEFINED;
347 LoggerW("Error while converting a platform sync interval.");
350 return SyncInfo::INTERVAL_UNDEFINED;
353 static sync_agent_ds_service_type_e convertToPlatformSyncServiceType(SyncServiceInfo::SyncServiceType serviceType)
355 if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
356 return SYNC_AGENT_CONTACT;
357 } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
358 return SYNC_AGENT_CALENDAR;
360 LoggerW("Error while converting a sync service type.");
363 return SYNC_AGENT_CONTACT;
366 static SyncServiceInfo::SyncServiceType convertToSyncServiceType(sync_agent_ds_service_type_e serviceType)
368 if(SYNC_AGENT_CONTACT==serviceType) {
369 return SyncServiceInfo::CONTACT_SERVICE_TYPE;
370 } else if(SYNC_AGENT_CALENDAR==serviceType) {
371 return SyncServiceInfo::EVENT_SERVICE_TYPE;
373 LoggerW("Error while converting a sync service type.");
376 return SyncServiceInfo::UNDEFINED_SERVICE_TYPE;
379 static sync_agent_ds_src_uri_e convertToPlatformSourceUri(SyncServiceInfo::SyncServiceType serviceType)
381 if(SyncServiceInfo::CONTACT_SERVICE_TYPE==serviceType) {
382 return SYNC_AGENT_SRC_URI_CONTACT;
383 } else if(SyncServiceInfo::EVENT_SERVICE_TYPE==serviceType) {
384 return SYNC_AGENT_SRC_URI_CALENDAR;
386 LoggerW("Error while converting a sync service.");
389 return SYNC_AGENT_SRC_URI_CONTACT;
392 static SyncStatistics::SyncStatus convertToSyncStatus(char* status)
394 if(0==strncmp(status, "success", 7)) {
395 return SyncStatistics::SUCCESS_STATUS;
396 } else if(0==strncmp(status, "stop", 4)) {
397 return SyncStatistics::STOP_STATUS;
398 } else if(0==strncmp(status, "fail", 4)) {
399 return SyncStatistics::FAIL_STATUS;
400 } else if(0==strncmp(status, "No", 2)) {
401 return SyncStatistics::NONE_STATUS;
403 LoggerW("Error while converting a sync status.");
406 return SyncStatistics::NONE_STATUS;
409 void DataSyncManager::OnRequestReceived(const IEventAddProfilePtr &event)
411 ds_profile_h profile_h = NULL;
415 // Check if the quota is full first.
416 GList *profile_list = NULL;
419 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
421 ret = sync_agent_ds_get_all_profile(&profile_list);
422 if (SYNC_AGENT_DS_SUCCESS!=ret) {
423 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
426 int numProfiles = g_list_length(profile_list);
427 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
428 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
431 g_list_free(profile_list);
433 LoggerD("numProfiles: "<<numProfiles);
434 if(MAX_PROFILES_NUM==numProfiles) {
435 ThrowMsg(OutOfRangeException, "There are already maximum number of profiles!");
438 SyncProfileInfoPtr profile;
439 profile = event->getProfile();
441 ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
444 ret = sync_agent_ds_create_profile_info(&profile_h);
445 if (SYNC_AGENT_DS_SUCCESS!=ret) {
446 ThrowMsg(PlatformException, "Platform error while creating a profile: "<<ret);
449 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
450 if (SYNC_AGENT_DS_SUCCESS!=ret) {
451 ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
454 ret = sync_agent_ds_set_server_info(profile_h, (char*)(profile->getSyncInfo()->getUrl().c_str()), (char*)(profile->getSyncInfo()->getId().c_str()), (char*)(profile->getSyncInfo()->getPassword().c_str()));
455 if (SYNC_AGENT_DS_SUCCESS!=ret) {
456 ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
459 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
460 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
461 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
462 LoggerD("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
464 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
465 if (SYNC_AGENT_DS_SUCCESS!=ret) {
466 ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
469 // Set the sync categories.
470 SyncServiceInfoListPtr categories = profile->getServiceInfo();
471 sync_agent_ds_service_type_e serviceType;
472 sync_agent_ds_src_uri_e srcURI;
473 std::string tgtURI, id, password;
475 for(unsigned int i=0; categories->size()>i; i++) {
476 serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
477 tgtURI = categories->at(i)->getServerDatabaseUri();
478 srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
479 id = categories->at(i)->getId();
480 password = categories->at(i)->getPassword();
481 enable = categories->at(i)->getEnable();
483 LoggerI("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<", enable: "<<enable<<" for index: "<<i);
485 ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
486 0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
487 if (SYNC_AGENT_DS_SUCCESS!=ret) {
488 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
493 ret = sync_agent_ds_add_profile(profile_h, &profileId);
494 if (SYNC_AGENT_DS_SUCCESS!=ret) {
495 ThrowMsg(PlatformException, "Platform error while adding a profile: "<<ret);
498 LoggerD("profileId from platform: "<<profileId);
500 char* profileName = NULL;
501 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
502 if (SYNC_AGENT_DS_SUCCESS!=ret) {
503 ThrowMsg(PlatformException, "Platform error while getting a profile name: "<<ret);
506 LoggerD("profileName: "<<profileName<<", profileId: "<<profileId);
508 std::stringstream ss;
510 profile->setProfileId(ss.str());
516 event->setResult(true);
518 Catch(OutOfRangeException)
520 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
521 event->setResult(false);
522 event->setExceptionCode(ExceptionCodes::OutOfRangeException);
526 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
527 event->setResult(false);
528 event->setExceptionCode(ExceptionCodes::PlatformException);
532 sync_agent_ds_free_profile_info(profile_h);
536 void DataSyncManager::OnRequestReceived(const IEventUpdateProfilePtr &event)
538 ds_profile_h profile_h = NULL;
542 SyncProfileInfoPtr profile;
543 profile = event->getProfile();
545 ThrowMsg(NullPointerException, "SyncProfileInfo is NULL.");
548 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
551 std::stringstream ss(profile->getProfileId());
553 LoggerD("profileId: "<<profileId);
554 ret = sync_agent_ds_get_profile(profileId, &profile_h);
555 if (SYNC_AGENT_DS_SUCCESS!=ret) {
556 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
559 ret = sync_agent_ds_set_profile_name(profile_h, (char*)(profile->getProfileName().c_str()));
560 if (SYNC_AGENT_DS_SUCCESS!=ret) {
561 ThrowMsg(PlatformException, "Platform error while settting a profile name: "<<ret);
564 ret = sync_agent_ds_set_server_info(profile_h, (char*)(profile->getSyncInfo()->getUrl().c_str()), (char*)(profile->getSyncInfo()->getId().c_str()), (char*)(profile->getSyncInfo()->getPassword().c_str()));
565 if (SYNC_AGENT_DS_SUCCESS!=ret) {
566 ThrowMsg(PlatformException, "Platform error while settting a server info: "<<ret);
569 sync_agent_ds_sync_mode_e syncMode = convertToPlatformSyncMode(profile->getSyncInfo()->getSyncMode());
570 sync_agent_ds_sync_type_e syncType = convertToPlatformSyncType(profile->getSyncInfo()->getSyncType());
571 sync_agent_ds_sync_interval_e syncInterval = convertToPlatformSyncInterval(profile->getSyncInfo()->getSyncInterval());
572 LoggerD("syncMode: "<<syncMode<<", syncType: "<<syncType<<", syncInterval: "<<syncInterval);
574 ret = sync_agent_ds_set_sync_info(profile_h, syncMode, syncType, syncInterval);
575 if (SYNC_AGENT_DS_SUCCESS!=ret) {
576 ThrowMsg(PlatformException, "Platform error while settting a sync info: "<<ret);
579 // Set the sync categories.
580 SyncServiceInfoListPtr categories = profile->getServiceInfo();
581 sync_agent_ds_service_type_e serviceType;
582 sync_agent_ds_src_uri_e srcURI;
583 std::string tgtURI, id, password;
585 for(unsigned int i=0; categories->size()<i; i++) {
586 serviceType = convertToPlatformSyncServiceType(categories->at(i)->getSyncServiceType());
587 tgtURI = categories->at(i)->getServerDatabaseUri();
588 srcURI = convertToPlatformSourceUri(categories->at(i)->getSyncServiceType());
589 id = categories->at(i)->getId();
590 password = categories->at(i)->getPassword();
591 enable = categories->at(i)->getEnable();
593 LoggerD("serviceType: "<<serviceType<<", tgtURI: "<<tgtURI<<" for index: "<<i);
595 ret = sync_agent_ds_set_sync_service_info(profile_h, serviceType, enable, srcURI, (char*)(tgtURI.c_str()),
596 0==id.size() ? NULL : (char*)(id.c_str()), 0==password.size() ? NULL : (char*)(password.c_str()));
597 if (SYNC_AGENT_DS_SUCCESS!=ret) {
598 ThrowMsg(PlatformException, "Platform error while settting a sync service info: "<<ret);
602 ret = sync_agent_ds_update_profile(profile_h);
603 if (SYNC_AGENT_DS_SUCCESS!=ret && SYNC_AGENT_DS_SYNCHRONISING !=ret) {
604 ThrowMsg(NotFoundException, "Platform error while updating a profile: "<<ret);
607 event->setResult(true);
609 Catch(NotFoundException)
611 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
612 event->setResult(false);
613 event->setExceptionCode(ExceptionCodes::NotFoundException);
617 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
618 event->setResult(false);
619 event->setExceptionCode(ExceptionCodes::PlatformException);
623 sync_agent_ds_free_profile_info(profile_h);
627 void DataSyncManager::OnRequestReceived(const IEventRemoveProfilePtr &event)
629 ds_profile_h profile_h = NULL;
633 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
636 std::stringstream ss(event->getProfileId());
638 LoggerD("profileId: "<<profileId);
639 ret = sync_agent_ds_get_profile(profileId, &profile_h);
640 if (SYNC_AGENT_DS_SUCCESS!=ret) {
641 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
644 ret = sync_agent_ds_delete_profile(profile_h);
645 if (SYNC_AGENT_DS_SUCCESS!=ret && SYNC_AGENT_DS_SYNCHRONISING !=ret) {
646 ThrowMsg(PlatformException, "Platform error while deleting a profile: "<<ret);
649 event->setResult(true);
651 Catch(NotFoundException)
653 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
654 event->setResult(false);
655 event->setExceptionCode(ExceptionCodes::NotFoundException);
659 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
660 event->setResult(false);
661 event->setExceptionCode(ExceptionCodes::PlatformException);
665 sync_agent_ds_free_profile_info(profile_h);
669 void DataSyncManager::OnRequestReceived(const IEventGetMaxProfilesNumPtr &event)
673 LoggerD("Return maximum number of supported profiles: "<<MAX_PROFILES_NUM);
675 event->setNumMaxProfiles(MAX_PROFILES_NUM);
677 event->setResult(true);
681 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
682 event->setResult(false);
683 event->setExceptionCode(ExceptionCodes::PlatformException);
687 void DataSyncManager::OnRequestReceived(const IEventGetProfilesNumPtr &event)
689 GList *profile_list = NULL;
694 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
696 ret = sync_agent_ds_get_all_profile(&profile_list);
697 if (SYNC_AGENT_DS_SUCCESS!=ret) {
698 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
702 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
703 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
705 LoggerD("Free sync_agent_ds_profile_info for index: "<<numProfiles);
708 LoggerD("numProfiles: "<<numProfiles);
710 event->setNumProfiles(numProfiles);
712 event->setResult(true);
716 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
717 event->setResult(false);
718 event->setExceptionCode(ExceptionCodes::PlatformException);
722 void DataSyncManager::OnRequestReceived(const IEventGetProfilePtr &event)
724 ds_profile_h profile_h = NULL;
728 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
731 std::stringstream ss(event->getProfileId());
733 LoggerD("profileId: "<<profileId);
734 ret = sync_agent_ds_get_profile(profileId, &profile_h);
735 if (SYNC_AGENT_DS_SUCCESS!=ret) {
736 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
739 SyncProfileInfoPtr profile( new SyncProfileInfo() );
741 profile->setProfileId(event->getProfileId());
743 char *profileName = NULL;
744 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
745 if (SYNC_AGENT_DS_SUCCESS!=ret) {
746 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
748 profile->setProfileName(profileName);
750 sync_agent_ds_server_info server_info = { NULL };
751 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
752 if (SYNC_AGENT_DS_SUCCESS!=ret) {
753 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
755 profile->getSyncInfo()->setUrl(server_info.addr);
756 profile->getSyncInfo()->setId(server_info.id);
757 profile->getSyncInfo()->setPassword(server_info.password);
759 sync_agent_ds_sync_info sync_info;
760 ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
761 if (SYNC_AGENT_DS_SUCCESS!=ret) {
762 ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
764 profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
765 profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
766 profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
768 LoggerD("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
770 GList *category_list = NULL;
771 sync_agent_ds_service_info *category_info = NULL;
772 ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
773 if (SYNC_AGENT_DS_SUCCESS!=ret) {
774 ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
776 int category_count = g_list_length(category_list);
777 LoggerD("category_count: "<<category_count);
778 while(category_count--) {
779 category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
780 if(SYNC_AGENT_CALENDAR<category_info->service_type) {
781 LoggerD("Skip unsupported sync service type: "<<category_info->service_type);
785 SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
786 serviceInfo->setEnable(category_info->enabled);
787 if(category_info->id) {
788 serviceInfo->setId(category_info->id);
790 if(category_info->password) {
791 serviceInfo->setPassword(category_info->password);
793 serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
794 if(category_info->tgt_uri) {
795 serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
798 LoggerD("Service type: "<<serviceInfo->getSyncServiceType());
799 profile->getServiceInfo()->push_back(serviceInfo);
802 g_list_free(category_list);
805 event->setProfile(profile);
807 event->setResult(true);
809 Catch(NotFoundException)
811 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
812 event->setResult(false);
813 event->setExceptionCode(ExceptionCodes::NotFoundException);
817 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
818 event->setResult(false);
819 event->setExceptionCode(ExceptionCodes::PlatformException);
823 sync_agent_ds_free_profile_info(profile_h);
827 void DataSyncManager::OnRequestReceived(const IEventGetAllProfilesPtr &event)
829 GList *profile_list = NULL;
834 ds_profile_h profile_h = NULL;
835 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
837 ret = sync_agent_ds_get_all_profile(&profile_list);
838 if (SYNC_AGENT_DS_SUCCESS!=ret) {
839 ThrowMsg(PlatformException, "Platform error while getting all profiles: "<<ret);
842 LoggerD("Number of profiles: "<< g_list_length(profile_list));
843 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
844 profile_h = (ds_profile_h) iter->data;
845 SyncProfileInfoPtr profile( new SyncProfileInfo() );
848 ret = sync_agent_ds_get_profile_id(profile_h, &profileId);
849 if (SYNC_AGENT_DS_SUCCESS!=ret) {
850 ThrowMsg(PlatformException, "Platform error while gettting a profile id: "<<ret);
853 std::stringstream ss;
855 profile->setProfileId(ss.str());
857 LoggerD("Processing a profile with id: "<<profile->getProfileId());
859 char *profileName = NULL;
860 ret = sync_agent_ds_get_profile_name(profile_h, &profileName);
861 if (SYNC_AGENT_DS_SUCCESS!=ret) {
862 ThrowMsg(PlatformException, "Platform error while gettting a profile name: "<<ret);
864 profile->setProfileName(profileName);
866 sync_agent_ds_server_info server_info = { NULL };
867 ret = sync_agent_ds_get_server_info(profile_h, &server_info);
868 if (SYNC_AGENT_DS_SUCCESS!=ret) {
869 ThrowMsg(PlatformException, "Platform error while gettting a server info: "<<ret);
871 profile->getSyncInfo()->setUrl(server_info.addr);
872 profile->getSyncInfo()->setId(server_info.id);
873 profile->getSyncInfo()->setPassword(server_info.password);
875 sync_agent_ds_sync_info sync_info;
876 ret = sync_agent_ds_get_sync_info(profile_h, &sync_info);
877 if (SYNC_AGENT_DS_SUCCESS!=ret) {
878 ThrowMsg(PlatformException, "Platform error while gettting a sync info: "<<ret);
880 profile->getSyncInfo()->setSyncMode(convertToSyncMode(sync_info.sync_mode));
881 profile->getSyncInfo()->setSyncType(convertToSyncType(sync_info.sync_type));
882 profile->getSyncInfo()->setSyncInterval(convertToSyncInterval(sync_info.interval));
884 LoggerD("Sync mode: "<<sync_info.sync_mode<<", type: "<<sync_info.sync_type<<", interval: "<<sync_info.interval);
886 GList *category_list = NULL;
887 sync_agent_ds_service_info *category_info = NULL;
888 ret = sync_agent_ds_get_sync_service_info(profile_h, &category_list);
889 if (SYNC_AGENT_DS_SUCCESS!=ret) {
890 ThrowMsg(PlatformException, "Platform error while gettting sync categories: "<<ret);
892 int category_count = g_list_length(category_list);
893 LoggerD("category_count: "<<category_count);
894 while(category_count--) {
895 category_info = (sync_agent_ds_service_info *) g_list_nth_data(category_list, category_count);
896 if(SYNC_AGENT_CALENDAR<category_info->service_type) {
897 LoggerD("Skip unsupported sync service type: "<<category_info->service_type);
901 SyncServiceInfoPtr serviceInfo( new SyncServiceInfo() );
902 serviceInfo->setEnable(category_info->enabled);
903 if(category_info->id) {
904 serviceInfo->setId(category_info->id);
906 if(category_info->password) {
907 serviceInfo->setPassword(category_info->password);
909 serviceInfo->setSyncServiceType(convertToSyncServiceType(category_info->service_type));
910 if(category_info->tgt_uri) {
911 serviceInfo->setServerDatabaseUri(category_info->tgt_uri);
914 LoggerD("Service type: "<<serviceInfo->getSyncServiceType());
915 profile->getServiceInfo()->push_back(serviceInfo);
918 g_list_free(category_list);
921 LoggerD("Adding a profile to the list.");
922 event->getProfiles()->push_back(profile);
925 event->setResult(true);
929 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
930 event->setResult(false);
931 event->setExceptionCode(ExceptionCodes::PlatformException);
934 LoggerD("Free profiles list.");
935 for (iter = profile_list; iter != NULL; iter = g_list_next(iter)) {
936 sync_agent_ds_free_profile_info((ds_profile_h) iter->data);
939 g_list_free(profile_list);
943 void DataSyncManager::OnRequestReceived(const IEventStartSyncPtr &event)
945 ds_profile_h profile_h = NULL;
949 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
950 sync_agent_event_error_e err= SYNC_AGENT_EVENT_FAIL;
953 std::stringstream ss(event->getProfileId());
955 LoggerD("profileId: "<<profileId);
956 ret = sync_agent_ds_get_profile(profileId, &profile_h);
957 if (SYNC_AGENT_DS_SUCCESS!=ret) {
958 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
961 err = sync_agent_set_noti_callback(1, datasync_state_changed_cb, this);
962 if (SYNC_AGENT_EVENT_SUCCESS != err) {
963 ThrowMsg(PlatformException, "Platform error while setting state changed cb: " << err);
966 err = sync_agent_set_noti_callback(2, datasync_progress_cb, this);
967 if (SYNC_AGENT_EVENT_SUCCESS != err) {
968 ThrowMsg(PlatformException, "Platform error while setting progress cb: " << err);
971 ret = sync_agent_ds_start_sync(profile_h);
972 if (SYNC_AGENT_DS_SUCCESS!=ret && SYNC_AGENT_DS_SYNCHRONISING !=ret) {
973 ThrowMsg(PlatformException, "Platform error while starting a profile: "<<ret);
976 if(event->getEmitter()) {
977 LoggerD("Attaching the emitter with profileId: "<<event->getProfileId());
978 m_changeEmitters[std::string("Sync") + event->getProfileId()] = event->getEmitter();
981 event->setResult(true);
983 Catch(NotFoundException)
985 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
986 event->setResult(false);
987 event->setExceptionCode(ExceptionCodes::NotFoundException);
991 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
992 event->setResult(false);
993 event->setExceptionCode(ExceptionCodes::PlatformException);
997 sync_agent_ds_free_profile_info(profile_h);
1001 void DataSyncManager::OnRequestReceived(const IEventStopSyncPtr &event)
1003 ds_profile_h profile_h = NULL;
1007 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
1010 std::stringstream ss(event->getProfileId());
1012 LoggerD("profileId: "<<profileId);
1013 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1014 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1015 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1018 ret = sync_agent_ds_stop_sync(profile_h);
1019 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1020 ThrowMsg(PlatformException, "Platform error while stopping a profile: "<<ret);
1023 event->setResult(true);
1025 Catch(NotFoundException)
1027 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1028 event->setResult(false);
1029 event->setExceptionCode(ExceptionCodes::NotFoundException);
1033 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1034 event->setResult(false);
1035 event->setExceptionCode(ExceptionCodes::PlatformException);
1039 sync_agent_ds_free_profile_info(profile_h);
1044 void DataSyncManager::OnRequestReceived(const IEventGetLastSyncStatisticsPtr &event)
1046 ds_profile_h profile_h = NULL;
1050 sync_agent_ds_error_e ret = SYNC_AGENT_DS_FAIL;
1053 std::stringstream ss(event->getProfileId());
1055 LoggerD("profileId: "<<profileId);
1056 ret = sync_agent_ds_get_profile(profileId, &profile_h);
1057 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1058 ThrowMsg(NotFoundException, "Platform error while getting a profile: "<<ret);
1061 SyncStatisticsListPtr statisticsList( new SyncStatisticsList() );
1063 GList *statistics_list = NULL;
1064 ret = sync_agent_ds_get_sync_statistics(profile_h, &statistics_list);
1065 if (SYNC_AGENT_DS_SUCCESS!=ret) {
1066 ThrowMsg(PlatformException, "Platform error while gettting sync statistics: "<<ret);
1069 int statistics_count = g_list_length(statistics_list);
1070 LoggerD("statistics_count: "<<statistics_count);
1071 sync_agent_ds_statistics_info *statistics = NULL;
1072 for (int i = 0; i < statistics_count; i++) {
1073 statistics = (sync_agent_ds_statistics_info *) g_list_nth_data(statistics_list, i);
1075 SyncStatisticsPtr statisticsPtr( new SyncStatistics() );
1078 LoggerD("Statistics for contact.");
1079 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CONTACT));
1081 LoggerD("Statistics for event.");
1082 statisticsPtr->setServiceType(convertToSyncServiceType(SYNC_AGENT_CALENDAR));
1084 LoggerW("Unsupported category for statistics: "<<i);
1088 LoggerD("dbsynced: "<<statistics->dbsynced);
1089 if (statistics->dbsynced) {
1090 statisticsPtr->setSyncStatus(convertToSyncStatus(statistics->dbsynced));
1091 statisticsPtr->setClientToServerTotal(statistics->client2server_total);
1092 statisticsPtr->setClientToServerAdded(statistics->client2server_nrofadd);
1093 statisticsPtr->setClientToServerUpdated(statistics->client2server_nrofreplace);
1094 statisticsPtr->setClientToServerRemoved(statistics->client2server_nrofdelete);
1095 statisticsPtr->setServerToClientTotal(statistics->server2client_total);
1096 statisticsPtr->setServerToClientAdded(statistics->server2client_nrofadd);
1097 statisticsPtr->setServerToClientUpdated(statistics->server2client_nrofreplace);
1098 statisticsPtr->setServerToClientRemoved(statistics->server2client_nrofdelete);
1100 statisticsPtr->setLastSyncTime((long long int) (statistics->last_session_time));
1103 LoggerD("ClientToServerTotal: "<<statisticsPtr->getClientToServerTotal()<<", ServerToClientTotal: "<<statisticsPtr->getServerToClientTotal());
1105 statisticsList->push_back(statisticsPtr);
1107 if(statistics_list) {
1108 g_list_free(statistics_list);
1111 event->setSyncStatictics(statisticsList);
1113 event->setResult(true);
1115 Catch(NotFoundException)
1117 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1118 event->setResult(false);
1119 event->setExceptionCode(ExceptionCodes::NotFoundException);
1123 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
1124 event->setResult(false);
1125 event->setExceptionCode(ExceptionCodes::PlatformException);
1129 sync_agent_ds_free_profile_info(profile_h);