sbRemoteWebPlaylist.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 
27 #include "sbRemoteAPIUtils.h"
28 #include "sbRemotePlayer.h"
29 #include "sbRemoteWebPlaylist.h"
31 
32 #include <sbClassInfoUtils.h>
33 #include <sbIPropertyManager.h>
34 #include <sbIRemoteMediaList.h>
35 #include <sbIMediaListView.h>
36 #include <sbIMediaListViewSelection.h>
37 #include <sbPropertiesCID.h>
38 #include <sbIPropertyBuilder.h>
39 
40 #include <nsAutoPtr.h>
41 #include <nsIDOMElement.h>
42 #include <nsServiceManagerUtils.h>
43 #include <nsStringGlue.h>
44 #include <prlog.h>
45 
46 /*
47  * To log this module, set the following environment variable:
48  * NSPR_LOG_MODULES=sbRemoteWebPlaylist:5
49  */
50 #ifdef PR_LOGGING
51 static PRLogModuleInfo* gRemoteWebPlaylistLog = nsnull;
52 #endif
53 
54 #undef LOG
55 #define LOG(args) PR_LOG(gRemoteWebPlaylistLog, PR_LOG_WARN, args)
56 
57 const static char* sPublicWProperties[] =
58 {
59  "site:mediaList",
60  "site:hidden"
61 };
62 
63 const static char* sPublicRProperties[] =
64 {
65  // sbIRemoteWebPlaylist
66  "site:selection",
67  "site:mediaList",
68  "site:hidden",
69 
70  // nsIClassInfo
71  "classinfo:classDescription",
72  "classinfo:contractID",
73  "classinfo:classID",
74  "classinfo:implementationLanguage",
75  "classinfo:flags"
76 };
77 
78 const static char* sPublicMethods[] =
79 {
80  // sbIRemoteWebPlaylist
81  "site:appendColumn",
82  "site:setSelectionByIndex",
83  "site:showColumnBefore",
84 
85  // sbIPlaylistWidget
86  "site:showColumn",
87  "site:hideColumn",
88  "site:getColumnCount",
89  "site:clearColumns",
90  "site:insertColumnBefore",
91  "site:removeColumn",
92  "site:getColumnPropertyIDByIndex",
93  "site:setSortColumn",
94  "internal:getView"
95 };
96 
103 
104 NS_IMPL_CI_INTERFACE_GETTER4( sbRemoteWebPlaylist,
109 
110 SB_IMPL_CLASSINFO_INTERFACES_ONLY( sbRemoteWebPlaylist )
111 
112 SB_IMPL_SECURITYCHECKEDCOMP_INIT( sbRemoteWebPlaylist )
113 
114 sbRemoteWebPlaylist::sbRemoteWebPlaylist( sbRemotePlayer *aRemotePlayer,
115  sbIPlaylistWidget *aWebPlaylistWidget,
116  sbITabBrowserTab *aTabBrowserTab ) :
117  mInitialized(PR_FALSE),
118  mRemotePlayer(aRemotePlayer),
119  mPlaylistWidget(aWebPlaylistWidget),
120  mOwnerTab(aTabBrowserTab)
121 {
122  NS_ASSERTION( aRemotePlayer, "Null remote player!" );
123  NS_ASSERTION( aWebPlaylistWidget, "Null playlist widget!" );
124  NS_ASSERTION( aTabBrowserTab, "Null playlist widget!" );
125 
126 #ifdef PR_LOGGING
127  if (!gRemoteWebPlaylistLog) {
128  gRemoteWebPlaylistLog = PR_NewLogModule("sbRemoteWebPlaylist");
129  }
130  LOG(("sbRemoteWebPlaylist::sbRemoteWebPlaylist()"));
131 #endif
132 }
133 
134 // ---------------------------------------------------------------------------
135 //
136 // sbISecurityAggregator
137 //
138 // ---------------------------------------------------------------------------
139 
140 NS_IMETHODIMP
141 sbRemoteWebPlaylist::GetRemotePlayer(sbIRemotePlayer * *aRemotePlayer)
142 {
143  NS_ENSURE_STATE(mRemotePlayer);
144  NS_ENSURE_ARG_POINTER(aRemotePlayer);
145 
146  nsresult rv;
147  *aRemotePlayer = nsnull;
148 
149  nsCOMPtr<sbIRemotePlayer> remotePlayer;
150 
151  rv = mRemotePlayer->QueryInterface( NS_GET_IID( sbIRemotePlayer ), getter_AddRefs( remotePlayer ) );
152  NS_ENSURE_SUCCESS( rv, rv );
153 
154  remotePlayer.swap( *aRemotePlayer );
155 
156  return NS_OK;
157 }
158 
159 // ---------------------------------------------------------------------------
160 //
161 // sbIRemoteWebPlaylist
162 //
163 // ---------------------------------------------------------------------------
164 
165 NS_IMETHODIMP
166 sbRemoteWebPlaylist::SetMediaList( sbIRemoteMediaList *aMediaList )
167 {
168  LOG(("sbRemoteWebPlaylist::SetMediaList()"));
169  NS_ENSURE_ARG_POINTER(aMediaList);
170 
171  nsresult rv;
172  nsCOMPtr<sbIMediaListView> mediaListView;
173  rv = aMediaList->GetView( getter_AddRefs(mediaListView) );
174  if (!mediaListView) {
175  // Create a view from the RemoteMediaList passed in
176  nsCOMPtr<sbIMediaList> webMediaList = do_QueryInterface(aMediaList);
177  NS_ENSURE_TRUE( webMediaList, NS_ERROR_INVALID_ARG );
178 
179  rv = webMediaList->CreateView( nsnull, getter_AddRefs(mediaListView) );
180  NS_ENSURE_TRUE( mediaListView, NS_ERROR_FAILURE );
181  }
182 
183  rv = mOwnerTab->DisableScan();
184  NS_ENSURE_SUCCESS( rv, rv );
185 
186  rv = mOwnerTab->ClearOuterPlaylist();
187  NS_ENSURE_SUCCESS( rv, rv );
188 
189  // Bind the view to the playlist widget
190  rv = Bind( mediaListView, nsnull, PR_FALSE, PR_FALSE );
191  NS_ENSURE_SUCCESS( rv, rv );
192 
193  nsCOMPtr<nsIDOMElement> playlist ( do_QueryInterface( mPlaylistWidget,
194  &rv ) );
195  NS_ENSURE_SUCCESS( rv, rv );
196 
197  //mOwnerTab->SetOuterPlaylistShowing(PR_TRUE);
198  mOwnerTab->ShowOuterPlaylist();
199  //rv = playlist->SetAttribute( NS_LITERAL_STRING("hidden"),
200  //NS_LITERAL_STRING("false") );
201  NS_ENSURE_SUCCESS( rv, rv );
202 
203  // Update the commands because the widget has been reset and
204  // needs to know the useDefaultCommands setting
205  RescanCommands();
206 
207  return NS_OK;
208 }
209 
210 NS_IMETHODIMP
211 sbRemoteWebPlaylist::GetMediaList( sbIRemoteMediaList **aMediaList )
212 {
213  LOG(("sbRemoteWebPlaylist::GetMediaList()"));
214  NS_ENSURE_ARG_POINTER(aMediaList);
215 
216  // get the view from the web playlist widget
217  nsresult rv;
218  nsCOMPtr<sbIMediaListView> mediaListView;
219  rv = GetListView( getter_AddRefs(mediaListView) );
220  NS_ENSURE_SUCCESS( rv, rv );
221 
222  // the getter call can succeed and return null, better check it
223  if (!mediaListView) {
224  LOG(("sbRemoteWebPlaylist::GetMediaList() - NO MEDIA LIST VIEW" ));
225  // return error condition so a webpage can catch it.
226  return NS_ERROR_FAILURE;
227  }
228 
229  // create wrapped medialist, this addrefs
230  return SB_WrapMediaList( mRemotePlayer, mediaListView, aMediaList );
231 }
232 
233 NS_IMETHODIMP
235 {
236  LOG(("sbRemoteWebPlaylist::GetSelection()"));
237 
238  NS_ASSERTION( mPlaylistWidget, "Playlist widget not set" );
239 
240  // Get the current view
241  nsCOMPtr<sbIMediaListView> mediaListView;
242  nsresult rv = GetListView( getter_AddRefs(mediaListView) );
243  NS_ENSURE_SUCCESS( rv, rv );
244 
245  nsCOMPtr<sbIMediaListViewSelection> viewSelection;
246  mediaListView->GetSelection( getter_AddRefs(viewSelection) );
247  NS_ENSURE_TRUE( viewSelection, NS_ERROR_UNEXPECTED );
248 
249  // get enumeration of selected items
250  nsCOMPtr<nsISimpleEnumerator> selection;
251  rv = viewSelection->GetSelectedIndexedMediaItems( getter_AddRefs(selection) );
252  NS_ENSURE_SUCCESS( rv, rv );
253 
254  // wrap it so it's safe
255  nsRefPtr<sbRemoteWrappingSimpleEnumerator> wrapped(
257  NS_ENSURE_TRUE( wrapped, NS_ERROR_OUT_OF_MEMORY );
258 
259  rv = wrapped->Init();
260  NS_ENSURE_SUCCESS( rv, rv );
261 
262  NS_ADDREF( *aSelection = wrapped );
263  return NS_OK;
264 }
265 
266 NS_IMETHODIMP
267 sbRemoteWebPlaylist::SetSelectionByIndex( PRUint32 aIndex, PRBool aSelected )
268 {
269  LOG(("sbRemoteWebPlaylist::SetSelectionByIndex()"));
270 
271  NS_ASSERTION( mPlaylistWidget, "Playlist widget not set" );
272 
273  nsCOMPtr<sbIMediaListView> mediaListView;
274  nsresult rv = GetListView( getter_AddRefs(mediaListView) );
275  NS_ENSURE_SUCCESS( rv, rv );
276 
277  nsCOMPtr<sbIMediaListViewSelection> viewSelection;
278  mediaListView->GetSelection( getter_AddRefs(viewSelection) );
279  NS_ENSURE_TRUE( viewSelection, NS_ERROR_UNEXPECTED );
280 
281  if (aSelected) {
282  rv = viewSelection->Select(aIndex);
283  NS_ENSURE_SUCCESS(rv, rv);
284  }
285  else {
286  rv = viewSelection->Clear(aIndex);
287  NS_ENSURE_SUCCESS(rv, rv);
288  }
289 
290  return NS_OK;
291 }
292 
293 nsresult
295 {
296  LOG(("sbRemoteWebPlaylist::SetSelectionByIndex()"));
297  NS_ENSURE_ARG_POINTER(aWebPlaylist);
298  NS_IF_ADDREF( *aWebPlaylist = mPlaylistWidget );
299  return NS_OK;
300 }
301 
302 nsresult
303 sbRemoteWebPlaylist::SetHidden(PRBool aHide)
304 {
305  LOG(("sbRemoteWebPlaylist::SetHidden()"));
306 
307  nsresult rv = NS_OK;
308 
309  if (aHide) {
310  rv = mOwnerTab->HideOuterPlaylist();
311  } else {
312  rv = mOwnerTab->ShowOuterPlaylist();
313  }
314 
315  return rv;
316 }
317 
318 nsresult
319 sbRemoteWebPlaylist::GetHidden(PRBool* aHidden)
320 {
321  NS_ENSURE_ARG_POINTER(aHidden);
322  LOG(("sbRemoteWebPlaylist::GetHidden()"));
323 
324  nsresult rv = NS_OK;
325  PRBool showing;
326  rv = mOwnerTab->GetOuterPlaylistShowing(&showing);
327  NS_ENSURE_SUCCESS(rv, rv);
328 
329  *aHidden = !showing;
330 
331  return rv;
332 }
static nsresult SB_WrapMediaList(sbRemotePlayer *aRemotePlayer, sbIMediaList *aMediaList, sbIMediaList **aRemoteMediaList)
An interface to control a media list from remote web pages.
#define SB_IMPL_SECURITYCHECKEDCOMP_INIT(_class)
Definition: sbRemoteAPI.h:82
return NS_OK
static const char * sPublicMethods[]
NS_IMPL_ISUPPORTS5(sbRemoteWebPlaylist, nsIClassInfo, nsISecurityCheckedComponent, sbISecurityAggregator, sbIRemoteWebPlaylist, sbIPlaylistWidget) NS_IMPL_CI_INTERFACE_GETTER4(sbRemoteWebPlaylist
readonly attribute sbIRemotePlayer remotePlayer
This interface is a composition of many of our other interfaces with the goal of exposing a more web-...
nsresult GetPlaylistWidget(sbIPlaylistWidget **aWebPlaylist)
nsISecurityCheckedComponent
static const char * sPublicWProperties[]
A marker interface for objects that aggregate the security mixin.
#define LOG(args)
nsCOMPtr< sbIPlaylistWidget > mPlaylistWidget
NS_IMETHOD GetSelection(sbLocalDatabaseMediaListViewSelectionState **aState)
static const char * sPublicRProperties[]
nsRefPtr< sbRemotePlayer > mRemotePlayer
#define SB_IMPL_CLASSINFO_INTERFACES_ONLY(_class)
nsCOMPtr< sbITabBrowserTab > mOwnerTab
readonly attribute nsISimpleEnumerator selection
An interface to control a media list from remote web pages.