Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default directory when adding files to an entry #459

Merged
merged 5 commits into from
Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/JabRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
}
MetaData metaData = pr.getMetaData();
metaData.setFile(theFile);
Globals.prefs.fileDirForDatabase = metaData.getFileDirectory(Globals.FILE_FIELD);
Globals.prefs.fileDirForDatabase = metaData.getFileDirectory(Globals.FILE_FIELD).toArray(new String[0]);
Globals.prefs.databaseFile = metaData.getFile();
System.out.println(Localization.lang("Exporting") + ": " + data[0]);
IExportFormat format = ExportFormats.getExportFormat(data[1]);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ public final class JabRefPreferences {
public static final String DB_CONNECT_HOSTNAME = "dbConnectHostname";
public static final String DB_CONNECT_SERVER_TYPE = "dbConnectServerType";
public static final String BIB_LOC_AS_PRIMARY_DIR = "bibLocAsPrimaryDir";
public static final String BIB_LOCATION_AS_FILE_DIR = "bibLocationAsFileDir";
public static final String SELECTED_FETCHER_INDEX = "selectedFetcherIndex";
public static final String WEB_SEARCH_VISIBLE = "webSearchVisible";
public static final String ALLOW_FILE_AUTO_OPEN_BROWSE = "allowFileAutoOpenBrowse";
Expand Down Expand Up @@ -744,7 +743,6 @@ private JabRefPreferences() {
defaults.put(ALLOW_FILE_AUTO_OPEN_BROWSE, Boolean.TRUE);
defaults.put(WEB_SEARCH_VISIBLE, Boolean.FALSE);
defaults.put(SELECTED_FETCHER_INDEX, 0);
defaults.put(BIB_LOCATION_AS_FILE_DIR, Boolean.TRUE);
defaults.put(BIB_LOC_AS_PRIMARY_DIR, Boolean.FALSE);
defaults.put(DB_CONNECT_SERVER_TYPE, "MySQL");
defaults.put(DB_CONNECT_HOSTNAME, "localhost");
Expand Down
57 changes: 33 additions & 24 deletions src/main/java/net/sf/jabref/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,34 @@ public void putData(String key, Vector<String> orderedData) {
/**
* Look up the directory set up for the given field type for this database.
* If no directory is set up, return that defined in global preferences.
* There can be up to three directory definitions for these files:
* the database's metadata can specify a general directory and/or a user-specific directory
* or the preferences can specify one.
*
* The settings are prioritized in the following order and the first defined setting is used:
* 1. metadata user-specific directory
* 2. metadata general directory
* 3. preferences directory
* 4. bib file directory
*
* @param fieldName The field type
* @return The default directory for this field type.
*/
public String[] getFileDirectory(String fieldName) {
// There can be up to three directory definitions for these files - the database's
// metadata can specify a general directory and/or a user-specific directory, or
// the preferences can specify one. The settings are prioritized in the following
// order and the first defined setting is used: metadata user-specific directory,
// metadata general directory, preferences directory.
String key = Globals.prefs.get(JabRefPreferences.USER_FILE_DIR_INDIVIDUAL);
List<String> dirs = new ArrayList<>();

Vector<String> vec = getData(key);
if (vec == null) {
key = Globals.prefs.get(JabRefPreferences.USER_FILE_DIR);
vec = getData(key);
public List<String> getFileDirectory(String fieldName) {
List<String> fileDirs = new ArrayList<>();

// 1. metadata user-specific directory
String key = Globals.prefs.get(JabRefPreferences.USER_FILE_DIR_INDIVIDUAL); // USER_SPECIFIC_FILE_DIR_FOR_DB
List<String> metaData = getData(key);
if (metaData == null) {
key = Globals.prefs.get(JabRefPreferences.USER_FILE_DIR); // FILE_DIR_FOR_DB
metaData = getData(key);
}
if ((vec != null) && !vec.isEmpty()) {

// 2. metadata general directory
if ((metaData != null) && !metaData.isEmpty()) {
String dir;
dir = vec.get(0);
dir = metaData.get(0);
// If this directory is relative, we try to interpret it as relative to
// the file path of this bib file:
if (!new File(dir).isAbsolute() && (file != null)) {
Expand All @@ -188,33 +195,35 @@ public String[] getFileDirectory(String fieldName) {
// if dir is only "current" directory, just use its parent (== real current directory) as path
relDir = file.getParent();
} else {
relDir = file.getParent() + System.getProperty("file.separator") + dir;
relDir = file.getParent() + File.separator + dir;
}
// If this directory actually exists, it is very likely that the
// user wants us to use it:
if (new File(relDir).exists()) {
dir = relDir;
}
}
dirs.add(dir);
fileDirs.add(dir);
} else {
String dir = Globals.prefs.get(fieldName + Globals.DIR_SUFFIX);
// 3. preferences directory?
String dir = Globals.prefs.get(fieldName + Globals.DIR_SUFFIX); // FILE_DIR
if (dir != null) {
dirs.add(dir);
fileDirs.add(dir);
}
}

// Check if the bib file location should be included, and if so, if it is set:
if (Globals.prefs.getBoolean(JabRefPreferences.BIB_LOCATION_AS_FILE_DIR) && (getFile() != null)) {
// 4. bib file directory
if (getFile() != null) {
String parentDir = getFile().getParent();
// Check if we should add it as primary file dir (first in the list) or not:
if (Globals.prefs.getBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR)) {
dirs.add(0, getFile().getParent());
fileDirs.add(0, parentDir);
} else {
dirs.add(getFile().getParent());
fileDirs.add(parentDir);
}
}

return dirs.toArray(new String[dirs.size()]);
return fileDirs;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/exporter/ExportFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void actionPerformed(ActionEvent e) {
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().metaData()
.getFileDirectory(Globals.FILE_FIELD);
.getFileDirectory(Globals.FILE_FIELD).toArray(new String[0]);
// Also store the database's file in a global variable:
Globals.prefs.databaseFile = frame.getCurrentBasePanel().metaData().getFile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void run() {
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().metaData()
.getFileDirectory(Globals.FILE_FIELD);
.getFileDirectory(Globals.FILE_FIELD).toArray(new String[0]);
// Also store the database's file in a global variable:
Globals.prefs.databaseFile = frame.getCurrentBasePanel().metaData().getFile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.sf.jabref.model.entry.FileField.ParsedFileField;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -75,7 +76,7 @@ public String format(String field) {
dirs = new String[] {Globals.prefs.get(Globals.FILE_FIELD + Globals.DIR_SUFFIX)};
}

File f = FileUtil.expandFilename(link, dirs);
File f = FileUtil.expandFilename(link, Arrays.asList(dirs));

/*
* Stumbled over this while investigating
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public String format(String field) {
dirs = new String[] {Globals.prefs.get(Globals.FILE_FIELD + Globals.DIR_SUFFIX)};
}

File f = FileUtil.expandFilename(flEntry.link, dirs);
File f = FileUtil.expandFilename(flEntry.link, Arrays.asList(dirs));

/*
* Stumbled over this while investigating
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/sf/jabref/external/DownloadExternalFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
* This class handles the download of an external file. Typically called when the user clicks
Expand Down Expand Up @@ -156,12 +157,12 @@ public void run() {
}

String suggestedName = getSuggestedFileName(suffix);
String[] fDirectory = getFileDirectory();
List<String> fDirectory = getFileDirectory();
final String directory;
if (fDirectory.length == 0) {
if (fDirectory.size() == 0) {
directory = null;
} else {
directory = fDirectory[0];
directory = fDirectory.get(0);
}
final String suggestDir = directory != null ? directory : System.getProperty("user.home");
File file = new File(new File(suggestDir), suggestedName);
Expand Down Expand Up @@ -344,7 +345,7 @@ private String getSuffix(final String link) {

}

private String[] getFileDirectory() {
private List<String> getFileDirectory() {
return metaData.getFileDirectory(Globals.FILE_FIELD);
}

Expand All @@ -354,7 +355,6 @@ private String[] getFileDirectory() {
* notification when download is complete.
*/
public interface DownloadCallback {

void downloadComplete(FileListEntry file);
}
}
32 changes: 16 additions & 16 deletions src/main/java/net/sf/jabref/external/DroppedFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ private boolean showLinkMoveCopyRenameDialog(String linkFileName, ExternalFileTy
BibEntry entry, boolean newEntry, final boolean multipleEntries, BibDatabase database) {

String dialogTitle = Localization.lang("Link to file %0", linkFileName);
String[] dirs = panel.metaData().getFileDirectory(Globals.FILE_FIELD);
List<String> dirs = panel.metaData().getFileDirectory(Globals.FILE_FIELD);
int found = -1;
for (int i = 0; i < dirs.length; i++) {
if (new File(dirs[i]).exists()) {
for (int i = 0; i < dirs.size(); i++) {
if (new File(dirs.get(i)).exists()) {
found = i;
break;
}
Expand All @@ -364,7 +364,7 @@ private boolean showLinkMoveCopyRenameDialog(String linkFileName, ExternalFileTy
renameCheckBox.setEnabled(false);
linkInPlace.setSelected(true);
} else {
destDirLabel.setText(Localization.lang("File directory is '%0':", dirs[found]));
destDirLabel.setText(Localization.lang("File directory is '%0':", dirs.get(found)));
copyRadioButton.setEnabled(true);
moveRadioButton.setEnabled(true);
renameToTextBox.setEnabled(true);
Expand Down Expand Up @@ -452,14 +452,14 @@ private void doLink(BibEntry entry, ExternalFileType fileType, String filename,
// If avoidDuplicate==true, we should check if this file is already linked:
if (avoidDuplicate) {
// For comparison, find the absolute filename:
String[] dirs = panel.metaData().getFileDirectory(Globals.FILE_FIELD);
String absFilename = !new File(filename).isAbsolute() && (dirs.length > 0) ?
List<String> dirs = panel.metaData().getFileDirectory(Globals.FILE_FIELD);
String absFilename = !new File(filename).isAbsolute() && (dirs.size() > 0) ?
FileUtil.expandFilename(filename, dirs).getAbsolutePath() : filename;

for (int i = 0; i < tm.getRowCount(); i++) {
FileListEntry flEntry = tm.getEntry(i);
// Find the absolute filename for this existing link:
String absName = !new File(flEntry.link).isAbsolute() && (dirs.length > 0) ?
String absName = !new File(flEntry.link).isAbsolute() && (dirs.size() > 0) ?
FileUtil.expandFilename(flEntry.link, dirs).getAbsolutePath() : flEntry.link;
System.out.println("absName: " + absName);
// If the filenames are equal, we don't need to link, so we simply return:
Expand Down Expand Up @@ -494,10 +494,10 @@ private void doLink(BibEntry entry, ExternalFileType fileType, String filename,
*/
private boolean doMove(String fileName, ExternalFileType fileType, String destFilename,
NamedCompound edits) {
String[] dirs = panel.metaData().getFileDirectory(Globals.FILE_FIELD);
List<String> dirs = panel.metaData().getFileDirectory(Globals.FILE_FIELD);
int found = -1;
for (int i = 0; i < dirs.length; i++) {
if (new File(dirs[i]).exists()) {
for (int i = 0; i < dirs.size(); i++) {
if (new File(dirs.get(i)).exists()) {
found = i;
break;
}
Expand All @@ -509,7 +509,7 @@ private boolean doMove(String fileName, ExternalFileType fileType, String destFi
return false;
}
File fromFile = new File(fileName);
File toFile = new File(dirs[found] + System.getProperty("file.separator") + destFilename);
File toFile = new File(dirs.get(found) + System.getProperty("file.separator") + destFilename);
if (toFile.exists()) {
int answer = JOptionPane.showConfirmDialog(frame,
Localization.lang("'%0' exists. Overwrite file?", toFile.getAbsolutePath()),
Expand Down Expand Up @@ -545,23 +545,23 @@ private boolean doMove(String fileName, ExternalFileType fileType, String destFi
private boolean doCopy(String fileName, ExternalFileType fileType, String toFile,
NamedCompound edits) {

String[] dirs = panel.metaData().getFileDirectory(Globals.FILE_FIELD);
List<String> dirs = panel.metaData().getFileDirectory(Globals.FILE_FIELD);
int found = -1;
for (int i = 0; i < dirs.length; i++) {
if (new File(dirs[i]).exists()) {
for (int i = 0; i < dirs.size(); i++) {
if (new File(dirs.get(i)).exists()) {
found = i;
break;
}
}
if (found < 0) {
// OOps, we don't know which directory to put it in, or the given
// dir doesn't exist....
System.out.println("dir: " + dirs[found] + "\t ext: " + fileType.getExtension());
System.out.println("dir: " + dirs.get(found) + "\t ext: " + fileType.getExtension());
return false;
}
toFile = new File(toFile).getName();

File destFile = new File(dirs[found] + System.getProperty("file.separator") + toFile);
File destFile = new File(dirs.get(found) + System.getProperty("file.separator") + toFile);
if (destFile.equals(new File(fileName))) {
// File is already in the correct position. Don't override!
return true;
Expand Down
Loading