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.FileOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.text.MessageFormat;
025:
026: import javax.swing.JOptionPane;
027:
028: import org.columba.api.command.ICommandReference;
029: import org.columba.api.command.IWorkerStatusController;
030: import org.columba.core.command.Command;
031: import org.columba.core.gui.frame.FrameManager;
032: import org.columba.mail.command.IMailFolderCommandReference;
033: import org.columba.mail.folder.IMailbox;
034: import org.columba.mail.util.MailResourceLoader;
035:
036: /**
037: * Export all selected folders to a single MBOX mailbox file.
038: *
039: * MBOX mailbox format: http://www.qmail.org/qmail-manual-html/man5/mbox.html
040: *
041: * @author fdietz
042: */
043: public class ExportFolderCommand extends Command {
044:
045: protected Object[] destUids;
046:
047: /**
048: * @param references
049: */
050: public ExportFolderCommand(ICommandReference reference) {
051: super (reference);
052: }
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see org.columba.api.command.Command#execute(org.columba.api.command.Worker)
058: */
059: public void execute(IWorkerStatusController worker)
060: throws Exception {
061: // get references
062: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
063:
064: OutputStream os = null;
065:
066: try {
067: // create output stream
068: os = new BufferedOutputStream(new FileOutputStream(r
069: .getDestFile()));
070:
071: int counter = 0;
072: IMailbox srcFolder;
073: Object[] uids;
074: InputStream in;
075: int read;
076: byte[] buffer = new byte[1024];
077:
078: // get source folder
079: srcFolder = (IMailbox) r.getSourceFolder();
080:
081: // get array of message UIDs
082: uids = srcFolder.getUids();
083:
084: // initialize progressbar with total number of messages
085: worker.setProgressBarMaximum(uids.length);
086: worker.setProgressBarValue(0);
087:
088: // for each message in folder i
089: for (int j = 0; (j < uids.length) && !worker.cancelled(); j++) {
090: // get message source from folder
091: in = new BufferedInputStream(srcFolder
092: .getMessageSourceStream(uids[j]));
093:
094: // prepend From line
095: os.write(new String("From \r\n").getBytes());
096:
097: // write message source to file
098: while ((read = in.read(buffer, 0, buffer.length)) > 0) {
099: os.write(buffer, 0, read);
100: }
101:
102: try {
103: in.close();
104: } catch (IOException ioe_) {
105: }
106:
107: // append newline
108: os.write(new String("\r\n").getBytes());
109:
110: os.flush();
111:
112: worker.setProgressBarValue(j);
113: counter++;
114: }
115:
116: // update status message
117: if (worker.cancelled()) {
118: worker.setDisplayText(MailResourceLoader.getString(
119: "statusbar", "message",
120: "export_messages_cancelled"));
121: } else {
122: worker.setDisplayText(MessageFormat.format(
123: MailResourceLoader.getString("statusbar",
124: "message", "export_messages_success"),
125: new Object[] { Integer.toString(counter) }));
126: }
127: } catch (IOException ioe) {
128: JOptionPane.showMessageDialog(FrameManager.getInstance()
129: .getActiveFrame(), MailResourceLoader.getString(
130: "statusbar", "message", "err_export_messages_msg"),
131: MailResourceLoader.getString("statusbar",
132: "messages", "err_export_messages_title"),
133: JOptionPane.ERROR_MESSAGE);
134: } finally {
135: try {
136: // close output stream
137: if (os != null) {
138: os.close();
139: }
140: } catch (IOException ioe) {
141: }
142: }
143: }
144: }
|