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.IOException;
019: import java.io.InputStream;
020: import java.text.MessageFormat;
021: import java.util.logging.Logger;
022:
023: import javax.swing.JOptionPane;
024:
025: import org.columba.api.command.ICommandReference;
026: import org.columba.api.command.IWorkerStatusController;
027: import org.columba.core.command.Command;
028: import org.columba.core.command.StatusObservableImpl;
029: import org.columba.core.command.Worker;
030: import org.columba.core.gui.frame.FrameManager;
031: import org.columba.mail.command.IMailFolderCommandReference;
032: import org.columba.mail.folder.IMailbox;
033: import org.columba.mail.util.MailResourceLoader;
034: import org.columba.ristretto.message.Attributes;
035: import org.columba.ristretto.message.Flags;
036:
037: /**
038: * Copy a set of messages from a source to a destination folder.
039: * <p>
040: * A dialog asks the user the destination folder.
041: *
042: * @author fdietz
043: *
044: */
045: public class CopyMessageCommand extends Command {
046:
047: /** JDK 1.4+ logging framework logger, used for logging. */
048: private static final Logger LOG = Logger
049: .getLogger("org.columba.mail.folder.command");
050:
051: protected IMailbox destFolder;
052:
053: protected IMailFolderCommandReference r;
054:
055: /**
056: * Constructor for CopyMessageCommand.
057: *
058: * @param frameMediator
059: * @param references
060: */
061: public CopyMessageCommand(ICommandReference reference) {
062: super (reference);
063: }
064:
065: protected void doExecute(IWorkerStatusController worker,
066: String statusMessage, String errorRetryMessage,
067: String errorIgnoreMessage, String errorCopyMessage,
068: String errorTitle, String canceledMessage) throws Exception {
069: // get references
070: r = (IMailFolderCommandReference) getReference();
071:
072: // get destination foldedr
073: destFolder = (IMailbox) r.getDestinationFolder();
074:
075: Object[] uids = r.getUids();
076:
077: // get source folder
078: IMailbox srcFolder = (IMailbox) r.getSourceFolder();
079:
080: // register for status events
081: ((StatusObservableImpl) srcFolder.getObservable())
082: .setWorker(worker);
083:
084: // setting lastSelection for srcFolder to null
085: srcFolder.setLastSelection(null);
086:
087: LOG.fine("src=" + srcFolder + " dest=" + destFolder);
088:
089: // update status message
090: worker.setDisplayText(MessageFormat.format(MailResourceLoader
091: .getString("statusbar", "message", statusMessage),
092: new Object[] { destFolder.getName() }));
093:
094: // initialize progress bar with total number of messages
095: worker.setProgressBarMaximum(uids.length);
096:
097: if (srcFolder.getRootFolder()
098: .equals(destFolder.getRootFolder())) {
099: // folders have same root folder
100: // -> for example: two IMAP folders on the same server
101: // -----> this means we use server-side copying which
102: // -----> is much faster than using inputstreams here
103: //
104: // also used for local folders, which saves some parsing work
105: srcFolder.innerCopy(destFolder, uids);
106: } else {
107: // two different root folders
108: // -> get inputstream from source-folder and add it to
109: // -> destination-folder as inputstream
110: // -----> moving of raw message source
111: // (works also for copying from local to IMAP folders, etc.
112: for (int j = 0; (j < uids.length) && !worker.cancelled(); j++) {
113: if (!srcFolder.exists(uids[j])) {
114: continue;
115: }
116:
117: try {
118: // add source to destination folder
119: Attributes attributes = srcFolder
120: .getAttributes(uids[j]);
121: Flags flags = srcFolder.getFlags(uids[j]);
122: InputStream messageSourceStream = srcFolder
123: .getMessageSourceStream(uids[j]);
124: destFolder.addMessage(messageSourceStream,
125: attributes, flags);
126: messageSourceStream.close();
127: } catch (IOException ioe) {
128: String[] options = new String[] {
129: MailResourceLoader.getString("statusbar",
130: "message", errorRetryMessage),
131: MailResourceLoader.getString("statusbar",
132: "message", errorIgnoreMessage),
133: MailResourceLoader.getString("", "global",
134: "cancel") };
135:
136: int result = JOptionPane
137: .showOptionDialog(FrameManager
138: .getInstance().getActiveFrame(),
139: MailResourceLoader.getString(
140: "statusbar", "message",
141: errorCopyMessage),
142: MailResourceLoader.getString(
143: "statusbar", "message",
144: errorTitle),
145: JOptionPane.YES_NO_CANCEL_OPTION,
146: JOptionPane.ERROR_MESSAGE, null,
147: options, options[0]);
148: switch (result) {
149: case JOptionPane.YES_OPTION:
150:
151: //retry copy
152: j--;
153:
154: break;
155:
156: case JOptionPane.CANCEL_OPTION:
157: worker.cancel();
158:
159: default:
160:
161: continue;
162: }
163: }
164:
165: // update progress bar
166: worker.setProgressBarValue(j);
167: }
168: }
169:
170: //reset progress bar
171: worker.setProgressBarValue(0);
172:
173: if (worker.cancelled()) {
174: worker.setDisplayText(MailResourceLoader.getString(
175: "statusbar", "message", canceledMessage));
176: } else {
177: // We are done - clear the status message with a delay
178: worker.clearDisplayTextWithDelay();
179: }
180:
181: }
182:
183: /**
184: * @see org.columba.api.command.Command#execute(Worker)
185: */
186: public void execute(IWorkerStatusController worker)
187: throws Exception {
188: doExecute(worker, "copy_messages", "err_copy_messages_retry",
189: "err_copy_messages_ignore", "err_copy_messages_msg",
190: "err_copy_messages_title", "copy_messages_cancelled");
191: }
192: }
|