2011-11-22 01:34:21 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-17 12:42:38 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-22 01:34:21 -08:00
|
|
|
|
|
|
|
#include "SmsMessage.h"
|
|
|
|
#include "nsIDOMClassInfo.h"
|
|
|
|
#include "jsapi.h" // For OBJECT_TO_JSVAL and JS_NewDateObjectMsec
|
2012-01-15 00:13:07 -08:00
|
|
|
#include "jsfriendapi.h" // For js_DateGetMsecSinceEpoch
|
2011-11-24 03:45:55 -08:00
|
|
|
#include "Constants.h"
|
2011-11-22 01:34:21 -08:00
|
|
|
|
|
|
|
DOMCI_DATA(MozSmsMessage, mozilla::dom::sms::SmsMessage)
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace sms {
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(SmsMessage)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsMessage)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsMessage)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(SmsMessage)
|
|
|
|
NS_IMPL_RELEASE(SmsMessage)
|
|
|
|
|
2012-01-10 11:34:50 -08:00
|
|
|
SmsMessage::SmsMessage(PRInt32 aId, DeliveryState aDelivery,
|
|
|
|
const nsString& aSender, const nsString& aReceiver,
|
2012-05-17 12:42:38 -07:00
|
|
|
const nsString& aBody, PRUint64 aTimestamp, bool aRead)
|
|
|
|
: mData(aId, aDelivery, aSender, aReceiver, aBody, aTimestamp, aRead)
|
2012-01-10 11:34:50 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-24 03:45:55 -08:00
|
|
|
SmsMessage::SmsMessage(const SmsMessageData& aData)
|
|
|
|
: mData(aData)
|
2011-11-22 01:34:21 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-12-23 21:02:52 -08:00
|
|
|
/* static */ nsresult
|
|
|
|
SmsMessage::Create(PRInt32 aId,
|
|
|
|
const nsAString& aDelivery,
|
|
|
|
const nsAString& aSender,
|
|
|
|
const nsAString& aReceiver,
|
|
|
|
const nsAString& aBody,
|
|
|
|
const jsval& aTimestamp,
|
2012-05-17 12:42:38 -07:00
|
|
|
const bool aRead,
|
2011-12-23 21:02:52 -08:00
|
|
|
JSContext* aCx,
|
|
|
|
nsIDOMMozSmsMessage** aMessage)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aMessage = nullptr;
|
2011-12-23 21:02:52 -08:00
|
|
|
|
|
|
|
// SmsMessageData exposes these as references, so we can simply assign
|
|
|
|
// to them.
|
|
|
|
SmsMessageData data;
|
|
|
|
data.id() = aId;
|
|
|
|
data.sender() = nsString(aSender);
|
|
|
|
data.receiver() = nsString(aReceiver);
|
|
|
|
data.body() = nsString(aBody);
|
2012-05-17 12:42:38 -07:00
|
|
|
data.read() = aRead;
|
2011-12-23 21:02:52 -08:00
|
|
|
|
|
|
|
if (aDelivery.Equals(DELIVERY_RECEIVED)) {
|
|
|
|
data.delivery() = eDeliveryState_Received;
|
|
|
|
} else if (aDelivery.Equals(DELIVERY_SENT)) {
|
|
|
|
data.delivery() = eDeliveryState_Sent;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We support both a Date object and a millisecond timestamp as a number.
|
|
|
|
if (aTimestamp.isObject()) {
|
|
|
|
JSObject& obj = aTimestamp.toObject();
|
|
|
|
if (!JS_ObjectIsDate(aCx, &obj)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
data.timestamp() = js_DateGetMsecSinceEpoch(aCx, &obj);
|
|
|
|
} else {
|
|
|
|
if (!aTimestamp.isNumber()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2012-02-24 14:19:52 -08:00
|
|
|
double number = aTimestamp.toNumber();
|
2011-12-23 21:02:52 -08:00
|
|
|
if (static_cast<PRUint64>(number) != number) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
data.timestamp() = static_cast<PRUint64>(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMMozSmsMessage> message = new SmsMessage(data);
|
|
|
|
message.swap(*aMessage);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-24 03:45:55 -08:00
|
|
|
const SmsMessageData&
|
|
|
|
SmsMessage::GetData() const
|
|
|
|
{
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
2011-11-22 01:34:21 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetId(PRInt32* aId)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
*aId = mData.id();
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetDelivery(nsAString& aDelivery)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
switch (mData.delivery()) {
|
2011-11-22 01:34:21 -08:00
|
|
|
case eDeliveryState_Received:
|
2012-01-17 10:48:58 -08:00
|
|
|
aDelivery = DELIVERY_RECEIVED;
|
2011-11-22 01:34:21 -08:00
|
|
|
break;
|
2011-11-24 03:45:55 -08:00
|
|
|
case eDeliveryState_Sent:
|
2012-01-17 10:48:58 -08:00
|
|
|
aDelivery = DELIVERY_SENT;
|
2011-11-24 03:45:55 -08:00
|
|
|
break;
|
|
|
|
case eDeliveryState_Unknown:
|
2012-01-17 10:42:05 -08:00
|
|
|
case eDeliveryState_EndGuard:
|
2011-11-24 03:45:55 -08:00
|
|
|
default:
|
2012-01-17 10:42:05 -08:00
|
|
|
NS_ASSERTION(true, "We shouldn't get any other delivery state!");
|
2011-11-24 03:45:55 -08:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-11-22 01:34:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetSender(nsAString& aSender)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
aSender = mData.sender();
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetReceiver(nsAString& aReceiver)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
aReceiver = mData.receiver();
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetBody(nsAString& aBody)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
aBody = mData.body();
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetTimestamp(JSContext* cx, jsval* aDate)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
*aDate = OBJECT_TO_JSVAL(JS_NewDateObjectMsec(cx, mData.timestamp()));
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-17 12:42:38 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetRead(bool* aRead)
|
|
|
|
{
|
|
|
|
*aRead = mData.read();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-22 01:34:21 -08:00
|
|
|
} // namespace sms
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|