[Messaging] Bound core function to sync() method
authorJerzy Pabich <j.pabich@samsung.com>
Tue, 16 Dec 2014 07:24:15 +0000 (08:24 +0100)
committerJerzy Pabich <j.pabich@samsung.com>
Tue, 16 Dec 2014 12:14:37 +0000 (21:14 +0900)
Synchronization of email service works; DBus response to be added.

Change-Id: I20725d43d35d03a18565577be61b31917ec137ca
Signed-off-by: Jerzy Pabich <j.pabich@samsung.com>
src/common/callback_user_data.cc
src/common/callback_user_data.h
src/messaging/email_manager.cc [new file with mode: 0644]
src/messaging/email_manager.h [new file with mode: 0644]
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.gyp
src/messaging/messaging_instance.cc

index 35c15b90320775c5efe371a4c2fa884915758b79..6480e8798c6729f5177308858d608935a06d4751 100644 (file)
@@ -110,5 +110,15 @@ bool CallbackUserData::isActive() const
     return m_is_act;
 }
 
+void CallbackUserData::setJson(std::shared_ptr<picojson::value> json)
+{
+    m_json = json;
+}
+
+std::shared_ptr<picojson::value> CallbackUserData::getJson() const
+{
+    return m_json;
+}
+
 } // Common
 //} // DeviceAPI
index f9f9dc3eb3a3272cf7e9dd66123a0fb2317a9f49..b29abaa106041a3c580446b067235ea7df5f4d6f 100644 (file)
@@ -22,6 +22,9 @@
 #define __TIZEN_COMMON_CALLBACK_USER_DATA_H__
 
 //#include <JavaScriptCore/JavaScript.h>
+#include "common/picojson.h"
+
+#include <memory>
 
 //namespace DeviceAPI {
 namespace common {
@@ -47,10 +50,14 @@ public:
 
     void setActive(bool act);
     bool isActive() const;
+    void setJson(std::shared_ptr<picojson::value> json);
+    std::shared_ptr<picojson::value> getJson() const;
+
 private:
 //    JSContextRef m_context;
 //    MultiCallbackUserData* m_impl;
     bool m_is_act;
+    std::shared_ptr<picojson::value> m_json;
 };
 
 } // Common
diff --git a/src/messaging/email_manager.cc b/src/messaging/email_manager.cc
new file mode 100644 (file)
index 0000000..8afa325
--- /dev/null
@@ -0,0 +1,1408 @@
+//
+// Tizen Web Device API
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file: EmailManager.cpp
+ */
+
+//#include <JSWebAPIErrorFactory.h>
+//#include <JSWebAPIError.h>
+//#include <JSUtil.h>
+#include "common/logger.h"
+#include <memory>
+//#include <PlatformException.h>
+#include <sstream>
+//#include <GlobalContextManager.h>
+
+//#include <AbstractFilter.h>
+
+#include <email-api-network.h>
+#include <email-api-account.h>
+#include <email-api-mail.h>
+#include <email-api-mailbox.h>
+
+#include "email_manager.h"
+#include "messaging_util.h"
+#include "message_service.h"
+#include "message.h"
+//#include "MessageConversation.h"
+//#include "MessageCallbackUserData.h"
+//#include "MessagesCallbackUserData.h"
+//#include "FindMsgCallbackUserData.h"
+//#include "ConversationCallbackData.h"
+#include "message_email.h"
+//#include "MessagingDatabaseManager.h"
+
+//#include "JSMessage.h"
+//#include "JSMessageConversation.h"
+//#include "JSMessageFolder.h"
+
+#include <email-api.h>
+#include <vconf.h>
+
+//#include "DBus/SyncProxy.h"
+//#include "DBus/LoadBodyProxy.h"
+//#include "DBus/LoadAttachmentProxy.h"
+
+#include <sstream>
+//#include <FilterIterator.h>
+
+//using namespace DeviceAPI::Common;
+//using namespace DeviceAPI::Tizen;
+
+namespace extension {
+namespace messaging {
+
+namespace {
+const int ACCOUNT_ID_NOT_INITIALIZED = -1;
+const std::string FIND_FOLDERS_ATTRIBUTE_ACCOUNTID_NAME  = "serviceId";
+} //anonymous namespace
+
+EmailManager& EmailManager::getInstance()
+{
+    LOGD("Entered");
+
+    static EmailManager instance;
+    return instance;
+}
+
+EmailManager::EmailManager()
+{
+    LOGD("Entered");
+    getUniqueOpId();
+    const int non_err = EMAIL_ERROR_NONE;
+
+    if(non_err != email_service_begin()){
+        LOGE("Email service failed to begin");
+        //TODO throw Common::UnknownException("Email service failed to begin");
+    }
+    if(non_err != email_open_db()){
+        LOGE("Email DB failed to open");
+        //TODO throw Common::UnknownException("Email DB failed to open");
+    }
+
+    int slot_size = -1;
+    vconf_get_int("db/private/email-service/slot_size", &(slot_size));
+    if (slot_size > 0) {
+        m_slot_size = slot_size;
+    }
+
+//    m_proxy_sync = std::make_shared<DBus::SyncProxy>(
+//                                      DBus::Proxy::DBUS_PATH_NETWORK_STATUS,
+//                                      DBus::Proxy::DBUS_IFACE_NETWORK_STATUS);
+//    if (!m_proxy_sync) {
+//        LOGE("Sync proxy is null");
+//        throw Common::UnknownException("Sync proxy is null");
+//    }
+//    m_proxy_sync->signalSubscribe();
+//
+//    m_proxy_load_body = std::make_shared<DBus::LoadBodyProxy>(
+//                                        DBus::Proxy::DBUS_PATH_NETWORK_STATUS,
+//                                        DBus::Proxy::DBUS_IFACE_NETWORK_STATUS);
+//    if (!m_proxy_load_body) {
+//        LOGE("Load body proxy is null");
+//        throw Common::UnknownException("Load body proxy is null");
+//    }
+//    m_proxy_load_body->signalSubscribe();
+//
+//    m_proxy_load_attachment = std::make_shared<DBus::LoadAttachmentProxy>(
+//                                        DBus::Proxy::DBUS_PATH_NETWORK_STATUS,
+//                                        DBus::Proxy::DBUS_IFACE_NETWORK_STATUS);
+//    if (!m_proxy_load_attachment) {
+//        LOGE("Load attachment proxy is null");
+//        throw Common::UnknownException("Load attachment proxy is null");
+//    }
+//    m_proxy_load_attachment->signalSubscribe();
+//
+//    m_proxy_messageStorage = std::make_shared<DBus::MessageProxy>();
+//    if (!m_proxy_messageStorage) {
+//        LOGE("Message proxy is null");
+//        throw Common::UnknownException("Message proxy is null");
+//    }
+//    m_proxy_messageStorage->signalSubscribe();
+//
+//    m_proxy_send = std::make_shared<DBus::SendProxy>();
+//    if (!m_proxy_send) {
+//        LOGE("Send proxy is null");
+//        throw Common::UnknownException("Send proxy is null");
+//    }
+//    m_proxy_send->signalSubscribe();
+}
+
+EmailManager::~EmailManager()
+{
+    LOGD("Entered");
+}
+
+//void EmailManager::addDraftMessagePlatform(int account_id,
+//    std::shared_ptr<Message> message)
+//{
+//    addMessagePlatform(account_id, message, EMAIL_MAILBOX_TYPE_DRAFT);
+//}
+//
+//void EmailManager::addOutboxMessagePlatform(int account_id,
+//    std::shared_ptr<Message> message)
+//{
+//    addMessagePlatform(account_id, message, EMAIL_MAILBOX_TYPE_OUTBOX);
+//}
+//
+//void EmailManager::addMessagePlatform(int account_id,
+//    std::shared_ptr<Message> message, email_mailbox_type_e mailbox_type)
+//{
+//    email_mail_data_t* mail_data = NULL;
+//    email_mail_data_t* mail_data_final = NULL;
+//    int err = EMAIL_ERROR_NONE;
+//
+//    mail_data = Message::convertPlatformEmail(message);
+//
+//    mail_data->account_id = account_id;
+//
+//    //Adding "from" email address
+//    email_account_t* account = NULL;
+//    err = email_get_account(account_id, EMAIL_ACC_GET_OPT_FULL_DATA, &account);
+//    if(EMAIL_ERROR_NONE != err) {
+//        LOGE("email_get_account failed. [%d]\n",err);
+//        err = email_free_mail_data(&mail_data,1);
+//        if(EMAIL_ERROR_NONE != err) {
+//            LOGE("Failed to free mail data memory");
+//        }
+//        throw UnknownException("Cannot retrieve email account information");
+//    }
+//    LOGE("FROM %s", account->user_email_address);
+//    std::stringstream ss;
+//    ss << "<" << account->user_email_address << ">";
+//    std::string address_from;
+//    ss >> address_from;
+//    mail_data->full_address_from = strdup(address_from.c_str());
+//    LOGE("FROM %s", mail_data->full_address_from);
+//    err = email_free_account(&account,1);
+//    if(EMAIL_ERROR_NONE != err) {
+//        LOGE("Failed to free account data memory");
+//    }
+//    //Setting mailbox id
+//    email_mailbox_t *mailbox_data = NULL;
+//    err = email_get_mailbox_by_mailbox_type(account_id, mailbox_type,
+//            &mailbox_data);
+//    if(EMAIL_ERROR_NONE != err) {
+//        LOGD("email_get_mailbox_by_mailbox_type failed. [%d]\n",err);
+//        err = email_free_mail_data(&mail_data,1);
+//        if(EMAIL_ERROR_NONE != err) {
+//            LOGE("Failed to free mail data memory");
+//        }
+//        throw UnknownException("Cannot retrieve draft mailbox");
+//    }
+//    else {
+//        LOGD("email_get_mailbox_by_mailbox_type success.\n");
+//        mail_data->mailbox_id = mailbox_data->mailbox_id;
+//        mail_data->mailbox_type = mailbox_data->mailbox_type;
+//    }
+//
+//    mail_data->report_status = EMAIL_MAIL_REPORT_NONE;
+//    mail_data->save_status = EMAIL_MAIL_STATUS_SAVED;
+//    mail_data->flags_draft_field = 1;
+//
+//    //adding email without attachments
+//    err = email_add_mail(mail_data, NULL, 0, NULL, 0);
+//    if(EMAIL_ERROR_NONE != err) {
+//        LOGD("email_add_mail failed. [%d]\n",err);
+//        err = email_free_mail_data(&mail_data,1);
+//        if(EMAIL_ERROR_NONE != err) {
+//            LOGE("Failed to free mail data memory");
+//        }
+//        err = email_free_mailbox(&mailbox_data, 1);
+//        if (EMAIL_ERROR_NONE != err) {
+//            LOGE("Failed to destroy mailbox");
+//        }
+//        throw UnknownException("Couldn't add message to draft mailbox");
+//    }
+//    else {
+//        LOGD("email_add_mail success.\n");
+//    }
+//
+//    LOGD("saved mail without attachments id = [%d]\n", mail_data->mail_id);
+//
+//    message->setId(mail_data->mail_id);
+//    message->setMessageStatus(MessageStatus::STATUS_DRAFT);
+//
+//    //Adding attachments
+//    if (message->getHasAttachment()){
+//        Message::addEmailAttachments(message);
+//    }
+//
+//    err = email_get_mail_data(message->getId(), &mail_data_final);
+//    if(EMAIL_ERROR_NONE != err) {
+//        LOGE("Failed to retrieve added mail data");
+//        throw UnknownException("Couldn't retrieve added mail data");
+//    }
+//
+//    message->updateEmailMessage(*mail_data_final);
+//
+//    err = email_free_mail_data(&mail_data_final,1);
+//    if(EMAIL_ERROR_NONE != err) {
+//        LOGE("Failed to free mail data final memory");
+//    }
+//
+//    err = email_free_mail_data(&mail_data,1);
+//    if(EMAIL_ERROR_NONE != err) {
+//        LOGE("Failed to free mail data memory");
+//    }
+//
+//    err = email_free_mailbox(&mailbox_data, 1);
+//    if (EMAIL_ERROR_NONE != err) {
+//        LOGE("Failed to destroy mailbox");
+//    }
+//}
+//
+//static gboolean addDraftMessageCompleteCB(void *data)
+//{
+//    MessageCallbackUserData* callback =
+//        static_cast<MessageCallbackUserData *>(data);
+//    if (!callback) {
+//        LOGE("Callback is null");
+//        return false;
+//    }
+//    JSContextRef context = callback->getContext();
+//    if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
+//        LOGE("context was closed");
+//        delete callback;
+//        callback = NULL;
+//        return false;
+//    }
+//
+//    try {
+//        if (callback->isError()) {
+//            LOGD("Calling error callback");
+//            JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context,
+//                    callback->getErrorName(),
+//                    callback->getErrorMessage());
+//            callback->callErrorCallback(errobj);
+//            callback->getMessage()->setMessageStatus(MessageStatus::STATUS_FAILED);
+//        } else {
+//            LOGD("Calling success callback");
+//            callback->callSuccessCallback();
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("%s (%s)", (err.getName()).c_str(), (err.getMessage()).c_str());
+//        JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context, err);
+//        callback->callErrorCallback(errobj);
+//    } catch (...) {
+//        LOGE("Message add draft failed");
+//        JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context,
+//                JSWebAPIErrorFactory::UNKNOWN_ERROR, "Message add draft failed");
+//        callback->callErrorCallback(errobj);
+//    }
+//
+//    delete callback;
+//    callback = NULL;
+//
+//    return false;
+//}
+//
+//void EmailManager::addDraftMessage(MessageCallbackUserData* callback)
+//{
+//    LOGD("Entered");
+//
+//    if(!callback){
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    try {
+//        std::lock_guard<std::mutex> lock(m_mutex);
+//        std::shared_ptr<Message> message = callback->getMessage();
+//        addDraftMessagePlatform(callback->getAccountId(), message);
+//    } catch (const BasePlatformException& err) {
+//        LOGE("%s (%s)", (err.getName()).c_str(), (err.getMessage()).c_str());
+//        callback->setError(err.getName(), err.getMessage());
+//    } catch (...) {
+//        LOGE("Message add draft failed");
+//        callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR, "Message add draft failed");
+//    }
+//
+//    //Complete task
+//    if (!g_idle_add(addDraftMessageCompleteCB, static_cast<void *>(callback))) {
+//        LOGE("g_idle addition failed");
+//        delete callback;
+//        callback = NULL;
+//    }
+//}
+//
+//
+////**** sending email ****
+//static gboolean sendEmailCompleteCB(void* data)
+//{
+//    LOGD("Entered");
+//
+//    MessageRecipientsCallbackData* callback =
+//            static_cast<MessageRecipientsCallbackData*>(data);
+//    if (!callback) {
+//        LOGE("Callback is null");
+//        return false;
+//    }
+//
+//    JSContextRef context = callback->getContext();
+//    if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
+//        LOGE("context was closed");
+//        delete callback;
+//        callback = NULL;
+//        return false;
+//    }
+//
+//    try {
+//        if (callback->isError()) {
+//            JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context,
+//                    callback->getErrorName(),
+//                    callback->getErrorMessage());
+//            callback->callErrorCallback(errobj);
+//            callback->getMessage()->setMessageStatus(MessageStatus::STATUS_FAILED);
+//        }
+//        else {
+//            std::shared_ptr<Message> message = callback->getMessage();
+//            callback->callSuccessCallback(
+//                    JSUtil::toJSValueRef(context, message->getTO()));
+//            callback->getMessage()->setMessageStatus(MessageStatus::STATUS_SENT);
+//        }
+//    }
+//    catch (const BasePlatformException& err) {
+//        LOGE("%s (%s)", (err.getName()).c_str(), (err.getMessage()).c_str());
+//    }
+//    catch (...) {
+//        LOGE("Unknown error when calling send message callback");
+//    }
+//
+//    delete callback;
+//    callback = NULL;
+//
+//    return false;
+//}
+//
+//void EmailManager::sendMessage(MessageRecipientsCallbackData* callback)
+//{
+//    LOGD("Entered");
+//    int err = EMAIL_ERROR_NONE;
+//    email_mail_data_t *mail_data = NULL;
+//
+//    try{
+//        if(!callback){
+//            LOGE("Callback is null");
+//            throw UnknownException("Callback is null");
+//        }
+//
+//        std::shared_ptr<Message> message = callback->getMessage();
+//        if(!message) {
+//            LOGE("Message is null");
+//            throw UnknownException("Message is null");
+//        }
+//
+//        if(!(message->is_id_set())) {
+//            addOutboxMessagePlatform(callback->getAccountId(),message);
+//        }
+//
+//        err = email_get_mail_data(message->getId(),&mail_data);
+//        if(EMAIL_ERROR_NONE != err) {
+//            LOGE("email_get_mail_data failed. [%d]\n",err);
+//            throw UnknownException("Failed to get platform email structure");
+//        }
+//
+//        LOGD("email_get_mail_data success.\n");
+//
+//        //Sending EMAIL
+//        mail_data->save_status = EMAIL_MAIL_STATUS_SENDING;
+//
+//        int req_id = 0;
+//        err = email_send_mail(mail_data->mail_id, &req_id);
+//        if (EMAIL_ERROR_NONE != err) {
+//            LOGE("Failed to send message %d", err);
+//            throw UnknownException("Failed to send message");
+//        }
+//        LOGD("req_id: %d", req_id);
+//        callback->getMessage()->setMessageStatus(MessageStatus::STATUS_SENDING);
+//        m_sendRequests[req_id] = callback;
+//
+//    } catch (const BasePlatformException& ex) {
+//         LOGE("%s (%s)", (ex.getName()).c_str(), (ex.getMessage()).c_str());
+//         callback->setError(ex.getName(), ex.getMessage());
+//         if (!g_idle_add(sendEmailCompleteCB, static_cast<void*>(callback))) {
+//             LOGE("g_idle addition failed");
+//             delete callback;
+//             callback = NULL;
+//         }
+//    }catch (...) {
+//        LOGE("Message send failed");
+//        callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR, "Message send failed");
+//        if (!g_idle_add(sendEmailCompleteCB, static_cast<void*>(callback))) {
+//            LOGE("g_idle addition failed");
+//            delete callback;
+//            callback = NULL;
+//        }
+//    }
+//
+//    err = email_free_mail_data(&mail_data,1);
+//    if(EMAIL_ERROR_NONE != err) {
+//        LOGE("Failed to free mail data memory");
+//    }
+//
+//    return;
+//}
+//
+//void EmailManager::sendStatusCallback(int mail_id,
+//        email_noti_on_network_event status,
+//        int error_code)
+//{
+//    LOGD("Enter");
+//
+//    std::lock_guard<std::mutex> lock(m_mutex);
+//    //find first request for this mail_id
+//    SendReqMapIterator it = getSendRequest(mail_id);
+//    if (it != m_sendRequests.end()) {
+//        LOGD("Found request");
+//        MessageRecipientsCallbackData* callback = it->second;
+//        m_sendRequests.erase(it);
+//
+//        if (NOTI_SEND_FAIL == status) {
+//            LOGD("Failed to send message, set proper error");
+//            switch (error_code) {
+//                case EMAIL_ERROR_NO_SIM_INSERTED:
+//                case EMAIL_ERROR_SOCKET_FAILURE:
+//                case EMAIL_ERROR_CONNECTION_FAILURE:
+//                case EMAIL_ERROR_CONNECTION_BROKEN:
+//                case EMAIL_ERROR_NO_SUCH_HOST:
+//                case EMAIL_ERROR_NETWORK_NOT_AVAILABLE:
+//                case EMAIL_ERROR_INVALID_STREAM:
+//                case EMAIL_ERROR_NO_RESPONSE:
+//                    LOGE("Network error %d", error_code);
+//                    callback->setError(JSWebAPIErrorFactory::NETWORK_ERROR,
+//                            "Failed to send message");
+//                    break;
+//                default:
+//                    LOGE("Unknown error %d", error_code);
+//                    callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR,
+//                            "Failed to send message");
+//            }
+//        } else if (NOTI_SEND_FINISH == status) {
+//            LOGD("Message sent successfully");
+//        }
+//
+//        if (!g_idle_add(sendEmailCompleteCB, static_cast<void*>(callback))) {
+//            LOGE("g_idle addition failed");
+//            delete callback;
+//            callback = NULL;
+//        }
+//    } else {
+//        LOGW("No matching request found");
+//    }
+//}
+//
+//email_mail_data_t* EmailManager::loadMessage(int msg_id)
+//{
+//    email_mail_data_t* mail_data = NULL;
+//    int err = EMAIL_ERROR_NONE;
+//    err = email_get_mail_data(msg_id, &mail_data);
+//    if (EMAIL_ERROR_NONE != err) {
+//        LOGE("email_get_mail_data failed. [%d]", err);
+//    } else {
+//        LOGD("email_get_mail_data success.");
+//    }
+//    return mail_data;
+//}
+//
+//EmailManager::SendReqMapIterator EmailManager::getSendRequest(int mail_id)
+//{
+//    for (auto it = m_sendRequests.begin(); it != m_sendRequests.end(); it++) {
+//        if (it->second->getMessage()->getId() == mail_id) {
+//            return it;
+//        }
+//    }
+//    return m_sendRequests.end();
+//}
+//
+//void EmailManager::freeMessage(email_mail_data_t* mail_data)
+//{
+//    if(!mail_data) {
+//        return;
+//    }
+//
+//    int err = email_free_mail_data(&mail_data,1);
+//    if(EMAIL_ERROR_NONE != err) {
+//        LOGE("Could not free mail data!");
+//    }
+//}
+//
+//void EmailManager::loadMessageBody(MessageBodyCallbackData* callback)
+//{
+//    LOGD("Entered");
+//    if(!callback){
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    if(!callback->getMessage()) {
+//        LOGE("Callback's message is null");
+//        return;
+//    }
+//
+//    m_proxy_load_body->addCallback(callback);
+//
+//    const int mailId = callback->getMessage()->getId();
+//    int err = EMAIL_ERROR_NONE;
+//
+//    int op_handle = -1;
+//    err = email_download_body(mailId, 0, &op_handle);
+//    if(EMAIL_ERROR_NONE != err){
+//        LOGE("Email download body failed, %d", err);
+//        m_proxy_load_body->removeCallback(callback);
+//        return;
+//    }
+//    callback->setOperationHandle(op_handle);
+//}
+//
+//void EmailManager::loadMessageAttachment(MessageAttachmentCallbackData* callback)
+//{
+//    LOGD("Entered");
+//    if(!callback) {
+//        LOGE("Callback is null");
+//        throw Common::InvalidValuesException("Callback is null");
+//    }
+//    if(!callback->getMessageAttachment()) {
+//        LOGE("Callback's message attachment is null");
+//        throw Common::InvalidValuesException("Callback's message attachment is null");
+//    }
+//
+//    std::shared_ptr<MessageAttachment> msgAttachment = callback->getMessageAttachment();
+//    LOGD("attachmentId:%d mailId:%d", msgAttachment->getId(),
+//            msgAttachment->getMessageId());
+//
+//    struct ScopedEmailMailData {
+//        ScopedEmailMailData() : data(NULL) { }
+//        ~ScopedEmailMailData() { EmailManager::freeMessage(data); }
+//        email_mail_data_t* data;
+//    } mail_data_holder;
+//
+//    mail_data_holder.data = EmailManager::loadMessage(msgAttachment->getMessageId());
+//    if(!mail_data_holder.data) {
+//        std::stringstream err_ss;
+//        err_ss << "Couldn't get email_mail_data_t for messageId:"
+//                << msgAttachment->getMessageId();
+//        LOGE("%s",err_ss.str().c_str());
+//        throw Common::UnknownException(err_ss.str().c_str());
+//    }
+//
+//    AttachmentPtrVector attachments = Message::convertEmailToMessageAttachment(
+//            *mail_data_holder.data);
+//    LOGD("Mail:%d contain:%d attachments", msgAttachment->getMessageId(),
+//        attachments.size());
+//
+//    AttachmentPtrVector::iterator it = attachments.begin();
+//    int attachmentIndex = -1;
+//    for(int i = 0; it != attachments.end(); ++i, ++it) {
+//        if((*it)->getId() == msgAttachment->getId()) {
+//            attachmentIndex = i;
+//            break;
+//        }
+//    }
+//
+//    if(attachmentIndex < 0) {
+//        std::stringstream err_ss;
+//        err_ss << "Attachment with id:" << msgAttachment->getId() << "not found";
+//        LOGE("%s",err_ss.str().c_str());
+//        throw Common::UnknownException(err_ss.str().c_str());
+//    }
+//
+//    LOGD("Attachment with id:%d is located at index:%d", msgAttachment->getId(),
+//            attachmentIndex);
+//
+//    int op_handle = -1;
+//    const int nth = attachmentIndex + 1; //in documentation: the minimum number is "1"
+//    callback->setNth(nth);
+//
+//    int err = email_download_attachment(msgAttachment->getMessageId(), nth, &op_handle);
+//    if (EMAIL_ERROR_NONE != err) {
+//        std::stringstream err_ss;
+//        err_ss << "Download email attachment failed with error: " << err;
+//        LOGE("%s",err_ss.str().c_str());
+//        throw Common::UnknownException(err_ss.str().c_str());
+//    } else  {
+//        LOGD("email_download_attachment returned handle:%d",op_handle);
+//        callback->setOperationHandle(op_handle);
+//        m_proxy_load_attachment->addCallback(callback);
+//    }
+//}
+
+//#################################### sync: ###################################
+
+void EmailManager::sync(void* data)
+{
+    LOGD("Entered");
+    SyncCallbackData* callback = static_cast<SyncCallbackData*>(data);
+    if(!callback){
+        LOGE("Callback is null");
+        return;
+    }
+    long op_id = callback->getOpId();
+    //TODO m_proxy_sync->addCallback(op_id, callback);
+
+    int err = EMAIL_ERROR_NONE;
+    int limit = callback->getLimit();
+    int slot_size = -1;
+    int account_id = callback->getAccountId();
+
+    if (limit < 0) {
+        slot_size = m_slot_size;
+    }
+    else {
+        slot_size = limit;
+    }
+
+    err = email_set_mail_slot_size(0, 0, slot_size);
+    if(EMAIL_ERROR_NONE != err){
+        LOGE("Email set slot size failed, %d", err);
+        //TODO m_proxy_sync->removeCallback(op_id);
+        return;
+    }
+
+    int op_handle = -1;
+    err = email_sync_header(account_id, 0, &op_handle);
+    if(EMAIL_ERROR_NONE != err){
+        LOGE("Email sync header failed, %d", err);
+        //TODO m_proxy_sync->removeCallback(op_id);
+    }
+    callback->setOperationHandle(op_handle);
+}
+
+//#################################### ^sync ###################################
+
+////################################## syncFolder: ###############################
+//
+//void EmailManager::syncFolder(SyncFolderCallbackData* callback)
+//{
+//    LOGD("Entered");
+//    if(!callback){
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    const long op_id = callback->getOpId();
+//    m_proxy_sync->addCallback(op_id, callback);
+//
+//    if(!callback->getMessageFolder())
+//    {
+//        LOGE("Callback's messageFolder is null");
+//        m_proxy_sync->removeCallback(op_id);
+//        return;
+//    }
+//
+//    int err = EMAIL_ERROR_NONE;
+//
+//    email_mailbox_t* mailbox = NULL;
+//
+//    const std::string folder_id_str = callback->getMessageFolder()->getId();
+//    int folder_id = 0;
+//    std::istringstream(folder_id_str) >> folder_id;
+//
+//    err = email_get_mailbox_by_mailbox_id(folder_id, &mailbox);
+//    if (EMAIL_ERROR_NONE != err || NULL == mailbox) {
+//        LOGE("Couldn't get mailbox, error code: %d", err);
+//        m_proxy_sync->removeCallback(op_id);
+//        return;
+//    }
+//
+//    try {
+//        const int limit = callback->getLimit();
+//        int slot_size = -1;
+//
+//        if (limit < 0) {
+//            slot_size = m_slot_size;
+//        }
+//        else {
+//            slot_size = limit;
+//        }
+//
+//        err = email_set_mail_slot_size(0, 0, slot_size);
+//        if(EMAIL_ERROR_NONE != err){
+//            LOGE("Email set slot size failed, %d", err);
+//            throw UnknownException("Email set slot size failed");
+//        }
+//
+//        int op_handle = -1;
+//        const int account_id = callback->getAccountId();
+//        err = email_sync_header(account_id, mailbox->mailbox_id, &op_handle);
+//        if(EMAIL_ERROR_NONE != err) {
+//            LOGE("Email sync header failed, %d", err);
+//            m_proxy_sync->removeCallback(op_id);
+//            throw UnknownException("Email sync header failed");
+//        }
+//        callback->setOperationHandle(op_handle);
+//    }
+//    catch (const BasePlatformException& e) {
+//        LOGE("Exception in syncFolder");
+//    }
+//
+//    if (NULL != mailbox)
+//    {
+//        err = email_free_mailbox(&mailbox , 1);
+//        if  (EMAIL_ERROR_NONE !=  err) {
+//            LOGD("Failed to email_free_mailbox - err:%d ", err);
+//        }
+//        mailbox = NULL;
+//    }
+//}
+//
+////#################################### ^syncFolder #############################
+//
+////################################## stopSync: #################################
+//
+//void EmailManager::stopSync(long op_id)
+//{
+//    LOGD("Entered");
+//    SyncCallbackData* callback = NULL;
+//    try {
+//        callback = dynamic_cast<SyncCallbackData*>(
+//                m_proxy_sync->getCallback(op_id));
+//    }
+//    catch (const BasePlatformException& e) {
+//        LOGE("Could not get callback");
+//    }
+//    if(!callback){
+//        LOGE("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){
+//        LOGE("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 #################################
+//
+//void removeEmailCompleteCB(MessagesCallbackUserData* callback)
+//{
+//    LOGD("Entered");
+//    if (!callback) {
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    JSContextRef context = callback->getContext();
+//    if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
+//        LOGE("context was closed");
+//        delete callback;
+//        callback = NULL;
+//        return;
+//    }
+//
+//    try {
+//        if (callback->isError()) {
+//            LOGD("Calling error callback");
+//            JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context,
+//                    callback->getErrorName(),
+//                    callback->getErrorMessage());
+//            callback->callErrorCallback(errobj);
+//        } else {
+//            LOGD("Calling success callback");
+//            callback->callSuccessCallback();
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("Error while calling removeEmail callback: %s (%s)",
+//                (err.getName()).c_str(), (err.getMessage()).c_str());
+//    } catch (...) {
+//        LOGE("Unknown error when calling removeEmail callback.");
+//    }
+//
+//    delete callback;
+//    callback = NULL;
+//}
+//
+//EmailManager::DeleteReqVector::iterator EmailManager::getDeleteRequest(
+//        const std::vector<int> &ids)
+//{
+//    for (auto idIt = ids.begin(); idIt != ids.end(); ++idIt) {
+//        for (auto reqIt = m_deleteRequests.begin(); reqIt != m_deleteRequests.end(); ++reqIt) {
+//            MessagePtrVector msgs = reqIt->callback->getMessages();
+//            for (auto msgIt = msgs.begin(); msgIt != msgs.end(); ++msgIt) {
+//                if ((*msgIt)->getId() == *idIt) {
+//                    return reqIt;
+//                }
+//            }
+//        }
+//    }
+//    return m_deleteRequests.end();
+//}
+//
+//void EmailManager::removeStatusCallback(const std::vector<int> &ids,
+//            email_noti_on_storage_event status)
+//{
+//    LOGD("Enter");
+//    std::lock_guard<std::mutex> lock(m_mutex);
+//    DeleteReqVector::iterator it = getDeleteRequest(ids);
+//    if (it != m_deleteRequests.end()) {
+//        LOGD("Found request");
+//        if (NOTI_MAIL_DELETE_FINISH == status) {
+//            LOGD("Successfully removed %d mails", ids.size());
+//            it->messagesDeleted += ids.size();
+//        }
+//        MessagesCallbackUserData* callback = it->callback;
+//        if (NOTI_MAIL_DELETE_FAIL == status) {
+//            LOGD("Failed to remove mail");
+//            callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR, "Messages remove failed");
+//        }
+//        //if one of mails failed, call error callback
+//        //if all mails are deleted, call success.
+//        // >= is used in case of duplicated dbus messages
+//        if (NOTI_MAIL_DELETE_FAIL == status ||
+//                static_cast<unsigned int>(it->messagesDeleted) >= it->callback->getMessages().size()) {
+//            LOGD("Calling callback");
+//            m_deleteRequests.erase(it);
+//            m_mutex.unlock();
+//            removeEmailCompleteCB(callback);
+//        } else {
+//            LOGD("Not all messages are removed, waiting for next callback");
+//        }
+//    } else {
+//        LOGD("Request not found, ignoring");
+//    }
+//}
+//
+//void EmailManager::removeMessages(MessagesCallbackUserData* callback)
+//{
+//    LOGD("Entered");
+//
+//    if (!callback){
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    int error;
+//    email_mail_data_t *mail = NULL;
+//
+//    try {
+//        std::lock_guard<std::mutex> lock(m_mutex);
+//        std::vector<std::shared_ptr<Message>> messages = callback->getMessages();
+//        MessageType type = callback->getMessageServiceType();
+//        for(auto it = messages.begin() ; it != messages.end(); ++it) {
+//            if((*it)->getType() != type) {
+//                LOGE("Invalid message type");
+//                throw TypeMismatchException("Error while deleting email");
+//            }
+//        }
+//        for (auto it = messages.begin() ; it != messages.end(); ++it) {
+//            error = email_get_mail_data((*it)->getId(), &mail);
+//            if (EMAIL_ERROR_NONE != error) {
+//                LOGE("Couldn't retrieve mail data");
+//                throw UnknownException("Error while deleting mail");
+//            }
+//
+//            //This task (_EMAIL_API_DELETE_MAIL) is for async
+//            error = email_delete_mail(mail->mailbox_id, &mail->mail_id, 1, 0);
+//            if (EMAIL_ERROR_NONE != error) {
+//                email_free_mail_data(&mail, 1);
+//                LOGE("Error while deleting mail");
+//                throw UnknownException("Error while deleting mail");
+//            }
+//            email_free_mail_data(&mail, 1);
+//        }
+//        //store delete request and wait for dbus response
+//        DeleteReq request;
+//        request.callback = callback;
+//        request.messagesDeleted = 0;
+//        m_deleteRequests.push_back(request);
+//    } catch (const BasePlatformException& err) {
+//        LOGE("%s (%s)", (err.getName()).c_str(), (err.getMessage()).c_str());
+//        callback->setError(err.getName(), err.getMessage());
+//        removeEmailCompleteCB(callback);
+//    } catch (...) {
+//        LOGE("Messages remove failed");
+//        callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR, "Messages remove failed");
+//        removeEmailCompleteCB(callback);
+//    }
+//}
+//
+//void EmailManager::updateMessages(MessagesCallbackUserData* callback)
+//{
+//    LOGD("Entered");
+//
+//    if (!callback){
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    int error;
+//    email_mail_data_t *mail = NULL;
+//
+//    try {
+//        std::lock_guard<std::mutex> lock(m_mutex);
+//        std::vector<std::shared_ptr<Message>> messages = callback->getMessages();
+//        MessageType type = callback->getMessageServiceType();
+//        for (auto it = messages.begin() ; it != messages.end(); ++it) {
+//            if ((*it)->getType() != type) {
+//                LOGE("Invalid message type");
+//                throw TypeMismatchException("Error while updating message");
+//            }
+//        }
+//        for (auto it = messages.begin() ; it != messages.end(); ++it) {
+//
+//            mail = Message::convertPlatformEmail((*it));
+//
+//            if((*it)->getHasAttachment())
+//            {
+//                LOGD("Message has attachments. Workaround need to be used.");
+//                //Update of mail on server using function email_update_mail() is not possible.
+//                //Attachment is updated only locally (can't be later loaded from server),
+//                //so use of workaround is needed:
+//                //1. add new mail
+//                //2. delete old mail
+//
+//                //adding message again after changes
+//                addDraftMessagePlatform(mail->account_id, (*it));
+//                LOGD("mail added - new id = [%d]\n", (*it)->getId());
+//
+//                //deleting old mail
+//                LOGD("mail deleted = [%d]\n", mail->mail_id);
+//                error = email_delete_mail(mail->mailbox_id,&mail->mail_id,1,1);
+//                if (EMAIL_ERROR_NONE != error) {
+//                    email_free_mail_data(&mail, 1);
+//                    LOGE("Error while deleting old mail on update: %d", error);
+//                    throw Common::UnknownException("Error while deleting old mail on update");
+//                }
+//            } else {
+//                LOGD("There are no attachments, updating only email data.");
+//                error = email_update_mail(mail, NULL, 0, NULL, 0);
+//                if (EMAIL_ERROR_NONE != error) {
+//                    email_free_mail_data(&mail, 1);
+//                    LOGE("Error while updating mail");
+//                    throw UnknownException("Error while updating mail");
+//                }
+//            }
+//
+//            email_free_mail_data(&mail, 1);
+//        }
+//
+//    } catch (const BasePlatformException& err) {
+//        LOGE("%s (%s)", (err.getName()).c_str(), (err.getMessage()).c_str());
+//        callback->setError(err.getName(), err.getMessage());
+//    } catch (...) {
+//        LOGE("Messages update failed");
+//        callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR, "Messages update failed");
+//    }
+//
+//    //Complete task
+//    JSContextRef context = callback->getContext();
+//    if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
+//        LOGE("context was closed");
+//        delete callback;
+//        callback = NULL;
+//        return;
+//    }
+//
+//    try {
+//        if (callback->isError()) {
+//            LOGD("Calling error callback");
+//            JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context,
+//                    callback->getErrorName(),
+//                    callback->getErrorMessage());
+//            callback->callErrorCallback(errobj);
+//        } else {
+//            LOGD("Calling success callback");
+//            callback->callSuccessCallback();
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("Error while calling updateEmail callback: %s (%s)",
+//                (err.getName()).c_str(), (err.getMessage()).c_str());
+//    } catch (...) {
+//        LOGE("Unknown error when calling updateEmail callback.");
+//    }
+//
+//    delete callback;
+//    callback = NULL;
+//}
+//
+//
+//void EmailManager::findMessages(FindMsgCallbackUserData* callback)
+//{
+//    LOGD("Entered");
+//
+//    if(!callback){
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    email_mail_data_t* mailList = NULL;
+//    int mailListCount = 0;
+//    try {
+//        std::lock_guard<std::mutex> lock(m_mutex);
+//        std::pair<int, email_mail_data_t*> emails =
+//                MessagingDatabaseManager::getInstance().findEmails(callback);
+//        mailListCount = emails.first;
+//        LOGD("Found %d mails", mailListCount);
+//
+//        mailList = emails.second;
+//        email_mail_data_t* nth_email = mailList;
+//
+//        for (int i = 0; i < mailListCount; ++i) {
+//            std::shared_ptr<Message> email =
+//                    Message::convertPlatformEmailToObject(*nth_email);
+//            callback->addMessage(email);
+//            nth_email++;
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("%s (%s)", (err.getName()).c_str(), (err.getMessage()).c_str());
+//        callback->setError(err.getName(), err.getMessage());
+//    } catch (...) {
+//        LOGE("Message find failed");
+//        callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR, "Message find failed");
+//    }
+//
+//    if (mailListCount > 0 && mailList != NULL) {
+//        if (EMAIL_ERROR_NONE != email_free_mail_data(&mailList, mailListCount)) {
+//            LOGW("Failed to free mailList");
+//        }
+//    }
+//
+//    //Complete task
+//    LOGD("callback: %p error:%d messages.size()=%d", callback, callback->isError(),
+//            callback->getMessages().size());
+//
+//    JSContextRef context = callback->getContext();
+//    if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
+//        LOGE("context was closed");
+//        delete callback;
+//        callback = NULL;
+//        return;
+//    }
+//
+//    try {
+//        if (callback->isError()) {
+//            LOGD("Calling error callback");
+//            JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context,
+//                    callback->getErrorName(),
+//                    callback->getErrorMessage());
+//            callback->callErrorCallback(errobj);
+//        } else {
+//            LOGD("Calling success callback");
+//            callback->callSuccessCallback(JSMessage::messageVectorToJSObjectArray(context,
+//                    callback->getMessages()));
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("Error while calling findMessages callback: %s (%s)",
+//                (err.getName()).c_str(), (err.getMessage()).c_str());
+//    } catch (...) {
+//        LOGE("Failed to call findMessages callback.");
+//    }
+//
+//    delete callback;
+//    callback = NULL;
+//}
+//
+//void EmailManager::findConversations(ConversationCallbackData* callback)
+//{
+//    LOGE("Entered");
+//
+//    if(!callback){
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    int convListCount = 0;
+//    try {
+//        std::lock_guard<std::mutex> lock(m_mutex);
+//        std::vector<EmailConversationInfo> conversationsInfo =
+//                MessagingDatabaseManager::getInstance().findEmailConversations(callback);
+//        convListCount = conversationsInfo.size();
+//        LOGD("Found %d conversations", convListCount);
+//
+//        for (int i = 0; i < convListCount; ++i) {
+//            std::shared_ptr<MessageConversation> conversation =
+//                    MessageConversation::convertEmailConversationToObject(conversationsInfo.at(i).id);
+//            conversation->setUnreadMessages(conversationsInfo.at(i).unreadMessages);
+//            callback->addConversation(conversation);
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("%s (%s)", (err.getName()).c_str(), (err.getMessage()).c_str());
+//        callback->setError(err.getName(), err.getMessage());
+//    } catch (...) {
+//        LOGE("Conversation find failed");
+//        callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR, "Conversation find failed");
+//    }
+//
+//    //Complete task
+//    LOGD("callback: %p error:%d conversations.size()=%d", callback, callback->isError(),
+//            callback->getConversations().size());
+//
+//    JSContextRef context = callback->getContext();
+//    if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
+//        LOGE("context was closed");
+//        delete callback;
+//        callback = NULL;
+//        return;
+//    }
+//
+//    try {
+//        if (callback->isError()) {
+//            LOGD("Calling error callback");
+//            JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context,
+//                    callback->getErrorName(),
+//                    callback->getErrorMessage());
+//            callback->callErrorCallback(errobj);
+//        } else {
+//            LOGD("Calling success callback");
+//            callback->callSuccessCallback(
+//                    MessagingUtil::vectorToJSObjectArray<ConversationPtr,
+//                    JSMessageConversation>(context,
+//                            callback->getConversations()));
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("Error while calling findConversations callback: %s (%s)",
+//                (err.getName()).c_str(), (err.getMessage()).c_str());
+//    } catch (...) {
+//        LOGE("Failed to call findConversations callback.");
+//    }
+//
+//    delete callback;
+//    callback = NULL;
+//}
+
+long EmailManager::getUniqueOpId()
+{
+    // mutex is created only on first call (first call added to constructor
+    // to initialize mutex correctly)
+    static std::mutex op_id_mutex;
+    std::lock_guard<std::mutex> lock(op_id_mutex);
+    static long op_id = 0;
+    return op_id++;
+}
+
+//void EmailManager::findFolders(FoldersCallbackData* callback)
+//{
+//    LOGD("Entered");
+//
+//    if (!callback){
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    int ret = EMAIL_ERROR_UNKNOWN;
+//    int account_id = ACCOUNT_ID_NOT_INITIALIZED;
+//    email_mailbox_t* mailboxes = NULL;
+//    email_mailbox_t* nth_mailbox = NULL;
+//    int mailboxes_count;
+//
+//    try {
+//        std::lock_guard<std::mutex> lock(m_mutex);
+//
+//        Tizen::AbstractFilterPtr filter = callback->getFilter();
+//        if (!filter) {
+//            LOGE("Filter not provided");
+//            throw UnknownException("Filter not provided");
+//        }
+//
+//        for(FilterIterator it(filter); false == it.isEnd(); it++) {
+//
+//            if(FIS_COMPOSITE_START == it.getState()) {
+//                CompositeFilterPtr cf = castToCompositeFilter((*it));
+//                if(cf && INTERSECTION != cf->getType()) {
+//                    LOGE("[ERROR] >>> invalid Filter type: %d", cf->getType());
+//                    throw TypeMismatchException("Invalid Filter Type");
+//                }
+//            }
+//            else if(FIS_ATTRIBUTE_FILTER == it.getState()) {
+//                AttributeFilterPtr attrf = castToAttributeFilter((*it));
+//                if(attrf) {
+//                    const std::string attr_name = attrf->getAttributeName();
+//                    if (FIND_FOLDERS_ATTRIBUTE_ACCOUNTID_NAME == attr_name) {
+//                        account_id = static_cast<int>(attrf->getMatchValue()->toLong());
+//                    } else {
+//                        LOGE("The attribute name: %s is invalid", attr_name.c_str());
+//                        throw InvalidValuesException("The attribute name is invalid");
+//                    }
+//                }
+//            }
+//        }
+//
+//        LOGD("Listing folders for account ID: %d", account_id);
+//        if (account_id > 0) {
+//            ret = email_get_mailbox_list(account_id,
+//                    -1,
+//                    &mailboxes,
+//                    &mailboxes_count);
+//            if (EMAIL_ERROR_NONE != ret || !mailboxes) {
+//                LOGE("Cannot get folders: %d", ret);
+//                throw Common::UnknownException(
+//                        "Platform error, cannot get folders");
+//            }
+//
+//            if (mailboxes_count <= 0) {
+//                LOGD("Empty mailboxes");
+//            }
+//            else {
+//                LOGD("Founded mailboxes: %d", mailboxes_count);
+//
+//                nth_mailbox = mailboxes;
+//                for (int i = 0; i < mailboxes_count; ++i) {
+//                    std::shared_ptr<MessageFolder> fd;
+//                    fd = std::make_shared<MessageFolder>(*nth_mailbox);
+//                    callback->addFolder(fd);
+//                    nth_mailbox++;
+//                }
+//            }
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("%s (%s)", (err.getName()).c_str(), (err.getMessage()).c_str());
+//        callback->setError(err.getName(), err.getMessage());
+//    } catch (...) {
+//        LOGE("Messages update failed");
+//        callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR,
+//                "Messages update failed");
+//    }
+//
+//    if (mailboxes != NULL) {
+//        if (EMAIL_ERROR_NONE != email_free_mailbox(&mailboxes,
+//                mailboxes_count)) {
+//            LOGW("Free mailboxes failed: %d", ret);
+//        }
+//    }
+//
+//    //Complete task
+//    LOGD("callback: %p error:%d folders.size()=%d", callback, callback->isError(),
+//            callback->getFolders().size());
+//
+//    JSContextRef context = callback->getContext();
+//    if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
+//        LOGE("context was closed");
+//        delete callback;
+//        callback = NULL;
+//        return;
+//    }
+//
+//    try {
+//        if (callback->isError()) {
+//            LOGD("Calling error callback");
+//            JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context,
+//                    callback->getErrorName(),
+//                    callback->getErrorMessage());
+//            callback->callErrorCallback(errobj);
+//        } else {
+//            LOGD("Calling success callback");
+//            JSObjectRef js_obj = MessagingUtil::vectorToJSObjectArray<FolderPtr,
+//                    JSMessageFolder>(context, callback->getFolders());
+//            callback->callSuccessCallback(js_obj);
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("Error while calling findFolders callback: %s (%s)",
+//                (err.getName()).c_str(), (err.getMessage()).c_str());
+//    } catch (...) {
+//        LOGE("Unknown error when calling findFolders callback.");
+//    }
+//
+//    delete callback;
+//    callback = NULL;
+//}
+//
+//void EmailManager::removeConversations(ConversationCallbackData* callback)
+//{
+//    LOGD("Entered");
+//
+//    if (!callback){
+//        LOGE("Callback is null");
+//        return;
+//    }
+//
+//    int error;
+//    try {
+//        std::lock_guard<std::mutex> lock(m_mutex);
+//        std::vector<std::shared_ptr<MessageConversation>> conversations =
+//                callback->getConversations();
+//        MessageType type = callback->getMessageServiceType();
+//
+//        int thread_id = 0;
+//        for(auto it = conversations.begin() ; it != conversations.end(); ++it) {
+//            if((*it)->getType() != type) {
+//                LOGE("Invalid message type");
+//                throw TypeMismatchException("Error while deleting email conversation");
+//            }
+//        }
+//
+//        for (auto it = conversations.begin() ; it != conversations.end(); ++it) {
+//            thread_id = (*it)->getConversationId();
+//            error = email_delete_thread(thread_id, false);
+//            if (EMAIL_ERROR_NONE != error) {
+//                LOGE("Couldn't delete conversation data");
+//                throw UnknownException("Error while deleting mail conversation");
+//            }
+//
+//            // for now, there is no way to recognize deleting email thread job is completed.
+//            // so use polling to wait the thread is removed.
+//            email_mail_data_t *thread_info = NULL;
+//            do {
+//                usleep(300 * 1000);
+//                LOGD("Waiting to delete this email thread...");
+//                error = email_get_thread_information_by_thread_id(
+//                    thread_id, &thread_info);
+//
+//                if (thread_info != NULL) {
+//                    free(thread_info);
+//                    thread_info = NULL;
+//                }
+//            } while (error != EMAIL_ERROR_MAIL_NOT_FOUND);
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("%s (%s)", (err.getName()).c_str(), (err.getMessage()).c_str());
+//        callback->setError(err.getName(), err.getMessage());
+//    } catch (...) {
+//        LOGE("Messages remove failed");
+//        callback->setError(JSWebAPIErrorFactory::UNKNOWN_ERROR, "Messages remove failed");
+//    }
+//
+//    JSContextRef context = callback->getContext();
+//    if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
+//        LOGE("context was closed");
+//        delete callback;
+//        callback = NULL;
+//        return;
+//    }
+//
+//    try {
+//        if (callback->isError()) {
+//            LOGD("Calling error callback");
+//            JSObjectRef errobj = JSWebAPIErrorFactory::makeErrorObject(context,
+//                    callback->getErrorName(),
+//                    callback->getErrorMessage());
+//            callback->callErrorCallback(errobj);
+//        } else {
+//            LOGD("Calling success callback");
+//            callback->callSuccessCallback();
+//        }
+//    } catch (const BasePlatformException& err) {
+//        LOGE("Error while calling removeConversations callback: %s (%s)",
+//                (err.getName()).c_str(), (err.getMessage()).c_str());
+//    } catch (...) {
+//        LOGE("Unknown error when calling removeConversations callback.");
+//    }
+//
+//    delete callback;
+//    callback = NULL;
+//}
+
+} // Messaging
+} // DeviceAPI
diff --git a/src/messaging/email_manager.h b/src/messaging/email_manager.h
new file mode 100644 (file)
index 0000000..539610b
--- /dev/null
@@ -0,0 +1,134 @@
+//
+// Tizen Web Device API
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file: EmailManager.h
+ */
+
+#ifndef __TIZEN_EMAIL_MANAGER_H__
+#define __TIZEN_EMAIL_MANAGER_H__
+
+#include <glib.h>
+#include <mutex>
+#include <string>
+#include <map>
+#include <vector>
+
+#include "email-api-network.h"
+#include "email-api-account.h"
+#include "email-api-mail.h"
+#include "email-api-mailbox.h"
+
+#include "common/callback_user_data.h"
+//#include <PlatformException.h>
+
+#include "messaging_util.h"
+#include "message_service.h"
+
+//#include "DBus/Connection.h"
+//#include "DBus/SyncProxy.h"
+//#include "DBus/LoadBodyProxy.h"
+//#include "DBus/LoadAttachmentProxy.h"
+//#include "DBus/MessageProxy.h"
+//#include "DBus/SendProxy.h"
+
+namespace extension {
+namespace messaging {
+
+//class Message;
+//class MessageCallbackUserData;
+//class FindMsgCallbackUserData;
+//class SyncFolderCallbackData;
+
+class EmailManager {
+public:
+    static EmailManager& getInstance();
+
+//    void addDraftMessage(MessageCallbackUserData* callback);
+//    void removeMessages(MessagesCallbackUserData* callback);
+//    void updateMessages(MessagesCallbackUserData* callback);
+//    void findMessages(FindMsgCallbackUserData* callback);
+//    void findConversations(ConversationCallbackData* callback);
+//    void findFolders(FoldersCallbackData* callback);
+//    void removeConversations(ConversationCallbackData* callback);
+//
+//    void sendMessage(MessageRecipientsCallbackData* callback);
+//    void sendStatusCallback(int mail_id, email_noti_on_network_event status,
+//            int error_code);
+//    void removeStatusCallback(const std::vector<int> &ids,
+//            email_noti_on_storage_event status);
+//
+//    void loadMessageBody(MessageBodyCallbackData* callback);
+//    void loadMessageAttachment(MessageAttachmentCallbackData* callback);
+
+    void sync(void* data);
+//    void syncFolder(SyncFolderCallbackData* callback);
+//    void stopSync(long op_id);
+//
+//    void registerStatusCallback(msg_handle_t msg_handle);
+
+    /**
+     * Use freeMessage() to release returned email_mail_data_t object.
+     */
+    static email_mail_data_t* loadMessage(int msg_id);
+    static void freeMessage(email_mail_data_t*);
+
+    long getUniqueOpId();
+
+private:
+    EmailManager();
+    EmailManager(const EmailManager &);
+    void operator=(const EmailManager &);
+    virtual ~EmailManager();
+//    void addDraftMessagePlatform(int account_id,
+//        std::shared_ptr<Message> message);
+//    void addOutboxMessagePlatform(int account_id,
+//        std::shared_ptr<Message> message);
+//    void addMessagePlatform(int account_id, std::shared_ptr<Message> message,
+//        email_mailbox_type_e mailbox_type);
+//
+//    typedef std::map<int, MessageRecipientsCallbackData*> SendReqMap;
+//    typedef SendReqMap::iterator SendReqMapIterator;
+//    SendReqMapIterator getSendRequest(int mail_id);
+//    SendReqMap m_sendRequests;
+//    struct DeleteReq {
+//        MessagesCallbackUserData* callback;
+//        int messagesDeleted;
+//    };
+//    typedef std::vector<DeleteReq> DeleteReqVector;
+//    /**
+//     * Find first request containing at least one message id
+//     * @param ids
+//     * @return
+//     */
+//    DeleteReqVector::iterator getDeleteRequest(const std::vector<int> &ids);
+//    DeleteReqVector m_deleteRequests;
+
+    int m_slot_size;
+
+//    DBus::SyncProxyPtr m_proxy_sync;
+//    DBus::LoadBodyProxyPtr m_proxy_load_body;
+//    DBus::LoadAttachmentProxyPtr m_proxy_load_attachment;
+//    DBus::MessageProxyPtr m_proxy_messageStorage;
+//    DBus::SendProxyPtr m_proxy_send;
+
+    std::mutex m_mutex;
+};
+
+} // Messaging
+} // DeviceAPI
+#endif // __TIZEN_EMAIL_MANAGER_H__
index daa2ebdeb76182d77c0e3e560d5843c42f2bf119..e4a9b7dc9e6c9e99e89f7a56cfbea5521afa32c1 100644 (file)
@@ -221,7 +221,7 @@ void MessageService::loadMessageAttachment()
     throw common::NotSupportedException("Cannot load message attachment");
 }
 
-long MessageService::sync(const double callbackId, long limit)
+long MessageService::sync(SyncCallbackData *callback)
 {
     // this method should be overwritten by email service
     // for MMS and SMS this function is not supported
index 80705bb02f8286858061a7d234e7821500038cca..2138d2efacf15e9e9526be8c7717d136731f5089 100755 (executable)
@@ -95,7 +95,7 @@ public:
     virtual void sendMessage();
     virtual void loadMessageBody();
     virtual void loadMessageAttachment();
-    virtual long sync(const double callbackId, long limit = 0);
+    virtual long sync(SyncCallbackData *callback);
     virtual long syncFolder();
     virtual void stopSync();
 
index c383ed90bf374f8c04c0646fa1bb4abac56f6246..88a00611c175bac2061bb54571247c333efedf84 100644 (file)
@@ -3,6 +3,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 #include "message_service_email.h"
+#include "email_manager.h"
 
 #include "common/logger.h"
 
@@ -40,11 +41,42 @@ void MessageServiceEmail::loadMessageAttachment()
     //TODO add implementation
 }
 
-long MessageServiceEmail::sync(const double callbackId, long limit)
+static gboolean syncTask(void* data)
+{
+    LOGD("Entered");
+
+    try {
+        EmailManager::getInstance().sync(data);
+
+//    } catch(const Common::BasePlatformException& exception) {
+//        LOGE("Unhandled exception: %s (%s)!", (exception.getName()).c_str(),
+//             (exception.getMessage()).c_str());
+    } catch(...) {
+        LOGE("Unhandled exception!");
+    }
+
+    return FALSE;
+}
+
+long MessageServiceEmail::sync(SyncCallbackData *callback)
 {
     LoggerD("Entered");
-    //TODO add implementation
-    return 0;
+    if (!callback) {
+        LOGE("Callback is null");
+        //TODO throw Common::UnknownException("Callback is null");
+    }
+
+    long op_id = EmailManager::getInstance().getUniqueOpId();
+    callback->setOpId(op_id);
+    //callback->setAccountId(m_id);
+
+    guint id = g_idle_add(syncTask, static_cast<void*>(callback));
+    if (!id) {
+        LOGE("g_idle_add failed");
+        delete callback;
+        //TODO throw Common::UnknownException("Could not add task");
+    }
+    return op_id;
 }
 
 long MessageServiceEmail::syncFolder()
index 4d44c9b4e69f509604b3f326341b32f09abcaa00..71e16529fcb06756368bcc68157273953c9a9cb5 100755 (executable)
@@ -19,7 +19,7 @@ public:
     virtual void sendMessage();
     virtual void loadMessageBody();
     virtual void loadMessageAttachment();
-    virtual long sync(const double callbackId, long limit = 0);
+    virtual long sync(SyncCallbackData *callback);
     virtual long syncFolder();
     virtual void stopSync();
 };
index f9a7202dbfbeebce6a8baac0169ddaaf9a94c06f..8fb0dc392673246b3df9be442e3984649526b43c 100644 (file)
             'dbus-1',
             'dbus-glib-1',
             'capi-system-info',
-            'tapi'
+            'tapi',
+            'vconf'
         ],
       },
       'sources': [
         'messaging_api.js',
+        'email_manager.cc',
+        'email_manager.h',
         'messaging_instance.cc',
         'messaging_instance.h',
         'messaging_extension.cc',
index 61541cf6c4e9cb7f0a799943b97205763e2f1ef6..f31a789dd4f4c3a90294607c13d58bbf90742edb 100644 (file)
@@ -176,7 +176,18 @@ void MessagingInstance::MessageServiceSync(const picojson::value& args,
     if (v_limit.is<double>()) {
         limit = static_cast<long>(v_limit.get<double>());
     }
-    MessagingManager::getInstance().getMessageServiceEmail(id)->sync(callbackId, limit);
+
+    auto json = std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
+    picojson::object& obj = json->get<picojson::object>();
+    obj[JSON_CALLBACK_ID] = picojson::value(callbackId);
+    //obj[JSON_DATA] = picojson::value( TODO );
+
+    SyncCallbackData *callback = new SyncCallbackData();
+    callback->setJson(json);
+    callback->setAccountId(id);
+    callback->setLimit(limit);
+
+    MessagingManager::getInstance().getMessageServiceEmail(id)->sync(callback);
 }
 
 void MessagingInstance::MessageServiceSyncFolder(const picojson::value& args,