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;
018:
019: import java.util.Enumeration;
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.regex.Matcher;
023: import java.util.regex.Pattern;
024:
025: import org.columba.api.plugin.ExtensionMetadata;
026: import org.columba.api.plugin.IExtension;
027: import org.columba.api.plugin.IExtensionHandler;
028: import org.columba.api.plugin.PluginHandlerNotFoundException;
029: import org.columba.core.plugin.PluginManager;
030: import org.columba.mail.config.AccountItem;
031: import org.columba.mail.config.IFolderItem;
032: import org.columba.mail.config.MailConfig;
033: import org.columba.mail.folder.imap.IMAPFolder;
034: import org.columba.mail.folder.imap.IMAPRootFolder;
035: import org.columba.mail.gui.tree.FolderTreeModel;
036: import org.columba.mail.plugin.IExtensionHandlerKeys;
037:
038: /**
039: * Factory for creating subfolders. Implemented as a singelton. Use
040: * {@link #getInstance()}.
041: *
042: * @author Timo Stich <tstich@users.sourceforge.net>
043: */
044: public class FolderFactory {
045: // Groups are separated by at least one WS character
046: private static final Pattern groupPattern = Pattern
047: .compile("([^\\s]+)\\s*");
048:
049: private static FolderFactory instance;
050:
051: private IExtensionHandler handler;
052:
053: // parent directory for mail folders
054: // for example: ".columba/mail/"
055: private String path = MailConfig.getInstance().getConfigDirectory()
056: .getPath();
057:
058: protected FolderFactory() throws PluginHandlerNotFoundException {
059: // Get the handler
060: handler = PluginManager.getInstance().getExtensionHandler(
061: IExtensionHandlerKeys.ORG_COLUMBA_MAIL_FOLDER);
062:
063: }
064:
065: /**
066: * Singleton - pattern
067: *
068: * @return the instance of the factory
069: */
070: public static FolderFactory getInstance() {
071: if (instance == null) {
072: try {
073: instance = new FolderFactory();
074: } catch (PluginHandlerNotFoundException phnfe) {
075: throw new RuntimeException(phnfe);
076: }
077: }
078: return instance;
079: }
080:
081: /**
082: * Gets a list of all possible child foldertypes.
083: *
084: * @param parent
085: * @return a list that contains Strings of foldertypes
086: */
087: public List getPossibleChilds(IMailFolder parent) {
088: List list = new LinkedList();
089:
090: // which parents are possible ?
091: IFolderItem item = parent.getConfiguration();
092: String parentType = item.get("type");
093:
094: // the group of the given parent
095: String parentGroup = getGroup(parentType);
096:
097: // iterate through all foldertypes to find suitable ones
098: Enumeration e = handler.getExtensionEnumeration();
099: while (e.hasMoreElements()) {
100: IExtension extension = (IExtension) e.nextElement();
101: ExtensionMetadata metadata = (ExtensionMetadata) extension
102: .getMetadata();
103: String possibleParents = metadata
104: .getAttribute("possible_parents");
105: String id = metadata.getId();
106:
107: if (possibleParents != null) {
108: Matcher matcher = groupPattern.matcher(possibleParents);
109:
110: while (matcher.find()) {
111: if (matcher.group(1).equals(parentGroup)
112: || matcher.group(1).equals("all")) {
113: list.add(id);
114: }
115: }
116: }
117: }
118:
119: return list;
120: }
121:
122: /**
123: * Creates the default child for the given parent.
124: *
125: * @param parent
126: * the parent folder
127: * @return the childfolder
128: * @throws Exception
129: */
130: public IMailFolder createDefaultChild(IMailFolder parent,
131: String name) throws FolderCreationException {
132: List possibleChilds = getPossibleChilds(parent);
133:
134: if (possibleChilds.size() > 0) {
135: String childType = (String) possibleChilds.get(0);
136: return createChild(parent, name, childType);
137: } else {
138: return null;
139: }
140: }
141:
142: /**
143: * Creates a subfolder for the given folder with the given type.
144: *
145: * @param parent
146: * the parentfolder
147: * @param childType
148: * the type of the child (e.g. CachedMHFolder )
149: * @return the childfolder
150: * @throws Exception
151: */
152: public IMailFolder createChild(IMailFolder parent, String name,
153: String childType) throws FolderCreationException {
154: IMailFolder child;
155: try {
156: IExtension extension = handler.getExtension(childType);
157:
158: child = (IMailFolder) extension
159: .instanciateExtension(new Object[] { name,
160: childType, path });
161:
162: // Add child to parent
163: parent.addSubfolder(child);
164: } catch (Exception e) {
165: throw new FolderCreationException(e);
166: }
167: return child;
168: }
169:
170: public IMailFolder createVirtualFolder(IMailFolder parent,
171: String name) throws FolderCreationException {
172: IMailFolder child;
173: try {
174: IExtension extension = handler
175: .getExtension("VirtualFolder");
176:
177: child = (IMailFolder) extension
178: .instanciateExtension(new Object[] { name });
179:
180: // Add child to parent
181: parent.addSubfolder(child);
182: } catch (Exception e) {
183: throw new FolderCreationException(e);
184: }
185: return child;
186: }
187:
188: public IMAPFolder createIMAPRootFolder(AccountItem account)
189: throws FolderCreationException {
190:
191: try {
192: IExtension extension = handler
193: .getExtension("IMAPRootFolder");
194:
195: IMAPRootFolder child = (IMAPRootFolder) extension
196: .instanciateExtension(new Object[] { account, path });
197:
198: // get root folder
199: IMailFolder parent = (IMailFolder) FolderTreeModel
200: .getInstance().getRoot();
201:
202: // Add child to parent
203: parent.addSubfolder(child);
204:
205: // create mandatory IMAP Inbox folder
206: IExtension extension2 = handler.getExtension("IMAPFolder");
207: IMAPFolder inbox = (IMAPFolder) extension2
208: .instanciateExtension(new Object[] { "INBOX",
209: "IMAPFolder", path });
210:
211: // associate inbox with root folder
212: child.setInbox(inbox);
213:
214: // notify folder tree model
215: FolderTreeModel.getInstance().nodeStructureChanged(parent);
216:
217: return inbox;
218: } catch (Exception e) {
219: throw new FolderCreationException(e);
220: }
221: }
222:
223: public String getGroup(String parentType) {
224: // iterate through all foldertypes to find suitable ones
225: Enumeration e = handler.getExtensionEnumeration();
226: while (e.hasMoreElements()) {
227: IExtension extension = (IExtension) e.nextElement();
228: ExtensionMetadata metadata = (ExtensionMetadata) extension
229: .getMetadata();
230: String id = metadata.getId();
231: String group = metadata.getAttribute("group");
232:
233: if (id.equals(parentType))
234: return group;
235: }
236:
237: return null;
238:
239: }
240: }
|