1 package net.suberic.pooka; 2 3 import javax.mail.*; 4 import java.util.*; 5 import net.suberic.util.*; 6 7 10 11 public class AddressBookManager implements ValueChangeListener { 12 13 private Vector addressBookList; 14 private Vector valueChangeListenerList = new Vector(); 15 16 public AddressBookManager() { 17 addressBookList = createAddressBookList(); 18 Pooka.getResources().addValueChangeListener(this, "AddressBook"); 19 } 20 21 24 30 public void valueChanged(String changedValue) { 31 if (changedValue.equals("AddressBook")) 32 refreshAddressBooks(); 33 } 34 35 39 public java.util.Vector getAddressBookList() { 40 return new Vector(addressBookList); 41 } 42 43 47 public void addAddressBook(String addressBookName) { 48 if (getAddressBook(addressBookName) == null) { 49 appendToAddressBookString(addressBookName); 50 } 51 } 52 53 57 public void addAddressBook(String [] addressBookName) { 58 if (addressBookName != null && addressBookName.length > 0) { 59 StringBuffer addressBookString = new StringBuffer (); 60 for (int i = 0 ; i < addressBookName.length; i++) { 61 if (getAddressBook(addressBookName[i]) == null) 62 addressBookString.append(addressBookName[i] + ":"); 63 } 64 if (addressBookString.length() > 0) 65 appendToAddressBookString(new String (addressBookString.deleteCharAt(addressBookString.length() -1))); 66 } 67 } 68 69 72 public void removeAddressBook(String addressBookName) { 73 if (getAddressBook(addressBookName) != null) 74 removeFromAddressBookString(new String [] { addressBookName }); 75 } 76 77 80 public void removeAddressBook(String [] addressBookNames) { 81 83 if (addressBookNames == null || addressBookNames.length < 1) 84 return; 85 86 Vector matches = new Vector(); 87 for ( int i = 0; i < addressBookNames.length; i++) { 88 if (getAddressBook(addressBookNames[i]) != null) 89 matches.add(addressBookNames[i]); 90 91 } 92 93 if (matches.size() < 1) 94 return; 95 96 String [] removedAddressBooks = new String [matches.size()]; 97 98 for (int i = 0; i < matches.size(); i++) 99 removedAddressBooks[i] = (String ) matches.elementAt(i); 100 101 removeFromAddressBookString(removedAddressBooks); 102 } 103 104 107 public void removeAddressBook(AddressBook addressBook) { 108 if (addressBook != null) 109 removeAddressBook(addressBook.getAddressBookID()); 110 } 111 112 115 public void removeAddressBook(AddressBook[] addressBook) { 116 if (addressBook != null && addressBook.length > 0) { 117 String [] addressBookNames = new String [addressBook.length]; 118 for (int i = 0; i < addressBook.length; i++) { 119 if (addressBook[i] != null) 120 addressBookNames[i] = addressBook[i].getAddressBookID(); 121 } 122 123 removeAddressBook(addressBookNames); 124 } 125 } 126 127 131 public void refreshAddressBooks() { 132 Vector newAddressBookList = new Vector(); 133 134 StringTokenizer tokens = new StringTokenizer(Pooka.getProperty("AddressBook", ""), ":"); 135 136 String addressBookID; 137 while (tokens.hasMoreTokens()) { 138 addressBookID = tokens.nextToken(); 139 AddressBook currentAddressBook = getAddressBook(addressBookID); 140 if (currentAddressBook != null) { 141 newAddressBookList.add(currentAddressBook); 142 } else { 143 currentAddressBook = createAddressBook(addressBookID); 144 if (currentAddressBook != null) { 145 newAddressBookList.add(currentAddressBook); 146 147 if (Pooka.getProperty("AddressBook._default", "").equalsIgnoreCase("")) 148 Pooka.setProperty("AddressBook._default", addressBookID); 149 } 150 151 } 152 } 153 154 if (! newAddressBookList.equals(addressBookList)) { 155 addressBookList = newAddressBookList; 156 fireAddressBookListChangedEvent(); 157 } 158 } 159 160 163 public AddressBook createAddressBook(String id) { 164 String type = Pooka.getProperty("AddressBook." + id + ".type", ""); 165 if (type.equalsIgnoreCase("file")) { 166 AddressBook returnValue = new net.suberic.pooka.vcard.VcardAddressBook(); 167 returnValue.configureAddressBook(id); 168 return returnValue; 169 } 170 171 return null; 172 } 173 174 177 public AddressBook getAddressBook(String name) { 178 if (addressBookList == null) 179 return null; 180 181 for (int i = 0; i < addressBookList.size(); i++) { 182 AddressBook currentBook = (AddressBook) addressBookList.elementAt(i); 183 if (currentBook != null && currentBook.getAddressBookID().equals(name)) 184 return currentBook; 185 } 186 187 return null; 188 } 189 190 193 public void addValueChangeListener(ValueChangeListener vcl) { 194 if (! valueChangeListenerList.contains(vcl)) 195 valueChangeListenerList.add(vcl); 196 } 197 198 201 public void removeValueChangeListener(ValueChangeListener vcl) { 202 valueChangeListenerList.remove(vcl); 203 } 204 205 208 209 public void fireAddressBookListChangedEvent() { 210 for (int i = 0; i < valueChangeListenerList.size(); i++) 211 ((ValueChangeListener)valueChangeListenerList.elementAt(i)).valueChanged("AddressBook"); 212 } 213 214 215 218 222 private Vector createAddressBookList() { 223 Vector allAddressBooks = new Vector(); 224 String addressBookID = null; 225 226 StringTokenizer tokens = new StringTokenizer(Pooka.getProperty("AddressBook", ""), ":"); 227 228 while (tokens.hasMoreTokens()) { 229 addressBookID=(String )tokens.nextToken(); 230 allAddressBooks.add(createAddressBook(addressBookID)); 231 } 232 233 return allAddressBooks; 234 } 235 236 239 private void appendToAddressBookString(String newAddressBookString) { 240 String oldValue = Pooka.getProperty("AddressBook"); 241 String newValue; 242 if (oldValue.length() > 0 && oldValue.charAt(oldValue.length() -1) != ':') { 243 newValue = oldValue + ":" + newAddressBookString; 244 } else { 245 newValue = oldValue + newAddressBookString; 246 } 247 248 Pooka.setProperty("AddressBook", newValue); 249 } 250 251 255 private void removeFromAddressBookString(String [] addressBookNames) { 256 StringTokenizer tokens = new StringTokenizer(Pooka.getProperty("AddressBook", ""), ":"); 257 258 boolean first = true; 259 StringBuffer newValue = new StringBuffer (); 260 String addressBookID; 261 262 while (tokens.hasMoreTokens()) { 263 addressBookID=tokens.nextToken(); 264 boolean keep=true; 265 266 for (int i = 0; keep == true && i < addressBookNames.length; i++) { 267 if (addressBookID.equals(addressBookNames[i])) 268 keep = false; 269 } 270 if (keep) { 271 if (!first) 272 newValue.append(":"); 273 274 newValue.append(addressBookID); 275 first = false; 276 } 277 278 } 279 280 Pooka.setProperty("AddressBook", newValue.toString()); 281 } 282 283 286 public AddressBook getDefault() { 287 String defaultName = Pooka.getProperty("AddressBook._default", ""); 288 if (! defaultName.equals("")) { 289 AddressBook defaultBook = getAddressBook(defaultName); 290 return defaultBook; 291 } else 292 return null; 293 294 } 295 296 297 } 298 299 | Popular Tags |