diff --git a/chrome/content/zotero/preferences/preferences_advanced.js b/chrome/content/zotero/preferences/preferences_advanced.js
index f99e41d10c..e5eee66599 100644
--- a/chrome/content/zotero/preferences/preferences_advanced.js
+++ b/chrome/content/zotero/preferences/preferences_advanced.js
@@ -323,69 +323,77 @@ Zotero_Preferences.Advanced = {
Zotero_Preferences.Attachment_Base_Directory = {
- choosePath: function () {
- // 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);
+ getPath: function () {
+ var oldPath = Zotero.Prefs.get('baseAttachmentPath');
+ if (oldPath) {
try {
- oldBasePathFile.persistentDescriptor = oldBasePath;
+ return OS.Path.normalize(oldPath);
}
catch (e) {
- Zotero.debug(e, 1);
- Components.utils.reportError(e);
- oldBasePathFile = null;
+ Zotero.logError(e);
+ return false;
}
}
+ },
+
+
+ choosePath: Zotero.Promise.coroutine(function* () {
+ var oldPath = this.getPath();
//Prompt user to choose new base path
+ if (oldPath) {
+ var oldPathFile = Zotero.File.pathToFile(oldPath);
+ }
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
- if (oldBasePathFile) {
- fp.displayDirectory = oldBasePathFile;
+ if (oldPathFile) {
+ fp.displayDirectory = oldPathFile;
}
fp.init(window, Zotero.getString('attachmentBasePath.selectDir'), nsIFilePicker.modeGetFolder);
fp.appendFilters(nsIFilePicker.filterAll);
if (fp.show() != nsIFilePicker.returnOK) {
return false;
}
- var newBasePathFile = fp.file;
+ var newPath = OS.Path.normalize(fp.file.path);
- if (oldBasePathFile && oldBasePathFile.equals(newBasePathFile)) {
+ if (oldPath && oldPath == newPath) {
Zotero.debug("Base directory hasn't changed");
return false;
}
+ return changePath(newPath);
+ }),
+
+
+ changePath: Zotero.Promise.coroutine(function* (basePath) {
// Find all current attachments with relative attachment paths
var sql = "SELECT itemID FROM itemAttachments WHERE linkMode=? AND path LIKE '"
+ Zotero.Attachments.BASE_PATH_PLACEHOLDER + "%'";
var params = [Zotero.Attachments.LINK_MODE_LINKED_FILE];
- var oldRelativeAttachmentIDs = Zotero.DB.columnQuery(sql, params) || [];
+ var oldRelativeAttachmentIDs = yield Zotero.DB.columnQueryAsync(sql, params);
//Find all attachments on the new base path
var sql = "SELECT itemID FROM itemAttachments WHERE linkMode=?";
var params = [Zotero.Attachments.LINK_MODE_LINKED_FILE];
- var allAttachments = Zotero.DB.columnQuery(sql,params);
+ var allAttachments = yield Zotero.DB.columnQueryAsync(sql, params);
var newAttachmentPaths = {};
var numNewAttachments = 0;
var numOldAttachments = 0;
- var attachmentFile = Components.classes["@mozilla.org/file/local;1"]
- .createInstance(Components.interfaces.nsILocalFile);
for (let i=0; i
-
diff --git a/test/tests/preferences_advancedTest.js b/test/tests/preferences_advancedTest.js
new file mode 100644
index 0000000000..bc4181e825
--- /dev/null
+++ b/test/tests/preferences_advancedTest.js
@@ -0,0 +1,129 @@
+describe("Advanced Preferences", function () {
+ describe("Files & Folders", function () {
+ describe("Linked Attachment Base Directory", function () {
+ var setBaseDirectory = Zotero.Promise.coroutine(function* (basePath) {
+ var win = yield loadWindow("chrome://zotero/content/preferences/preferences.xul", {
+ pane: 'zotero-prefpane-advanced',
+ tabIndex: 1
+ });
+
+ // Wait for tab to load
+ var doc = win.document;
+ var prefwindow = doc.documentElement;
+ var defer = Zotero.Promise.defer();
+ var pane = doc.getElementById('zotero-prefpane-advanced');
+ if (!pane.loaded) {
+ pane.addEventListener('paneload', function () {
+ defer.resolve();
+ })
+ yield defer.promise;
+ }
+
+ var promise = waitForDialog();
+ yield win.Zotero_Preferences.Attachment_Base_Directory.changePath(basePath);
+ yield promise;
+
+ win.close();
+ });
+
+ var clearBaseDirectory = Zotero.Promise.coroutine(function* (basePath) {
+ var win = yield loadWindow("chrome://zotero/content/preferences/preferences.xul", {
+ pane: 'zotero-prefpane-advanced',
+ tabIndex: 1
+ });
+
+ // Wait for tab to load
+ var doc = win.document;
+ var prefwindow = doc.documentElement;
+ var defer = Zotero.Promise.defer();
+ var pane = doc.getElementById('zotero-prefpane-advanced');
+ if (!pane.loaded) {
+ pane.addEventListener('paneload', function () {
+ defer.resolve();
+ })
+ yield defer.promise;
+ }
+
+ var promise = waitForDialog();
+ yield win.Zotero_Preferences.Attachment_Base_Directory.clearPath();
+ yield promise;
+
+ win.close();
+ });
+
+ beforeEach(function () {
+ Zotero.Prefs.clear('baseAttachmentPath');
+ Zotero.Prefs.clear('saveRelativeAttachmentPath');
+ });
+
+ it("should set new base directory", function* () {
+ var basePath = getTestDataDirectory().path;
+ yield setBaseDirectory(basePath);
+ assert.equal(Zotero.Prefs.get('baseAttachmentPath'), basePath);
+ assert.isTrue(Zotero.Prefs.get('saveRelativeAttachmentPath'));
+ })
+
+ it("should clear base directory", function* () {
+ var basePath = getTestDataDirectory().path;
+ yield setBaseDirectory(basePath);
+ yield clearBaseDirectory();
+
+ assert.equal(Zotero.Prefs.get('baseAttachmentPath'), '');
+ assert.isFalse(Zotero.Prefs.get('saveRelativeAttachmentPath'));
+ })
+
+ it("should change absolute path of linked attachment under new base dir to prefixed path", function* () {
+ var file = getTestDataDirectory();
+ file.append('test.png');
+ var attachment = yield Zotero.Attachments.linkFromFile({ file });
+ assert.equal(attachment.attachmentPath, file.path);
+
+ var basePath = getTestDataDirectory().path;
+ yield setBaseDirectory(basePath);
+
+ assert.equal(
+ attachment.attachmentPath,
+ Zotero.Attachments.BASE_PATH_PLACEHOLDER + 'test.png'
+ );
+ })
+
+ it("should change prefixed path to absolute when changing base directory", function* () {
+ var basePath = getTestDataDirectory().path;
+ yield setBaseDirectory(basePath);
+
+ var file = getTestDataDirectory();
+ file.append('test.png');
+ var attachment = yield Zotero.Attachments.linkFromFile({ file });
+ assert.equal(
+ attachment.attachmentPath,
+ Zotero.Attachments.BASE_PATH_PLACEHOLDER + 'test.png'
+ );
+
+ var basePath = Zotero.getTempDirectory().path;
+ yield setBaseDirectory(basePath);
+
+ assert.equal(attachment.attachmentPath, file.path);
+ })
+
+ it("should change prefixed path to absolute when clearing base directory", function* () {
+ var basePath = getTestDataDirectory().path;
+ yield setBaseDirectory(basePath);
+
+ var file = getTestDataDirectory();
+ file.append('test.png');
+ var attachment = yield Zotero.Attachments.linkFromFile({ file });
+ assert.equal(
+ attachment.attachmentPath,
+ Zotero.Attachments.BASE_PATH_PLACEHOLDER + 'test.png'
+ );
+
+ yield clearBaseDirectory();
+
+ assert.equal(Zotero.Prefs.get('baseAttachmentPath'), '');
+ assert.isFalse(Zotero.Prefs.get('saveRelativeAttachmentPath'));
+
+ assert.equal(attachment.attachmentPath, file.path);
+ })
+ })
+ })
+})