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