KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > CheckMailTask


1 /*
2  * CheckMailTask.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: CheckMailTask.java,v 1.2 2004/07/03 17:04:12 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.util.ArrayList JavaDoc;
25 import org.armedbear.j.Buffer;
26 import org.armedbear.j.BufferIterator;
27 import org.armedbear.j.BufferList;
28 import org.armedbear.j.Editor;
29 import org.armedbear.j.IdleThreadTask;
30 import org.armedbear.j.Log;
31 import org.armedbear.j.Property;
32
33 public final class CheckMailTask extends IdleThreadTask
34 {
35     private static CheckMailTask instance;
36
37     private long lastRun;
38
39     private CheckMailTask()
40     {
41         setIdle(10000); // User must be idle for 10 seconds.
42
setRunnable(runnable);
43     }
44
45     public static synchronized CheckMailTask getInstance()
46     {
47         if (instance == null)
48             instance = new CheckMailTask();
49         return instance;
50     }
51
52     private final Runnable JavaDoc runnable = new Runnable JavaDoc()
53     {
54         public void run()
55         {
56             if (!Editor.preferences().getBooleanProperty(Property.CHECK_ENABLED))
57                 return;
58             if (!Editor.isMailEnabled())
59                 return;
60             // Only check every 10 seconds.
61
if (System.currentTimeMillis() - lastRun > 10000) {
62                 // Make a list of mailboxes to check. We don't want to keep the
63
// buffer list locked while we do the actual check!
64
ArrayList JavaDoc mailboxes = new ArrayList JavaDoc();
65                 BufferList bufferList = Editor.getBufferList();
66                 synchronized (bufferList) {
67                     for (BufferIterator it = new BufferIterator(); it.hasNext();) {
68                         Buffer buf = it.nextBuffer();
69                         if (buf instanceof ImapMailbox || buf instanceof PopMailbox)
70                             mailboxes.add(buf);
71                     }
72                 }
73                 // Now check the mailboxes in the list.
74
for (int i = 0; i < mailboxes.size(); i++) {
75                     Mailbox mb = (Mailbox) mailboxes.get(i);
76                     if (bufferList.contains(mb))
77                         check(mb);
78                 }
79                 lastRun = System.currentTimeMillis();
80             }
81         }
82     };
83
84     private void check(final Mailbox mb)
85     {
86         // Avoid locking unnecessarily.
87
if (!mb.getBooleanProperty(Property.CHECK_ENABLED))
88             return;
89         int interval = mb.getIntegerProperty(Property.CHECK_INTERVAL);
90         if (interval <= 0)
91             return;
92         long now = System.currentTimeMillis();
93         if (now - mb.getLastCheckMillis() < interval * 1000)
94             return;
95         // Wait 2 minutes after last error.
96
if (now - mb.getLastErrorMillis() < 120000)
97             return;
98         int fg = mb.getIntegerProperty(Property.CHECK_IDLE_FOREGROUND);
99         int bg = mb.getIntegerProperty(Property.CHECK_IDLE_BACKGROUND);
100         if (!mb.isIdle(fg, bg))
101             return;
102         if (mb.lock()) {
103             // Double check.
104
if (mb.isIdle(fg, bg)) {
105                 // Still idle.
106
mb.setBusy(true);
107                 mb.setWaitCursor();
108                 // Starts a new thread, unlocks mailbox when done.
109
mb.getNewMessages(false);
110                 // lastCheckMillis will be updated again at the end of
111
// GetNewMessagesProcess.run().
112
mb.setLastCheckMillis(System.currentTimeMillis());
113             } else {
114                 // Not idle.
115
mb.unlock();
116             }
117         }
118     }
119 }
120
Popular Tags