KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > config > AccountItem


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.mail.config;
18
19
20 import org.columba.core.config.DefaultItem;
21 import org.columba.core.xml.XmlElement;
22 import org.columba.ristretto.parser.ParserException;
23
24 public class AccountItem extends DefaultItem {
25   
26     /*
27      * Add supported account formats here
28      * */

29     public static final int POP3_ACCOUNT = 0,
30                                                     IMAP_ACCOUNT = 1,
31                                                     UNKNOWN_TYPE = -1;
32     
33     private AccountItem defaultAccount;
34     //private boolean pop3;
35
private Identity identity;
36     private PopItem pop;
37     private ImapItem imap;
38     private OutgoingItem smtp;
39     private SecurityItem pgp;
40     private SpecialFoldersItem folder;
41     private SpamItem spam;
42     
43         private int accountType = UNKNOWN_TYPE;
44         
45     public AccountItem(XmlElement e) {
46         super(e);
47
48         if (e.getElement("/popserver") != null)
49         {
50             accountType = POP3_ACCOUNT;
51         }
52         else if (e.getElement("/imapserver") != null)
53         {
54             accountType = IMAP_ACCOUNT;
55         }
56         else
57           accountType = UNKNOWN_TYPE;
58         
59     }
60
61     /*
62      * validates a hostname, i.e.:
63      * mail.myhost.com
64      * mail.us.myhost.com
65      * 127.0.0.1
66      * */

67     
68     public final String JavaDoc getAccountTypeDescription()
69     {
70         switch(accountType)
71         {
72             case POP3_ACCOUNT:
73               return "POP3";
74             case IMAP_ACCOUNT:
75               return "IMAP";
76             default:
77               return "UNKNOWN";
78         }
79     }
80     
81     public final int getAccountType()
82     {
83         return accountType;
84     }
85     
86     public boolean isPopAccount()
87     {
88       return (accountType == POP3_ACCOUNT);
89     }
90
91     public SpecialFoldersItem getSpecialFoldersItem() {
92         if (folder == null) {
93             folder = new SpecialFoldersItem(getRoot().getElement("specialfolders"));
94         }
95
96         if (folder.getBoolean("use_default_account")) {
97             // return default-account ImapItem instead
98
SpecialFoldersItem item = MailConfig.getInstance().getAccountList()
99                                                           .getDefaultAccount()
100                                                           .getSpecialFoldersItem();
101
102             return item;
103         }
104
105         return folder;
106     }
107
108     private AccountItem getDefaultAccount() {
109         if (defaultAccount == null) {
110             defaultAccount = MailConfig.getInstance().getAccountList()
111                                                  .getDefaultAccount();
112         }
113
114         return defaultAccount;
115     }
116
117     public PopItem getPopItem() {
118         if (pop == null) {
119             pop = new PopItem(getRoot().getElement("popserver"));
120         }
121
122         if (pop.getBoolean("use_default_account")) {
123             // return default-account ImapItem instead
124
PopItem item = MailConfig.getInstance().getAccountList()
125                                                .getDefaultAccount().getPopItem();
126
127             return item;
128         }
129
130         return pop;
131     }
132
133     public IncomingItem getIncomingItem() {
134         if( isPopAccount() ) {
135             return getPopItem();
136         } else {
137             return getImapItem();
138         }
139     }
140     
141     public OutgoingItem getSmtpItem() {
142         if (smtp == null) {
143             smtp = new OutgoingItem(getRoot().getElement("smtpserver"));
144         }
145
146         if (smtp.getBoolean("use_default_account")) {
147             // return default-account ImapItem instead
148
return getDefaultAccount().getSmtpItem();
149         }
150
151         return smtp;
152     }
153     
154     public SpamItem getSpamItem() {
155         if ( spam == null) {
156             XmlElement parent = getRoot().getElement("spam");
157             // create if not available
158
if ( parent == null) parent = getRoot().addSubElement("spam");
159             
160             spam = new SpamItem(parent);
161         }
162         
163         if (spam.getBoolean("use_default_account")) {
164             // return default-account SpamItem instead
165
return getDefaultAccount().getSpamItem();
166         }
167         
168         return spam;
169     }
170
171     public SecurityItem getPGPItem() {
172         if (pgp == null) {
173             pgp = new SecurityItem(getRoot().getElement("pgp"));
174         }
175
176         if (pgp.getBoolean("use_default_account")) {
177             // return default-account ImapItem instead
178
SecurityItem item = MailConfig.getInstance().getAccountList()
179                                                .getDefaultAccount().getPGPItem();
180
181             return item;
182         }
183
184         return pgp;
185     }
186
187     public ImapItem getImapItem() {
188         if (imap == null) {
189             imap = new ImapItem(getRoot().getElement("imapserver"));
190         }
191
192         if (imap.getBoolean("use_default_account")) {
193             // return default-account ImapItem instead
194
ImapItem item = MailConfig.getInstance().getAccountList()
195                                                 .getDefaultAccount()
196                                                 .getImapItem();
197
198             return item;
199         }
200
201         return imap;
202     }
203
204     /**
205      * Returns the identity used with this account.
206      */

207     public Identity getIdentity() {
208         if (identity == null) {
209             XmlElement e = getRoot().getElement("identity");
210             if (Boolean.valueOf(e.getAttribute("use_default_account", "false"))
211                     .booleanValue()) {
212                 // return default-account identityItem instead
213
return MailConfig.getInstance().getAccountList().getDefaultAccount()
214                         .getIdentity();
215             } else {
216                 try {
217                     identity = new Identity(e);
218                 } catch (ParserException ex) {
219                     ex.printStackTrace();
220                 }
221             }
222         }
223
224         return identity;
225     }
226
227     public void setName(String JavaDoc str) {
228         setString("name", str);
229     }
230
231     public String JavaDoc getName() {
232         return get("name");
233     }
234
235     public void setUid(int i) {
236         setInteger("uid", i);
237     }
238
239     public int getUid() {
240         return getInteger("uid");
241     }
242
243     public boolean isDefault() {
244         if (MailConfig.getInstance().getAccountList().getDefaultAccountUid() == getUid()) {
245             return true;
246         }
247
248         return false;
249     }
250
251     /** {@inheritDoc} */
252     public boolean equals(Object JavaDoc obj) {
253         if (this == obj) {
254             return true; // same object
255
}
256
257         if ((obj == null) || !(obj instanceof AccountItem)) {
258             return false;
259         }
260
261         /*
262          * The fields on this object is in fact represented in the xml
263          * structure found as getRoot(). Therefore super.equals()
264          * should do the job
265          */

266         return super.equals(obj);
267     }
268
269     /** {@inheritDoc} */
270     public int hashCode() {
271         /*
272          * The fields on this object is in fact represented in the xml
273          * structure found as getRoot(). Therefore super.hashCode()
274          * should do the job.
275          */

276         return super.hashCode();
277     }
278 }
279
Popular Tags