2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @author Wojciech Bielawski (w.bielawski@samsung.com)
23 #include <Commons/Exception.h>
24 //#include <API/Messaging/EventMessagingService.h>
25 #include <API/Messaging/EmailAccountInfo.h>
26 #include "MessagingService.h"
28 #include "API/Messaging/EventMessagingService.h"
30 #include <email-types.h>
31 #include <email-api.h>
35 using namespace TizenApis::Api::Messaging;
41 MessagingService::MessagingService() :
47 MessagingService::~MessagingService() {
50 m_opRequests.clear(); //clean Messaging Service Handles
55 void MessagingService::initialize() {
63 void MessagingService::setMessagingServiceType(int type)
68 void MessagingService::setMessagingServiceName(const string& name)
73 void MessagingService::setMessagingServiceId(int serviceId)
78 int MessagingService::sync(const Api::Messaging::IMessagingServicePtr& messagingService, const int limit)
82 return MailSync::getInstance().syncAccount( messagingService, limit );
85 void MessagingService::syncCancel(int handle)
88 return MailSync::getInstance().syncAccountCancel(handle);
91 int MessagingService::syncFolder(const Api::Messaging::IMessagingServicePtr& messagingService, const int folder_id, const int limit)
94 return MailSync::getInstance().syncFolder( messagingService, folder_id, limit);
97 void MessagingService::syncFolderCancel(int handle)
100 return MailSync::getInstance().syncFolderCancel(handle);
104 int MessagingService::createOpId(int type)
107 int cnt = m_opRequests.size(); //item count.
112 //OpRequestsIterator it = std::max_element(m_opRequests.begin(), m_opRequests.end(), MessagingService::OpRequest_comparer);
113 OpRequestsIterator it = m_opRequests.end();
114 int lastIndex = it->first;
116 if ( (cnt * MESSAGING_SERVICE_OP_COUNT) >= lastIndex )
118 index = ( lastIndex + MESSAGING_SERVICE_OP_COUNT - ( lastIndex % MESSAGING_SERVICE_OP_COUNT) ) + type;
124 OpRequestsIterator iter;
126 for( iter = m_opRequests.begin(); iter != m_opRequests.end(); ++iter)
128 int currentValue = it->first;
130 if ( currentValue - preValue > MESSAGING_SERVICE_OP_COUNT)
132 index = ( preValue + MESSAGING_SERVICE_OP_COUNT - ( preValue % MESSAGING_SERVICE_OP_COUNT) ) + type;
137 index = ( currentValue + MESSAGING_SERVICE_OP_COUNT - ( currentValue % MESSAGING_SERVICE_OP_COUNT) ) + type;
149 LogDebug("m_opRequests size : " << cnt );
150 LogDebug("index " << index);
151 m_opRequests.insert(std::make_pair(index, MessagingServiceOpData(0,type)));
156 int MessagingService::getHandleFromOpId(int opIdx)
158 LogDebug("operation index size : " << opIdx);
162 OpRequestsIterator it = m_opRequests.find(opIdx);
163 if ( m_opRequests.end() != it)
165 handle = it->second.handle;
168 LogDebug("index " << handle);
173 Api::Messaging::IMessagePtr MessagingService::getMessageFromOpId(int opIdx)
175 LogDebug("operation index : " << opIdx);
176 OpRequestsIterator it = m_opRequests.find(opIdx);
178 if ( m_opRequests.end() != it)
180 msg = it->second.message;
186 Api::Messaging::EventMessagingServicePtr MessagingService::getEventFromOpId(int opIdx)
188 LogDebug("operation index : " << opIdx);
189 OpRequestsIterator it = m_opRequests.find(opIdx);
190 EventMessagingServicePtr event;
191 if ( m_opRequests.end() != it)
193 LogDebug("Find Event");
194 event = it->second.event;
200 void MessagingService::setHandleToOpId(int opIdx, int handle)
202 LogDebug("operation index : " << opIdx);
203 OpRequestsIterator it = m_opRequests.find(opIdx);
205 if ( m_opRequests.end() != it)
207 LogDebug(" find Message ");
208 it->second.handle = handle;
213 void MessagingService::setMessageToOpId(int opIdx, Api::Messaging::IMessagePtr& message)
215 LogDebug("operation index : " << opIdx);
216 OpRequestsIterator it = m_opRequests.find(opIdx);
218 if ( m_opRequests.end() != it)
220 LogDebug("set Message ");
221 it->second.message = message;
225 void MessagingService::setEventToOpId(int opIdx, Api::Messaging::EventMessagingServicePtr & event)
227 LogDebug("operation index : " << opIdx);
228 OpRequestsIterator it = m_opRequests.find(opIdx);
230 if ( m_opRequests.end() != it)
232 LogDebug("set Message ");
235 LogDebug("vaild event ");
236 it->second.event = event;
241 int MessagingService::getOpTypeFromOpId(int opIdx)
243 LogDebug("operation index : " << opIdx);
247 OpRequestsIterator it = m_opRequests.find(opIdx);
248 if ( m_opRequests.end() != it)
250 type = it->second.type;
253 LogDebug("type " << type);
257 int MessagingService::deleteOpId(int opIdx)
260 OpRequestsIterator it = m_opRequests.find(opIdx);
261 if ( m_opRequests.end() != it)
263 m_opRequests.erase(it); //delete
264 LogDebug("delete Request : " << m_opRequests.size());
269 void MessagingService::cancelOperation(int opId, int handle, int eventType, Api::Messaging::IMessagePtr& message)
273 if (eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY
274 || eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
276 Api::Messaging::IEmailPtr email = DPL::DynamicPointerCast<IEmail >(message);
279 if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY )
281 LogDebug("Cancel Download Body , handle = " << handle);
282 email->downloadBodyCancel(handle);
284 else if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
286 LogDebug("Cancel Download Attachment , handle = " << handle);
287 email->downloadAttachmentCancel(handle);
292 ThrowMsg( WrtDeviceApis::Commons::ConversionException, "Conversion IMessage to IEmail error");
296 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC)
300 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
302 syncFolderCancel(handle);
304 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SEND_MESSAGE)
306 message->sendCancel(handle);
319 } //namespace Platform
320 } //namespace WrtPlugins