1 19 package org.columba.mail.config; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.net.URL ; 24 25 import org.columba.core.config.DefaultItem; 26 import org.columba.core.io.DiskIO; 27 import org.columba.core.xml.XmlElement; 28 import org.columba.core.xml.XmlIO; 29 30 public class AccountList extends DefaultItem { 31 32 protected int nextUid; 33 34 protected AccountItem defaultAccount; 35 36 public AccountList(XmlElement root) { 37 super(root); 38 39 AccountItem item; 40 41 nextUid = -1; 42 43 int uid; 44 45 for (int i = 0; i < count(); i++) { 46 item = get(i); 47 uid = item.getInteger("uid"); 48 49 if (uid > nextUid) { 50 nextUid = uid; 51 } 52 } 53 54 nextUid++; 55 } 56 57 public AccountItem get(int index) { 58 XmlElement e = getChildElement(index); 59 60 62 68 return new AccountItem(e); 69 } 70 71 public AccountItem uidGet(int uid) { 72 XmlElement e; 73 74 for (int i = 0; i < count(); i++) { 75 e = getChildElement(i); 76 77 int u = Integer.parseInt(e.getAttribute("uid")); 78 79 if (uid == u) { return new AccountItem(e); } 80 } 81 82 return null; 83 } 84 85 89 public SecurityItem getPGPItem(String to) { 90 for (int i = 0; i < count(); i++) { 91 AccountItem item = (AccountItem) get(i); 92 SecurityItem pgpItem = item.getPGPItem(); 93 String id = pgpItem.get("id"); 94 95 to = to.toLowerCase(); 96 id = id.toLowerCase(); 97 98 if (to.indexOf(id) != -1) { 99 return pgpItem; 100 } else if (id.indexOf(to) != -1) { return pgpItem; } 101 } 102 103 return null; 104 } 105 106 113 public AccountItem getAccount(String address) { 114 115 for (int i = 0; i < count(); i++) { 116 AccountItem item = get(i); 117 Identity identity = item.getIdentity(); 118 String str = identity.getAddress().toString(); 119 if (address.indexOf(str) != -1) { 120 return item; 122 } 123 } 124 return null; 125 } 126 127 public AccountItem hostGetAccount(String host, String address) { 128 XmlElement account; 129 XmlElement server; 130 XmlElement identity; 131 132 if (address == null) { return get(0); } 133 134 for (int i = 0; i < count(); i++) { 135 account = getChildElement(i); 136 137 server = account.getElement("popserver"); 138 139 if (server == null) { 140 server = account.getElement("imapserver"); 141 } 142 143 if (server.getAttribute("host").equals(host)) { 144 return new AccountItem(account); 145 } 146 } 147 148 for (int i = 0; i < count(); i++) { 149 account = getChildElement(i); 150 151 identity = account.getElement("identity"); 152 153 if (identity.getAttribute("address").indexOf(address) != -1) { 154 return new AccountItem(account); 155 } 156 } 157 158 return null; 159 } 160 161 public AccountItem addEmptyAccount(String type) { 162 String hstr = "org/columba/mail/config/account_template.xml"; 164 URL url = DiskIO.getResourceURL(hstr); 165 XmlIO xmlIo = new XmlIO(); 166 xmlIo.load(url); 168 XmlElement root = xmlIo.getRoot(); 169 XmlElement emptyAccount = root.getElement("/template/" + type 171 + "/account"); 172 173 174 if (emptyAccount != null) { 175 AccountItem newAccount = new AccountItem((XmlElement) emptyAccount 176 .clone()); 177 newAccount.setInteger("uid", getNextUid()); 178 add(newAccount); 179 180 File dir = MailConfig.getInstance().getConfigDirectory(); 182 File signatureFile = new File (dir, "signature_" + newAccount.getName() + ".txt"); 183 184 String sigURL = "org/columba/mail/config/default_signature.txt"; 185 try { 186 DiskIO.copyResource(sigURL, signatureFile); 187 188 newAccount.getIdentity().setSignature(signatureFile); 189 } catch (IOException e) { 190 } 192 193 194 return newAccount; 195 } 196 197 return null; 198 } 199 200 public void add(AccountItem item) { 201 getRoot().addSubElement(item.getRoot()); 202 203 if (item.getInteger("uid") >= nextUid) { 204 nextUid = item.getInteger("uid") + 1; 205 } 206 207 if (count() == 1) { 208 setDefaultAccount(item.getInteger("uid")); 209 } 210 } 211 212 public AccountItem remove(int index) { 213 return new AccountItem(getRoot().removeElement(index)); 214 } 215 216 public int count() { 217 return getRoot().count(); 218 } 219 220 protected int getNextUid() { 221 return nextUid++; 222 } 223 224 225 public void setDefaultAccount(int uid) { 226 setInteger("default", uid); 227 defaultAccount = null; 228 } 229 230 public int getDefaultAccountUid() { 231 return getInteger("default"); 232 } 233 234 public AccountItem getDefaultAccount() { 235 if (defaultAccount == null) { 236 defaultAccount = uidGet(getDefaultAccountUid()); 237 if ( defaultAccount == null ) { 239 defaultAccount = get(0); 240 } 241 } 242 243 return defaultAccount; 244 } 245 } | Popular Tags |