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.logging.Level;
022: import java.util.logging.Logger;
023:
024: import org.columba.api.command.ICommandReference;
025: import org.columba.api.command.IWorkerStatusController;
026: import org.columba.core.command.Command;
027: import org.columba.core.command.ProgressObservedInputStream;
028: import org.columba.core.command.Worker;
029: import org.columba.core.io.StreamUtils;
030: import org.columba.mail.command.IMailFolderCommandReference;
031: import org.columba.mail.folder.IMailbox;
032: import org.columba.ristretto.coder.Base64DecoderInputStream;
033: import org.columba.ristretto.coder.EncodedWord;
034: import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream;
035: import org.columba.ristretto.message.MimeHeader;
036:
037: /**
038: * @author freddy
039: */
040: public abstract class SaveAttachmentCommand extends Command {
041: protected static File lastDir = null;
042:
043: private static final Logger LOG = Logger
044: .getLogger("org.columba.mail.gui.message.attachment.command");
045:
046: /**
047: * Constructor for SaveAttachmentCommand.
048: *
049: * @param reference
050: * the reference for the command.
051: */
052: public SaveAttachmentCommand(ICommandReference reference) {
053: super (reference);
054: }
055:
056: /**
057: * @see org.columba.api.command.Command#execute(Worker)
058: */
059: public void execute(IWorkerStatusController worker)
060: throws Exception {
061: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
062: IMailbox folder = (IMailbox) r.getSourceFolder();
063: Object[] uids = r.getUids();
064:
065: Integer[] address = r.getAddress();
066:
067: MimeHeader header = folder.getMimePartTree(uids[0])
068: .getFromAddress(address).getHeader();
069:
070: InputStream bodyStream = folder.getMimePartBodyStream(uids[0],
071: address);
072:
073: // wrap with observable stream for progress bar updates
074: bodyStream = new ProgressObservedInputStream(bodyStream, worker);
075:
076: File destFile = getDestinationFile(header);
077: if (destFile == null)
078: return;
079:
080: worker.setDisplayText("Saving " + destFile.getName());
081:
082: // write to temporary file
083: File tempFile = new File(destFile.getAbsoluteFile() + ".part");
084:
085: if (tempFile == null)
086: return;
087:
088: int encoding = header.getContentTransferEncoding();
089:
090: switch (encoding) {
091: case MimeHeader.QUOTED_PRINTABLE:
092: bodyStream = new QuotedPrintableDecoderInputStream(
093: bodyStream);
094: break;
095:
096: case MimeHeader.BASE64:
097: bodyStream = new Base64DecoderInputStream(bodyStream);
098: break;
099: default:
100: }
101:
102: if (LOG.isLoggable(Level.FINE)) {
103: LOG.fine("Storing the attachment to :" + tempFile);
104: }
105:
106: FileOutputStream fileStream = new FileOutputStream(tempFile);
107: StreamUtils.streamCopy(bodyStream, fileStream);
108: fileStream.close();
109: bodyStream.close();
110:
111: // rename "*.part" file to destination file
112: tempFile.renameTo(destFile);
113:
114: // reset progress bar
115: worker.setProgressBarValue(0);
116:
117: // We are done - clear the status message with a delay
118: worker.clearDisplayTextWithDelay();
119: }
120:
121: /**
122: * Returns the filename of the attachment.
123: *
124: * @param mimepart
125: * the mime part containing the attachment.
126: * @return the filename for the attachment.
127: */
128: protected String getFilename(MimeHeader header) {
129: String fileName = header.getContentParameter("name");
130:
131: if (fileName == null) {
132: fileName = header.getDispositionParameter("filename");
133: }
134:
135: // decode filename
136: if (fileName != null) {
137: StringBuffer buf = EncodedWord.decode(fileName);
138: fileName = buf.toString();
139: }
140: return fileName;
141: }
142:
143: /**
144: * Returns the destination file for the attachment.
145: *
146: * @param mimepart
147: * the mime part containing the attachment.
148: * @return a File path; null if the saving should be cancelled.
149: */
150: protected abstract File getDestinationFile(MimeHeader header);
151: }
|