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.util.logging.Logger;
019:
020: import org.columba.api.command.ICommandReference;
021: import org.columba.api.command.IWorkerStatusController;
022: import org.columba.api.gui.frame.IFrameMediator;
023: import org.columba.core.command.Command;
024: import org.columba.core.command.StatusObservableImpl;
025: import org.columba.core.command.Worker;
026: import org.columba.core.filter.FilterRule;
027: import org.columba.core.filter.IFilterRule;
028: import org.columba.mail.command.IMailFolderCommandReference;
029: import org.columba.mail.filter.MailFilterCriteria;
030: import org.columba.mail.folder.FolderFactory;
031: import org.columba.mail.folder.IMailbox;
032: import org.columba.mail.folder.virtual.VirtualFolder;
033: import org.columba.mail.gui.config.filter.ConfigFrame;
034: import org.columba.mail.gui.tree.FolderTreeModel;
035: import org.columba.ristretto.message.Header;
036:
037: /**
038: * This class is used to create a virtual folder based on the currently
039: * selected message (if multiple selected, the first one in the selection array
040: * is used) - either using Subject, To or From.
041: *
042: * @author Karl Peder Olesen (karlpeder), 20030621
043: */
044: public class CreateVFolderOnMessageCommand extends Command {
045:
046: /** JDK 1.4+ logging framework logger, used for logging. */
047: private static final Logger LOG = Logger
048: .getLogger("org.columba.mail.folder.command");
049:
050: /** Used for creating a virtual folder based on Subject */
051: public static final String VFOLDER_ON_SUBJECT = "Subject";
052:
053: /** Used for creating a virtual folder based on From */
054: public static final String VFOLDER_ON_FROM = "From";
055:
056: /** Used for creating a virtual folder based on To */
057: public static final String VFOLDER_ON_TO = "To";
058:
059: /** Type of virtual folder to create (Subject/From/To) */
060: private String vfolderType;
061:
062: /** Parent for the virtual folder */
063: private IMailbox parentFolder = null;
064:
065: /** Virtual folder created created */
066: private VirtualFolder vfolder = null;
067:
068: private IFrameMediator mediator;
069:
070: /**
071: * Constructor for CreateVFolderOnMessageCommand. Calls super constructor
072: * and saves flag for which kind of virtual folder to create. Default for
073: * filter type is FILTER_ON_SUBJECT.
074: *
075: * @param frameMediator
076: * @param references
077: * @param vfolderType
078: * Which type of filter to create. Used defined constants
079: */
080: public CreateVFolderOnMessageCommand(
081: IFrameMediator frameController,
082: ICommandReference reference, String vfolderType) {
083: super (reference);
084: this .vfolderType = vfolderType;
085: this .mediator = frameController;
086: }
087:
088: /**
089: * Displays search dialog for user modifications after creation of the
090: * virtual folder in execute. Also refreshes the tree view.
091: *
092: * @see org.columba.api.command.Command#updateGUI()
093: */
094: public void updateGUI() throws Exception {
095: FolderTreeModel.getInstance()
096: .nodeStructureChanged(parentFolder);
097:
098: if (vfolder != null) {
099: //vfolder.showFilterDialog((AbstractMailFrameController) getFrameMediator());
100: new ConfigFrame(mediator, vfolder);
101: }
102: }
103:
104: /**
105: * This method generates a virtual folder based on Subject, From or To
106: * (depending on parameter transferred to constructor) of the currently
107: * selected message.
108: *
109: * @param worker
110: * @see org.columba.api.command.Command#execute(Worker)
111: */
112: public void execute(IWorkerStatusController worker)
113: throws Exception {
114: // get references to selected folder and message
115: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
116: Object[] uids = r.getUids(); // uid for messages to save
117:
118: if (uids.length == 0) {
119: LOG
120: .fine("No virtual folder created since no message was selected");
121:
122: return; // no message selected.
123: }
124:
125: Object uid = uids[0];
126: parentFolder = (IMailbox) r.getSourceFolder();
127:
128: //register for status events
129: ((StatusObservableImpl) parentFolder.getObservable())
130: .setWorker(worker);
131:
132: // get value of Subject, From or To header
133: Header header = parentFolder.getHeaderFields(uid, new String[] {
134: "Subject", "From", "To" });
135: String headerValue = (String) header.get(vfolderType);
136:
137: if (headerValue == null) {
138: LOG.warning("Error getting " + vfolderType
139: + " header. No virtual folder created");
140:
141: return;
142: }
143:
144: // create virtual folder (is attached to parentFolder)
145: String name = vfolderType + " contains [" + headerValue + "]";
146: vfolder = createVirtualFolder(name, vfolderType, headerValue,
147: parentFolder);
148: }
149:
150: /**
151: * Private utility for creating a virtual folder on a given headerfield.
152: * The criteria used is "contains".
153: *
154: * @param folderName
155: * Name of virtual folder
156: * @param headerField
157: * The header field to base virtual folder on
158: * @param pattern
159: * The pattern to use in the virtual folder
160: * @param parent
161: * Parent folder
162: * @return The filter created
163: */
164: public VirtualFolder createVirtualFolder(String folderName,
165: String headerField, String pattern, IMailbox parent) {
166: // create virtual folder
167: VirtualFolder vfolder;
168:
169: try {
170: vfolder = (VirtualFolder) FolderFactory.getInstance()
171: .createChild(parent, folderName, "VirtualFolder");
172: } catch (Exception e) {
173: LOG.warning("Error creating new virtual folder: "
174: + e.getMessage());
175:
176: return null;
177: }
178:
179: // set properties for virtual folder
180: String parentUid = "101"; // default is inbox if parent is null
181:
182: if (parent != null) {
183: parentUid = parent.getId();
184: }
185:
186: vfolder.getConfiguration().setString("property", "source_uid",
187: parentUid);
188: vfolder.getConfiguration().setBoolean("property",
189: "include_subfolders", false);
190:
191: // define filter rule
192: IFilterRule rule = vfolder.getFilter().getFilterRule();
193: rule.setCondition("matchall");
194: rule.removeAll();
195: rule.addEmptyCriteria();
196:
197: // define criteria
198: MailFilterCriteria c = new MailFilterCriteria(rule.get(0));
199: c.setCriteriaString("contains");
200: c.setHeaderfieldString(headerField);
201: c.setTypeString(headerField);
202: c.setPatternString(pattern);
203:
204: return vfolder;
205: }
206: }
|