KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Directories


1 /*
2  * Directories.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: Directories.java,v 1.2 2003/06/28 23:52:27 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public final class Directories
25 {
26     private static File userHomeDirectory; // ~
27
private static File editorDirectory; // ~/.j
28
private static File tempDirectory; // ~/.j/temp
29
private static File mailDirectory; // ~/.j/mail
30
private static File draftsFolder; // ~/.j/mail/local/drafts
31
private static File registersDirectory; // ~/.j/registers
32

33     public static void initialize(File userHome)
34     {
35         userHomeDirectory = userHome;
36         if (userHomeDirectory == null) {
37             // Home directory was not specified on the command line.
38
if (Platform.isPlatformWindows()) {
39                 FastStringBuffer sb = new FastStringBuffer("C:\\");
40                 for (char c = 'C'; c <= 'Z'; c++) {
41                     sb.setCharAt(0, c);
42                     File dir = File.getInstance(sb.toString());
43                     if (dir != null && dir.isDirectory() && dir.canWrite()) {
44                         userHomeDirectory = dir;
45                         break;
46                     }
47                 }
48             } else {
49                 // Not Windows.
50
userHomeDirectory =
51                     File.getInstance(System.getProperty("user.home"));
52             }
53             if (userHomeDirectory == null)
54                 Editor.fatal("Use \"--home\" option to specify home directory.");
55         }
56         // Make sure required directories exist and are writable.
57
editorDirectory =
58             provideDirectory(File.getInstance(userHomeDirectory, ".j"));
59         tempDirectory =
60             provideDirectory(File.getInstance(editorDirectory, "temp"));
61         mailDirectory =
62             provideDirectory(File.getInstance(editorDirectory, "mail"));
63         draftsFolder =
64             provideDirectory(File.getInstance(mailDirectory, "local/drafts"));
65         registersDirectory =
66             provideDirectory(File.getInstance(editorDirectory, "registers"));
67         // Avoid garbage collection.
68
Editor.protect(Directories.class);
69     }
70
71     private static File provideDirectory(final File dir)
72     {
73         if (dir == null)
74             return null;
75         if (!dir.isDirectory()) {
76             dir.mkdirs();
77             if (!dir.isDirectory())
78                 Editor.fatal("Unable to create directory " + dir);
79         }
80         if (!dir.canWrite())
81             Editor.fatal("The directory " + dir + " is not writable");
82         return dir;
83     }
84
85     public static final File getUserHomeDirectory()
86     {
87         return userHomeDirectory;
88     }
89
90     public static File getEditorDirectory()
91     {
92         return editorDirectory;
93     }
94
95     public static File getTempDirectory()
96     {
97         return tempDirectory;
98     }
99
100     public static final File getMailDirectory()
101     {
102         return mailDirectory;
103     }
104
105     public static final File getDraftsFolder()
106     {
107         return draftsFolder;
108     }
109
110     public static final File getRegistersDirectory()
111     {
112         return registersDirectory;
113     }
114
115     public static final void cleanTempDirectory()
116     {
117         if (tempDirectory != null && tempDirectory.isDirectory()) {
118             String JavaDoc[] files = tempDirectory.list();
119             for (int i = files.length; i-- > 0;)
120                 File.getInstance(tempDirectory, files[i]).delete();
121         }
122     }
123
124     public static final void moveUnsentMessagesToDraftsFolder()
125     {
126         File unsentMessagesDirectory =
127             File.getInstance(mailDirectory, "unsent");
128         if (unsentMessagesDirectory == null) {
129             Debug.bug();
130             return;
131         }
132         if (!unsentMessagesDirectory.isDirectory())
133             return; // Nothing to do.
134
String JavaDoc[] files = unsentMessagesDirectory.list();
135         if (files.length == 0) {
136             unsentMessagesDirectory.delete();
137             return;
138         }
139         File draftsFolder = getDraftsFolder();
140         if (draftsFolder == null) {
141             Debug.bug();
142             return;
143         }
144         Log.info("moving unsent messages to drafts folder...");
145         if (!draftsFolder.isDirectory()) {
146             draftsFolder.mkdirs();
147             if (!draftsFolder.isDirectory()) {
148                 Log.error("unable to create directory " + draftsFolder);
149                 return;
150             }
151         }
152         if (!draftsFolder.canWrite()) {
153             Log.error(draftsFolder.netPath() + " is not writable");
154         }
155         for (int i = 0; i < files.length; i++) {
156             File source =
157                 File.getInstance(unsentMessagesDirectory, files[i]);
158             File destination =
159                 File.getInstance(draftsFolder, files[i]);
160             Log.debug("moving " + source + " to " + destination);
161             if (!source.renameTo(destination))
162                 Log.error("error moving " + source + " to " + destination);
163         }
164         files = unsentMessagesDirectory.list();
165         if (files.length == 0) {
166             Log.debug("removing empty directory " + unsentMessagesDirectory);
167             unsentMessagesDirectory.delete();
168         } else
169             Log.debug("not removing directory " + unsentMessagesDirectory +
170                 " (directory is not empty)");
171     }
172 }
173
Popular Tags