From: Jerzy Pabich Date: Thu, 18 Dec 2014 16:52:41 +0000 (+0100) Subject: [Messaging] Added stopSync() method X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~785^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d06b62306324a0b0842230ef0928809e88966ed9;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Messaging] Added stopSync() method [Verification] The code below works fine: var email_serv; // Defines the success callback. function serviceSynced() { console.log("New messages are fetched!"); } // Defines the success callback. function servicesListSuccessCB(services) { console.log("services"); email_serv = services[0]; //services[0].sync(serviceSynced, null, 30); // Subscribes to MessageStorage notification } tizen.messaging.getMessageServices("messaging.email", servicesListSuccessCB); var op_id = email_serv.sync(function(){console.log("hura")}, function(e){console.log("name: " + e.name); console.log("message: " + e.message);}); email_serv.stopSync(op_id); Change-Id: I7cabadf3825c8f9670d30196f5f0f9ea1e9a09ac Signed-off-by: Jerzy Pabich --- diff --git a/src/common/platform_exception.h b/src/common/platform_exception.h index 26fae3fb..2d2ad981 100644 --- a/src/common/platform_exception.h +++ b/src/common/platform_exception.h @@ -41,9 +41,10 @@ DEFINE_EXCEPTION(Security) DEFINE_EXCEPTION(NotSupported) DEFINE_EXCEPTION(NotFound) DEFINE_EXCEPTION(InvalidAccess) +DEFINE_EXCEPTION(Abort) DEFINE_EXCEPTION(QuotaExceeded) #undef DEFINE_EXCEPTION } // namespace common -#endif // COMMON_PLATFORM_EXCEPTION_H_ \ No newline at end of file +#endif // COMMON_PLATFORM_EXCEPTION_H_ diff --git a/src/messaging/email_manager.cc b/src/messaging/email_manager.cc index 9f2ca1c3..973dc3b6 100644 --- a/src/messaging/email_manager.cc +++ b/src/messaging/email_manager.cc @@ -753,41 +753,42 @@ void EmailManager::sync(void* data) //} // ////#################################### ^syncFolder ############################# -// -////################################## stopSync: ################################# -// -//void EmailManager::stopSync(long op_id) -//{ -// LoggerD("Entered"); -// SyncCallbackData* callback = NULL; -// try { -// callback = dynamic_cast( -// m_proxy_sync->getCallback(op_id)); -// } -// catch (const BasePlatformException& e) { -// LoggerE("Could not get callback"); -// } -// if(!callback){ -// LoggerE("Callback is null"); -// return; -// } -// -// int err = EMAIL_ERROR_NONE; -// err = email_cancel_job(callback->getAccountId(), callback->getOperationHandle(), -// EMAIL_CANCELED_BY_USER); -// if(EMAIL_ERROR_NONE != err){ -// LoggerE("Email cancel job failed, %d", err); -// } -// JSObjectRef err_obj = -// JSWebAPIErrorFactory::makeErrorObject(callback->getContext(), -// JSWebAPIErrorFactory::ABORT_ERROR, -// "Sync aborted by user"); -// callback->callErrorCallback(err_obj); -// m_proxy_sync->removeCallback(op_id); -//} -// -////################################## ^stopSync ################################# -// + +//################################## stopSync: ################################# + +void EmailManager::stopSync(long op_id) +{ + LoggerD("Entered"); + SyncCallbackData* callback = NULL; + try { + callback = dynamic_cast( + m_proxy_sync->getCallback(op_id)); + } + catch (const common::PlatformException& e) { + LoggerE("Could not get callback"); + } + if(!callback){ + LoggerE("Callback is null"); + return; + } + + int err = EMAIL_ERROR_NONE; + err = email_cancel_job(callback->getAccountId(), callback->getOperationHandle(), + EMAIL_CANCELED_BY_USER); + if(EMAIL_ERROR_NONE != err){ + LoggerE("Email cancel job failed, %d", err); + } + + std::shared_ptr response = callback->getJson(); + picojson::object& obj = response->get(); + common::AbortException error("Sync aborted by user"); + callback->setError(error.name(), error.message()); + MessagingInstance::getInstance().PostMessage(response->serialize().c_str()); + m_proxy_sync->removeCallback(op_id); +} + +//################################## ^stopSync ################################# + //void removeEmailCompleteCB(MessagesCallbackUserData* callback) //{ // LoggerD("Entered"); diff --git a/src/messaging/email_manager.h b/src/messaging/email_manager.h index 48087313..f1ffaa44 100644 --- a/src/messaging/email_manager.h +++ b/src/messaging/email_manager.h @@ -77,8 +77,8 @@ public: void sync(void* data); // void syncFolder(SyncFolderCallbackData* callback); -// void stopSync(long op_id); -// + void stopSync(long op_id); + // void registerStatusCallback(msg_handle_t msg_handle); /** diff --git a/src/messaging/message_service.cc b/src/messaging/message_service.cc index 7a8f1fca..1b572b60 100644 --- a/src/messaging/message_service.cc +++ b/src/messaging/message_service.cc @@ -245,7 +245,7 @@ long MessageService::syncFolder() throw common::NotSupportedException("Cannot sync folder with external server"); } -void MessageService::stopSync() +void MessageService::stopSync(long op_id) { // this method should be overwritten by email service // for MMS and SMS this function is not supported diff --git a/src/messaging/message_service.h b/src/messaging/message_service.h index 2138d2ef..6a5f9e21 100755 --- a/src/messaging/message_service.h +++ b/src/messaging/message_service.h @@ -97,7 +97,7 @@ public: virtual void loadMessageAttachment(); virtual long sync(SyncCallbackData *callback); virtual long syncFolder(); - virtual void stopSync(); + virtual void stopSync(long op_id); picojson::object toPicoJS() const; diff --git a/src/messaging/message_service_email.cc b/src/messaging/message_service_email.cc index 2ff3f2cc..e1923290 100644 --- a/src/messaging/message_service_email.cc +++ b/src/messaging/message_service_email.cc @@ -85,10 +85,42 @@ long MessageServiceEmail::syncFolder() return 0; } -void MessageServiceEmail::stopSync() +static gboolean stopSyncTask(void* data) { LoggerD("Entered"); - //TODO add implementation + + try { + if (!data) { + LoggerE("opId is null"); + return FALSE; + } + + const long op_id = *(static_cast(data)); + delete static_cast(data); + data = NULL; + EmailManager::getInstance().stopSync(op_id); + + } catch(const common::PlatformException& exception) { + LoggerE("Unhandled exception: %s (%s)!", (exception.name()).c_str(), + (exception.message()).c_str()); + } catch(...) { + LoggerE("Unhandled exception!"); + } + + return FALSE; +} + +void MessageServiceEmail::stopSync(long op_id) +{ + LoggerD("Entered"); + long* data = new long(op_id); + guint id = g_idle_add(stopSyncTask, static_cast(data)); + if (!id) { + LOGE("g_idle_add failed"); + delete data; + data = NULL; + throw common::UnknownException("Could not add task"); + } } } // messaging diff --git a/src/messaging/message_service_email.h b/src/messaging/message_service_email.h index 71e16529..c3fec938 100755 --- a/src/messaging/message_service_email.h +++ b/src/messaging/message_service_email.h @@ -21,7 +21,7 @@ public: virtual void loadMessageAttachment(); virtual long sync(SyncCallbackData *callback); virtual long syncFolder(); - virtual void stopSync(); + virtual void stopSync(long op_id); }; } // messaging diff --git a/src/messaging/messaging_api.js b/src/messaging/messaging_api.js index b6d7c2a6..21c974c4 100644 --- a/src/messaging/messaging_api.js +++ b/src/messaging/messaging_api.js @@ -279,7 +279,7 @@ Messaging.prototype.getMessageServices = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -317,7 +317,7 @@ MessageService.prototype.sendMessage = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -347,7 +347,7 @@ MessageService.prototype.loadMessageBody = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -379,7 +379,7 @@ MessageService.prototype.loadMessageAttachment = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -403,7 +403,7 @@ MessageService.prototype.sync = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -444,7 +444,7 @@ MessageService.prototype.syncFolder = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -455,9 +455,11 @@ MessageService.prototype.stopSync = function () { {name: 'opId', type: types_.LONG} ]); + var self = this; bridge.sync({ cmd: 'MessageService_stopSync', args: { + id: self.id, opId: args.opId } }); @@ -490,7 +492,7 @@ MessageStorage.prototype.addDraftMessage = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -528,7 +530,7 @@ MessageStorage.prototype.findMessages = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -557,7 +559,7 @@ MessageStorage.prototype.removeMessages = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -586,7 +588,7 @@ MessageStorage.prototype.updateMessages = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -624,7 +626,7 @@ MessageStorage.prototype.findConversations = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -653,7 +655,7 @@ MessageStorage.prototype.removeConversations = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } @@ -687,7 +689,7 @@ MessageStorage.prototype.findFolders = function () { if (args.errorCallback) { args.errorCallback.call( null, - new tizen.WebAPIException(e.code, e.name, e.message) + new tizen.WebAPIException(e.code, e.message, e.name) ) } } diff --git a/src/messaging/messaging_instance.cc b/src/messaging/messaging_instance.cc index acf7391e..e1dd03b4 100644 --- a/src/messaging/messaging_instance.cc +++ b/src/messaging/messaging_instance.cc @@ -32,14 +32,15 @@ const char* FUN_MESSAGE_SERVICE_LOAD_MESSAGE_ATTACHMENT = "MessageService_loadMe const char* LOAD_MESSAGE_ATTACHMENT_ARGS_ATTACHMENT = "attachment"; const char* FUN_MESSAGE_SERVICE_SYNC = "MessageService_sync"; -const char* SERVICE_SYNC_ARGS_ID = "id"; -const char* SERVICE_SYNC_ARGS_LIMIT = "limit"; +const char* SYNC_ARGS_ID = "id"; +const char* SYNC_ARGS_LIMIT = "limit"; const char* FUN_MESSAGE_SERVICE_SYNC_FOLDER = "MessageService_syncFolder"; const char* SYNC_FOLDER_ARGS_FOLDER = "folder"; const char* SYNC_FOLDER_ARGS_LIMIT = "limit"; const char* FUN_MESSAGE_SERVICE_STOP_SYNC = "MessageService_stopSync"; +const char* STOP_SYNC_ARGS_ID = "id"; const char* STOP_SYNC_ARGS_OPID = "opId"; const char* FUN_MESSAGE_STORAGE_ADD_DRAFT_MESSAGE = "MessageStorage_addDraftMessage"; @@ -171,8 +172,8 @@ void MessagingInstance::MessageServiceSync(const picojson::value& args, LoggerD("Entered"); picojson::object data = args.get(JSON_DATA).get(); - picojson::value v_id = data.at(SERVICE_SYNC_ARGS_ID); - picojson::value v_limit = data.at(SERVICE_SYNC_ARGS_LIMIT); + picojson::value v_id = data.at(SYNC_ARGS_ID); + picojson::value v_limit = data.at(SYNC_ARGS_LIMIT); const double callbackId = args.get(JSON_CALLBACK_ID).get(); int id = static_cast(v_id.get()); @@ -206,6 +207,20 @@ void MessagingInstance::MessageServiceStopSync(const picojson::value& args, picojson::object& out) { LoggerD("Entered"); + + picojson::object data = args.get(JSON_DATA).get(); + picojson::value v_id = data.at(STOP_SYNC_ARGS_ID); + picojson::value v_op_id = data.at(STOP_SYNC_ARGS_OPID); + + int id = static_cast(v_id.get()); + long op_id = 0; + if (v_op_id.is()) { + op_id = static_cast(v_op_id.get()); + } + + MessagingManager::getInstance().getMessageServiceEmail(id)->stopSync(op_id); + + ReportSuccess(out); } /* Code used to testing in node.js console diff --git a/src/messaging/messaging_util.h b/src/messaging/messaging_util.h index 48785ce8..e83c8a69 100644 --- a/src/messaging/messaging_util.h +++ b/src/messaging/messaging_util.h @@ -47,10 +47,6 @@ extern const char* MESSAGE_BODY_ATTRIBUTE_LOADED; extern const char* MESSAGE_BODY_ATTRIBUTE_PLAIN_BODY; extern const char* MESSAGE_BODY_ATTRIBUTE_HTML_BODY; -extern const char* JSON_RET_DATA; -extern const char* JSON_RET_ERR_MESSAGE; -extern const char* JSON_RET_ERR_NAME; - enum MessageType { UNDEFINED = 0, SMS,