1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "SmsFilter.h"
39 : #include "nsIDOMClassInfo.h"
40 : #include "Constants.h"
41 : #include "nsError.h"
42 : #include "Constants.h"
43 : #include "jsapi.h"
44 : #include "jsfriendapi.h" // For js_DateGetMsecSinceEpoch.
45 : #include "js/Utility.h"
46 : #include "nsJSUtils.h"
47 : #include "nsDOMString.h"
48 :
49 : DOMCI_DATA(MozSmsFilter, mozilla::dom::sms::SmsFilter)
50 :
51 : namespace mozilla {
52 : namespace dom {
53 : namespace sms {
54 :
55 0 : NS_INTERFACE_MAP_BEGIN(SmsFilter)
56 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsFilter)
57 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
58 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsFilter)
59 0 : NS_INTERFACE_MAP_END
60 :
61 0 : NS_IMPL_ADDREF(SmsFilter)
62 0 : NS_IMPL_RELEASE(SmsFilter)
63 :
64 0 : SmsFilter::SmsFilter()
65 : {
66 0 : mData.startDate() = 0;
67 0 : mData.endDate() = 0;
68 0 : mData.delivery() = eDeliveryState_Unknown;
69 0 : }
70 :
71 0 : SmsFilter::SmsFilter(const SmsFilterData& aData)
72 0 : : mData(aData)
73 : {
74 0 : }
75 :
76 : /* static */ nsresult
77 0 : SmsFilter::NewSmsFilter(nsISupports** aSmsFilter)
78 : {
79 0 : NS_ADDREF(*aSmsFilter = new SmsFilter());
80 0 : return NS_OK;
81 : }
82 :
83 : NS_IMETHODIMP
84 0 : SmsFilter::GetStartDate(JSContext* aCx, jsval* aStartDate)
85 : {
86 0 : if (mData.startDate() == 0) {
87 0 : *aStartDate = JSVAL_NULL;
88 0 : return NS_OK;
89 : }
90 :
91 0 : aStartDate->setObjectOrNull(JS_NewDateObjectMsec(aCx, mData.startDate()));
92 0 : NS_ENSURE_TRUE(aStartDate->isObject(), NS_ERROR_FAILURE);
93 :
94 0 : return NS_OK;
95 : }
96 :
97 : NS_IMETHODIMP
98 0 : SmsFilter::SetStartDate(JSContext* aCx, const jsval& aStartDate)
99 : {
100 0 : if (aStartDate == JSVAL_NULL) {
101 0 : mData.startDate() = 0;
102 0 : return NS_OK;
103 : }
104 :
105 0 : if (!aStartDate.isObject()) {
106 0 : return NS_ERROR_INVALID_ARG;
107 : }
108 :
109 0 : JSObject& obj = aStartDate.toObject();
110 0 : if (!JS_ObjectIsDate(aCx, &obj)) {
111 0 : return NS_ERROR_INVALID_ARG;
112 : }
113 :
114 0 : mData.startDate() = js_DateGetMsecSinceEpoch(aCx, &obj);
115 0 : return NS_OK;
116 : }
117 :
118 : NS_IMETHODIMP
119 0 : SmsFilter::GetEndDate(JSContext* aCx, jsval* aEndDate)
120 : {
121 0 : if (mData.endDate() == 0) {
122 0 : *aEndDate = JSVAL_NULL;
123 0 : return NS_OK;
124 : }
125 :
126 0 : aEndDate->setObjectOrNull(JS_NewDateObjectMsec(aCx, mData.endDate()));
127 0 : NS_ENSURE_TRUE(aEndDate->isObject(), NS_ERROR_FAILURE);
128 :
129 0 : return NS_OK;
130 : }
131 :
132 : NS_IMETHODIMP
133 0 : SmsFilter::SetEndDate(JSContext* aCx, const jsval& aEndDate)
134 : {
135 0 : if (aEndDate == JSVAL_NULL) {
136 0 : mData.endDate() = 0;
137 0 : return NS_OK;
138 : }
139 :
140 0 : if (!aEndDate.isObject()) {
141 0 : return NS_ERROR_INVALID_ARG;
142 : }
143 :
144 0 : JSObject& obj = aEndDate.toObject();
145 0 : if (!JS_ObjectIsDate(aCx, &obj)) {
146 0 : return NS_ERROR_INVALID_ARG;
147 : }
148 :
149 0 : mData.endDate() = js_DateGetMsecSinceEpoch(aCx, &obj);
150 0 : return NS_OK;
151 : }
152 :
153 : NS_IMETHODIMP
154 0 : SmsFilter::GetNumbers(JSContext* aCx, jsval* aNumbers)
155 : {
156 0 : PRUint32 length = mData.numbers().Length();
157 :
158 0 : if (length == 0) {
159 0 : *aNumbers = JSVAL_NULL;
160 0 : return NS_OK;
161 : }
162 :
163 0 : jsval* numbers = new jsval[length];
164 :
165 0 : for (PRUint32 i=0; i<length; ++i) {
166 0 : numbers[i].setString(JS_NewUCStringCopyN(aCx, mData.numbers()[i].get(),
167 0 : mData.numbers()[i].Length()));
168 : }
169 :
170 0 : aNumbers->setObjectOrNull(JS_NewArrayObject(aCx, length, numbers));
171 0 : NS_ENSURE_TRUE(aNumbers->isObject(), NS_ERROR_FAILURE);
172 :
173 0 : return NS_OK;
174 : }
175 :
176 : NS_IMETHODIMP
177 0 : SmsFilter::SetNumbers(JSContext* aCx, const jsval& aNumbers)
178 : {
179 0 : if (aNumbers == JSVAL_NULL) {
180 0 : mData.numbers().Clear();
181 0 : return NS_OK;
182 : }
183 :
184 0 : if (!aNumbers.isObject()) {
185 0 : return NS_ERROR_INVALID_ARG;
186 : }
187 :
188 0 : JSObject& obj = aNumbers.toObject();
189 0 : if (!JS_IsArrayObject(aCx, &obj)) {
190 0 : return NS_ERROR_INVALID_ARG;
191 : }
192 :
193 : uint32_t size;
194 0 : JS_ALWAYS_TRUE(JS_GetArrayLength(aCx, &obj, &size));
195 :
196 0 : nsTArray<nsString> numbers;
197 :
198 0 : for (uint32_t i=0; i<size; ++i) {
199 : jsval jsNumber;
200 0 : if (!JS_GetElement(aCx, &obj, i, &jsNumber)) {
201 0 : return NS_ERROR_INVALID_ARG;
202 : }
203 :
204 0 : if (!jsNumber.isString()) {
205 0 : return NS_ERROR_INVALID_ARG;
206 : }
207 :
208 0 : nsDependentJSString number;
209 0 : number.init(aCx, jsNumber.toString());
210 :
211 0 : numbers.AppendElement(number);
212 : }
213 :
214 0 : mData.numbers().Clear();
215 0 : mData.numbers().AppendElements(numbers);
216 :
217 0 : return NS_OK;
218 : }
219 :
220 : NS_IMETHODIMP
221 0 : SmsFilter::GetDelivery(nsAString& aDelivery)
222 : {
223 0 : switch (mData.delivery()) {
224 : case eDeliveryState_Received:
225 0 : aDelivery = DELIVERY_RECEIVED;
226 0 : break;
227 : case eDeliveryState_Sent:
228 0 : aDelivery = DELIVERY_SENT;
229 0 : break;
230 : case eDeliveryState_Unknown:
231 0 : SetDOMStringToNull(aDelivery);
232 0 : break;
233 : default:
234 0 : NS_ASSERTION(false, "We shouldn't get another delivery state!");
235 0 : return NS_ERROR_UNEXPECTED;
236 : }
237 :
238 0 : return NS_OK;
239 : }
240 :
241 : NS_IMETHODIMP
242 0 : SmsFilter::SetDelivery(const nsAString& aDelivery)
243 : {
244 0 : if (aDelivery.IsEmpty()) {
245 0 : mData.delivery() = eDeliveryState_Unknown;
246 0 : return NS_OK;
247 : }
248 :
249 0 : if (aDelivery.Equals(DELIVERY_RECEIVED)) {
250 0 : mData.delivery() = eDeliveryState_Received;
251 0 : return NS_OK;
252 : }
253 :
254 0 : if (aDelivery.Equals(DELIVERY_SENT)) {
255 0 : mData.delivery() = eDeliveryState_Sent;
256 0 : return NS_OK;
257 : }
258 :
259 0 : return NS_ERROR_INVALID_ARG;
260 : }
261 :
262 : } // namespace sms
263 : } // namespace dom
264 : } // namespace mozilla
|