sbScriptableFilterResult.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
28 
29 #include <sbClassInfoUtils.h>
30 #include <sbStandardProperties.h>
31 
32 #include <nsAutoPtr.h>
33 #include <nsMemory.h>
34 #include <nsIClassInfoImpl.h>
35 #include <nsIStringEnumerator.h>
36 #include <nsStringGlue.h>
37 
38 #include <jsapi.h>
39 
40 #include <sbIFilterableMediaListView.h>
41 #include <sbILibraryConstraints.h>
42 
43 #include "sbRemotePlayer.h"
44 #include "sbScriptableFilter.h"
46 
47 /*
48  * To log this module, set the following environment variable:
49  * NSPR_LOG_MODULES=sbScriptableFilterResult:5
50  */
51 #ifdef PR_LOGGING
52 static PRLogModuleInfo* gScriptableFilterResultLog = nsnull;
53 #endif
54 
55 #undef LOG
56 #define LOG(args) PR_LOG(gScriptableFilterResultLog, PR_LOG_WARN, args)
57 #define TRACE(args) PR_LOG(gScriptableFilterResultLog, PR_LOG_DEBUG, args)
58 
63 
65  sbRemotePlayer *aRemotePlayer )
66  : mListView(aMediaListView),
67  mPlayer(aRemotePlayer)
68 {
69 #ifdef PR_LOGGING
70  if (!gScriptableFilterResultLog) {
71  gScriptableFilterResultLog = PR_NewLogModule( "sbScriptableFilterResult" );
72  }
73 #endif
74  TRACE(("sbScriptableFilterResult::sbScriptableFilterResult()"));
75 }
76 
77 sbScriptableFilterResult::~sbScriptableFilterResult()
78 {
79 }
80 
81 
82 // ---------------------------------------------------------------------------
83 //
84 // sbIScriptableFilterResult
85 //
86 // ---------------------------------------------------------------------------
87 
88 NS_IMETHODIMP sbScriptableFilterResult::GetArtists(nsIStringEnumerator * *aArtists)
89 {
90  LOG(("sbScriptableFilterResult::GetArtists()"));
91 
92  nsRefPtr<sbScriptableFilter> filter =
94  NS_LITERAL_STRING(SB_PROPERTY_ARTISTNAME),
95  mPlayer );
96  NS_ENSURE_TRUE( filter, NS_ERROR_OUT_OF_MEMORY );
97 
98  NS_ADDREF( *aArtists = filter );
99  return NS_OK;
100 }
101 
102 NS_IMETHODIMP sbScriptableFilterResult::GetAlbums(nsIStringEnumerator * *aAlbums)
103 {
104  LOG(("sbScriptableFilterResult::GetAlbums()"));
105 
106  nsRefPtr<sbScriptableFilter> filter =
108  NS_LITERAL_STRING(SB_PROPERTY_ALBUMNAME),
109  mPlayer );
110  NS_ENSURE_TRUE( filter, NS_ERROR_OUT_OF_MEMORY );
111 
112  NS_ADDREF( *aAlbums = filter );
113  return NS_OK;
114 }
115 
116 NS_IMETHODIMP sbScriptableFilterResult::GetGenres(nsIStringEnumerator * *aGenres)
117 {
118  LOG(("sbScriptableFilterResult::GetGenres()"));
119 
120  nsRefPtr<sbScriptableFilter> filter =
122  NS_LITERAL_STRING(SB_PROPERTY_GENRE),
123  mPlayer );
124  NS_ENSURE_TRUE( filter, NS_ERROR_OUT_OF_MEMORY );
125 
126  NS_ADDREF( *aGenres = filter );
127  return NS_OK;
128 }
129 
130 NS_IMETHODIMP sbScriptableFilterResult::GetYears(nsIStringEnumerator * *aYears)
131 {
132  LOG(("sbScriptableFilterResult::GetYears()"));
133 
134  nsRefPtr<sbScriptableFilter> filter =
136  NS_LITERAL_STRING(SB_PROPERTY_YEAR),
137  mPlayer );
138  NS_ENSURE_TRUE( filter, NS_ERROR_OUT_OF_MEMORY );
139 
140  NS_ADDREF( *aYears = filter );
141  return NS_OK;
142 }
143 
144 NS_IMETHODIMP sbScriptableFilterResult::GetItems(nsISupports * *aItems)
145 {
146  NS_ENSURE_ARG_POINTER(aItems);
147  nsRefPtr<sbScriptableFilterItems> items =
149 
150  return CallQueryInterface( items.get(), aItems );
151 }
152 
153 // ---------------------------------------------------------------------------
154 //
155 // nsIXPCScriptable
156 //
157 // ---------------------------------------------------------------------------
158 
159 NS_IMETHODIMP sbScriptableFilterResult::GetClassName(char * *aClassName)
160 {
161  NS_ENSURE_ARG_POINTER(aClassName);
162 
163  NS_NAMED_LITERAL_CSTRING( kClassName, "sbScriptableFilterResult" );
164  *aClassName = ToNewCString(kClassName);
165  return aClassName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
166 }
167 
168 /* readonly attribute PRUint32 scriptableFlags; */
169 NS_IMETHODIMP sbScriptableFilterResult::GetScriptableFlags(PRUint32 *aScriptableFlags)
170 {
171  NS_ENSURE_ARG_POINTER(aScriptableFlags);
172  // XXX Mook: USE_JSSTUB_FOR_ADDPROPERTY is needed to define things on the
173  // prototype properly; even with it set scripts cannot add
174  // properties onto the object (because they're not allow to *set*)
175  *aScriptableFlags = DONT_ENUM_STATIC_PROPS |
176  DONT_ENUM_QUERY_INTERFACE |
177  DONT_REFLECT_INTERFACE_NAMES |
178  CLASSINFO_INTERFACES_ONLY |
179  USE_JSSTUB_FOR_ADDPROPERTY |
180  WANT_EQUALITY;
181  return NS_OK;
182 }
183 
184 NS_IMETHODIMP sbScriptableFilterResult::Equality( nsIXPConnectWrappedNative *wrapper,
185  JSContext * cx,
186  JSObject * obj,
187  jsval val,
188  PRBool *_retval )
189 {
190  LOG(("sbScriptableFilterResult::Equality()"));
191  nsresult rv;
192  NS_ENSURE_ARG_POINTER(_retval);
193  NS_ENSURE_ARG_POINTER(obj);
194  NS_ENSURE_ARG_POINTER(wrapper);
195 
196  *_retval = PR_FALSE;
197 
198  if ( !JSVAL_IS_OBJECT(val) ) {
199  return NS_OK;
200  }
201  JSObject* otherObj = JSVAL_TO_OBJECT(val);
202 
203  nsCOMPtr<nsIXPConnect> xpc;
204  rv = wrapper->GetXPConnect( getter_AddRefs(xpc) );
205  NS_ENSURE_SUCCESS( rv, rv );
206 
207  nsCOMPtr<nsIXPConnectWrappedNative> otherWrapper;
208  rv = xpc->GetWrappedNativeOfJSObject( cx, otherObj, getter_AddRefs(otherWrapper) );
209  if ( NS_FAILED(rv) ) {
210  // not a C++ XPCOM object
211  return NS_OK;
212  }
213 
214  nsCOMPtr<sbIScriptableFilterResult> otherIface;
215  otherIface = do_QueryInterface( otherWrapper->Native(), &rv );
216  if ( NS_FAILED(rv) ) {
217  // not us
218  return NS_OK;
219  }
220 
221  nsCOMPtr<sbILibraryConstraint> constraint;
222  rv = mListView->GetFilterConstraint( getter_AddRefs(constraint) );
223  NS_ENSURE_SUCCESS( rv, rv );
224  NS_ENSURE_STATE(constraint);
225 
226  nsCOMPtr<sbILibraryConstraint> otherConstraint;
227  rv = otherIface->GetConstraint( getter_AddRefs(otherConstraint) );
228  NS_ENSURE_SUCCESS( rv, rv );
229 
230  rv = constraint->Equals( otherConstraint, _retval );
231  NS_ENSURE_SUCCESS( rv, rv );
232 
233  return NS_OK;
234 }
235 
236 NS_IMETHODIMP sbScriptableFilterResult::GetConstraint(sbILibraryConstraint * *aConstraint)
237 {
238  nsresult rv;
239  rv = mListView->GetFilterConstraint(aConstraint);
240  NS_ENSURE_SUCCESS( rv, rv );
241  NS_ENSURE_STATE(aConstraint);
242  return NS_OK;
243 }
244 
245 
246 // ---------------------------------------------------------------------------
247 //
248 // nsISecurityCheckedComponent
249 //
250 // ---------------------------------------------------------------------------
251 
252 // note that this class is only obtainable by going through the security checks
253 // so we allow access for everything (other than writing) without looking at
254 // what the user is asking.
255 
256 NS_IMETHODIMP sbScriptableFilterResult::CanCreateWrapper( const nsIID * iid,
257  char **_retval)
258 {
259  TRACE(("sbScriptableFilterResult::CanCreateWrapper()"));
260 
261  NS_ENSURE_ARG_POINTER(_retval);
262  *_retval = ToNewCString( NS_LITERAL_CSTRING("AllAccess") );
263  return NS_OK;
264 }
265 
266 NS_IMETHODIMP sbScriptableFilterResult::CanCallMethod( const nsIID * iid,
267  const PRUnichar *methodName,
268  char **_retval)
269 {
270  TRACE(("sbScriptableFilterResult::CanCallMethod() - %s",
271  NS_LossyConvertUTF16toASCII(methodName).BeginReading()));
272 
273  NS_ENSURE_ARG_POINTER(_retval);
274  *_retval = ToNewCString( NS_LITERAL_CSTRING("AllAccess") );
275  return NS_OK;
276 }
277 
278 NS_IMETHODIMP sbScriptableFilterResult::CanGetProperty( const nsIID * iid,
279  const PRUnichar *propertyName,
280  char **_retval)
281 {
282  TRACE(("sbScriptableFilterResult::CanGetProperty() - %s",
283  NS_LossyConvertUTF16toASCII(propertyName).BeginReading()));
284 
285  NS_ENSURE_ARG_POINTER(_retval);
286  *_retval = ToNewCString( NS_LITERAL_CSTRING("AllAccess") );
287  return NS_OK;
288 }
289 
290 NS_IMETHODIMP sbScriptableFilterResult::CanSetProperty( const nsIID * iid,
291  const PRUnichar *propertyName,
292  char **_retval)
293 {
294  TRACE(("sbScriptableFilterResult::CanSetProperty() - %s",
295  NS_LossyConvertUTF16toASCII(propertyName).BeginReading()));
296 
297  NS_ENSURE_ARG_POINTER(_retval);
298  *_retval = ToNewCString( NS_LITERAL_CSTRING("NoAccess") );
299  return NS_OK;
300 }
301 
302 // ---------------------------------------------------------------------------
303 //
304 // nsIClassInfo
305 //
306 // ---------------------------------------------------------------------------
308 
return NS_OK
readonly attribute sbILibraryConstraint constraint
NS_DECL_ISUPPORTS NS_DECL_NSICLASSINFO NS_DECL_NSISECURITYCHECKEDCOMPONENT NS_DECL_SBISCRIPTABLEFILTERRESULT NS_IMETHOD GetClassName(char **aClassName)
nsISecurityCheckedComponent
NS_IMETHOD GetScriptableFlags(PRUint32 *aScriptableFlags)
nsRefPtr< sbRemotePlayer > mPlayer
#define SB_PROPERTY_GENRE
Control the filter settings on a media list.
this _dialogInput val(dateText)
The result of a filtering expression in a library.
#define SB_PROPERTY_ARTISTNAME
NS_IMETHOD Equality(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval val, PRBool *_retval)
NS_IMPL_ISUPPORTS3_CI(sbScriptableFilterResult, sbIScriptableFilterResult, nsISecurityCheckedComponent, nsIXPCScriptable) sbScriptableFilterResult
#define SB_PROPERTY_ALBUMNAME
sbIJobCancelable NS_DECL_CLASSINFO(sbGstreamerMediaInspector)
#define SB_PROPERTY_YEAR
nsCOMPtr< sbIFilterableMediaListView > mListView
#define LOG(args)
#define SB_IMPL_CLASSINFO_INTERFACES_ONLY(_class)
#define TRACE(args)
Array filter(tab.attributes, function(aAttr){return(_this.xulAttributes.indexOf(aAttr.name) >-1);}).forEach(tab.removeAttribute
readonly attribute nsISupports items