/**
 * \brief Adds multiple filters to a GUID array.
 *
 * This method enumerates a hash table and calls AddFilter on a GUIDArray once
 * for each key. It constructs a string enumerator for the string array that
 * the hash table contains.
 *
 * This method expects to be handed an sbILocalDatabaseGUIDArray pointer as its
 * aUserData parameter.
 */
/* static */ PLDHashOperator PR_CALLBACK
sbLocalDatabaseMediaListBase::AddFilterToGUIDArrayCallback(nsStringHashKey::KeyType aKey,
                                                           sbStringArray* aEntry,
                                                           void* aUserData)
{
  NS_ASSERTION(aEntry, "Null entry in the hash?!");
  NS_ASSERTION(aUserData, "Null userData!");

  // Make a string enumerator for the string array.
  nsCOMPtr<nsIStringEnumerator> valueEnum =
    new sbTArrayStringEnumerator(aEntry);

  // If we failed then we're probably out of memory. Hope we do better on the
  // next key?
  NS_ENSURE_TRUE(valueEnum, PL_DHASH_NEXT);

  // Unbox the guidArray.
  nsCOMPtr<sbILocalDatabaseGUIDArray> guidArray =
    static_cast<sbILocalDatabaseGUIDArray*>(aUserData);

  // Set the filter.
  nsresult SB_UNUSED_IN_RELEASE(rv) =
   guidArray->AddFilter(aKey, valueEnum, PR_FALSE);
  NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "AddFilter failed!");

  return PL_DHASH_NEXT;
}
/**
 * \brief Load all of the libraries we need for startup.
 */
NS_IMETHODIMP
sbLocalDatabaseLibraryLoader::OnRegisterStartupLibraries(sbILibraryManager* aLibraryManager)
{
  TRACE("sbLocalDatabaseLibraryLoader[0x%x] - LoadLibraries", this);

  nsresult rv = Init();
  NS_ENSURE_SUCCESS(rv, rv);

  rv = EnsureDefaultLibraries();
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<sbILibraryFactory> localDatabaseLibraryFactory =
    do_GetService(SB_LOCALDATABASE_LIBRARYFACTORY_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  sbLocalDatabaseLibraryFactory *libraryFactory = 
    reinterpret_cast<sbLocalDatabaseLibraryFactory *>(localDatabaseLibraryFactory.get());
  
  sbLoaderInfo info(aLibraryManager, libraryFactory);

  PRUint32 SB_UNUSED_IN_RELEASE(enumeratedLibraries) = 
    mLibraryInfoTable.EnumerateRead(LoadLibrariesCallback, &info);
  NS_ASSERTION(enumeratedLibraries >= MINIMUM_LIBRARY_COUNT, "Too few libraries enumerated!");

  return NS_OK;
}
sbGUIDArrayValueEnumerator::sbGUIDArrayValueEnumerator(sbILocalDatabaseGUIDArray* aArray) :
  mArray(aArray),
  mLength(0),
  mNextIndex(0)
{
  NS_ASSERTION(aArray, "Null value passed to ctor");

  nsresult SB_UNUSED_IN_RELEASE(rv) = mArray->GetLength(&mLength);
  NS_ASSERTION(NS_SUCCEEDED(rv), "Could not get length");
}
sbAutoIgnoreWatchFolderPath::~sbAutoIgnoreWatchFolderPath()
{
  // If the path was watched, it is now time to clean up and stop ignoring
  // the watch path.
  if (mIsIgnoring) {
    nsresult SB_UNUSED_IN_RELEASE(rv) = mWFService->RemoveIgnorePath(mWatchPath);
    NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
        "Could not remove a file path from the watchfolders ignore list!");
  }
}