001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.folder.imap;
017:
018: import java.io.IOException;
019:
020: import javax.swing.Action;
021:
022: import org.columba.api.command.ICommandReference;
023: import org.columba.api.command.IWorkerStatusController;
024: import org.columba.core.command.Command;
025: import org.columba.core.command.CommandCancelledException;
026: import org.columba.core.command.StatusObservableImpl;
027: import org.columba.mail.command.IMailFolderCommandReference;
028: import org.columba.mail.command.MailFolderCommandReference;
029: import org.columba.mail.mailchecking.MailCheckingManager;
030:
031: /**
032: * Check for new messages in IMAPFolder.
033: *
034: *
035: * @author fdietz
036: */
037: public class CheckForNewMessagesCommand extends Command {
038:
039: IMAPFolder imapFolder;
040:
041: private Action action;
042: private boolean triggerNotification;
043:
044: public CheckForNewMessagesCommand(ICommandReference reference) {
045: super (reference);
046: triggerNotification = false;
047:
048: // get references
049: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
050:
051: imapFolder = (IMAPFolder) r.getSourceFolder();
052:
053: imapFolder.setMailboxSyncEnabled(false);
054:
055: }
056:
057: public CheckForNewMessagesCommand(Action action,
058: ICommandReference reference) {
059: super (reference);
060: this .action = action;
061: triggerNotification = true;
062:
063: // get references
064: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
065:
066: imapFolder = (IMAPFolder) r.getSourceFolder();
067:
068: imapFolder.setMailboxSyncEnabled(false);
069: }
070:
071: /*
072: * (non-Javadoc)
073: *
074: * @see org.columba.api.command.Command#execute(org.columba.api.command.Worker)
075: */
076: public void execute(IWorkerStatusController worker)
077: throws Exception {
078:
079: // register for status events
080: ((StatusObservableImpl) imapFolder.getObservable())
081: .setWorker(worker);
082:
083: // Find old numbers
084: int total = imapFolder.getMessageFolderInfo().getExists();
085:
086: // check for new headers
087: Object[] uids = new Object[0];
088: try {
089: uids = imapFolder.synchronizeHeaderlist();
090: } catch (IOException e) {
091: imapFolder.setMailboxSyncEnabled(true);
092: worker.cancel();
093: throw new CommandCancelledException(e);
094: }
095:
096: // Get the new numbers
097: int newTotal = imapFolder.getMessageFolderInfo().getExists();
098:
099: // fire new message event to interested listeners
100: if (triggerNotification && (newTotal != total)) {
101: if (((IMAPRootFolder) imapFolder.getRootFolder())
102: .getAccountItem().getImapItem().getBoolean(
103: "enable_sound")) {
104: // create reference of newly arrived messages
105: IMailFolderCommandReference ref = new MailFolderCommandReference(
106: imapFolder, uids);
107: // fire event
108: MailCheckingManager.getInstance()
109: .fireNewMessageArrived(ref);
110:
111: }
112: }
113: }
114:
115: /**
116: * @see org.columba.api.command.Command#updateGUI()
117: */
118: public void updateGUI() throws Exception {
119: // Reenable the action
120: if (action != null)
121: action.setEnabled(true);
122: }
123: }
|