KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RewriteMailboxesTask.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: RewriteMailboxesTask.java,v 1.1.1.1 2002/09/24 16:10:09 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 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 {
31     private static final long REWRITE_MAILBOXES_IDLE = 5000; // 5 seconds
32

33     private static RewriteMailboxesTask instance;
34
35     private RewriteMailboxesTask()
36     {
37         setIdle(REWRITE_MAILBOXES_IDLE);
38         setRunnable(runnable);
39     }
40
41     public static synchronized RewriteMailboxesTask getInstance()
42     {
43         if (instance == null)
44             instance = new RewriteMailboxesTask();
45         return instance;
46     }
47
48     private final Runnable JavaDoc runnable = new Runnable JavaDoc() {
49         private long lastRun;
50
51         public void run()
52         {
53             if (!Editor.isMailEnabled())
54                 return;
55             long now = System.currentTimeMillis();
56             if (lastRun == 0 || now - lastRun > REWRITE_MAILBOXES_IDLE) {
57                 synchronized (Editor.getBufferList()) {
58                     for (BufferIterator it = new BufferIterator(); it.hasNext();) {
59                         Buffer buf = it.nextBuffer();
60                         if (buf instanceof PopMailbox) {
61                             final PopMailbox mb = (PopMailbox) buf;
62                             // User must be idle for 5 minutes if mailbox is in
63
// foreground, 1 minute if mailbox is in background.
64
if (mb.isDirty() && mb.isIdle(300, 60)) {
65                                 if (mb.lock()) {
66                                     // Double check.
67
if (mb.isDirty() && mb.isIdle(300, 60)) {
68                                         mb.setBusy(true);
69                                         mb.setWaitCursor();
70                                         Runnable JavaDoc r = new Runnable JavaDoc() {
71                                             public void run()
72                                             {
73                                                 mb.rewriteMailbox(false);
74                                                 mb.setBusy(false);
75                                                 mb.unlock();
76                                                 mb.setDefaultCursor();
77                                             }
78                                         };
79                                         Thread JavaDoc t = new Thread JavaDoc(r);
80                                         t.setPriority(Thread.MIN_PRIORITY);
81                                         t.start();
82                                     } else
83                                         mb.unlock();
84                                 }
85                             }
86                         }
87                     }
88                 }
89                 lastRun = now;
90             }
91         }
92     };
93 }
94
Popular Tags