KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > AddressBook


1 /*
2  * AddressBook.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: AddressBook.java,v 1.2 2003/06/29 00:19:34 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.mail;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.BufferedWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.OutputStreamWriter JavaDoc;
31 import java.util.Vector JavaDoc;
32 import org.armedbear.j.Directories;
33 import org.armedbear.j.Editor;
34 import org.armedbear.j.File;
35 import org.armedbear.j.Log;
36 import org.armedbear.j.Utilities;
37
38 public final class AddressBook
39 {
40     // The global address book.
41
private static AddressBook addressBook;
42
43     private static File file;
44     private static File backupFile;
45
46     private Vector JavaDoc entries;
47
48     private AddressBook()
49     {
50         entries = new Vector JavaDoc();
51     }
52
53     public static AddressBook getGlobalAddressBook()
54     {
55         if (addressBook == null) {
56             file = File.getInstance(Directories.getEditorDirectory(), "addresses");
57             backupFile =
58                 File.getInstance(Directories.getEditorDirectory(), "addresses~");
59             addressBook = new AddressBook();
60             InputStream JavaDoc inputStream = null;
61             try {
62                 if (file != null && file.isFile())
63                     inputStream = file.getInputStream();
64                 else if (backupFile != null && backupFile.isFile()) {
65                     Log.debug("getGlobalAddressBook loading backup file");
66                     inputStream = backupFile.getInputStream();
67                 }
68                 if (inputStream != null) {
69                     BufferedReader JavaDoc reader =
70                         new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream));
71                     String JavaDoc s;
72                     while ((s = reader.readLine( )) != null) {
73                         AddressBookEntry entry =
74                             AddressBookEntry.parseAddressBookEntry(s);
75                         if (entry != null)
76                             addressBook.addEntry(entry);
77                     }
78                     reader.close();
79                 }
80             }
81             catch (IOException JavaDoc e) {
82                 Log.error(e);
83             }
84         }
85         return addressBook;
86     }
87
88     public static void saveGlobalAddressBook()
89     {
90         try {
91             File tempFile = Utilities.getTempFile();
92             OutputStream JavaDoc outputStream = tempFile.getOutputStream();
93             BufferedWriter JavaDoc writer =
94                 new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(outputStream));
95             final int limit = addressBook.size();
96             for (int i = 0; i < limit; i++) {
97                 AddressBookEntry entry = addressBook.getEntry(i);
98                 writer.write(entry.toString());
99                 writer.write("\n");
100             }
101             writer.flush();
102             writer.close();
103             if (!Utilities.deleteRename(file, backupFile)) {
104                 Log.error("saveGlobalAddressBook deleteRename error file = " +
105                     file + " backupFile = " + backupFile);
106             }
107             if (!Utilities.deleteRename(tempFile, file)) {
108                 Log.error("saveGlobalAddressBook deleteRename error tempFile = " +
109                     tempFile + " file = " + file);
110             }
111         }
112         catch (IOException JavaDoc e) {
113             Log.error(e);
114         }
115     }
116
117     public final int size()
118     {
119         return entries.size();
120     }
121
122     public void maybeAddMailAddress(MailAddress a)
123     {
124         String JavaDoc address = AddressBookEntry.canonicalizeAddress(a.getAddress());
125         // Don't add entries without a valid address.
126
if (address == null)
127             return;
128         String JavaDoc personal =
129             AddressBookEntry.canonicalizePersonal(a.getPersonal());
130         for (int i = entries.size()-1; i >= 0; i--) {
131             AddressBookEntry entry = getEntry(i);
132             if (address.equalsIgnoreCase(entry.getAddress())) {
133                 // Give preference to lower case addresses.
134
if (Utilities.isLowerCase(address))
135                     entry.setAddress(address);
136                 if (personal == null)
137                     return; // We've got no information to add.
138
if (entry.getPersonal() == null) {
139                     // Entry has no personal name. Use ours.
140
entry.setPersonal(personal);
141                     return;
142                 }
143                 if (entry.getPersonal().equals(personal))
144                     return;
145             }
146         }
147         Log.debug("calling addEntry a.getAddress() = |" + a.getAddress() + "|");
148         Log.debug("calling addEntry a.getPersonal() = |" + a.getPersonal() + "|");
149         Log.debug("calling addEntry personal = |" + personal + "| address = |" + address + "|");
150         addEntry(new AddressBookEntry(personal, address));
151     }
152
153     public void promote(MailAddress a)
154     {
155         String JavaDoc address = AddressBookEntry.canonicalizeAddress(a.getAddress());
156         // Ignore entries without a valid address.
157
if (address == null)
158             return;
159         String JavaDoc personal =
160             AddressBookEntry.canonicalizePersonal(a.getPersonal());
161         AddressBookEntry toBePromoted = new AddressBookEntry(personal, address);
162         for (int i = entries.size()-1; i >= 0; i--) {
163             if (toBePromoted.equals(getEntry(i))) {
164                 entries.removeElementAt(i);
165                 entries.insertElementAt(toBePromoted, 0);
166                 return;
167             }
168         }
169     }
170
171     private final void addEntry(AddressBookEntry entry)
172     {
173         entries.add(entry);
174     }
175
176     public final AddressBookEntry getEntry(int i)
177     {
178         return (AddressBookEntry) entries.get(i);
179     }
180 }
181
Popular Tags