KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folder > command > SaveMessageSourceAsCommand


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.folder.command;
17
18 import java.io.BufferedInputStream JavaDoc;
19 import java.io.BufferedOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.swing.JFileChooser JavaDoc;
28 import javax.swing.JOptionPane JavaDoc;
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 /**
43  * Defines command for saving message source to file
44  *
45  * @author Karl Peder Olesen (karlpeder), 20030615
46  *
47  */

48 public class SaveMessageSourceAsCommand extends Command {
49
50     /** JDK 1.4+ logging framework logger, used for logging. */
51     private static final Logger JavaDoc LOG = Logger.getLogger("org.columba.mail.folder.command");
52
53     /**
54      * Constructor for SaveMessageSourceAsCommand.
55      * @param frameMediator
56      * @param references
57      */

58     public SaveMessageSourceAsCommand(ICommandReference reference) {
59         super(reference);
60     }
61
62
63     /**
64      * Executes the command, i.e. saves source for selected
65      * messages.
66      * @see org.columba.api.command.Command#execute(Worker)
67      */

68     public void execute(IWorkerStatusController worker)
69         throws Exception JavaDoc {
70         IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
71         Object JavaDoc[] uids = r.getUids(); // uid for messages to save
72
IMailbox srcFolder = (IMailbox) r.getSourceFolder();
73
74         // register for status events
75
((StatusObservableImpl) srcFolder.getObservable()).setWorker(worker);
76
77         JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc();
78
79         // Save message source for each selected message
80
for (int j = 0; j < uids.length; j++) {
81             Object JavaDoc uid = uids[j];
82             LOG.info("Saving UID=" + uid);
83
84             //setup save dialog
85
String JavaDoc subject = (String JavaDoc) srcFolder.getHeaderFields(uid, new String JavaDoc[] { "Subject" }).get("Subject");
86             subject = EncodedWord.decode(subject).toString();
87             String JavaDoc 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 JavaDoc(defaultName));
94             fileChooser.setDialogTitle(MailResourceLoader.getString("dialog",
95                     "saveas", "save_msg_source"));
96
97             // show dialog
98
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
99                 File JavaDoc f = fileChooser.getSelectedFile();
100
101                 if (f.exists()) {
102                     // file exists, user needs to confirm overwrite
103
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 JavaDoc in = null;
119                 OutputStream JavaDoc out = null;
120
121                 try {
122                     // save message source under selected filename
123
in = new BufferedInputStream JavaDoc(srcFolder.getMessageSourceStream(uid));
124                     out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(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 JavaDoc 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 JavaDoc ioe) {
143                     }
144
145                     try {
146                         if (out != null) {
147                             out.close();
148                         }
149                     } catch (IOException JavaDoc ioe) {
150                     }
151                 }
152             }
153         }
154
155         // end of loop over selected messages
156
}
157
158     /**
159      * Private utility to extract a valid filename from a message
160      * subject or another string.<br>
161      * This means remove the chars: / \ : , \n \t
162      * NB: If the input string is null, an empty string is returned
163      * @param subj Message subject
164      * @param replSpaces If true, spaces are replaced by _
165      * @return A valid filename without the chars mentioned
166      */

167     private String JavaDoc getValidFilename(String JavaDoc subj, boolean replSpaces) {
168         if (subj == null) {
169             return "";
170         }
171
172         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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                 // dismiss char
180
} 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