31 #include <nsAutoPtr.h>
33 #include <nsIClassInfoImpl.h>
34 #include <nsIStringEnumerator.h>
35 #include <nsIXPConnect.h>
36 #include <nsStringGlue.h>
40 #include <sbIFilterableMediaListView.h>
41 #include <sbILibraryConstraints.h>
42 #include <sbIScriptableFilterResult.h>
43 #include <sbIMediaListView.h>
44 #include <sbIPropertyArray.h>
54 static PRLogModuleInfo* gScriptableFilterLog = nsnull;
58 #define LOG(args) PR_LOG(gScriptableFilterLog, PR_LOG_WARN, args)
59 #define TRACE(args) PR_LOG(gScriptableFilterLog, PR_LOG_DEBUG, args)
67 const nsAString &aPropertyName,
69 : mListView(aMediaListView),
70 mPropertyName(aPropertyName),
76 if (!gScriptableFilterLog) {
77 gScriptableFilterLog = PR_NewLogModule(
"sbScriptableFilter" );
80 TRACE((
"sbScriptableFilter::sbScriptableFilter()"));
83 sbScriptableFilter::~sbScriptableFilter()
96 return NS_ERROR_NOT_INITIALIZED;
101 nsCOMPtr<sbIMediaListView> view = do_QueryInterface(
mListView, &rv );
102 NS_ENSURE_SUCCESS( rv, rv );
104 nsCOMPtr<nsIStringEnumerator> enumerator;
105 rv = view->GetDistinctValuesForProperty(
mPropertyName, getter_AddRefs(enumerator) );
106 NS_ENSURE_SUCCESS( rv, rv );
111 rv = enumerator->HasMore(&hasMore);
112 NS_ENSURE_SUCCESS( rv, rv );
118 rv = enumerator->GetNext(value);
119 NS_ENSURE_SUCCESS( rv, rv );
138 NS_IMETHODIMP sbScriptableFilter::HasMore(PRBool *_retval)
140 TRACE((
"sbScriptableFilter::HasMore()"));
141 NS_ENSURE_ARG_POINTER(_retval);
144 NS_ENSURE_SUCCESS( rv, rv );
148 TRACE((
"sbScriptableFilter::HasMore() - got %s",
149 *_retval ?
"YES" :
"no"));
155 NS_IMETHODIMP sbScriptableFilter::GetNext(nsAString & _retval)
157 TRACE((
"sbScriptableFilter::GetNext()"));
159 NS_ENSURE_SUCCESS( rv, rv );
162 return NS_ERROR_FAILURE;
168 TRACE((
"sbScriptableFilter::GetNext() - got %s",
169 NS_LossyConvertUTF16toASCII(_retval).BeginReading()));
183 NS_ENSURE_ARG_POINTER(aClassName);
185 NS_NAMED_LITERAL_CSTRING( kClassName,
"sbScriptableFilter" );
186 *aClassName = ToNewCString(kClassName);
187 return aClassName ?
NS_OK : NS_ERROR_OUT_OF_MEMORY;
193 NS_ENSURE_ARG_POINTER(aScriptableFlags);
197 *aScriptableFlags = USE_JSSTUB_FOR_ADDPROPERTY |
198 DONT_ENUM_STATIC_PROPS |
199 DONT_ENUM_QUERY_INTERFACE |
203 ALLOW_PROP_MODS_DURING_RESOLVE |
204 DONT_REFLECT_INTERFACE_NAMES ;
215 TRACE((
"sbScriptableFilter::GetProperty()"));
216 NS_ENSURE_ARG_POINTER(_retval);
218 JSString* jsstr = JS_ValueToString( cx,
id );
224 NS_ENSURE_SUCCESS( rv, rv );
228 nsDependentString jsid( (PRUnichar *)::JS_GetStringChars(jsstr),
229 ::JS_GetStringLength(jsstr));
230 TRACE((
" getting property %s", NS_LossyConvertUTF16toASCII(jsid).
get() ));
233 for (PRInt32
i = 0;
i < length; ++
i) {
237 nsCOMPtr<sbIMediaListView> view = do_QueryInterface(
mListView, &rv );
238 NS_ENSURE_SUCCESS( rv, rv );
239 nsCOMPtr<sbIMediaListView> clonedView;
240 rv = view->Clone( getter_AddRefs(clonedView) );
241 NS_ENSURE_SUCCESS( rv, rv );
242 nsCOMPtr<sbIFilterableMediaListView> filterView =
243 do_QueryInterface( clonedView, &rv );
244 NS_ENSURE_SUCCESS( rv, rv );
247 nsCOMPtr<sbILibraryConstraint> constraint;
248 rv = filterView->GetFilterConstraint( getter_AddRefs(constraint) );
249 NS_ENSURE_SUCCESS( rv, rv );
251 nsCOMPtr<sbILibraryConstraintBuilder> builder =
252 do_CreateInstance(
"@songbirdnest.com/Songbird/Library/ConstraintBuilder;1",
254 NS_ENSURE_SUCCESS( rv, rv );
258 rv = builder->IncludeConstraint( constraint, nsnull );
259 NS_ENSURE_SUCCESS( rv, rv );
260 rv = builder->Intersect(nsnull);
261 NS_ENSURE_SUCCESS( rv, rv );
265 NS_ENSURE_SUCCESS( rv, rv );
266 rv = builder->Get( getter_AddRefs(constraint) );
267 NS_ENSURE_SUCCESS( rv, rv );
268 rv = filterView->SetFilterConstraint( constraint );
269 NS_ENSURE_SUCCESS( rv, rv );
272 nsCOMPtr<sbIScriptableFilterResult> result =
275 return NS_ERROR_OUT_OF_MEMORY;
278 nsCOMPtr<nsIXPConnect> xpc;
279 rv = wrapper->GetXPConnect( getter_AddRefs(xpc) );
280 NS_ENSURE_SUCCESS( rv, rv );
282 nsCOMPtr<nsIXPConnectJSObjectHolder> objHolder;
284 rv = xpc->WrapNative( cx,
288 getter_AddRefs(objHolder) );
289 NS_ENSURE_SUCCESS( rv, rv );
291 JSObject*
object = nsnull;
292 rv = objHolder->GetJSObject( &
object );
293 NS_ENSURE_SUCCESS( rv, rv );
295 *vp = OBJECT_TO_JSVAL(
object);
296 return NS_SUCCESS_I_DID_SOMETHING;
301 if ( jsid.EqualsLiteral(
"length") ) {
302 *vp = INT_TO_JSVAL(
mStrings.Count() );
303 return NS_SUCCESS_I_DID_SOMETHING;
317 TRACE((
"sbScriptableFilter::NewEnumerate()"));
319 NS_ENSURE_ARG_POINTER(_retval);
320 NS_ENSURE_ARG_POINTER(statep);
323 NS_ENSURE_SUCCESS( rv, rv );
328 case JSENUMERATE_INIT: {
329 *statep = INT_TO_JSVAL(0);
331 *idp = INT_TO_JSVAL(
mStrings.Count());
336 case JSENUMERATE_NEXT: {
338 JSAutoRequest ar(cx);
340 PRInt32
i = JSVAL_TO_INT(*statep);
341 if ( i < 0 || i >
mStrings.Count() ) {
344 *statep = JSVAL_NULL;
345 return NS_ERROR_INVALID_ARG;
346 }
else if ( i ==
mStrings.Count() ) {
347 TRACE((
" finished iteration"));
349 *statep = JSVAL_NULL;
355 JSString *jsstr = JS_NewUCStringCopyN( cx,
359 TRACE((
" failed to alloc string"));
361 return NS_ERROR_OUT_OF_MEMORY;
365 *_retval = JS_DefineUCProperty( cx,
367 JS_GetStringChars(jsstr),
368 JS_GetStringLength(jsstr),
376 return NS_ERROR_FAILURE;
379 *_retval = JS_ValueToId( cx, STRING_TO_JSVAL(jsstr), idp );
381 TRACE((
" failed to get id"));
382 return NS_ERROR_FAILURE;
385 *statep = INT_TO_JSVAL(++i);
386 TRACE((
" next: %i", JSVAL_TO_INT(*statep)));
389 case JSENUMERATE_DESTROY: {
396 return NS_ERROR_INVALID_ARG;
409 TRACE((
"sbScriptableFilter::NewResolve()"));
410 NS_ENSURE_ARG_POINTER(_retval);
413 NS_ENSURE_SUCCESS( rv, rv );
416 *_retval = JS_IdToValue( cx,
id, &v );
417 NS_ENSURE_TRUE( *_retval, NS_ERROR_INVALID_ARG );
420 JSString *jsstr = JS_ValueToString( cx,
id );
428 nsDependentString prop( JS_GetStringChars(jsstr) );
429 TRACE((
" Resolving property %s",
430 NS_LossyConvertUTF16toASCII(prop).BeginReading() ));
433 for (PRInt32
i = 0;
i < length; ++
i) {
435 *_retval = JS_DefineUCProperty( cx,
437 JS_GetStringChars(jsstr),
438 JS_GetStringLength(jsstr),
464 NS_IMETHODIMP sbScriptableFilter::CanCreateWrapper(
const nsIID * iid,
467 TRACE((
"sbScriptableFilter::CanCreateWrapper()"));
469 NS_ENSURE_ARG_POINTER(_retval);
470 *_retval = ToNewCString( NS_LITERAL_CSTRING(
"AllAccess") );
474 NS_IMETHODIMP sbScriptableFilter::CanCallMethod(
const nsIID * iid,
475 const PRUnichar *methodName,
478 TRACE((
"sbScriptableFilter::CanCallMethod() - %s",
479 NS_LossyConvertUTF16toASCII(methodName).BeginReading()));
481 NS_ENSURE_ARG_POINTER(_retval);
482 *_retval = ToNewCString( NS_LITERAL_CSTRING(
"AllAccess") );
486 NS_IMETHODIMP sbScriptableFilter::CanGetProperty(
const nsIID * iid,
487 const PRUnichar *propertyName,
490 TRACE((
"sbScriptableFilter::CanGetProperty() - %s",
491 NS_LossyConvertUTF16toASCII(propertyName).BeginReading()));
493 NS_ENSURE_ARG_POINTER(_retval);
494 *_retval = ToNewCString( NS_LITERAL_CSTRING(
"AllAccess") );
498 NS_IMETHODIMP sbScriptableFilter::CanSetProperty(
const nsIID * iid,
499 const PRUnichar *propertyName,
502 TRACE((
"sbScriptableFilter::CanSetProperty() - %s",
503 NS_LossyConvertUTF16toASCII(propertyName).BeginReading()));
505 NS_ENSURE_ARG_POINTER(_retval);
506 *_retval = ToNewCString( NS_LITERAL_CSTRING(
"NoAccess") );
NS_IMETHOD NewEnumerate(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, PRUint32 enum_op, jsval *statep, jsid *idp, PRBool *_retval)
nsCOMPtr< sbIFilterableMediaListView > mListView
sbDeviceFirmwareAutoCheckForUpdate prototype flags
nsRefPtr< sbRemotePlayer > mPlayer
nsISecurityCheckedComponent
NS_IMETHOD GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, jsval *vp, PRBool *_retval)
NS_IMETHOD NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, PRUint32 flags, JSObject **objp, PRBool *_retval)
nsresult ReadEnumerator()
The result of a filtering expression in a library.
NS_DECL_ISUPPORTS NS_DECL_NSICLASSINFO NS_DECL_NSISECURITYCHECKEDCOMPONENT NS_DECL_NSISTRINGENUMERATOR NS_IMETHOD GetClassName(char **aClassName)
NS_IMPL_ISUPPORTS3_CI(sbScriptableFilter, nsISecurityCheckedComponent, nsIXPCScriptable, nsIStringEnumerator) sbScriptableFilter
StringArrayEnumerator prototype hasMore
NS_IMETHOD GetScriptableFlags(PRUint32 *aScriptableFlags)
#define SB_IMPL_CLASSINFO_INTERFACES_ONLY(_class)
friend class sbScriptableFilterResult
_getSelectedPageStyle s i