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