[Messaging] Added stopSync() method
authorJerzy Pabich <j.pabich@samsung.com>
Thu, 18 Dec 2014 16:52:41 +0000 (17:52 +0100)
committerJerzy Pabich <j.pabich@samsung.com>
Thu, 18 Dec 2014 18:43:10 +0000 (19:43 +0100)
[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 <j.pabich@samsung.com>
src/common/platform_exception.h
src/messaging/email_manager.cc
src/messaging/email_manager.h
src/messaging/message_service.cc
src/messaging/message_service.h
src/messaging/message_service_email.cc
src/messaging/message_service_email.h
src/messaging/messaging_api.js
src/messaging/messaging_instance.cc
src/messaging/messaging_util.h

index 26fae3fb2c1b4906df2405239fb22f0f7aaae19a..2d2ad981d3f298cf43a79809d45fe93ac4e920ee 100644 (file)
@@ -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_
index 9f2ca1c378eb207a064fed6d8529159bc3a2ac4a..973dc3b6c4d4d0e4177b2d72dce0c8ddedfff716 100644 (file)
@@ -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<SyncCallbackData*>(
-//                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<SyncCallbackData*>(
+                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<picojson::value> response = callback->getJson();
+    picojson::object& obj = response->get<picojson::object>();
+    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");
index 48087313d32aecd69c9416da2c7bfda023234bfa..f1ffaa4451c96a3752107e12ce09e52490067929 100644 (file)
@@ -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);
 
     /**
index 7a8f1fca46e87b589a4e3038694960fc3b705930..1b572b60ebabc68d94060d145ec310bb5f3dd823 100644 (file)
@@ -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
index 2138d2efacf15e9e9526be8c7717d136731f5089..6a5f9e213b790878dbbcf3ad935f64efde391f16 100755 (executable)
@@ -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;
 
index 2ff3f2cc50fa231cd84d63c9371b919865f7ecf4..e1923290284c026d4cdbeb813d42c87ebd6209ac 100644 (file)
@@ -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<long*>(data));
+        delete static_cast<long*>(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<void*>(data));
+    if (!id) {
+        LOGE("g_idle_add failed");
+        delete data;
+        data = NULL;
+        throw common::UnknownException("Could not add task");
+    }
 }
 
 } // messaging
index 71e16529fcb06756368bcc68157273953c9a9cb5..c3fec938085ca916d42008df0d607853818e3a44 100755 (executable)
@@ -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
index b6d7c2a682a833e7ba9682def8bab01112bf5e0a..21c974c497efa112cb14d80da1669b146d52c2cb 100644 (file)
@@ -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)
                 )
             }
         }
index acf7391eb6e4a062dcc4cefebbe6fe1c7c827599..e1dd03b4c09c023e007e922ec3e0133de6f2dba3 100644 (file)
@@ -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::object>();
-    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<double>();
 
     int id = static_cast<int>(v_id.get<double>());
@@ -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::object>();
+    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<int>(v_id.get<double>());
+    long op_id = 0;
+    if (v_op_id.is<double>()) {
+        op_id = static_cast<long>(v_op_id.get<double>());
+    }
+
+    MessagingManager::getInstance().getMessageServiceEmail(id)->stopSync(op_id);
+
+    ReportSuccess(out);
 }
 
 /*  Code used to testing in node.js console
index 48785ce891617256b30dff99fab4b8d4773acd10..e83c8a695de2f6b1c59382af3133399c96b26521 100644 (file)
@@ -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,