📄 jwmafolderimpl.java
字号:
/*** * jwma Java WebMail * Copyright (c) 2000-2003 jwma team * * jwma is free software; you can distribute and use this source * under the terms of the BSD-style license received along with * the distribution. ***/package dtw.webmail.model;import java.io.*;import java.util.*;import javax.mail.*;import org.apache.log4j.Logger;//import dtw.webmail.JwmaKernel;/** * Class implementing the <tt>JwmaFolder</tt> model. * * It also implements the <tt>JwmaTrashInfo</tt> and the * <tt>JwmaInboxInfo</tt> models, because both are just * simplified interfaces to a folder. * * @author Dieter Wimberger * @version 0.9.7 07/02/2003 */public class JwmaFolderImpl implements JwmaFolder, JwmaTrashInfo, JwmaInboxInfo { //logging private static Logger log = Logger.getLogger(JwmaFolderImpl.class); //associations protected JwmaStoreImpl m_Store; protected Folder m_Folder; //instance attributes protected JwmaFolderList m_Subfolders; protected JwmaMessageInfoListImpl m_MessageInfoList; protected String m_Name; protected String m_Path; protected int m_Type; protected JwmaMessage m_ActualMessage; protected boolean m_OnlineCounting; protected FetchProfile m_DraftProfile; /** * Creates a <tt>JwmaFolderImpl</tt> instance. * * @param f the <tt>javax.mail.Folder</tt> instance to be wrapped. */ private JwmaFolderImpl(Folder f) { m_Folder = f; }//constructor /** * Creates a <tt>JwmaFolderImpl</tt> instance. * * @param f the <tt>javax.mail.Folder</tt> instance to be wrapped. */ private JwmaFolderImpl(Folder f, JwmaStoreImpl store) { m_Folder = f; m_Store = store; }//constructor /*** Basic info ************************************************************/ public String getName() { return m_Name; }//getName /** * Sets this folder's name. * * @param name the name of the folder as <tt>String</tt>. */ private void setName(String name) { m_Name = name; }//setName public String getPath() { return m_Path; }//getPath /** * Sets this folder's path. * * @param name the path of the folder as <tt>String</tt>. */ private void setPath(String path) { m_Path = path; }//setPath public int getType() { return m_Type; }//getType /** * Sets this folder's type. * * @param type this folder's type as <tt>int</tt>. */ private void setType(int type) { m_Type = type; }//setType public boolean isType(int type) { return (m_Type == type); }//isType public boolean isSubscribed() { return m_Folder.isSubscribed(); }//isSubscribed public void setSubscribed(boolean b) throws JwmaException { try { m_Folder.setSubscribed(b); } catch (Exception ex) { log.error("setSubscribed()",ex); throw new JwmaException("folder.subscription.failed"); } }//setSubscribed /** * Returns this folder's wrapped mail folder instance. * * @return wrapped instance as <tt>javax.mail.Folder</tt>. */ public Folder getFolder() { return m_Folder; }//getFolder /** * Tests if this folder returns an online count of the * contained messages. * Online count means that it will request the count * from the JavaMail folder implementation instance, instead of * returning the size of the cached list of messages. * * @return boolean true if online counting, false otherwise. */ public boolean isOnlineCounting() { return m_OnlineCounting; }//isOnlineCounting /** * Sets the online counting flag. * This will cause this folder to return the count * of messages as obtained from the JavaMail folder * implementation instance, instead of returning the size * of the cached list of messages. */ public void setOnlineCounting(boolean b) { m_OnlineCounting = b; }//setOnlineCounting /*** Subfolders related ****************************************************/ /** * Adds a folder to the cached list of subfolders, if * it is a subfolder of this folder. * * @param folder a <tt>JwmaFolderImpl</tt> instance. */ public void addIfSubfolder(JwmaFolderImpl folder) { if (isSubfolder(this.getPath(), folder.getPath())) { m_Subfolders.addFolderToList(folder); } }//addIfSubfolder /** * Removes a folder from the cached list of subfolders, if * it is a subfolder of this folder. * * @param path a <tt>String</tt> representing the path of a folder. */ public void removeIfSubfolder(String path) { if (isSubfolder(this.getPath(), path)) { m_Subfolders.removeFolderFromList(path); } }//removeIfSubfolder /** * Removes all folder from the cached list of subfolders, if * they are a subfolder of this folder. * This is a convenience method, it iterates over the array and * calls <tt>removeIfSubfolder(String)</tt> * * @param folders a <tt>String[]</tt> containing paths of folders. * * @see #removeIfSubfolder(String) */ public void removeIfSubfolder(String[] folders) { for (int i = 0; i < folders.length; i++) { removeIfSubfolder(folders[i]); } }//removeIfSubfolder /** * Tests if a folder is a subfolder of another folder, by using * their respective paths. * A folder is a subfolder if the path starts with the path of * the folder it is tested against. * * @param folder path of a folder as <tt>String</tt>. * @param possiblesubfolder path of a possible subfolder of the folder * as <tt>String</tt>. * @return boolean that is true, if possiblesubfolder is a subfolder * of folder, false otherwise. */ public boolean isSubfolder(String folder, String possiblesubfolder) { return possiblesubfolder.startsWith(folder); }//isSubfolder public JwmaFolder[] listSubfolders(int type) { //fetch list with type filter and create array from it return m_Subfolders.createFolderArray(m_Subfolders.sublist(type,JwmaStoreImpl.c_SubscribedOnly)); }//listFolders public JwmaFolder[] listSubfolders() { //fetch list with type filter and create array from it return m_Subfolders.createFolderArray(m_Subfolders.sublist(JwmaFolder.TYPE_ALL,JwmaStoreImpl.c_SubscribedOnly)); }//listFolders public JwmaFolder[] listSubfolders(int type, boolean subscribed) { String[] excludes = { m_Store.getTrashFolder().getFullName(), m_Store.getDraftFolder().getFullName(), ((m_Store.getReadMailFolder()!=null)? m_Store.getReadMailFolder().getFullName():""), ((m_Store.getSentMailFolder()!=null)? m_Store.getSentMailFolder().getFullName():"") }; return m_Subfolders.createFolderArray(m_Subfolders.sublist(type, excludes, subscribed)); }//listFolders public boolean hasSubfolders() { if (m_Subfolders != null && m_Subfolders.size() > 0) { return true; } else { return false; } }//hasFolders /*** End subfolder related *************************************************/ /*** Messages related ******************************************************/ public boolean hasMessages() { if (!isType(TYPE_FOLDER) && getMessageCount() > 0) { return true; } else { return false; } }//hasMessages public int getNewMessageCount() { try { return m_Folder.getNewMessageCount(); } catch (MessagingException ex) { return -1; } }//getNewMessageCount public boolean hasNewMessages() { return (getNewMessageCount() > 0); }//hasNewMessages public int getUnreadMessageCount() { try { return m_Folder.getUnreadMessageCount(); } catch (MessagingException ex) { return -1; } }//getUnreadMessageCount public boolean hasUnreadMessages() { return (getUnreadMessageCount() > 0); }//hasNewMessages public int getMessageCount() { if (isOnlineCounting()) { try { return m_Folder.getMessageCount(); } catch (MessagingException mex) { log.error(mex.getMessage(), mex); //JwmaKernel.getReference().debugLog().writeStackTrace(mex); return 0; } } else { if (m_MessageInfoList != null) { return m_MessageInfoList.size(); } else { return 0; } } }//getMessageCount public boolean isEmpty() { return (!hasMessages()); }//isEmpty /** * Returns the actual message. * The actual message is a reference to the message wrapper * instance of the message that was requested last. * * @return the <tt>JwmaMessage</tt> instance wrapping the last * requested message. */ public JwmaMessage getActualMessage() { return m_ActualMessage; }//getActualMessage /** * Returns the message with the given number from the * mailfolder. * * @return the message with the given number as <tt>javax.mail.Message</tt>. * * @throws <tt>JwmaException</tt> if a message with this number does not exist, * or the message cannot be retrieved from the store. */ private Message getMessage(int num) throws JwmaException { try { return m_Folder.getMessage(num); } catch (MessagingException mex) { throw new JwmaException("jwma.folder.getmessage.failed", true) .setException(mex); } }//getMessage /** * Returns the numbers of all read messages within * this instance. * * @return array of int's representing the message numbers of * all read messages. * @throws JwmaException if retrieving the numbers from the mailfolder fails. */ public int[] getReadMessages() throws JwmaException { int[] readmsg = new int[0]; try { m_Folder.open(Folder.READ_ONLY); Message[] messages = m_Folder.getMessages(); ArrayList readlist = new ArrayList(messages.length); for (int i = 0; i < messages.length; i++) { if (messages[i].isSet(Flags.Flag.SEEN)) { readlist.add( new Integer(messages[i].getMessageNumber()) ); } } readmsg = new int[readlist.size()]; int c = 0; for (Iterator iter = readlist.iterator(); iter.hasNext(); c++) { readmsg[c] = ((Integer) iter.next()).intValue(); } } catch (MessagingException ex) { //pass it on as JwmaException throw new JwmaException("jwma.folder.readmessages"); } finally { //ensure the inbox is closed try { m_Folder.close(false); } catch (MessagingException mex) { //closed anyway regarding to specs } } return readmsg; }//getReadMessages /** * Returns the number of the next message (relative to the actual) * in this folder, observing the actual sorting. * The method returns -1 if the actual message is the last message * in this folder. * * @return the number of the next message as <tt>int</tt> or -1 if * the actual message is the last message in this folder. * */ public int getNextMessageNumber() { return m_MessageInfoList.getNextMessageNumber( m_ActualMessage.getMessageNumber() ); }//getNextMessageNumber /** * Returns the number of the previous message (relative to the actual) * in this folder, observing the actual sorting. * The method returns -1 if the actual message is the first message * in this folder. * * @return the number of the previous message as <tt>int</tt> or -1 if * the actual message is the first message in this folder. * */ public int getPreviousMessageNumber() { return m_MessageInfoList.getPreviousMessageNumber( m_ActualMessage.getMessageNumber() ); }//getPreviousMessageNumber /** * Tests if a message with the given number exists in this folder. * * @return true if it exists, false otherwise. */ public boolean checkMessageExistence(int number) { return (number >= 1 && number <= getMessageCount()); }//checkMessageExistence /** * Returns a <tt>JwmaMessage</tt> instance that wraps * the mailmessage with the given number. * * @param num the number of the message to be retrieved as <tt>int</tt>. * @return the <tt>JwmaMessage</tt> instance wrapping the retrieved message. * @throws JwmaException if the message does not exist, or * cannot be retrieved from the store. */ public JwmaMessage getJwmaMessage(int num) throws JwmaException { try { //we want to change the seen flag if JwmaMessage created m_Folder.open(Folder.READ_WRITE); //get message and create wrapper Message msg = getMessage(num); JwmaMessage message = JwmaDisplayMessage.createJwmaDisplayMessage(msg, m_Store.getPreferences()); //set seen flag if not set already if (!msg.isSet(Flags.Flag.SEEN)) { msg.setFlag(Flags.Flag.SEEN, true); } //close without expunge m_Folder.close(false); //store the actual message m_ActualMessage = message; //rebuild the file list return message;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -