001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018:
019: package org.columba.mail.config;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.net.URL;
024:
025: import org.columba.core.config.DefaultItem;
026: import org.columba.core.io.DiskIO;
027: import org.columba.core.xml.XmlElement;
028: import org.columba.core.xml.XmlIO;
029:
030: public class AccountList extends DefaultItem {
031:
032: protected int nextUid;
033:
034: protected AccountItem defaultAccount;
035:
036: public AccountList(XmlElement root) {
037: super (root);
038:
039: AccountItem item;
040:
041: nextUid = -1;
042:
043: int uid;
044:
045: for (int i = 0; i < count(); i++) {
046: item = get(i);
047: uid = item.getInteger("uid");
048:
049: if (uid > nextUid) {
050: nextUid = uid;
051: }
052: }
053:
054: nextUid++;
055: }
056:
057: public AccountItem get(int index) {
058: XmlElement e = getChildElement(index);
059:
060: //XmlElement.printNode(e,"");
061:
062: /*
063: * if ((index >= 0) && (index < list.size())) return (AccountItem)
064: * list.get(index);
065: *
066: * return null;
067: */
068: return new AccountItem(e);
069: }
070:
071: public AccountItem uidGet(int uid) {
072: XmlElement e;
073:
074: for (int i = 0; i < count(); i++) {
075: e = getChildElement(i);
076:
077: int u = Integer.parseInt(e.getAttribute("uid"));
078:
079: if (uid == u) {
080: return new AccountItem(e);
081: }
082: }
083:
084: return null;
085: }
086:
087: /*
088: * search for SecurityItem based on To headerfield
089: *
090: */
091: public SecurityItem getPGPItem(String to) {
092: for (int i = 0; i < count(); i++) {
093: AccountItem item = (AccountItem) get(i);
094: SecurityItem pgpItem = item.getPGPItem();
095: String id = pgpItem.get("id");
096:
097: to = to.toLowerCase();
098: id = id.toLowerCase();
099:
100: if (to.indexOf(id) != -1) {
101: return pgpItem;
102: } else if (id.indexOf(to) != -1) {
103: return pgpItem;
104: }
105: }
106:
107: return null;
108: }
109:
110: /**
111: * Get account using the email address to identify it.
112: *
113: * @param address
114: * email address
115: * @return account item
116: */
117: public AccountItem getAccount(String address) {
118:
119: for (int i = 0; i < count(); i++) {
120: AccountItem item = get(i);
121: Identity identity = item.getIdentity();
122: String str = identity.getAddress().toString();
123: if (address.indexOf(str) != -1) {
124: // found match
125: return item;
126: }
127: }
128: return null;
129: }
130:
131: public AccountItem hostGetAccount(String host, String address) {
132: XmlElement account;
133: XmlElement server;
134: XmlElement identity;
135:
136: if (address == null) {
137: return get(0);
138: }
139:
140: for (int i = 0; i < count(); i++) {
141: account = getChildElement(i);
142:
143: server = account.getElement("popserver");
144:
145: if (server == null) {
146: server = account.getElement("imapserver");
147: }
148:
149: if (server.getAttribute("host").equals(host)) {
150: return new AccountItem(account);
151: }
152: }
153:
154: for (int i = 0; i < count(); i++) {
155: account = getChildElement(i);
156:
157: identity = account.getElement("identity");
158:
159: if (identity.getAttribute("address").indexOf(address) != -1) {
160: return new AccountItem(account);
161: }
162: }
163:
164: return null;
165: }
166:
167: public AccountItem addEmptyAccount(String type) {
168: // path to account templates for POP3/IMAP
169: String hstr = "org/columba/mail/config/account_template.xml";
170: URL url = DiskIO.getResourceURL(hstr);
171: XmlIO xmlIo = new XmlIO();
172: // load xml document
173: xmlIo.load(url);
174: XmlElement root = xmlIo.getRoot();
175: // get pop3 or imap account xml node
176: XmlElement emptyAccount = root.getElement("/template/" + type
177: + "/account");
178:
179: if (emptyAccount != null) {
180: AccountItem newAccount = new AccountItem(
181: (XmlElement) emptyAccount.clone());
182: newAccount.setInteger("uid", getNextUid());
183: add(newAccount);
184:
185: // Default signature
186: File dir = MailConfig.getInstance().getConfigDirectory();
187: File signatureFile = new File(dir, "signature_"
188: + newAccount.getName() + ".txt");
189:
190: String sigURL = "org/columba/mail/config/default_signature.txt";
191: try {
192: DiskIO.copyResource(sigURL, signatureFile);
193:
194: newAccount.getIdentity().setSignature(signatureFile);
195: } catch (IOException e) {
196: //Do nothing
197: }
198:
199: return newAccount;
200: }
201:
202: return null;
203: }
204:
205: public void add(AccountItem item) {
206: getRoot().addSubElement(item.getRoot());
207:
208: if (item.getInteger("uid") >= nextUid) {
209: nextUid = item.getInteger("uid") + 1;
210: }
211:
212: if (count() == 1) {
213: setDefaultAccount(item.getInteger("uid"));
214: }
215: }
216:
217: public AccountItem remove(int index) {
218: return new AccountItem(getRoot().removeElement(index));
219: }
220:
221: public int count() {
222: return getRoot().count();
223: }
224:
225: protected int getNextUid() {
226: return nextUid++;
227: }
228:
229: /** ************************** default account ******************* */
230: public void setDefaultAccount(int uid) {
231: setInteger("default", uid);
232: defaultAccount = null;
233: }
234:
235: public int getDefaultAccountUid() {
236: return getInteger("default");
237: }
238:
239: public AccountItem getDefaultAccount() {
240: if (defaultAccount == null) {
241: defaultAccount = uidGet(getDefaultAccountUid());
242: // fall back to first account as default
243: if (defaultAccount == null) {
244: defaultAccount = get(0);
245: }
246: }
247:
248: return defaultAccount;
249: }
250: }
|