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.gui.message.command;
017:
018: import java.io.File;
019: import java.io.FileOutputStream;
020: import java.io.InputStream;
021: import java.util.Enumeration;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import javax.swing.JFileChooser;
026: import javax.swing.JOptionPane;
027:
028: import org.columba.api.command.ICommandReference;
029: import org.columba.api.command.IWorkerStatusController;
030: import org.columba.api.plugin.IExtension;
031: import org.columba.api.plugin.IExtensionHandler;
032: import org.columba.api.plugin.PluginException;
033: import org.columba.api.plugin.PluginHandlerNotFoundException;
034: import org.columba.core.base.cFileChooser;
035: import org.columba.core.base.cFileFilter;
036: import org.columba.core.command.Command;
037: import org.columba.core.command.CommandProcessor;
038: import org.columba.core.command.ProgressObservedInputStream;
039: import org.columba.core.command.Worker;
040: import org.columba.core.desktop.ColumbaDesktop;
041: import org.columba.core.gui.frame.DefaultContainer;
042: import org.columba.core.gui.frame.FrameManager;
043: import org.columba.core.io.StreamUtils;
044: import org.columba.core.logging.Logging;
045: import org.columba.core.plugin.PluginManager;
046: import org.columba.core.util.TempFileStore;
047: import org.columba.mail.command.IMailFolderCommandReference;
048: import org.columba.mail.command.MailFolderCommandReference;
049: import org.columba.mail.folder.IMailbox;
050: import org.columba.mail.folder.temp.TempFolder;
051: import org.columba.mail.gui.attachment.IAttachmentHandler;
052: import org.columba.mail.gui.message.util.AttachmentContext;
053: import org.columba.mail.gui.messageframe.MessageFrameController;
054: import org.columba.mail.gui.tree.FolderTreeModel;
055: import org.columba.mail.plugin.IExtensionHandlerKeys;
056: import org.columba.ristretto.coder.Base64DecoderInputStream;
057: import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream;
058: import org.columba.ristretto.message.MimeHeader;
059:
060: /**
061: * @author freddy
062: */
063: public class OpenAttachmentCommand extends SaveAttachmentCommand {
064: private static final Logger LOG = Logger
065: .getLogger("org.columba.mail.gui.message.attachment.command");
066:
067: private File tempFile;
068:
069: private TempFolder tempFolder;
070:
071: private Object tempMessageUid;
072:
073: private MimeHeader header;
074:
075: /**
076: * Constructor for OpenAttachmentCommand.
077: *
078: * @param references
079: * command parameters
080: */
081: public OpenAttachmentCommand(ICommandReference reference) {
082: super (reference);
083:
084: priority = Command.REALTIME_PRIORITY;
085: commandType = Command.NORMAL_OPERATION;
086: }
087:
088: /**
089: * @see org.columba.api.command.Command#updateGUI()
090: */
091: public void updateGUI() throws Exception {
092:
093: if (header.getMimeType().getType().toLowerCase().indexOf(
094: "message") != -1) {
095: MessageFrameController c = new MessageFrameController();
096: new DefaultContainer(c);
097:
098: Object[] uidList = new Object[1];
099: uidList[0] = tempMessageUid;
100:
101: IMailFolderCommandReference r = new MailFolderCommandReference(
102: tempFolder, uidList);
103:
104: c.setTreeSelection(r);
105: c.setTableSelection(r);
106:
107: CommandProcessor.getInstance().addOp(
108: new ViewMessageCommand(c, r));
109:
110: } else {
111:
112: boolean attachmentHandlerExecuted = false;
113: try {
114: IExtensionHandler handler = PluginManager
115: .getInstance()
116: .getExtensionHandler(
117: IExtensionHandlerKeys.ORG_COLUMBA_ATTACHMENT_HANDLER);
118:
119: Enumeration<IExtension> e = handler
120: .getExtensionEnumeration();
121: while (e.hasMoreElements()) {
122: IExtension extension = e.nextElement();
123: try {
124: IAttachmentHandler attachmentHandler = (IAttachmentHandler) extension
125: .instanciateExtension(null);
126:
127: attachmentHandler
128: .execute(new AttachmentContext(
129: tempFile, header));
130: attachmentHandlerExecuted &= true;
131: } catch (PluginException e1) {
132: LOG.severe("Error while loading plugin: "
133: + e1.getMessage());
134: if (Logging.DEBUG)
135: e1.printStackTrace();
136: }
137: }
138:
139: } catch (PluginHandlerNotFoundException e2) {
140: LOG.severe("Error while loading plugin: "
141: + e2.getMessage());
142: if (Logging.DEBUG)
143: e2.printStackTrace();
144: }
145:
146: // in case no attachment handler was executed correctly
147: // -> fall back to default handler
148: if (!attachmentHandlerExecuted) {
149: boolean success = ColumbaDesktop.getInstance().open(
150: tempFile);
151:
152: // if attachment can't be opened, save it only
153: if (!success) {
154: File saveToFile = getDestinationFile(header);
155:
156: if (saveToFile.exists())
157: saveToFile.delete();
158: tempFile.renameTo(saveToFile);
159: }
160: }
161: }
162: }
163:
164: /**
165: * @see org.columba.api.command.Command#execute(Worker)
166: */
167: public void execute(IWorkerStatusController worker)
168: throws Exception {
169: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
170: IMailbox folder = (IMailbox) r.getSourceFolder();
171: Object[] uids = r.getUids();
172:
173: Integer[] address = r.getAddress();
174:
175: header = folder.getMimePartTree(uids[0])
176: .getFromAddress(address).getHeader();
177:
178: worker.setDisplayText("Opening " + header.getFileName());
179:
180: InputStream bodyStream = folder.getMimePartBodyStream(uids[0],
181: address);
182: // wrap with observable stream for progress bar updates
183: bodyStream = new ProgressObservedInputStream(bodyStream, worker);
184:
185: if (header.getMimeType().getType().equals("message")) {
186:
187: tempFolder = FolderTreeModel.getInstance().getTempFolder();
188: try {
189: tempMessageUid = tempFolder.addMessage(bodyStream);
190: } catch (Exception e) {
191: LOG
192: .warning("Could not create temporary email from the attachment.");
193: }
194:
195: } else {
196:
197: String filename = header.getFileName();
198: if (filename != null) {
199: tempFile = TempFileStore.createTempFile(filename);
200: } else {
201: tempFile = TempFileStore.createTempFile();
202: }
203:
204: int encoding = header.getContentTransferEncoding();
205:
206: switch (encoding) {
207: case MimeHeader.QUOTED_PRINTABLE:
208: bodyStream = new QuotedPrintableDecoderInputStream(
209: bodyStream);
210: break;
211:
212: case MimeHeader.BASE64:
213: bodyStream = new Base64DecoderInputStream(bodyStream);
214: break;
215: default:
216: }
217:
218: if (LOG.isLoggable(Level.FINE)) {
219: LOG.fine("Storing the attachment to :" + tempFile);
220: }
221:
222: FileOutputStream fileStream = new FileOutputStream(tempFile);
223: StreamUtils.streamCopy(bodyStream, fileStream);
224: fileStream.close();
225: bodyStream.close();
226: }
227: }
228:
229: protected File getDestinationFile(MimeHeader header) {
230: cFileChooser fileChooser;
231:
232: if (lastDir == null) {
233: fileChooser = new cFileChooser();
234: } else {
235: fileChooser = new cFileChooser(lastDir);
236: }
237:
238: cFileFilter fileFilter = new cFileFilter();
239: fileFilter
240: .acceptFilesWithProperty(cFileFilter.FILEPROPERTY_FILE);
241:
242: fileChooser.setDialogTitle("Save Attachment as ...");
243:
244: String fileName = getFilename(header);
245: if (fileName != null) {
246: fileChooser.forceSelectedFile(new File(fileName));
247: }
248:
249: fileChooser.setSelectFilter(fileFilter);
250: File tempFile = null;
251:
252: while (true) {
253: if (fileChooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) {
254: return null;
255: }
256:
257: tempFile = fileChooser.getSelectedFile();
258: lastDir = tempFile.getParentFile();
259:
260: if (tempFile.exists()) {
261: if (JOptionPane.showConfirmDialog(FrameManager
262: .getInstance().getActiveFrame(),
263: "Overwrite File?", "Warning",
264: JOptionPane.YES_NO_OPTION,
265: JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) {
266: break;
267: }
268: } else {
269: break;
270: }
271: }
272: return tempFile;
273: }
274: }
|