KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 package org.columba.mail.config;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
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         //XmlElement.printNode(e,"");
61

62         /*
63          * if ((index >= 0) && (index < list.size())) return (AccountItem)
64          * list.get(index);
65          *
66          * return null;
67          */

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     /*
86      * search for SecurityItem based on To headerfield
87      *
88      */

89     public SecurityItem getPGPItem(String JavaDoc to) {
90         for (int i = 0; i < count(); i++) {
91             AccountItem item = (AccountItem) get(i);
92             SecurityItem pgpItem = item.getPGPItem();
93             String JavaDoc 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     /**
107      * Get account using the email address to identify it.
108      *
109      * @param address
110      * email address
111      * @return account item
112      */

113     public AccountItem getAccount(String JavaDoc address) {
114
115         for (int i = 0; i < count(); i++) {
116             AccountItem item = get(i);
117             Identity identity = item.getIdentity();
118             String JavaDoc str = identity.getAddress().toString();
119             if (address.indexOf(str) != -1) {
120                 // found match
121
return item;
122             }
123         }
124         return null;
125     }
126
127     public AccountItem hostGetAccount(String JavaDoc host, String JavaDoc 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 JavaDoc type) {
162         // path to account templates for POP3/IMAP
163
String JavaDoc hstr = "org/columba/mail/config/account_template.xml";
164         URL JavaDoc url = DiskIO.getResourceURL(hstr);
165         XmlIO xmlIo = new XmlIO();
166         // load xml document
167
xmlIo.load(url);
168         XmlElement root = xmlIo.getRoot();
169         // get pop3 or imap account xml node
170
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             // Default signature
181
File JavaDoc dir = MailConfig.getInstance().getConfigDirectory();
182             File JavaDoc signatureFile = new File JavaDoc(dir, "signature_" + newAccount.getName() + ".txt");
183
184             String JavaDoc sigURL = "org/columba/mail/config/default_signature.txt";
185             try {
186                 DiskIO.copyResource(sigURL, signatureFile);
187
188                 newAccount.getIdentity().setSignature(signatureFile);
189             } catch (IOException JavaDoc e) {
190                 //Do nothing
191
}
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     /** ************************** default account ******************* */
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             // fall back to first account as default
238
if ( defaultAccount == null ) {
239                 defaultAccount = get(0);
240             }
241         }
242
243         return defaultAccount;
244     }
245 }
Popular Tags