1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "SmsMessage.h"
7 #include "nsIDOMClassInfo.h"
8 #include "jsapi.h" // For OBJECT_TO_JSVAL and JS_NewDateObjectMsec
9 #include "jsfriendapi.h" // For js_DateGetMsecSinceEpoch
10 #include "mozilla/dom/mobilemessage/Constants.h" // For MessageType
12 using namespace mozilla::dom::mobilemessage
;
17 NS_INTERFACE_MAP_BEGIN(SmsMessage
)
18 NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsMessage
)
19 NS_INTERFACE_MAP_ENTRY(nsISupports
)
20 NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsMessage
)
23 NS_IMPL_ADDREF(SmsMessage
)
24 NS_IMPL_RELEASE(SmsMessage
)
26 SmsMessage::SmsMessage(int32_t aId
,
28 const nsString
& aIccId
,
29 DeliveryState aDelivery
,
30 DeliveryStatus aDeliveryStatus
,
31 const nsString
& aSender
,
32 const nsString
& aReceiver
,
33 const nsString
& aBody
,
34 MessageClass aMessageClass
,
36 uint64_t aSentTimestamp
,
37 uint64_t aDeliveryTimestamp
,
39 : mData(aId
, aThreadId
, aIccId
, aDelivery
, aDeliveryStatus
,
40 aSender
, aReceiver
, aBody
, aMessageClass
, aTimestamp
, aSentTimestamp
,
41 aDeliveryTimestamp
, aRead
)
45 SmsMessage::SmsMessage(const SmsMessageData
& aData
)
51 SmsMessage::Create(int32_t aId
,
53 const nsAString
& aIccId
,
54 const nsAString
& aDelivery
,
55 const nsAString
& aDeliveryStatus
,
56 const nsAString
& aSender
,
57 const nsAString
& aReceiver
,
58 const nsAString
& aBody
,
59 const nsAString
& aMessageClass
,
61 uint64_t aSentTimestamp
,
62 uint64_t aDeliveryTimestamp
,
65 nsIDOMMozSmsMessage
** aMessage
)
69 // SmsMessageData exposes these as references, so we can simply assign
73 data
.threadId() = aThreadId
;
74 data
.iccId() = nsString(aIccId
);
75 data
.sender() = nsString(aSender
);
76 data
.receiver() = nsString(aReceiver
);
77 data
.body() = nsString(aBody
);
80 if (aDelivery
.Equals(DELIVERY_RECEIVED
)) {
81 data
.delivery() = eDeliveryState_Received
;
82 } else if (aDelivery
.Equals(DELIVERY_SENDING
)) {
83 data
.delivery() = eDeliveryState_Sending
;
84 } else if (aDelivery
.Equals(DELIVERY_SENT
)) {
85 data
.delivery() = eDeliveryState_Sent
;
86 } else if (aDelivery
.Equals(DELIVERY_ERROR
)) {
87 data
.delivery() = eDeliveryState_Error
;
89 return NS_ERROR_INVALID_ARG
;
92 if (aDeliveryStatus
.Equals(DELIVERY_STATUS_NOT_APPLICABLE
)) {
93 data
.deliveryStatus() = eDeliveryStatus_NotApplicable
;
94 } else if (aDeliveryStatus
.Equals(DELIVERY_STATUS_SUCCESS
)) {
95 data
.deliveryStatus() = eDeliveryStatus_Success
;
96 } else if (aDeliveryStatus
.Equals(DELIVERY_STATUS_PENDING
)) {
97 data
.deliveryStatus() = eDeliveryStatus_Pending
;
98 } else if (aDeliveryStatus
.Equals(DELIVERY_STATUS_ERROR
)) {
99 data
.deliveryStatus() = eDeliveryStatus_Error
;
101 return NS_ERROR_INVALID_ARG
;
104 if (aMessageClass
.Equals(MESSAGE_CLASS_NORMAL
)) {
105 data
.messageClass() = eMessageClass_Normal
;
106 } else if (aMessageClass
.Equals(MESSAGE_CLASS_CLASS_0
)) {
107 data
.messageClass() = eMessageClass_Class0
;
108 } else if (aMessageClass
.Equals(MESSAGE_CLASS_CLASS_1
)) {
109 data
.messageClass() = eMessageClass_Class1
;
110 } else if (aMessageClass
.Equals(MESSAGE_CLASS_CLASS_2
)) {
111 data
.messageClass() = eMessageClass_Class2
;
112 } else if (aMessageClass
.Equals(MESSAGE_CLASS_CLASS_3
)) {
113 data
.messageClass() = eMessageClass_Class3
;
115 return NS_ERROR_INVALID_ARG
;
119 data
.timestamp() = aTimestamp
;
121 // Set |sentTimestamp|.
122 data
.sentTimestamp() = aSentTimestamp
;
124 // Set |deliveryTimestamp|.
125 data
.deliveryTimestamp() = aDeliveryTimestamp
;
127 nsCOMPtr
<nsIDOMMozSmsMessage
> message
= new SmsMessage(data
);
128 message
.swap(*aMessage
);
132 const SmsMessageData
&
133 SmsMessage::GetData() const
139 SmsMessage::GetType(nsAString
& aType
)
141 aType
= NS_LITERAL_STRING("sms");
146 SmsMessage::GetId(int32_t* aId
)
153 SmsMessage::GetThreadId(uint64_t* aThreadId
)
155 *aThreadId
= mData
.threadId();
160 SmsMessage::GetIccId(nsAString
& aIccId
)
162 aIccId
= mData
.iccId();
167 SmsMessage::GetDelivery(nsAString
& aDelivery
)
169 switch (mData
.delivery()) {
170 case eDeliveryState_Received
:
171 aDelivery
= DELIVERY_RECEIVED
;
173 case eDeliveryState_Sending
:
174 aDelivery
= DELIVERY_SENDING
;
176 case eDeliveryState_Sent
:
177 aDelivery
= DELIVERY_SENT
;
179 case eDeliveryState_Error
:
180 aDelivery
= DELIVERY_ERROR
;
182 case eDeliveryState_Unknown
:
183 case eDeliveryState_EndGuard
:
185 MOZ_CRASH("We shouldn't get any other delivery state!");
192 SmsMessage::GetDeliveryStatus(nsAString
& aDeliveryStatus
)
194 switch (mData
.deliveryStatus()) {
195 case eDeliveryStatus_NotApplicable
:
196 aDeliveryStatus
= DELIVERY_STATUS_NOT_APPLICABLE
;
198 case eDeliveryStatus_Success
:
199 aDeliveryStatus
= DELIVERY_STATUS_SUCCESS
;
201 case eDeliveryStatus_Pending
:
202 aDeliveryStatus
= DELIVERY_STATUS_PENDING
;
204 case eDeliveryStatus_Error
:
205 aDeliveryStatus
= DELIVERY_STATUS_ERROR
;
207 case eDeliveryStatus_EndGuard
:
209 MOZ_CRASH("We shouldn't get any other delivery status!");
216 SmsMessage::GetSender(nsAString
& aSender
)
218 aSender
= mData
.sender();
223 SmsMessage::GetReceiver(nsAString
& aReceiver
)
225 aReceiver
= mData
.receiver();
230 SmsMessage::GetBody(nsAString
& aBody
)
232 aBody
= mData
.body();
237 SmsMessage::GetMessageClass(nsAString
& aMessageClass
)
239 switch (mData
.messageClass()) {
240 case eMessageClass_Normal
:
241 aMessageClass
= MESSAGE_CLASS_NORMAL
;
243 case eMessageClass_Class0
:
244 aMessageClass
= MESSAGE_CLASS_CLASS_0
;
246 case eMessageClass_Class1
:
247 aMessageClass
= MESSAGE_CLASS_CLASS_1
;
249 case eMessageClass_Class2
:
250 aMessageClass
= MESSAGE_CLASS_CLASS_2
;
252 case eMessageClass_Class3
:
253 aMessageClass
= MESSAGE_CLASS_CLASS_3
;
256 MOZ_CRASH("We shouldn't get any other message class!");
263 SmsMessage::GetTimestamp(DOMTimeStamp
* aTimestamp
)
265 *aTimestamp
= mData
.timestamp();
270 SmsMessage::GetSentTimestamp(DOMTimeStamp
* aSentTimestamp
)
272 *aSentTimestamp
= mData
.sentTimestamp();
277 SmsMessage::GetDeliveryTimestamp(DOMTimeStamp
* aDate
)
279 *aDate
= mData
.deliveryTimestamp();
284 SmsMessage::GetRead(bool* aRead
)
286 *aRead
= mData
.read();
291 } // namespace mozilla