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: package org.columba.mail.mailchecking;
017:
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Observable;
021: import java.util.Vector;
022:
023: import javax.swing.event.EventListenerList;
024:
025: import org.columba.core.connectionstate.ConnectionStateImpl;
026: import org.columba.core.gui.action.AbstractColumbaAction;
027: import org.columba.mail.command.IMailFolderCommandReference;
028: import org.columba.mail.config.AccountItem;
029: import org.columba.mail.config.AccountList;
030: import org.columba.mail.config.MailConfig;
031:
032: /**
033: * Manages automatic mail-checking for all accounts.
034: * <p>
035: * {@link AbstractMailCheckingAction} contains a timer object
036: * for automatic mail-checking, which triggers the account-type
037: * specific <code>actionPerformed</code> method.
038: *
039: *
040: * @author fdietz
041: */
042: public class MailCheckingManager extends Observable {
043: private List list;
044:
045: private static MailCheckingManager instance = new MailCheckingManager();
046:
047: protected EventListenerList listenerList = new EventListenerList();
048:
049: public MailCheckingManager() {
050: super ();
051:
052: list = new Vector();
053:
054: // get list of all accounts
055: AccountList accountList = MailConfig.getInstance()
056: .getAccountList();
057:
058: // for each account
059: for (int i = 0; i < accountList.count(); i++) {
060: AccountItem accountItem = accountList.get(i);
061:
062: add(accountItem);
063: }
064:
065: // system peep on new messages
066: addMailCheckingListener(new SystemPeepMailCheckingListener());
067: }
068:
069: public static MailCheckingManager getInstance() {
070: return instance;
071: }
072:
073: /**
074: * Return array of actions to create the mail-checking
075: * menu.
076: *
077: *
078: * @return array of actions
079: *
080: * @see
081: */
082: public AbstractColumbaAction[] getActions() {
083: AbstractColumbaAction[] actions = new AbstractColumbaAction[list
084: .size()];
085:
086: Iterator it = list.iterator();
087: int i = 0;
088:
089: while (it.hasNext()) {
090: actions[i++] = ((AbstractMailCheckingAction) it.next());
091: }
092:
093: return actions;
094: }
095:
096: public AbstractMailCheckingAction get(int uid) {
097: Iterator it = list.iterator();
098:
099: // for each account
100: while (it.hasNext()) {
101: AbstractMailCheckingAction action = (AbstractMailCheckingAction) it
102: .next();
103:
104: AccountItem accountItem = action.getAccountItem();
105: int i = accountItem.getUid();
106:
107: if (i == uid) {
108: // found matching account
109: return action;
110: }
111: }
112:
113: return null;
114: }
115:
116: public void remove(int uid) {
117: AbstractMailCheckingAction action = get(uid);
118:
119: // remove this account
120: if (action != null) {
121: list.remove(action);
122: }
123: }
124:
125: public void restartTimer(int uid) {
126: AbstractMailCheckingAction action = get(uid);
127:
128: // restart timer
129: if (action != null) {
130: action.restartTimer();
131: }
132: }
133:
134: public void add(AccountItem accountItem) {
135: if (accountItem.isPopAccount()) {
136: list.add(new POP3MailCheckingAction(accountItem));
137: } else {
138: list.add(new IMAPMailCheckingAction(accountItem));
139: }
140: }
141:
142: /**
143: * Check for new messages in all accounts
144: *
145: */
146: public void checkAll() {
147: // check if we are online
148: if (ConnectionStateImpl.getInstance().isOnline() == false) {
149: // offline -> go online
150: ConnectionStateImpl.getInstance().setOnline(true);
151: }
152: Iterator it = list.iterator();
153:
154: // for each account that is enabled and in the fetchalllist
155: while (it.hasNext()) {
156: AbstractMailCheckingAction action = (AbstractMailCheckingAction) it
157: .next();
158: if (action.isCheckAll() && action.isEnabled())
159: action.check();
160: }
161: }
162:
163: /**
164: * Notify all observers.
165: *
166: */
167: public void update() {
168: setChanged();
169:
170: notifyObservers();
171: }
172:
173: /***************************** Listneer ************************************/
174:
175: /**
176: * Adds a listener.
177: */
178: public void addMailCheckingListener(IMailCheckingListener l) {
179: listenerList.add(IMailCheckingListener.class, l);
180: }
181:
182: /**
183: * Removes a previously registered listener.
184: */
185: public void removeMailCheckingListener(IMailCheckingListener l) {
186: listenerList.remove(IMailCheckingListener.class, l);
187: }
188:
189: /**
190: * Propagates an event to all registered listeners notifying them of a item
191: * addition.
192: */
193: public void fireNewMessageArrived(IMailFolderCommandReference ref) {
194:
195: MailCheckingEvent e = new MailCheckingEvent(this , ref);
196: // Guaranteed to return a non-null array
197: Object[] listeners = listenerList.getListenerList();
198:
199: // Process the listeners last to first, notifying
200: // those that are interested in this event
201: for (int i = listeners.length - 2; i >= 0; i -= 2) {
202: if (listeners[i] == IMailCheckingListener.class) {
203: ((IMailCheckingListener) listeners[i + 1])
204: .newMessageArrived(e);
205: }
206: }
207: }
208:
209: }
|