001: /*
002: * Directories.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: Directories.java,v 1.2 2003/06/28 23:52:27 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: public final class Directories {
025: private static File userHomeDirectory; // ~
026: private static File editorDirectory; // ~/.j
027: private static File tempDirectory; // ~/.j/temp
028: private static File mailDirectory; // ~/.j/mail
029: private static File draftsFolder; // ~/.j/mail/local/drafts
030: private static File registersDirectory; // ~/.j/registers
031:
032: public static void initialize(File userHome) {
033: userHomeDirectory = userHome;
034: if (userHomeDirectory == null) {
035: // Home directory was not specified on the command line.
036: if (Platform.isPlatformWindows()) {
037: FastStringBuffer sb = new FastStringBuffer("C:\\");
038: for (char c = 'C'; c <= 'Z'; c++) {
039: sb.setCharAt(0, c);
040: File dir = File.getInstance(sb.toString());
041: if (dir != null && dir.isDirectory()
042: && dir.canWrite()) {
043: userHomeDirectory = dir;
044: break;
045: }
046: }
047: } else {
048: // Not Windows.
049: userHomeDirectory = File.getInstance(System
050: .getProperty("user.home"));
051: }
052: if (userHomeDirectory == null)
053: Editor
054: .fatal("Use \"--home\" option to specify home directory.");
055: }
056: // Make sure required directories exist and are writable.
057: editorDirectory = provideDirectory(File.getInstance(
058: userHomeDirectory, ".j"));
059: tempDirectory = provideDirectory(File.getInstance(
060: editorDirectory, "temp"));
061: mailDirectory = provideDirectory(File.getInstance(
062: editorDirectory, "mail"));
063: draftsFolder = provideDirectory(File.getInstance(mailDirectory,
064: "local/drafts"));
065: registersDirectory = provideDirectory(File.getInstance(
066: editorDirectory, "registers"));
067: // Avoid garbage collection.
068: Editor.protect(Directories.class);
069: }
070:
071: private static File provideDirectory(final File dir) {
072: if (dir == null)
073: return null;
074: if (!dir.isDirectory()) {
075: dir.mkdirs();
076: if (!dir.isDirectory())
077: Editor.fatal("Unable to create directory " + dir);
078: }
079: if (!dir.canWrite())
080: Editor.fatal("The directory " + dir + " is not writable");
081: return dir;
082: }
083:
084: public static final File getUserHomeDirectory() {
085: return userHomeDirectory;
086: }
087:
088: public static File getEditorDirectory() {
089: return editorDirectory;
090: }
091:
092: public static File getTempDirectory() {
093: return tempDirectory;
094: }
095:
096: public static final File getMailDirectory() {
097: return mailDirectory;
098: }
099:
100: public static final File getDraftsFolder() {
101: return draftsFolder;
102: }
103:
104: public static final File getRegistersDirectory() {
105: return registersDirectory;
106: }
107:
108: public static final void cleanTempDirectory() {
109: if (tempDirectory != null && tempDirectory.isDirectory()) {
110: String[] files = tempDirectory.list();
111: for (int i = files.length; i-- > 0;)
112: File.getInstance(tempDirectory, files[i]).delete();
113: }
114: }
115:
116: public static final void moveUnsentMessagesToDraftsFolder() {
117: File unsentMessagesDirectory = File.getInstance(mailDirectory,
118: "unsent");
119: if (unsentMessagesDirectory == null) {
120: Debug.bug();
121: return;
122: }
123: if (!unsentMessagesDirectory.isDirectory())
124: return; // Nothing to do.
125: String[] files = unsentMessagesDirectory.list();
126: if (files.length == 0) {
127: unsentMessagesDirectory.delete();
128: return;
129: }
130: File draftsFolder = getDraftsFolder();
131: if (draftsFolder == null) {
132: Debug.bug();
133: return;
134: }
135: Log.info("moving unsent messages to drafts folder...");
136: if (!draftsFolder.isDirectory()) {
137: draftsFolder.mkdirs();
138: if (!draftsFolder.isDirectory()) {
139: Log.error("unable to create directory " + draftsFolder);
140: return;
141: }
142: }
143: if (!draftsFolder.canWrite()) {
144: Log.error(draftsFolder.netPath() + " is not writable");
145: }
146: for (int i = 0; i < files.length; i++) {
147: File source = File.getInstance(unsentMessagesDirectory,
148: files[i]);
149: File destination = File.getInstance(draftsFolder, files[i]);
150: Log.debug("moving " + source + " to " + destination);
151: if (!source.renameTo(destination))
152: Log.error("error moving " + source + " to "
153: + destination);
154: }
155: files = unsentMessagesDirectory.list();
156: if (files.length == 0) {
157: Log.debug("removing empty directory "
158: + unsentMessagesDirectory);
159: unsentMessagesDirectory.delete();
160: } else
161: Log.debug("not removing directory "
162: + unsentMessagesDirectory
163: + " (directory is not empty)");
164: }
165: }
|