KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > message > command > SaveAttachmentCommand


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.gui.message.command;
17
18 import java.io.File JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.logging.Level JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24 import org.columba.api.command.ICommandReference;
25 import org.columba.api.command.IWorkerStatusController;
26 import org.columba.core.command.Command;
27 import org.columba.core.command.ProgressObservedInputStream;
28 import org.columba.core.command.Worker;
29 import org.columba.core.io.StreamUtils;
30 import org.columba.mail.command.IMailFolderCommandReference;
31 import org.columba.mail.folder.IMailbox;
32 import org.columba.ristretto.coder.Base64DecoderInputStream;
33 import org.columba.ristretto.coder.EncodedWord;
34 import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream;
35 import org.columba.ristretto.message.MimeHeader;
36
37 /**
38  * @author freddy
39  */

40 public abstract class SaveAttachmentCommand extends Command {
41     protected static File JavaDoc lastDir = null;
42
43     private static final Logger JavaDoc LOG = Logger
44             .getLogger("org.columba.mail.gui.message.attachment.command");
45
46     /**
47      * Constructor for SaveAttachmentCommand.
48      *
49      * @param reference
50      * the reference for the command.
51      */

52     public SaveAttachmentCommand(ICommandReference reference) {
53         super(reference);
54     }
55
56     /**
57      * @see org.columba.api.command.Command#execute(Worker)
58      */

59     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
60         IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
61         IMailbox folder = (IMailbox) r.getSourceFolder();
62         Object JavaDoc[] uids = r.getUids();
63
64         Integer JavaDoc[] address = r.getAddress();
65
66         MimeHeader header = folder.getMimePartTree(uids[0]).getFromAddress(
67                 address).getHeader();
68
69         InputStream JavaDoc bodyStream = folder.getMimePartBodyStream(uids[0], address);
70
71         // wrap with observable stream for progress bar updates
72
bodyStream = new ProgressObservedInputStream(bodyStream, worker);
73
74         File JavaDoc destFile = getDestinationFile(header);
75         if ( destFile == null ) return;
76         
77         worker.setDisplayText("Saving " + destFile.getName());
78
79         // write to temporary file
80
File JavaDoc tempFile = new File JavaDoc(destFile.getAbsoluteFile() + ".part");
81
82         if (tempFile == null)
83             return;
84
85         int encoding = header.getContentTransferEncoding();
86
87         switch (encoding) {
88         case MimeHeader.QUOTED_PRINTABLE:
89             bodyStream = new QuotedPrintableDecoderInputStream(bodyStream);
90             break;
91
92         case MimeHeader.BASE64:
93             bodyStream = new Base64DecoderInputStream(bodyStream);
94             break;
95         default:
96         }
97
98         if (LOG.isLoggable(Level.FINE)) {
99             LOG.fine("Storing the attachment to :" + tempFile);
100         }
101
102         FileOutputStream JavaDoc fileStream = new FileOutputStream JavaDoc(tempFile);
103         StreamUtils.streamCopy(bodyStream, fileStream);
104         fileStream.close();
105         bodyStream.close();
106
107         // rename "*.part" file to destination file
108
tempFile.renameTo(destFile);
109
110         // reset progress bar
111
worker.setProgressBarValue(0);
112
113         // We are done - clear the status message with a delay
114
worker.clearDisplayTextWithDelay();
115     }
116
117     /**
118      * Returns the filename of the attachment.
119      *
120      * @param mimepart
121      * the mime part containing the attachment.
122      * @return the filename for the attachment.
123      */

124     protected String JavaDoc getFilename(MimeHeader header) {
125         String JavaDoc fileName = header.getContentParameter("name");
126
127         if (fileName == null) {
128             fileName = header.getDispositionParameter("filename");
129         }
130
131         // decode filename
132
if (fileName != null) {
133             StringBuffer JavaDoc buf = EncodedWord.decode(fileName);
134             fileName = buf.toString();
135         }
136         return fileName;
137     }
138
139     /**
140      * Returns the destination file for the attachment.
141      *
142      * @param mimepart
143      * the mime part containing the attachment.
144      * @return a File path; null if the saving should be cancelled.
145      */

146     protected abstract File JavaDoc getDestinationFile(MimeHeader header);
147 }
Popular Tags