merge with master
[framework/osp/messaging.git] / src / FMsgEmailManager.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                FMsgEmailManager.cpp
19  * @brief               This is the implementation file for the %EmailManager class.
20  *
21  * This file contains the implementation of the %EmailManager 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 <FMsgEmailManager.h>
30 #include <FBaseSysLog.h>
31 #include <FSec_AccessController.h>
32 #include "FMsg_EmailManagerImpl.h"
33 #include "FMsg_MsgUtil.h"
34 #include "FMsg_RecipientListImpl.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Messaging;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Security;
40
41 namespace Tizen { namespace Messaging
42 {
43
44 EmailManager::EmailManager(void)
45         : __pImpl(null)
46 {
47 }
48
49 EmailManager::~EmailManager(void)
50 {
51         if (__pImpl)
52         {
53                 delete __pImpl;
54                 __pImpl = null;
55         }
56 }
57
58 result
59 EmailManager::Construct(IEmailListener& listener)
60 {
61         // method return code
62         result r = E_SUCCESS;
63
64         SysAssertf(__pImpl == null, "The EmailManager instance is already constructed.");
65
66         // create an _EmailManagerImpl instance
67         __pImpl = new (std::nothrow) _EmailManagerImpl();
68         SysTryReturnResult(NID_MSG, __pImpl != null, E_OUT_OF_MEMORY, "memory allocation failed.");
69
70         r = __pImpl->Construct(listener);
71         if (IsFailed(r))
72         {
73                 SysLogException(NID_MSG, r, "[%s] Failed to Construct an instance.", GetErrorMessage(r));
74                 goto CATCH;
75         }
76
77         return r;
78
79 CATCH:
80         return r;
81 }
82
83 result
84 EmailManager::Send(const EmailMessage& message, const RecipientList& recipientList, bool saveToSentBox)
85 {
86         result r = E_SUCCESS;
87
88         // checking privilege
89         r = _AccessController::CheckUserPrivilege(_PRV_MESSAGING_EMAIL);
90         SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagating.");
91
92         SysAssertf(__pImpl != null, "The EmailManager instance is not constructed yet.");
93         SysTryReturnResult(NID_MSG,
94                 MAX_EMAIL_RECIPIENT_COUNT >= _RecipientListImpl::GetInstance(recipientList)->GetTotalRecipientCount(), E_MAX_EXCEEDED,
95                 "[E_MAX_EXCEEDED] The number of recipients (%d) exceeds the limit (%d).",
96                 _RecipientListImpl::GetInstance(recipientList)->GetTotalRecipientCount(), MAX_EMAIL_RECIPIENT_COUNT);
97         SysTryReturnResult(NID_MSG,
98                 0 != _RecipientListImpl::GetInstance(recipientList)->GetTotalRecipientCount(), E_INVALID_ARG,
99                 "[E_INVALID_ARG] The number of recipients is 0.");
100
101         // email address validity check
102         r = _MsgUtil::CheckEmailAddressValidity(recipientList);
103         if (IsFailed(r))
104         {
105                 SysLogException(NID_MSG, r, "[%s] There is an invalid email address in the recipient list.", GetErrorMessage(r));
106                 goto CATCH;
107         }
108
109         // send an email message
110         r = __pImpl->Send(message, recipientList, saveToSentBox);
111         if (IsFailed(r))
112         {
113                 SysLogException(NID_MSG, r, "[%s] Failed to send the email message.", GetErrorMessage(r));
114                 goto CATCH;
115         }
116
117         return E_SUCCESS;
118
119 CATCH:
120         return r;
121 }
122
123 } } // Tizen::Messaging