Update change log and spec for wrt-plugins-tizen_0.4.49
[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
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 size : " << 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("operation index size : " << opIdx);
166
167     int handle = -1;
168         
169     OpRequestsIterator it = m_opRequests.find(opIdx);
170     if ( m_opRequests.end() != it)
171     {
172                 handle = it->second.handle;
173     }
174
175     LoggerD("index " << handle);
176     return handle;
177
178 }
179
180 IMessagePtr MessagingService::getMessageFromOpId(int opIdx)
181 {
182         LoggerD("operation index : " << opIdx);
183         OpRequestsIterator it = m_opRequests.find(opIdx);
184         IMessagePtr msg;
185         if ( m_opRequests.end() != it)
186         {
187                 msg = it->second.message;
188         }
189
190         return msg;
191 }
192
193 EventMessagingServicePtr MessagingService::getEventFromOpId(int opIdx)
194 {
195         LoggerD("operation index : " << opIdx);
196         OpRequestsIterator it = m_opRequests.find(opIdx);
197         EventMessagingServicePtr event;
198         if ( m_opRequests.end() != it)
199         {
200                 LoggerD("Find Event");
201                 event = it->second.event;
202         }
203
204         return event;
205 }
206
207 EventMessagingServicePtr MessagingService::getEventFromHandle(int handle)
208 {
209         LoggerD("handle index : " << handle);
210         EventMessagingServicePtr event;
211         int cnt = m_opRequests.size();  //item count.
212         LoggerD("cnt : " << cnt);
213         if (cnt > 0)
214         {
215                 OpRequestsIterator it;
216                 for( it = m_opRequests.begin(); it != m_opRequests.end(); ++it)
217                 {
218                         LoggerD("it->first : " << it->first);
219                         LoggerD("it->second.handle : " << it->second.handle);
220                         if((unsigned int)handle == it->second.handle)
221                         {
222                                 LoggerD("get opId : " << it->first);
223                                 LoggerD("event of handle : " << it->second.handle);
224                                 event = it->second.event;
225                         }
226                 }
227         }
228         return event;
229 }
230
231 void MessagingService::setHandleToOpId(int opIdx, int handle)
232 {
233         LoggerD("operation index : " << opIdx);
234         OpRequestsIterator it = m_opRequests.find(opIdx);
235
236         if ( m_opRequests.end() != it)
237         {
238                 LoggerD(" find Message ");
239                 it->second.handle = handle;
240         }
241         
242 }
243
244 void MessagingService::setMessageToOpId(int opIdx, IMessagePtr& message)
245 {
246         LoggerD("operation index : " << opIdx);
247         OpRequestsIterator it = m_opRequests.find(opIdx);
248
249         if ( m_opRequests.end() != it)
250         {
251                 LoggerD("set Message ");
252                 it->second.message = message;
253         }
254 }
255
256 void MessagingService::setEventToOpId(int opIdx, EventMessagingServicePtr & event)
257 {
258         LoggerD("operation index : " << opIdx);
259         OpRequestsIterator it = m_opRequests.find(opIdx);
260
261         if ( m_opRequests.end() != it)
262         {
263                 LoggerD("set event ");
264                 if (event)
265                 {
266                         LoggerD("vaild event ");
267                         it->second.event = event;
268                 }
269         }
270 }
271
272 int MessagingService::getOpTypeFromOpId(int opIdx)
273 {
274     LoggerD("operation index : " << opIdx);
275
276     int type = 0;
277         
278     OpRequestsIterator it = m_opRequests.find(opIdx);
279     if ( m_opRequests.end() != it)
280     {
281                 type = it->second.type;
282     }
283
284     LoggerD("type " << type);
285     return type;
286 }
287
288 int MessagingService::deleteOpId(int opIdx)
289 {
290         
291         OpRequestsIterator it = m_opRequests.find(opIdx);
292         if ( m_opRequests.end() != it)
293         {
294                 m_opRequests.erase(it); //delete
295                 LoggerD("delete Request : " << m_opRequests.size());
296         }
297         return opIdx;
298 }
299
300 void MessagingService::cancelOperation(int opId, int handle, int eventType, IMessagePtr& message)
301 {
302         if (handle > 0)
303         {
304                 if (eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY
305                         || eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
306                 {
307                         IEmailPtr email = DPL::DynamicPointerCast<IEmail >(message);
308                         if (email) 
309                         {
310                                 if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY )
311                                 {       
312                                         LoggerD("Cancel Download Body , handle = " << handle);
313                                         email->downloadBodyCancel(handle);
314                                 }
315                                 else if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
316                                 {
317                                         LoggerD("Cancel Download Attachment , handle = " << handle);
318                                         email->downloadAttachmentCancel(handle);
319                                 }                               
320                         }
321                         else
322                         {
323                                 ThrowMsg(  WrtDeviceApis::Commons::ConversionException, "Conversion IMessage to IEmail error");
324                         }
325
326                 }
327                 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC)
328                 {
329                         syncCancel(handle);
330                 }
331                 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
332                 {
333                         syncFolderCancel(handle);
334                 }
335                 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SEND_MESSAGE)
336                 {
337                         message->sendCancel(handle);
338                         deleteOpId(opId);
339                 }
340                 else
341                 {
342
343                 }
344                                                 
345         }
346         
347 }
348
349 }
350 }
351