sbMediaListEnumeratorWrapper.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
28 
29 // Mozilla includes
30 #include <nsComponentManagerUtils.h>
31 #include <nsIClassInfoImpl.h>
32 #include <nsIProgrammingLanguage.h>
33 #include <nsMemory.h>
34 #include <nsStringAPI.h>
35 #include <nsThreadUtils.h>
36 
37 // Songbird includes
39 #include <sbStandardProperties.h>
41 
42 // Songbird interfaces
43 #include <sbILibrary.h>
44 #include <sbIMediaItem.h>
45 #include <sbIMediaList.h>
46 
50  nsIClassInfo);
54  nsIClassInfo);
57 
58 sbMediaListEnumeratorWrapper::sbMediaListEnumeratorWrapper()
59 : mMonitor(nsnull)
60 {
61 }
62 
63 sbMediaListEnumeratorWrapper::~sbMediaListEnumeratorWrapper()
64 {
65  if(mMonitor) {
66  nsAutoMonitor::DestroyMonitor(mMonitor);
67  }
68 }
69 
70 NS_IMETHODIMP
71 sbMediaListEnumeratorWrapper::Initialize(
72  nsISimpleEnumerator * aEnumerator,
74 {
75  NS_ENSURE_ARG_POINTER(aEnumerator);
76 
77  mMonitor =
78  nsAutoMonitor::NewMonitor("sbMediaListEnumeratorWrapper::mMonitor");
79  NS_ENSURE_TRUE(mMonitor, NS_ERROR_OUT_OF_MEMORY);
80 
81  mEnumerator = aEnumerator;
82 
83  if(aListener) {
84  nsCOMPtr<nsIThread> target;
85  nsresult rv = NS_GetMainThread(getter_AddRefs(target));
86  NS_ENSURE_SUCCESS(rv, rv);
87 
88  rv = do_GetProxyForObject(target,
90  aListener,
91  NS_PROXY_SYNC | NS_PROXY_ALWAYS,
92  getter_AddRefs(mListener));
93  NS_ENSURE_SUCCESS(rv, rv);
94  }
95 
96  return NS_OK;
97 }
98 
99 // nsISimpleEnumerator implementation
100 NS_IMETHODIMP
101 sbMediaListEnumeratorWrapper::HasMoreElements(PRBool *aMore)
102 {
103  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
104  NS_ENSURE_TRUE(mEnumerator, NS_ERROR_NOT_INITIALIZED);
105 
106  nsAutoMonitor mon(mMonitor);
107  nsresult rv = mEnumerator->HasMoreElements(aMore);
108  NS_ENSURE_SUCCESS(rv, rv);
109 
110  if(mListener) {
111  nsCOMPtr<nsISimpleEnumerator> grip(mEnumerator);
112  nsCOMPtr<sbIMediaListEnumeratorWrapperListener> listener(mListener);
113  mon.Exit();
114 
115  rv = listener->OnHasMoreElements(grip, *aMore);
116  NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "onHasMoreElements returned an error");
117  }
118 
119  return NS_OK;
120 }
121 
122 NS_IMETHODIMP
123 sbMediaListEnumeratorWrapper::GetNext(nsISupports ** aItem)
124 {
125  NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
126  NS_ENSURE_ARG_POINTER(aItem);
127 
128  nsAutoMonitor mon(mMonitor);
129 
130  nsCOMPtr<nsISupports> supports;
131  nsresult rv = mEnumerator->GetNext(getter_AddRefs(supports));
132  NS_ENSURE_SUCCESS(rv, rv);
133 
134  nsCOMPtr<sbIMediaItem> mediaItem;
135  nsCOMPtr<sbIIndexedMediaItem> indexedMediaItem = do_QueryInterface(supports, &rv);
136 
137  if(NS_SUCCEEDED(rv)) {
138  rv = indexedMediaItem->GetMediaItem(getter_AddRefs(mediaItem));
139  NS_ENSURE_SUCCESS(rv, rv);
140  }
141  else if(rv == NS_ERROR_NO_INTERFACE) {
142  mediaItem = do_QueryInterface(supports, &rv);
143  NS_ENSURE_SUCCESS(rv, rv);
144  }
145  else {
146  return rv;
147  }
148 
149  nsString itemGuid;
150  rv = mediaItem->GetGuid(itemGuid);
151  NS_ENSURE_SUCCESS(rv, rv);
152 
153  nsCOMPtr<sbILibrary> library;
154  rv = mediaItem->GetLibrary(getter_AddRefs(library));
155  NS_ENSURE_SUCCESS(rv, rv);
156 
157  nsString libraryGuid;
158  rv = library->GetGuid(libraryGuid);
159  NS_ENSURE_SUCCESS(rv, rv);
160 
161  nsString propertyValue(libraryGuid);
162  propertyValue += NS_LITERAL_STRING(",");
163  propertyValue += itemGuid;
164 
165  // Do not send notification when the item is updated.
166  nsCOMPtr<sbILocalDatabaseMediaItem> item =
167  do_QueryInterface(mediaItem, &rv);
168  NS_ENSURE_SUCCESS(rv, rv);
169 
170  item->SetSuppressNotifications(PR_TRUE);
171  rv =
172  mediaItem->SetProperty(NS_LITERAL_STRING(SB_PROPERTY_DOWNLOAD_STATUS_TARGET),
173  propertyValue);
174  NS_ENSURE_SUCCESS(rv, rv);
175  item->SetSuppressNotifications(PR_FALSE);
176 
177  NS_ADDREF(*aItem = mediaItem);
178 
179  if(mListener) {
180  nsCOMPtr<nsISimpleEnumerator> grip(mEnumerator);
181  nsCOMPtr<sbIMediaListEnumeratorWrapperListener> listener(mListener);
182  mon.Exit();
183 
184  rv = listener->OnGetNext(grip, mediaItem);
185  NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "onGetNext returned an error");
186  }
187 
188  return NS_OK;
189 }
return NS_OK
NS_IMPL_CI_INTERFACE_GETTER3(sbMediaListEnumeratorWrapper, sbIMediaListEnumeratorWrapper, nsISimpleEnumerator, nsIClassInfo)
nsISimpleEnumerator wrapper which sets downloadStatusTarget on the items being enumerated.
NS_IMPL_THREADSAFE_CI(sbMediaListEnumeratorWrapper)
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
NS_IMPL_THREADSAFE_ISUPPORTS3(sbMediaListEnumeratorWrapper, sbIMediaListEnumeratorWrapper, nsISimpleEnumerator, nsIClassInfo)
The enumerator wrapper is used to set downloadStatusTarget when adding / inserting items from one med...
NS_DECL_CLASSINFO(sbMediaListEnumeratorWrapper)
Listener interface for use with sbIMediaListEnumeratorWrapper.
#define SB_PROPERTY_DOWNLOAD_STATUS_TARGET