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:
017: package org.columba.mail.folder.mailboximport;
018:
019: import java.io.File;
020:
021: import javax.swing.JOptionPane;
022:
023: import org.columba.api.command.IWorkerStatusController;
024: import org.columba.api.plugin.IExtensionInterface;
025: import org.columba.core.gui.frame.FrameManager;
026: import org.columba.core.resourceloader.IconKeys;
027: import org.columba.core.resourceloader.ImageLoader;
028: import org.columba.mail.folder.IMailbox;
029: import org.columba.ristretto.io.CharSequenceSource;
030: import org.columba.ristretto.io.SourceInputStream;
031:
032: /**
033: * This is the base class for mailbox importers.
034: */
035: public abstract class AbstractMailboxImporter implements
036: IExtensionInterface {
037: public static final int TYPE_FILE = 0;
038: public static final int TYPE_DIRECTORY = 1;
039:
040: protected IMailbox destinationFolder;
041: protected File[] sourceFiles;
042: protected int counter = 0;
043:
044: public AbstractMailboxImporter(IMailbox destinationFolder,
045: File[] sourceFiles) {
046: this ();
047: setDestinationFolder(destinationFolder);
048: setSourceFiles(sourceFiles);
049: }
050:
051: /**
052: * Default constructor
053: */
054: public AbstractMailboxImporter() {
055: }
056:
057: /**
058: * Override this method to specify type.
059: * the wizard dialog will open the correct file/directory dialog automatically
060: */
061: public int getType() {
062: return TYPE_FILE;
063: }
064:
065: /**
066: * Override this method to do the actual import work. In here, the messages
067: * should be read and passed to the folder using saveMessage.
068: */
069: public abstract void importMailboxFile(File file,
070: IWorkerStatusController worker, IMailbox destFolder)
071: throws Exception;
072:
073: /**
074: * Override this method to provide an adequate description to the user.
075: */
076: public abstract String getDescription();
077:
078: /*********** intern methods (no need to overwrite these) ****************/
079: /**
080: * Sets the source files/directories.
081: */
082: public void setSourceFiles(File[] files) {
083: this .sourceFiles = files;
084: }
085:
086: /**
087: * Set the destination folder.
088: */
089: public void setDestinationFolder(IMailbox folder) {
090: destinationFolder = folder;
091: }
092:
093: /**
094: * Returns the number of successfully imported messages so far.
095: */
096: public int getCount() {
097: return counter;
098: }
099:
100: /**
101: * This method calls your overridden importMailbox(File)-method
102: * and handles exceptions.
103: */
104: public void run(IWorkerStatusController worker) {
105: //TODO (@author fdietz): i18n
106: worker.setDisplayText("Importing messages...");
107:
108: importMailbox(worker);
109:
110: if (getCount() == 0) {
111: //TODO (@author fdietz): i18n
112: JOptionPane
113: .showMessageDialog(
114: FrameManager.getInstance().getActiveFrame(),
115: "Message import failed! No messages were added to the folder.\n"
116: + "This means that the parser didn't throw any exception even if "
117: + "it didn't recognize the mailbox format or the messagebox simply "
118: + "didn't contain any messages.",
119: "Warning", JOptionPane.WARNING_MESSAGE);
120:
121: return;
122: } else {
123: //TODO (@author fdietz): i18n
124: JOptionPane.showMessageDialog(null,
125: "Message import was successful!", "Information",
126: JOptionPane.INFORMATION_MESSAGE, ImageLoader
127: .getIcon(IconKeys.DIALOG_INFO));
128: }
129: }
130:
131: /**
132: * Import all mailbox files in Columba. This method makes use of the
133: * importMailbox method you have to override and simply iterates over all
134: * given files/directories.
135: *
136: * @param worker
137: */
138: public void importMailbox(IWorkerStatusController worker) {
139: File[] listing = getSourceFiles();
140:
141: for (int i = 0; i < listing.length; i++) {
142: if (worker.cancelled()) {
143: return;
144: }
145:
146: try {
147: importMailboxFile(listing[i], worker,
148: getDestinationFolder());
149: } catch (Exception ex) {
150: //TODO (@author fdietz): i18n
151: int result = JOptionPane
152: .showConfirmDialog(
153: FrameManager.getInstance()
154: .getActiveFrame(),
155: "An error occured while importing a message. Try again?",
156: "Retry message import?",
157: JOptionPane.YES_NO_CANCEL_OPTION,
158: JOptionPane.WARNING_MESSAGE);
159: if (result == JOptionPane.YES_OPTION) {
160: i--;
161: } else if (result == JOptionPane.CANCEL_OPTION) {
162: worker.cancel();
163: }
164: }
165: }
166: }
167:
168: /**
169: * Use this method to save a message in the specified destination folder.
170: */
171: protected void saveMessage(String rawString,
172: IWorkerStatusController worker, IMailbox destFolder)
173: throws Exception {
174: /*
175: * *20031231, karlpeder* Using InputStream instead of rawString
176: * directly. Ensures size is set correctly by addMessage (bug #843657)
177: */
178:
179: SourceInputStream in = new SourceInputStream(
180: new CharSequenceSource(rawString));
181: destFolder.addMessage(in);
182: in.close();
183:
184: counter++;
185:
186: //TODO (@author fdietz): i18n
187: worker.setDisplayText("Importing messages: " + getCount());
188: }
189:
190: /**
191: * Returns the folder new messages will be added to.
192: */
193: public IMailbox getDestinationFolder() {
194: return destinationFolder;
195: }
196:
197: /**
198: * Returns the source files/directories new messages will be read from.
199: */
200: public File[] getSourceFiles() {
201: return sourceFiles;
202: }
203: }
|