1 package org.columba.mail.folder.command; 17 18 import java.io.BufferedInputStream ; 19 import java.io.BufferedOutputStream ; 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.logging.Logger ; 26 27 import javax.swing.JFileChooser ; 28 import javax.swing.JOptionPane ; 29 30 import org.columba.api.command.ICommandReference; 31 import org.columba.api.command.IWorkerStatusController; 32 import org.columba.core.command.Command; 33 import org.columba.core.command.StatusObservableImpl; 34 import org.columba.core.command.Worker; 35 import org.columba.core.gui.frame.FrameManager; 36 import org.columba.mail.command.IMailFolderCommandReference; 37 import org.columba.mail.folder.IMailbox; 38 import org.columba.mail.util.MailResourceLoader; 39 import org.columba.ristretto.coder.EncodedWord; 40 41 42 48 public class SaveMessageSourceAsCommand extends Command { 49 50 51 private static final Logger LOG = Logger.getLogger("org.columba.mail.folder.command"); 52 53 58 public SaveMessageSourceAsCommand(ICommandReference reference) { 59 super(reference); 60 } 61 62 63 68 public void execute(IWorkerStatusController worker) 69 throws Exception { 70 IMailFolderCommandReference r = (IMailFolderCommandReference) getReference(); 71 Object [] uids = r.getUids(); IMailbox srcFolder = (IMailbox) r.getSourceFolder(); 73 74 ((StatusObservableImpl) srcFolder.getObservable()).setWorker(worker); 76 77 JFileChooser fileChooser = new JFileChooser (); 78 79 for (int j = 0; j < uids.length; j++) { 81 Object uid = uids[j]; 82 LOG.info("Saving UID=" + uid); 83 84 String subject = (String ) srcFolder.getHeaderFields(uid, new String [] { "Subject" }).get("Subject"); 86 subject = EncodedWord.decode(subject).toString(); 87 String defaultName = getValidFilename(subject, false); 88 89 if (defaultName.length() == 0) { 90 defaultName = srcFolder.getHeaderList().get(uid).get("columba.from").toString(); 91 } 92 93 fileChooser.setSelectedFile(new File (defaultName)); 94 fileChooser.setDialogTitle(MailResourceLoader.getString("dialog", 95 "saveas", "save_msg_source")); 96 97 if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { 99 File f = fileChooser.getSelectedFile(); 100 101 if (f.exists()) { 102 int confirm = JOptionPane.showConfirmDialog(FrameManager.getInstance() 104 .getActiveFrame(), 105 MailResourceLoader.getString("dialog", "saveas", 106 "overwrite_existing_file"), 107 MailResourceLoader.getString("dialog", "saveas", 108 "file_exists"), JOptionPane.YES_NO_OPTION, 109 JOptionPane.QUESTION_MESSAGE); 110 111 if (confirm == JOptionPane.NO_OPTION) { 112 j--; 113 114 continue; 115 } 116 } 117 118 InputStream in = null; 119 OutputStream out = null; 120 121 try { 122 in = new BufferedInputStream (srcFolder.getMessageSourceStream(uid)); 124 out = new BufferedOutputStream (new FileOutputStream (f)); 125 126 byte[] buffer = new byte[1024]; 127 int read; 128 129 while ((read = in.read(buffer, 0, buffer.length)) > 0) { 130 out.write(buffer, 0, read); 131 } 132 } catch (IOException ioe) { 133 LOG.severe("Error saving msg source to file: " + ioe.getMessage()); 134 JOptionPane.showMessageDialog(null, 135 MailResourceLoader.getString("dialog", "saveas", 136 "err_save_msg"), 137 MailResourceLoader.getString("dialog", "saveas", 138 "err_save_title"), JOptionPane.ERROR_MESSAGE); 139 } finally { 140 try { 141 in.close(); 142 } catch (IOException ioe) { 143 } 144 145 try { 146 if (out != null) { 147 out.close(); 148 } 149 } catch (IOException ioe) { 150 } 151 } 152 } 153 } 154 155 } 157 158 167 private String getValidFilename(String subj, boolean replSpaces) { 168 if (subj == null) { 169 return ""; 170 } 171 172 StringBuffer buf = new StringBuffer (); 173 174 for (int i = 0; i < subj.length(); i++) { 175 char c = subj.charAt(i); 176 177 if ((c == '\\') || (c == '/') || (c == ':') || (c == ',') 178 || (c == '\n') || (c == '\t') || (c == '[') || (c == ']')) { 179 } else if ((c == ' ') && (replSpaces)) { 181 buf.append('_'); 182 } else { 183 buf.append(c); 184 } 185 } 186 187 return buf.toString(); 188 } 189 } 190 | Popular Tags |