1 package org.columba.mail.folder.command; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import org.columba.api.command.ICommand; 24 import org.columba.api.command.ICommandReference; 25 import org.columba.api.command.IWorkerStatusController; 26 import org.columba.core.command.Command; 27 import org.columba.core.command.CommandProcessor; 28 import org.columba.core.command.StatusObservableImpl; 29 import org.columba.core.command.Worker; 30 import org.columba.mail.command.IMailFolderCommandReference; 31 import org.columba.mail.command.MailFolderCommandReference; 32 import org.columba.mail.config.AccountItem; 33 import org.columba.mail.folder.IMailFolder; 34 import org.columba.mail.folder.IMailbox; 35 import org.columba.mail.folder.RootFolder; 36 import org.columba.mail.gui.tree.FolderTreeModel; 37 import org.columba.mail.spam.command.CommandHelper; 38 import org.columba.mail.spam.command.LearnMessageAsHamCommand; 39 import org.columba.mail.spam.command.LearnMessageAsSpamCommand; 40 import org.columba.ristretto.message.Flags; 41 42 54 public class ToggleMarkCommand extends Command { 55 56 private static final java.util.logging.Logger LOG = 57 java.util.logging.Logger.getLogger("org.columba.mail.folder.command"); 59 private IWorkerStatusController worker; 60 61 private List <MarkMessageCommand> commandList; 62 63 69 public ToggleMarkCommand(ICommandReference reference) { 70 super(reference); 71 72 commandList = new ArrayList <MarkMessageCommand>(); 73 } 74 75 78 public void execute(IWorkerStatusController worker) throws Exception { 79 this.worker = worker; 80 81 88 IMailFolderCommandReference r = (IMailFolderCommandReference) getReference(); 89 90 Object [] uids = r.getUids(); 92 93 IMailbox srcFolder = (IMailbox) r.getSourceFolder(); 95 96 ((StatusObservableImpl) srcFolder.getObservable()).setWorker(worker); 98 99 int markVariant = r.getMarkVariant(); 101 102 List <Object > list1 = new ArrayList <Object >(); 105 List <Object > list2 = new ArrayList <Object >(); 106 107 for (int j = 0; j < uids.length; j++) { 108 Flags flags = srcFolder.getFlags(uids[j]); 109 110 boolean result = false; 111 if (markVariant == MarkMessageCommand.MARK_AS_READ) { 112 if (flags.getSeen()) 113 result = true; 114 } else if (markVariant == MarkMessageCommand.MARK_AS_FLAGGED) { 115 if (flags.getFlagged()) 116 result = true; 117 } else if (markVariant == MarkMessageCommand.MARK_AS_EXPUNGED) { 118 if (flags.getDeleted()) 119 result = true; 120 } else if (markVariant == MarkMessageCommand.MARK_AS_ANSWERED) { 121 if (flags.getAnswered()) 122 result = true; 123 } else if (markVariant == MarkMessageCommand.MARK_AS_DRAFT) { 124 if (flags.getDraft()) 125 result = true; 126 } else if (markVariant == MarkMessageCommand.MARK_AS_SPAM) { 127 boolean spam = ((Boolean ) srcFolder.getAttribute(uids[j], 128 "columba.spam")).booleanValue(); 129 if (spam) 130 result = true; 131 } 132 133 if (result) 134 list1.add(uids[j]); 135 else 136 list2.add(uids[j]); 137 } 138 139 MailFolderCommandReference ref = null; 140 141 if (list1.size() > 0) { 143 ref = new MailFolderCommandReference(srcFolder, list1.toArray()); 144 ref.setMarkVariant(-markVariant); 145 MarkMessageCommand c = new MarkMessageCommand(ref); 146 commandList.add(c); 147 c.execute(worker); 148 149 if ((markVariant == MarkMessageCommand.MARK_AS_SPAM) 151 || (markVariant == MarkMessageCommand.MARK_AS_NOTSPAM)) { 152 processSpamFilter(uids, srcFolder, -markVariant); 153 } 154 } 155 156 if (list2.size() > 0) { 158 ref = new MailFolderCommandReference(srcFolder, list2.toArray()); 159 ref.setMarkVariant(markVariant); 160 MarkMessageCommand c = new MarkMessageCommand(ref); 161 commandList.add(c); 162 c.execute(worker); 163 164 if ((markVariant == MarkMessageCommand.MARK_AS_SPAM) 166 || (markVariant == MarkMessageCommand.MARK_AS_NOTSPAM)) { 167 processSpamFilter(uids, srcFolder, markVariant); 168 } 169 } 170 171 } 172 173 187 private void processSpamFilter(Object [] uids, IMailbox srcFolder, 188 int markVariant) throws Exception { 189 190 worker.setDisplayText("Training messages..."); 192 worker.setProgressBarMaximum(uids.length); 193 194 for (int j = 0; j < uids.length; j++) { 197 198 worker.setDisplayText("Training messages..."); 199 worker.setProgressBarMaximum(uids.length); 200 worker.setProgressBarValue(j); 202 203 if (worker.cancelled()) { 205 break; 206 } 207 208 AccountItem item = CommandHelper.retrieveAccountItem(srcFolder, 210 uids[j]); 211 if (item == null) 213 continue; 214 215 if (item.getSpamItem().isEnabled() == false) 217 continue; 218 219 LOG.info("learning uid=" + uids[j]); 221 IMailFolderCommandReference ref = new MailFolderCommandReference(srcFolder, 223 new Object [] { uids[j] }); 224 225 ICommand c = null; 227 if (markVariant == MarkMessageCommand.MARK_AS_SPAM) 228 c = new LearnMessageAsSpamCommand(ref); 229 else 230 c = new LearnMessageAsHamCommand(ref); 231 232 c.execute(worker); 234 235 if (markVariant == MarkMessageCommand.MARK_AS_NOTSPAM) 237 continue; 238 239 if (item.getSpamItem().isMoveMessageWhenMarkingEnabled() == false) 241 continue; 242 243 if (item.getSpamItem().isMoveTrashSelected() == false) { 244 IMailFolder destFolder = FolderTreeModel.getInstance() 247 .getFolder(item.getSpamItem().getMoveCustomFolder()); 248 249 MailFolderCommandReference ref2 = new MailFolderCommandReference( 251 srcFolder, destFolder, new Object [] { uids[j] }); 252 CommandProcessor.getInstance().addOp(new MoveMessageCommand(ref2)); 253 254 } else { 255 IMailbox trash = (IMailbox) ((RootFolder) srcFolder 257 .getRootFolder()).getTrashFolder(); 258 259 MailFolderCommandReference ref2 = new MailFolderCommandReference( 261 srcFolder, trash, new Object [] { uids[j] }); 262 263 CommandProcessor.getInstance().addOp(new MoveMessageCommand(ref2)); 264 265 } 266 267 } 268 } 269 } | Popular Tags |