001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.imap.processor;
017    
018    import com.hs.mail.imap.ImapConstants;
019    import com.hs.mail.imap.ImapSession;
020    import com.hs.mail.imap.mailbox.Mailbox;
021    import com.hs.mail.imap.mailbox.MailboxManager;
022    import com.hs.mail.imap.mailbox.SelectedMailbox;
023    import com.hs.mail.imap.message.request.DeleteRequest;
024    import com.hs.mail.imap.message.request.ImapRequest;
025    import com.hs.mail.imap.message.responder.Responder;
026    import com.hs.mail.imap.message.response.HumanReadableText;
027    
028    /**
029     * 
030     * @author Won Chul Doh
031     * @since Feb 1, 2010
032     *
033     */
034    public class DeleteProcessor extends AbstractImapProcessor {
035    
036            @Override
037            protected void doProcess(ImapSession session, ImapRequest message,
038                            Responder responder) {
039                    DeleteRequest request = (DeleteRequest) message;
040                    String mailboxName = request.getMailbox();
041                    if (ImapConstants.INBOX_NAME.equalsIgnoreCase(mailboxName)) {
042                            responder.taggedNo(request,
043                                            HumanReadableText.FAILED_TO_DELETE_INBOX);
044                    } else {
045                            MailboxManager manager = getMailboxManager();
046                            Mailbox mailbox = manager.getMailbox(session.getUserID(),
047                                            mailboxName);
048                            if (mailbox == null) {
049                                    responder
050                                                    .taggedNo(request, HumanReadableText.MAILBOX_NOT_FOUND);
051                            } else {
052                                    // Check for inferior hierarchical names
053                                    if (!mailbox.isNoInferiors() && manager.hasChildren(mailbox)) {
054                                            // Check for \Noselect mailbox name attribute
055                                            if (!mailbox.isNoSelect()) {
056                                                    // Remove all messages and set \Noselect mailbox
057                                                    // name attribute
058                                                    manager.deleteMailbox(session.getUserID(), mailbox
059                                                                    .getMailboxID(), false);
060                                            }
061                                    } else {
062                                            manager.deleteMailbox(session.getUserID(), mailbox
063                                                            .getMailboxID(), true);
064                                            fireMailboxDeleted(session, mailbox.getMailboxID());
065                                    }
066                                    SelectedMailbox selected = session.getSelectedMailbox();
067                                    if (selected != null
068                                                    && selected.getMailboxID() == mailbox.getMailboxID()) {
069                                            manager.removeEventListener(selected);
070                                            session.deselect();
071                                    }
072                                    responder.okCompleted(request);
073                            }
074                    }
075            }
076            
077            private void fireMailboxDeleted(ImapSession session, long mailboxID) {
078                    getMailboxManager().getEventDispatcher().mailboxDeleted(
079                                    session.getSessionID(), mailboxID);
080            }
081    
082    }