28 #ifdef METRICS_ENABLED
33 #include <nsICategoryManager.h>
34 #include <nsIConverterInputStream.h>
35 #include <nsIInputStream.h>
36 #include <nsILocalFile.h>
37 #include <nsIMutableArray.h>
38 #include <nsIObserverService.h>
40 #include <nsIVariant.h>
41 #include <nsIWeakReferenceUtils.h>
43 #include <nsArrayUtils.h>
44 #include <nsAutoLock.h>
45 #include <nsCOMArray.h>
46 #include <nsComponentManagerUtils.h>
47 #include <nsNetUtil.h>
48 #include <nsThreadUtils.h>
49 #include <nsServiceManagerUtils.h>
50 #include <nsXPCOMCID.h>
54 #include <sbILibrary.h>
55 #include <sbILibraryManager.h>
56 #include <sbIMediacoreEvent.h>
57 #include <sbIMediacoreEventTarget.h>
58 #include <sbIMediacoreManager.h>
59 #include <sbIMediaList.h>
60 #include <sbIPlaybackHistoryEntry.h>
61 #include <sbIPropertyArray.h>
62 #include <sbIPropertyInfo.h>
63 #include <sbIPropertyManager.h>
64 #include <sbISQLBuilder.h>
76 #define NS_APPSTARTUP_CATEGORY "app-startup"
77 #define NS_APPSTARTUP_TOPIC "app-startup"
79 #define CONVERTER_BUFFER_SIZE 8192
81 #define PLAYBACKHISTORY_DB_GUID "playbackhistory@songbirdnest.com"
83 "chrome://songbird/content/mediacore/playback/history/playbackhistoryservice.sql"
85 #define PLAYBACKHISTORY_ENTRIES_TABLE "playback_history_entries"
86 #define PLAYBACKHISTORY_ANNOTATIONS_TABLE "playback_history_entry_annotations"
87 #define PLAYBACKHISTORY_PROPERTIES_TABLE "properties"
88 #define PLAYBACKHISTORY_COUNT_ENTRIES "count(entry_id)"
90 #define ENTRY_ID_COLUMN "entry_id"
91 #define LIBRARY_GUID_COLUMN "library_guid"
92 #define MEDIA_ITEM_GUID_COLUMN "media_item_guid"
93 #define PLAY_TIME_COLUMN "play_time"
94 #define PLAY_DURATION_COLUMN "play_duration"
96 #define ENTRY_ID_COLUMN "entry_id"
97 #define PROPERTY_ID_COLUMN "property_id"
98 #define PROPERTY_NAME_COLUMN "property_name"
99 #define OBJ_COLUMN "obj"
100 #define OBJ_SORTABLE "obj_sortable"
105 static already_AddRefed<nsILocalFile>
109 nsCOMPtr<nsIProperties> ds =
110 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
111 NS_ENSURE_SUCCESS(rv, nsnull);
114 rv = ds->Get(
"ProfD", NS_GET_IID(nsILocalFile), (
void**)&file);
115 NS_ENSURE_SUCCESS(rv, nsnull);
117 rv = file->AppendRelativePath(NS_LITERAL_STRING(
"db"));
119 NS_WARNING(
"AppendRelativePath failed!");
137 : mCurrentlyTracking(PR_FALSE)
138 , mCurrentStartTime(0)
139 , mCurrentPauseTime(0)
153 const char* aLoaderStr,
155 const nsModuleComponentInfo *aInfo)
157 NS_ENSURE_ARG_POINTER(aCompMgr);
158 NS_ENSURE_ARG_POINTER(aPath);
159 NS_ENSURE_ARG_POINTER(aLoaderStr);
160 NS_ENSURE_ARG_POINTER(aType);
161 NS_ENSURE_ARG_POINTER(aInfo);
163 nsresult rv = NS_ERROR_UNEXPECTED;
164 nsCOMPtr<nsICategoryManager> categoryManager =
165 do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
166 NS_ENSURE_SUCCESS(rv, rv);
172 PR_TRUE, PR_TRUE, nsnull);
173 NS_ENSURE_SUCCESS(rv, rv);
178 PLDHashOperator PR_CALLBACK
183 NS_ASSERTION(aKey,
"Null key in hashtable!");
184 NS_ASSERTION(aEntry,
"Null entry in hashtable!");
186 nsCOMArray<sbIPlaybackHistoryListener>*
array =
187 static_cast<nsCOMArray<sbIPlaybackHistoryListener>*
>(aUserData);
189 PRBool success = array->AppendObject(aEntry);
190 NS_ENSURE_TRUE(success, PL_DHASH_STOP);
192 return PL_DHASH_NEXT;
198 nsresult rv = NS_ERROR_UNEXPECTED;
200 do_GetService(
"@mozilla.org/observer-service;1", &rv);
201 NS_ENSURE_SUCCESS(rv, rv);
203 rv = observerService->AddObserver(
this,
206 NS_ENSURE_SUCCESS(rv, rv);
209 rv = observerService->AddObserver(
this,
212 NS_ENSURE_SUCCESS(rv, rv);
214 mMonitor = nsAutoMonitor::NewMonitor(
"sbPlaybackHistoryService::mMonitor");
215 NS_ENSURE_TRUE(mMonitor, NS_ERROR_OUT_OF_MEMORY);
218 NS_ENSURE_SUCCESS(rv, rv);
220 PRBool success = mLibraries.Init();
221 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
223 success = mListeners.Init();
224 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
226 success = mPropertyDBIDToID.Init();
227 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
229 success = mPropertyIDToDBID.Init();
230 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
232 nsCOMPtr<nsISupportsWeakReference> weakRef =
234 NS_ENSURE_SUCCESS(rv, rv);
236 rv = weakRef->GetWeakReference(getter_AddRefs(mMediacoreManager));
237 NS_ENSURE_SUCCESS(rv, rv);
240 do_QueryReferent(mMediacoreManager, &rv);
241 NS_ENSURE_SUCCESS(rv, rv);
243 rv = eventTarget->AddListener(
this);
244 NS_ENSURE_SUCCESS(rv, rv);
246 #ifdef METRICS_ENABLED
249 NS_ENSURE_SUCCESS(rv, rv);
258 nsresult rv = NS_ERROR_UNEXPECTED;
260 NS_NAMED_LITERAL_STRING(playbackHistoryEntriesTableName,
263 NS_NAMED_LITERAL_STRING(playbackHistoryAnnotationsTableName,
266 NS_NAMED_LITERAL_STRING(playbackHistoryCountEntries,
275 NS_NAMED_LITERAL_STRING(propertiesTableName,
280 NS_NAMED_LITERAL_STRING(objColumn,
OBJ_COLUMN);
281 NS_NAMED_LITERAL_STRING(objSortableColumn,
OBJ_SORTABLE);
283 nsCOMPtr<sbISQLInsertBuilder> insertBuilder =
285 NS_ENSURE_SUCCESS(rv, rv);
288 rv = insertBuilder->SetIntoTableName(playbackHistoryEntriesTableName);
289 NS_ENSURE_SUCCESS(rv, rv);
291 rv = insertBuilder->AddColumn(libraryGuidColumn);
292 NS_ENSURE_SUCCESS(rv, rv);
294 rv = insertBuilder->AddColumn(mediaItemGuidColumn);
295 NS_ENSURE_SUCCESS(rv, rv);
297 rv = insertBuilder->AddColumn(playTimeColumn);
298 NS_ENSURE_SUCCESS(rv, rv);
300 rv = insertBuilder->AddColumn(playDurationColumn);
301 NS_ENSURE_SUCCESS(rv, rv);
303 rv = insertBuilder->AddValueParameter();
304 NS_ENSURE_SUCCESS(rv, rv);
306 rv = insertBuilder->AddValueParameter();
307 NS_ENSURE_SUCCESS(rv, rv);
309 rv = insertBuilder->AddValueParameter();
310 NS_ENSURE_SUCCESS(rv, rv);
312 rv = insertBuilder->AddValueParameter();
313 NS_ENSURE_SUCCESS(rv, rv);
315 rv = insertBuilder->ToString(mAddEntryQuery);
316 NS_ENSURE_SUCCESS(rv, rv);
318 rv = insertBuilder->Reset();
319 NS_ENSURE_SUCCESS(rv, rv);
322 rv = insertBuilder->Reset();
323 NS_ENSURE_SUCCESS(rv, rv);
325 rv = insertBuilder->SetIntoTableName(playbackHistoryAnnotationsTableName);
326 NS_ENSURE_SUCCESS(rv, rv);
328 rv = insertBuilder->AddColumn(entryIdColumn);
329 NS_ENSURE_SUCCESS(rv, rv);
331 rv = insertBuilder->AddValueRaw(NS_LITERAL_STRING(
"(select entry_id from playback_history_entries where library_guid = ? AND media_item_guid = ? AND play_time = ?)"));
332 NS_ENSURE_SUCCESS(rv, rv);
334 rv = insertBuilder->AddColumn(propertyIdColumn);
335 NS_ENSURE_SUCCESS(rv, rv);
337 rv = insertBuilder->AddValueParameter();
338 NS_ENSURE_SUCCESS(rv, rv);
340 rv = insertBuilder->AddColumn(objColumn);
341 NS_ENSURE_SUCCESS(rv, rv);
343 rv = insertBuilder->AddValueParameter();
344 NS_ENSURE_SUCCESS(rv, rv);
346 rv = insertBuilder->AddColumn(objSortableColumn);
347 NS_ENSURE_SUCCESS(rv, rv);
349 rv = insertBuilder->AddValueParameter();
350 NS_ENSURE_SUCCESS(rv, rv);
352 rv = insertBuilder->ToString(mAddAnnotationQuery);
353 NS_ENSURE_SUCCESS(rv, rv);
356 mInsertAnnotationQuery.AssignLiteral(
"insert into ");
357 mInsertAnnotationQuery.Append(playbackHistoryAnnotationsTableName);
358 mInsertAnnotationQuery.AppendLiteral(
" (");
360 mInsertAnnotationQuery.Append(entryIdColumn);
361 mInsertAnnotationQuery.AppendLiteral(
",");
363 mInsertAnnotationQuery.Append(propertyIdColumn);
364 mInsertAnnotationQuery.AppendLiteral(
",");
366 mInsertAnnotationQuery.Append(objColumn);
367 mInsertAnnotationQuery.AppendLiteral(
",");
369 mInsertAnnotationQuery.Append(objSortableColumn);
371 mInsertAnnotationQuery.AppendLiteral(
" )");
372 mInsertAnnotationQuery.AppendLiteral(
" values (?, ?, ?, ?)");
375 mUpdateAnnotationQuery.AssignLiteral(
"update ");
376 mUpdateAnnotationQuery.Append(playbackHistoryAnnotationsTableName);
377 mUpdateAnnotationQuery.AppendLiteral(
" set ");
379 mUpdateAnnotationQuery.Append(propertyIdColumn);
380 mUpdateAnnotationQuery.AppendLiteral(
" = ?, ");
382 mUpdateAnnotationQuery.Append(objColumn);
383 mUpdateAnnotationQuery.AppendLiteral(
" = ?, ");
385 mUpdateAnnotationQuery.Append(objSortableColumn);
386 mUpdateAnnotationQuery.AppendLiteral(
" = ? ");
388 mUpdateAnnotationQuery.AppendLiteral(
" where entry_id = ?");
391 mRemoveAnnotationQuery.AssignLiteral(
"delete from ");
392 mRemoveAnnotationQuery.Append(playbackHistoryAnnotationsTableName);
393 mRemoveAnnotationQuery.AppendLiteral(
" where entry_id = ? AND property_id = ?");
396 mIsAnnotationPresentQuery.AssignLiteral(
"select entry_id from ");
397 mIsAnnotationPresentQuery.Append(playbackHistoryAnnotationsTableName);
398 mIsAnnotationPresentQuery.AppendLiteral(
" where entry_id = ? and property_id = ?");
401 nsCOMPtr<sbISQLSelectBuilder> selectBuilder =
403 NS_ENSURE_SUCCESS(rv, rv);
405 rv = selectBuilder->SetBaseTableName(playbackHistoryEntriesTableName);
406 NS_ENSURE_SUCCESS(rv, rv);
408 rv = selectBuilder->AddColumn(EmptyString(), playbackHistoryCountEntries);
409 NS_ENSURE_SUCCESS(rv, rv);
411 rv = selectBuilder->ToString(mGetEntryCountQuery);
412 NS_ENSURE_SUCCESS(rv, rv);
415 rv = insertBuilder->Reset();
416 NS_ENSURE_SUCCESS(rv, rv);
418 rv = insertBuilder->SetIntoTableName(propertiesTableName);
419 NS_ENSURE_SUCCESS(rv, rv);
421 rv = insertBuilder->AddColumn(propertyNameColumn);
422 NS_ENSURE_SUCCESS(rv, rv);
424 rv = insertBuilder->AddValueParameter();
425 NS_ENSURE_SUCCESS(rv, rv);
427 rv = insertBuilder->ToString(mInsertPropertyIDQuery);
428 NS_ENSURE_SUCCESS(rv, rv);
431 rv = selectBuilder->Reset();
432 NS_ENSURE_SUCCESS(rv, rv);
434 rv = selectBuilder->SetBaseTableName(playbackHistoryEntriesTableName);
435 NS_ENSURE_SUCCESS(rv, rv);
437 rv = selectBuilder->AddColumn(EmptyString(), entryIdColumn);
438 NS_ENSURE_SUCCESS(rv, rv);
440 rv = selectBuilder->AddColumn(EmptyString(), libraryGuidColumn);
441 NS_ENSURE_SUCCESS(rv, rv);
443 rv = selectBuilder->AddColumn(EmptyString(), mediaItemGuidColumn);
444 NS_ENSURE_SUCCESS(rv, rv);
446 rv = selectBuilder->AddColumn(EmptyString(), playTimeColumn);
447 NS_ENSURE_SUCCESS(rv, rv);
449 rv = selectBuilder->AddColumn(EmptyString(), playDurationColumn);
450 NS_ENSURE_SUCCESS(rv, rv);
452 rv = selectBuilder->AddOrder(EmptyString(), playTimeColumn, PR_FALSE);
453 NS_ENSURE_SUCCESS(rv, rv);
455 rv = selectBuilder->SetLimitIsParameter(PR_TRUE);
456 NS_ENSURE_SUCCESS(rv, rv);
458 rv = selectBuilder->SetOffsetIsParameter(PR_TRUE);
459 NS_ENSURE_SUCCESS(rv, rv);
461 rv = selectBuilder->ToString(mGetEntriesByIndexQuery);
462 NS_ENSURE_SUCCESS(rv, rv);
465 rv = selectBuilder->Reset();
466 NS_ENSURE_SUCCESS(rv, rv);
468 rv = selectBuilder->SetBaseTableName(playbackHistoryEntriesTableName);
469 NS_ENSURE_SUCCESS(rv, rv);
471 rv = selectBuilder->AddColumn(EmptyString(), entryIdColumn);
472 NS_ENSURE_SUCCESS(rv, rv);
474 rv = selectBuilder->AddColumn(EmptyString(), libraryGuidColumn);
475 NS_ENSURE_SUCCESS(rv, rv);
477 rv = selectBuilder->AddColumn(EmptyString(), mediaItemGuidColumn);
478 NS_ENSURE_SUCCESS(rv, rv);
480 rv = selectBuilder->AddColumn(EmptyString(), playTimeColumn);
481 NS_ENSURE_SUCCESS(rv, rv);
483 rv = selectBuilder->AddColumn(EmptyString(), playDurationColumn);
484 NS_ENSURE_SUCCESS(rv, rv);
486 rv = selectBuilder->AddOrder(EmptyString(), playTimeColumn, PR_TRUE);
487 NS_ENSURE_SUCCESS(rv, rv);
489 rv = selectBuilder->SetLimitIsParameter(PR_TRUE);
490 NS_ENSURE_SUCCESS(rv, rv);
492 rv = selectBuilder->SetOffsetIsParameter(PR_TRUE);
493 NS_ENSURE_SUCCESS(rv, rv);
495 rv = selectBuilder->ToString(mGetEntriesByIndexQueryAscending);
496 NS_ENSURE_SUCCESS(rv, rv);
498 rv = selectBuilder->Reset();
499 NS_ENSURE_SUCCESS(rv, rv);
502 rv = selectBuilder->Reset();
503 NS_ENSURE_SUCCESS(rv, rv);
505 rv = selectBuilder->SetBaseTableName(playbackHistoryEntriesTableName);
506 NS_ENSURE_SUCCESS(rv, rv);
508 rv = selectBuilder->AddColumn(EmptyString(), entryIdColumn);
509 NS_ENSURE_SUCCESS(rv, rv);
511 rv = selectBuilder->AddColumn(EmptyString(), libraryGuidColumn);
512 NS_ENSURE_SUCCESS(rv, rv);
514 rv = selectBuilder->AddColumn(EmptyString(), mediaItemGuidColumn);
515 NS_ENSURE_SUCCESS(rv, rv);
517 rv = selectBuilder->AddColumn(EmptyString(), playTimeColumn);
518 NS_ENSURE_SUCCESS(rv, rv);
520 rv = selectBuilder->AddColumn(EmptyString(), playDurationColumn);
521 NS_ENSURE_SUCCESS(rv, rv);
523 rv = selectBuilder->AddOrder(EmptyString(), playTimeColumn, PR_FALSE);
524 NS_ENSURE_SUCCESS(rv, rv);
526 nsCOMPtr<sbISQLBuilderCriterion> criterionPlayTimeMin;
527 rv = selectBuilder->CreateMatchCriterionParameter(
531 getter_AddRefs(criterionPlayTimeMin));
532 NS_ENSURE_SUCCESS(rv, rv);
534 nsCOMPtr<sbISQLBuilderCriterion> criterionPlayTimeMax;
535 rv = selectBuilder->CreateMatchCriterionParameter(
539 getter_AddRefs(criterionPlayTimeMax));
540 NS_ENSURE_SUCCESS(rv, rv);
542 nsCOMPtr<sbISQLBuilderCriterion> criterionPlayTimeRange;
543 rv = selectBuilder->CreateAndCriterion(criterionPlayTimeMin,
544 criterionPlayTimeMax,
545 getter_AddRefs(criterionPlayTimeRange));
546 NS_ENSURE_SUCCESS(rv, rv);
548 rv = selectBuilder->AddCriterion(criterionPlayTimeRange);
549 NS_ENSURE_SUCCESS(rv, rv);
551 rv = selectBuilder->ToString(mGetEntriesByTimestampQuery);
552 NS_ENSURE_SUCCESS(rv, rv);
555 rv = selectBuilder->Reset();
556 NS_ENSURE_SUCCESS(rv, rv);
558 rv = selectBuilder->SetBaseTableName(playbackHistoryEntriesTableName);
559 NS_ENSURE_SUCCESS(rv, rv);
561 rv = selectBuilder->AddColumn(EmptyString(), entryIdColumn);
562 NS_ENSURE_SUCCESS(rv, rv);
564 rv = selectBuilder->AddColumn(EmptyString(), libraryGuidColumn);
565 NS_ENSURE_SUCCESS(rv, rv);
567 rv = selectBuilder->AddColumn(EmptyString(), mediaItemGuidColumn);
568 NS_ENSURE_SUCCESS(rv, rv);
570 rv = selectBuilder->AddColumn(EmptyString(), playTimeColumn);
571 NS_ENSURE_SUCCESS(rv, rv);
573 rv = selectBuilder->AddColumn(EmptyString(), playDurationColumn);
574 NS_ENSURE_SUCCESS(rv, rv);
576 rv = selectBuilder->AddOrder(EmptyString(), playTimeColumn, PR_FALSE);
577 NS_ENSURE_SUCCESS(rv, rv);
579 rv = selectBuilder->AddCriterion(criterionPlayTimeRange);
580 NS_ENSURE_SUCCESS(rv, rv);
582 rv = selectBuilder->ToString(mGetEntriesByTimestampQueryAscending);
583 NS_ENSURE_SUCCESS(rv, rv);
586 rv = selectBuilder->Reset();
587 NS_ENSURE_SUCCESS(rv, rv);
589 rv = selectBuilder->SetBaseTableName(playbackHistoryEntriesTableName);
590 NS_ENSURE_SUCCESS(rv, rv);
592 rv = selectBuilder->AddColumn(EmptyString(), entryIdColumn);
593 NS_ENSURE_SUCCESS(rv, rv);
595 rv = selectBuilder->AddColumn(EmptyString(), libraryGuidColumn);
596 NS_ENSURE_SUCCESS(rv, rv);
598 rv = selectBuilder->AddColumn(EmptyString(), mediaItemGuidColumn);
599 NS_ENSURE_SUCCESS(rv, rv);
601 rv = selectBuilder->AddColumn(EmptyString(), playTimeColumn);
602 NS_ENSURE_SUCCESS(rv, rv);
604 rv = selectBuilder->AddColumn(EmptyString(), playDurationColumn);
605 NS_ENSURE_SUCCESS(rv, rv);
607 rv = selectBuilder->AddOrder(EmptyString(), playTimeColumn, PR_FALSE);
608 NS_ENSURE_SUCCESS(rv, rv);
610 rv = selectBuilder->ToString(mGetAllEntriesQuery);
611 NS_ENSURE_SUCCESS(rv, rv);
614 rv = selectBuilder->Reset();
615 NS_ENSURE_SUCCESS(rv, rv);
617 rv = selectBuilder->SetBaseTableName(playbackHistoryAnnotationsTableName);
618 NS_ENSURE_SUCCESS(rv, rv);
620 rv = selectBuilder->AddColumn(EmptyString(), propertyIdColumn);
621 NS_ENSURE_SUCCESS(rv, rv);
623 rv = selectBuilder->AddColumn(EmptyString(), objColumn);
624 NS_ENSURE_SUCCESS(rv, rv);
626 nsCOMPtr<sbISQLBuilderCriterion> criterionEntryId;
627 rv = selectBuilder->CreateMatchCriterionParameter(
631 getter_AddRefs(criterionEntryId));
632 NS_ENSURE_SUCCESS(rv, rv);
634 rv = selectBuilder->AddCriterion(criterionEntryId);
635 NS_ENSURE_SUCCESS(rv, rv);
637 rv = selectBuilder->ToString(mGetAnnotationsForEntryQuery);
638 NS_ENSURE_SUCCESS(rv, rv);
641 nsCOMPtr<sbISQLDeleteBuilder> deleteBuilder =
643 NS_ENSURE_SUCCESS(rv, rv);
645 rv = deleteBuilder->SetTableName(playbackHistoryEntriesTableName);
646 NS_ENSURE_SUCCESS(rv, rv);
648 nsCOMPtr<sbISQLBuilderCriterionIn> criterionIn;
649 rv = deleteBuilder->CreateMatchCriterionIn(EmptyString(),
651 getter_AddRefs(criterionIn));
652 NS_ENSURE_SUCCESS(rv, rv);
654 rv = selectBuilder->Reset();
655 NS_ENSURE_SUCCESS(rv, rv);
657 rv = selectBuilder->SetBaseTableName(playbackHistoryEntriesTableName);
658 NS_ENSURE_SUCCESS(rv, rv);
660 rv = selectBuilder->AddColumn(EmptyString(), entryIdColumn);
661 NS_ENSURE_SUCCESS(rv, rv);
663 nsCOMPtr<sbISQLBuilderCriterion> criterionLibraryGuid;
664 rv = selectBuilder->CreateMatchCriterionParameter(
668 getter_AddRefs(criterionLibraryGuid));
669 NS_ENSURE_SUCCESS(rv, rv);
671 nsCOMPtr<sbISQLBuilderCriterion> criterionMediaItemGuid;
672 rv = selectBuilder->CreateMatchCriterionParameter(
676 getter_AddRefs(criterionMediaItemGuid));
677 NS_ENSURE_SUCCESS(rv, rv);
679 nsCOMPtr<sbISQLBuilderCriterion> criterionPlayTime;
680 rv = selectBuilder->CreateMatchCriterionParameter(
684 getter_AddRefs(criterionPlayTime));
685 NS_ENSURE_SUCCESS(rv, rv);
687 nsCOMPtr<sbISQLBuilderCriterion> criterionLeft;
688 rv = selectBuilder->CreateAndCriterion(criterionLibraryGuid,
689 criterionMediaItemGuid,
690 getter_AddRefs(criterionLeft));
691 NS_ENSURE_SUCCESS(rv, rv);
693 nsCOMPtr<sbISQLBuilderCriterion> criterionRight;
694 rv = selectBuilder->CreateAndCriterion(criterionLeft,
696 getter_AddRefs(criterionRight));
697 NS_ENSURE_SUCCESS(rv, rv);
699 rv = selectBuilder->AddCriterion(criterionRight);
700 NS_ENSURE_SUCCESS(rv, rv);
702 rv = criterionIn->AddSubquery(selectBuilder);
703 NS_ENSURE_SUCCESS(rv, rv);
705 rv = deleteBuilder->AddCriterion(criterionIn);
706 NS_ENSURE_SUCCESS(rv, rv);
708 rv = deleteBuilder->ToString(mRemoveEntriesQuery);
709 NS_ENSURE_SUCCESS(rv, rv);
712 rv = deleteBuilder->Reset();
713 NS_ENSURE_SUCCESS(rv, rv);
715 rv = deleteBuilder->SetTableName(playbackHistoryEntriesTableName);
716 NS_ENSURE_SUCCESS(rv, rv);
718 rv = selectBuilder->CreateMatchCriterionParameter(
722 getter_AddRefs(criterionEntryId));
723 NS_ENSURE_SUCCESS(rv, rv);
725 rv = deleteBuilder->AddCriterion(criterionEntryId);
726 NS_ENSURE_SUCCESS(rv, rv);
728 rv = deleteBuilder->ToString(mRemoveAnnotationsQuery);
729 NS_ENSURE_SUCCESS(rv, rv);
732 rv = deleteBuilder->Reset();
733 NS_ENSURE_SUCCESS(rv, rv);
735 rv = deleteBuilder->SetTableName(playbackHistoryEntriesTableName);
736 NS_ENSURE_SUCCESS(rv, rv);
738 rv = deleteBuilder->ToString(mRemoveAllEntriesQuery);
739 NS_ENSURE_SUCCESS(rv, rv);
742 rv = deleteBuilder->Reset();
743 NS_ENSURE_SUCCESS(rv, rv);
745 rv = deleteBuilder->SetTableName(playbackHistoryAnnotationsTableName);
746 NS_ENSURE_SUCCESS(rv, rv);
748 rv = deleteBuilder->ToString(mRemoveAllAnnotationsQuery);
749 NS_ENSURE_SUCCESS(rv, rv);
757 NS_ENSURE_ARG_POINTER(aQuery);
759 NS_NAMED_LITERAL_STRING(playbackHistoryDatabaseGUID,
762 nsresult rv = NS_ERROR_UNEXPECTED;
763 nsCOMPtr<sbIDatabaseQuery> query =
765 NS_ENSURE_SUCCESS(rv, rv);
767 rv = query->SetAsyncQuery(PR_FALSE);
768 NS_ENSURE_SUCCESS(rv, rv);
770 rv = query->SetDatabaseGUID(playbackHistoryDatabaseGUID);
771 NS_ENSURE_SUCCESS(rv, rv);
773 query.forget(aQuery);
783 NS_ENSURE_ARG_POINTER(aAnnotations);
784 NS_ENSURE_ARG(aEntryId != -1);
786 nsCOMPtr<sbIDatabaseQuery> query;
788 NS_ENSURE_SUCCESS(rv, rv);
790 rv = query->AddQuery(mGetAnnotationsForEntryQuery);
791 NS_ENSURE_SUCCESS(rv, rv);
793 rv = query->BindInt64Parameter(0, aEntryId);
794 NS_ENSURE_SUCCESS(rv, rv);
797 rv = query->Execute(&dbOk);
798 NS_ENSURE_SUCCESS(rv, rv);
799 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
801 nsCOMPtr<sbIDatabaseResult> result;
802 rv = query->GetResultObject(getter_AddRefs(result));
803 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
805 PRUint32 rowCount = 0;
806 rv = result->GetRowCount(&rowCount);
807 NS_ENSURE_SUCCESS(rv, rv);
809 nsCOMPtr<sbIMutablePropertyArray> annotations =
811 NS_ENSURE_SUCCESS(rv, rv);
813 for(PRUint32 current = 0; current < rowCount; ++current) {
814 nsString propertyDBIDStr;
815 rv = result->GetRowCell(current, 0, propertyDBIDStr);
816 NS_ENSURE_SUCCESS(rv, rv);
818 nsString annotationValue;
819 rv = result->GetRowCell(current, 1, annotationValue);
820 NS_ENSURE_SUCCESS(rv, rv);
822 PRUint32 propertyId = propertyDBIDStr.ToInteger(&rv);
823 NS_ENSURE_SUCCESS(rv, rv);
825 nsString annotationId;
826 PRBool success = mPropertyDBIDToID.Get(propertyId, &annotationId);
827 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
829 rv = annotations->AppendProperty(annotationId, annotationValue);
830 NS_ENSURE_SUCCESS(rv, rv);
833 nsCOMPtr<sbIPropertyArray> immutableAnnotations =
834 do_QueryInterface(annotations, &rv);
835 NS_ENSURE_SUCCESS(rv, rv);
837 immutableAnnotations.forget(aAnnotations);
848 NS_ENSURE_ARG_POINTER(aResult);
849 NS_ENSURE_ARG_POINTER(aEntry);
851 PRUint32 rowCount = 0;
852 nsresult rv = aResult->GetRowCount(&rowCount);
853 NS_ENSURE_SUCCESS(rv, rv);
854 NS_ENSURE_TRUE(aRow < rowCount, NS_ERROR_INVALID_ARG);
857 rv = aResult->GetRowCell(aRow, 0, entryIdStr);
858 NS_ENSURE_SUCCESS(rv, rv);
860 nsString libraryGuid;
861 rv = aResult->GetRowCell(aRow, 1, libraryGuid);
862 NS_ENSURE_SUCCESS(rv, rv);
864 nsString mediaItemGuid;
865 rv = aResult->GetRowCell(aRow, 2, mediaItemGuid);
866 NS_ENSURE_SUCCESS(rv, rv);
869 rv = aResult->GetRowCell(aRow, 3, playTime);
870 NS_ENSURE_SUCCESS(rv, rv);
872 nsString playDuration;
873 rv = aResult->GetRowCell(aRow, 4, playDuration);
874 NS_ENSURE_SUCCESS(rv, rv);
877 NS_ENSURE_SUCCESS(rv, rv);
880 NS_ENSURE_SUCCESS(rv, rv);
882 nsCOMPtr<sbIMediaItem> item;
883 rv =
GetItem(libraryGuid, mediaItemGuid, getter_AddRefs(item));
884 NS_ENSURE_SUCCESS(rv, rv);
887 NS_ENSURE_SUCCESS(rv, rv);
889 nsCOMPtr<sbIPropertyArray> annotations;
891 NS_ENSURE_SUCCESS(rv, rv);
893 nsCOMPtr<sbIPlaybackHistoryEntry>
entry;
894 rv = CreateEntry(item, timestamp, duration, annotations, getter_AddRefs(entry));
895 NS_ENSURE_SUCCESS(rv, rv);
897 entry->SetEntryId(entryId);
898 entry.forget(aEntry);
907 NS_ENSURE_ARG_POINTER(aResult);
908 NS_ENSURE_ARG_POINTER(aEntries);
910 nsresult rv = NS_ERROR_UNEXPECTED;
911 nsCOMPtr<nsIMutableArray> mutableArray =
913 NS_ENSURE_SUCCESS(rv, rv);
915 PRUint32 rowCount = 0;
916 rv = aResult->GetRowCount(&rowCount);
917 NS_ENSURE_SUCCESS(rv, rv);
919 for(PRUint32 currentRow = 0; currentRow < rowCount; ++currentRow) {
920 nsCOMPtr<sbIPlaybackHistoryEntry>
entry;
927 if (rv == NS_ERROR_NOT_AVAILABLE)
930 rv = mutableArray->AppendElement(entry, PR_FALSE);
931 NS_ENSURE_SUCCESS(rv, rv);
934 nsCOMPtr<nsIArray>
array = do_QueryInterface(mutableArray);
935 array.forget(aEntries);
943 nsresult rv = NS_ERROR_UNEXPECTED;
946 NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
949 dbFileName.AppendLiteral(
".db");
951 rv = file->AppendRelativePath(dbFileName);
952 NS_ENSURE_SUCCESS(rv, rv);
954 PRBool exists = PR_FALSE;
955 rv = file->Exists(&exists);
956 NS_ENSURE_SUCCESS(rv, rv);
962 nsCOMPtr<sbIDatabaseQuery> query =
964 NS_ENSURE_SUCCESS(rv, rv);
967 NS_ENSURE_SUCCESS(rv, rv);
969 nsCOMPtr<nsIURI> schemaURI;
970 rv = NS_NewURI(getter_AddRefs(schemaURI), NS_LITERAL_CSTRING(
SCHEMA_URL));
971 NS_ENSURE_SUCCESS(rv, rv);
973 nsCOMPtr<nsIInputStream> input;
974 rv = NS_OpenURI(getter_AddRefs(input), schemaURI);
975 NS_ENSURE_SUCCESS(rv, rv);
977 nsCOMPtr<nsIConverterInputStream> converterStream =
978 do_CreateInstance(
"@mozilla.org/intl/converter-input-stream;1", &rv);
979 NS_ENSURE_SUCCESS(rv, rv);
981 rv = converterStream->Init(input,
984 nsIConverterInputStream::DEFAULT_REPLACEMENT_CHARACTER);
985 NS_ENSURE_SUCCESS(rv, rv);
987 nsCOMPtr<nsIUnicharInputStream> unichar =
988 do_QueryInterface(converterStream, &rv);
989 NS_ENSURE_SUCCESS(rv, rv);
994 NS_ENSURE_SUCCESS(rv, rv);
996 NS_ASSERTION(read,
"Schema file zero bytes?");
998 rv = unichar->Close();
999 NS_ENSURE_SUCCESS(rv, rv);
1001 NS_NAMED_LITERAL_STRING(colonNewline,
";\n");
1003 PRInt32 posStart = 0;
1004 PRInt32 posEnd = response.Find(colonNewline, posStart);
1005 while (posEnd >= 0) {
1006 rv = query->AddQuery(Substring(response, posStart, posEnd - posStart));
1007 NS_ENSURE_SUCCESS(rv, rv);
1008 posStart = posEnd + 2;
1009 posEnd = response.Find(colonNewline, posStart);
1013 rv = query->Execute(&dbOk);
1014 NS_ENSURE_SUCCESS(rv, rv);
1015 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
1024 NS_ENSURE_ARG_POINTER(aQuery);
1025 NS_ENSURE_ARG_POINTER(aEntry);
1027 nsCOMPtr<sbIMediaItem> item;
1028 nsresult rv = aEntry->GetItem(getter_AddRefs(item));
1029 NS_ENSURE_SUCCESS(rv, rv);
1031 nsCOMPtr<sbILibrary> library;
1032 rv = item->GetLibrary(getter_AddRefs(library));
1033 NS_ENSURE_SUCCESS(rv, rv);
1035 nsString libraryGuid;
1036 rv = library->GetGuid(libraryGuid);
1037 NS_ENSURE_SUCCESS(rv, rv);
1039 rv = aQuery->BindStringParameter(0, libraryGuid);
1040 NS_ENSURE_SUCCESS(rv, rv);
1043 rv = item->GetGuid(itemGuid);
1044 NS_ENSURE_SUCCESS(rv, rv);
1046 rv = aQuery->BindStringParameter(1, itemGuid);
1047 NS_ENSURE_SUCCESS(rv, rv);
1049 PRTime timestamp = 0;
1050 rv = aEntry->GetTimestamp(×tamp);
1051 NS_ENSURE_SUCCESS(rv, rv);
1054 PRUint32 len = PR_snprintf(buf,
sizeof(buf),
"%lld", timestamp);
1056 NS_ConvertASCIItoUTF16 timestampString(buf, len);
1057 rv = aQuery->BindStringParameter(2, timestampString);
1058 NS_ENSURE_SUCCESS(rv, rv);
1061 rv = aEntry->GetDuration(&duration);
1062 NS_ENSURE_SUCCESS(rv, rv);
1066 len = PR_snprintf(buf,
sizeof(buf),
"%lld", duration);
1067 NS_ConvertASCIItoUTF16 durationString(buf, len);
1069 rv = aQuery->BindStringParameter(3, durationString);
1070 NS_ENSURE_SUCCESS(rv, rv);
1073 rv = aQuery->BindNullParameter(3);
1074 NS_ENSURE_SUCCESS(rv, rv);
1077 rv = aQuery->AddQuery(NS_LITERAL_STRING(
"select last_insert_rowid()"));
1087 NS_ENSURE_ARG_POINTER(aQuery);
1088 NS_ENSURE_ARG_POINTER(aEntry);
1090 nsCOMPtr<sbIPropertyArray> annotations;
1091 nsresult rv = aEntry->GetAnnotations(getter_AddRefs(annotations));
1092 NS_ENSURE_SUCCESS(rv, rv);
1098 nsCOMPtr<sbIPropertyManager> propMan =
1100 NS_ENSURE_SUCCESS(rv, rv);
1102 nsCOMPtr<sbIMediaItem> item;
1103 rv = aEntry->GetItem(getter_AddRefs(item));
1104 NS_ENSURE_SUCCESS(rv, rv);
1106 nsCOMPtr<sbILibrary> library;
1107 rv = item->GetLibrary(getter_AddRefs(library));
1108 NS_ENSURE_SUCCESS(rv, rv);
1110 nsString libraryGuid;
1111 rv = library->GetGuid(libraryGuid);
1112 NS_ENSURE_SUCCESS(rv, rv);
1115 rv = item->GetGuid(itemGuid);
1116 NS_ENSURE_SUCCESS(rv, rv);
1118 PRTime timestamp = 0;
1119 rv = aEntry->GetTimestamp(×tamp);
1120 NS_ENSURE_SUCCESS(rv, rv);
1123 PRUint32 len = PR_snprintf(buf,
sizeof(buf),
"%lld", timestamp);
1124 NS_ConvertASCIItoUTF16 timestampString(buf, len);
1126 PRUint32 length = 0;
1127 rv = annotations->GetLength(&length);
1128 NS_ENSURE_SUCCESS(rv, rv);
1130 for(PRUint32 current = 0; current < length; ++current) {
1131 nsCOMPtr<sbIProperty> annotation;
1132 rv = annotations->GetPropertyAt(current, getter_AddRefs(annotation));
1133 NS_ENSURE_SUCCESS(rv, rv);
1136 rv = annotation->GetId(
id);
1137 NS_ENSURE_SUCCESS(rv, rv);
1140 rv = annotation->GetValue(value);
1141 NS_ENSURE_SUCCESS(rv, rv);
1143 PRUint32 propertyId;
1145 NS_ENSURE_SUCCESS(rv, rv);
1147 nsCOMPtr<sbIPropertyInfo> propertyInfo;
1148 rv = propMan->GetPropertyInfo(
id, getter_AddRefs(propertyInfo));
1149 NS_ENSURE_SUCCESS(rv, rv);
1151 nsAutoString sortableValue;
1152 rv = propertyInfo->MakeSortable(value, sortableValue);
1153 NS_ENSURE_SUCCESS(rv, rv);
1155 rv = aQuery->AddQuery(mAddAnnotationQuery);
1156 NS_ENSURE_SUCCESS(rv, rv);
1158 rv = aQuery->BindStringParameter(0, libraryGuid);
1159 NS_ENSURE_SUCCESS(rv, rv);
1161 rv = aQuery->BindStringParameter(1, itemGuid);
1162 NS_ENSURE_SUCCESS(rv, rv);
1164 rv = aQuery->BindStringParameter(2, timestampString);
1165 NS_ENSURE_SUCCESS(rv, rv);
1167 rv = aQuery->BindInt32Parameter(3, propertyId);
1168 NS_ENSURE_SUCCESS(rv, rv);
1170 rv = aQuery->BindStringParameter(4, value);
1171 NS_ENSURE_SUCCESS(rv, rv);
1173 rv = aQuery->BindStringParameter(5, sortableValue);
1174 NS_ENSURE_SUCCESS(rv, rv);
1184 NS_ENSURE_ARG_POINTER(aQuery);
1185 NS_ENSURE_ARG_POINTER(aEntry);
1187 nsCOMPtr<sbIMediaItem> item;
1188 nsresult rv = aEntry->GetItem(getter_AddRefs(item));
1189 NS_ENSURE_SUCCESS(rv, rv);
1191 nsCOMPtr<sbILibrary> library;
1192 rv = item->GetLibrary(getter_AddRefs(library));
1193 NS_ENSURE_SUCCESS(rv, rv);
1195 nsString libraryGuid;
1196 rv = library->GetGuid(libraryGuid);
1197 NS_ENSURE_SUCCESS(rv, rv);
1199 rv = aQuery->BindStringParameter(0, libraryGuid);
1200 NS_ENSURE_SUCCESS(rv, rv);
1203 rv = item->GetGuid(itemGuid);
1204 NS_ENSURE_SUCCESS(rv, rv);
1206 rv = aQuery->BindStringParameter(1, itemGuid);
1207 NS_ENSURE_SUCCESS(rv, rv);
1209 PRTime timestamp = 0;
1210 rv = aEntry->GetTimestamp(×tamp);
1211 NS_ENSURE_SUCCESS(rv, rv);
1214 PRUint32 len = PR_snprintf(buf,
sizeof(buf),
"%lld", timestamp);
1216 NS_ConvertASCIItoUTF16 timestampString(buf, len);
1217 rv = aQuery->BindStringParameter(2, timestampString);
1218 NS_ENSURE_SUCCESS(rv, rv);
1225 const nsAString &aItemGuid,
1228 NS_ENSURE_ARG_POINTER(aItem);
1230 nsresult rv = NS_ERROR_UNEXPECTED;
1231 nsCOMPtr<sbILibrary> library;
1232 if(!mLibraries.Get(aLibraryGuid, getter_AddRefs(library))) {
1235 NS_ENSURE_SUCCESS(rv, rv);
1237 rv = libraryManager->GetLibrary(aLibraryGuid, getter_AddRefs(library));
1238 NS_ENSURE_SUCCESS(rv, rv);
1240 PRBool success = mLibraries.Put(aLibraryGuid, library);
1241 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1244 nsCOMPtr<sbIMediaItem> item;
1245 rv = library->GetMediaItem(aItemGuid, getter_AddRefs(item));
1246 NS_ENSURE_SUCCESS(rv, rv);
1255 PRUint32 *aPropertyDBID)
1257 NS_ENSURE_ARG_POINTER(aPropertyDBID);
1260 if (!mPropertyIDToDBID.Get(aPropertyID, aPropertyDBID)) {
1262 NS_ENSURE_SUCCESS (rv, rv);
1270 PRUint32 *aPropertyDBID)
1272 NS_ENSURE_ARG_POINTER(aPropertyDBID);
1274 nsCOMPtr<sbIDatabaseQuery> query;
1276 NS_ENSURE_SUCCESS(rv, rv);
1278 rv = query->AddQuery(mInsertPropertyIDQuery);
1279 NS_ENSURE_SUCCESS(rv, rv);
1281 rv = query->BindStringParameter(0, aPropertyID);
1282 NS_ENSURE_SUCCESS(rv, rv);
1284 rv = query->AddQuery(NS_LITERAL_STRING(
"select last_insert_rowid()"));
1285 NS_ENSURE_SUCCESS(rv, rv);
1288 rv = query->Execute(&dbOk);
1289 NS_ENSURE_SUCCESS(rv, rv);
1290 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
1292 nsCOMPtr<sbIDatabaseResult> result;
1293 rv = query->GetResultObject(getter_AddRefs(result));
1294 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
1296 nsAutoString propertyDBIDStr;
1297 rv = result->GetRowCell(0, 0, propertyDBIDStr);
1298 NS_ENSURE_SUCCESS(rv, rv);
1300 PRUint32 propertyDBID = propertyDBIDStr.ToInteger(&rv);
1301 NS_ENSURE_SUCCESS(rv, rv);
1303 *aPropertyDBID = propertyDBID;
1305 mPropertyDBIDToID.Put(propertyDBID, nsAutoString(aPropertyID));
1306 mPropertyIDToDBID.Put(nsAutoString(aPropertyID), propertyDBID);
1314 nsresult rv = NS_ERROR_UNEXPECTED;
1316 nsCOMPtr<sbISQLSelectBuilder> builder =
1318 NS_ENSURE_SUCCESS(rv, rv);
1321 NS_ENSURE_SUCCESS(rv, rv);
1324 NS_ENSURE_SUCCESS(rv, rv);
1327 NS_ENSURE_SUCCESS(rv, rv);
1330 rv = builder->ToString(sql);
1331 NS_ENSURE_SUCCESS(rv, rv);
1333 nsCOMPtr<sbIDatabaseQuery> query;
1335 NS_ENSURE_SUCCESS(rv, rv);
1337 rv = query->AddQuery(sql);
1338 NS_ENSURE_SUCCESS(rv, rv);
1341 rv = query->Execute(&dbOk);
1342 NS_ENSURE_SUCCESS(rv, rv);
1343 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
1345 nsCOMPtr<sbIDatabaseResult> result;
1346 rv = query->GetResultObject(getter_AddRefs(result));
1347 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
1350 rv = result->GetRowCount(&rowCount);
1351 NS_ENSURE_SUCCESS(rv, rv);
1353 for (PRUint32
i = 0;
i < rowCount;
i++) {
1354 nsAutoString propertyDBIDStr;
1355 rv = result->GetRowCell(
i, 0, propertyDBIDStr);
1356 NS_ENSURE_SUCCESS(rv, rv);
1358 PRUint32 propertyDBID = propertyDBIDStr.ToInteger(&rv);
1359 NS_ENSURE_SUCCESS(rv, rv);
1361 nsString propertyID;
1362 rv = result->GetRowCell(
i, 1, propertyID);
1363 NS_ENSURE_SUCCESS(rv, rv);
1365 PRBool success = mPropertyDBIDToID.Put(propertyDBID, propertyID);
1366 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1368 success = mPropertyIDToDBID.Put(propertyID, propertyDBID);
1369 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1378 nsCOMArray<sbIPlaybackHistoryListener> listeners;
1381 (
void *)&listeners);
1383 nsCOMPtr<sbIPlaybackHistoryListener>
listener;
1384 PRInt32
count = listeners.Count();
1386 nsresult rv = NS_ERROR_UNEXPECTED;
1387 nsCOMPtr<nsIMutableArray>
array =
1388 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
1389 NS_ENSURE_SUCCESS(rv, rv);
1391 rv = array->AppendElement(aEntry, PR_FALSE);
1392 NS_ENSURE_SUCCESS(rv, rv);
1394 for(PRInt32 current = 0; current <
count; ++current) {
1395 rv = listeners[current]->OnEntriesAdded(array);
1398 NS_ENSURE_SUCCESS(rv, rv);
1408 nsCOMArray<sbIPlaybackHistoryListener> listeners;
1411 (
void *)&listeners);
1413 nsCOMPtr<sbIPlaybackHistoryListener>
listener;
1414 PRInt32
count = listeners.Count();
1416 for(PRInt32 current = 0; current <
count; ++current) {
1418 listeners[current]->OnEntriesAdded(aEntries);
1421 NS_ENSURE_SUCCESS(rv, rv);
1431 nsCOMArray<sbIPlaybackHistoryListener> listeners;
1434 (
void *)&listeners);
1436 nsCOMPtr<sbIPlaybackHistoryListener>
listener;
1437 PRInt32
count = listeners.Count();
1439 nsresult rv = NS_ERROR_UNEXPECTED;
1440 nsCOMPtr<nsIMutableArray>
array =
1441 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
1442 NS_ENSURE_SUCCESS(rv, rv);
1444 rv = array->AppendElement(aEntry, PR_FALSE);
1445 NS_ENSURE_SUCCESS(rv, rv);
1447 for(PRInt32 current = 0; current <
count; ++current) {
1448 rv = listeners[current]->OnEntriesUpdated(array);
1451 NS_ENSURE_SUCCESS(rv, rv);
1461 nsCOMArray<sbIPlaybackHistoryListener> listeners;
1464 (
void *)&listeners);
1466 nsCOMPtr<sbIPlaybackHistoryListener>
listener;
1467 PRInt32
count = listeners.Count();
1469 for(PRInt32 current = 0; current <
count; ++current) {
1471 listeners[current]->OnEntriesUpdated(aEntries);
1474 NS_ENSURE_SUCCESS(rv, rv);
1484 nsCOMArray<sbIPlaybackHistoryListener> listeners;
1487 (
void *)&listeners);
1489 nsCOMPtr<sbIPlaybackHistoryListener>
listener;
1490 PRInt32
count = listeners.Count();
1492 nsresult rv = NS_ERROR_UNEXPECTED;
1493 nsCOMPtr<nsIMutableArray>
array =
1494 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
1495 NS_ENSURE_SUCCESS(rv, rv);
1497 rv = array->AppendElement(aEntry, PR_FALSE);
1498 NS_ENSURE_SUCCESS(rv, rv);
1500 for(PRInt32 current = 0; current <
count; ++current) {
1501 rv = listeners[current]->OnEntriesRemoved(array);
1504 NS_ENSURE_SUCCESS(rv, rv);
1514 nsCOMArray<sbIPlaybackHistoryListener> listeners;
1517 (
void *)&listeners);
1519 nsCOMPtr<sbIPlaybackHistoryListener>
listener;
1520 PRInt32
count = listeners.Count();
1522 for(PRInt32 current = 0; current <
count; ++current) {
1524 listeners[current]->OnEntriesRemoved(aEntries);
1527 NS_ENSURE_SUCCESS(rv, rv);
1537 nsCOMArray<sbIPlaybackHistoryListener> listeners;
1540 (
void *)&listeners);
1542 nsCOMPtr<sbIPlaybackHistoryListener>
listener;
1543 PRInt32
count = listeners.Count();
1545 for(PRInt32 current = 0; current <
count; ++current) {
1547 listeners[current]->OnEntriesCleared();
1550 NS_ENSURE_SUCCESS(rv, rv);
1560 NS_ENSURE_ARG_POINTER(aEvent);
1562 nsCOMPtr<nsIVariant> variant;
1563 nsresult rv = aEvent->GetData(getter_AddRefs(variant));
1564 NS_ENSURE_SUCCESS(rv, rv);
1566 nsCOMPtr<nsISupports> supports;
1567 rv = variant->GetAsISupports(getter_AddRefs(supports));
1568 NS_ENSURE_SUCCESS(rv, rv);
1570 nsCOMPtr<sbIMediaItem> item = do_QueryInterface(supports, &rv);
1571 NS_ENSURE_SUCCESS(rv, rv);
1573 mCurrentItem = item;
1574 mCurrentlyTracking = PR_TRUE;
1576 mCurrentStartTime = 0;
1585 NS_ENSURE_ARG_POINTER(aEvent);
1587 nsCOMPtr<nsIVariant> variant;
1588 nsresult rv = aEvent->GetData(getter_AddRefs(variant));
1589 NS_ENSURE_SUCCESS(rv, rv);
1591 nsCOMPtr<nsISupports> supports;
1592 rv = variant->GetAsISupports(getter_AddRefs(supports));
1593 NS_ENSURE_SUCCESS(rv, rv);
1595 nsCOMPtr<sbIMediaListView> view = do_QueryInterface(supports, &rv);
1596 NS_ENSURE_SUCCESS(rv, rv);
1598 view.swap(mCurrentView);
1606 nsAutoMonitor mon(mMonitor);
1608 NS_ENSURE_STATE(mCurrentlyTracking);
1609 NS_ENSURE_STATE(mCurrentItem);
1610 NS_ENSURE_STATE(mCurrentStartTime);
1612 if(!mCurrentlyTracking) {
1613 return NS_ERROR_UNEXPECTED;
1617 PRTime actualPlayingTime = PR_Now() - mCurrentStartTime - mCurrentDelta;
1618 actualPlayingTime /= PR_USEC_PER_MSEC;
1625 NS_NAMED_LITERAL_STRING(PROPERTY_EXCLUDE_FROM_HISTORY,
1628 nsString durationStr;
1629 nsresult rv = mCurrentItem->GetProperty(PROPERTY_DURATION, durationStr);
1630 NS_ENSURE_SUCCESS(rv, rv);
1633 duration /= PR_USEC_PER_MSEC;
1634 NS_ENSURE_SUCCESS(rv, rv);
1636 nsString excludeFromHistoryStr;
1637 rv = mCurrentItem->GetProperty(PROPERTY_EXCLUDE_FROM_HISTORY,
1638 excludeFromHistoryStr);
1639 NS_ENSURE_SUCCESS(rv, rv);
1641 PRBool excludeFromHistory = excludeFromHistoryStr.EqualsLiteral(
"1");
1645 if((duration && (actualPlayingTime >= (duration / 2))) ||
1646 actualPlayingTime >= (240 * 1000)) {
1649 nsString playCountStr;
1650 rv = mCurrentItem->GetProperty(PROPERTY_PLAYCOUNT, playCountStr);
1651 NS_ENSURE_SUCCESS(rv, rv);
1654 if(!playCountStr.IsEmpty()) {
1656 NS_ENSURE_SUCCESS(rv, rv);
1661 rv = mCurrentItem->SetProperty(PROPERTY_PLAYCOUNT, newPlayCountStr);
1662 NS_ENSURE_SUCCESS(rv, rv);
1665 sbAutoString newLastPlayTimeStr((PRUint64)(mCurrentStartTime /
1667 rv = mCurrentItem->SetProperty(PROPERTY_LASTPLAYTIME, newLastPlayTimeStr);
1668 NS_ENSURE_SUCCESS(rv, rv);
1671 if (!excludeFromHistory) {
1672 nsCOMPtr<sbIPlaybackHistoryEntry>
entry;
1673 rv = CreateEntry(mCurrentItem,
1677 getter_AddRefs(entry));
1678 NS_ENSURE_SUCCESS(rv, rv);
1680 rv = AddEntry(entry);
1681 NS_ENSURE_SUCCESS(rv, rv);
1686 nsString skipCountStr;
1687 rv = mCurrentItem->GetProperty(PROPERTY_SKIPCOUNT, skipCountStr);
1688 NS_ENSURE_SUCCESS(rv, rv);
1690 PRUint64 skipCount = 0;
1691 if(!skipCountStr.IsEmpty()) {
1693 NS_ENSURE_SUCCESS(rv, rv);
1698 rv = mCurrentItem->SetProperty(PROPERTY_SKIPCOUNT, newSkipCountStr);
1699 NS_ENSURE_SUCCESS(rv, rv);
1702 sbAutoString newLastSkipTimeStr((PRUint64) (mCurrentStartTime /
1704 rv = mCurrentItem->SetProperty(PROPERTY_LASTSKIPTIME, newLastSkipTimeStr);
1705 NS_ENSURE_SUCCESS(rv, rv);
1724 nsAutoMonitor mon(mMonitor);
1726 mCurrentlyTracking = PR_FALSE;
1727 mCurrentStartTime = 0;
1728 mCurrentPauseTime = 0;
1730 mCurrentItem = nsnull;
1735 #ifdef METRICS_ENABLED
1737 sbPlaybackHistoryService::UpdateMetrics()
1739 nsAutoMonitor mon(mMonitor);
1741 NS_ENSURE_STATE(mCurrentView);
1742 NS_ENSURE_STATE(mCurrentItem);
1750 PRTime actualPlayingTime = PR_Now() - mCurrentStartTime - mCurrentDelta;
1751 actualPlayingTime /= PR_USEC_PER_SEC;
1757 NS_ENSURE_SUCCESS(rv, rv);
1764 NS_ENSURE_SUCCESS(rv, rv);
1767 nsString bitRateStr;
1770 if(NS_FAILED(rv) || bitRateStr.IsEmpty()) {
1775 bitRateKey.AppendLiteral(
".");
1776 bitRateKey += bitRateStr;
1781 NS_ENSURE_SUCCESS(rv, rv);
1784 nsCOMPtr<sbILibrary> library;
1785 rv = mCurrentItem->GetLibrary(getter_AddRefs(library));
1786 NS_ENSURE_SUCCESS(rv, rv);
1788 nsCOMPtr<sbIMediaList> libraryList = do_QueryInterface(library, &rv);
1789 NS_ENSURE_SUCCESS(rv, rv);
1791 nsCOMPtr<sbIMediaList> list;
1792 rv = mCurrentView->GetMediaList(getter_AddRefs(list));
1793 NS_ENSURE_SUCCESS(rv, rv);
1795 nsString listCustomType, libraryCustomType;
1798 if(NS_FAILED(rv) || listCustomType.IsEmpty()) {
1799 listCustomType.AssignLiteral(
"simple");
1804 if(NS_FAILED(rv) || listCustomType.IsEmpty()) {
1805 libraryCustomType.AssignLiteral(
"library");
1808 nsString medialistKey;
1809 if(listCustomType.EqualsLiteral(
"dynamic")) {
1812 else if(listCustomType.EqualsLiteral(
"local") ||
1813 listCustomType.EqualsLiteral(
"simple") ||
1814 libraryCustomType.EqualsLiteral(
"library")) {
1815 if(libraryList == list) {
1818 else if(libraryCustomType.EqualsLiteral(
"web")) {
1822 medialistKey.AssignLiteral(
"simple");
1826 medialistKey = listCustomType;
1832 NS_ENSURE_SUCCESS(rv, rv);
1835 nsCOMPtr<nsIURI>
uri;
1836 rv = mCurrentItem->GetContentSrc(getter_AddRefs(uri));
1837 NS_ENSURE_SUCCESS(rv, rv);
1840 rv = uri->GetScheme(scheme);
1841 NS_ENSURE_SUCCESS(rv, rv);
1843 if(scheme.IsEmpty()) {
1847 nsCString extension;
1848 nsCOMPtr<nsIURL>
url = do_QueryInterface(uri, &rv);
1849 if(NS_SUCCEEDED(rv)) {
1850 rv = url->GetFileExtension(extension);
1851 NS_ENSURE_SUCCESS(rv, rv);
1854 if(extension.IsEmpty()) {
1860 playAttemptKey.AppendLiteral(
".");
1861 playAttemptKey += NS_ConvertUTF8toUTF16(extension);
1862 playAttemptKey.AppendLiteral(
".");
1863 playAttemptKey += NS_ConvertUTF8toUTF16(scheme);
1868 NS_ENSURE_SUCCESS(rv, rv);
1871 if(scheme.EqualsLiteral(
"http") ||
1872 scheme.EqualsLiteral(
"https") ||
1873 scheme.EqualsLiteral(
"ftp") ||
1874 scheme.EqualsLiteral(
"rtsp")) {
1878 NS_ENSURE_SUCCESS(rv, rv);
1889 sbPlaybackHistoryService::Observe(
nsISupports* aSubject,
1891 const PRUnichar*
aData)
1893 NS_ENSURE_ARG_POINTER(aTopic);
1895 nsresult rv = NS_ERROR_UNEXPECTED;
1897 do_GetService(
"@mozilla.org/observer-service;1", &rv);
1898 NS_ENSURE_SUCCESS(rv, rv);
1902 NS_ENSURE_SUCCESS(rv, rv);
1905 NS_ENSURE_SUCCESS(rv, rv);
1908 NS_ENSURE_SUCCESS(rv, rv);
1913 mCurrentItem = nsnull;
1914 mCurrentView = nsnull;
1916 #ifdef METRICS_ENABLED
1920 observerService->RemoveObserver(
this,
1922 NS_ENSURE_SUCCESS(rv, rv);
1935 NS_ENSURE_ARG_POINTER(aEvent);
1937 PRUint32 eventType = 0;
1938 nsresult rv = aEvent->GetType(&eventType);
1939 NS_ENSURE_SUCCESS(rv, rv);
1941 nsAutoMonitor mon(mMonitor);
1945 if(mCurrentlyTracking && !mCurrentStartTime) {
1946 mCurrentStartTime = PR_Now();
1949 if(mCurrentlyTracking && mCurrentStartTime && mCurrentPauseTime) {
1950 mCurrentDelta = mCurrentDelta + (PR_Now() - mCurrentPauseTime);
1951 mCurrentPauseTime = 0;
1957 if(mCurrentlyTracking && mCurrentStartTime && !mCurrentPauseTime) {
1958 mCurrentPauseTime = PR_Now();
1965 if(mCurrentlyTracking && mCurrentStartTime) {
1967 NS_ASSERTION(NS_SUCCEEDED(rv),
"Entry won't be created.");
1970 NS_ENSURE_SUCCESS(rv, rv);
1978 if(mCurrentlyTracking) {
1982 NS_ENSURE_SUCCESS(rv, rv);
1987 NS_ENSURE_SUCCESS(rv, rv);
1994 NS_ENSURE_SUCCESS(rv, rv);
2008 NS_ENSURE_ARG_POINTER(aEntries);
2010 nsCOMPtr<sbIDatabaseQuery> query;
2012 NS_ENSURE_SUCCESS(rv, rv);
2014 rv = query->AddQuery(mGetAllEntriesQuery);
2015 NS_ENSURE_SUCCESS(rv, rv);
2017 PRInt32 dbError = 0;
2018 rv = query->Execute(&dbError);
2019 NS_ENSURE_SUCCESS(rv, rv);
2020 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2022 nsCOMPtr<sbIDatabaseResult> result;
2023 rv = query->GetResultObject(getter_AddRefs(result));
2024 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2026 nsCOMPtr<nsIArray>
array;
2028 NS_ENSURE_SUCCESS(rv, rv);
2030 rv = array->Enumerate(aEntries);
2031 NS_ENSURE_SUCCESS(rv, rv);
2037 sbPlaybackHistoryService::GetEntryCount(PRUint64 *aEntryCount)
2039 NS_ENSURE_ARG_POINTER(aEntryCount);
2041 nsCOMPtr<sbIDatabaseQuery> query;
2043 NS_ENSURE_SUCCESS(rv, rv);
2045 rv = query->AddQuery(mGetEntryCountQuery);
2046 NS_ENSURE_SUCCESS(rv, rv);
2048 PRInt32 dbError = 0;
2049 rv = query->Execute(&dbError);
2050 NS_ENSURE_SUCCESS(rv, rv);
2051 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2053 nsCOMPtr<sbIDatabaseResult> result;
2054 rv = query->GetResultObject(getter_AddRefs(result));
2055 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2058 rv = result->GetRowCount(&rowCount);
2059 NS_ENSURE_SUCCESS(rv, rv);
2062 NS_ENSURE_TRUE(rowCount == 1, NS_ERROR_UNEXPECTED);
2064 nsAutoString countStr;
2065 rv = result->GetRowCell(0, 0, countStr);
2066 NS_ENSURE_SUCCESS(rv, rv);
2069 NS_ENSURE_SUCCESS(rv, rv);
2075 sbPlaybackHistoryService::CreateEntry(
sbIMediaItem *aItem,
2081 nsresult rv = NS_ERROR_UNEXPECTED;
2083 nsCOMPtr<sbIPlaybackHistoryEntry>
entry =
2085 NS_ENSURE_SUCCESS(rv, rv);
2087 rv = entry->Init(aItem, aTimestamp, aDuration, aAnnotations);
2088 NS_ENSURE_SUCCESS(rv, rv);
2090 entry.forget(_retval);
2098 NS_ENSURE_ARG_POINTER(aEntry);
2100 nsCOMPtr<sbIDatabaseQuery> query;
2102 NS_ENSURE_SUCCESS(rv, rv);
2104 rv = query->AddQuery(mAddEntryQuery);
2105 NS_ENSURE_SUCCESS(rv, rv);
2108 NS_ENSURE_SUCCESS(rv, rv);
2111 NS_ENSURE_SUCCESS(rv, rv);
2113 PRInt32 dbError = 0;
2114 rv = query->Execute(&dbError);
2115 NS_ENSURE_SUCCESS(rv, rv);
2116 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2118 nsCOMPtr<sbIDatabaseResult> result;
2119 rv = query->GetResultObject(getter_AddRefs(result));
2120 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2122 nsAutoString entryIdStr;
2123 rv = result->GetRowCell(0, 0, entryIdStr);
2124 NS_ENSURE_SUCCESS(rv, rv);
2127 NS_ENSURE_SUCCESS(rv, rv);
2129 aEntry->SetEntryId(entryId);
2132 NS_ENSURE_SUCCESS(rv, rv);
2138 sbPlaybackHistoryService::AddEntries(nsIArray *aEntries)
2140 NS_ENSURE_ARG_POINTER(aEntries);
2142 nsCOMPtr<sbIDatabaseQuery> query;
2144 NS_ENSURE_SUCCESS(rv, rv);
2146 rv = query->AddQuery(NS_LITERAL_STRING(
"BEGIN"));
2147 NS_ENSURE_SUCCESS(rv, rv);
2149 PRUint32 length = 0;
2150 rv = aEntries->GetLength(&length);
2152 for(PRUint32 current = 0; current < length; ++current) {
2153 nsCOMPtr<sbIPlaybackHistoryEntry> entry =
2154 do_QueryElementAt(aEntries, current, &rv);
2155 NS_ENSURE_SUCCESS(rv, rv);
2157 rv = query->AddQuery(mAddEntryQuery);
2158 NS_ENSURE_SUCCESS(rv, rv);
2161 NS_ENSURE_SUCCESS(rv, rv);
2164 NS_ENSURE_SUCCESS(rv, rv);
2167 rv = query->AddQuery(NS_LITERAL_STRING(
"COMMIT"));
2168 NS_ENSURE_SUCCESS(rv, rv);
2170 PRInt32 dbError = 0;
2171 rv = query->Execute(&dbError);
2172 NS_ENSURE_SUCCESS(rv, rv);
2173 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2175 nsCOMPtr<sbIDatabaseResult> result;
2176 rv = query->GetResultObject(getter_AddRefs(result));
2177 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2179 PRUint32 rowCount = 0;
2180 rv = result->GetRowCount(&rowCount);
2181 NS_ENSURE_SUCCESS(rv, rv);
2183 NS_ENSURE_TRUE(length == rowCount, NS_ERROR_UNEXPECTED);
2185 for(PRUint32 currentRow = 0; currentRow < rowCount; ++currentRow) {
2186 nsString entryIdStr;
2188 rv = result->GetRowCell(currentRow, 0, entryIdStr);
2189 NS_ENSURE_SUCCESS(rv, rv);
2191 nsCOMPtr<sbIPlaybackHistoryEntry> entry =
2192 do_QueryElementAt(aEntries, currentRow, &rv);
2193 NS_ENSURE_SUCCESS(rv, rv);
2196 NS_ENSURE_SUCCESS(rv, rv);
2198 entry->SetEntryId(entryId);
2202 NS_ENSURE_SUCCESS(rv, rv);
2208 sbPlaybackHistoryService::GetEntryByIndex(PRInt64 aIndex,
2211 NS_ENSURE_ARG_POINTER(_retval);
2213 nsCOMPtr<sbIDatabaseQuery> query;
2215 NS_ENSURE_SUCCESS(rv, rv);
2217 PRInt64 actualIndex = aIndex;
2218 if(actualIndex >= 0) {
2219 rv = query->AddQuery(mGetEntriesByIndexQuery);
2220 NS_ENSURE_SUCCESS(rv, rv);
2223 rv = query->AddQuery(mGetEntriesByIndexQueryAscending);
2224 NS_ENSURE_SUCCESS(rv, rv);
2226 actualIndex = PR_ABS(actualIndex) - 1;
2229 rv = query->BindInt64Parameter(0, 1);
2230 NS_ENSURE_SUCCESS(rv, rv);
2232 rv = query->BindInt64Parameter(1, actualIndex);
2233 NS_ENSURE_SUCCESS(rv, rv);
2235 PRInt32 dbError = 0;
2236 rv = query->Execute(&dbError);
2237 NS_ENSURE_SUCCESS(rv, rv);
2238 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2240 nsCOMPtr<sbIDatabaseResult> result;
2241 rv = query->GetResultObject(getter_AddRefs(result));
2242 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2245 NS_ENSURE_SUCCESS(rv, rv);
2251 sbPlaybackHistoryService::GetEntriesByIndex(PRInt64 aStartIndex,
2255 NS_ENSURE_ARG_POINTER(_retval);
2257 nsCOMPtr<sbIDatabaseQuery> query;
2259 NS_ENSURE_SUCCESS(rv, rv);
2261 PRInt64 actualIndex = aStartIndex;
2262 if( actualIndex >= 0) {
2263 rv = query->AddQuery(mGetEntriesByIndexQuery);
2264 NS_ENSURE_SUCCESS(rv, rv);
2267 rv = query->AddQuery(mGetEntriesByIndexQueryAscending);
2268 NS_ENSURE_SUCCESS(rv, rv);
2270 actualIndex = PR_ABS(actualIndex) - 1;
2273 rv = query->BindInt64Parameter(0, aCount);
2274 NS_ENSURE_SUCCESS(rv, rv);
2276 rv = query->BindInt64Parameter(1, actualIndex);
2277 NS_ENSURE_SUCCESS(rv, rv);
2279 PRInt32 dbError = 0;
2280 rv = query->Execute(&dbError);
2281 NS_ENSURE_SUCCESS(rv, rv);
2282 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2284 nsCOMPtr<sbIDatabaseResult> result;
2285 rv = query->GetResultObject(getter_AddRefs(result));
2286 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2289 NS_ENSURE_SUCCESS(rv, rv);
2295 sbPlaybackHistoryService::GetEntriesByTimestamp(PRInt64 aStartTimestamp,
2296 PRInt64 aEndTimestamp,
2299 NS_ENSURE_ARG_POINTER(_retval);
2301 PRBool isAscending = aStartTimestamp > aEndTimestamp;
2303 nsCOMPtr<sbIDatabaseQuery> query;
2305 NS_ENSURE_SUCCESS(rv, rv);
2308 rv = query->AddQuery(mGetEntriesByTimestampQuery);
2309 NS_ENSURE_SUCCESS(rv, rv);
2311 rv = query->BindInt64Parameter(0, aStartTimestamp);
2312 NS_ENSURE_SUCCESS(rv, rv);
2314 rv = query->BindInt64Parameter(1, aEndTimestamp);
2315 NS_ENSURE_SUCCESS(rv, rv);
2318 rv = query->AddQuery(mGetEntriesByTimestampQueryAscending);
2319 NS_ENSURE_SUCCESS(rv, rv);
2321 rv = query->BindInt64Parameter(0, aEndTimestamp);
2322 NS_ENSURE_SUCCESS(rv, rv);
2324 rv = query->BindInt64Parameter(1, aStartTimestamp);
2325 NS_ENSURE_SUCCESS(rv, rv);
2328 PRInt32 dbError = 0;
2329 rv = query->Execute(&dbError);
2330 NS_ENSURE_SUCCESS(rv, rv);
2331 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2333 nsCOMPtr<sbIDatabaseResult> result;
2334 rv = query->GetResultObject(getter_AddRefs(result));
2335 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2338 NS_ENSURE_SUCCESS(rv, rv);
2346 NS_ENSURE_ARG_POINTER(aEntry);
2348 nsCOMPtr<sbIDatabaseQuery> query;
2350 NS_ENSURE_SUCCESS(rv, rv);
2352 rv = query->AddQuery(mRemoveEntriesQuery);
2353 NS_ENSURE_SUCCESS(rv, rv);
2356 NS_ENSURE_SUCCESS(rv, rv);
2358 rv = query->AddQuery(mRemoveAnnotationsQuery);
2359 NS_ENSURE_SUCCESS(rv, rv);
2361 PRInt64 entryId = -1;
2362 rv = aEntry->GetEntryId(&entryId);
2363 NS_ENSURE_SUCCESS(rv, rv);
2365 rv = query->BindInt64Parameter(0, entryId);
2366 NS_ENSURE_SUCCESS(rv, rv);
2368 PRInt32 dbError = 0;
2369 rv = query->Execute(&dbError);
2370 NS_ENSURE_SUCCESS(rv, rv);
2371 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2374 NS_ENSURE_SUCCESS(rv, rv);
2380 sbPlaybackHistoryService::RemoveEntryByIndex(PRInt64 aIndex)
2382 nsCOMPtr<sbIPlaybackHistoryEntry>
entry;
2384 nsresult rv = GetEntryByIndex(aIndex, getter_AddRefs(entry));
2385 NS_ENSURE_SUCCESS(rv, rv);
2387 rv = RemoveEntry(entry);
2388 NS_ENSURE_SUCCESS(rv, rv);
2391 NS_ENSURE_SUCCESS(rv, rv);
2397 sbPlaybackHistoryService::RemoveEntriesByIndex(PRInt64 aStartIndex,
2402 nsresult rv = GetEntriesByIndex(aStartIndex, aCount, getter_AddRefs(entries));
2403 NS_ENSURE_SUCCESS(rv, rv);
2405 rv = RemoveEntries(entries);
2406 NS_ENSURE_SUCCESS(rv, rv);
2409 NS_ENSURE_SUCCESS(rv, rv);
2415 sbPlaybackHistoryService::RemoveEntries(nsIArray *aEntries)
2417 NS_ENSURE_ARG_POINTER(aEntries);
2419 nsCOMPtr<sbIDatabaseQuery> query;
2421 NS_ENSURE_SUCCESS(rv, rv);
2423 PRUint32 length = 0;
2424 rv = aEntries->GetLength(&length);
2425 NS_ENSURE_SUCCESS(rv, rv);
2427 rv = query->AddQuery(NS_LITERAL_STRING(
"BEGIN"));
2428 NS_ENSURE_SUCCESS(rv, rv);
2430 for(PRUint32 current = 0; current < length; ++current) {
2431 nsCOMPtr<sbIPlaybackHistoryEntry> entry =
2432 do_QueryElementAt(aEntries, current, &rv);
2433 NS_ENSURE_SUCCESS(rv, rv);
2435 rv = query->AddQuery(mRemoveEntriesQuery);
2436 NS_ENSURE_SUCCESS(rv, rv);
2439 NS_ENSURE_SUCCESS(rv, rv);
2442 rv = query->AddQuery(NS_LITERAL_STRING(
"COMMIT"));
2443 NS_ENSURE_SUCCESS(rv, rv);
2445 PRInt32 dbError = 0;
2446 rv = query->Execute(&dbError);
2447 NS_ENSURE_SUCCESS(rv, rv);
2448 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2451 NS_ENSURE_SUCCESS(rv, rv);
2457 sbPlaybackHistoryService::GetEntriesByAnnotation(
2458 const nsAString & aAnnotationId,
2459 const nsAString & aAnnotationValue,
2463 NS_ENSURE_ARG_POINTER(_retval);
2466 sql.AssignLiteral(
"select entry_id, library_guid, media_item_guid, play_time, play_duration from ");
2468 sql.AppendLiteral(
" where entry_id in ( ");
2470 sql.AppendLiteral(
"select entry_id from ");
2472 sql.AppendLiteral(
" where property_id = ? and obj = ? ");
2475 sql.AppendLiteral(
" limit ?");
2478 sql.AppendLiteral(
" ) ");
2479 sql.AppendLiteral(
"order by play_time desc");
2481 PRUint32 propertyId = 0;
2483 NS_ENSURE_SUCCESS(rv, rv);
2485 nsCOMPtr<sbIDatabaseQuery> query;
2487 NS_ENSURE_SUCCESS(rv, rv);
2489 rv = query->AddQuery(sql);
2490 NS_ENSURE_SUCCESS(rv, rv);
2492 rv = query->BindInt32Parameter(0, propertyId);
2493 NS_ENSURE_SUCCESS(rv, rv);
2495 rv = query->BindStringParameter(1, aAnnotationValue);
2496 NS_ENSURE_SUCCESS(rv, rv);
2499 rv = query->BindInt32Parameter(2, aCount);
2502 PRInt32 dbError = 0;
2503 rv = query->Execute(&dbError);
2504 NS_ENSURE_SUCCESS(rv, rv);
2505 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2507 nsCOMPtr<sbIDatabaseResult> result;
2508 rv = query->GetResultObject(getter_AddRefs(result));
2509 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2512 NS_ENSURE_SUCCESS(rv, rv);
2518 sbPlaybackHistoryService::GetEntriesByAnnotations(
2523 NS_ENSURE_ARG_POINTER(aAnnotations);
2524 NS_ENSURE_ARG_POINTER(_retval);
2527 sql.AssignLiteral(
"select entry_id, library_guid, media_item_guid, play_time, play_duration from ");
2529 sql.AppendLiteral(
" where entry_id in ( ");
2531 sql.AppendLiteral(
"select entry_id from ");
2533 sql.AppendLiteral(
" where property_id = ? and obj = ? ");
2535 PRUint32 length = 0;
2536 nsresult rv = aAnnotations->GetLength(&length);
2537 NS_ENSURE_SUCCESS(rv, rv);
2539 for(PRUint32 current = 0; current < length - 1; ++current) {
2540 sql.AppendLiteral(
" or property_id = ? and obj = ? ");
2544 sql.AppendLiteral(
" limit ?");
2547 sql.AppendLiteral(
" ) ");
2548 sql.AppendLiteral(
" order by play_time desc");
2550 nsCOMPtr<sbIDatabaseQuery> query;
2552 NS_ENSURE_SUCCESS(rv, rv);
2554 rv = query->AddQuery(sql);
2555 NS_ENSURE_SUCCESS(rv, rv);
2557 for(PRUint32 current = 0, currentEntry = 0;
2558 current < length * 2;
2559 current += 2, ++currentEntry) {
2561 nsCOMPtr<sbIProperty> property;
2562 rv = aAnnotations->GetPropertyAt(currentEntry, getter_AddRefs(property));
2563 NS_ENSURE_SUCCESS(rv, rv);
2565 nsString annotationId;
2566 rv =
property->GetId(annotationId);
2567 NS_ENSURE_SUCCESS(rv, rv);
2569 nsString annotationValue;
2570 rv =
property->GetValue(annotationValue);
2571 NS_ENSURE_SUCCESS(rv, rv);
2573 PRUint32 propertyId = 0;
2575 NS_ENSURE_SUCCESS(rv, rv);
2577 rv = query->BindInt32Parameter(current, propertyId);
2578 NS_ENSURE_SUCCESS(rv, rv);
2580 rv = query->BindStringParameter(current + 1, annotationValue);
2581 NS_ENSURE_SUCCESS(rv, rv);
2585 rv = query->BindInt32Parameter(length * 2, aCount);
2588 PRInt32 dbError = 0;
2589 rv = query->Execute(&dbError);
2590 NS_ENSURE_SUCCESS(rv, rv);
2591 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2593 nsCOMPtr<sbIDatabaseResult> result;
2594 rv = query->GetResultObject(getter_AddRefs(result));
2595 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2598 NS_ENSURE_SUCCESS(rv, rv);
2604 sbPlaybackHistoryService::Clear()
2606 nsCOMPtr<sbIDatabaseQuery> query;
2608 NS_ENSURE_SUCCESS(rv, rv);
2610 rv = query->AddQuery(mRemoveAllEntriesQuery);
2611 NS_ENSURE_SUCCESS(rv, rv);
2613 rv = query->AddQuery(mRemoveAllAnnotationsQuery);
2614 NS_ENSURE_SUCCESS(rv, rv);
2616 PRInt32 dbError = 0;
2617 rv = query->Execute(&dbError);
2618 NS_ENSURE_SUCCESS(rv, rv);
2619 NS_ENSURE_TRUE(dbError == 0, NS_ERROR_FAILURE);
2622 NS_ENSURE_SUCCESS(rv, rv);
2630 NS_ENSURE_ARG_POINTER(aListener);
2634 nsCOMPtr<nsIThread>
target;
2635 nsresult rv = NS_GetCurrentThread(getter_AddRefs(target));
2636 NS_ENSURE_SUCCESS(rv, rv);
2638 nsCOMPtr<sbIPlaybackHistoryListener> proxy;
2642 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
2643 getter_AddRefs(proxy));
2644 NS_ENSURE_SUCCESS(rv, rv);
2647 PRBool success = mListeners.Put(aListener, proxy);
2648 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
2656 NS_ENSURE_ARG_POINTER(aListener);
2657 mListeners.Remove(aListener);
2662 sbPlaybackHistoryService::AddOrUpdateAnnotation(
2664 const nsAString &aAnnotationId,
2665 const nsAString &aAnnotationValue)
2667 nsresult rv = NS_ERROR_UNEXPECTED;
2668 nsCOMPtr<sbIPropertyManager> propMan =
2670 NS_ENSURE_SUCCESS(rv, rv);
2672 nsCOMPtr<sbIPropertyInfo> propertyInfo;
2673 rv = propMan->GetPropertyInfo(aAnnotationId, getter_AddRefs(propertyInfo));
2674 NS_ENSURE_SUCCESS(rv, rv);
2676 nsAutoString sortableValue;
2677 rv = propertyInfo->MakeSortable(aAnnotationValue, sortableValue);
2678 NS_ENSURE_SUCCESS(rv, rv);
2680 PRUint32 propertyId = 0;
2682 NS_ENSURE_SUCCESS(rv, rv);
2684 nsCOMPtr<sbIDatabaseQuery> query;
2686 NS_ENSURE_SUCCESS(rv, rv);
2688 rv = query->AddQuery(mIsAnnotationPresentQuery);
2689 NS_ENSURE_SUCCESS(rv, rv);
2691 rv = query->BindInt64Parameter(0, aEntryId);
2692 NS_ENSURE_SUCCESS(rv, rv);
2694 rv = query->BindInt32Parameter(1, propertyId);
2695 NS_ENSURE_SUCCESS(rv, rv);
2698 rv = query->Execute(&dbOk);
2699 NS_ENSURE_SUCCESS(rv, rv);
2700 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
2702 nsCOMPtr<sbIDatabaseResult> result;
2703 rv = query->GetResultObject(getter_AddRefs(result));
2704 NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
2706 PRUint32 rowCount = 0;
2707 rv = result->GetRowCount(&rowCount);
2708 NS_ENSURE_SUCCESS(rv, rv);
2710 rv = query->ResetQuery();
2711 NS_ENSURE_SUCCESS(rv, rv);
2714 rv = query->AddQuery(mUpdateAnnotationQuery);
2715 NS_ENSURE_SUCCESS(rv, rv);
2717 rv = query->BindInt32Parameter(0, propertyId);
2718 NS_ENSURE_SUCCESS(rv, rv);
2720 rv = query->BindStringParameter(1, aAnnotationValue);
2721 NS_ENSURE_SUCCESS(rv, rv);
2723 rv = query->BindStringParameter(2, sortableValue);
2724 NS_ENSURE_SUCCESS(rv, rv);
2726 rv = query->BindInt64Parameter(3, aEntryId);
2727 NS_ENSURE_SUCCESS(rv, rv);
2730 rv = query->AddQuery(mInsertAnnotationQuery);
2731 NS_ENSURE_SUCCESS(rv, rv);
2733 rv = query->BindInt64Parameter(0, aEntryId);
2734 NS_ENSURE_SUCCESS(rv, rv);
2736 rv = query->BindInt32Parameter(1, propertyId);
2737 NS_ENSURE_SUCCESS(rv, rv);
2739 rv = query->BindStringParameter(2, aAnnotationValue);
2740 NS_ENSURE_SUCCESS(rv, rv);
2742 rv = query->BindStringParameter(3, sortableValue);
2743 NS_ENSURE_SUCCESS(rv, rv);
2747 rv = query->Execute(&dbOk);
2748 NS_ENSURE_SUCCESS(rv, rv);
2749 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
2755 sbPlaybackHistoryService::RemoveAnnotation(PRInt64 aEntryId,
2756 const nsAString &aAnnotationId)
2758 nsCOMPtr<sbIDatabaseQuery> query;
2760 NS_ENSURE_SUCCESS(rv, rv);
2762 rv = query->AddQuery(mRemoveAnnotationQuery);
2763 NS_ENSURE_SUCCESS(rv, rv);
2765 rv = query->BindInt64Parameter(0, aEntryId);
2766 NS_ENSURE_SUCCESS(rv, rv);
2768 PRUint32 propertyId = 0;
2770 NS_ENSURE_SUCCESS(rv, rv);
2772 rv = query->BindInt32Parameter(1, propertyId);
2773 NS_ENSURE_SUCCESS(rv, rv);
2776 rv = query->Execute(&dbOk);
2777 NS_ENSURE_SUCCESS(rv, rv);
2778 NS_ENSURE_TRUE(dbOk == 0, NS_ERROR_FAILURE);
nsresult UpdateCurrentViewFromEvent(sbIMediacoreEvent *aEvent)
nsresult FillAddQueryParameters(sbIDatabaseQuery *aQuery, sbIPlaybackHistoryEntry *aEntry)
#define NS_APPSTARTUP_CATEGORY
#define SB_PLAYBACKHISTORYSERVICE_DESCRIPTION
#define METRIC_MEDIACORE_ROOT
#define SB_PROPERTY_PLAYCOUNT
const SB_LIBRARY_MANAGER_READY_TOPIC
#define SONGBIRD_DATABASEQUERY_CONTRACTID
static nsCOMPtr< nsIObserverService > observerService
nsresult DoEntryRemovedCallback(sbIPlaybackHistoryEntry *aEntry)
nsresult DoEntryUpdatedCallback(sbIPlaybackHistoryEntry *aEntry)
#define METRIC_SUBSCRIPTION
PRUint64 nsString_ToUint64(const nsAString &str, nsresult *rv)
nsresult CreateAnnotationsFromEntryId(PRInt64 aEntryId, sbIPropertyArray **aAnnotations)
#define SB_PLAYBACKHISTORYENTRY_CONTRACTID
#define METRIC_PLAYTIME
Total Playtime, in seconds.
#define SB_THREADSAFE_ARRAY_CONTRACTID
#define SB_SQLBUILDER_INSERT_CONTRACTID
nsresult DoEntriesRemovedCallback(nsIArray *aEntry)
const unsigned long MATCH_LESSEQUAL
#define MEDIA_ITEM_GUID_COLUMN
nsresult FillRemoveEntryQueryParameters(sbIDatabaseQuery *aQuery, sbIPlaybackHistoryEntry *aEntry)
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
#define SB_PROPERTY_EXCLUDE_FROM_HISTORY
#define SB_PROPERTY_LASTPLAYTIME
nsresult GetPropertyDBID(const nsAString &aPropertyID, PRUint32 *aPropertyDBID)
nsresult DoEntriesAddedCallback(nsIArray *aEntries)
nsresult EnsureHistoryDatabaseAvailable()
readonly attribute nsISimpleEnumerator entries
Enumerator of all entries in the playback history service.
nsresult FillAddAnnotationsQueryParameters(sbIDatabaseQuery *aQuery, sbIPlaybackHistoryEntry *aEntry)
#define SB_PROPERTY_BITRATE
#define PROPERTY_NAME_COLUMN
nsresult CreateDefaultQuery(sbIDatabaseQuery **aQuery)
NS_DECL_ISUPPORTS NS_DECL_NSIOBSERVER NS_DECL_SBIMEDIACOREEVENTLISTENER static NS_DECL_SBIPLAYBACKHISTORYSERVICE NS_METHOD RegisterSelf(nsIComponentManager *aCompMgr, nsIFile *aPath, const char *aLoaderStr, const char *aType, const nsModuleComponentInfo *aInfo)
NS_IMPL_THREADSAFE_ISUPPORTS3(sbPlaybackHistoryService, sbIPlaybackHistoryService, sbIMediacoreEventListener, nsIObserver) sbPlaybackHistoryService
#define SB_PROPERTYMANAGER_CONTRACTID
const sbCreateProxiedComponent do_ProxiedCreateInstance(const nsCID &aCID, nsresult *error=0)
nsresult CreateEntriesFromResultSet(sbIDatabaseResult *aResult, nsIArray **aEntries)
#define SB_SQLBUILDER_DELETE_CONTRACTID
nsresult ResetTrackingData()
#define PLAYBACKHISTORY_DB_GUID
#define PLAY_DURATION_COLUMN
#define SB_SQLBUILDER_SELECT_CONTRACTID
#define LIBRARY_GUID_COLUMN
nsresult LoadPropertyIDs()
#define SB_PROPERTY_DURATION
~sbPlaybackHistoryService()
nsresult DoEntriesClearedCallback()
#define SB_PLAYBACKHISTORYSERVICE_CONTRACTID
An object containing the results of a database SELECT query.
nsresult InsertPropertyID(const nsAString &aPropertyID, PRUint32 *aPropertyDBID)
nsresult VerifyDataAndCreateNewEntry()
const unsigned long MATCH_GREATEREQUAL
var getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('Songbird SBProperties playCount
#define PLAYBACKHISTORY_PROPERTIES_TABLE
#define PLAYBACKHISTORY_ANNOTATIONS_TABLE
static already_AddRefed< nsILocalFile > GetDBFolder()
PRInt64 nsString_ToInt64(const nsAString &str, nsresult *rv)
An object responsible for executing SQL queries on the database.
#define PLAYBACKHISTORY_ENTRIES_TABLE
nsresult UpdateTrackingDataFromEvent(sbIMediacoreEvent *aEvent)
#define SB_PROPERTY_LASTSKIPTIME
#define CONVERTER_BUFFER_SIZE
restoreHistoryPrecursor aCount
#define PROPERTY_ID_COLUMN
nsresult GetItem(const nsAString &aLibraryGuid, const nsAString &aItemGuid, sbIMediaItem **aItem)
#define SB_PROPERTY_CUSTOMTYPE
#define PLAYBACKHISTORY_COUNT_ENTRIES
nsresult DoEntryAddedCallback(sbIPlaybackHistoryEntry *aEntry)
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
#define SONGBIRD_LIBRARYMANAGER_CONTRACTID
#define METRIC_MEDIALIST_ROOT
_getSelectedPageStyle s i
nsresult DoEntriesUpdatedCallback(nsIArray *aEntry)
#define METRIC_WEBPLAYLIST
#define SB_PROPERTY_SKIPCOUNT
#define SB_UNUSED_IN_RELEASE(decl)
nsresult CreateEntryFromResultSet(sbIDatabaseResult *aResult, PRUint32 aRow, sbIPlaybackHistoryEntry **aEntry)
const SB_LIBRARY_MANAGER_BEFORE_SHUTDOWN_TOPIC
const unsigned long MATCH_EQUALS
_updateTextAndScrollDataForFrame aData
static PLDHashOperator PR_CALLBACK AddListenersToCOMArrayCallback(nsISupportsHashKey::KeyType aKey, sbIPlaybackHistoryListener *aEntry, void *aUserData)
Songbird Database Object Definition.