2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <Commons/Exception.h>
20 //#include "EventMessagingService.h"
21 #include "EmailAccountInfo.h"
22 #include "MessagingService.h"
24 #include "EventMessagingService.h"
26 #include <email-types.h>
27 #include <email-api.h>
35 MessagingService::MessagingService() :
41 MessagingService::~MessagingService() {
44 m_opRequests.clear(); //clean Messaging Service Handles
49 void MessagingService::initialize() {
57 void MessagingService::setMessagingServiceType(int type)
62 void MessagingService::setMessagingServiceName(const string& name)
67 void MessagingService::setMessagingServiceId(int serviceId)
72 int MessagingService::sync(const IMessagingServicePtr& messagingService, const int limit)
76 return MailSync::getInstance().syncAccount( messagingService, limit );
79 void MessagingService::syncCancel(int handle)
82 return MailSync::getInstance().syncAccountCancel(handle);
85 int MessagingService::syncFolder(const IMessagingServicePtr& messagingService, const int folder_id, const int limit)
88 return MailSync::getInstance().syncFolder( messagingService, folder_id, limit);
91 void MessagingService::syncFolderCancel(int handle)
94 return MailSync::getInstance().syncFolderCancel(handle);
98 int MessagingService::createOpId(int type)
101 int cnt = m_opRequests.size(); //item count.
106 //OpRequestsIterator it = std::max_element(m_opRequests.begin(), m_opRequests.end(), MessagingService::OpRequest_comparer);
107 // OpRequestsIterator it = m_opRequests.end();
108 OpRequestsIterator it;
110 for( it = m_opRequests.begin(); it != m_opRequests.end(); ++it)
112 lastIndex = it->first;
115 if ( (cnt * MESSAGING_SERVICE_OP_COUNT) >= lastIndex )
117 index = ( lastIndex + MESSAGING_SERVICE_OP_COUNT - ( lastIndex % MESSAGING_SERVICE_OP_COUNT) ) + type;
123 OpRequestsIterator iter;
125 for( iter = m_opRequests.begin(); iter != m_opRequests.end(); ++iter)
127 int currentValue = iter->first;
129 if ( currentValue - preValue > MESSAGING_SERVICE_OP_COUNT)
131 index = ( preValue + MESSAGING_SERVICE_OP_COUNT - ( preValue % MESSAGING_SERVICE_OP_COUNT) ) + type;
136 index = ( currentValue + MESSAGING_SERVICE_OP_COUNT - ( currentValue % MESSAGING_SERVICE_OP_COUNT) ) + type;
148 LogDebug("m_opRequests size : " << cnt );
149 LogDebug("index " << index);
150 m_opRequests.insert(std::make_pair(index, MessagingServiceOpData(0,type)));
155 int MessagingService::getHandleFromOpId(int opIdx)
157 LogDebug("operation index size : " << opIdx);
161 OpRequestsIterator it = m_opRequests.find(opIdx);
162 if ( m_opRequests.end() != it)
164 handle = it->second.handle;
167 LogDebug("index " << handle);
172 IMessagePtr MessagingService::getMessageFromOpId(int opIdx)
174 LogDebug("operation index : " << opIdx);
175 OpRequestsIterator it = m_opRequests.find(opIdx);
177 if ( m_opRequests.end() != it)
179 msg = it->second.message;
185 EventMessagingServicePtr MessagingService::getEventFromOpId(int opIdx)
187 LogDebug("operation index : " << opIdx);
188 OpRequestsIterator it = m_opRequests.find(opIdx);
189 EventMessagingServicePtr event;
190 if ( m_opRequests.end() != it)
192 LogDebug("Find Event");
193 event = it->second.event;
199 void MessagingService::setHandleToOpId(int opIdx, int handle)
201 LogDebug("operation index : " << opIdx);
202 OpRequestsIterator it = m_opRequests.find(opIdx);
204 if ( m_opRequests.end() != it)
206 LogDebug(" find Message ");
207 it->second.handle = handle;
212 void MessagingService::setMessageToOpId(int opIdx, IMessagePtr& message)
214 LogDebug("operation index : " << opIdx);
215 OpRequestsIterator it = m_opRequests.find(opIdx);
217 if ( m_opRequests.end() != it)
219 LogDebug("set Message ");
220 it->second.message = message;
224 void MessagingService::setEventToOpId(int opIdx, EventMessagingServicePtr & event)
226 LogDebug("operation index : " << opIdx);
227 OpRequestsIterator it = m_opRequests.find(opIdx);
229 if ( m_opRequests.end() != it)
231 LogDebug("set Message ");
234 LogDebug("vaild event ");
235 it->second.event = event;
240 int MessagingService::getOpTypeFromOpId(int opIdx)
242 LogDebug("operation index : " << opIdx);
246 OpRequestsIterator it = m_opRequests.find(opIdx);
247 if ( m_opRequests.end() != it)
249 type = it->second.type;
252 LogDebug("type " << type);
256 int MessagingService::deleteOpId(int opIdx)
259 OpRequestsIterator it = m_opRequests.find(opIdx);
260 if ( m_opRequests.end() != it)
262 m_opRequests.erase(it); //delete
263 LogDebug("delete Request : " << m_opRequests.size());
268 void MessagingService::cancelOperation(int opId, int handle, int eventType, IMessagePtr& message)
272 if (eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY
273 || eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
275 IEmailPtr email = DPL::DynamicPointerCast<IEmail >(message);
278 if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY )
280 LogDebug("Cancel Download Body , handle = " << handle);
281 email->downloadBodyCancel(handle);
283 else if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
285 LogDebug("Cancel Download Attachment , handle = " << handle);
286 email->downloadAttachmentCancel(handle);
291 ThrowMsg( WrtDeviceApis::Commons::ConversionException, "Conversion IMessage to IEmail error");
295 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC)
299 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
301 syncFolderCancel(handle);
303 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SEND_MESSAGE)
305 message->sendCancel(handle);