?? maildirfolder.java
字號:
/*
* MaildirFolder.java
* Copyright (C) 1999 fengyun <fengyun@gbsource.net>
*/
package fengyun.Fastmail.Maildir;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
/**
* Maildir形式的郵件夾
* @author fengyun
* @version 1.01
*/
public class MaildirFolder extends Folder {
protected MaildirFolderSummary summary = null; //郵夾摘要
protected Vector msgsummarys = new Vector(); //消息摘要集
protected Vector messages = new Vector(); //消息集合
/**
* 構造MaildirFolder
* @param store 郵件的存儲對象
* @param dirname 郵件夾名
*/
protected MaildirFolder(Store store,String name) throws MessagingException{
super(store);
String fulldir = ((MaildirStore)store).getMaildir() + File.separator + name;
summary = new MaildirFolderSummary(new File(fulldir));
}
protected MaildirFolder(Store store,MaildirFolderSummary summary) throws MessagingException{
super(store);
this.summary = summary;
}
/**
* 返回摘要
* @return MaildirFolderSummary 郵件夾摘要
*/
public MaildirFolderSummary getSummary() {
return summary;
}
/**
* 返回郵件夾類型
* @return int 郵件夾類型
*/
public int getType() {
return summary.getType();
}
/**
* 設置郵件夾類型
* @param type 郵件夾類型
* @return void 空
*/
public void setType(int type) {
summary.setType(type);
}
/**
* 返回郵件夾名
* @return String 郵件夾名稱
*/
public String getName() {
return summary.getName();
}
/**
* 返回郵件夾大小
* @return long 郵件夾大小
*/
public long getSize() {
return summary.getSize();
}
/**
* 返回郵件夾全名
* @return String 郵件夾全名
*/
public String getFullName() {
return summary.getFullName();
}
/**
* 郵件夾擁有消息總數
* @return int 消息總數
*/
public int getMessageCount() {
return summary.getMessageCount();
}
/**
* 返回新消息數
* @return int 新消息總數
*/
public int getNewMessageCount() {
return summary.getNewMessageCount();
}
/**
* 郵件夾是否擁有新消息
* @return boolean 是否有新消息
*/
public boolean hasNewMessages() {
return summary.getNewMessageCount() > 0;
}
/**
* 將消息數組插入文件夾
* @param m 消息數組
*/
public synchronized void appendMessages(Message [] m) throws MessagingException{
if (m == null || m.length == 0) throw new MessagingException("Error Message List");
if (!isOpen()) throw new MessagingException("Folder is not Opened");
int j=0;
for (int i=0; i<m.length;i++) {
for (j=0; j< summary.getMessageCount() && ((MaildirMessageSummary)msgsummarys.get(j)).getReceiveTime()>((MaildirMessage)m[i]).getReceiveTime();j++); //查找合適位置
MaildirMessageSummary mms = ((MaildirMessage)m[i]).summary;
msgsummarys.add(j,mms);
if (j < messages.size()) { //直接插入
messages.add(j,m[i]);
}
summary.incMessageCount();
if (mms.isNewMessage()) summary.incNewMessageCount();
//Debug
if (((MaildirStore)store).getSession().getDebug())
System.err.println("DEBUG: appendMessage:" + mms.getMessageID());
}
}
/**
* 插入一個消息
* @param m 消息
*/
public void appendMessage(Message m) throws MessagingException {
if (m == null || !(m instanceof MaildirMessage)) throw new MessagingException("Error Message");
Message[] msglist = new Message[1];
msglist[0] = m;
appendMessages(msglist);
}
/**
* 郵夾是否存在
* @return boolean 是否存在
*/
public boolean exists() throws MessagingException {
return summary.exists();
}
/**
* 打開郵件夾
* @param Type 打開類型
*/
public void open(int Type) throws MessagingException {
if (((MaildirStore)store).getSession().getDebug())
System.err.println("DEBUG: Maildir: opening " + getFullName());
if (!exists())
throw new MessagingException("The Folder Format Error or Folder is not exists.");
summary.Open();
//對消息列表,取得消息摘要
listMessageSummarys();
//列郵件夾子目錄
// if (Type == MaildirFolderSummary.HOLDS_FOLDERS) listFolderSummarys();
notifyConnectionListeners(ConnectionEvent.OPENED);
//Debug
if (((MaildirStore)store).getSession().getDebug())
System.err.println("DEBUG: open:" + this.getName());
}
/**
* 對Folder列表
* 返回下一級子郵件夾
* @return Folder[] 子郵件夾列表
*/
public Folder[] list() throws MessagingException {
if (summary.getType() != MaildirFolderSummary.HOLDS_FOLDERS)
throw new MessagingException("The Folder can't hold Sub Folder");
String [] list = summary.list(new MaildirFilenameFilter('/')); //所有目錄文件
if (list != null && list.length >0) {
Folder[] folders = new Folder[list.length];
for(int i = 0;i<list.length;i++) {
folders[i] = store.getFolder(list[i]);
}
return folders;
}
return null;
}
/**
* 根據要求列郵件夾
* 返回下一級郵件夾
* @param pattern 無效參數
* @return Folder[] 與 list() 的返回一樣
*/
public Folder[] list(String pattern) throws MessagingException {
return list();
}
/**
* 返回子郵件夾
* @param foldername 子郵件夾名稱
* @return Folder 對應的郵件夾
*/
public Folder getFolder(String foldername) throws MessagingException {
String fullname = getName() + getSeparator() + foldername;
return store.getFolder(fullname);
}
/**
* 對Folder列表
* 得到所有消息摘要
*/
public void listMessageSummarys() throws MessagingException{
if (isOpen()) {
String[] list = summary.list(new MaildirFilenameFilter('*'));
if (list == null) return;
summary.setMessageCount(list.length);
summary.setNewMessageCount(0);
Vector mess = new Vector();
for(int i=0;i< getMessageCount(); i++) {
try {
int cur = list[i].indexOf(".");
long ReceiveTime = Long.valueOf(list[i].substring(0,cur)).longValue() ;
char Flag = list[i].charAt(cur+1);
File file = new File(getFullName() + getSeparator() + list[i],"r");
long Size = file.length();
MaildirMessageSummary mms = new MaildirMessageSummary(list[i],ReceiveTime,Flag,Size);
mess.add(mms);
}
catch(Exception e) {
System.out.println("Can't get MessageSummary" + list[i] + ':' +e.getMessage());
}
}
msgsummarys.removeAllElements();
//排序
while(!mess.isEmpty()) {
int index=0;
for(int i=0 ; i < mess.size(); i++) {
MaildirMessageSummary mms = (MaildirMessageSummary)mess.get(index);
if (mms.getReceiveTime() < ((MaildirMessageSummary)mess.get(i)).getReceiveTime()) index = i;
}
MaildirMessageSummary mms = (MaildirMessageSummary)mess.get(index);
if (mms.isNewMessage()) summary.incNewMessageCount();
msgsummarys.addElement(mms);
mess.remove(index);
}
mess = null;
}
}
/**
* 關閉文件夾
* @param b 是否清空(此Folder不需要)
*/
public void close(boolean b) throws MessagingException {
if (isOpen()) {
summary.Close();
//回收內存
msgsummarys.removeAllElements();
msgsummarys = null;
notifyConnectionListeners(ConnectionEvent.CLOSED);
}
if (((MaildirStore)store).getSession().getDebug())
System.err.println("DEBUG: Maildir: closing " + getFullName());
}
/**
* 是否存在消息摘要
* @param msgname 消息名稱
* @return int 消息摘要所在位置
*/
public int hasMessageSummary(String msgname) {
for(int i = 0 ;i <msgsummarys.size(); i++ ) {
if (msgname.equals(((MaildirMessageSummary)msgsummarys.get(i)).getMessageID())) return i;
}
return -1;
}
/**
* 是否存在消息
* @param msgname 消息名稱
* @return 消息所在位置
*/
public int hasMessage(String msgname) {
for(int i = 0 ;i < messages.size(); i++ ) {
if (msgname.equals(((MaildirMessage)messages.get(i)).summary.getMessageID())) return i;
}
return -1;
}
/**
* 刪除消息
* @param msgnum 消息號
*/
public void deleteMessages(String msgname) throws MessagingException {
if (msgname == null) throw new MessagingException("Error Message Name");
String[] msglist = { msgname };
deleteMessages(msglist);
}
/**
* 刪除消息號數組內的消息
* @param msgnum 消息號數組
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -