merge with master
[platform/framework/native/messaging.git] / src / FMsg_EmailManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 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  * @file                FMsg_EmailManagerImpl.cpp
19  * @brief               This is the implementation file for the %_EmailManagerImpl class.
20  *
21  * This file contains the implementation of the %_EmailManagerImpl class.
22  */
23
24 #include <msg.h>
25 #include <msg_transport.h>
26 #include <msg_storage.h>
27 #include <dbus/dbus.h>
28 #include <email-api.h>
29 #include <FMsgEmailMessage.h>
30 #include <FMsgEmailManager.h>
31 #include <FMsgRecipientList.h>
32 #include <FMsgIEmailListener.h>
33 #include <FBaseSysLog.h>
34 #include "FMsg_EmailEvent.h"
35 #include "FMsg_EmailEventArg.h"
36 #include "FMsg_EmailManagerImpl.h"
37 #include "FMsg_Types.h"
38 #include "FMsg_MsgUtil.h"
39 #include "FMsg_RecipientListImpl.h"
40
41 using namespace std;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
44
45 namespace Tizen { namespace Messaging
46 {
47
48         ArrayListT<_EmailAsyncData*>* _EmailManagerImpl::__pEmailAsyncData = null;
49         Tizen::Base::Runtime::Mutex * _EmailManagerImpl::__pMutex = null;
50
51 _EmailManagerImpl::_EmailManagerImpl(void)
52         : __pEmailEvent(null)
53         , __isConstructed(false)
54 {
55 }
56
57 _EmailManagerImpl::~_EmailManagerImpl(void)
58 {
59         __isConstructed = false;
60 }
61
62 result
63 _EmailManagerImpl::Construct(IEmailListener& listener)
64 {
65         SysAssertf(__isConstructed == false, "_EmailManagerImpl instance is already constructed.");
66
67         result r = E_SUCCESS;
68
69         // set event
70         unique_ptr<_EmailEvent> pEmailEvent(new (std::nothrow) _EmailEvent());
71         SysTryReturn(NID_MSG, pEmailEvent != null, E_OUT_OF_MEMORY, r = E_OUT_OF_MEMORY, "memory allocation failed");
72
73         // event construction
74         r = pEmailEvent->Construct();
75         SysTryReturn(NID_MSG, r == E_SUCCESS, r, r, "Failed to construct an instance");
76
77         // add listener
78         r = pEmailEvent->AddListener(listener, true);
79         SysTryReturn(NID_MSG, r == E_SUCCESS, r, r, "Failed to add a listener");
80
81         __pEmailEvent = move(pEmailEvent);
82         __isConstructed = true;
83
84         return r;
85 }
86
87 void
88 _EmailManagerImpl::SentEmailStatusCallback(int msgId, email_noti_on_network_event status, int errorCode )
89 {
90         result r = E_SUCCESS;
91
92         switch (status)
93         {
94         case NOTI_SEND_FAIL:
95         {
96                 r = E_FAILURE;
97                 _EmailEvent* pEmailEvent = null;
98                 _EmailAsyncData *pEmailAsyncData = null;
99
100                 for( int i=0; i < _EmailManagerImpl::__pEmailAsyncData->GetCount() ; i++)
101                 {
102                         _EmailManagerImpl::__pMutex->Acquire();
103                         _EmailManagerImpl::__pEmailAsyncData->GetAt(i, pEmailAsyncData);
104                         if ( null == pEmailAsyncData)
105                         {
106                                 _EmailManagerImpl::__pMutex->Release();
107                                 break;
108                         
109                         }
110                         if ( msgId == pEmailAsyncData->msgId )
111                         {
112                                 _EmailManagerImpl::__pEmailAsyncData->RemoveAt(i);
113                         }
114                         _EmailManagerImpl::__pMutex->Release();
115
116
117                         if (msgId == pEmailAsyncData->msgId);
118                         {
119                                 _EmailEventArg* pEmailEventArg = null;
120                                 pEmailEvent = pEmailAsyncData->pEventData;
121
122                                 if (pEmailEvent)
123                                 {
124                                         pEmailEventArg = new (std::nothrow) _EmailEventArg(r);
125                                         if (pEmailEventArg)
126                                         {
127                                                 pEmailEvent->Fire(*pEmailEventArg);
128                                         }
129                                 }
130                                 if (pEmailAsyncData)
131                                 {
132                                         delete []pEmailAsyncData;
133                                         pEmailAsyncData = null;
134                                 
135                                 }
136
137                                 break;
138                         }
139                 }
140
141
142         }
143         break;
144
145         case NOTI_SEND_FINISH:
146         {
147                 r = E_SUCCESS;
148                 _EmailEvent* pEmailEvent = null;
149                 _EmailAsyncData *pEmailAsyncData = null;
150
151                 for( int i=0; i < _EmailManagerImpl::__pEmailAsyncData->GetCount() ; i++)
152                 {
153                         _EmailManagerImpl::__pMutex->Acquire();
154                         _EmailManagerImpl::__pEmailAsyncData->GetAt(i, pEmailAsyncData);
155                         if ( null == pEmailAsyncData)
156                         {
157                                 _EmailManagerImpl::__pMutex->Release();
158                                 break;
159                         
160                         }
161                         if ( msgId == pEmailAsyncData->msgId )
162                         {
163                                 _EmailManagerImpl::__pEmailAsyncData->RemoveAt(i);
164                         }
165                         _EmailManagerImpl::__pMutex->Release();
166
167
168                         if (msgId == pEmailAsyncData->msgId);
169                         {
170                                 _EmailEventArg* pEmailEventArg = null;
171                                 pEmailEvent = pEmailAsyncData->pEventData;
172
173                                 if (pEmailEvent)
174                                 {
175                                         pEmailEventArg = new (std::nothrow) _EmailEventArg(r);
176                                         if (pEmailEventArg)
177                                         {
178                                                 pEmailEvent->Fire(*pEmailEventArg);
179                                         }
180                                 }
181
182                                 if (pEmailAsyncData)
183                                 {
184                                         delete []pEmailAsyncData;
185                                         pEmailAsyncData = null;
186                                 
187                                 }
188
189                                 break;
190                         }
191                 }
192
193
194         }
195         break;
196
197         default:
198                 r = E_FAILURE;
199                 break;
200         }
201 }
202
203
204 result
205 _EmailManagerImpl::Send(const EmailMessage& message, const RecipientList& recipientList, bool saveToSentBox)
206 {
207         result r = E_SUCCESS;
208         int msgId = -1;
209         int requestId = 0;
210         _EmailAsyncData *pTemp=null;
211
212         SysAssertf(__isConstructed == true, "The _EmailManagerImpl instance is not constructed yet.");
213         SysLog(NID_MSG, "sending an email message.");
214
215         r = _MsgUtil::SendEmail(message, recipientList, msgId, requestId);
216         SysTryCatch(NID_MSG, r == E_SUCCESS, , r, "[%s] Failed to send the email message.", GetErrorMessage(r));        
217
218         pTemp = new (std::nothrow) _EmailAsyncData();
219         SysTryCatch(NID_MSG, pTemp != null, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
220
221         pTemp->requestId = requestId;
222         pTemp->msgId = msgId;
223         pTemp->pEventData = __pEmailEvent.get();
224
225         if(null ==__pEmailAsyncData)
226
227                 {
228                 __pEmailAsyncData = new (std::nothrow) ArrayListT<_EmailAsyncData*> ;
229                 SysTryCatch(NID_MSG, __pEmailAsyncData != null, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
230                 __pEmailAsyncData->Construct();
231                                                 
232         }
233
234         __pEmailAsyncData->Add(pTemp);
235
236
237         if ( null == __pMutex)
238                 {
239                 __pMutex = new (std::nothrow) Tizen::Base::Runtime::Mutex();
240                 SysTryCatch(NID_MSG, __pMutex != null, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
241                 __pMutex->Create();
242
243         }
244
245         return r;
246
247 CATCH:
248         
249         if (pTemp)
250         {
251                 if (__pEmailAsyncData->Contains(pTemp))
252                 {
253                         __pEmailAsyncData->Remove(pTemp);
254                         
255                 }
256                 delete pTemp;
257                 pTemp = null;
258
259         }
260
261         return r;
262 }
263
264
265 _EmailManagerImpl*
266 _EmailManagerImpl::GetInstance(EmailManager& emailManager)
267 {
268         return emailManager.__pImpl;
269 }
270
271 const _EmailManagerImpl*
272 _EmailManagerImpl::GetInstance(const EmailManager& emailManager)
273 {
274         return emailManager.__pImpl;
275 }
276
277 } } // Tizen::Messaging