OSDN Git Service

Cleanup of initial sync with ink notes and added the ability to highlight words in...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / config / FileManager.java
index d27764a..5f51497 100644 (file)
@@ -13,6 +13,9 @@ public class FileManager {
 
     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;
 
@@ -24,6 +27,12 @@ public class FileManager {
     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;
 
@@ -32,32 +41,54 @@ public class FileManager {
 
     private final File xmlDir;
 
+    private final String translateDirPath;
+    private final File translateDir;
+
     /**
-     * Check or create the db, log and res directories, and purge files from 'res' .
+     * 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);
@@ -69,19 +100,32 @@ public class FileManager {
         resDir = new File(homeDir, "res");
         createDirOrCheckWriteable(resDir);
         resDirPath = slashTerminatePath(resDir.getPath());
+    }
 
-        deleteTopLevelFiles(resDir);
+    /**
+     * Get a file below the base user home directory.
+     */
+    public File getProgramDirFile(String relativePath) {
+        return new File(programDir, toPlatformPathSeparator(relativePath));
     }
 
     /**
-     * Get a file below the base installation directory.
+     * 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) {
@@ -96,14 +140,50 @@ public class FileManager {
     }
 
     /**
-     * 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) {
@@ -156,8 +236,21 @@ public class FileManager {
         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) {
-        return ALL_PATH_SEPARATORS_REGEX.matcher(relativePath).replaceAll(File.separator);
+       // 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
+                               (File.separator.equals("\\") ? "\\\\" : File.separator));
     }
 
     private static String slashTerminatePath(String path) {
@@ -172,7 +265,7 @@ public class FileManager {
      *
      * @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) {
@@ -180,7 +273,7 @@ public class FileManager {
             }
         });
         for (File f : toDelete) {
-            if (!f.delete()) {
+            if (!f.delete() && exitOnFail) {
                 throw new InitializationException("Failed to delete file: '" + f + "'");
             }
         }
@@ -226,10 +319,18 @@ public class FileManager {
     /**
      * @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(boolean exitOnFail) throws InitializationException {
+        deleteTopLevelFiles(resDir, exitOnFail);
+    }
 }