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.gui.message.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:
026: import org.columba.api.command.ICommandReference;
027: import org.columba.api.command.IWorkerStatusController;
028: import org.columba.core.command.Command;
029: import org.columba.core.command.StatusObservableImpl;
030: import org.columba.core.command.Worker;
031: import org.columba.core.desktop.ColumbaDesktop;
032: import org.columba.core.util.TempFileStore;
033: import org.columba.mail.command.IMailFolderCommandReference;
034: import org.columba.mail.folder.IMailbox;
035:
036: /**
037: * @author freddy
038: *
039: * To change this generated comment edit the template variable "typecomment":
040: * Window>Preferences>Java>Templates. To enable and disable the creation of type
041: * comments go to Window>Preferences>Java>Code Generation.
042: */
043: public class ViewMessageSourceCommand extends Command {
044: protected File tempFile;
045:
046: /**
047: * Constructor for ViewMessageSourceCommand.
048: *
049: * @param frameMediator
050: * @param references
051: */
052: public ViewMessageSourceCommand(ICommandReference reference) {
053: super (reference);
054: }
055:
056: /**
057: * @see org.columba.api.command.Command#updateGUI()
058: */
059: public void updateGUI() throws Exception {
060: ColumbaDesktop.getInstance().open(tempFile);
061: }
062:
063: /**
064: * @see org.columba.api.command.Command#execute(Worker)
065: */
066: public void execute(IWorkerStatusController worker)
067: throws Exception {
068:
069: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
070:
071: Object[] uids = r.getUids();
072:
073: IMailbox folder = (IMailbox) r.getSourceFolder();
074:
075: // register for status events
076: ((StatusObservableImpl) folder.getObservable())
077: .setWorker(worker);
078:
079: Object uid = uids[0];
080:
081: InputStream in = null;
082: OutputStream out = null;
083:
084: try {
085: in = new BufferedInputStream(folder
086: .getMessageSourceStream(uid));
087: tempFile = TempFileStore.createTempFileWithSuffix("txt");
088: out = new BufferedOutputStream(new FileOutputStream(
089: tempFile));
090:
091: byte[] buffer = new byte[1024];
092: int read;
093:
094: while ((read = in.read(buffer, 0, buffer.length)) > 0) {
095: out.write(buffer, 0, read);
096: }
097: } catch (IOException ioe) {
098: ioe.printStackTrace();
099: } finally {
100: if (in != null) {
101: try {
102: in.close();
103: } catch (IOException ioe) {
104: }
105: }
106:
107: if (out != null) {
108: try {
109: out.close();
110: } catch (IOException ioe) {
111: }
112: }
113: }
114: }
115: }
|