KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > smtp > command > SendAllMessagesCommand


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.smtp.command;
17
18 import java.util.List JavaDoc;
19 import java.util.Vector JavaDoc;
20
21 import javax.swing.Action JavaDoc;
22
23 import org.columba.api.command.ICommandReference;
24 import org.columba.api.command.IWorkerStatusController;
25 import org.columba.core.command.Command;
26 import org.columba.core.command.CommandProcessor;
27 import org.columba.core.command.Worker;
28 import org.columba.mail.command.IMailFolderCommandReference;
29 import org.columba.mail.command.MailFolderCommandReference;
30 import org.columba.mail.composer.SendableMessage;
31 import org.columba.mail.config.AccountItem;
32 import org.columba.mail.config.MailConfig;
33 import org.columba.mail.folder.IMailbox;
34 import org.columba.mail.folder.command.MoveMessageCommand;
35 import org.columba.mail.folder.outbox.OutboxFolder;
36 import org.columba.mail.folder.outbox.SendListManager;
37 import org.columba.mail.gui.tree.FolderTreeModel;
38 import org.columba.mail.smtp.SMTPServer;
39 import org.columba.mail.util.MailResourceLoader;
40
41 /**
42  * @author fdietz
43  *
44  * Send all messages in folder Outbox
45  *
46  */

47 public class SendAllMessagesCommand extends Command {
48     protected SendListManager sendListManager = new SendListManager();
49
50     protected OutboxFolder outboxFolder;
51
52     private Action JavaDoc action;
53
54     public SendAllMessagesCommand(Action JavaDoc action, ICommandReference reference) {
55         super(reference);
56
57         this.action = action;
58     }
59
60     /**
61      * @see org.columba.api.command.Command#execute(Worker)
62      */

63     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
64         IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
65
66         // display status message
67
worker.setDisplayText(MailResourceLoader.getString("statusbar",
68                 "message", "send_message"));
69
70         // get Outbox folder from reference
71
outboxFolder = (OutboxFolder) r.getSourceFolder();
72
73         // get UID list of messages
74
Object JavaDoc[] uids = outboxFolder.getUids();
75
76         // save every message in a list
77
for (int i = 0; i < uids.length; i++) {
78             if (outboxFolder.exists(uids[i]) == true) {
79                 SendableMessage message = outboxFolder
80                         .getSendableMessage(uids[i]);
81                 sendListManager.add(message);
82             }
83         }
84
85         int actAccountUid = -1;
86         List JavaDoc sentList = new Vector JavaDoc();
87
88         SMTPServer smtpServer = null;
89         IMailbox sentFolder = null;
90
91         // send all messages
92
while (sendListManager.hasMoreMessages()) {
93             SendableMessage message = sendListManager.getNextMessage();
94
95             // get account information from message
96
if (message.getAccountUid() != actAccountUid) {
97                 actAccountUid = message.getAccountUid();
98
99                 AccountItem accountItem = MailConfig.getInstance()
100                         .getAccountList().uidGet(actAccountUid);
101
102                 if (accountItem == null) {
103                     // use the default account
104
accountItem = MailConfig.getInstance().getAccountList()
105                             .getDefaultAccount();
106
107                     if (accountItem == null)
108                         continue; // skip message if there's no account
109
// available to send it
110
}
111
112                 // Sent folder
113
sentFolder = (IMailbox) FolderTreeModel
114                         .getInstance()
115                         .getFolder(
116                                 accountItem.getSpecialFoldersItem().get("sent"));
117
118                 // open connection to SMTP server
119
smtpServer = new SMTPServer(accountItem);
120             }
121             smtpServer.sendMessage(message, worker);
122
123             sentList.add(message.getHeader().get("columba.uid"));
124         }
125
126         // we are done - clear status text with a delay
127
// (if this is not done, the initial text will stay in
128
// case no messages were sent)
129
worker.clearDisplayTextWithDelay();
130
131         // move all successfully send messages to the Sent folder
132
if (sentList.size() > 0) {
133             moveToSentFolder(sentList, sentFolder);
134             sentList.clear();
135         }
136     }
137
138     /**
139      *
140      * Move all send messages to the Sent folder
141      *
142      * @param v
143      * list of SendableMessage objects
144      *
145      * @param sentFolder
146      * Sent folder
147      */

148     protected void moveToSentFolder(List JavaDoc v, IMailbox sentFolder) {
149         IMailFolderCommandReference r = new MailFolderCommandReference(
150                 outboxFolder, sentFolder, v.toArray());
151
152         // start move command
153
MoveMessageCommand c = new MoveMessageCommand(r);
154
155         CommandProcessor.getInstance().addOp(c);
156     }
157
158     /**
159      * @see org.columba.api.command.Command#updateGUI()
160      */

161     public void updateGUI() throws Exception JavaDoc {
162         if (action != null)
163             action.setEnabled(true);
164     }
165 }
Popular Tags