56df77b09f95fca7b024e78edfc9e8c1afca1c47
[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 void MessagingService::setHandleToOpId(int opIdx, int handle)
200 {
201         LogDebug("operation index : " << opIdx);
202         OpRequestsIterator it = m_opRequests.find(opIdx);
203
204         if ( m_opRequests.end() != it)
205         {
206                 LogDebug(" find Message ");
207                 it->second.handle = handle;
208         }
209         
210 }
211
212 void MessagingService::setMessageToOpId(int opIdx, IMessagePtr& message)
213 {
214         LogDebug("operation index : " << opIdx);
215         OpRequestsIterator it = m_opRequests.find(opIdx);
216
217         if ( m_opRequests.end() != it)
218         {
219                 LogDebug("set Message ");
220                 it->second.message = message;
221         }
222 }
223
224 void MessagingService::setEventToOpId(int opIdx, EventMessagingServicePtr & event)
225 {
226         LogDebug("operation index : " << opIdx);
227         OpRequestsIterator it = m_opRequests.find(opIdx);
228
229         if ( m_opRequests.end() != it)
230         {
231                 LogDebug("set Message ");
232                 if (event)
233                 {
234                         LogDebug("vaild event ");
235                         it->second.event = event;
236                 }
237         }
238 }
239
240 int MessagingService::getOpTypeFromOpId(int opIdx)
241 {
242     LogDebug("operation index : " << opIdx);
243
244     int type = 0;
245         
246     OpRequestsIterator it = m_opRequests.find(opIdx);
247     if ( m_opRequests.end() != it)
248     {
249                 type = it->second.type;
250     }
251
252     LogDebug("type " << type);
253     return type;
254 }
255
256 int MessagingService::deleteOpId(int opIdx)
257 {
258         
259         OpRequestsIterator it = m_opRequests.find(opIdx);
260         if ( m_opRequests.end() != it)
261         {
262                 m_opRequests.erase(it); //delete
263                 LogDebug("delete Request : " << m_opRequests.size());
264         }
265         return opIdx;
266 }
267
268 void MessagingService::cancelOperation(int opId, int handle, int eventType, IMessagePtr& message)
269 {
270         if (handle > 0)
271         {
272                 if (eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY
273                         || eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
274                 {
275                         IEmailPtr email = DPL::DynamicPointerCast<IEmail >(message);
276                         if (email) 
277                         {
278                                 if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY )
279                                 {       
280                                         LogDebug("Cancel Download Body , handle = " << handle);
281                                         email->downloadBodyCancel(handle);
282                                 }
283                                 else if ( eventType == MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT)
284                                 {
285                                         LogDebug("Cancel Download Attachment , handle = " << handle);
286                                         email->downloadAttachmentCancel(handle);
287                                 }                               
288                         }
289                         else
290                         {
291                                 ThrowMsg(  WrtDeviceApis::Commons::ConversionException, "Conversion IMessage to IEmail error");
292                         }
293
294                 }
295                 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC)
296                 {
297                         syncCancel(handle);
298                 }
299                 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER)
300                 {
301                         syncFolderCancel(handle);
302                 }
303                 else if (eventType == MESSAGING_SERVICE_EVENT_TYPE_SEND_MESSAGE)
304                 {
305                         message->sendCancel(handle);
306                         deleteOpId(opId);
307                 }
308                 else
309                 {
310
311                 }
312                                                 
313         }
314         
315 }
316
317 }
318 }
319