001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.mail.config;
018:
019: import org.columba.core.config.DefaultItem;
020: import org.columba.core.xml.XmlElement;
021: import org.columba.ristretto.parser.ParserException;
022:
023: public class AccountItem extends DefaultItem {
024:
025: /*
026: * Add supported account formats here
027: * */
028: public static final int POP3_ACCOUNT = 0, IMAP_ACCOUNT = 1,
029: UNKNOWN_TYPE = -1;
030:
031: private AccountItem defaultAccount;
032: //private boolean pop3;
033: private Identity identity;
034: private PopItem pop;
035: private ImapItem imap;
036: private OutgoingItem smtp;
037: private SecurityItem pgp;
038: private SpecialFoldersItem folder;
039: private SpamItem spam;
040:
041: private int accountType = UNKNOWN_TYPE;
042:
043: public AccountItem(XmlElement e) {
044: super (e);
045:
046: if (e.getElement("/popserver") != null) {
047: accountType = POP3_ACCOUNT;
048: } else if (e.getElement("/imapserver") != null) {
049: accountType = IMAP_ACCOUNT;
050: } else
051: accountType = UNKNOWN_TYPE;
052:
053: }
054:
055: /*
056: * validates a hostname, i.e.:
057: * mail.myhost.com
058: * mail.us.myhost.com
059: * 127.0.0.1
060: * */
061:
062: public final String getAccountTypeDescription() {
063: switch (accountType) {
064: case POP3_ACCOUNT:
065: return "POP3";
066: case IMAP_ACCOUNT:
067: return "IMAP";
068: default:
069: return "UNKNOWN";
070: }
071: }
072:
073: public final int getAccountType() {
074: return accountType;
075: }
076:
077: public boolean isPopAccount() {
078: return (accountType == POP3_ACCOUNT);
079: }
080:
081: public SpecialFoldersItem getSpecialFoldersItem() {
082: if (folder == null) {
083: folder = new SpecialFoldersItem(getRoot().getElement(
084: "specialfolders"));
085: }
086:
087: if (folder.getBoolean("use_default_account")) {
088: // return default-account ImapItem instead
089: SpecialFoldersItem item = MailConfig.getInstance()
090: .getAccountList().getDefaultAccount()
091: .getSpecialFoldersItem();
092:
093: return item;
094: }
095:
096: return folder;
097: }
098:
099: private AccountItem getDefaultAccount() {
100: if (defaultAccount == null) {
101: defaultAccount = MailConfig.getInstance().getAccountList()
102: .getDefaultAccount();
103: }
104:
105: return defaultAccount;
106: }
107:
108: public PopItem getPopItem() {
109: if (pop == null) {
110: pop = new PopItem(getRoot().getElement("popserver"));
111: }
112:
113: if (pop.getBoolean("use_default_account")) {
114: // return default-account ImapItem instead
115: PopItem item = MailConfig.getInstance().getAccountList()
116: .getDefaultAccount().getPopItem();
117:
118: return item;
119: }
120:
121: return pop;
122: }
123:
124: public IncomingItem getIncomingItem() {
125: if (isPopAccount()) {
126: return getPopItem();
127: } else {
128: return getImapItem();
129: }
130: }
131:
132: public OutgoingItem getSmtpItem() {
133: if (smtp == null) {
134: smtp = new OutgoingItem(getRoot().getElement("smtpserver"));
135: }
136:
137: if (smtp.getBoolean("use_default_account")) {
138: // return default-account ImapItem instead
139: return getDefaultAccount().getSmtpItem();
140: }
141:
142: return smtp;
143: }
144:
145: public SpamItem getSpamItem() {
146: if (spam == null) {
147: XmlElement parent = getRoot().getElement("spam");
148: // create if not available
149: if (parent == null)
150: parent = getRoot().addSubElement("spam");
151:
152: spam = new SpamItem(parent);
153: }
154:
155: if (spam.getBoolean("use_default_account")) {
156: // return default-account SpamItem instead
157: return getDefaultAccount().getSpamItem();
158: }
159:
160: return spam;
161: }
162:
163: public SecurityItem getPGPItem() {
164: if (pgp == null) {
165: pgp = new SecurityItem(getRoot().getElement("pgp"));
166: }
167:
168: if (pgp.getBoolean("use_default_account")) {
169: // return default-account ImapItem instead
170: SecurityItem item = MailConfig.getInstance()
171: .getAccountList().getDefaultAccount().getPGPItem();
172:
173: return item;
174: }
175:
176: return pgp;
177: }
178:
179: public ImapItem getImapItem() {
180: if (imap == null) {
181: imap = new ImapItem(getRoot().getElement("imapserver"));
182: }
183:
184: if (imap.getBoolean("use_default_account")) {
185: // return default-account ImapItem instead
186: ImapItem item = MailConfig.getInstance().getAccountList()
187: .getDefaultAccount().getImapItem();
188:
189: return item;
190: }
191:
192: return imap;
193: }
194:
195: /**
196: * Returns the identity used with this account.
197: */
198: public Identity getIdentity() {
199: if (identity == null) {
200: XmlElement e = getRoot().getElement("identity");
201: if (Boolean.valueOf(
202: e.getAttribute("use_default_account", "false"))
203: .booleanValue()) {
204: // return default-account identityItem instead
205: return MailConfig.getInstance().getAccountList()
206: .getDefaultAccount().getIdentity();
207: } else {
208: try {
209: identity = new Identity(e);
210: } catch (ParserException ex) {
211: ex.printStackTrace();
212: }
213: }
214: }
215:
216: return identity;
217: }
218:
219: public void setName(String str) {
220: setString("name", str);
221: }
222:
223: public String getName() {
224: return get("name");
225: }
226:
227: public void setUid(int i) {
228: setInteger("uid", i);
229: }
230:
231: public int getUid() {
232: return getInteger("uid");
233: }
234:
235: public boolean isDefault() {
236: if (MailConfig.getInstance().getAccountList()
237: .getDefaultAccountUid() == getUid()) {
238: return true;
239: }
240:
241: return false;
242: }
243:
244: /** {@inheritDoc} */
245: public boolean equals(Object obj) {
246: if (this == obj) {
247: return true; // same object
248: }
249:
250: if ((obj == null) || !(obj instanceof AccountItem)) {
251: return false;
252: }
253:
254: /*
255: * The fields on this object is in fact represented in the xml
256: * structure found as getRoot(). Therefore super.equals()
257: * should do the job
258: */
259: return super .equals(obj);
260: }
261:
262: /** {@inheritDoc} */
263: public int hashCode() {
264: /*
265: * The fields on this object is in fact represented in the xml
266: * structure found as getRoot(). Therefore super.hashCode()
267: * should do the job.
268: */
269: return super.hashCode();
270: }
271: }
|