📄 jwmafolderimpl.java
字号:
} catch (MessagingException mex) { throw new JwmaException("jwma.folder.jwmamessage", true).setException(mex); } finally { try { //ensure closed the folder if (m_Folder.isOpen()) { m_Folder.close(false); } } catch (MessagingException mesx) { //don't care, the specs say it IS closed anyway } } }//getJwmaMessage public JwmaMessage getDraftMessage(int num) throws JwmaException { //get message and create wrapper try { m_Folder.open(Folder.READ_WRITE);//to ensure message can be changed Message[] msg = {getMessage(num)}; if (m_DraftProfile == null) { m_DraftProfile = new FetchProfile(); m_DraftProfile.add(FetchProfile.Item.ENVELOPE); //contains the headers m_DraftProfile.add(FetchProfile.Item.FLAGS); //contains the flags; m_DraftProfile.add(FetchProfile.Item.CONTENT_INFO); //contains the content info } m_Folder.fetch(msg, m_DraftProfile); return JwmaComposeMessage.createDraft(msg[0]); } catch (MessagingException ex) { throw new JwmaException("jwma.folder.draftmessage", true).setException(ex); } finally { try { //ensure closed the folder if (m_Folder.isOpen()) { m_Folder.close(false); } } catch (MessagingException mesx) { //don't care, the specs say it IS closed anyway } } }//getDraftMessage /** * Returns an array of <tt>JwmaMessageInfo[]</tt> listing * the info's of all messages in this folder. * The method actually delegates the job to the associated * <tt>JwmaMessageInfoListImpl</tt> instance. * * @return an array of <tt>JwmaMessageInfo</tt> instances. * * @see dtw.webmail.model.JwmaMessageInfoListImpl#listMessageInfos() */ public JwmaMessageInfo[] listMessageInfos() { return m_MessageInfoList.listMessageInfos(); }//getMessageInfoList /** * Returns the <tt>JwmaMessageInfoListImpl</tt> instance that * contains a list of stored <tt>JwmaMessageInfoImpl</tt> references * wrapping information about the messages in the wrapped mailfolder. * * @return the list containing the <tt>JwmaMessageInfoImpl</tt> references. */ public JwmaMessageInfoListImpl getMessageInfoList() { return m_MessageInfoList; }//getMessageInfoList /** * Deletes the actual message from this folder. * * @return the number of the next message or -1 if there is none. * * @throws JwmaException if it fails to delete the message from the store. */ public int deleteActualMessage() throws JwmaException { //remember the next message (or if ther is none) int nextmsgnum = getNextMessageNumber() - 1; //delete message deleteMessage(m_ActualMessage.getMessageNumber()); return nextmsgnum; }//deleteActualMessage /** * Deletes the given message from this folder. * Note that this is a convenience method that creates an array with a * single entry, and calls <tt>deleteMessage(int[])</tt>. * * @param number the number of the message to be deleted as <tt>int</tt>. * * @throws JwmaException if it fails to delete the given message. * * @see #deleteMessages(int[]) */ public void deleteMessage(int number) throws JwmaException { int[] nums = {number}; deleteMessages(nums); }//deleteMessage /** * Deletes all messages from this folder. * Note that this is a convenience method that creates an array with a * all message numbers, and calls <tt>deleteMessage(int[])</tt>. * * * @throws JwmaException if it fails to delete any of the messages. * * @see #deleteMessages(int[]) */ public void deleteAllMessages() throws JwmaException { int[] msgnumbers = new int[getMessageCount()]; for (int i = 0; i < getMessageCount(); i++) { msgnumbers[i] = i + 1; } deleteMessages(msgnumbers); }//deleteAllMessages /** * Deletes the messages with the given numbers. * * @param numbers the messages to be deleted as <tt>int[]</tt>. * * @throws JwmaException if it fails to delete any of the given messages. */ public void deleteMessages(int[] numbers) throws JwmaException { //dont work with null or empty arrays if (numbers == null || numbers.length == 0) { return; } try { m_Folder.open(Folder.READ_WRITE); Message[] msgs = m_Folder.getMessages(numbers); if (msgs.length != 0) { //if not the trash copy the messages to the trash if (!m_Path.equals(m_Store.getTrashFolder().getFullName())) { m_Folder.copyMessages(msgs, m_Store.getTrashFolder()); } //flag deleted, so when closing with expunge //the messages are erased. m_Folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); } //close with expunge m_Folder.close(true); //update messagelist: //deleted and already flagged and now expunged messages m_MessageInfoList.remove(numbers); m_MessageInfoList.removeDeleted(); //renumber to reflect standard behaviour m_MessageInfoList.renumber(); } catch (MessagingException mex) { throw new JwmaException("jwma.folder.deletemessage.failed", true) .setException(mex); } finally { try { //ensure closed the folder if (m_Folder.isOpen()) { m_Folder.close(false); } } catch (MessagingException mesx) { //don't care, the specs say it IS closed anyway } } }//deleteMessages /** * Moves the actual message to the given destination folder. * * @param destfolder the path of the destination folder as <tt>String</tt>. * @return the number of the next message, or -1 if there is none. * * @throws JwmaException if it fails to move the message. * * @see #moveMessage(int,String) */ public int moveActualMessage(String destfolder) throws JwmaException { //remember if next messagenum (or if there is one at all) int nextmsgnum = getNextMessageNumber() - 1; //delete message moveMessage(m_ActualMessage.getMessageNumber(), destfolder); return nextmsgnum; }//moveActualMessage /** * Moves the given message to the given destination folder. * Note that this is actually a convenience method. It wraps * the message number into an array and calls * <tt>moveMessages(int[],String)</tt>. * * @param destfolder the path of the destination folder as <tt>String</tt>. * * @return the number of the next message, or -1 if there is none. * * @throws JwmaException if it fails to move the message. * * @see #moveMessages(int[],String) */ public void moveMessage(int number, String destfolder) throws JwmaException { int[] nums = {number}; moveMessages(nums, destfolder); }//moveMessage /** * Moves the messages with the given numbers to the given destination folder. * * @param numbers the messages to be moved as <tt>int[]</tt>. * @param destfolder the destination folder path as <tt>String</tt>. * * @throws JwmaException if it fails to move if the destination folder does not * exist, or if any of the given messages cannot be moved. */ public void moveMessages(int[] numbers, String destfolder) throws JwmaException { //dont work with null or empty arrays if (numbers == null || numbers.length == 0 || destfolder == null || destfolder.length() == 0) { return; } //JwmaKernel.getReference().debugLog().write("Moving msgs to->"+destfolder+"<-"); try { if (!m_Store.checkFolderExistence(destfolder)) { throw new JwmaException( "jwma.folder.movemessage.destination.missing", true ); } Folder dest = m_Store.getFolder(destfolder); //check destination type if (dest.getType() == JwmaFolder.TYPE_FOLDER) { throw new JwmaException("jwma.folder.movemessage.destination.foul", true); } //Note that apidocs state that only source //has to be opened m_Folder.open(Folder.READ_WRITE); //prepare messages Message[] msgs = m_Folder.getMessages(numbers); if (msgs.length != 0) { m_Folder.copyMessages(msgs, dest); m_Folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); } m_Folder.close(true); //update messagelist: //deleted and already flagged and now expunged messages m_MessageInfoList.remove(numbers); m_MessageInfoList.removeDeleted(); m_MessageInfoList.renumber(); } catch (MessagingException mex) { throw new JwmaException( "jwma.folder.movemessage.failed", true ).setException(mex); } finally { try { //ensure closed the folder if (m_Folder.isOpen()) { m_Folder.close(false); } } catch (MessagingException mesx) { //don't care, the specs say it IS closed anyway } } }//moveMessages /*** End Messages related ******************************************************/ /*** MessageParts related ******************************************************/ /** * Writes the given message part from the given message to the given * output stream. * * @param part the part to be written to the output stream. * @param out the <tt>OutputStream</tt> to be written to. * * @throws IOException if an I/O related error occurs. * @throws JwmaException if the message part does not exist, or * cannot be retrieved from the message. */ public void writeMessagePart(Part part, OutputStream out) throws IOException, JwmaException { try { m_Folder.open(Folder.READ_ONLY); InputStream in = part.getInputStream(); int i; while ((i = in.read()) != -1) { //this is not very efficient, should //write in blocks (byte[]'s) out.write(i); } out.flush(); } catch (MessagingException mex) { throw new JwmaException("message.displaypart.failed") .setException(mex); } finally { try { if (m_Folder.isOpen()) { m_Folder.close(false); } } catch (MessagingException ex) { log.error(ex.getMessage(), ex); //JwmaKernel.getReference().debugLog().writeStackTrace(ex); } } }//writeMessagePart /*** End MessageParts related ******************************************************/ /** * Tests if this folder instance equals a given object. * Overrides the superclass behaviour to compare the folder's * paths in case the given object is a JwmaFolderImpl. * * @param o the object to compare this folder with. * * @return true if the paths are equal, false otherwise, or when the * object is not an instance of <tt>JwmaFolderImpl</tt>. */ public boolean equals(Object o) { if (o instanceof JwmaFolderImpl) { return this.getPath().equals(((JwmaFolderImpl) o).getPath()); } else { return false; } }//equals /** * Prepares this folder instance. * This method fills in values from the wrapped mailfolder instance * and creates the cached subfolder list and the cached messages list. * * @throws JwmaException if it fails to retrieve values from the wrapped instance, or * if it fails to create the subfolder list. */ public void prepare() throws JwmaException { try { //set basic data m_Name = m_Folder.getName(); m_Path = m_Folder.getFullName(); m_Type = m_Folder.getType(); //create folder list m_Subfolders = JwmaFolderList.createSubfolderList(m_Folder); if (isType(JwmaFolder.TYPE_MAILBOX) || isType(JwmaFolder.TYPE_MIXED)) { //create m_MessageInfoList, or set null m_MessageInfoList = JwmaMessageInfoListImpl.createJwmaMessageInfoListImpl(m_Folder); } } catch (MessagingException mex) { throw new JwmaException( "jwma.folder.failedcreation", true ).setException(mex); } }//prepare /** * Updates this <tt>JwmaFolderImpl</tt> instance by setting the store instance * reference and calling <tt>update()</tt>. * This method can be used on a primarily lightweight created folder, to get it fully * prepared for extended use. * * @param store the reference to the store this folder belongs to. * * @throws JwmaException if the <tt>update()</tt> method fails. * * @see #prepare() */ public void update(JwmaStoreImpl store) throws JwmaException { m_Store = store; prepare(); }//update /** * Creates a <tt>JwmaFolderImpl</tt> instance from the given * <tt>Folder</tt>. * * @param f mail <tt>Folder</tt> this instance will "wrap". * * @return the newly created instance. * * @throws JwmaException if it fails to create the new instance. */ public static JwmaFolderImpl createJwmaFolderImpl(JwmaStoreImpl store, Folder f) throws JwmaException { JwmaFolderImpl folder = new JwmaFolderImpl(f, store); folder.prepare(); return folder; }//createJwmaFolderImpl /** * Creates a <tt>JwmaFolderImpl</tt> instance from the given * <tt>Folder</tt>. * * @param f mail <tt>Folder</tt> this instance will "wrap". * * @return the newly created instance. * * @throws JwmaException if it fails to create the new instance. */ public static JwmaFolderImpl createJwmaFolderImpl(JwmaStoreImpl store, String fullname) throws JwmaException { //FIXME: What if...the folder name is "" JwmaFolderImpl folder = new JwmaFolderImpl(store.getFolder(fullname), store); folder.prepare(); return folder; }//createJwmaFolderImpl /** * Creates a <tt>JwmaFolderImpl</tt> instance from the given * <tt>Folder</tt>. * * @param folder mail <tt>Folder</tt> this instance will "wrap". * * @return the newly created instance. * * @throws JwmaException if it fails to create the new instance. */ public static JwmaFolderImpl createLight(Folder folder) throws JwmaException { try { JwmaFolderImpl jwmafolder = new JwmaFolderImpl(folder); jwmafolder.setName(folder.getName()); jwmafolder.setPath(folder.getFullName()); jwmafolder.setType(folder.getType()); return jwmafolder; } catch (MessagingException mex) { throw new JwmaException( "jwma.folder.failedcreation", true ).setException(mex); } }//createJwmaFolderImpl}//JwmaFolderImpl
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -