📄 jdavmailfolder.java
字号:
/*
* This file is part of the JDAVMail package
* Copyright (C) 2002 Luc Claes. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
* SHALL THE JDAVMail AUTHORS OR THE PROJECT CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* luc@posisoft.com
*
* The JDAVMail's home page is located at:
*
* http://jdavmail.sourceforge.net
*
*/
package com.posisoft.jdavmail;import java.util.Iterator;
import java.util.List;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.FolderNotFoundException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.URLName;
import javax.mail.event.ConnectionEvent;
import javax.mail.event.FolderEvent;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/** * An JDAVMail Folder: JavaMail Folder subclass. * Most of the actual work is delegated to a FolderProxy instance.
* * @author Luc Claes */public class JDAVMailFolder extends Folder { static private final Log m_log = LogFactory.getLog("com.posisoft.JDAVMail.JDAVMailFolder");
private String m_strName = null; private FolderProxy m_folderProxy = null; private boolean m_bOpen = false;
/**
*/ JDAVMailFolder(JDAVMailStore store, String strName) { super(store); m_strName = strName;
m_folderProxy = store._getService().getFolderProxy(strName); }
/**
*/ JDAVMailFolder(JDAVMailStore store, URLName url) throws URIException { super(store); m_strName = url.getFile();
m_folderProxy = store._getService().getFolderProxy(url); }
/**
*/ JDAVMailFolder(JDAVMailStore store, FolderProxy fp) { super(store); m_strName = fp.getName();
m_folderProxy = fp; }
/**
*/ public String getName() { return m_strName; }
/**
*/ public String getFullName() { return m_strName; }
/**
* Returns the parent folder of this folder.
* This method can be invoked on a closed Folder.
* If this folder is the top of a folder hierarchy, this method returns null
*/ public Folder getParent() { //TODO: check in the spec if the folder must exist.
FolderProxy fp = (m_folderProxy != null) ? m_folderProxy.getParent() : null;
if (fp != null) { return new JDAVMailFolder((JDAVMailStore)getStore(), fp);
} return null; } /** */ public boolean exists() { return m_folderProxy != null; } /** */ public Folder[] list(String strPattern) throws MessagingException {
checkExists();
Folder[] arrayFolders = null; List listFolders = m_folderProxy.list(strPattern); if (listFolders != null) {
arrayFolders = new Folder[listFolders.size()]; Iterator iterFolders = listFolders.iterator();
for (int i = 0; iterFolders.hasNext(); i++) {
arrayFolders[i] = new JDAVMailFolder((JDAVMailStore)getStore(), (FolderProxy) iterFolders.next()); } } else {
arrayFolders = new Folder[0]; }
return arrayFolders; } /** */ public char getSeparator() { return '\0';
} /** */ public int getType() throws FolderNotFoundException {
checkExists(); return m_folderProxy.getType(); } /** */ public boolean create(int nType) throws MessagingException {
if (exists()) {
throw new MessagingException("Creation: folder already exists"); }
if (nType != HOLDS_MESSAGES) {
throw new MessagingException("Folder creation error: only messages containers are supported"); } try {
m_folderProxy = FolderProxy.createInstance(this); } catch (Exception e) { throw new MessagingException("Folder creation error:", e); }
m_folderProxy.setType(nType); notifyFolderListeners(FolderEvent.CREATED);
return true; } /** */ public boolean hasNewMessages() throws MessagingException {
checkExists(); return m_folderProxy.hasNewMessages(); } /** */ public Folder getFolder(String strName) throws MessagingException {
FolderProxy fp = (m_folderProxy != null) ? m_folderProxy.getChild(strName) : null;
return (fp != null) ? new JDAVMailFolder((JDAVMailStore)getStore(), fp) : new JDAVMailFolder((JDAVMailStore)getStore(), strName);
} /** */ public boolean delete(boolean bRecurse) throws MessagingException { checkExists();
checkClosed();
try {
if (m_folderProxy.delete(bRecurse)) {
notifyFolderListeners(FolderEvent.DELETED);
return true;
}
} catch (URIException e) {
throw new MessagingException("", e);
}
return false;
} /** */ public boolean renameTo(Folder f) throws MessagingException { checkExists();
try { if (m_folderProxy.renameTo(f.getName())) {
notifyFolderListeners(FolderEvent.RENAMED);
}
} catch (URIException e) {
throw new MessagingException("", e);
}
return false; } /** */ public synchronized void open(int nMode) throws MessagingException {
if (m_log.isDebugEnabled()) { m_log.debug("Open JDAVMailFolder " + m_strName + " with mode " + nMode); }
checkExists(); checkClosed();
try { m_folderProxy.open(this, nMode); m_bOpen = true; super.mode = nMode;
} catch (Exception e) { throw new MessagingException("Folder " + m_strName + " open exception", e);
} notifyConnectionListeners(ConnectionEvent.OPENED); }
/**
*/ public synchronized void close(boolean bExpunge) throws MessagingException { checkExists();
checkOpen();
m_folderProxy.close(bExpunge);
m_bOpen = false; }
/**
*/ public boolean isOpen() { return m_bOpen; } /** */ public Flags getPermanentFlags() { return new Flags(); // empty flags object } /** */ public int getMessageCount() throws MessagingException {
checkExists(); return m_folderProxy.getMessagesCount(); }
/**
*/
public int getNewMessageCount() throws MessagingException {
checkExists();
return m_folderProxy.getNewMessagesCount();
}
/**
*/
public int getUnreadMessageCount() throws MessagingException {
checkExists();
return m_folderProxy.getUnreadMessagesCount();
}
/**
*/ public Message getMessage(int nMessageIndex) throws MessagingException { checkExists();
checkOpen();
return m_folderProxy.getMessage(nMessageIndex); } /** */ public void appendMessages(Message[] msgs) throws MessagingException {
checkExists();
// checkReadWrite();
try {
m_folderProxy.open(this, Folder.READ_WRITE);
} catch (Exception e) {
throw new MessagingException("Folder " + m_strName + " open exception", e);
}
m_folderProxy.appendMessages(msgs);
m_folderProxy.close(false);
notifyMessageAddedListeners(msgs); } /** */ public Message[] expunge() throws MessagingException { checkExists();
checkOpen(); checkReadWrite();
Message[] msgs = m_folderProxy.expunge();
notifyMessageRemovedListeners(true, msgs);
return msgs; } /** * Close the folder when we're finalized. */ protected void finalize() throws Throwable {
if (isOpen()) { close(false);
} } /**
* Ensure the folder 'exists'
*/ protected void checkExists() throws FolderNotFoundException { if (!exists()) {
throw new FolderNotFoundException(this); } } /**
* Ensure the folder is open
*/ protected void checkOpen() throws IllegalStateException { if (!isOpen()) { throw new IllegalStateException("Folder is not Open");
} } /**
* Ensure the folder is not open
*/ protected void checkClosed() throws IllegalStateException { if (isOpen()) { throw new IllegalStateException("Folder is Open");
} }
/**
* Ensure the folder writable
*/ protected void checkReadWrite() throws IllegalStateException { if (super.mode != READ_WRITE) { throw new IllegalStateException("Folder is not writable");
} }
/* * Only here to make accessible to JDAVMailMessage. */ protected void notifyMessageChangedListeners(int type, Message m) { super.notifyMessageChangedListeners(type, m); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -