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