1 17 package org.columba.mail.folder; 18 19 import java.util.Enumeration ; 20 import java.util.LinkedList ; 21 import java.util.List ; 22 import java.util.regex.Matcher ; 23 import java.util.regex.Pattern ; 24 25 import org.columba.api.plugin.ExtensionMetadata; 26 import org.columba.api.plugin.IExtension; 27 import org.columba.api.plugin.IExtensionHandler; 28 import org.columba.api.plugin.PluginHandlerNotFoundException; 29 import org.columba.core.plugin.PluginManager; 30 import org.columba.mail.config.AccountItem; 31 import org.columba.mail.config.IFolderItem; 32 import org.columba.mail.config.MailConfig; 33 import org.columba.mail.folder.imap.IMAPFolder; 34 import org.columba.mail.folder.imap.IMAPRootFolder; 35 import org.columba.mail.gui.tree.FolderTreeModel; 36 import org.columba.mail.plugin.IExtensionHandlerKeys; 37 38 44 public class FolderFactory { 45 private static final Pattern groupPattern = Pattern 47 .compile("([^\\s]+)\\s*"); 48 49 private static FolderFactory instance; 50 51 private IExtensionHandler handler; 52 53 private String path = MailConfig.getInstance().getConfigDirectory() 56 .getPath(); 57 58 protected FolderFactory() throws PluginHandlerNotFoundException { 59 handler = PluginManager.getInstance().getExtensionHandler( 61 IExtensionHandlerKeys.ORG_COLUMBA_MAIL_FOLDER); 62 63 } 64 65 70 public static FolderFactory getInstance() { 71 if (instance == null) { 72 try { 73 instance = new FolderFactory(); 74 } catch (PluginHandlerNotFoundException phnfe) { 75 throw new RuntimeException (phnfe); 76 } 77 } 78 return instance; 79 } 80 81 87 public List getPossibleChilds(IMailFolder parent) { 88 List list = new LinkedList (); 89 90 IFolderItem item = parent.getConfiguration(); 92 String parentType = item.get("type"); 93 94 String parentGroup = getGroup(parentType); 96 97 Enumeration e = handler.getExtensionEnumeration(); 99 while (e.hasMoreElements()) { 100 IExtension extension = (IExtension) e.nextElement(); 101 ExtensionMetadata metadata = (ExtensionMetadata) extension.getMetadata(); 102 String possibleParents = metadata.getAttribute("possible_parents"); 103 String id = metadata.getId(); 104 105 if (possibleParents != null) { 106 Matcher matcher = groupPattern.matcher(possibleParents); 107 108 while (matcher.find()) { 109 if (matcher.group(1).equals(parentGroup) 110 || matcher.group(1).equals("all")) { 111 list.add(id); 112 } 113 } 114 } 115 } 116 117 return list; 118 } 119 120 128 public IMailFolder createDefaultChild(IMailFolder parent, String name) 129 throws FolderCreationException { 130 List possibleChilds = getPossibleChilds(parent); 131 132 if (possibleChilds.size() > 0) { 133 String childType = (String ) possibleChilds.get(0); 134 return createChild(parent, name, childType); 135 } else { 136 return null; 137 } 138 } 139 140 150 public IMailFolder createChild(IMailFolder parent, String name, 151 String childType) throws FolderCreationException { 152 IMailFolder child; 153 try { 154 IExtension extension = handler.getExtension(childType); 155 156 child = (IMailFolder) extension.instanciateExtension(new Object [] { 157 name, childType, path }); 158 159 parent.addSubfolder(child); 161 } catch (Exception e) { 162 throw new FolderCreationException(e); 163 } 164 return child; 165 } 166 167 public IMailFolder createVirtualFolder(IMailFolder parent, String name) throws FolderCreationException { 168 IMailFolder child; 169 try { 170 IExtension extension = handler.getExtension("VirtualFolder"); 171 172 child = (IMailFolder) extension.instanciateExtension(new Object [] {name}); 173 174 parent.addSubfolder(child); 176 } catch (Exception e) { 177 throw new FolderCreationException(e); 178 } 179 return child; 180 } 181 182 public IMAPFolder createIMAPRootFolder(AccountItem account) throws FolderCreationException { 183 184 try { 185 IExtension extension = handler.getExtension("IMAPRootFolder"); 186 187 IMAPRootFolder child = (IMAPRootFolder) extension.instanciateExtension(new Object [] {account, path}); 188 189 IMailFolder parent = (IMailFolder) FolderTreeModel.getInstance().getRoot(); 191 192 parent.addSubfolder(child); 194 195 IExtension extension2 = handler.getExtension("IMAPFolder"); 197 IMAPFolder inbox = (IMAPFolder) extension2.instanciateExtension(new Object [] { 198 "INBOX", "IMAPFolder", path }); 199 200 child.setInbox(inbox); 202 203 FolderTreeModel.getInstance().nodeStructureChanged(parent); 205 206 return inbox; 207 } catch (Exception e) { 208 throw new FolderCreationException(e); 209 } 210 } 211 212 public String getGroup(String parentType) { 213 Enumeration e = handler.getExtensionEnumeration(); 215 while (e.hasMoreElements()) { 216 IExtension extension = (IExtension) e.nextElement(); 217 ExtensionMetadata metadata = (ExtensionMetadata) extension.getMetadata(); 218 String id = metadata.getId(); 219 String group = metadata.getAttribute("group"); 220 221 if (id.equals(parentType)) 222 return group; 223 } 224 225 return null; 226 227 } 228 } 229 | Popular Tags |