1 21 22 package org.armedbear.j; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.util.Enumeration ; 28 import java.util.Properties ; 29 30 public final class Autosave implements Constants 31 { 32 private static final String CATALOG_NAME = "catalog"; 33 private static Properties catalog; 34 private static File catalogFile; 35 36 private static boolean autosaveEnabled = true; 37 private static File autosaveDirectory; 38 private static boolean initialized; 39 40 public static synchronized File getAutosaveDirectory() 41 { 42 if (!initialized) { 43 autosaveDirectory = 44 File.getInstance(Directories.getEditorDirectory(), "autosave"); 45 if (autosaveDirectory != null) { 46 if (!autosaveDirectory.isDirectory()) { 47 autosaveDirectory.mkdirs(); 48 if (!autosaveDirectory.isDirectory()) { 49 autosaveDirectory = null; 50 autosaveEnabled = false; 51 } 52 } 53 } 54 initialized = true; 55 } 56 return autosaveDirectory; 57 } 58 59 public static synchronized final boolean isAutosaveEnabled() 60 { 61 return autosaveEnabled; 62 } 63 64 public static synchronized void put(String netPath, String alias) 65 { 66 if (catalog == null) 67 catalog = new Properties (); 68 catalog.put(netPath, alias); 69 } 70 71 public static synchronized void rename(String oldName, String newName) 73 { 74 if (catalog != null && oldName != null) { 75 String alias = (String ) catalog.remove(oldName); 76 if (alias != null) { 77 catalog.put(newName, alias); 78 flush(); 79 } 80 } 81 } 82 83 public static synchronized void flush() 84 { 85 if (catalogFile == null) { 86 if (getAutosaveDirectory() == null) 87 return; 88 catalogFile = File.getInstance(getAutosaveDirectory(), CATALOG_NAME); 89 } 90 try { 91 OutputStream out = catalogFile.getOutputStream(); 92 catalog.store(out, null); 93 out.flush(); 94 out.close(); 95 } 96 catch (IOException e) { 97 Log.error(e); 98 } 99 } 100 101 public static synchronized void deleteCatalogFile() 102 { 103 if (catalogFile == null) { 104 if (getAutosaveDirectory() == null) 105 return; 106 catalogFile = File.getInstance(getAutosaveDirectory(), CATALOG_NAME); 107 } 108 catalogFile.delete(); 109 } 110 111 public static synchronized void recover() 112 { 113 if (catalogFile == null) { 114 if (getAutosaveDirectory() == null) 115 return; 116 catalogFile = File.getInstance(getAutosaveDirectory(), CATALOG_NAME); 117 } 118 if (!catalogFile.exists()) 119 return; if (catalog == null) 121 catalog = new Properties (); 122 try { 123 InputStream in = catalogFile.getInputStream(); 124 catalog.load(in); 125 in.close(); 126 } 127 catch (IOException e) { 128 Log.error(e); 129 } 130 Enumeration e = catalog.propertyNames(); 131 while (e.hasMoreElements()) { 132 String netPath = (String ) e.nextElement(); 133 String alias = catalog.getProperty(netPath); 134 if (alias != null) 135 queryRecoverFile(netPath, alias); 136 } 137 catalogFile.delete(); 138 } 139 140 private static void queryRecoverFile(String netPath, String alias) 141 { 142 File autosaveFile = File.getInstance(getAutosaveDirectory(), alias); 143 if (!autosaveFile.exists()) { 144 catalog.remove(netPath); 146 return; 147 } 148 final File file = File.getInstance(netPath); 149 if (file == null) 150 return; boolean confirmed = false; 152 File dir = file.getParentFile(); 153 if (dir != null && dir.equals(Directories.getDraftsFolder())) 154 confirmed = true; 155 if (!confirmed) { 156 String prompt = "Recover " + netPath + " from autosave file?"; 157 int response = ConfirmDialog.showConfirmDialog(null, prompt, "Autosave"); 158 if (response == RESPONSE_YES) 159 confirmed = true; 160 } 161 if (confirmed) { 162 if (file.isRemote()) { 163 String recoverPath = file.getHostName() + '/' + file.canonicalPath(); 164 Log.debug("recoverPath = |" + recoverPath + "|"); 165 File recoverFile = File.getInstance(getRecoverDirectory(), recoverPath); 166 Log.debug("recoverFile = |" + recoverFile.netPath() + "|"); 167 File parentDir = recoverFile.getParentFile(); 168 if (!parentDir.isDirectory()) 169 parentDir.mkdirs(); 170 if (parentDir.isDirectory()) { 171 recoverFile.delete(); 172 if (autosaveFile.renameTo(recoverFile) || Utilities.copyFile(autosaveFile, recoverFile)) { 174 autosaveFile.delete(); 175 catalog.remove(netPath); 176 String message = "File has been saved as " + recoverFile.canonicalPath() + "."; 177 MessageDialog.showMessageDialog(message, "Autosave"); 178 } 179 } 180 } else { 181 file.delete(); 183 if (autosaveFile.renameTo(file) || Utilities.copyFile(autosaveFile, file)) { 185 autosaveFile.delete(); 186 catalog.remove(netPath); 187 } 188 } 189 } else { 190 String prompt = "Delete autosave file for " + netPath + "?"; 191 int response = ConfirmDialog.showConfirmDialog(null, prompt, "Autosave"); 192 if (response == RESPONSE_YES) { 193 autosaveFile.delete(); 194 catalog.remove(netPath); 195 } 196 } 197 } 198 199 private static final File getRecoverDirectory() 200 { 201 return File.getInstance(Directories.getEditorDirectory(), "recover"); 202 } 203 } 204 | Popular Tags |