31 #include <nsAutoPtr.h>
33 #include <nsIClassInfoImpl.h>
34 #include <nsIStringEnumerator.h>
35 #include <nsStringGlue.h>
39 #include <sbIFilterableMediaListView.h>
40 #include <sbIMediaListView.h>
41 #include <sbIMediaItem.h>
52 static PRLogModuleInfo* gScriptableFilterItemsLog = nsnull;
56 #define LOG(args) PR_LOG(gScriptableFilterItemsLog, PR_LOG_WARN, args)
57 #define TRACE(args) PR_LOG(gScriptableFilterItemsLog, PR_LOG_DEBUG, args)
80 if (!gScriptableFilterItemsLog) {
81 gScriptableFilterItemsLog = PR_NewLogModule(
"sbScriptableFilterItems" );
84 TRACE((
"sbScriptableFilterItems::sbScriptableFilterItems()"));
95 if (!gScriptableFilterItemsLog) {
96 gScriptableFilterItemsLog = PR_NewLogModule(
"sbScriptableFilterItems" );
99 TRACE((
"sbScriptableFilterItems::sbScriptableFilterItems()"));
102 sbScriptableFilterItems::~sbScriptableFilterItems()
116 return NS_ERROR_NOT_INITIALIZED;
121 nsCOMPtr<sbIMediaListView> listView = do_QueryInterface(
mListView, &rv );
122 NS_ENSURE_SUCCESS( rv, rv );
125 rv = listView->GetLength( &length );
126 NS_ENSURE_SUCCESS( rv, rv );
128 for (PRUint32
i = 0;
i < length; ++
i ) {
129 nsCOMPtr<sbIMediaItem> item;
130 rv = listView->GetItemByIndex(
i, getter_AddRefs(item) );
131 NS_ENSURE_SUCCESS( rv, rv );
132 mItems.AppendObject(item);
147 NS_ENSURE_ARG_POINTER(aClassName);
149 NS_NAMED_LITERAL_CSTRING( kClassName,
"sbScriptableFilterItems" );
150 *aClassName = ToNewCString(kClassName);
151 return aClassName ?
NS_OK : NS_ERROR_OUT_OF_MEMORY;
157 NS_ENSURE_ARG_POINTER(aScriptableFlags);
161 *aScriptableFlags = USE_JSSTUB_FOR_ADDPROPERTY |
162 DONT_ENUM_STATIC_PROPS |
163 DONT_ENUM_QUERY_INTERFACE |
164 CLASSINFO_INTERFACES_ONLY |
169 ALLOW_PROP_MODS_DURING_RESOLVE |
170 DONT_REFLECT_INTERFACE_NAMES ;
181 TRACE((
"sbScriptableFilterItems::GetProperty()"));
182 NS_ENSURE_ARG_POINTER(_retval);
184 JSString* jsstr = JS_ValueToString( cx,
id );
190 NS_ENSURE_SUCCESS( rv, rv );
194 nsDependentString jsid( (PRUnichar *)::JS_GetStringChars(jsstr),
195 ::JS_GetStringLength(jsstr));
196 TRACE((
" getting property %s", NS_LossyConvertUTF16toASCII(jsid).
get() ));
198 PRInt32 length =
mItems.Count();
199 for (PRInt32
i = 0;
i < length; ++
i) {
202 NS_ENSURE_SUCCESS( rv, rv );
203 if ( guid.Equals(jsid) ) {
204 nsCOMPtr<sbIMediaItem> remoteItem;
206 NS_ENSURE_SUCCESS( rv, rv );
208 nsCOMPtr<nsIXPConnect> xpc;
209 rv = wrapper->GetXPConnect( getter_AddRefs(xpc) );
210 NS_ENSURE_SUCCESS( rv, rv );
212 nsCOMPtr<nsIXPConnectJSObjectHolder> objHolder;
214 rv = xpc->WrapNative( cx,
218 getter_AddRefs(objHolder) );
219 NS_ENSURE_SUCCESS( rv, rv );
221 JSObject*
object = nsnull;
222 rv = objHolder->GetJSObject( &
object );
223 NS_ENSURE_SUCCESS( rv, rv );
225 *vp = OBJECT_TO_JSVAL(
object);
226 return NS_SUCCESS_I_DID_SOMETHING;
231 if ( jsid.EqualsLiteral(
"length") ) {
232 *vp = INT_TO_JSVAL(
mItems.Count() );
233 return NS_SUCCESS_I_DID_SOMETHING;
247 TRACE((
"sbScriptableFilterItems::NewEnumerate()"));
249 NS_ENSURE_ARG_POINTER(_retval);
250 NS_ENSURE_ARG_POINTER(statep);
253 NS_ENSURE_SUCCESS( rv, rv );
258 case JSENUMERATE_INIT: {
259 *statep = INT_TO_JSVAL(0);
261 *idp = INT_TO_JSVAL(
mItems.Count());
266 case JSENUMERATE_NEXT: {
268 JSAutoRequest ar(cx);
270 PRInt32
i = JSVAL_TO_INT(*statep);
271 if ( i < 0 || i >
mItems.Count() ) {
272 TRACE((
" invalid state %i of %i", i,
mItems.Count() ));
274 *statep = JSVAL_NULL;
275 return NS_ERROR_INVALID_ARG;
276 }
else if ( i ==
mItems.Count() ) {
277 TRACE((
" finished iteration"));
279 *statep = JSVAL_NULL;
283 nsCOMPtr<sbIMediaItem> item =
mItems[
i];
286 rv = item->GetGuid(guid);
287 NS_ENSURE_SUCCESS( rv, rv );
289 JSString *jsstr = JS_NewUCStringCopyN( cx,
293 TRACE((
" failed to alloc string"));
295 return NS_ERROR_OUT_OF_MEMORY;
299 *_retval = JS_DefineUCProperty( cx,
301 JS_GetStringChars(jsstr),
302 JS_GetStringLength(jsstr),
310 return NS_ERROR_FAILURE;
313 *_retval = JS_ValueToId( cx, STRING_TO_JSVAL(jsstr), idp );
315 TRACE((
" failed to get id"));
316 return NS_ERROR_FAILURE;
319 *statep = INT_TO_JSVAL(++i);
320 TRACE((
" next: %i", JSVAL_TO_INT(*statep)));
323 case JSENUMERATE_DESTROY: {
330 return NS_ERROR_INVALID_ARG;
343 TRACE((
"sbScriptableFilterItems::NewResolve()"));
344 NS_ENSURE_ARG_POINTER(_retval);
347 NS_ENSURE_SUCCESS( rv, rv );
350 *_retval = JS_IdToValue( cx,
id, &v );
351 NS_ENSURE_TRUE( *_retval, NS_ERROR_INVALID_ARG );
354 JSString *jsstr = JS_ValueToString( cx,
id );
362 nsDependentString prop( JS_GetStringChars(jsstr) );
363 TRACE((
" Resolving property %s",
364 NS_LossyConvertUTF16toASCII(prop).BeginReading() ));
366 PRInt32 length =
mItems.Count();
367 for (PRInt32
i = 0;
i < length; ++
i) {
370 NS_ENSURE_SUCCESS( rv, rv );
371 if ( guid.Equals(prop) ) {
372 *_retval = JS_DefineUCProperty( cx,
374 JS_GetStringChars(jsstr),
375 JS_GetStringLength(jsstr),
401 LOG((
"sbScriptableFilterItems::Equality()"));
403 NS_ENSURE_ARG_POINTER(_retval);
404 NS_ENSURE_ARG_POINTER(obj);
405 NS_ENSURE_ARG_POINTER(wrapper);
409 if ( !JSVAL_IS_OBJECT(val) ) {
412 JSObject* otherObj = JSVAL_TO_OBJECT(val);
414 nsCOMPtr<nsIXPConnect> xpc;
415 rv = wrapper->GetXPConnect( getter_AddRefs(xpc) );
416 NS_ENSURE_SUCCESS( rv, rv );
418 nsCOMPtr<nsIXPConnectWrappedNative> otherWrapper;
419 rv = xpc->GetWrappedNativeOfJSObject( cx, otherObj, getter_AddRefs(otherWrapper) );
420 if ( NS_FAILED(rv) ) {
425 nsRefPtr<sbScriptableFilterItems> other;
427 rv = CallQueryInterface( otherWrapper->Native(), &otherPtr );
428 if ( NS_FAILED(rv) ) {
432 other = already_AddRefed<sbScriptableFilterItems>(otherPtr);
435 NS_ENSURE_SUCCESS( rv, rv );
436 rv = other->ReadEnumerator();
437 NS_ENSURE_SUCCESS( rv, rv );
439 if (
mItems.Count() != other->mItems.Count() ) {
447 std::multiset<nsString> itemSet;
448 for (PRInt32
i = 0;
i < other->mItems.Count(); ++
i) {
450 rv = other->mItems[
i]->GetGuid(guid);
451 NS_ENSURE_SUCCESS( rv, rv );
452 itemSet.insert(guid);
455 PRInt32 itemsCount =
mItems.Count();
456 for (PRInt32
i = 0;
i < itemsCount; ++
i) {
459 NS_ENSURE_SUCCESS( rv, rv );
460 std::multiset<nsString>::iterator found;
461 found = itemSet.find(guid);
462 if ( found == itemSet.end() ) {
466 itemSet.erase(found);
468 if ( itemSet.empty() ) {
483 NS_IMETHODIMP sbScriptableFilterItems::HasMoreElements(PRBool *_retval)
485 NS_ENSURE_ARG_POINTER(_retval);
486 NS_ENSURE_TRUE(
mHasItems, NS_ERROR_NOT_INITIALIZED );
493 NS_IMETHODIMP sbScriptableFilterItems::GetNext(
nsISupports **_retval)
495 NS_ENSURE_ARG_POINTER(_retval);
496 NS_ENSURE_TRUE(
mHasItems, NS_ERROR_NOT_INITIALIZED );
503 nsCOMPtr<sbIIndexedMediaItem> item = do_QueryInterface(rawItem, &rv);
504 if (NS_SUCCEEDED(rv)) {
506 nsRefPtr<sbRemoteIndexedMediaItem> indexedMediaItem =
508 NS_ENSURE_TRUE(indexedMediaItem, NS_ERROR_OUT_OF_MEMORY);
510 rv = indexedMediaItem->Init();
511 NS_ENSURE_SUCCESS(rv, rv);
516 nsCOMPtr<sbIMediaItem> remoteItem;
518 NS_ENSURE_SUCCESS(rv, rv);
523 TRACE((
"sbScriptableFilterItems::GetNext(): got %i = %08x",
536 NS_IMETHODIMP sbScriptableFilterItems::CanCreateWrapper(
const nsIID * iid,
539 TRACE((
"sbScriptableFilterItems::CanCreateWrapper()"));
541 NS_ENSURE_ARG_POINTER(_retval);
542 *_retval = ToNewCString( NS_LITERAL_CSTRING(
"AllAccess") );
546 NS_IMETHODIMP sbScriptableFilterItems::CanCallMethod(
const nsIID * iid,
547 const PRUnichar *methodName,
550 TRACE((
"sbScriptableFilterItems::CanCallMethod() - %s",
551 NS_LossyConvertUTF16toASCII(methodName).BeginReading()));
553 NS_ENSURE_ARG_POINTER(_retval);
554 *_retval = ToNewCString( NS_LITERAL_CSTRING(
"AllAccess") );
558 NS_IMETHODIMP sbScriptableFilterItems::CanGetProperty(
const nsIID * iid,
559 const PRUnichar *propertyName,
562 TRACE((
"sbScriptableFilterItems::CanGetProperty() - %s",
563 NS_LossyConvertUTF16toASCII(propertyName).BeginReading()));
565 NS_ENSURE_ARG_POINTER(_retval);
566 *_retval = ToNewCString( NS_LITERAL_CSTRING(
"AllAccess") );
570 NS_IMETHODIMP sbScriptableFilterItems::CanSetProperty(
const nsIID * iid,
571 const PRUnichar *propertyName,
574 TRACE((
"sbScriptableFilterItems::CanSetProperty() - %s",
575 NS_LossyConvertUTF16toASCII(propertyName).BeginReading()));
577 NS_ENSURE_ARG_POINTER(_retval);
578 *_retval = ToNewCString( NS_LITERAL_CSTRING(
"NoAccess") );
NS_IMETHOD GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, jsval *vp, PRBool *_retval)
sbScriptableFilterItems(sbIFilterableMediaListView *aFilterList, sbRemotePlayer *aPlayer)
#define SB_SCRIPTABLE_FILETER_ITEMS_CID
sbDeviceFirmwareAutoCheckForUpdate prototype flags
NS_IMETHOD NewEnumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, PRUint32 enum_op, jsval *statep, jsid *idp, PRBool *_retval)
NS_IMPL_ISUPPORTS4(sbScriptableFilterItems, nsISecurityCheckedComponent, nsIXPCScriptable, nsISimpleEnumerator, sbScriptableFilterItems) NS_IMPL_CI_INTERFACE_GETTER3(sbScriptableFilterItems
NS_IMETHOD Equality(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval val, PRBool *_retval)
nsRefPtr< sbRemotePlayer > mPlayer
nsCOMPtr< sbIFilterableMediaListView > mListView
this _dialogInput val(dateText)
NS_IMETHOD NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, PRUint32 flags, JSObject **objp, PRBool *_retval)
nsCOMArray< sbIMediaItem > mItems
nsRefPtr< sbRemotePlayer > mPlayer
PRUint32 mEnumerationIndex
NS_DECL_ISUPPORTS NS_DECL_NSICLASSINFO NS_DECL_NSISECURITYCHECKEDCOMPONENT NS_DECL_NSISIMPLEENUMERATOR NS_IMETHOD GetClassName(char **aClassName)
static nsresult SB_WrapMediaItem(sbRemotePlayer *aRemotePlayer, sbIMediaItem *aMediaItem, sbIMediaItem **aRemoteMediaItem)
nsCOMPtr< sbIFilterableMediaListView > mListView
#define SB_IMPL_CLASSINFO_INTERFACES_ONLY(_class)
_getSelectedPageStyle s i
nsISecurityCheckedComponent
nsresult ReadEnumerator()
NS_IMETHOD GetScriptableFlags(PRUint32 *aScriptableFlags)