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.folder.command;
017:
018: import java.io.BufferedInputStream;
019: import java.io.BufferedOutputStream;
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.util.logging.Logger;
026:
027: import javax.swing.JFileChooser;
028: import javax.swing.JOptionPane;
029:
030: import org.columba.api.command.ICommandReference;
031: import org.columba.api.command.IWorkerStatusController;
032: import org.columba.core.command.Command;
033: import org.columba.core.command.StatusObservableImpl;
034: import org.columba.core.command.Worker;
035: import org.columba.core.gui.frame.FrameManager;
036: import org.columba.mail.command.IMailFolderCommandReference;
037: import org.columba.mail.folder.IMailbox;
038: import org.columba.mail.util.MailResourceLoader;
039: import org.columba.ristretto.coder.EncodedWord;
040:
041: /**
042: * Defines command for saving message source to file
043: *
044: * @author Karl Peder Olesen (karlpeder), 20030615
045: *
046: */
047: public class SaveMessageSourceAsCommand extends Command {
048:
049: /** JDK 1.4+ logging framework logger, used for logging. */
050: private static final Logger LOG = Logger
051: .getLogger("org.columba.mail.folder.command");
052:
053: /**
054: * Constructor for SaveMessageSourceAsCommand.
055: * @param frameMediator
056: * @param references
057: */
058: public SaveMessageSourceAsCommand(ICommandReference reference) {
059: super (reference);
060: }
061:
062: /**
063: * Executes the command, i.e. saves source for selected
064: * messages.
065: * @see org.columba.api.command.Command#execute(Worker)
066: */
067: public void execute(IWorkerStatusController worker)
068: throws Exception {
069: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
070: Object[] uids = r.getUids(); // uid for messages to save
071: IMailbox srcFolder = (IMailbox) r.getSourceFolder();
072:
073: // register for status events
074: ((StatusObservableImpl) srcFolder.getObservable())
075: .setWorker(worker);
076:
077: JFileChooser fileChooser = new JFileChooser();
078:
079: // Save message source for each selected message
080: for (int j = 0; j < uids.length; j++) {
081: Object uid = uids[j];
082: LOG.info("Saving UID=" + uid);
083:
084: //setup save dialog
085: String subject = (String) srcFolder.getHeaderFields(uid,
086: new String[] { "Subject" }).get("Subject");
087: subject = EncodedWord.decode(subject).toString();
088: String defaultName = getValidFilename(subject, false);
089:
090: if (defaultName.length() == 0) {
091: defaultName = srcFolder.getHeaderList().get(uid).get(
092: "columba.from").toString();
093: }
094:
095: fileChooser.setSelectedFile(new File(defaultName));
096: fileChooser.setDialogTitle(MailResourceLoader.getString(
097: "dialog", "saveas", "save_msg_source"));
098:
099: // show dialog
100: if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
101: File f = fileChooser.getSelectedFile();
102:
103: if (f.exists()) {
104: // file exists, user needs to confirm overwrite
105: int confirm = JOptionPane
106: .showConfirmDialog(FrameManager
107: .getInstance().getActiveFrame(),
108: MailResourceLoader.getString(
109: "dialog", "saveas",
110: "overwrite_existing_file"),
111: MailResourceLoader.getString(
112: "dialog", "saveas",
113: "file_exists"),
114: JOptionPane.YES_NO_OPTION,
115: JOptionPane.QUESTION_MESSAGE);
116:
117: if (confirm == JOptionPane.NO_OPTION) {
118: j--;
119:
120: continue;
121: }
122: }
123:
124: InputStream in = null;
125: OutputStream out = null;
126:
127: try {
128: // save message source under selected filename
129: in = new BufferedInputStream(srcFolder
130: .getMessageSourceStream(uid));
131: out = new BufferedOutputStream(
132: new FileOutputStream(f));
133:
134: byte[] buffer = new byte[1024];
135: int read;
136:
137: while ((read = in.read(buffer, 0, buffer.length)) > 0) {
138: out.write(buffer, 0, read);
139: }
140: } catch (IOException ioe) {
141: LOG.severe("Error saving msg source to file: "
142: + ioe.getMessage());
143: JOptionPane.showMessageDialog(null,
144: MailResourceLoader.getString("dialog",
145: "saveas", "err_save_msg"),
146: MailResourceLoader.getString("dialog",
147: "saveas", "err_save_title"),
148: JOptionPane.ERROR_MESSAGE);
149: } finally {
150: try {
151: in.close();
152: } catch (IOException ioe) {
153: }
154:
155: try {
156: if (out != null) {
157: out.close();
158: }
159: } catch (IOException ioe) {
160: }
161: }
162: }
163: }
164:
165: // end of loop over selected messages
166: }
167:
168: /**
169: * Private utility to extract a valid filename from a message
170: * subject or another string.<br>
171: * This means remove the chars: / \ : , \n \t
172: * NB: If the input string is null, an empty string is returned
173: * @param subj Message subject
174: * @param replSpaces If true, spaces are replaced by _
175: * @return A valid filename without the chars mentioned
176: */
177: private String getValidFilename(String subj, boolean replSpaces) {
178: if (subj == null) {
179: return "";
180: }
181:
182: StringBuffer buf = new StringBuffer();
183:
184: for (int i = 0; i < subj.length(); i++) {
185: char c = subj.charAt(i);
186:
187: if ((c == '\\') || (c == '/') || (c == ':') || (c == ',')
188: || (c == '\n') || (c == '\t') || (c == '[')
189: || (c == ']')) {
190: // dismiss char
191: } else if ((c == ' ') && (replSpaces)) {
192: buf.append('_');
193: } else {
194: buf.append(c);
195: }
196: }
197:
198: return buf.toString();
199: }
200: }
|