diff options
Diffstat (limited to 'mobile_src/Messaging/MailRemover.cpp')
-rw-r--r-- | mobile_src/Messaging/MailRemover.cpp | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/mobile_src/Messaging/MailRemover.cpp b/mobile_src/Messaging/MailRemover.cpp new file mode 100644 index 0000000..42486f4 --- /dev/null +++ b/mobile_src/Messaging/MailRemover.cpp @@ -0,0 +1,176 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 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. +// + +#include <utility> +#include <email-types.h> +#include <email-api.h> +#include <email-api-network.h> +#include <email-api-init.h> +#include <email-api-account.h> + +#include <Commons/Exception.h> +#include <Commons/ThreadPool.h> +#include "ReqReceiverMessage.h" +#include "EventDeleteMessages.h" +#include "EventMessagingService.h" +#include "MailRemover.h" +#include "Messaging.h" +#include <Logger.h> + + +namespace { +const char* DBUS_FILTER_REMOVE_STATUS = + "type='signal',interface='User.Email.StorageChange'"; +} + +namespace DeviceAPI { +namespace Messaging { + +using namespace WrtDeviceApis::Commons; + +MailRemover& MailRemover::getInstance() +{ + static MailRemover instance; + return instance; +} + +int MailRemover::removeCheck(int accountId, const IEmailPtr& mail) +{ + + email_mail_data_t* result = NULL; + int mailId = mail->convertId(mail->getIdRef()); + + int error = email_get_mail_data(mailId, &result); + + email_free_mail_data(&result,1); + + if (error == EMAIL_ERROR_MAIL_NOT_FOUND) { + return 1; + } + else + { + RemoveRequestData data = RemoveRequestData(accountId, mail); + m_requests.insert(std::make_pair(mailId, data)); + } + return 0; +} + +// TODO Copied from former solution, refactor it. +void MailRemover::OnEventReceived(const DBus::MessageEvent& event) +{ + DBus::MessagePtr message = event.GetArg0(); + + int account_id = 0, mail_id = 0, thread_id = 0, status = 0, mailId; + std::string name; + + if(m_requests.size()) + { + + DBus::Message::ReadIterator readIterator = message->getReadIterator(); + for (int i = 0; readIterator->isValid(); readIterator->next(), ++i) + { + if ((i == 0) && (readIterator->getArgType() == DBUS_TYPE_INT32)) + { + status = readIterator->getInt(); + } + else if ((i == 1) && (readIterator->getArgType() == DBUS_TYPE_INT32)) + { + account_id = readIterator->getInt(); + } + else if ((i == 2) && (readIterator->getArgType() == DBUS_TYPE_INT32)) + { + mail_id = readIterator->getInt(); + } + else if ((i == 3) && (readIterator->getArgType() == DBUS_TYPE_STRING)) + { + name = readIterator->getString(); + } + else if ((i == 4) && (readIterator->getArgType() == DBUS_TYPE_INT32)) + { + thread_id = readIterator->getInt(); + } + } + + std::istringstream(name) >> mailId; + + RemoveRequestsIterator requestsIt = m_requests.find(mailId); //find reqeuset + if (m_requests.end() != requestsIt) { + switch (status) { + case NOTI_MAIL_DELETE_FINISH: + { + const IEmailPtr& mail = requestsIt->second.mail; + + EventDeleteMessagesReqReceiver* requestReceiver = mail->getRequestReceiver(); + + if (requestReceiver) { + EventDeleteMessagesPtr event = mail->getRemoveMessagesEvent(); + if (event) + { + requestReceiver->ManualAnswer(event); + } + + } + m_requests.erase(mailId); + break; + } + default: + break; + } + } + else + { + LoggerW("Can't find mail_id " << mail_id); + } + } +} + +MailRemover::MailRemover() : + m_dbusThread(new DPL::Thread()), + m_dbus(new DBus::Connection()) +{ + m_dbusThread->Run(); + + m_dbus->setWorkerThread(m_dbusThread.Get()); + m_dbus->AddListener(this); + m_dbus->SwitchAllListenersToThread( + ThreadPool::getInstance().getThreadRef(ThreadEnum::MESSAGING_THREAD) + ); + m_dbus->addFilter(DBUS_FILTER_REMOVE_STATUS); + m_dbus->open(DBUS_BUS_SYSTEM); +} + +MailRemover::~MailRemover() +{ + m_dbus->RemoveListener(this); + m_dbus->close(); + m_dbusThread->Quit(); + + //close email service + if (EMAIL_ERROR_NONE == email_close_db()) { + if (EMAIL_ERROR_NONE == email_service_end()){ + } + else{ + LoggerD("Email service end failed\n"); + } + } + else{ + LoggerD("Email Close DB failed\n"); + } +} + +} +} |