Bug 422919 - Bookmark Import and Restore do not intelligently handle two different bookmark formats (r=mano, a=beltzner)

This commit is contained in:
dietrich%mozilla.com 2008-04-03 20:51:55 +00:00
Родитель d019173fc3
Коммит 849a55be44
11 изменённых файлов: 69 добавлений и 81 удалений

Просмотреть файл

@ -335,6 +335,12 @@ pref("browser.bookmarks.sort.resource", "rdf:http://home.netscape.com/NC-rdf#Nam
// be exported as HTML to the bookmarks.html file.
pref("browser.bookmarks.autoExportHTML", false);
// The maximum number of daily bookmark backups to
// keep in {PROFILEDIR}/bookmarkbackups. Special values:
// -1: unlimited
// 0: no backups created (and deletes all existing backups)
pref("browser.bookmarks.max_backups", 5);
// Scripts & Windows prefs
pref("dom.disable_open_during_load", true);
#ifdef DEBUG

Просмотреть файл

@ -106,7 +106,7 @@
<radio id="dogbert" label="&importFromNetscape4.label;" accesskey="&importFromNetscape4.accesskey;"/>
#endif
#endif
<radio id="fromfile" label="&importFromFile.label;" accesskey="&importFromFile.accesskey;" hidden="true"/>
<radio id="fromfile" label="&importFromHTMLFile.label;" accesskey="&importFromHTMLFile.accesskey;" hidden="true"/>
<radio id="nothing" label="&importFromNothing.label;" accesskey="&importFromNothing.accesskey;" hidden="true"/>
</radiogroup>
<label id="noSources" hidden="true">&noMigrationSources.label;</label>

Просмотреть файл

@ -298,7 +298,7 @@ var PlacesOrganizer = {
openDialog("chrome://browser/content/migration/migration.xul",
"migration", features, "bookmarks");
if (window.fromFile)
this.importFromFile();
this.importFromFile();
},
/**
@ -309,7 +309,7 @@ var PlacesOrganizer = {
createInstance(Ci.nsIFilePicker);
fp.init(window, PlacesUIUtils.getString("SelectImport"),
Ci.nsIFilePicker.modeOpen);
fp.appendFilters(Ci.nsIFilePicker.filterHTML | Ci.nsIFilePicker.filterAll);
fp.appendFilters(Ci.nsIFilePicker.filterHTML);
if (fp.show() != Ci.nsIFilePicker.returnCancel) {
if (fp.file) {
var importer = Cc["@mozilla.org/browser/places/import-export-service;1"].
@ -353,7 +353,7 @@ var PlacesOrganizer = {
var files = this.bookmarksBackupDir.directoryEntries;
while (files.hasMoreElements()) {
var f = files.getNext().QueryInterface(Ci.nsIFile);
if (!f.isHidden() && f.leafName.match(/^bookmarks-.+(html|json)?$/))
if (!f.isHidden() && f.leafName.match(/^bookmarks-.+json$/))
fileList.push(f);
}
@ -370,7 +370,7 @@ var PlacesOrganizer = {
(document.createElement("menuitem"),
document.getElementById("restoreFromFile"));
var dateStr = fileList[i].leafName.replace("bookmarks-", "").
replace(/\.(html|json)$/, "");
replace(/\.json$/, "");
if (!dateStr.length)
dateStr = fileList[i].leafName;
m.setAttribute("label", dateStr);
@ -404,6 +404,8 @@ var PlacesOrganizer = {
var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
fp.init(window, PlacesUIUtils.getString("bookmarksRestoreTitle"),
Ci.nsIFilePicker.modeOpen);
fp.appendFilter(PlacesUIUtils.getString("bookmarksRestoreFilterName"),
PlacesUIUtils.getString("bookmarksRestoreFilterExtension"));
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
@ -411,13 +413,20 @@ var PlacesOrganizer = {
fp.displayDirectory = backupsDir;
if (fp.show() != Ci.nsIFilePicker.returnCancel)
PlacesUtils.restoreBookmarksFromFile(fp.file);
this.restoreBookmarksFromFile(fp.file);
},
/**
* Restores bookmarks from an HTML or JSON file.
* Restores bookmarks from a JSON file.
*/
restoreBookmarksFromFile: function PO_restoreBookmarksFromFile(aFile) {
// check file extension
if (!aFile.leafName.match(/\.json$/)) {
this._showErrorAlert(PlacesUIUtils.getString("bookmarksRestoreFormatError"));
return;
}
// confirm ok to delete existing bookmarks
var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"].
getService(Ci.nsIPromptService);
if (!prompts.confirm(null,
@ -425,17 +434,23 @@ var PlacesOrganizer = {
PlacesUIUtils.getString("bookmarksRestoreAlert")))
return;
if (aFile.leafName.match("\.json$")) {
// restore a JSON backup
try {
PlacesUtils.restoreBookmarksFromJSONFile(aFile);
}
else {
var importer = Cc["@mozilla.org/browser/places/import-export-service;1"].
getService(Ci.nsIPlacesImportExportService);
importer.importHTMLFromFile(aFile, true /* overwrite existing */);
catch(ex) {
this._showErrorAlert(PlacesUIUtils.getString("bookmarksRestoreParseError"));
}
},
_showErrorAlert: function PO__showErrorAlert(aMsg) {
var brandShortName = document.getElementById("brandStrings").
getString("brandShortName");
Cc["@mozilla.org/embedcomp/prompt-service;1"].
getService(Ci.nsIPromptService).
alert(window, brandShortName, aMsg);
},
/**
* Backup bookmarks to desktop, auto-generate a filename with a date.
* The file is a JSON serialization of bookmarks, tags and any annotations
@ -445,6 +460,8 @@ var PlacesOrganizer = {
var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
fp.init(window, PlacesUIUtils.getString("bookmarksBackupTitle"),
Ci.nsIFilePicker.modeSave);
fp.appendFilter(PlacesUIUtils.getString("bookmarksRestoreFilterName"),
PlacesUIUtils.getString("bookmarksRestoreFilterExtension"));
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
@ -454,7 +471,7 @@ var PlacesOrganizer = {
// Use YYYY-MM-DD (ISO 8601) as it doesn't contain illegal characters
// and makes the alphabetical order of multiple backup files more useful.
var date = (new Date).toLocaleFormat("%Y-%m-%d");
fp.defaultString = PlacesUtils.getFormattedString("bookmarksBackupFilename",
fp.defaultString = PlacesUIUtils.getFormattedString("bookmarksBackupFilenameJSON",
[date]);
if (fp.show() != Ci.nsIFilePicker.returnCancel) {
@ -463,6 +480,7 @@ var PlacesOrganizer = {
// copy new backup to /backups dir (bug 424389)
var latestBackup = PlacesUtils.getMostRecentBackup();
if (latestBackup != fp.file) {
latestBackup.remove(false);
var date = new Date().toLocaleFormat("%Y-%m-%d");
var name = PlacesUtils.getFormattedString("bookmarksArchiveFilename",
[date]);
@ -478,7 +496,7 @@ var PlacesOrganizer = {
var bookmarksBackupDir = dirSvc.get("ProfD", Ci.nsIFile);
bookmarksBackupDir.append("bookmarkbackups");
if (!bookmarksBackupDir.exists())
bookmarksBackupDir.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
bookmarksBackupDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0700);
return this.bookmarksBackupDir = bookmarksBackupDir;
},

Просмотреть файл

@ -86,6 +86,11 @@
<script type="application/javascript"
src="chrome://browser/content/places/editBookmarkOverlay.js"/>
<stringbundleset id="placesStringSet">
<stringbundle id="brandStrings" src="chrome://branding/locale/brand.properties"/>
</stringbundleset>
#ifdef XP_MACOSX
#include ../../../base/content/browserMountPoints.inc
#else
@ -346,20 +351,11 @@
#endif
id="maintenanceButton" label="&maintenance.label;">
<menupopup id="maintenanceButtonPopup">
<menuitem id="fileImport"
command="OrganizerCommand_import"
label="&cmd.import.label;"
accesskey="&cmd.import.accesskey;"/>
<menuitem id="fileExport"
command="OrganizerCommand_export"
label="&cmd.export.label;"
accesskey="&cmd.export.accesskey;"/>
<menuseparator/>
<menuitem id="backupBookmarks"
command="OrganizerCommand_backup"
label="&cmd.backup.label;"
accesskey="&cmd.backup.accesskey;"/>
<menu id="fileRestoreMenu" label="&cmd.restore.label;"
<menu id="fileRestoreMenu" label="&cmd.restore2.label;"
accesskey="&cmd.restore.accesskey;">
<menupopup id="fileRestorePopup" onpopupshowing="PlacesOrganizer.populateRestoreMenu();">
<menuitem id="restoreFromFile"
@ -368,6 +364,15 @@
accesskey="&cmd.restoreFromFile.accesskey;"/>
</menupopup>
</menu>
<menuseparator/>
<menuitem id="fileImport"
command="OrganizerCommand_import"
label="&cmd.importHTML.label;"
accesskey="&cmd.importHTML.accesskey;"/>
<menuitem id="fileExport"
command="OrganizerCommand_export"
label="&cmd.exportHTML.label;"
accesskey="&cmd.exportHTML.accesskey;"/>
</menupopup>
#ifdef XP_MACOSX
</toolbarbutton>

Просмотреть файл

@ -99,7 +99,7 @@ function run_test() {
} catch(ex) { do_throw("couldn't export to file: " + ex); }
LOG("exported json");
try {
PlacesUtils.restoreBookmarksFromFile(jsonFile);
PlacesUtils.restoreBookmarksFromJSONFile(jsonFile);
} catch(ex) { do_throw("couldn't import the exported file: " + ex); }
LOG("imported json");
validate();

Просмотреть файл

@ -31,8 +31,8 @@
<!ENTITY importFromEpiphany.accesskey "E">
<!ENTITY importFromGaleon.label "Galeon">
<!ENTITY importFromGaleon.accesskey "G">
<!ENTITY importFromFile.label "From File">
<!ENTITY importFromFile.accesskey "F">
<!ENTITY importFromHTMLFile.label "From an HTML File">
<!ENTITY importFromHTMLFile.accesskey "F">
<!ENTITY noMigrationSources.label "No programs that contain bookmarks, history or password data could be found.">

Просмотреть файл

@ -37,14 +37,14 @@
<!ENTITY cmd.findCurrent.label "Find in Current Collection…">
<!ENTITY cmd.findCurrent.accesskey "i">
<!ENTITY cmd.export.label "Export…">
<!ENTITY cmd.export.accesskey "E">
<!ENTITY cmd.import.label "Import…">
<!ENTITY cmd.import.accesskey "I">
<!ENTITY cmd.exportHTML.label "Export HTML…">
<!ENTITY cmd.exportHTML.accesskey "E">
<!ENTITY cmd.importHTML.label "Import HTML…">
<!ENTITY cmd.importHTML.accesskey "I">
<!ENTITY cmd.backup.label "Backup…">
<!ENTITY cmd.backup.accesskey "B">
<!ENTITY cmd.restore.label "Restore">
<!ENTITY cmd.restore2.label "Restore">
<!ENTITY cmd.restore.accesskey "R">
<!ENTITY cmd.restoreFromFile.label "Choose File…">
<!ENTITY cmd.restoreFromFile.accesskey "C">

Просмотреть файл

@ -23,6 +23,11 @@ bookmarksRestoreAlertTitle=Revert Bookmarks
bookmarksRestoreAlert=This will replace all of your current bookmarks with the backup. Are you sure?
bookmarksRestoreAlertTags=This will replace all of your current bookmarks and tags with the backup. Are you sure?
bookmarksRestoreTitle=Select a bookmarks backup
bookmarksRestoreFilterName=JSON
bookmarksRestoreFilterExtension=*.json
bookmarksRestoreFormatError=Unsupported file type.
bookmarksRestoreParseError=Unable to process the backup file.
headerTextPrefix1=Showing
headerTextPrefix2=Search Results for

Просмотреть файл

@ -948,47 +948,6 @@ var PlacesUtils = {
return urls;
},
/**
* Restores bookmarks/tags from a JSON file.
* WARNING: This method *removes* any bookmarks in the collection before
* restoring from the file.
*/
restoreBookmarksFromFile: function PU_restoreBookmarksFromFile(aFile) {
var errorStr = null;
var ioSvc = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
var fileURL = ioSvc.newFileURI(aFile).QueryInterface(Ci.nsIURL);
var fileExtension = fileURL.fileExtension.toLowerCase();
if (fileExtension == "json") {
try {
this.restoreBookmarksFromJSONFile(aFile);
} catch(ex) {
errorStr = this.getString("restoreParseError");
}
}
else {
errorStr = this.getString("restoreFormatError");
}
if (errorStr) {
const BRANDING_BUNDLE_URI = "chrome://branding/locale/brand.properties";
var brandShortName = Cc["@mozilla.org/intl/stringbundle;1"].
getService(Ci.nsIStringBundleService).
createBundle(BRANDING_BUNDLE_URI).
GetStringFromName("brandShortName");
var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
var win = wm.getMostRecentWindow(null);
Cc["@mozilla.org/embedcomp/prompt-service;1"].
getService(Ci.nsIPromptService).
alert(win, brandShortName, errorStr);
}
},
/**
* Restores bookmarks/tags from a JSON file.
* WARNING: This method *removes* any bookmarks in the collection before

Просмотреть файл

@ -118,7 +118,7 @@ function run_test() {
// restore json file
try {
PlacesUtils.restoreBookmarksFromFile(jsonFile);
PlacesUtils.restoreBookmarksFromJSONFile(jsonFile);
} catch(ex) { do_throw("couldn't import the exported file: " + ex); }
// validate

Просмотреть файл

@ -13,14 +13,9 @@ finduri-AgeInDays-isgreater=Older than %S days
localhost=(local files)
# LOCALIZATION NOTE (bookmarksBackupFilename & bookmarksArchiveFilename):
# LOCALIZATION NOTE (bookmarksArchiveFilename):
# %S will be replaced by the current date in ISO 8601 format, YYYY-MM-DD.
# The resulting string will be suggested as a filename, so make sure that you're
# only using characters legal for file names. Consider falling back to the
# en-US value if you have to use non-ascii characters.
bookmarksBackupFilename=Bookmarks %S.json
bookmarksBackupTitle=Bookmarks backup filename
bookmarksArchiveFilename=bookmarks-%S.json
restoreFormatError=Unsupported file type.
restoreParseError=Unable to process the backup file.