KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folder > command > ToggleMarkCommand


1 // The contents of this file are subject to the Mozilla Public License Version
2
//1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
//Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.folder.command;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
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 /**
43  * Toggle flag.
44  * <p>
45  * Creates two sets of messages and uses {@link MarkMessageCommand}, which does
46  * the flag change.
47  * <p>
48  * Additionally, if message is marked as spam or non-spam the bayesian filter is
49  * trained.
50  *
51  * @see MarkMessageCommand
52  * @author fdietz
53  */

54 public class ToggleMarkCommand extends Command {
55     
56     private static final java.util.logging.Logger JavaDoc LOG =
57         java.util.logging.Logger.getLogger("org.columba.mail.folder.command"); //$NON-NLS-1$
58

59     private IWorkerStatusController worker;
60
61     private List JavaDoc<MarkMessageCommand> commandList;
62
63     /**
64      * Constructor for ToggleMarkCommand.
65      *
66      * @param frameMediator
67      * @param references
68      */

69     public ToggleMarkCommand(ICommandReference reference) {
70         super(reference);
71
72         commandList = new ArrayList JavaDoc<MarkMessageCommand>();
73     }
74
75     /**
76      * @see org.columba.api.command.Command#execute(Worker)
77      */

78     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
79         this.worker = worker;
80
81         /*
82          * // use wrapper class for easier handling of references array adapter =
83          * new FolderCommandAdapter( (MailFolderCommandReference[])
84          * getReferences());
85          * // get array of source references MailFolderCommandReference[] r =
86          * adapter.getSourceFolderReferences();
87          */

88         IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
89
90         // get array of message UIDs
91
Object JavaDoc[] uids = r.getUids();
92
93         // get source folder
94
IMailbox srcFolder = (IMailbox) r.getSourceFolder();
95
96         // register for status events
97
((StatusObservableImpl) srcFolder.getObservable()).setWorker(worker);
98
99         // which kind of mark?
100
int markVariant = r.getMarkVariant();
101
102         // create one list containing the marked items, which have to be "unmarked"
103
// and another list containing the items remained to be marked
104
List JavaDoc<Object JavaDoc> list1 = new ArrayList JavaDoc<Object JavaDoc>();
105         List JavaDoc<Object JavaDoc> list2 = new ArrayList JavaDoc<Object JavaDoc>();
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 JavaDoc) 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         // un-mark messages
142
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             // train bayesian filter
150
if ((markVariant == MarkMessageCommand.MARK_AS_SPAM)
151                     || (markVariant == MarkMessageCommand.MARK_AS_NOTSPAM)) {
152                 processSpamFilter(uids, srcFolder, -markVariant);
153             }
154         }
155
156         // mark messages
157
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             // train bayesian filter
165
if ((markVariant == MarkMessageCommand.MARK_AS_SPAM)
166                     || (markVariant == MarkMessageCommand.MARK_AS_NOTSPAM)) {
167                 processSpamFilter(uids, srcFolder, markVariant);
168             }
169         }
170
171     }
172
173     /**
174      * Train spam filter.
175      * <p>
176      * Move message to specified folder or delete message immediately based on
177      * account configuration.
178      *
179      * @param uids
180      * message uid
181      * @param srcFolder
182      * source folder
183      * @param markVariant
184      * mark variant (spam/not spam)
185      * @throws Exception
186      */

187     private void processSpamFilter(Object JavaDoc[] uids, IMailbox srcFolder,
188             int markVariant) throws Exception JavaDoc {
189
190         // update status message
191
worker.setDisplayText("Training messages...");
192         worker.setProgressBarMaximum(uids.length);
193
194         // mark as spam /as not spam
195
// for each message
196
for (int j = 0; j < uids.length; j++) {
197
198             worker.setDisplayText("Training messages...");
199             worker.setProgressBarMaximum(uids.length);
200             // increase progressbar value
201
worker.setProgressBarValue(j);
202
203             // cancel here if user requests
204
if (worker.cancelled()) {
205                 break;
206             }
207
208             // message belongs to which account?
209
AccountItem item = CommandHelper.retrieveAccountItem(srcFolder,
210                     uids[j]);
211             // skip if account information is not available
212
if (item == null)
213                 continue;
214
215             // if spam filter is not enabled -> return
216
if (item.getSpamItem().isEnabled() == false)
217                 continue;
218
219             LOG.info("learning uid=" + uids[j]); //$NON-NLS-1$
220

221             // create reference
222
IMailFolderCommandReference ref = new MailFolderCommandReference(srcFolder,
223                     new Object JavaDoc[] { uids[j] });
224
225             // create command
226
ICommand c = null;
227             if (markVariant == MarkMessageCommand.MARK_AS_SPAM)
228                 c = new LearnMessageAsSpamCommand(ref);
229             else
230                 c = new LearnMessageAsHamCommand(ref);
231
232             // execute command
233
c.execute(worker);
234
235             // skip if message is *not* marked as spam
236
if (markVariant == MarkMessageCommand.MARK_AS_NOTSPAM)
237                 continue;
238
239             // skip if user didn't enable this option
240
if (item.getSpamItem().isMoveMessageWhenMarkingEnabled() == false)
241                 continue;
242
243             if (item.getSpamItem().isMoveTrashSelected() == false) {
244                 // move message to user-configured folder (generally "Junk"
245
// folder)
246
IMailFolder destFolder = FolderTreeModel.getInstance()
247                         .getFolder(item.getSpamItem().getMoveCustomFolder());
248
249                 // create reference
250
MailFolderCommandReference ref2 = new MailFolderCommandReference(
251                         srcFolder, destFolder, new Object JavaDoc[] { uids[j] });
252                 CommandProcessor.getInstance().addOp(new MoveMessageCommand(ref2));
253
254             } else {
255                 // move message to trash
256
IMailbox trash = (IMailbox) ((RootFolder) srcFolder
257                         .getRootFolder()).getTrashFolder();
258
259                 // create reference
260
MailFolderCommandReference ref2 = new MailFolderCommandReference(
261                         srcFolder, trash, new Object JavaDoc[] { uids[j] });
262
263                 CommandProcessor.getInstance().addOp(new MoveMessageCommand(ref2));
264
265             }
266
267         }
268     }
269 }
Popular Tags