01: /*
02: * RewriteMailboxesTask.java
03: *
04: * Copyright (C) 2002 Peter Graves
05: * $Id: RewriteMailboxesTask.java,v 1.1.1.1 2002/09/24 16:10:09 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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 org.armedbear.j.Buffer;
25: import org.armedbear.j.BufferIterator;
26: import org.armedbear.j.Editor;
27: import org.armedbear.j.IdleThreadTask;
28:
29: public final class RewriteMailboxesTask extends IdleThreadTask {
30: private static final long REWRITE_MAILBOXES_IDLE = 5000; // 5 seconds
31:
32: private static RewriteMailboxesTask instance;
33:
34: private RewriteMailboxesTask() {
35: setIdle(REWRITE_MAILBOXES_IDLE);
36: setRunnable(runnable);
37: }
38:
39: public static synchronized RewriteMailboxesTask getInstance() {
40: if (instance == null)
41: instance = new RewriteMailboxesTask();
42: return instance;
43: }
44:
45: private final Runnable runnable = new Runnable() {
46: private long lastRun;
47:
48: public void run() {
49: if (!Editor.isMailEnabled())
50: return;
51: long now = System.currentTimeMillis();
52: if (lastRun == 0 || now - lastRun > REWRITE_MAILBOXES_IDLE) {
53: synchronized (Editor.getBufferList()) {
54: for (BufferIterator it = new BufferIterator(); it
55: .hasNext();) {
56: Buffer buf = it.nextBuffer();
57: if (buf instanceof PopMailbox) {
58: final PopMailbox mb = (PopMailbox) buf;
59: // User must be idle for 5 minutes if mailbox is in
60: // foreground, 1 minute if mailbox is in background.
61: if (mb.isDirty() && mb.isIdle(300, 60)) {
62: if (mb.lock()) {
63: // Double check.
64: if (mb.isDirty()
65: && mb.isIdle(300, 60)) {
66: mb.setBusy(true);
67: mb.setWaitCursor();
68: Runnable r = new Runnable() {
69: public void run() {
70: mb
71: .rewriteMailbox(false);
72: mb.setBusy(false);
73: mb.unlock();
74: mb.setDefaultCursor();
75: }
76: };
77: Thread t = new Thread(r);
78: t
79: .setPriority(Thread.MIN_PRIORITY);
80: t.start();
81: } else
82: mb.unlock();
83: }
84: }
85: }
86: }
87: }
88: lastRun = now;
89: }
90: }
91: };
92: }
|