Bug 386678 append the default bookmarks when first run migration from IE, Opera, Safari, etc. (r=sspitzer)

This commit is contained in:
dietrich@mozilla.com 2007-08-29 20:01:07 -07:00
parent 1b371b1fae
commit 5968a9e503
10 changed files with 119 additions and 62 deletions

View File

@ -59,6 +59,8 @@
#define MIGRATION_BUNDLE "chrome://browser/locale/migration/migration.properties"
#define BOOKMARKS_FILE_NAME NS_LITERAL_STRING("bookmarks.html")
void SetUnicharPref(const char* aPref, const nsAString& aValue,
nsIPrefBranch* aPrefs)
{
@ -217,11 +219,25 @@ AnnotatePersonalToolbarFolder(nsIFile* aSourceBookmarksFile,
nsresult
ImportBookmarksHTML(nsIFile* aBookmarksFile,
PRBool aImportIntoRoot,
PRBool aOverwriteDefaults,
const PRUnichar* aImportSourceNameKey)
{
nsresult rv;
// Look for the localized name of the bookmarks toolbar
nsCOMPtr<nsILocalFile> localFile(do_QueryInterface(aBookmarksFile));
NS_ENSURE_TRUE(localFile, NS_ERROR_FAILURE);
nsCOMPtr<nsIPlacesImportExportService> importer = do_GetService(NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Import file directly into the bookmarks root folder.
if (aImportIntoRoot) {
rv = importer->ImportHTMLFromFile(localFile, aOverwriteDefaults);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
// Get the source application name.
nsCOMPtr<nsIStringBundleService> bundleService =
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
@ -239,7 +255,7 @@ ImportBookmarksHTML(nsIFile* aBookmarksFile,
sourceNameStrings, 1,
getter_Copies(importedBookmarksTitle));
// Get the bookmarks service
// Get the bookmarks service.
nsCOMPtr<nsINavBookmarksService> bms =
do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
@ -254,9 +270,17 @@ ImportBookmarksHTML(nsIFile* aBookmarksFile,
NS_ENSURE_SUCCESS(rv, rv);
// Import the bookmarks into the folder.
nsCOMPtr<nsILocalFile> localFile(do_QueryInterface(aBookmarksFile));
NS_ENSURE_TRUE(localFile, NS_ERROR_FAILURE);
nsCOMPtr<nsIPlacesImportExportService> importer = do_GetService(NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
return importer->ImportHTMLFromFileToFolder(localFile, folder, PR_FALSE);
}
nsresult
InitializeBookmarks(nsIFile* aTargetProfile)
{
nsCOMPtr<nsIFile> bookmarksFile;
aTargetProfile->Clone(getter_AddRefs(bookmarksFile));
bookmarksFile->Append(BOOKMARKS_FILE_NAME);
nsresult rv = ImportBookmarksHTML(bookmarksFile, PR_TRUE, PR_TRUE, EmptyString().get());
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}

View File

@ -101,11 +101,18 @@ nsresult AnnotatePersonalToolbarFolder(nsIFile* aSourceBookmarksFile,
nsIFile* aTargetBookmarksFile,
const char* aToolbarFolderName);
// In-place import from aBookmarksFile into a folder in the user's bookmarks
// with the name "From (STR:aImportSourceNameKey)" (aImportSourceNameKey
// is a key into migration.properties with the pretty name of the application.
// In-place import from aBookmarksFile into a folder in the user's bookmarks.
// If the importIntoRoot parameter has a value of true, the bookmarks will be
// imported into the bookmarks root folder. Otherwise, they'll be imported into
// a new folder with the name "From (STR:aImportSourceNameKey)".
// aImportSourceNameKey is a key into migration.properties with the pretty name
// of the application.
nsresult ImportBookmarksHTML(nsIFile* aBookmarksFile,
PRBool aImportIntoRoot,
PRBool aOverwriteDefaults,
const PRUnichar* aImportSourceNameKey);
nsresult InitializeBookmarks(nsIFile* aTargetProfile);
#endif

View File

@ -622,8 +622,11 @@ nsresult
nsDogbertProfileMigrator::CopyBookmarks(PRBool aReplace)
{
// If we're blowing away existing content, just copy the file, don't do fancy importing.
if (aReplace)
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());

View File

@ -1199,6 +1199,12 @@ nsIEProfileMigrator::CopyFavorites(PRBool aReplace) {
bms->CreateFolder(root, importedIEFavsTitle, -1, &folder);
}
else {
// Initialize the default bookmarks
nsCOMPtr<nsIFile> profile;
GetProfilePath(nsnull, profile);
rv = InitializeBookmarks(profile);
NS_ENSURE_SUCCESS(rv, rv);
// Locate the Links toolbar folder, we want to replace the Personal Toolbar content with
// Favorites in this folder.
nsCOMPtr<nsIWindowsRegKey> regKey =
@ -1419,9 +1425,6 @@ nsIEProfileMigrator::ParseFavoritesFolder(nsIFile* aDirectory,
PRInt64 folder;
if (bookmarkName.Equals(aPersonalToolbarFolderName)) {
aBookmarksService->GetToolbarFolder(&folder);
// If we're here, it means the user's doing a _replace_ import which means
// clear out the content of this folder, and replace it with the new content
aBookmarksService->RemoveFolderChildren(folder);
}
else {
rv = aBookmarksService->CreateFolder(aParentFolder,

View File

@ -51,7 +51,7 @@
#define MACIE_BOOKMARKS_FILE_NAME NS_LITERAL_STRING("Favorites.html")
#define MACIE_PREFERENCES_FOLDER_NAME NS_LITERAL_STRING("Explorer")
#define FIREFOX_BOOKMARKS_FILE_NAME NS_LITERAL_STRING("bookmarks.html")
#define TEMP_BOOKMARKS_FILE_NAME NS_LITERAL_STRING("bookmarks_tmp.html")
#define MIGRATION_BUNDLE "chrome://browser/locale/migration/migration.properties"
@ -172,6 +172,7 @@ nsMacIEProfileMigrator::GetSourceHomePageURL(nsACString& aResult)
nsresult
nsMacIEProfileMigrator::CopyBookmarks(PRBool aReplace)
{
nsresult rv;
nsCOMPtr<nsIFile> sourceFile;
mSourceProfile->Clone(getter_AddRefs(sourceFile));
@ -181,46 +182,52 @@ nsMacIEProfileMigrator::CopyBookmarks(PRBool aReplace)
if (!exists)
return NS_OK;
nsCOMPtr<nsIFile> targetFile;
mTargetProfile->Clone(getter_AddRefs(targetFile));
targetFile->Append(FIREFOX_BOOKMARKS_FILE_NAME);
// 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 just copy the file.
if (aReplace) {
nsresult rv;
// 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);
// 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);
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);
nsString toolbarFolderNameMacIE;
bundle->GetStringFromName(NS_LITERAL_STRING("toolbarFolderNameMacIE").get(),
getter_Copies(toolbarFolderNameMacIE));
nsCAutoString ctoolbarFolderNameMacIE;
CopyUTF16toUTF8(toolbarFolderNameMacIE, ctoolbarFolderNameMacIE);
// If we can't find it for some reason, just copy the file.
if (NS_FAILED(rv)) {
targetFile->Exists(&exists);
if (exists)
targetFile->Remove(PR_FALSE);
// 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);
return sourceFile->CopyTo(mTargetProfile, FIREFOX_BOOKMARKS_FILE_NAME);
}
// import the temp file
rv = ImportBookmarksHTML(tempFile,
PR_TRUE,
PR_FALSE,
EmptyString().get());
NS_ENSURE_SUCCESS(rv, rv);
// Now read the 4.x bookmarks file, correcting the Personal Toolbar Folder
// line and writing to the new location.
return AnnotatePersonalToolbarFolder(sourceFile,
targetFile,
ctoolbarFolderNameMacIE.get());
}
return ImportBookmarksHTML(sourceFile,
NS_LITERAL_STRING("sourceNameIE").get());
// remove the temp file
return tempFile->Remove(PR_FALSE);
}

View File

@ -318,7 +318,7 @@ nsNetscapeProfileMigratorBase::ImportNetscapeBookmarks(const nsAString& aBookmar
mSourceProfile->Clone(getter_AddRefs(bookmarksFile));
bookmarksFile->Append(aBookmarksFileName);
return ImportBookmarksHTML(bookmarksFile, aImportSourceNameKey);
return ImportBookmarksHTML(bookmarksFile, PR_FALSE, PR_FALSE, aImportSourceNameKey);
}
nsresult

View File

@ -963,15 +963,15 @@ nsOperaCookieMigrator::ReadHeader()
nsresult
nsOperaProfileMigrator::CopyHistory(PRBool aReplace)
{
nsresult rv;
nsCOMPtr<nsINavHistoryService> history = do_GetService(NS_NAVHISTORYSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
return history->RunInBatchMode(this, nsnull);
}
NS_IMETHODIMP
nsOperaProfileMigrator::RunBatched(nsISupports* aUserData)
nsresult rv;
nsCOMPtr<nsINavHistoryService> history = do_GetService(NS_NAVHISTORYSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
return history->RunInBatchMode(this, nsnull);
}
NS_IMETHODIMP
nsOperaProfileMigrator::RunBatched(nsISupports* aUserData)
{
nsCOMPtr<nsIBrowserHistory> hist(do_GetService(NS_GLOBALHISTORY2_CONTRACTID));
@ -1071,8 +1071,13 @@ nsOperaProfileMigrator::CopyBookmarks(PRBool aReplace)
bms->CreateFolder(parentFolder, importedOperaHotlistTitle,
nsINavBookmarksService::DEFAULT_INDEX, &parentFolder);
}
else
else {
nsCOMPtr<nsIFile> profile;
GetProfilePath(nsnull, profile);
rv = InitializeBookmarks(profile);
NS_ENSURE_SUCCESS(rv, rv);
parentFolder = root;
}
#if defined(XP_WIN) || (defined(XP_UNIX) && !defined(XP_MACOSX))
printf("*** about to copy smart keywords\n");

View File

@ -423,6 +423,7 @@ nsPhoenixProfileMigrator::CopyPasswords(PRBool aReplace)
nsresult
nsPhoenixProfileMigrator::CopyBookmarks(PRBool aReplace)
{
// This overwrites the defaults. This might be ok in this instance.
return aReplace ? CopyFile(FILE_NAME_BOOKMARKS, FILE_NAME_BOOKMARKS) : NS_OK;
}

View File

@ -899,7 +899,7 @@ nsresult
nsSafariProfileMigrator::CopyBookmarks(PRBool aReplace)
{
// If "aReplace" is true, merge into the root level of bookmarks. Otherwise, create
// a folder called "Imported IE Favorites" and place all the Bookmarks there.
// a folder called "Imported Safari Favorites" and place all the Bookmarks there.
nsresult rv;
nsCOMPtr<nsINavBookmarksService> bms(do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID, &rv));
@ -930,7 +930,11 @@ nsSafariProfileMigrator::CopyBookmarks(PRBool aReplace)
&folder);
}
else {
// In non-replace mode we are merging at the top level.
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 = root;
}

View File

@ -723,8 +723,11 @@ nsSeamonkeyProfileMigrator::CopyPasswords(PRBool aReplace)
nsresult
nsSeamonkeyProfileMigrator::CopyBookmarks(PRBool aReplace)
{
if (aReplace)
if (aReplace) {
nsresult rv = InitializeBookmarks(mTargetProfile);
NS_ENSURE_SUCCESS(rv, rv);
return CopyFile(FILE_NAME_BOOKMARKS, FILE_NAME_BOOKMARKS);
}
return ImportNetscapeBookmarks(FILE_NAME_BOOKMARKS,
NS_LITERAL_STRING("sourceNameSeamonkey").get());
}