001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.smtp.command;
019:
020: import java.io.IOException;
021:
022: import javax.swing.JOptionPane;
023:
024: import org.columba.api.command.ICommandReference;
025: import org.columba.api.command.IWorkerStatusChangeListener;
026: import org.columba.api.command.IWorkerStatusController;
027: import org.columba.api.command.WorkerStatusChangedEvent;
028: import org.columba.core.command.Command;
029: import org.columba.core.command.CommandProcessor;
030: import org.columba.core.command.Worker;
031: import org.columba.core.gui.frame.FrameManager;
032: import org.columba.mail.command.ComposerCommandReference;
033: import org.columba.mail.command.IMailFolderCommandReference;
034: import org.columba.mail.composer.MessageComposer;
035: import org.columba.mail.composer.SendableMessage;
036: import org.columba.mail.config.AccountItem;
037: import org.columba.mail.folder.IMailbox;
038: import org.columba.mail.folder.command.MarkMessageCommand;
039: import org.columba.mail.gui.composer.ComposerController;
040: import org.columba.mail.gui.composer.ComposerModel;
041: import org.columba.mail.gui.composer.command.SaveMessageCommand;
042: import org.columba.mail.gui.tree.FolderTreeModel;
043: import org.columba.mail.gui.util.SendMessageDialog;
044: import org.columba.mail.pgp.CancelledException;
045: import org.columba.mail.smtp.SMTPServer;
046: import org.columba.mail.util.MailResourceLoader;
047: import org.columba.ristretto.message.Flags;
048: import org.waffel.jscf.JSCFException;
049:
050: /**
051: *
052: * This command is started when the user sends the message after creating it in
053: * the composer window.
054: * <p>
055: * After closing the compser window, it will open a little dialog showing the
056: * progress of sending the message.
057: * <p>
058: * If the user cancelles sending, the composer window will be opened again.
059: *
060: * @author fdietz
061: */
062: public class SendMessageCommand extends Command {
063: private SendMessageDialog sendMessageDialog;
064:
065: private boolean showComposer = false;
066:
067: private ComposerController composerController;
068:
069: /**
070: * Constructor for SendMessageCommand.
071: *
072: * @param frameMediator
073: * @param references
074: */
075: public SendMessageCommand(ICommandReference reference) {
076: super (reference);
077: }
078:
079: /*
080: * validate command parameters. At the moment only checks if there are any
081: * invalid email addresses
082: *
083: */
084: private boolean validArguments(ComposerCommandReference reference) {
085:
086: //String invalidRecipient = null;
087:
088: // VALIDATION DISABLE ! Sebastian Witt 25.07.04,
089: // "NAME" <email@somewhat.de> isnt true, which should :(
090: // root@localhost is valid, but not with this check. :(
091: // root is also valid (with local mailserver), but not with this check
092: // :(
093:
094: // for(int i=0;i<references.length;i++)
095: // {
096: //
097: // invalidRecipient = references[i].getComposerController().getModel()
098: // .getInvalidRecipients();
099: //
100: // if (invalidRecipient != null)
101: // {
102: //
103: // //it would be really nice to highlight the invalid recipient
104: // showInvalidRecipientMessage(invalidRecipient);
105: // //AFAIK, there's no need to set showComposer to true because
106: // //composer window is already displayed
107: // // open composer view
108: // //showComposer = true;
109: //
110: // return false;
111: //
112: // }
113: //
114: // }
115: //
116: return true;
117:
118: }
119:
120: /**
121: * @see org.columba.api.command.Command#execute(Worker)
122: */
123: public void execute(IWorkerStatusController worker)
124: throws Exception {
125:
126: ComposerCommandReference r = (ComposerCommandReference) getReference();
127:
128: if (!validArguments(r))
129: return;
130:
131: // display status message
132: worker.setDisplayText(MailResourceLoader.getString("statusbar",
133: "message", "send_message_compose"));
134:
135: // get composer controller
136: // -> get all the account information from the controller
137: composerController = r.getComposerController();
138:
139: // close composer view
140: if (composerController.getView().getFrame() != null) {
141: composerController.getView().getFrame().setVisible(false);
142: }
143:
144: sendMessageDialog = new SendMessageDialog(worker);
145:
146: ComposerModel model = ((ComposerModel) composerController
147: .getModel());
148:
149: AccountItem item = model.getAccountItem();
150:
151: // sent folder
152: IMailbox sentFolder = (IMailbox) FolderTreeModel.getInstance()
153: .getFolder(item.getSpecialFoldersItem().get("sent"));
154:
155: // get the SendableMessage object
156: SendableMessage message = null;
157:
158: try {
159: // compose the message suitable for sending
160: message = new MessageComposer(model).compose(worker, r
161: .isAppendSignature());
162:
163: } catch (JSCFException e1) {
164: if (e1 instanceof CancelledException) {
165: // user cancelled sending operation
166: // open composer view
167: showComposer = true;
168:
169: return;
170: } else {
171: JOptionPane.showMessageDialog(FrameManager
172: .getInstance().getActiveFrame(), e1
173: .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(
193: "statusbar", "message", "send_message"));
194:
195: IWorkerStatusChangeListener listener = new IWorkerStatusChangeListener() {
196: public void workerStatusChanged(
197: WorkerStatusChangedEvent e) {
198: if (e.getSource().cancelled()) {
199: try {
200: server.dropConnection();
201: } catch (IOException e1) {
202: }
203: }
204:
205: }
206: };
207:
208: // important for cancel
209: worker.addWorkerStatusChangeListener(listener);
210:
211: // send message
212: server.sendMessage(message, worker);
213:
214: // not needed anymore
215: worker.removeWorkerStatusChangeListener(listener);
216:
217: if (worker.cancelled()) {
218: showComposer = true;
219: return;
220: }
221:
222: // mark as read
223: Flags flags = new Flags();
224: flags.setSeen(true);
225: message.getHeader().setFlags(flags);
226:
227: // save message in Sent folder
228: ComposerCommandReference ref = new ComposerCommandReference(
229: composerController, sentFolder);
230: ref.setMessage(message);
231:
232: SaveMessageCommand c = new SaveMessageCommand(ref);
233:
234: CommandProcessor.getInstance().addOp(c);
235:
236: // -> get source reference of message
237: // when replying this is the original sender's message
238: // you selected and replied to
239: IMailFolderCommandReference ref2 = model
240: .getSourceReference();
241: if (ref2 != null
242: && ((IMailbox) ref2.getSourceFolder()).exists(ref2
243: .getUids()[0])) {
244: // mark message as answered
245: ref2
246: .setMarkVariant(MarkMessageCommand.MARK_AS_ANSWERED);
247: MarkMessageCommand c1 = new MarkMessageCommand(ref2);
248: CommandProcessor.getInstance().addOp(c1);
249: }
250:
251: // display status message
252: worker.setDisplayText(MailResourceLoader.getString(
253: "statusbar", "message", "send_message_closing"));
254:
255: // close connection to server
256: server.closeConnection();
257:
258: // display status message
259: worker.setDisplayText(MailResourceLoader.getString(
260: "statusbar", "message", "send_message_success"));
261: } /*
262: * catch (SMTPException e) { JOptionPane.showMessageDialog(null,
263: * e.getMessage(), "Error while sending",
264: * JOptionPane.ERROR_MESSAGE); // open composer view showComposer =
265: * true; }
266: */catch (Exception e) {
267: // e.printStackTrace();
268:
269: // open composer view
270: showComposer = true;
271:
272: throw e;
273: }
274: }
275:
276: public void updateGUI() throws Exception {
277:
278: // can no longer assume that sendMessageDialog has been displayed
279: if (sendMessageDialog != null) {
280: // close send message dialog
281: sendMessageDialog.setVisible(false);
282: }
283:
284: if (showComposer == true
285: && composerController.getView().getFrame() != null) {
286: // re-open composer view
287: composerController.getView().getFrame().setVisible(true);
288: composerController.getView().getFrame().requestFocus();
289: } else {
290: // do not prompt user if composer should be really closed
291: composerController.setPromptOnDialogClosing(false);
292: // save composer window state
293: composerController.fireClosed();
294: }
295: }
296: }
|