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.mailbox;
017    
018    import java.util.List;
019    
020    import org.apache.commons.collections.bidimap.DualHashBidiMap;
021    
022    import com.hs.mail.imap.event.EventListener;
023    import com.hs.mail.imap.event.EventTracker;
024    
025    /**
026     * 
027     * @author Won Chul Doh
028     * @since Mar 31, 2010
029     *
030     */
031    public class SelectedMailbox implements EventListener {
032    
033            /**
034             * ID of the session who selected this mailbox
035             */
036            private long sessionID;
037            /**
038             * ID of the selected mailbox
039             */
040            private long mailboxID;
041            /**
042             * Is this mailbox read only ?
043             */
044            private boolean readOnly;
045            /**
046             * Whether this mailbox has recent messages?
047             */
048            private boolean recent = false;
049            /**
050             * Mapping table for UID to MSN and vice versa
051             */
052            private DualHashBidiMap converter;
053            /**
054             * Keep tracks the notified events.
055             */
056            private EventTracker tracker;
057    
058            /**
059             * Constructor used to create a selected mailbox.
060             * 
061             * @param sessionID
062             *            ID of the session who selected this mailbox
063             * @param mailboxID
064             *            ID of the selected mailbox
065             * @param readOnly
066             *            Is this mailbox is read-only? If selected via EXAMINE command
067             *            this mailbox is read-only. Otherwise if selected via SELECT
068             *            command this mailbox is not read-only.
069             */
070            public SelectedMailbox(long sessionID, long mailboxID, boolean readOnly) {
071                    this.sessionID = sessionID;
072                    this.mailboxID = mailboxID;
073                    this.readOnly = readOnly;
074                    this.converter = new DualHashBidiMap();
075            }
076            
077            public EventTracker getEventTracker() {
078                    return tracker;
079            }
080            
081            public List<Long> getCachedUids() {
082                    return (tracker != null) ? tracker.getCachedUids() : null;
083            }
084            
085            public long getMailboxID() {
086                    return mailboxID;
087            }
088    
089            public boolean isReadOnly() {
090                    return readOnly;
091            }
092    
093            /**
094             * Check whether this mailbox contains recent messages.
095             */
096            public boolean isRecent() {
097                    return recent;
098            }
099            
100            /**
101             * Is the mailbox deleted by other session?
102             * 
103             * @return true when the mailbox has been deleted by another session, false
104             *         otherwise
105             */
106            public boolean isDeletedMailbox() {
107                    return (tracker != null) ? tracker.isDeletedMailbox() : false;
108            }
109    
110            public void setRecent(boolean recent) {
111                    this.recent = recent;
112            }
113    
114            /**
115             * Get the message sequence number corresponding to the given UID from the
116             * cache. If not exist, <code>-1</code> is returned.
117             */
118            public int getMessageNumber(long uid) {
119                    Integer v = (Integer) converter.getKey(uid);
120                    return (v != null) ? v.intValue() : -1;
121            }
122    
123            /**
124             * Get the UID corresponding to the given message sequence number from the
125             * cache. If not exist, <code>-1</code> is returned.
126             */
127            public long getUID(int msn) {
128                    Long v = (Long) converter.get(msn);
129                    return (v != null) ? v.longValue() : -1;
130            }
131            
132            public void add(int msn, long uid) {
133                    converter.put(new Integer(msn), new Long(uid));
134            }
135            
136            public void event(Event event) {
137                    if (sessionID != event.getSessionID()
138                                    && mailboxID == event.getMailboxID()) {
139                            if (tracker == null) {
140                                    // Instantiate event tracker if necessary
141                                    tracker = new EventTracker();
142                            }
143                            tracker.event(event);
144                    }
145            }
146            
147            public void resetEvents() {
148                    converter.clear();
149                    if (tracker != null) {
150                            tracker.reset();
151                            tracker = null;
152                    }
153            }
154            
155    }