KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > mailchecking > MailCheckingManager


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
package org.columba.mail.mailchecking;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Observable JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import javax.swing.event.EventListenerList JavaDoc;
24
25 import org.columba.core.connectionstate.ConnectionStateImpl;
26 import org.columba.core.gui.action.AbstractColumbaAction;
27 import org.columba.mail.command.IMailFolderCommandReference;
28 import org.columba.mail.config.AccountItem;
29 import org.columba.mail.config.AccountList;
30 import org.columba.mail.config.MailConfig;
31
32
33 /**
34  * Manages automatic mail-checking for all accounts.
35  * <p>
36  * {@link AbstractMailCheckingAction} contains a timer object
37  * for automatic mail-checking, which triggers the account-type
38  * specific <code>actionPerformed</code> method.
39  *
40  *
41  * @author fdietz
42  */

43 public class MailCheckingManager extends Observable JavaDoc {
44     private List JavaDoc list;
45
46     private static MailCheckingManager instance = new MailCheckingManager();
47     
48     
49     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
50     
51     public MailCheckingManager() {
52         super();
53
54         list = new Vector JavaDoc();
55
56         // get list of all accounts
57
AccountList accountList = MailConfig.getInstance().getAccountList();
58
59         // for each account
60
for (int i = 0; i < accountList.count(); i++) {
61             AccountItem accountItem = accountList.get(i);
62
63             add(accountItem);
64         }
65         
66         // system peep on new messages
67
addMailCheckingListener(new SystemPeepMailCheckingListener());
68     }
69
70     public static MailCheckingManager getInstance() {
71         return instance;
72     }
73     
74     /**
75      * Return array of actions to create the mail-checking
76      * menu.
77      *
78      *
79      * @return array of actions
80      *
81      * @see
82      */

83     public AbstractColumbaAction[] getActions() {
84         AbstractColumbaAction[] actions = new AbstractColumbaAction[list.size()];
85
86         Iterator JavaDoc it = list.iterator();
87         int i = 0;
88
89         while (it.hasNext()) {
90             actions[i++] = ((AbstractMailCheckingAction) it.next());
91         }
92
93         return actions;
94     }
95
96     public AbstractMailCheckingAction get(int uid) {
97         Iterator JavaDoc it = list.iterator();
98
99         // for each account
100
while (it.hasNext()) {
101             AbstractMailCheckingAction action = (AbstractMailCheckingAction) it.next();
102
103             AccountItem accountItem = action.getAccountItem();
104             int i = accountItem.getUid();
105
106             if (i == uid) {
107                 // found matching account
108
return action;
109             }
110         }
111
112         return null;
113     }
114
115     public void remove(int uid) {
116         AbstractMailCheckingAction action = get(uid);
117
118         // remove this account
119
if (action != null) {
120             list.remove(action);
121         }
122     }
123
124     public void restartTimer(int uid) {
125         AbstractMailCheckingAction action = get(uid);
126
127         // restart timer
128
if (action != null) {
129             action.restartTimer();
130         }
131     }
132
133     public void add(AccountItem accountItem) {
134         if (accountItem.isPopAccount()) {
135             list.add(new POP3MailCheckingAction(accountItem));
136         } else {
137             list.add(new IMAPMailCheckingAction(accountItem));
138         }
139     }
140
141     /**
142      * Check for new messages in all accounts
143      *
144      */

145     public void checkAll() {
146         // check if we are online
147
if (ConnectionStateImpl.getInstance().isOnline() == false) {
148             // offline -> go online
149
ConnectionStateImpl.getInstance().setOnline(true);
150         }
151         Iterator JavaDoc it = list.iterator();
152
153         // for each account that is enabled and in the fetchalllist
154
while (it.hasNext()) {
155             AbstractMailCheckingAction action = (AbstractMailCheckingAction) it.next();
156             if( action.isCheckAll() && action.isEnabled()) action.check();
157         }
158     }
159
160     /**
161      * Notify all observers.
162      *
163      */

164     public void update() {
165         setChanged();
166
167         notifyObservers();
168     }
169     
170     /***************************** Listneer ************************************/
171     
172     
173     /**
174      * Adds a listener.
175      */

176     public void addMailCheckingListener(IMailCheckingListener l) {
177         listenerList.add(IMailCheckingListener.class, l);
178     }
179
180     /**
181      * Removes a previously registered listener.
182      */

183     public void removeMailCheckingListener(IMailCheckingListener l) {
184         listenerList.remove(IMailCheckingListener.class, l);
185     }
186
187     /**
188      * Propagates an event to all registered listeners notifying them of a item
189      * addition.
190      */

191     public void fireNewMessageArrived(IMailFolderCommandReference ref) {
192
193         MailCheckingEvent e = new MailCheckingEvent(this, ref);
194         // Guaranteed to return a non-null array
195
Object JavaDoc[] listeners = listenerList.getListenerList();
196
197         // Process the listeners last to first, notifying
198
// those that are interested in this event
199
for (int i = listeners.length - 2; i >= 0; i -= 2) {
200             if (listeners[i] == IMailCheckingListener.class) {
201                 ((IMailCheckingListener) listeners[i + 1]).newMessageArrived(e);
202             }
203         }
204     }
205     
206 }
207
Popular Tags