001: /*
002: * Autosave.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: Autosave.java,v 1.2 2002/10/11 14:06:18 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027: import java.util.Enumeration;
028: import java.util.Properties;
029:
030: public final class Autosave implements Constants {
031: private static final String CATALOG_NAME = "catalog";
032: private static Properties catalog;
033: private static File catalogFile;
034:
035: private static boolean autosaveEnabled = true;
036: private static File autosaveDirectory;
037: private static boolean initialized;
038:
039: public static synchronized File getAutosaveDirectory() {
040: if (!initialized) {
041: autosaveDirectory = File.getInstance(Directories
042: .getEditorDirectory(), "autosave");
043: if (autosaveDirectory != null) {
044: if (!autosaveDirectory.isDirectory()) {
045: autosaveDirectory.mkdirs();
046: if (!autosaveDirectory.isDirectory()) {
047: autosaveDirectory = null;
048: autosaveEnabled = false;
049: }
050: }
051: }
052: initialized = true;
053: }
054: return autosaveDirectory;
055: }
056:
057: public static synchronized final boolean isAutosaveEnabled() {
058: return autosaveEnabled;
059: }
060:
061: public static synchronized void put(String netPath, String alias) {
062: if (catalog == null)
063: catalog = new Properties();
064: catalog.put(netPath, alias);
065: }
066:
067: // Update catalog file when a buffer is renamed.
068: public static synchronized void rename(String oldName,
069: String newName) {
070: if (catalog != null && oldName != null) {
071: String alias = (String) catalog.remove(oldName);
072: if (alias != null) {
073: catalog.put(newName, alias);
074: flush();
075: }
076: }
077: }
078:
079: public static synchronized void flush() {
080: if (catalogFile == null) {
081: if (getAutosaveDirectory() == null)
082: return;
083: catalogFile = File.getInstance(getAutosaveDirectory(),
084: CATALOG_NAME);
085: }
086: try {
087: OutputStream out = catalogFile.getOutputStream();
088: catalog.store(out, null);
089: out.flush();
090: out.close();
091: } catch (IOException e) {
092: Log.error(e);
093: }
094: }
095:
096: public static synchronized void deleteCatalogFile() {
097: if (catalogFile == null) {
098: if (getAutosaveDirectory() == null)
099: return;
100: catalogFile = File.getInstance(getAutosaveDirectory(),
101: CATALOG_NAME);
102: }
103: catalogFile.delete();
104: }
105:
106: public static synchronized void recover() {
107: if (catalogFile == null) {
108: if (getAutosaveDirectory() == null)
109: return;
110: catalogFile = File.getInstance(getAutosaveDirectory(),
111: CATALOG_NAME);
112: }
113: if (!catalogFile.exists())
114: return; // No catalog file.
115: if (catalog == null)
116: catalog = new Properties();
117: try {
118: InputStream in = catalogFile.getInputStream();
119: catalog.load(in);
120: in.close();
121: } catch (IOException e) {
122: Log.error(e);
123: }
124: Enumeration e = catalog.propertyNames();
125: while (e.hasMoreElements()) {
126: String netPath = (String) e.nextElement();
127: String alias = catalog.getProperty(netPath);
128: if (alias != null)
129: queryRecoverFile(netPath, alias);
130: }
131: catalogFile.delete();
132: }
133:
134: private static void queryRecoverFile(String netPath, String alias) {
135: File autosaveFile = File.getInstance(getAutosaveDirectory(),
136: alias);
137: if (!autosaveFile.exists()) {
138: // Nothing we can do.
139: catalog.remove(netPath);
140: return;
141: }
142: final File file = File.getInstance(netPath);
143: if (file == null)
144: return; // Shouldn't happen.
145: boolean confirmed = false;
146: File dir = file.getParentFile();
147: if (dir != null && dir.equals(Directories.getDraftsFolder()))
148: confirmed = true;
149: if (!confirmed) {
150: String prompt = "Recover " + netPath
151: + " from autosave file?";
152: int response = ConfirmDialog.showConfirmDialog(null,
153: prompt, "Autosave");
154: if (response == RESPONSE_YES)
155: confirmed = true;
156: }
157: if (confirmed) {
158: if (file.isRemote()) {
159: String recoverPath = file.getHostName() + '/'
160: + file.canonicalPath();
161: Log.debug("recoverPath = |" + recoverPath + "|");
162: File recoverFile = File.getInstance(
163: getRecoverDirectory(), recoverPath);
164: Log.debug("recoverFile = |" + recoverFile.netPath()
165: + "|");
166: File parentDir = recoverFile.getParentFile();
167: if (!parentDir.isDirectory())
168: parentDir.mkdirs();
169: if (parentDir.isDirectory()) {
170: recoverFile.delete();
171: // BUG! Error handling! What if rename and copy both fail?
172: if (autosaveFile.renameTo(recoverFile)
173: || Utilities.copyFile(autosaveFile,
174: recoverFile)) {
175: autosaveFile.delete();
176: catalog.remove(netPath);
177: String message = "File has been saved as "
178: + recoverFile.canonicalPath() + ".";
179: MessageDialog.showMessageDialog(message,
180: "Autosave");
181: }
182: }
183: } else {
184: // BUG! Should back up old file first!
185: file.delete();
186: // BUG! Error handling! What if rename and copy both fail?
187: if (autosaveFile.renameTo(file)
188: || Utilities.copyFile(autosaveFile, file)) {
189: autosaveFile.delete();
190: catalog.remove(netPath);
191: }
192: }
193: } else {
194: String prompt = "Delete autosave file for " + netPath + "?";
195: int response = ConfirmDialog.showConfirmDialog(null,
196: prompt, "Autosave");
197: if (response == RESPONSE_YES) {
198: autosaveFile.delete();
199: catalog.remove(netPath);
200: }
201: }
202: }
203:
204: private static final File getRecoverDirectory() {
205: return File.getInstance(Directories.getEditorDirectory(),
206: "recover");
207: }
208: }
|