2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FMsgSmsManager.cpp
19 * @brief This is the implementation file for the %SmsManager class.
21 * This file contains the implementation of the %SmsManager class.
25 #include <msg_transport.h>
26 #include <msg_storage.h>
27 #include <dbus/dbus.h>
28 #include <email-api.h>
29 #include <FMsgSmsManager.h>
30 #include <FMsgSmsMessage.h>
31 #include <FBaseSysLog.h>
32 #include <FSec_AccessController.h>
33 #include "FMsg_SmsManagerImpl.h"
34 #include "FMsg_Types.h"
35 #include "FMsg_SmsEvent.h"
36 #include "FMsg_MsgUtil.h"
37 #include "FMsg_RecipientListImpl.h"
39 using namespace Tizen::Base;
40 using namespace Tizen::Messaging;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Security;
44 namespace Tizen { namespace Messaging
47 SmsManager::SmsManager(void)
52 SmsManager::~SmsManager(void)
62 SmsManager::Construct(ISmsListener& listener)
67 SysAssertf(__pImpl == null, "The SmsManager instance is already constructed.");
69 // create an _SmsManagerImpl instance
70 __pImpl = new (std::nothrow) _SmsManagerImpl();
71 SysTryReturnResult(NID_MSG, __pImpl != null, E_OUT_OF_MEMORY, "memory allocation failed.");
73 r = __pImpl->Construct(listener);
74 SysTryCatch(NID_MSG, r == E_SUCCESS, , r, "[%s] Failed to construct an instance.", GetErrorMessage(r));
83 SmsManager::AddSmsEventListener(int port, ISmsEventListener& eventListener)
88 r = _AccessController::CheckUserPrivilege(_PRV_SMSTRIGGER);
89 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
91 // checking conditions
92 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
95 SysTryReturn(NID_MSG, (port >= 1 && port <= 9999), E_INVALID_ARG, r = E_INVALID_ARG,
96 "[E_INVALID_ARG] The port number should range from 1 to 9999 (1 <= port <= 9999).");
98 r = __pImpl->AddSmsEventListener(port, eventListener);
101 SysLogException(NID_MSG, r, "[%s] Failed to register the event listener with port [%d].", GetErrorMessage(r), port);
112 SmsManager::RemoveSmsEventListener(int port, ISmsEventListener& eventListener)
114 result r = E_SUCCESS;
116 // checking privilege
117 r = _AccessController::CheckUserPrivilege(_PRV_SMSTRIGGER);
118 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagating.");
120 // checking conditions
121 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
124 SysTryReturn(NID_MSG, (port >= 1 && port <= 9999), E_INVALID_ARG, r = E_INVALID_ARG,
125 "[E_INVALID_ARG] The port number should range from 1 to 9999 (1 <= port <= 9999).");
127 r = __pImpl->RemoveSmsEventListener(port, eventListener);
130 SysLogException(NID_MSG, r, "[%s] Failed to unregister the event listener.", GetErrorMessage(r));
141 SmsManager::AddSmsMessageEventListener(ISmsMessageEventListener& eventListener)
143 result r = E_SUCCESS;
145 // checking privilege
146 r = _AccessController::CheckUserPrivilege(_PRV_MESSAGING_SMS);
147 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
149 // checking conditions
150 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
153 r = __pImpl->AddSmsMessageEventListener(eventListener);
156 SysLogException(NID_MSG, r, "[%s] Failed to add a listener.", GetErrorMessage(r));
168 SmsManager::RemoveSmsMessageEventListener(ISmsMessageEventListener& eventListener)
170 result r = E_SUCCESS;
172 // checking privilege
173 r = _AccessController::CheckUserPrivilege(_PRV_MESSAGING_SMS);
174 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
176 // checking conditions
177 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
179 r = __pImpl->RemoveSmsMessageEventListener(eventListener);
182 SysLogException(NID_MSG, r, "[%s] Failed to remove a listener.", GetErrorMessage(r));
193 SmsManager::GetTotalMessageCount(SmsMessageBoxType type) const
195 result r = E_SUCCESS;
199 // checking privilege
200 r = _AccessController::CheckUserPrivilege(_PRV_MESSAGING_SMS);
203 SetLastResult(E_PRIVILEGE_DENIED);
204 SysLogException(NID_MSG, GetLastResult(), "[E_PRIVILEGE_DENIED] Propagated.");
208 // checking condition
209 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
212 if (type <= SMS_MESSAGE_BOX_TYPE_NONE || type > SMS_MESSAGE_BOX_TYPE_ALL)
214 SetLastResult(E_INVALID_ARG);
215 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] Invalid SmsMessageBoxType argument.");
218 if (type > SMS_MESSAGE_BOX_TYPE_OUTBOX && type < SMS_MESSAGE_BOX_TYPE_ALL)
220 SetLastResult(E_INVALID_ARG);
221 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] Invalid SmsMessageBoxType argument.");
225 count = __pImpl->GetTotalMessageCount(type);
229 SysLogException(NID_MSG, r, "[%s] exception occurred. Failed to get SMS total count. type:[%d].", GetErrorMessage(r), (int) type);
237 Tizen::Base::Collection::IList*
238 SmsManager::SearchInboxN(const Tizen::Base::String* pKeyword, const Tizen::Base::String* pSenderAddress, int startIndex, int count,
239 int& totalResultCount) const
242 result r = E_SUCCESS;
246 // checking privilege
247 r = _AccessController::CheckUserPrivilege(_PRV_MESSAGING_SMS);
250 SetLastResult(E_PRIVILEGE_DENIED);
251 SysLogException(NID_MSG, GetLastResult(), "[E_PRIVILEGE_DENIED] Propagated.");
256 // checking condition
257 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
259 // checking arguments
262 if (pKeyword->GetLength() == MIN_KEYWORD_LENGTH || pKeyword->GetLength() > MAX_KEYWORD_LENGTH)
264 SetLastResult(E_INVALID_ARG);
265 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] The specified keyword string length [%d] is smaller than 2 or greater than 30.",
266 pKeyword->GetLength());
273 if (!pSenderAddress->IsEmpty())
275 if (pSenderAddress->GetLength() < MIN_LENGTH_PHONE || pSenderAddress->GetLength() > MAX_LENGTH_PHONE)
277 SetLastResult(E_INVALID_ARG);
278 SysLogException(NID_MSG, GetLastResult(),
279 "[E_INVALID_ARG] The length of the given sender address should be greater than 3 and less than 41.");
282 else if (_MsgUtil::IsValidPhoneNumber(*pSenderAddress) != true)
284 SetLastResult(E_INVALID_ARG);
285 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] The sender address may contain non-digit characters.");
293 SetLastResult(E_INVALID_ARG);
294 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] The start index [%d] should be equal to or greater than 0.", startIndex);
298 if (count < 0 || count > 20)
300 SetLastResult(E_INVALID_ARG);
301 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] The count value [%d] is too small (< 0) or too big (> 20).", count);
308 SetLastResult(E_SUCCESS);
309 totalResultCount = 0;
314 pList = __pImpl->SearchInboxN(pKeyword, pSenderAddress, startIndex, count, totalResultCount);
316 if (IsFailed(r) || !pList)
319 SysLogException(NID_MSG, r, "[%s] Failed to search.", GetErrorMessage(r));
328 if (pList->GetCount() > 0)
330 pList->RemoveAll(true);
338 Tizen::Base::Collection::IList*
339 SmsManager::SearchMessageBoxN(SmsMessageBoxType type, const Tizen::Base::String* pKeyword, int startIndex, int count,
340 int& totalResultCount) const
343 result r = E_SUCCESS;
347 // checking privilege
348 r = _AccessController::CheckUserPrivilege(_PRV_MESSAGING_SMS);
351 SetLastResult(E_PRIVILEGE_DENIED);
352 SysLogException(NID_MSG, GetLastResult(), "[E_PRIVILEGE_DENIED] Propagated.");
357 // checking condition
358 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
360 // checking sms message box type argument
361 if (type <= SMS_MESSAGE_BOX_TYPE_NONE || type > SMS_MESSAGE_BOX_TYPE_ALL)
363 SetLastResult(E_INVALID_ARG);
364 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] The SmsMessageBoxType value [%d] is invalid.", (int) type);
368 if (type > SMS_MESSAGE_BOX_TYPE_OUTBOX && type < SMS_MESSAGE_BOX_TYPE_ALL)
370 SetLastResult(E_INVALID_ARG);
371 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] The SmsMessageBoxType value [%d] is invalid.", (int) type);
375 // checking keyword arguments
378 if (pKeyword->GetLength() == MIN_KEYWORD_LENGTH || pKeyword->GetLength() > MAX_KEYWORD_LENGTH)
380 SetLastResult(E_INVALID_ARG);
381 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] The specified keyword string length [%d] is smaller than 2 or greater than 30.",
382 pKeyword->GetLength());
387 // checking start index argument
390 SetLastResult(E_INVALID_ARG);
391 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] The start index [%d] should be equal to or greater than 0.", startIndex);
395 // checking count argument
396 if (count < 0 || count > 20)
398 SetLastResult(E_INVALID_ARG);
399 SysLogException(NID_MSG, GetLastResult(), "[E_INVALID_ARG] The count value [%d] is too small (< 0) or too big (> 20).", count);
405 SetLastResult(E_SUCCESS);
406 totalResultCount = 0;
411 pList = __pImpl->SearchMessageBoxN(type, pKeyword, startIndex, count, totalResultCount);
413 if (IsFailed(r) || !pList)
416 SysLogException(NID_MSG, r, "[%s] Failed to search.", GetErrorMessage(r));
425 if (pList->GetCount() > 0)
427 pList->RemoveAll(true);
436 SmsManager::GetFullText(int messageId) const
438 result r = E_SUCCESS;
443 // checking privilege
444 r = _AccessController::CheckUserPrivilege(_PRV_MESSAGING_SMS);
447 SetLastResult(E_PRIVILEGE_DENIED);
448 SysLogException(NID_MSG, GetLastResult(), "[E_PRIVILEGE_DENIED] Propagated.");
452 // checking condition
453 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
458 SetLastResult(E_INVALID_ARG);
459 SysLogException(NID_MSG, GetLastResult(), "[%s] exception occurred. The value [%d] of message ID is invalid.", GetErrorMessage(r), messageId);
463 fullText = __pImpl->GetFullText(messageId);
468 SysLogException(NID_MSG, r, "[%s] exception occurred. Failed to get SMS message using message ID [%d].", GetErrorMessage(
473 SysLog(NID_MSG, "Returned Full Text : [%ls]", fullText.GetPointer());
482 SmsManager::Send(const SmsMessage& message, const RecipientList& recipientList, bool saveToSentBox)
484 result r = E_SUCCESS;
486 // checking privilege
487 r = _AccessController::CheckUserPrivilege(_PRV_MESSAGING_SMS);
488 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
490 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
491 SysTryReturnResult(NID_MSG,
492 MAX_SMS_RECIPIENT_COUNT >= _RecipientListImpl::GetInstance(recipientList)->GetTotalRecipientCount(), E_MAX_EXCEEDED,
493 "[E_MAX_EXCEEDED] The number of recipients exceeds the limit (%d).", MAX_SMS_RECIPIENT_COUNT);
494 SysTryReturnResult(NID_MSG,
495 0 < _RecipientListImpl::GetInstance(recipientList)->GetTotalRecipientCount(), E_INVALID_ARG, "[E_INVALID_ARG] The number of recipients is 0.");
497 // check the validity of the phone number in recipient list
498 r = _MsgUtil::CheckPhoneNumberValidity(_MSG_SMS, recipientList);
501 SysLogException(NID_MSG, r, "[%s] There is an invalid phone number in the recipient list.", GetErrorMessage(r));
505 // send an SMS message
506 r = __pImpl->Send(message, recipientList, saveToSentBox);
509 SysLogException(NID_MSG, r, "[%s] Failed to send the SMS message.", GetErrorMessage(r));
520 SmsManager::SetCbsMessageEventListener(ICbsMessageEventListener* pListener)
522 result r = E_SUCCESS;
526 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
528 // checking privilege
529 r = _AccessController::CheckUserPrivilege(_PRV_CELLBROADCAST);
530 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
533 r = __pImpl->SetCbsMessageEventListener(pListener);
539 SmsManager::SetEtwsPrimaryNotificationEventListener(IEtwsPrimaryNotificationEventListener* pListener)
541 result r = E_SUCCESS;
545 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
547 // checking privilege
548 r = _AccessController::CheckUserPrivilege(_PRV_CELLBROADCAST);
549 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
552 r = __pImpl->SetEtwsPrimaryNotificationEventListener(pListener);
558 SmsManager::SetSavingToCbsBoxEnabled(bool enable)
560 result r = E_SUCCESS;
564 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
566 // checking privilege
567 r = _AccessController::CheckUserPrivilege(_PRV_CELLBROADCAST);
568 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
570 r = __pImpl->SetSavingToCbsBoxEnabled(enable);
576 SmsManager::IsCbsEnabled(void) const
578 result r = E_SUCCESS;
579 bool isCbsEnabled = false;
583 // checking conditions
584 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
586 // checking privilege
587 r = _AccessController::CheckUserPrivilege(_PRV_CELLBROADCAST);
588 SysTryCatch(NID_MSG, r == E_SUCCESS, r = E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
590 isCbsEnabled = __pImpl->IsCbsEnabled();
603 SmsManager::SetCbsEnabled(bool enable)
605 result r = E_SUCCESS;
609 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
611 // checking privilege
612 r = _AccessController::CheckUserPrivilege(_PRV_CELLBROADCAST);
613 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
615 r = __pImpl->SetCbsEnabled(enable);
621 SmsManager::AddCbsChannel(int from, int to, Tizen::Base::String& name, bool activate)
623 result r = E_SUCCESS;
627 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
629 // checking privilege
630 r = _AccessController::CheckUserPrivilege(_PRV_CELLBROADCAST);
631 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
634 SysTryReturn(NID_MSG,
635 name.GetLength() <= MAX_CBS_CHANNEL_NAME_LENGTH, E_INVALID_ARG, r = E_INVALID_ARG, "[E_INVALID_ARG] invalid channel name.");
636 SysTryReturn(NID_MSG,
637 ((to >= 0) && (from >= 0) && (to >= from) && (to <= MAX_CBS_CHANNEL_LIMIT) && ((to - from) <= MAX_CBS_CHANNEL_RANGE)),
638 E_INVALID_ARG, r = E_INVALID_ARG, "[E_INVALID_ARG] Failed to add construct channel, invalid arguments.");
640 r = __pImpl->AddCbsChannel(from, to, name, activate);
646 SmsManager::RemoveCbsChannel(int from, int to)
648 result r = E_SUCCESS;
652 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
654 // checking privilege
655 r = _AccessController::CheckUserPrivilege(_PRV_CELLBROADCAST);
656 SysTryReturn(NID_MSG, r == E_SUCCESS, E_PRIVILEGE_DENIED, r = E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] Propagated.");
658 SysTryReturn(NID_MSG,
659 ((to >= 0) && (from >= 0) && (to >= from) && (to <= MAX_CBS_CHANNEL_LIMIT) && ((to - from) <= MAX_CBS_CHANNEL_RANGE)),
660 E_INVALID_ARG, r = E_INVALID_ARG, "[E_INVALID_ARG] Failed to add construct channel, invalid arguments.");
662 r = __pImpl->RemoveCbsChannel(from, to);
668 SmsManager::GetCbsChannelN(int from, int to) const
670 result r = E_SUCCESS;
671 CbsChannel* pCbsChannel = null;
675 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
677 // checking privilege
678 r = _AccessController::CheckUserPrivilege(_PRV_CELLBROADCAST);
679 SysTryCatch(NID_MSG, r == E_SUCCESS, r = E_PRIVILEGE_DENIED, r, "[E_PRIVILEGE_DENIED] Propagated.");
682 ((to >= 0) && (from >= 0) && (to >= from) && (to <= MAX_CBS_CHANNEL_LIMIT) && ((to - from) <= MAX_CBS_CHANNEL_RANGE)),
683 r = E_INVALID_ARG, r, "[E_INVALID_ARG] Failed to add construct channel, invalid arguments.");
685 pCbsChannel = __pImpl->GetCbsChannelN(from, to);
690 SysTryCatch(NID_MSG, r == E_SUCCESS, , r, "[%s] Failed to get Cbs channel", GetErrorMessage(r));
705 Tizen::Base::Collection::IList*
706 SmsManager::GetCbsChannelListN(void)
708 result r = E_SUCCESS;
713 SysAssertf(__pImpl != null, "The SmsManager instance is not constructed yet.");
715 // checking privilege
716 r = _AccessController::CheckUserPrivilege(_PRV_CELLBROADCAST);
717 SysTryCatch(NID_MSG, r == E_SUCCESS, r = E_PRIVILEGE_DENIED, r, "[E_PRIVILEGE_DENIED] Propagated.");
719 pList = __pImpl->GetCbsChannelListN();
724 SysTryCatch(NID_MSG, r == E_SUCCESS, , r, "[%s] Failed to get Cbs channel list", GetErrorMessage(r));
732 if (pList->GetCount() > 0)
734 pList->RemoveAll(true);
742 } } // Tizen::Messaging