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;
124 OpRequestsIterator iter;
126 for( iter = m_opRequests.begin(); iter != m_opRequests.end(); ++iter)
128 int currentValue = iter->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 LoggerD("m_opRequests size : " << cnt );
150 LoggerD("index " << index);
151 m_opRequests.insert(std::make_pair(index, MessagingServiceOpData(0,type)));
156 int MessagingService::getHandleFromOpId(int opIdx)
158 LoggerD("operation index size : " << opIdx);
162 OpRequestsIterator it = m_opRequests.find(opIdx);
163 if ( m_opRequests.end() != it)
165 handle = it->second.handle;
168 LoggerD("index " << handle);
173 IMessagePtr MessagingService::getMessageFromOpId(int opIdx)
175 LoggerD("operation index : " << opIdx);
176 OpRequestsIterator it = m_opRequests.find(opIdx);
178 if ( m_opRequests.end() != it)
180 msg = it->second.message;
186 EventMessagingServicePtr MessagingService::getEventFromOpId(int opIdx)
188 LoggerD("operation index : " << opIdx);
189 OpRequestsIterator it = m_opRequests.find(opIdx);
190 EventMessagingServicePtr event;
191 if ( m_opRequests.end() != it)
193 LoggerD("Find Event");
194 event = it->second.event;
200 EventMessagingServicePtr MessagingService::getEventFromHandle(int handle)
202 LoggerD("handle index : " << handle);
203 EventMessagingServicePtr event;
204 int cnt = m_opRequests.size(); //item count.
205 LoggerD("cnt : " << cnt);
208 OpRequestsIterator it;
209 for( it = m_opRequests.begin(); it != m_opRequests.end(); ++it)
211 LoggerD("it->first : " << it->first);
212 LoggerD("it->second.handle : " << it->second.handle);
213 if((unsigned int)handle == it->second.handle)
215 LoggerD("get opId : " << it->first);
216 LoggerD("event of handle : " << it->second.handle);
217 event = it->second.event;
224 void MessagingService::setHandleToOpId(int opIdx, int handle)
226 LoggerD("operation index : " << opIdx);
227 OpRequestsIterator it = m_opRequests.find(opIdx);
229 if ( m_opRequests.end() != it)
231 LoggerD(" find Message ");
232 it->second.handle = handle;
237 void MessagingService::setMessageToOpId(int opIdx, IMessagePtr& message)
239 LoggerD("operation index : " << opIdx);
240 OpRequestsIterator it = m_opRequests.find(opIdx);
242 if ( m_opRequests.end() != it)
244 LoggerD("set Message ");
245 it->second.message = message;
249 void MessagingService::setEventToOpId(int opIdx, EventMessagingServicePtr & event)
251 LoggerD("operation index : " << opIdx);
252 OpRequestsIterator it = m_opRequests.find(opIdx);
254 if ( m_opRequests.end() != it)
256 LoggerD("set Message ");
259 LoggerD("vaild event ");
260 it->second.event = event;
265 int MessagingService::getOpTypeFromOpId(int opIdx)
267 LoggerD("operation index : " << opIdx);
271 OpRequestsIterator it = m_opRequests.find(opIdx);
272 if ( m_opRequests.end() != it)
274 type = it->second.type;
277 LoggerD("type " << type);
281 int MessagingService::deleteOpId(int opIdx)
284 OpRequestsIterator it = m_opRequests.find(opIdx);
285 if ( m_opRequests.end() != it)
287 m_opRequests.erase(it); //delete
288 LoggerD("delete Request : " << m_opRequests.size());
293 void MessagingService::cancelOperation(int opId, int handle, int eventType, IMessagePtr& message)
297 if (eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY
298 || eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
300 IEmailPtr email = DPL::DynamicPointerCast<IEmail >(message);
303 if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY )
305 LoggerD("Cancel Download Body , handle = " << handle);
306 email->downloadBodyCancel(handle);
308 else if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
310 LoggerD("Cancel Download Attachment , handle = " << handle);
311 email->downloadAttachmentCancel(handle);
316 ThrowMsg( WrtDeviceApis::Commons::ConversionException, "Conversion IMessage to IEmail error");
320 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC)
324 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
326 syncFolderCancel(handle);
328 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SEND_MESSAGE)
330 message->sendCancel(handle);