sbPlaybackHistoryEntry.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 "sbPlaybackHistoryEntry.h"
28 
29 #include <nsIClassInfoImpl.h>
30 #include <nsIMutableArray.h>
31 #include <nsIProgrammingLanguage.h>
32 #include <nsIStringEnumerator.h>
33 
34 #include <nsArrayUtils.h>
35 #include <nsAutoLock.h>
36 #include <nsComponentManagerUtils.h>
37 #include <nsServiceManagerUtils.h>
38 
39 #include <sbIPlaybackHistoryService.h>
40 
41 #include <sbPropertiesCID.h>
42 
45 
47 : mLock(nsnull)
48 , mEntryId(-1)
49 , mTimestamp(0)
50 , mDuration(0)
51 {
52  MOZ_COUNT_CTOR(sbPlaybackHistoryEntry);
53 }
54 
56 {
57  MOZ_COUNT_DTOR(sbPlaybackHistoryEntry);
58 
59  if(mLock) {
60  nsAutoLock::DestroyLock(mLock);
61  }
62 }
63 
64 NS_IMETHODIMP
65 sbPlaybackHistoryEntry::GetEntryId(PRInt64 *aEntryId)
66 {
67  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
68  NS_ENSURE_ARG_POINTER(aEntryId);
69 
70  nsAutoLock lock(mLock);
71  *aEntryId = mEntryId;
72 
73  return NS_OK;
74 }
75 
76 void
77 sbPlaybackHistoryEntry::SetEntryId(PRInt64 aEntryId)
78 {
79  NS_ENSURE_TRUE(mLock, );
80 
81  nsAutoLock lock(mLock);
82 
83  if(mEntryId != -1)
84  return;
85 
86  mEntryId = aEntryId;
87 
88  return;
89 }
90 
91 NS_IMETHODIMP
92 sbPlaybackHistoryEntry::GetItem(sbIMediaItem * *aItem)
93 {
94  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
95  NS_ENSURE_ARG_POINTER(aItem);
96 
97  nsAutoLock lock(mLock);
98  NS_IF_ADDREF(*aItem = mItem);
99 
100  return NS_OK;
101 }
102 
103 NS_IMETHODIMP
104 sbPlaybackHistoryEntry::GetTimestamp(PRInt64 *aTimestamp)
105 {
106  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
107  NS_ENSURE_ARG_POINTER(aTimestamp);
108 
109  nsAutoLock lock(mLock);
110  *aTimestamp = mTimestamp;
111 
112  return NS_OK;
113 }
114 
115 NS_IMETHODIMP
116 sbPlaybackHistoryEntry::GetDuration(PRInt64 *aDuration)
117 {
118  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
119  NS_ENSURE_ARG_POINTER(aDuration);
120 
121  nsAutoLock lock(mLock);
122  *aDuration = mDuration;
123 
124  return NS_OK;
125 }
126 
127 NS_IMETHODIMP
128 sbPlaybackHistoryEntry::GetAnnotations(sbIPropertyArray * *aAnnotations)
129 {
130  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
131  NS_ENSURE_ARG_POINTER(aAnnotations);
132 
133  nsAutoLock lock(mLock);
134  NS_IF_ADDREF(*aAnnotations = mAnnotations);
135 
136  return NS_OK;
137 }
138 
139 NS_IMETHODIMP
140 sbPlaybackHistoryEntry::GetAnnotation(const nsAString & aAnnotationId,
141  nsAString & _retval)
142 {
143  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
144 
145  _retval.Truncate();
146 
147  nsAutoLock lock(mLock);
148 
149  if(!mAnnotations)
150  return NS_OK;
151 
152  nsresult rv = mAnnotations->GetPropertyValue(aAnnotationId, _retval);
153  NS_ENSURE_SUCCESS(rv, rv);
154 
155  return NS_OK;
156 }
157 
158 NS_IMETHODIMP
159 sbPlaybackHistoryEntry::HasAnnotation(const nsAString & aAnnotationId,
160  PRBool *_retval)
161 {
162  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
163 
164  *_retval = PR_FALSE;
165 
166  nsAutoLock lock(mLock);
167 
168  if(!mAnnotations)
169  return NS_OK;
170 
171  nsString value;
172  nsresult rv = mAnnotations->GetPropertyValue(aAnnotationId, value);
173 
174  if(NS_SUCCEEDED(rv))
175  *_retval = PR_TRUE;
176 
177  return NS_OK;
178 }
179 
180 NS_IMETHODIMP
181 sbPlaybackHistoryEntry::SetAnnotation(const nsAString & aAnnotationId,
182  const nsAString & aAnnotationValue)
183 {
184  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
185 
186  nsAutoLock lock(mLock);
187 
188  nsresult rv = NS_ERROR_UNEXPECTED;
189  nsCOMPtr<sbIMutablePropertyArray> annotations;
190 
191  if(!mAnnotations) {
192  annotations = do_CreateInstance(SB_MUTABLEPROPERTYARRAY_CONTRACTID, &rv);
193  NS_ENSURE_SUCCESS(rv, rv);
194 
195  mAnnotations = do_QueryInterface(annotations, &rv);
196  NS_ENSURE_SUCCESS(rv, rv);
197  }
198  else {
199  annotations = do_QueryInterface(mAnnotations, &rv);
200  NS_ENSURE_SUCCESS(rv, rv);
201  }
202 
203  rv = annotations->AppendProperty(aAnnotationId, aAnnotationValue);
204  NS_ENSURE_SUCCESS(rv, rv);
205 
206  // The entry exists in the history service so we can actually set the
207  // annotation. If the entry doesn't exist yet, the annotations will
208  // automatically be set when the entry is added to the history.
209  if(mEntryId != -1) {
210  nsCOMPtr<sbIPlaybackHistoryService> history =
211  do_GetService(SB_PLAYBACKHISTORYSERVICE_CONTRACTID, &rv);
212  NS_ENSURE_SUCCESS(rv, rv);
213 
214  rv = history->AddOrUpdateAnnotation(mEntryId,
215  aAnnotationId,
216  aAnnotationValue);
217  NS_ENSURE_SUCCESS(rv, rv);
218  }
219 
220  return NS_OK;
221 }
222 
223 NS_IMETHODIMP
224 sbPlaybackHistoryEntry::RemoveAnnotation(const nsAString &aAnnotationId)
225 {
226  NS_ENSURE_TRUE(mLock, NS_ERROR_NOT_INITIALIZED);
227 
228  nsAutoLock lock(mLock);
229 
230  nsresult rv = NS_ERROR_UNEXPECTED;
231  nsCOMPtr<nsIMutableArray> annotations;
232 
233  if(!mAnnotations) {
234  annotations = do_CreateInstance(SB_MUTABLEPROPERTYARRAY_CONTRACTID, &rv);
235  NS_ENSURE_SUCCESS(rv, rv);
236 
237  mAnnotations = do_QueryInterface(annotations, &rv);
238  NS_ENSURE_SUCCESS(rv, rv);
239  }
240  else {
241  annotations = do_QueryInterface(mAnnotations, &rv);
242  NS_ENSURE_SUCCESS(rv, rv);
243  }
244 
245  PRUint32 length = 0;
246  rv = annotations->GetLength(&length);
247  NS_ENSURE_SUCCESS(rv, rv);
248 
249  for(PRUint32 current = 0; current < length; ++current) {
250  nsCOMPtr<sbIProperty> property =
251  do_QueryElementAt(annotations, current, &rv);
252  NS_ENSURE_SUCCESS(rv, rv);
253 
254  nsString id;
255  rv = property->GetId(id);
256  NS_ENSURE_SUCCESS(rv, rv);
257 
258  if(aAnnotationId.Equals(id)) {
259  rv = annotations->RemoveElementAt(current);
260  NS_ENSURE_SUCCESS(rv, rv);
261 
262  break;
263  }
264  }
265 
266  if(mEntryId != -1) {
267  nsCOMPtr<sbIPlaybackHistoryService> history =
268  do_GetService(SB_PLAYBACKHISTORYSERVICE_CONTRACTID, &rv);
269  NS_ENSURE_SUCCESS(rv, rv);
270 
271  rv = history->RemoveAnnotation(mEntryId, aAnnotationId);
272  NS_ENSURE_SUCCESS(rv, rv);
273  }
274 
275  return NS_OK;
276 }
277 
278 NS_IMETHODIMP
280  PRInt64 aTimestamp,
281  PRInt64 aDuration,
282  sbIPropertyArray *aAnnotations)
283 {
284  NS_ENSURE_ARG_POINTER(aItem);
285  NS_ENSURE_ARG_MIN(aTimestamp, 0);
286  NS_ENSURE_ARG_MIN(aDuration, 0);
287 
288  mLock = nsAutoLock::NewLock("sbPlaybackHistoryEntry::mLock");
289  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
290 
291  nsAutoLock lock(mLock);
292 
293  mItem = aItem;
294  mTimestamp = aTimestamp;
295  mDuration = aDuration;
296  mAnnotations = aAnnotations;
297 
298  return NS_OK;
299 }
return NS_OK
menuItem id
Definition: FeedWriter.js:971
var history
NS_IMPL_THREADSAFE_ISUPPORTS1(sbPlaybackHistoryEntry, sbIPlaybackHistoryEntry) sbPlaybackHistoryEntry
#define SB_MUTABLEPROPERTYARRAY_CONTRACTID
readonly attribute sbIPropertyArray annotations
Annotations to the entry. Annotations are properties.
function Init()
PRUint64 mTimestamp
#define SB_PLAYBACKHISTORYSERVICE_CONTRACTID
countRef value
Definition: FeedWriter.js:1423
~sbPlaybackHistoryEntry()
Interface that defines a single item of media in the system.
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...