From 27a2a9c1f7e9208ba54f225d0b30efc54ca10f4c Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 21 Feb 2013 04:40:45 -0500 Subject: [PATCH] More relative path changes - If attachment exists at same relative path in new base directory, leave it alone so that it continues to work. - If attachment doesn't exist in new base directory, revert it to an absolute path. - If new base directory is an ancestor or descendant of the previous base directory, adjust relative paths below the new directory so that they keep working. - More dialog changes - Select current base directory in file picker when changing directory - Always use .persistentDescriptor instead of initWithPath(), though it probably doesn't matter, and wrap in try/catch in case the old setting is broken in some way. - New function Zotero.File.directoryContains(dir, file), since nsIFile.contains() isn't recursive - Don't use a private Zotero.Item property from outside to force path changes. --- .../content/zotero/preferences/preferences.js | 127 +++++++++++++----- .../zotero/preferences/preferences.xul | 2 +- chrome/content/zotero/xpcom/data/item.js | 119 +++++++++++----- chrome/content/zotero/xpcom/file.js | 26 ++++ chrome/content/zotero/zoteroPane.js | 1 + chrome/locale/en-US/zotero/preferences.dtd | 2 +- chrome/locale/en-US/zotero/zotero.properties | 22 +-- 7 files changed, 219 insertions(+), 80 deletions(-) diff --git a/chrome/content/zotero/preferences/preferences.js b/chrome/content/zotero/preferences/preferences.js index 43a018dd7..1b293ed43 100644 --- a/chrome/content/zotero/preferences/preferences.js +++ b/chrome/content/zotero/preferences/preferences.js @@ -131,16 +131,39 @@ function init() function chooseBaseAttachmentPath() { + // Get existing base directory + var oldBasePath = Zotero.Prefs.get('baseAttachmentPath'); + if (oldBasePath) { + var oldBasePathFile = Components.classes["@mozilla.org/file/local;1"] + .createInstance(Components.interfaces.nsILocalFile); + try { + oldBasePathFile.persistentDescriptor = oldBasePath; + } + catch (e) { + Zotero.debug(e, 1); + Components.utils.reportError(e); + oldBasePathFile = null; + } + } + //Prompt user to choose new base path var nsIFilePicker = Components.interfaces.nsIFilePicker; var fp = Components.classes["@mozilla.org/filepicker;1"] .createInstance(nsIFilePicker); + if (oldBasePathFile) { + fp.displayDirectory = oldBasePathFile; + } fp.init(window, Zotero.getString('attachmentBasePath.selectDir'), nsIFilePicker.modeGetFolder); fp.appendFilters(nsIFilePicker.filterAll); if (fp.show() != nsIFilePicker.returnOK) { return false; } - var basePathFile = fp.file; + var newBasePathFile = fp.file; + + if (oldBasePathFile && oldBasePathFile.equals(newBasePathFile)) { + Zotero.debug("Base directory hasn't changed"); + return false; + } //Find all current attachments with relative attachment paths var sql = "SELECT itemID FROM itemAttachments WHERE linkMode=? AND path LIKE '" + @@ -152,28 +175,49 @@ function chooseBaseAttachmentPath() { var sql = "SELECT itemID FROM itemAttachments WHERE linkMode=?"; var params=[Zotero.Attachments.LINK_MODE_LINKED_FILE]; var allAttachments = Zotero.DB.columnQuery(sql,params); - var newRelativeAttachmentIDs = []; + var newAttachmentPaths = {}; + var numNewAttachments = 0; + var numOldAttachments = 0; var attachmentFile = Components.classes["@mozilla.org/file/local;1"] .createInstance(Components.interfaces.nsILocalFile); - var tempAttachmentID,tempAttachment; - for (var index=0; index -