KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ImapMessageBuffer.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: ImapMessageBuffer.java,v 1.4 2002/10/11 18:28:42 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 javax.swing.SwingUtilities JavaDoc;
25 import org.armedbear.j.BackgroundProcess;
26 import org.armedbear.j.Debug;
27 import org.armedbear.j.Editor;
28 import org.armedbear.j.FastStringBuffer;
29 import org.armedbear.j.Headers;
30 import org.armedbear.j.Log;
31 import org.armedbear.j.MessageDialog;
32 import org.armedbear.j.ProgressNotifier;
33 import org.armedbear.j.StatusBarProgressNotifier;
34
35 public final class ImapMessageBuffer extends MessageBuffer
36 {
37     private boolean cancelled;
38
39     /*package*/ ImapMessageBuffer(ImapMailbox mailbox, ImapMailboxEntry entry)
40     {
41         super();
42         // Mailbox is locked in ImapMailbox.readMessage() before this
43
// constructor is called.
44
Debug.assertTrue(mailbox.isLocked());
45         init(mailbox, entry);
46     }
47
48     /*package*/ ImapMessageBuffer(ImapMailbox mailbox, ImapMailboxEntry entry,
49         String JavaDoc rawText)
50     {
51         super();
52         init(mailbox, entry);
53         message = new Message(rawText);
54         parseMessage();
55         title = message.getHeaderValue(Headers.SUBJECT);
56         if (title == null)
57             title = "";
58         allHeaders = message.getAllHeaders();
59         defaultHeaders = getDefaultHeaders(allHeaders);
60         rawBody = message.getRawBody();
61         setText();
62         unmodified();
63         renumber();
64         formatter.parseBuffer();
65         setLoaded(true);
66     }
67
68     private void init(ImapMailbox mailbox, ImapMailboxEntry entry)
69     {
70         this.mailbox = mailbox;
71         showRawText = mailbox.showRawText;
72         showFullHeaders = mailbox.showFullHeaders;
73         setEntry(entry);
74         initializeUndo();
75         type = TYPE_NORMAL;
76         lineSeparator = "\n";
77         mode = MessageMode.getMode();
78         setFormatter(mode.getFormatter(this));
79         readOnly = true;
80     }
81
82     public int load()
83     {
84         if (isLoaded())
85             return LOAD_COMPLETED;
86         if (!mailbox.isLocked()) {
87             Debug.bug();
88             setText("");
89             return LOAD_FAILED;
90         }
91         setBusy(true);
92         new Thread JavaDoc(loadProcess).start();
93         return LOAD_PENDING;
94     }
95
96     private BackgroundProcess loadProcess = new BackgroundProcess() {
97         private ProgressNotifier progressNotifier;
98
99         public void run()
100         {
101             // Mailbox is locked in ImapMailbox.readMessage() before calling
102
// ImapMessageBuffer constructor.
103
if (!mailbox.isLocked()) {
104                 Debug.bug();
105                 return;
106             }
107             try {
108                 setBackgroundProcess(this);
109                 progressNotifier =
110                     new StatusBarProgressNotifier(ImapMessageBuffer.this);
111                 progressNotifier.progressStart();
112                 loadMessage(progressNotifier);
113                 progressNotifier.setText("");
114                 progressNotifier.progressStop();
115                 setBackgroundProcess(null);
116                 setBusy(false);
117             }
118             finally {
119                 mailbox.unlock();
120                 mailbox.setBusy(false);
121                 Editor.updateDisplayLater(mailbox);
122             }
123         }
124
125         public void cancel()
126         {
127             Log.debug("loadProcess.cancel cancelled!");
128             cancelled = true;
129             progressNotifier.cancel();
130             setBusy(false);
131             Log.debug("loadProcess.cancel calling kill");
132             kill();
133             Log.debug("loadProcess.cancel back from kill");
134         }
135     };
136
137     public void deleteMessage()
138     {
139         storeFlagsInternal(ACTION_DELETE);
140     }
141
142     public void flagMessage()
143     {
144         storeFlagsInternal(ACTION_FLAG);
145     }
146
147     private static final int ACTION_DELETE = 0;
148     private static final int ACTION_FLAG = 1;
149
150     private void storeFlagsInternal(final int action)
151     {
152         final Editor editor = Editor.currentEditor();
153         if (!mailbox.lock()) {
154             editor.status("Mailbox is locked");
155             return;
156         }
157         final int uid = ((ImapMailboxEntry)entry).getUid();
158         Runnable JavaDoc deleteMessageRunnable = new Runnable JavaDoc() {
159             public void run()
160             {
161                 try {
162                     ImapSession session = ((ImapMailbox)mailbox).getSession();
163                     String JavaDoc folderName = ((ImapMailbox)mailbox).getFolderName();
164                     if (session.verifyConnected() && session.verifySelected(folderName)) {
165                         if (session.isReadOnly()) {
166                             Log.debug("deleteMessage - read-only - reselecting...");
167                             session.reselect(folderName);
168                             if (session.isReadOnly()) {
169                                 ((ImapMailbox)mailbox).readOnlyError();
170                                 return;
171                             }
172                         }
173                         session.setEcho(true);
174                         switch (action) {
175                             case ACTION_DELETE:
176                                 session.uidStore(uid, "+flags.silent (\\deleted)");
177                                 break;
178                             case ACTION_FLAG:
179                                 // Toggle.
180
if (entry.isFlagged())
181                                     session.uidStore(uid, "-flags.silent (\\flagged)");
182                                 else
183                                     session.uidStore(uid, "+flags.silent (\\flagged)");
184                                 break;
185                             default:
186                                 Debug.assertTrue(false);
187                                 break;
188                         }
189                         if (session.getResponse() == ImapSession.OK) {
190                             switch (action) {
191                                 case ACTION_DELETE:
192                                     entry.setFlags(entry.getFlags() | MailboxEntry.DELETED);
193                                     break;
194                                 case ACTION_FLAG:
195                                     entry.toggleFlag();
196                                     break;
197                                 default:
198                                     Debug.assertTrue(false);
199                                     break;
200                             }
201                             mailbox.updateEntry(entry);
202                         }
203                         session.setEcho(false);
204                         MailboxEntry nextEntry = mailbox.getNextUndeleted(entry);
205                         if (nextEntry != null) {
206                             mailbox.setDotEntry(nextEntry);
207                             setEntry(nextEntry);
208                             loadMessage(null);
209                         } else {
210                             Runnable JavaDoc messageIndexRunnable = new Runnable JavaDoc() {
211                                 public void run()
212                                 {
213                                     MailCommands.messageIndex(editor);
214                                     editor.status("Last undeleted message");
215                                 }
216                             };
217                             SwingUtilities.invokeLater(messageIndexRunnable);
218                         }
219                         setBusy(false);
220                         editor.updateDisplayLater();
221                     }
222                 }
223                 finally {
224                     setBusy(false);
225                     mailbox.unlock();
226                 }
227             }
228         };
229         setBusy(true);
230         new Thread JavaDoc(deleteMessageRunnable).start();
231     }
232
233     public void moveMessage()
234     {
235         final Editor editor = Editor.currentEditor();
236         final ImapMailboxEntry toBeMoved = (ImapMailboxEntry) entry;
237         String JavaDoc title = "Move Message to Folder";
238         String JavaDoc s = ChooseFolderDialog.chooseFolder(editor, title);
239         if (s == null)
240             return;
241         if (!s.startsWith("mailbox:")) {
242             // Not local. Extract folder name from URL.
243
s = ((ImapMailbox)mailbox).extractFolderName(s);
244             if (s == null) {
245                 MessageDialog.showMessageDialog(editor, "Invalid destination",
246                     "Error");
247                 return;
248             }
249         }
250         if (!mailbox.lock()) {
251             editor.status("Mailbox is locked");
252             return;
253         }
254         final String JavaDoc destination = s;
255         Runnable JavaDoc moveMessageRunnable = new Runnable JavaDoc() {
256             public void run()
257             {
258                 try {
259                     ImapSession session = ((ImapMailbox) mailbox).getSession();
260                     String JavaDoc folderName = ((ImapMailbox) mailbox).getFolderName();
261                     if (session.verifyConnected() &&
262                         session.verifySelected(folderName)) {
263                         if (session.isReadOnly()) {
264                             Log.debug("moveMessage - read-only - reselecting...");
265                             session.reselect(folderName);
266                             if (session.isReadOnly()) {
267                                 ((ImapMailbox) mailbox).readOnlyError();
268                                 return;
269                             }
270                         }
271                         session.setEcho(true);
272                         boolean succeeded = false;
273                         if (destination.startsWith("mailbox:")) {
274                             succeeded = Mail.writeFcc(message, destination,
275                                 toBeMoved.getFlags() & ~MailboxEntry.TAGGED);
276                         } else {
277                             session.writeTagged("uid copy " + toBeMoved.getUid() + " " + destination);
278                             succeeded = session.getResponse() == ImapSession.OK;
279                         }
280                         if (succeeded) {
281                             session.writeTagged("uid store " + toBeMoved.getUid() + " +flags.silent (\\deleted)");
282                             if (session.getResponse() == ImapSession.OK) {
283                                 toBeMoved.setFlags(toBeMoved.getFlags() | MailboxEntry.DELETED);
284                                 mailbox.updateEntry(toBeMoved);
285                             } else
286                                 succeeded = false;
287                         }
288                         session.setEcho(false);
289                         if (succeeded) {
290                             MailboxEntry nextEntry = mailbox.getNextUndeleted(entry);
291                             if (nextEntry != null) {
292                                 mailbox.setDotEntry(nextEntry);
293                                 setEntry(nextEntry);
294                                 loadMessage(null);
295                             } else {
296                                 Runnable JavaDoc messageIndexRunnable = new Runnable JavaDoc() {
297                                     public void run()
298                                     {
299                                         MailCommands.messageIndex(editor);
300                                         editor.status("Last undeleted message");
301                                     }
302                                 };
303                                 SwingUtilities.invokeLater(messageIndexRunnable);
304                             }
305                             setBusy(false);
306                             editor.updateDisplayLater();
307                         } else {
308                             setBusy(false);
309                             Runnable JavaDoc reportError = new Runnable JavaDoc() {
310                                 public void run()
311                                 {
312                                     editor.updateDisplay();
313                                     MessageDialog.showMessageDialog(editor, "Operation failed", "Error");
314                                 }
315                             };
316                             SwingUtilities.invokeLater(reportError);
317                         }
318                     }
319                 }
320                 finally {
321                     setBusy(false);
322                     mailbox.unlock();
323                 }
324             }
325         };
326         setBusy(true);
327         new Thread JavaDoc(moveMessageRunnable).start();
328     }
329 }
330
Popular Tags