zotero/zotero — pull request #51 by willsALMANJ (2012-02-13 15:46:28+00:00)
Title: Relative paths to linked files

This patch implements relative paths for linked file attachments in Zotero.

How it works:
The user sets a file path in preferences as the base attachment path. Any linked file attachment that is saved on that path is saved relative to that path. When that attachment is opened, the path to the file constructed using the current base attachment path and the saved relative path.

How it is implemented:
The main changes were made in item.js. The function Item.save() was modified at the two points where it saves attachments to the SQL database. At those points, it is checked if the attachment being saved is a linked file and then if it is on the base attachment path (if a path has been set in preferences). If so, the path is saved as the string "" plus the relative path from the base attachment path to the attachment file. The getter function of Item.attachmentPath was changed so that it will check the beginning of linked file paths for the string "" and, if that path is found, it will replace it with the current base attachment path set in preferences.

I put the GUI elements for setting the base attachment path in the Advanced tab of Zotero's preferences window. Adding this preference and the GUI elements required modifying defaults/preferences/zotero.js, preferences.xul, preferences.dtd, and zotero.properties. Additionally, two functions were added to preferences.js for handling the base attachment path in a similar fashion to how the Zotero data directory is handled.

Quality of code:
I tested the usage cases I could think of for this patch (saving attachment on the base path, moving the base path, moving the attachment, saving not on the base path...). Since the attachmentPath getter should always return a full path, the attachmentPath setter was unchanged, and the rest of Zotero uses Item.attachmentPath to interact with the attachment path, there should be no change in behavior with this patch. I tried to order if statements in such a way as to minimize querying preferences, file paths, and the SQL database, so I think this patch should not slow Zotero down if the user does not want to use relative file paths. Obviously, you might have a different opinion about how the base attachment path is set -- I just put it in advanced because it seemed like an uncommon usage and put it at the top of the advanced tab because it came first alphabetically. This is my second pull request, and I am still getting used to using git. You can ignore the early commits. Next time I will try branching and merging locally so you only see the final commit (which I think is the proper way to submit a patch?).