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>
36 MessagingService::MessagingService() :
42 MessagingService::~MessagingService() {
45 m_opRequests.clear(); //clean Messaging Service Handles
50 void MessagingService::initialize() {
58 void MessagingService::setMessagingServiceType(int type)
63 void MessagingService::setMessagingServiceName(const string& name)
68 void MessagingService::setMessagingServiceId(int serviceId)
73 int MessagingService::sync(const IMessagingServicePtr& messagingService, const int limit)
77 return MailSync::getInstance().syncAccount( messagingService, limit );
80 void MessagingService::syncCancel(int handle)
83 return MailSync::getInstance().syncAccountCancel(handle);
86 int MessagingService::syncFolder(const IMessagingServicePtr& messagingService, const int folder_id, const int limit)
89 return MailSync::getInstance().syncFolder( messagingService, folder_id, limit);
92 void MessagingService::syncFolderCancel(int handle)
95 return MailSync::getInstance().syncFolderCancel(handle);
99 int MessagingService::createOpId(int type)
102 int cnt = m_opRequests.size(); //item count.
107 //OpRequestsIterator it = std::max_element(m_opRequests.begin(), m_opRequests.end(), MessagingService::OpRequest_comparer);
108 // OpRequestsIterator it = m_opRequests.end();
109 OpRequestsIterator it;
111 for( it = m_opRequests.begin(); it != m_opRequests.end(); ++it)
113 lastIndex = it->first;
116 if ( (cnt * MESSAGING_SERVICE_OP_COUNT) > lastIndex )
118 index = ( lastIndex + MESSAGING_SERVICE_OP_COUNT - ( lastIndex % MESSAGING_SERVICE_OP_COUNT) ) + type;
123 bool checkFirstEmpty = TRUE;
125 OpRequestsIterator iter;
127 for( iter = m_opRequests.begin(); iter != m_opRequests.end(); ++iter)
129 int currentValue = iter->first;
131 if(checkFirstEmpty ==TRUE && currentValue >= MESSAGING_SERVICE_OP_COUNT)
136 checkFirstEmpty = FALSE;
138 if ( currentValue - preValue < MESSAGING_SERVICE_OP_COUNT*2 )
140 preValue = currentValue - ( currentValue % MESSAGING_SERVICE_OP_COUNT);
144 index = preValue + MESSAGING_SERVICE_OP_COUNT + type;
156 LoggerD("m_opRequests size : " << cnt );
157 LoggerD("index " << index);
158 m_opRequests.insert(std::make_pair(index, MessagingServiceOpData(0,type)));
163 int MessagingService::getHandleFromOpId(int opIdx)
165 LoggerD("operation index size : " << opIdx);
169 OpRequestsIterator it = m_opRequests.find(opIdx);
170 if ( m_opRequests.end() != it)
172 handle = it->second.handle;
175 LoggerD("index " << handle);
180 IMessagePtr MessagingService::getMessageFromOpId(int opIdx)
182 LoggerD("operation index : " << opIdx);
183 OpRequestsIterator it = m_opRequests.find(opIdx);
185 if ( m_opRequests.end() != it)
187 msg = it->second.message;
193 EventMessagingServicePtr MessagingService::getEventFromOpId(int opIdx)
195 LoggerD("operation index : " << opIdx);
196 OpRequestsIterator it = m_opRequests.find(opIdx);
197 EventMessagingServicePtr event;
198 if ( m_opRequests.end() != it)
200 LoggerD("Find Event");
201 event = it->second.event;
207 EventMessagingServicePtr MessagingService::getEventFromHandle(int handle)
209 LoggerD("handle index : " << handle);
210 EventMessagingServicePtr event;
211 int cnt = m_opRequests.size(); //item count.
212 LoggerD("cnt : " << cnt);
215 OpRequestsIterator it;
216 for( it = m_opRequests.begin(); it != m_opRequests.end(); ++it)
218 LoggerD("it->first : " << it->first);
219 LoggerD("it->second.handle : " << it->second.handle);
220 if((unsigned int)handle == it->second.handle)
222 LoggerD("get opId : " << it->first);
223 LoggerD("event of handle : " << it->second.handle);
224 event = it->second.event;
231 void MessagingService::setHandleToOpId(int opIdx, int handle)
233 LoggerD("operation index : " << opIdx);
234 OpRequestsIterator it = m_opRequests.find(opIdx);
236 if ( m_opRequests.end() != it)
238 LoggerD(" find Message ");
239 it->second.handle = handle;
244 void MessagingService::setMessageToOpId(int opIdx, IMessagePtr& message)
246 LoggerD("operation index : " << opIdx);
247 OpRequestsIterator it = m_opRequests.find(opIdx);
249 if ( m_opRequests.end() != it)
251 LoggerD("set Message ");
252 it->second.message = message;
256 void MessagingService::setEventToOpId(int opIdx, EventMessagingServicePtr & event)
258 LoggerD("operation index : " << opIdx);
259 OpRequestsIterator it = m_opRequests.find(opIdx);
261 if ( m_opRequests.end() != it)
263 LoggerD("set event ");
266 LoggerD("vaild event ");
267 it->second.event = event;
272 int MessagingService::getOpTypeFromOpId(int opIdx)
274 LoggerD("operation index : " << opIdx);
278 OpRequestsIterator it = m_opRequests.find(opIdx);
279 if ( m_opRequests.end() != it)
281 type = it->second.type;
284 LoggerD("type " << type);
288 int MessagingService::deleteOpId(int opIdx)
291 OpRequestsIterator it = m_opRequests.find(opIdx);
292 if ( m_opRequests.end() != it)
294 m_opRequests.erase(it); //delete
295 LoggerD("delete Request : " << m_opRequests.size());
300 void MessagingService::cancelOperation(int opId, int handle, int eventType, IMessagePtr& message)
304 if (eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY
305 || eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
307 IEmailPtr email = DPL::DynamicPointerCast<IEmail >(message);
310 if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY )
312 LoggerD("Cancel Download Body , handle = " << handle);
313 email->downloadBodyCancel(handle);
315 else if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
317 LoggerD("Cancel Download Attachment , handle = " << handle);
318 email->downloadAttachmentCancel(handle);
323 ThrowMsg( WrtDeviceApis::Commons::ConversionException, "Conversion IMessage to IEmail error");
327 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC)
331 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
333 syncFolderCancel(handle);
335 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SEND_MESSAGE)
337 message->sendCancel(handle);