1 21 22 package org.armedbear.j.mail; 23 24 import java.util.ArrayList ; 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); 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 runnable = new Runnable () 53 { 54 public void run() 55 { 56 if (!Editor.preferences().getBooleanProperty(Property.CHECK_ENABLED)) 57 return; 58 if (!Editor.isMailEnabled()) 59 return; 60 if (System.currentTimeMillis() - lastRun > 10000) { 62 ArrayList mailboxes = new ArrayList (); 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 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 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 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 if (mb.isIdle(fg, bg)) { 105 mb.setBusy(true); 107 mb.setWaitCursor(); 108 mb.getNewMessages(false); 110 mb.setLastCheckMillis(System.currentTimeMillis()); 113 } else { 114 mb.unlock(); 116 } 117 } 118 } 119 } 120 | Popular Tags |