1 17 package org.columba.mail.folder.mailboximport; 18 19 import java.io.File ; 20 21 import javax.swing.JOptionPane ; 22 23 import org.columba.api.command.IWorkerStatusController; 24 import org.columba.api.plugin.IExtensionInterface; 25 import org.columba.core.gui.frame.FrameManager; 26 import org.columba.core.resourceloader.IconKeys; 27 import org.columba.core.resourceloader.ImageLoader; 28 import org.columba.mail.folder.IMailbox; 29 import org.columba.ristretto.io.CharSequenceSource; 30 import org.columba.ristretto.io.SourceInputStream; 31 32 35 public abstract class AbstractMailboxImporter implements IExtensionInterface{ 36 public static final int TYPE_FILE = 0; 37 public static final int TYPE_DIRECTORY = 1; 38 39 protected IMailbox destinationFolder; 40 protected File [] sourceFiles; 41 protected int counter = 0; 42 43 public AbstractMailboxImporter(IMailbox destinationFolder, File [] sourceFiles) { 44 this(); 45 setDestinationFolder(destinationFolder); 46 setSourceFiles(sourceFiles); 47 } 48 49 52 public AbstractMailboxImporter() {} 53 54 58 public int getType() { 59 return TYPE_FILE; 60 } 61 62 66 public abstract void importMailboxFile(File file, 67 IWorkerStatusController worker, IMailbox destFolder) 68 throws Exception ; 69 70 73 public abstract String getDescription(); 74 75 76 79 public void setSourceFiles(File [] files) { 80 this.sourceFiles = files; 81 } 82 83 86 public void setDestinationFolder(IMailbox folder) { 87 destinationFolder = folder; 88 } 89 90 93 public int getCount() { 94 return counter; 95 } 96 97 101 public void run(IWorkerStatusController worker) { 102 worker.setDisplayText("Importing messages..."); 104 105 importMailbox(worker); 106 107 if (getCount() == 0) { 108 JOptionPane.showMessageDialog(FrameManager.getInstance() 110 .getActiveFrame(), 111 "Message import failed! No messages were added to the folder.\n" + 112 "This means that the parser didn't throw any exception even if " + 113 "it didn't recognize the mailbox format or the messagebox simply " + 114 "didn't contain any messages.", 115 "Warning", JOptionPane.WARNING_MESSAGE); 116 117 return; 118 } else { 119 JOptionPane.showMessageDialog(null, 121 "Message import was successful!", "Information", 122 JOptionPane.INFORMATION_MESSAGE, 123 ImageLoader.getIcon(IconKeys.DIALOG_INFO)); 124 } 125 } 126 127 134 public void importMailbox(IWorkerStatusController worker) { 135 File [] listing = getSourceFiles(); 136 137 for (int i = 0; i < listing.length; i++) { 138 if (worker.cancelled()) { 139 return; 140 } 141 142 try { 143 importMailboxFile(listing[i], worker, getDestinationFolder()); 144 } catch (Exception ex) { 145 int result = JOptionPane.showConfirmDialog( 147 FrameManager.getInstance().getActiveFrame(), 148 "An error occured while importing a message. Try again?", 149 "Retry message import?", 150 JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); 151 if (result == JOptionPane.YES_OPTION) { 152 i--; 153 } else if (result == JOptionPane.CANCEL_OPTION) { 154 worker.cancel(); 155 } 156 } 157 } 158 } 159 160 163 protected void saveMessage(String rawString, IWorkerStatusController worker, 164 IMailbox destFolder) throws Exception { 165 169 170 SourceInputStream in = new SourceInputStream(new CharSequenceSource( 171 rawString)); 172 destFolder.addMessage(in); 173 in.close(); 174 175 counter++; 176 177 worker.setDisplayText("Importing messages: " + getCount()); 179 } 180 181 184 public IMailbox getDestinationFolder() { 185 return destinationFolder; 186 } 187 188 191 public File [] getSourceFiles() { 192 return sourceFiles; 193 } 194 } 195 | Popular Tags |