KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folder > imap > CheckForNewMessagesCommand


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.folder.imap;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.swing.Action JavaDoc;
21
22 import org.columba.api.command.ICommandReference;
23 import org.columba.api.command.IWorkerStatusController;
24 import org.columba.core.command.Command;
25 import org.columba.core.command.CommandCancelledException;
26 import org.columba.core.command.StatusObservableImpl;
27 import org.columba.mail.command.IMailFolderCommandReference;
28 import org.columba.mail.command.MailFolderCommandReference;
29 import org.columba.mail.mailchecking.MailCheckingManager;
30
31 /**
32  * Check for new messages in IMAPFolder.
33  *
34  *
35  * @author fdietz
36  */

37 public class CheckForNewMessagesCommand extends Command {
38
39     IMAPFolder imapFolder;
40
41     private Action JavaDoc action;
42     private boolean triggerNotification;
43     
44     
45     public CheckForNewMessagesCommand(ICommandReference reference) {
46         super(reference);
47         triggerNotification = false;
48
49         // get references
50
IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
51
52         imapFolder = (IMAPFolder) r.getSourceFolder();
53
54         imapFolder.setMailboxSyncEnabled(false);
55         
56     }
57
58     public CheckForNewMessagesCommand(Action JavaDoc action, ICommandReference reference) {
59         super(reference);
60         this.action = action;
61         triggerNotification = true;
62         
63         // get references
64
IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
65
66         imapFolder = (IMAPFolder) r.getSourceFolder();
67         
68         imapFolder.setMailboxSyncEnabled(false);
69     }
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see org.columba.api.command.Command#execute(org.columba.api.command.Worker)
75      */

76     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
77         
78         // register for status events
79
((StatusObservableImpl) imapFolder.getObservable()).setWorker(worker);
80
81
82         // Find old numbers
83
int total = imapFolder.getMessageFolderInfo().getExists();
84
85         // check for new headers
86
Object JavaDoc[] uids = new Object JavaDoc[0];
87         try {
88             uids = imapFolder.synchronizeHeaderlist();
89         } catch (IOException JavaDoc e) {
90             imapFolder.setMailboxSyncEnabled(true);
91             worker.cancel();
92             throw new CommandCancelledException(e);
93         }
94
95         // Get the new numbers
96
int newTotal = imapFolder.getMessageFolderInfo().getExists();
97         
98         // fire new message event to interested listeners
99
if (triggerNotification && (newTotal != total) ) {
100             if( ((IMAPRootFolder)imapFolder.getRootFolder()).getAccountItem().getImapItem().getBoolean("enable_sound")) {
101                 // create reference of newly arrived messages
102
IMailFolderCommandReference ref = new MailFolderCommandReference(imapFolder, uids);
103                 // fire event
104
MailCheckingManager.getInstance().fireNewMessageArrived(ref);
105                 
106             }
107         }
108     }
109
110     /**
111      * @see org.columba.api.command.Command#updateGUI()
112      */

113     public void updateGUI() throws Exception JavaDoc {
114         // Reenable the action
115
if( action != null) action.setEnabled(true);
116     }
117 }
Popular Tags