Update change log and spec for wrt-plugins-tizen_0.2.73
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Messaging / MessagingService.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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. 
15 */
16
17
18 /*
19  * @author      Wojciech Bielawski (w.bielawski@samsung.com)
20  */
21
22 #include <cassert>
23 #include <Commons/Exception.h>
24 //#include <API/Messaging/EventMessagingService.h>
25 #include <API/Messaging/EmailAccountInfo.h>
26 #include "MessagingService.h"
27 #include "MailSync.h"
28 #include "API/Messaging/EventMessagingService.h"
29
30 #include <email-types.h>
31 #include <email-api.h>
32
33 using namespace DPL;
34 using namespace std;
35 using namespace TizenApis::Api::Messaging;
36
37 namespace TizenApis {
38 namespace Platform {
39 namespace Messaging {
40
41 MessagingService::MessagingService() :
42         m_initialized(false)
43 {
44     LogDebug("Enter");
45 }
46
47 MessagingService::~MessagingService() {
48         LogDebug("Leave");
49
50         m_opRequests.clear();   //clean Messaging Service Handles
51 }
52
53 //public function.
54
55 void MessagingService::initialize() {
56         LogDebug("<<<");
57         m_initialized = true;
58
59         
60         LogDebug(">>>");
61 }
62
63 void MessagingService::setMessagingServiceType(int type)
64 {
65         setType(type);
66 }
67
68 void MessagingService::setMessagingServiceName(const string& name)
69 {
70         setName(name);  
71 }
72
73 void MessagingService::setMessagingServiceId(int serviceId)
74 {
75         setId(serviceId);       
76 }
77
78 int MessagingService::sync(const Api::Messaging::IMessagingServicePtr& messagingService, const int limit)
79 {
80         LogDebug("<<<");
81         
82         return MailSync::getInstance().syncAccount( messagingService, limit );
83 }
84
85 void MessagingService::syncCancel(int handle)
86 {
87         LogDebug("<<<");
88         return MailSync::getInstance().syncAccountCancel(handle);
89 }
90
91 int MessagingService::syncFolder(const Api::Messaging::IMessagingServicePtr& messagingService, const int folder_id, const int limit)
92 {
93         LogDebug("<<<");
94         return MailSync::getInstance().syncFolder( messagingService, folder_id, limit);
95 }
96
97 void MessagingService::syncFolderCancel(int handle)
98 {
99         LogDebug("<<<");
100         return MailSync::getInstance().syncFolderCancel(handle);
101 }
102
103 //operation.
104 int MessagingService::createOpId(int type)
105 {
106
107         int cnt = m_opRequests.size();  //item count.
108         int index = 0;
109
110         if (cnt > 0)
111         {
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;
115
116                 if ( (cnt * MESSAGING_SERVICE_OP_COUNT) >=  lastIndex )
117                 {
118                         index = ( lastIndex + MESSAGING_SERVICE_OP_COUNT - ( lastIndex % MESSAGING_SERVICE_OP_COUNT) ) + type;
119                 }
120                 else
121                 {
122                         int preValue = 0;
123
124                         OpRequestsIterator iter;
125         
126                         for( iter = m_opRequests.begin(); iter != m_opRequests.end(); ++iter)
127                         {
128                                 int currentValue = it->first;
129                                 
130                                 if ( currentValue - preValue > MESSAGING_SERVICE_OP_COUNT)
131                                 {
132                                         index = ( preValue + MESSAGING_SERVICE_OP_COUNT - ( preValue % MESSAGING_SERVICE_OP_COUNT) ) + type;
133                                         break;
134                                 }
135                                 else
136                                 {
137                                         index = ( currentValue + MESSAGING_SERVICE_OP_COUNT - ( currentValue % MESSAGING_SERVICE_OP_COUNT) ) + type;
138                                 }
139                         }
140                         
141                 }
142                 
143         }
144         else
145         {
146                 index = type;
147         }
148                 
149         LogDebug("m_opRequests size : " << cnt );
150         LogDebug("index " << index);
151         m_opRequests.insert(std::make_pair(index, MessagingServiceOpData(0,type)));
152
153         return index;   
154 }
155
156 int MessagingService::getHandleFromOpId(int opIdx)
157 {
158     LogDebug("operation index size : " << opIdx);
159
160     int handle = -1;
161         
162     OpRequestsIterator it = m_opRequests.find(opIdx);
163     if ( m_opRequests.end() != it)
164     {
165                 handle = it->second.handle;
166     }
167
168     LogDebug("index " << handle);
169     return handle;
170
171 }
172
173 Api::Messaging::IMessagePtr MessagingService::getMessageFromOpId(int opIdx)
174 {
175         LogDebug("operation index : " << opIdx);
176         OpRequestsIterator it = m_opRequests.find(opIdx);
177         IMessagePtr msg;
178         if ( m_opRequests.end() != it)
179         {
180                 msg = it->second.message;
181         }
182
183         return msg;
184 }
185
186 Api::Messaging::EventMessagingServicePtr MessagingService::getEventFromOpId(int opIdx)
187 {
188         LogDebug("operation index : " << opIdx);
189         OpRequestsIterator it = m_opRequests.find(opIdx);
190         EventMessagingServicePtr event;
191         if ( m_opRequests.end() != it)
192         {
193                 LogDebug("Find Event");
194                 event = it->second.event;
195         }
196
197         return event;
198 }
199
200 void MessagingService::setHandleToOpId(int opIdx, int handle)
201 {
202         LogDebug("operation index : " << opIdx);
203         OpRequestsIterator it = m_opRequests.find(opIdx);
204
205         if ( m_opRequests.end() != it)
206         {
207                 LogDebug(" find Message ");
208                 it->second.handle = handle;
209         }
210         
211 }
212
213 void MessagingService::setMessageToOpId(int opIdx, Api::Messaging::IMessagePtr& message)
214 {
215         LogDebug("operation index : " << opIdx);
216         OpRequestsIterator it = m_opRequests.find(opIdx);
217
218         if ( m_opRequests.end() != it)
219         {
220                 LogDebug("set Message ");
221                 it->second.message = message;
222         }
223 }
224
225 void MessagingService::setEventToOpId(int opIdx, Api::Messaging::EventMessagingServicePtr & event)
226 {
227         LogDebug("operation index : " << opIdx);
228         OpRequestsIterator it = m_opRequests.find(opIdx);
229
230         if ( m_opRequests.end() != it)
231         {
232                 LogDebug("set Message ");
233                 if (event)
234                 {
235                         LogDebug("vaild event ");
236                         it->second.event = event;
237                 }
238         }
239 }
240
241 int MessagingService::getOpTypeFromOpId(int opIdx)
242 {
243     LogDebug("operation index : " << opIdx);
244
245     int type = 0;
246         
247     OpRequestsIterator it = m_opRequests.find(opIdx);
248     if ( m_opRequests.end() != it)
249     {
250                 type = it->second.type;
251     }
252
253     LogDebug("type " << type);
254     return type;
255 }
256
257 int MessagingService::deleteOpId(int opIdx)
258 {
259         
260         OpRequestsIterator it = m_opRequests.find(opIdx);
261         if ( m_opRequests.end() != it)
262         {
263                 m_opRequests.erase(it); //delete
264                 LogDebug("delete Request : " << m_opRequests.size());
265         }
266         return opIdx;
267 }
268
269 void MessagingService::cancelOperation(int opId, int handle, int eventType, Api::Messaging::IMessagePtr& message)
270 {
271         if (handle > 0)
272         {
273                 if (eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY
274                         || eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
275                 {
276                         Api::Messaging::IEmailPtr email = DPL::DynamicPointerCast<IEmail >(message);
277                         if (email) 
278                         {
279                                 if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY )
280                                 {       
281                                         LogDebug("Cancel Download Body , handle = " << handle);
282                                         email->downloadBodyCancel(handle);
283                                 }
284                                 else if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
285                                 {
286                                         LogDebug("Cancel Download Attachment , handle = " << handle);
287                                         email->downloadAttachmentCancel(handle);
288                                 }                               
289                         }
290                         else
291                         {
292                                 ThrowMsg(  WrtDeviceApis::Commons::ConversionException, "Conversion IMessage to IEmail error");
293                         }
294
295                 }
296                 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC)
297                 {
298                         syncCancel(handle);
299                 }
300                 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
301                 {
302                         syncFolderCancel(handle);
303                 }
304                 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SEND_MESSAGE)
305                 {
306                         message->sendCancel(handle);
307                         deleteOpId(opId);
308                 }
309                 else
310                 {
311
312                 }
313                                                 
314         }
315         
316 }
317
318 }
319 }       //namespace Platform
320 }       //namespace WrtPlugins
321