private static final Pattern ALL_PATH_SEPARATORS_REGEX = Pattern.compile("[/\\\\]");
+ private final String programDirPath;
+ private final File programDir;
+
private final String homeDirPath;
private final File homeDir;
private final String imagesDirPath;
private final File imagesDir;
+ private final String spellDirPath;
+ private final File spellDir;
+
+ private final String spellDirPathUser;
+ private final File spellDirUser;
+
private final String qssDirPath;
private final File qssDir;
private final File xmlDir;
+ private final String translateDirPath;
+ private final File translateDir;
+
/**
* Check or create the db, log and res directories.
*
* @param homeDirPath the installation dir containing db/log/res directories, must exist
* @throws InitializationException for missing directories or file permissions problems
*/
- public FileManager(String homeDirPath) throws InitializationException {
+ public FileManager(String homeDirPath, String programDirPath) throws InitializationException {
if (homeDirPath == null) {
throw new IllegalArgumentException("homeDirPath must not be null");
}
+ if (programDirPath == null) {
+ throw new IllegalArgumentException("programDirPath must not be null");
+ }
this.homeDir = new File(toPlatformPathSeparator(homeDirPath));
- checkExistingWriteableDir(homeDir);
+ this.programDir = new File(toPlatformPathSeparator(programDirPath));
+ createDirOrCheckWriteable(homeDir);
this.homeDirPath = slashTerminatePath(homeDir.getPath());
-
+ this.programDirPath = slashTerminatePath(programDir.getPath());
+
// Read-only
- imagesDir = new File(homeDir, "images");
+ imagesDir = new File(programDir, "images");
checkExistingReadableDir(imagesDir);
imagesDirPath = slashTerminatePath(imagesDir.getPath());
- qssDir = new File(homeDir, "qss");
+ qssDir = new File(programDir, "qss");
checkExistingReadableDir(qssDir);
qssDirPath = slashTerminatePath(qssDir.getPath());
- xmlDir = new File(homeDir, "xml");
+ spellDir = new File(programDir, "spell");
+ checkExistingReadableDir(spellDir);
+ spellDirPath = slashTerminatePath(spellDir.getPath());
+
+
+ spellDirUser = new File(homeDir, "spell");
+ createDirOrCheckWriteable(spellDirUser);
+ spellDirPathUser = slashTerminatePath(spellDirUser.getPath());
+
+ xmlDir = new File(programDir, "xml");
checkExistingReadableDir(xmlDir);
+ translateDir = new File(programDir, "translations");
+ checkExistingReadableDir(translateDir);
+ translateDirPath= slashTerminatePath(translateDir.getPath());
+
// Read-write
dbDir = new File(homeDir, "db");
createDirOrCheckWriteable(dbDir);
}
/**
- * Get a file below the base installation directory.
+ * Get a file below the base user home directory.
+ */
+ public File getProgramDirFile(String relativePath) {
+ return new File(programDir, toPlatformPathSeparator(relativePath));
+ }
+
+ /**
+ * Get a path below the base user home directory, using native {@link File#separator}.
+ * This will contain backslashes on Windows.
+ */
+ public String getProgramDirPath(String relativePath) {
+ return programDirPath + toPlatformPathSeparator(relativePath);
+ }
+
+ /**
+ * Get a file below the base user home directory.
*/
public File getHomeDirFile(String relativePath) {
return new File(homeDir, toPlatformPathSeparator(relativePath));
}
/**
- * Get a path below the base installation directory, using native {@link File#separator}.
+ * Get a path below the base user home directory, using native {@link File#separator}.
* This will contain backslashes on Windows.
*/
public String getHomeDirPath(String relativePath) {
}
/**
- * Get a path below the 'db' directory, using native {@link File#separator}.
+ * Get a path below the 'spell' directory, using native {@link File#separator}.
* This will contain backslashes on Windows.
*/
- public String getDbDirPath(String relativePath) {
+ public String getSpellDirPath(String relativePath) {
return dbDirPath + toPlatformPathSeparator(relativePath);
}
/**
+ * Get a file below the 'spell' directory.
+ */
+ public File getSpellDirFile(String relativePath) {
+ return new File(spellDir, toPlatformPathSeparator(relativePath));
+ }
+
+ /**
+ * Get the spell directory for the jazzy word list
+ */
+ public String getSpellDirPath() {
+ return spellDirPath;
+ }
+
+ /**
+ * Get a file below the 'spell' directory for user dictionaries.
+ */
+ public File getSpellDirFileUser(String relativePath) {
+ return new File(spellDirUser, toPlatformPathSeparator(relativePath));
+ }
+
+ /**
+ * Get the spell directory for the jazzy word list (user dictionary).
+ */
+ public String getSpellDirPathUser() {
+ return spellDirPathUser;
+ }
+
+ /**
+ * Get a path below the 'db' directory, using native {@link File#separator}.
+ * This will contain backslashes on Windows.
+ */
+ public String getDbDirPath(String relativePath) {
+ return dbDirPath + toPlatformPathSeparator(relativePath);
+ }
+
+ /**
* Get a file below the 'images' directory.
*/
public File getImageDirFile(String relativePath) {
return new File(xmlDir, toPlatformPathSeparator(relativePath));
}
+ /**
+ * Get a path below the 'translate' directory, using native {@link File#separator}.
+ * This will contain backslashes on Windows.
+ */
+ public String getTranslateFilePath(String relativePath) {
+ return translateDirPath + toPlatformPathSeparator(relativePath);
+ }
+
private static String toPlatformPathSeparator(String relativePath) {
+ // Sometimes a space in the file name comes across as a %20. This is to put it back as a space.
+ relativePath = relativePath.replace("%20", " ");
return ALL_PATH_SEPARATORS_REGEX.matcher(relativePath).replaceAll(
// Must double-escape backslashes,
// because they have special meaning in the replacement string of Matcher.replaceAll
*
* @throws InitializationException for file deletion failures
*/
- private static void deleteTopLevelFiles(File dir) throws InitializationException {
+ private static void deleteTopLevelFiles(File dir, boolean exitOnFail) throws InitializationException {
File[] toDelete = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
}
});
for (File f : toDelete) {
- if (!f.delete()) {
+ if (!f.delete() && exitOnFail) {
throw new InitializationException("Failed to delete file: '" + f + "'");
}
}
/**
* @throws InitializationException if non-existent, bad file permissions, or a file instead of a directory
*/
- private static void checkExistingWriteableDir(File dir) throws InitializationException {
+ @SuppressWarnings("unused")
+ private static void checkExistingWriteableDir(File dir) throws InitializationException {
checkExistingReadableDir(dir);
if (!dir.canWrite()) {
throw new InitializationException("Directory '" + dir + "' does not have write permission");
/**
* Called at startup to purge files from 'res' directory.
*/
- public void purgeResDirectory() throws InitializationException {
- deleteTopLevelFiles(resDir);
+ public void purgeResDirectory(boolean exitOnFail) throws InitializationException {
+ deleteTopLevelFiles(resDir, exitOnFail);
}
}