KickJava   Java API By Example, From Geeks To Geeks.

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


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.smtp.command;
19
20 import java.io.IOException JavaDoc;
21
22 import javax.swing.JOptionPane JavaDoc;
23
24 import org.columba.api.command.ICommandReference;
25 import org.columba.api.command.IWorkerStatusChangeListener;
26 import org.columba.api.command.IWorkerStatusController;
27 import org.columba.api.command.WorkerStatusChangedEvent;
28 import org.columba.core.command.Command;
29 import org.columba.core.command.CommandProcessor;
30 import org.columba.core.command.Worker;
31 import org.columba.core.gui.frame.FrameManager;
32 import org.columba.mail.command.ComposerCommandReference;
33 import org.columba.mail.command.IMailFolderCommandReference;
34 import org.columba.mail.composer.MessageComposer;
35 import org.columba.mail.composer.SendableMessage;
36 import org.columba.mail.config.AccountItem;
37 import org.columba.mail.folder.IMailbox;
38 import org.columba.mail.folder.command.MarkMessageCommand;
39 import org.columba.mail.gui.composer.ComposerController;
40 import org.columba.mail.gui.composer.ComposerModel;
41 import org.columba.mail.gui.composer.command.SaveMessageCommand;
42 import org.columba.mail.gui.tree.FolderTreeModel;
43 import org.columba.mail.gui.util.SendMessageDialog;
44 import org.columba.mail.pgp.CancelledException;
45 import org.columba.mail.smtp.SMTPServer;
46 import org.columba.mail.util.MailResourceLoader;
47 import org.columba.ristretto.message.Flags;
48 import org.waffel.jscf.JSCFException;
49
50 /**
51  *
52  * This command is started when the user sends the message after creating it in
53  * the composer window.
54  * <p>
55  * After closing the compser window, it will open a little dialog showing the
56  * progress of sending the message.
57  * <p>
58  * If the user cancelles sending, the composer window will be opened again.
59  *
60  * @author fdietz
61  */

62 public class SendMessageCommand extends Command {
63     private SendMessageDialog sendMessageDialog;
64
65     private boolean showComposer = false;
66
67     private ComposerController composerController;
68
69     /**
70      * Constructor for SendMessageCommand.
71      *
72      * @param frameMediator
73      * @param references
74      */

75     public SendMessageCommand(ICommandReference reference) {
76         super(reference);
77     }
78
79
80
81     /*
82      * validate command parameters. At the moment only checks if there are any
83      * invalid email addresses
84      *
85      */

86     private boolean validArguments(ComposerCommandReference reference) {
87
88         //String invalidRecipient = null;
89

90         // VALIDATION DISABLE ! Sebastian Witt 25.07.04,
91
// "NAME" <email@somewhat.de> isnt true, which should :(
92
// root@localhost is valid, but not with this check. :(
93
// root is also valid (with local mailserver), but not with this check
94
// :(
95

96
97         // for(int i=0;i<references.length;i++)
98
// {
99
//
100
// invalidRecipient = references[i].getComposerController().getModel()
101
// .getInvalidRecipients();
102
//
103
// if (invalidRecipient != null)
104
// {
105
//
106
// //it would be really nice to highlight the invalid recipient
107
// showInvalidRecipientMessage(invalidRecipient);
108
// //AFAIK, there's no need to set showComposer to true because
109
// //composer window is already displayed
110
// // open composer view
111
// //showComposer = true;
112
//
113
// return false;
114
//
115
// }
116
//
117
// }
118
//
119
return true;
120
121     }
122
123     /**
124      * @see org.columba.api.command.Command#execute(Worker)
125      */

126     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
127
128         ComposerCommandReference r = (ComposerCommandReference) getReference();
129
130         if (!validArguments(r))
131             return;
132
133         // display status message
134
worker.setDisplayText(MailResourceLoader.getString("statusbar",
135                 "message", "send_message_compose"));
136
137         // get composer controller
138
// -> get all the account information from the controller
139
composerController = r.getComposerController();
140
141         // close composer view
142
if (composerController.getView().getFrame() != null) {
143             composerController.getView().getFrame().setVisible(false);
144         }
145
146         sendMessageDialog = new SendMessageDialog(worker);
147
148         ComposerModel model = ((ComposerModel) composerController.getModel());
149
150         AccountItem item = model.getAccountItem();
151
152         // sent folder
153
IMailbox sentFolder = (IMailbox) FolderTreeModel.getInstance()
154                 .getFolder(item.getSpecialFoldersItem().get("sent"));
155
156         // get the SendableMessage object
157
SendableMessage message = null;
158
159         try {
160             // compose the message suitable for sending
161
message = new MessageComposer(model).compose(worker, r
162                     .isAppendSignature());
163
164         } catch (JSCFException e1) {
165             if (e1 instanceof CancelledException) {
166                 // user cancelled sending operation
167
// open composer view
168
showComposer = true;
169
170                 return;
171             } else {
172                 JOptionPane.showMessageDialog(FrameManager.getInstance()
173                         .getActiveFrame(), e1.getMessage());
174
175                 // open composer view
176
showComposer = true;
177
178                 return;
179             }
180         }
181
182         // display status message
183
worker.setDisplayText(MailResourceLoader.getString("statusbar",
184                 "message", "send_message_connect"));
185
186         // open connection
187
final SMTPServer server = new SMTPServer(item);
188
189         // successfully connected and autenthenticated to SMTP server
190
try {
191             // display status message
192
worker.setDisplayText(MailResourceLoader.getString("statusbar",
193                     "message", "send_message"));
194
195             IWorkerStatusChangeListener listener = new IWorkerStatusChangeListener() {
196                 public void workerStatusChanged(WorkerStatusChangedEvent e) {
197                     if (e.getSource().cancelled()) {
198                         try {
199                             server.dropConnection();
200                         } catch (IOException JavaDoc e1) {
201                         }
202                     }
203
204                 }
205             };
206
207             // important for cancel
208
worker.addWorkerStatusChangeListener(listener);
209
210             // send message
211
server.sendMessage(message, worker);
212
213             // not needed anymore
214
worker.removeWorkerStatusChangeListener(listener);
215
216             if (worker.cancelled()) {
217                 showComposer = true;
218                 return;
219             }
220
221             // mark as read
222
Flags flags = new Flags();
223             flags.setSeen(true);
224             message.getHeader().setFlags(flags);
225
226             // save message in Sent folder
227
ComposerCommandReference ref = new ComposerCommandReference(
228                     composerController, sentFolder);
229             ref.setMessage(message);
230
231             SaveMessageCommand c = new SaveMessageCommand(ref);
232
233             CommandProcessor.getInstance().addOp(c);
234
235             // -> get source reference of message
236
// when replying this is the original sender's message
237
// you selected and replied to
238
IMailFolderCommandReference ref2 = model.getSourceReference();
239             if (ref2 != null
240                     && ((IMailbox) ref2.getSourceFolder()).exists(ref2
241                             .getUids()[0])) {
242                 // mark message as answered
243
ref2.setMarkVariant(MarkMessageCommand.MARK_AS_ANSWERED);
244                 MarkMessageCommand c1 = new MarkMessageCommand(ref2);
245                 CommandProcessor.getInstance().addOp(c1);
246             }
247
248             // display status message
249
worker.setDisplayText(MailResourceLoader.getString("statusbar",
250                     "message", "send_message_closing"));
251
252             // close connection to server
253
server.closeConnection();
254
255             // display status message
256
worker.setDisplayText(MailResourceLoader.getString("statusbar",
257                     "message", "send_message_success"));
258         } /*
259              * catch (SMTPException e) { JOptionPane.showMessageDialog(null,
260              * e.getMessage(), "Error while sending",
261              * JOptionPane.ERROR_MESSAGE); // open composer view showComposer =
262              * true; }
263              */
catch (Exception JavaDoc e) {
264             // e.printStackTrace();
265

266             // open composer view
267
showComposer = true;
268
269             throw e;
270         }
271     }
272
273     public void updateGUI() throws Exception JavaDoc {
274
275         // can no longer assume that sendMessageDialog has been displayed
276
if (sendMessageDialog != null) {
277             // close send message dialog
278
sendMessageDialog.setVisible(false);
279         }
280
281         if (showComposer == true
282                 && composerController.getView().getFrame() != null) {
283             // re-open composer view
284
composerController.getView().getFrame().setVisible(true);
285             composerController.getView().getFrame().requestFocus();
286         } else {
287             // do not prompt user if composer should be really closed
288
composerController.setPromptOnDialogClosing(false);
289             // save composer window state
290
composerController.fireClosed();
291         }
292     }
293 }
Popular Tags