001: /*
002: * CheckMailTask.java
003: *
004: * Copyright (C) 2002-2004 Peter Graves
005: * $Id: CheckMailTask.java,v 1.2 2004/07/03 17:04:12 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.util.ArrayList;
025: import org.armedbear.j.Buffer;
026: import org.armedbear.j.BufferIterator;
027: import org.armedbear.j.BufferList;
028: import org.armedbear.j.Editor;
029: import org.armedbear.j.IdleThreadTask;
030: import org.armedbear.j.Log;
031: import org.armedbear.j.Property;
032:
033: public final class CheckMailTask extends IdleThreadTask {
034: private static CheckMailTask instance;
035:
036: private long lastRun;
037:
038: private CheckMailTask() {
039: setIdle(10000); // User must be idle for 10 seconds.
040: setRunnable(runnable);
041: }
042:
043: public static synchronized CheckMailTask getInstance() {
044: if (instance == null)
045: instance = new CheckMailTask();
046: return instance;
047: }
048:
049: private final Runnable runnable = new Runnable() {
050: public void run() {
051: if (!Editor.preferences().getBooleanProperty(
052: Property.CHECK_ENABLED))
053: return;
054: if (!Editor.isMailEnabled())
055: return;
056: // Only check every 10 seconds.
057: if (System.currentTimeMillis() - lastRun > 10000) {
058: // Make a list of mailboxes to check. We don't want to keep the
059: // buffer list locked while we do the actual check!
060: ArrayList mailboxes = new ArrayList();
061: BufferList bufferList = Editor.getBufferList();
062: synchronized (bufferList) {
063: for (BufferIterator it = new BufferIterator(); it
064: .hasNext();) {
065: Buffer buf = it.nextBuffer();
066: if (buf instanceof ImapMailbox
067: || buf instanceof PopMailbox)
068: mailboxes.add(buf);
069: }
070: }
071: // Now check the mailboxes in the list.
072: for (int i = 0; i < mailboxes.size(); i++) {
073: Mailbox mb = (Mailbox) mailboxes.get(i);
074: if (bufferList.contains(mb))
075: check(mb);
076: }
077: lastRun = System.currentTimeMillis();
078: }
079: }
080: };
081:
082: private void check(final Mailbox mb) {
083: // Avoid locking unnecessarily.
084: if (!mb.getBooleanProperty(Property.CHECK_ENABLED))
085: return;
086: int interval = mb.getIntegerProperty(Property.CHECK_INTERVAL);
087: if (interval <= 0)
088: return;
089: long now = System.currentTimeMillis();
090: if (now - mb.getLastCheckMillis() < interval * 1000)
091: return;
092: // Wait 2 minutes after last error.
093: if (now - mb.getLastErrorMillis() < 120000)
094: return;
095: int fg = mb.getIntegerProperty(Property.CHECK_IDLE_FOREGROUND);
096: int bg = mb.getIntegerProperty(Property.CHECK_IDLE_BACKGROUND);
097: if (!mb.isIdle(fg, bg))
098: return;
099: if (mb.lock()) {
100: // Double check.
101: if (mb.isIdle(fg, bg)) {
102: // Still idle.
103: mb.setBusy(true);
104: mb.setWaitCursor();
105: // Starts a new thread, unlocks mailbox when done.
106: mb.getNewMessages(false);
107: // lastCheckMillis will be updated again at the end of
108: // GetNewMessagesProcess.run().
109: mb.setLastCheckMillis(System.currentTimeMillis());
110: } else {
111: // Not idle.
112: mb.unlock();
113: }
114: }
115: }
116: }
|