001: package net.suberic.pooka;
002:
003: import javax.mail.*;
004: import java.util.*;
005: import net.suberic.util.*;
006:
007: /**
008: * This class manages the a list of AddressBooks.
009: */
010:
011: public class AddressBookManager implements ValueChangeListener {
012:
013: private Vector addressBookList;
014: private Vector valueChangeListenerList = new Vector();
015:
016: public AddressBookManager() {
017: addressBookList = createAddressBookList();
018: Pooka.getResources()
019: .addValueChangeListener(this , "AddressBook");
020: }
021:
022: //-----------------------
023: // public interface.
024:
025: /**
026: * As defined in net.suberic.util.ValueChangeListener.
027: *
028: * This listens for changes to the "AddressBook" property and calls
029: * refreshAddressBooks() when it gets one.
030: */
031: public void valueChanged(String changedValue) {
032: if (changedValue.equals("AddressBook"))
033: refreshAddressBooks();
034: }
035:
036: /**
037: * This returns a Vector with all the currently registered AddressBook
038: * objects.
039: */
040: public java.util.Vector getAddressBookList() {
041: return new Vector(addressBookList);
042: }
043:
044: /**
045: * This adds the addressBook with the given addressBookName to the
046: * allAddressBooks list.
047: */
048: public void addAddressBook(String addressBookName) {
049: if (getAddressBook(addressBookName) == null) {
050: appendToAddressBookString(addressBookName);
051: }
052: }
053:
054: /**
055: * This adds the addressBooks with the given addressBookNames to the
056: * allAddressBooks list.
057: */
058: public void addAddressBook(String[] addressBookName) {
059: if (addressBookName != null && addressBookName.length > 0) {
060: StringBuffer addressBookString = new StringBuffer();
061: for (int i = 0; i < addressBookName.length; i++) {
062: if (getAddressBook(addressBookName[i]) == null)
063: addressBookString.append(addressBookName[i] + ":");
064: }
065: if (addressBookString.length() > 0)
066: appendToAddressBookString(new String(addressBookString
067: .deleteCharAt(addressBookString.length() - 1)));
068: }
069: }
070:
071: /**
072: * This removes the addressBook with the given addressBookName.
073: */
074: public void removeAddressBook(String addressBookName) {
075: if (getAddressBook(addressBookName) != null)
076: removeFromAddressBookString(new String[] { addressBookName });
077: }
078:
079: /**
080: * This removes the addressBooks with the given addressBookNames.
081: */
082: public void removeAddressBook(String[] addressBookNames) {
083: // this is probably not necessary at all, but what the hell?
084:
085: if (addressBookNames == null || addressBookNames.length < 1)
086: return;
087:
088: Vector matches = new Vector();
089: for (int i = 0; i < addressBookNames.length; i++) {
090: if (getAddressBook(addressBookNames[i]) != null)
091: matches.add(addressBookNames[i]);
092:
093: }
094:
095: if (matches.size() < 1)
096: return;
097:
098: String[] removedAddressBooks = new String[matches.size()];
099:
100: for (int i = 0; i < matches.size(); i++)
101: removedAddressBooks[i] = (String) matches.elementAt(i);
102:
103: removeFromAddressBookString(removedAddressBooks);
104: }
105:
106: /**
107: * This removes the given AddressBook.
108: */
109: public void removeAddressBook(AddressBook addressBook) {
110: if (addressBook != null)
111: removeAddressBook(addressBook.getAddressBookID());
112: }
113:
114: /**
115: * This removes the given AddressBooks.
116: */
117: public void removeAddressBook(AddressBook[] addressBook) {
118: if (addressBook != null && addressBook.length > 0) {
119: String[] addressBookNames = new String[addressBook.length];
120: for (int i = 0; i < addressBook.length; i++) {
121: if (addressBook[i] != null)
122: addressBookNames[i] = addressBook[i]
123: .getAddressBookID();
124: }
125:
126: removeAddressBook(addressBookNames);
127: }
128: }
129:
130: /**
131: * This compares the addressBookList object with the AddressBook property, and
132: * updates the addressBookList appropriately.
133: */
134: public void refreshAddressBooks() {
135: Vector newAddressBookList = new Vector();
136:
137: StringTokenizer tokens = new StringTokenizer(Pooka.getProperty(
138: "AddressBook", ""), ":");
139:
140: String addressBookID;
141: while (tokens.hasMoreTokens()) {
142: addressBookID = tokens.nextToken();
143: AddressBook currentAddressBook = getAddressBook(addressBookID);
144: if (currentAddressBook != null) {
145: newAddressBookList.add(currentAddressBook);
146: } else {
147: currentAddressBook = createAddressBook(addressBookID);
148: if (currentAddressBook != null) {
149: newAddressBookList.add(currentAddressBook);
150:
151: if (Pooka.getProperty("AddressBook._default", "")
152: .equalsIgnoreCase(""))
153: Pooka.setProperty("AddressBook._default",
154: addressBookID);
155: }
156:
157: }
158: }
159:
160: if (!newAddressBookList.equals(addressBookList)) {
161: addressBookList = newAddressBookList;
162: fireAddressBookListChangedEvent();
163: }
164: }
165:
166: /**
167: * Creates an address book.
168: */
169: public AddressBook createAddressBook(String id) {
170: String type = Pooka.getProperty("AddressBook." + id + ".type",
171: "");
172: if (type.equalsIgnoreCase("file")) {
173: AddressBook returnValue = new net.suberic.pooka.vcard.VcardAddressBook();
174: returnValue.configureAddressBook(id);
175: return returnValue;
176: }
177:
178: return null;
179: }
180:
181: /**
182: * This returns the AddressBook which corresponds to the given name.
183: */
184: public AddressBook getAddressBook(String name) {
185: if (addressBookList == null)
186: return null;
187:
188: for (int i = 0; i < addressBookList.size(); i++) {
189: AddressBook currentBook = (AddressBook) addressBookList
190: .elementAt(i);
191: if (currentBook != null
192: && currentBook.getAddressBookID().equals(name))
193: return currentBook;
194: }
195:
196: return null;
197: }
198:
199: /**
200: * This adds a ValueChangeListener to the local listener list.
201: */
202: public void addValueChangeListener(ValueChangeListener vcl) {
203: if (!valueChangeListenerList.contains(vcl))
204: valueChangeListenerList.add(vcl);
205: }
206:
207: /**
208: * This removes a ValueChangeListener from the local listener list.
209: */
210: public void removeValueChangeListener(ValueChangeListener vcl) {
211: valueChangeListenerList.remove(vcl);
212: }
213:
214: /**
215: * This notifies all listeners that the AddressBookList has changed.
216: */
217:
218: public void fireAddressBookListChangedEvent() {
219: for (int i = 0; i < valueChangeListenerList.size(); i++)
220: ((ValueChangeListener) valueChangeListenerList.elementAt(i))
221: .valueChanged("AddressBook");
222: }
223:
224: //---------------------------
225: // the background stuff.
226:
227: /**
228: * This loads and creates all the AddressBooks using the "AddressBook"
229: * property of the main Pooka VariableBundle.
230: */
231: private Vector createAddressBookList() {
232: Vector allAddressBooks = new Vector();
233: String addressBookID = null;
234:
235: StringTokenizer tokens = new StringTokenizer(Pooka.getProperty(
236: "AddressBook", ""), ":");
237:
238: while (tokens.hasMoreTokens()) {
239: addressBookID = (String) tokens.nextToken();
240: allAddressBooks.add(createAddressBook(addressBookID));
241: }
242:
243: return allAddressBooks;
244: }
245:
246: /**
247: * This appends the newAddressBookString to the "AddressBook" property.
248: */
249: private void appendToAddressBookString(String newAddressBookString) {
250: String oldValue = Pooka.getProperty("AddressBook");
251: String newValue;
252: if (oldValue.length() > 0
253: && oldValue.charAt(oldValue.length() - 1) != ':') {
254: newValue = oldValue + ":" + newAddressBookString;
255: } else {
256: newValue = oldValue + newAddressBookString;
257: }
258:
259: Pooka.setProperty("AddressBook", newValue);
260: }
261:
262: /**
263: * This removes the addressBook names in the addressBookNames array from the
264: * "AddressBook" property.
265: */
266: private void removeFromAddressBookString(String[] addressBookNames) {
267: StringTokenizer tokens = new StringTokenizer(Pooka.getProperty(
268: "AddressBook", ""), ":");
269:
270: boolean first = true;
271: StringBuffer newValue = new StringBuffer();
272: String addressBookID;
273:
274: while (tokens.hasMoreTokens()) {
275: addressBookID = tokens.nextToken();
276: boolean keep = true;
277:
278: for (int i = 0; keep == true && i < addressBookNames.length; i++) {
279: if (addressBookID.equals(addressBookNames[i]))
280: keep = false;
281: }
282: if (keep) {
283: if (!first)
284: newValue.append(":");
285:
286: newValue.append(addressBookID);
287: first = false;
288: }
289:
290: }
291:
292: Pooka.setProperty("AddressBook", newValue.toString());
293: }
294:
295: /**
296: * Gets the default Address Book, if there is one.
297: */
298: public AddressBook getDefault() {
299: String defaultName = Pooka.getProperty("AddressBook._default",
300: "");
301: if (!defaultName.equals("")) {
302: AddressBook defaultBook = getAddressBook(defaultName);
303: return defaultBook;
304: } else
305: return null;
306:
307: }
308:
309: }
|