nsresult
nsDogbertProfileMigrator::CopyBookmarks(PRBool aReplace)
{
  // If we're blowing away existing content, just copy the file, don't do fancy importing.
  if (aReplace) {
    nsresult rv = InitializeBookmarks(mTargetProfile);
    NS_ENSURE_SUCCESS(rv, rv);
    return MigrateDogbertBookmarks();
  }

  return ImportNetscapeBookmarks(BOOKMARKS_FILE_NAME_IN_4x, 
                                 NS_LITERAL_STRING("sourceNameDogbert").get());
}
nsresult
nsSeamonkeyProfileMigrator::CopyBookmarks(PRBool aReplace)
{
  nsresult rv;
  if (aReplace) {
    // Initialize the default bookmarks
    rv = InitializeBookmarks(mTargetProfile);
    NS_ENSURE_SUCCESS(rv, rv);

    // Merge in the bookmarks from the source profile
    nsCOMPtr<nsIFile> sourceFile;
    mSourceProfile->Clone(getter_AddRefs(sourceFile));
    sourceFile->Append(FILE_NAME_BOOKMARKS);
    rv = ImportBookmarksHTML(sourceFile, PR_TRUE, PR_FALSE, EmptyString().get());
    NS_ENSURE_SUCCESS(rv, rv);
  }
  else {
    rv = ImportNetscapeBookmarks(FILE_NAME_BOOKMARKS, 
                                 NS_LITERAL_STRING("sourceNameSeamonkey").get());
    NS_ENSURE_SUCCESS(rv, rv);
  }
  return NS_OK;
}
nsresult
nsOperaProfileMigrator::CopyBookmarksBatched(PRBool aReplace)
{
    // Find Opera Bookmarks
    nsCOMPtr<nsIFile> operaBookmarks;
    mOperaProfile->Clone(getter_AddRefs(operaBookmarks));
    operaBookmarks->Append(OPERA_BOOKMARKS_FILE_NAME);

    nsCOMPtr<nsIInputStream> fileInputStream;
    NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), operaBookmarks);
    NS_ENSURE_TRUE(fileInputStream, NS_ERROR_OUT_OF_MEMORY);

    nsCOMPtr<nsILineInputStream> lineInputStream(do_QueryInterface(fileInputStream));

    nsresult rv;
    nsCOMPtr<nsINavBookmarksService> bms =
        do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    PRInt64 bookmarksMenuFolderId;
    rv = bms->GetBookmarksMenuFolder(&bookmarksMenuFolderId);
    NS_ENSURE_SUCCESS(rv, rv);
    PRInt64 parentFolder = bookmarksMenuFolderId;

    nsCOMPtr<nsIStringBundleService> bundleService =
        do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    nsCOMPtr<nsIStringBundle> bundle;
    rv = bundleService->CreateBundle(MIGRATION_BUNDLE, getter_AddRefs(bundle));
    NS_ENSURE_SUCCESS(rv, rv);

    if (!aReplace) {
        nsString sourceNameOpera;
        rv = bundle->GetStringFromName(NS_LITERAL_STRING("sourceNameOpera").get(),
                                       getter_Copies(sourceNameOpera));
        NS_ENSURE_SUCCESS(rv, rv);

        const PRUnichar* sourceNameStrings[] = { sourceNameOpera.get() };
        nsString importedOperaHotlistTitle;
        rv = bundle->FormatStringFromName(NS_LITERAL_STRING("importedBookmarksFolder").get(),
                                          sourceNameStrings, 1,
                                          getter_Copies(importedOperaHotlistTitle));
        NS_ENSURE_SUCCESS(rv, rv);

        rv = bms->CreateFolder(parentFolder,
                               NS_ConvertUTF16toUTF8(importedOperaHotlistTitle),
                               nsINavBookmarksService::DEFAULT_INDEX,
                               &parentFolder);
        NS_ENSURE_SUCCESS(rv, rv);
    }
    else {
        nsCOMPtr<nsIFile> profile;
        GetProfilePath(nsnull, profile);
        rv = InitializeBookmarks(profile);
        NS_ENSURE_SUCCESS(rv, rv);
    }

#if defined(XP_WIN) || (defined(XP_UNIX) && !defined(XP_MACOSX))
    CopySmartKeywords(bms, bundle, parentFolder);
#endif

    PRInt64 bookmarksToolbarFolderId;
    rv = bms->GetToolbarFolder(&bookmarksToolbarFolderId);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = ParseBookmarksFolder(lineInputStream, parentFolder,
                              bookmarksToolbarFolderId, bms);
    NS_ENSURE_SUCCESS(rv, rv);

    return NS_OK;
}
nsresult
nsSafariProfileMigrator::CopyBookmarksBatched(bool aReplace)
{
  // If "aReplace" is true, merge into the root level of bookmarks. Otherwise, create
  // a folder called "Imported Safari Favorites" and place all the Bookmarks there.
  nsresult rv;

  nsCOMPtr<nsINavBookmarksService> bms =
    do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);
  PRInt64 bookmarksMenuFolderId;
  rv = bms->GetBookmarksMenuFolder(&bookmarksMenuFolderId);
  NS_ENSURE_SUCCESS(rv, rv);

  PRInt64 folder;
  if (!aReplace) {
    nsCOMPtr<nsIStringBundleService> bundleService =
      do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    nsCOMPtr<nsIStringBundle> bundle;
    rv = bundleService->CreateBundle(MIGRATION_BUNDLE, getter_AddRefs(bundle));
    NS_ENSURE_SUCCESS(rv, rv);

    nsString sourceNameSafari;
    rv = bundle->GetStringFromName(NS_LITERAL_STRING("sourceNameSafari").get(),
                                   getter_Copies(sourceNameSafari));
    NS_ENSURE_SUCCESS(rv, rv);

    const PRUnichar* sourceNameStrings[] = { sourceNameSafari.get() };
    nsString importedSafariBookmarksTitle;
    rv = bundle->FormatStringFromName(NS_LITERAL_STRING("importedBookmarksFolder").get(),
                                      sourceNameStrings, 1,
                                      getter_Copies(importedSafariBookmarksTitle));
    NS_ENSURE_SUCCESS(rv, rv);

    rv = bms->CreateFolder(bookmarksMenuFolderId,
                           NS_ConvertUTF16toUTF8(importedSafariBookmarksTitle),
                           nsINavBookmarksService::DEFAULT_INDEX, &folder);
    NS_ENSURE_SUCCESS(rv, rv);
  }
  else {
    nsCOMPtr<nsIFile> profile;
    GetProfilePath(nsnull, profile);
    rv = InitializeBookmarks(profile);
    NS_ENSURE_SUCCESS(rv, rv);
    // In replace mode we are merging at the top level.
    folder = bookmarksMenuFolderId;
  }

  nsCOMPtr<nsIProperties> fileLocator(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
  nsCOMPtr<nsILocalFile> safariBookmarksFile;
  fileLocator->Get(NS_MAC_USER_LIB_DIR, NS_GET_IID(nsILocalFile),
                   getter_AddRefs(safariBookmarksFile));
  safariBookmarksFile->Append(NS_LITERAL_STRING("Safari"));
  safariBookmarksFile->Append(SAFARI_BOOKMARKS_FILE_NAME);

  CFDictionaryRef safariBookmarks =
    static_cast<CFDictionaryRef>(CopyPListFromFile(safariBookmarksFile));
  if (!safariBookmarks)
    return NS_OK;

  // The Safari Bookmarks file looks like this:
  // At the top level are all the Folders, Special Folders and Proxies. Proxies
  // are references to other data sources such as History, Rendezvous etc.
  // We ignore these. Special folders exist for the Bookmarks Toolbar folder
  // (called "BookmarksBar" and the Bookmarks Menu (called "BookmarksMenu").
  // We put the contents of the "BookmarksBar" folder into our Personal Toolbar
  // and merge the contents of the "BookmarksMenu" folder and the other toplevel
  // non-special folders under our NC:BookmarksRoot.
  if (::CFDictionaryContainsKey(safariBookmarks, CFSTR("Children")) &&
      ::CFDictionaryContainsKey(safariBookmarks, CFSTR("WebBookmarkFileVersion")) ) {
    CFNumberRef intValue =
      (CFNumberRef)::CFDictionaryGetValue(safariBookmarks,
                                          CFSTR("WebBookmarkFileVersion"));
    PRInt32 value = 0;
    if (::CFNumberGetValue(intValue, kCFNumberSInt32Type, &value) && value ==1) {
      CFArrayRef children =
        (CFArrayRef)::CFDictionaryGetValue(safariBookmarks, CFSTR("Children"));
      if (children) {
        rv = ParseBookmarksFolder(children, folder, bms, true);
      }
    }
  }
  ::CFRelease(safariBookmarks);
  return rv;
}
nsresult
nsMacIEProfileMigrator::CopyBookmarks(PRBool aReplace)
{
  nsresult rv;
  nsCOMPtr<nsIFile> sourceFile;
  mSourceProfile->Clone(getter_AddRefs(sourceFile));

  sourceFile->Append(MACIE_BOOKMARKS_FILE_NAME);
  PRBool exists = PR_FALSE;
  sourceFile->Exists(&exists);
  if (!exists)
    return NS_OK;

  // it's an import
  if (!aReplace)
    return ImportBookmarksHTML(sourceFile,
                               PR_FALSE,
                               PR_FALSE,
                               NS_LITERAL_STRING("sourceNameIE").get());

  // Initialize the default bookmarks
  rv = InitializeBookmarks(mTargetProfile);
  NS_ENSURE_SUCCESS(rv, rv);

  // If we're blowing away existing content, annotate the Personal Toolbar and
  // then import the file. 
  nsCOMPtr<nsIFile> tempFile;
  mTargetProfile->Clone(getter_AddRefs(tempFile));
  tempFile->Append(TEMP_BOOKMARKS_FILE_NAME);

  // Look for the localized name of the IE Favorites Bar
  nsCOMPtr<nsIStringBundleService> bundleService =
    do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIStringBundle> bundle;
  rv = bundleService->CreateBundle(MIGRATION_BUNDLE, getter_AddRefs(bundle));
  NS_ENSURE_SUCCESS(rv, rv);

  nsString toolbarFolderNameMacIE;
  bundle->GetStringFromName(NS_LITERAL_STRING("toolbarFolderNameMacIE").get(), 
                            getter_Copies(toolbarFolderNameMacIE));
  nsCAutoString ctoolbarFolderNameMacIE;
  CopyUTF16toUTF8(toolbarFolderNameMacIE, ctoolbarFolderNameMacIE);

  // Now read the 4.x bookmarks file, correcting the Personal Toolbar Folder 
  // line and writing to the temporary file.
  rv = AnnotatePersonalToolbarFolder(sourceFile,
                                     tempFile,
                                     ctoolbarFolderNameMacIE.get());
  NS_ENSURE_SUCCESS(rv, rv);

  // import the temp file
  rv = ImportBookmarksHTML(tempFile,
                           PR_TRUE,
                           PR_FALSE,
                           EmptyString().get());
  NS_ENSURE_SUCCESS(rv, rv);

  // remove the temp file
  return tempFile->Remove(PR_FALSE);
}