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 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, void SetUnicharPref(const char* aPref, const nsAString& aValue,
nsIPrefBranch* aPrefs) nsIPrefBranch* aPrefs)
@ -164,72 +164,20 @@ GetProfilePath(nsIProfileStartup* aStartup, nsCOMPtr<nsIFile>& aProfileDir)
} }
nsresult nsresult
ImportBookmarksHTML(nsIFile* aBookmarksFile, ImportDefaultBookmarks()
bool aImportIntoRoot,
bool aOverwriteDefaults,
const PRUnichar* aImportSourceNameKey)
{ {
nsresult rv; nsCOMPtr<nsIPlacesImportExportService> importer =
do_GetService(NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID);
NS_ENSURE_STATE(importer);
nsCOMPtr<nsILocalFile> localFile(do_QueryInterface(aBookmarksFile)); nsCOMPtr<nsIIOService> ioService = mozilla::services::GetIOService();
NS_ENSURE_TRUE(localFile, NS_ERROR_FAILURE); NS_ENSURE_STATE(ioService);
nsCOMPtr<nsIPlacesImportExportService> importer = do_GetService(NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID, &rv); nsCOMPtr<nsIURI> bookmarksURI;
NS_ENSURE_SUCCESS(rv, rv); 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. rv = importer->ImportHTMLFromURI(bookmarksURI, true);
if (aImportIntoRoot) { return rv;
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;
} }

View File

@ -99,18 +99,10 @@ void GetMigrateDataFromArray(MigrationData* aDataArray,
// this is already cloned, modify it to your heart's content // this is already cloned, modify it to your heart's content
void GetProfilePath(nsIProfileStartup* aStartup, nsCOMPtr<nsIFile>& aProfileDir); 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 * Imports default bookmarks to the profile.
// imported into the bookmarks root folder. Otherwise, they'll be imported into */
// a new folder with the name "From (STR:aImportSourceNameKey)". nsresult ImportDefaultBookmarks();
// 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);
#endif #endif

View File

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

View File

@ -65,6 +65,8 @@
#include "nsNetUtil.h" #include "nsNetUtil.h"
#include "nsTArray.h" #include "nsTArray.h"
#include "mozilla/Util.h"
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
#define SAFARI_PREFERENCES_FILE_NAME NS_LITERAL_STRING("com.apple.Safari.plist") #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); NS_ENSURE_SUCCESS(rv, rv);
} }
else { else {
nsCOMPtr<nsIFile> profile; // If importing defaults fails for whatever reason, let the import process
GetProfilePath(nsnull, profile); // continue.
rv = InitializeBookmarks(profile); DebugOnly<nsresult> rv = ImportDefaultBookmarks();
NS_ENSURE_SUCCESS(rv, rv); MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to import default bookmarks");
// In replace mode we are merging at the top level. // In replace mode we are merging at the top level.
folder = bookmarksMenuFolderId; 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; let cid = "@mozilla.org/profile/migrator;1?app=browser&type=" + aKey;
return Cc[cid].createInstance(Ci.nsIBrowserProfileMigrator); return Cc[cid].createInstance(Ci.nsIBrowserProfileMigrator);
} }
let (bookmarkshtml = do_get_file("bookmarks.html")) {
bookmarkshtml.copyTo(gProfD, "bookmarks.html");
}