001: /*
002: * AddressBook.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: AddressBook.java,v 1.2 2003/06/29 00:19:34 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.mail;
023:
024: import java.io.BufferedReader;
025: import java.io.BufferedWriter;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.InputStreamReader;
029: import java.io.OutputStream;
030: import java.io.OutputStreamWriter;
031: import java.util.Vector;
032: import org.armedbear.j.Directories;
033: import org.armedbear.j.Editor;
034: import org.armedbear.j.File;
035: import org.armedbear.j.Log;
036: import org.armedbear.j.Utilities;
037:
038: public final class AddressBook {
039: // The global address book.
040: private static AddressBook addressBook;
041:
042: private static File file;
043: private static File backupFile;
044:
045: private Vector entries;
046:
047: private AddressBook() {
048: entries = new Vector();
049: }
050:
051: public static AddressBook getGlobalAddressBook() {
052: if (addressBook == null) {
053: file = File.getInstance(Directories.getEditorDirectory(),
054: "addresses");
055: backupFile = File.getInstance(Directories
056: .getEditorDirectory(), "addresses~");
057: addressBook = new AddressBook();
058: InputStream inputStream = null;
059: try {
060: if (file != null && file.isFile())
061: inputStream = file.getInputStream();
062: else if (backupFile != null && backupFile.isFile()) {
063: Log
064: .debug("getGlobalAddressBook loading backup file");
065: inputStream = backupFile.getInputStream();
066: }
067: if (inputStream != null) {
068: BufferedReader reader = new BufferedReader(
069: new InputStreamReader(inputStream));
070: String s;
071: while ((s = reader.readLine()) != null) {
072: AddressBookEntry entry = AddressBookEntry
073: .parseAddressBookEntry(s);
074: if (entry != null)
075: addressBook.addEntry(entry);
076: }
077: reader.close();
078: }
079: } catch (IOException e) {
080: Log.error(e);
081: }
082: }
083: return addressBook;
084: }
085:
086: public static void saveGlobalAddressBook() {
087: try {
088: File tempFile = Utilities.getTempFile();
089: OutputStream outputStream = tempFile.getOutputStream();
090: BufferedWriter writer = new BufferedWriter(
091: new OutputStreamWriter(outputStream));
092: final int limit = addressBook.size();
093: for (int i = 0; i < limit; i++) {
094: AddressBookEntry entry = addressBook.getEntry(i);
095: writer.write(entry.toString());
096: writer.write("\n");
097: }
098: writer.flush();
099: writer.close();
100: if (!Utilities.deleteRename(file, backupFile)) {
101: Log
102: .error("saveGlobalAddressBook deleteRename error file = "
103: + file + " backupFile = " + backupFile);
104: }
105: if (!Utilities.deleteRename(tempFile, file)) {
106: Log
107: .error("saveGlobalAddressBook deleteRename error tempFile = "
108: + tempFile + " file = " + file);
109: }
110: } catch (IOException e) {
111: Log.error(e);
112: }
113: }
114:
115: public final int size() {
116: return entries.size();
117: }
118:
119: public void maybeAddMailAddress(MailAddress a) {
120: String address = AddressBookEntry.canonicalizeAddress(a
121: .getAddress());
122: // Don't add entries without a valid address.
123: if (address == null)
124: return;
125: String personal = AddressBookEntry.canonicalizePersonal(a
126: .getPersonal());
127: for (int i = entries.size() - 1; i >= 0; i--) {
128: AddressBookEntry entry = getEntry(i);
129: if (address.equalsIgnoreCase(entry.getAddress())) {
130: // Give preference to lower case addresses.
131: if (Utilities.isLowerCase(address))
132: entry.setAddress(address);
133: if (personal == null)
134: return; // We've got no information to add.
135: if (entry.getPersonal() == null) {
136: // Entry has no personal name. Use ours.
137: entry.setPersonal(personal);
138: return;
139: }
140: if (entry.getPersonal().equals(personal))
141: return;
142: }
143: }
144: Log.debug("calling addEntry a.getAddress() = |"
145: + a.getAddress() + "|");
146: Log.debug("calling addEntry a.getPersonal() = |"
147: + a.getPersonal() + "|");
148: Log.debug("calling addEntry personal = |" + personal
149: + "| address = |" + address + "|");
150: addEntry(new AddressBookEntry(personal, address));
151: }
152:
153: public void promote(MailAddress a) {
154: String address = AddressBookEntry.canonicalizeAddress(a
155: .getAddress());
156: // Ignore entries without a valid address.
157: if (address == null)
158: return;
159: String personal = AddressBookEntry.canonicalizePersonal(a
160: .getPersonal());
161: AddressBookEntry toBePromoted = new AddressBookEntry(personal,
162: address);
163: for (int i = entries.size() - 1; i >= 0; i--) {
164: if (toBePromoted.equals(getEntry(i))) {
165: entries.removeElementAt(i);
166: entries.insertElementAt(toBePromoted, 0);
167: return;
168: }
169: }
170: }
171:
172: private final void addEntry(AddressBookEntry entry) {
173: entries.add(entry);
174: }
175:
176: public final AddressBookEntry getEntry(int i) {
177: return (AddressBookEntry) entries.get(i);
178: }
179: }
|