Bug 735312 - Properly handle importing default bookmarks from omni.ja.

r=felipe
This commit is contained in:
Marco Bonardo 2012-03-14 18:57:00 +01:00
parent 4679d4aef3
commit b948623777
6 changed files with 29 additions and 107 deletions

View File

@ -56,7 +56,7 @@
#define MIGRATION_BUNDLE "chrome://browser/locale/migration/migration.properties"
#define BOOKMARKS_FILE_NAME NS_LITERAL_STRING("bookmarks.html")
#define DEFAULT_BOOKMARKS NS_LITERAL_CSTRING("resource:///defaults/profile/bookmarks.html")
void SetUnicharPref(const char* aPref, const nsAString& aValue,
nsIPrefBranch* aPrefs)
@ -164,72 +164,20 @@ GetProfilePath(nsIProfileStartup* aStartup, nsCOMPtr<nsIFile>& aProfileDir)
}
nsresult
ImportBookmarksHTML(nsIFile* aBookmarksFile,
bool aImportIntoRoot,
bool aOverwriteDefaults,
const PRUnichar* aImportSourceNameKey)
ImportDefaultBookmarks()
{
nsresult rv;
nsCOMPtr<nsIPlacesImportExportService> importer =
do_GetService(NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID);
NS_ENSURE_STATE(importer);
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);
nsCOMPtr<nsIIOService> ioService = mozilla::services::GetIOService();
NS_ENSURE_STATE(ioService);
nsCOMPtr<nsIURI> bookmarksURI;
nsresult rv = ioService->NewURI(DEFAULT_BOOKMARKS, nsnull, nsnull,
getter_AddRefs(bookmarksURI));
if (NS_FAILED(rv))
return 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);
nsCOMPtr<nsIStringBundle> bundle;
rv = bundleService->CreateBundle(MIGRATION_BUNDLE, getter_AddRefs(bundle));
NS_ENSURE_SUCCESS(rv, rv);
nsString sourceName;
rv = bundle->GetStringFromName(aImportSourceNameKey,
getter_Copies(sourceName));
NS_ENSURE_SUCCESS(rv, rv);
const PRUnichar* sourceNameStrings[] = { sourceName.get() };
nsString importedBookmarksTitle;
rv = bundle->FormatStringFromName(NS_LITERAL_STRING("importedBookmarksFolder").get(),
sourceNameStrings, 1,
getter_Copies(importedBookmarksTitle));
NS_ENSURE_SUCCESS(rv, rv);
// Get the bookmarks service.
nsCOMPtr<nsINavBookmarksService> bms =
do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Create an imported bookmarks folder under the bookmarks menu.
PRInt64 root;
rv = bms->GetBookmarksMenuFolder(&root);
NS_ENSURE_SUCCESS(rv, rv);
PRInt64 folder;
rv = bms->CreateFolder(root, NS_ConvertUTF16toUTF8(importedBookmarksTitle),
nsINavBookmarksService::DEFAULT_INDEX, &folder);
NS_ENSURE_SUCCESS(rv, rv);
// Import the bookmarks into the folder.
return importer->ImportHTMLFromFileToFolder(localFile, folder, false);
}
nsresult
InitializeBookmarks(nsIFile* aTargetProfile)
{
nsCOMPtr<nsIFile> bookmarksFile;
aTargetProfile->Clone(getter_AddRefs(bookmarksFile));
bookmarksFile->Append(BOOKMARKS_FILE_NAME);
nsresult rv = ImportBookmarksHTML(bookmarksFile, true, true, EmptyString().get());
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
rv = importer->ImportHTMLFromURI(bookmarksURI, true);
return rv;
}

View File

@ -99,18 +99,10 @@ void GetMigrateDataFromArray(MigrationData* aDataArray,
// this is already cloned, modify it to your heart's content
void GetProfilePath(nsIProfileStartup* aStartup, nsCOMPtr<nsIFile>& aProfileDir);
// 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,
bool aImportIntoRoot,
bool aOverwriteDefaults,
const PRUnichar* aImportSourceNameKey);
nsresult InitializeBookmarks(nsIFile* aTargetProfile);
/**
* Imports default bookmarks to the profile.
*/
nsresult ImportDefaultBookmarks();
#endif

View File

@ -1404,11 +1404,10 @@ nsIEProfileMigrator::CopyFavoritesBatched(bool aReplace)
NS_ENSURE_SUCCESS(rv, rv);
}
else {
// Initialize the default bookmarks
nsCOMPtr<nsIFile> profile;
GetProfilePath(nsnull, profile);
rv = InitializeBookmarks(profile);
NS_ENSURE_SUCCESS(rv, rv);
// If importing defaults fails for whatever reason, let the import process
// continue.
DebugOnly<nsresult> rv = ImportDefaultBookmarks();
MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to import default bookmarks");
// Locate the Links toolbar folder, we want to replace the Personal Toolbar
// content with Favorites in this folder.

View File

@ -65,6 +65,8 @@
#include "nsNetUtil.h"
#include "nsTArray.h"
#include "mozilla/Util.h"
#include <Carbon/Carbon.h>
#define SAFARI_PREFERENCES_FILE_NAME NS_LITERAL_STRING("com.apple.Safari.plist")
@ -977,10 +979,11 @@ nsSafariProfileMigrator::CopyBookmarksBatched(bool aReplace)
NS_ENSURE_SUCCESS(rv, rv);
}
else {
nsCOMPtr<nsIFile> profile;
GetProfilePath(nsnull, profile);
rv = InitializeBookmarks(profile);
NS_ENSURE_SUCCESS(rv, rv);
// If importing defaults fails for whatever reason, let the import process
// continue.
DebugOnly<nsresult> rv = ImportDefaultBookmarks();
MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to import default bookmarks");
// In replace mode we are merging at the top level.
folder = bookmarksMenuFolderId;
}

View File

@ -1,16 +0,0 @@
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
It will be read and overwritten.
DO NOT EDIT! -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks Menu</H1>
<DL><p>
<DT><A HREF="http://example.com/" ADD_DATE="1233157972" LAST_MODIFIED="1233157984">example</A>
<DT><H3 ADD_DATE="1233157910" LAST_MODIFIED="1233157972" PERSONAL_TOOLBAR_FOLDER="true">Bookmarks Toolbar</H3>
<DD>Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar
<DL><p>
<DT><A HREF="http://example.com/" ADD_DATE="1233157972" LAST_MODIFIED="1233157984">example</A>
</DL><p>
</DL><p>

View File

@ -22,7 +22,3 @@ function newMigratorFor(aKey) {
let cid = "@mozilla.org/profile/migrator;1?app=browser&type=" + aKey;
return Cc[cid].createInstance(Ci.nsIBrowserProfileMigrator);
}
let (bookmarkshtml = do_get_file("bookmarks.html")) {
bookmarkshtml.copyTo(gProfD, "bookmarks.html");
}