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.smtp.command;
017:
018: import java.util.List;
019: import java.util.Vector;
020:
021: import javax.swing.Action;
022:
023: import org.columba.api.command.ICommandReference;
024: import org.columba.api.command.IWorkerStatusController;
025: import org.columba.core.command.Command;
026: import org.columba.core.command.CommandProcessor;
027: import org.columba.core.command.Worker;
028: import org.columba.mail.command.IMailFolderCommandReference;
029: import org.columba.mail.command.MailFolderCommandReference;
030: import org.columba.mail.composer.SendableMessage;
031: import org.columba.mail.config.AccountItem;
032: import org.columba.mail.config.MailConfig;
033: import org.columba.mail.folder.IMailbox;
034: import org.columba.mail.folder.command.MoveMessageCommand;
035: import org.columba.mail.folder.outbox.OutboxFolder;
036: import org.columba.mail.folder.outbox.SendListManager;
037: import org.columba.mail.gui.tree.FolderTreeModel;
038: import org.columba.mail.smtp.SMTPServer;
039: import org.columba.mail.util.MailResourceLoader;
040:
041: /**
042: * @author fdietz
043: *
044: * Send all messages in folder Outbox
045: *
046: */
047: public class SendAllMessagesCommand extends Command {
048: protected SendListManager sendListManager = new SendListManager();
049:
050: protected OutboxFolder outboxFolder;
051:
052: private Action action;
053:
054: public SendAllMessagesCommand(Action action,
055: ICommandReference reference) {
056: super (reference);
057:
058: this .action = action;
059: }
060:
061: /**
062: * @see org.columba.api.command.Command#execute(Worker)
063: */
064: public void execute(IWorkerStatusController worker)
065: throws Exception {
066: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
067:
068: // display status message
069: worker.setDisplayText(MailResourceLoader.getString("statusbar",
070: "message", "send_message"));
071:
072: // get Outbox folder from reference
073: outboxFolder = (OutboxFolder) r.getSourceFolder();
074:
075: // get UID list of messages
076: Object[] uids = outboxFolder.getUids();
077:
078: // save every message in a list
079: for (int i = 0; i < uids.length; i++) {
080: if (outboxFolder.exists(uids[i]) == true) {
081: SendableMessage message = outboxFolder
082: .getSendableMessage(uids[i]);
083: sendListManager.add(message);
084: }
085: }
086:
087: int actAccountUid = -1;
088: List sentList = new Vector();
089:
090: SMTPServer smtpServer = null;
091: IMailbox sentFolder = null;
092:
093: // send all messages
094: while (sendListManager.hasMoreMessages()) {
095: SendableMessage message = sendListManager.getNextMessage();
096:
097: // get account information from message
098: if (message.getAccountUid() != actAccountUid) {
099: actAccountUid = message.getAccountUid();
100:
101: AccountItem accountItem = MailConfig.getInstance()
102: .getAccountList().uidGet(actAccountUid);
103:
104: if (accountItem == null) {
105: // use the default account
106: accountItem = MailConfig.getInstance()
107: .getAccountList().getDefaultAccount();
108:
109: if (accountItem == null)
110: continue; // skip message if there's no account
111: // available to send it
112: }
113:
114: // Sent folder
115: sentFolder = (IMailbox) FolderTreeModel.getInstance()
116: .getFolder(
117: accountItem.getSpecialFoldersItem()
118: .get("sent"));
119:
120: // open connection to SMTP server
121: smtpServer = new SMTPServer(accountItem);
122: }
123: smtpServer.sendMessage(message, worker);
124:
125: sentList.add(message.getHeader().get("columba.uid"));
126: }
127:
128: // we are done - clear status text with a delay
129: // (if this is not done, the initial text will stay in
130: // case no messages were sent)
131: worker.clearDisplayTextWithDelay();
132:
133: // move all successfully send messages to the Sent folder
134: if (sentList.size() > 0) {
135: moveToSentFolder(sentList, sentFolder);
136: sentList.clear();
137: }
138: }
139:
140: /**
141: *
142: * Move all send messages to the Sent folder
143: *
144: * @param v
145: * list of SendableMessage objects
146: *
147: * @param sentFolder
148: * Sent folder
149: */
150: protected void moveToSentFolder(List v, IMailbox sentFolder) {
151: IMailFolderCommandReference r = new MailFolderCommandReference(
152: outboxFolder, sentFolder, v.toArray());
153:
154: // start move command
155: MoveMessageCommand c = new MoveMessageCommand(r);
156:
157: CommandProcessor.getInstance().addOp(c);
158: }
159:
160: /**
161: * @see org.columba.api.command.Command#updateGUI()
162: */
163: public void updateGUI() throws Exception {
164: if (action != null)
165: action.setEnabled(true);
166: }
167: }
|