001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.geronimo.javamail.store.imap.connection;
019    
020    import java.util.List;
021    
022    import javax.mail.Flags;
023    import javax.mail.Folder;
024    import javax.mail.MessagingException;
025    
026    import org.apache.geronimo.javamail.store.imap.connection.IMAPResponseTokenizer.Token;
027    
028    
029    /**
030     * Utility class to aggregate status responses for a mailbox.
031     */
032    public class IMAPMailboxStatus {
033        // the set of available flag values for this mailbox
034        public Flags availableFlags = null;
035        // the permanent flags for this mailbox.
036        public Flags permanentFlags = null;
037        // the open mode flags
038        public int mode = Folder.READ_WRITE;
039    
040        // number of messages in the box
041        public int messages = -1;
042        // the number of newly added messages
043        public int recentMessages = -1;
044        // the number of unseen messages
045        public int unseenMessages = -1;
046    
047        // the next UID for this mailbox
048        public long uidNext = -1L;
049        // the UID validity item
050        public long uidValidity = -1L;
051    
052        public IMAPMailboxStatus() {
053        }
054    
055    
056        /**
057         * Merge information from a server status message.  These
058         * messages are in the form "* NAME args".  We only handle
059         * STATUS and FLAGS messages here.
060         *
061         * @param source The parsed status message.
062         *
063         * @exception MessagingException
064         */
065        public void mergeStatus(IMAPStatusResponse source) throws MessagingException {
066            // update any of the values that have changed since the last. 
067            if (source.messages != -1) {
068                messages = source.messages; 
069            }
070            if (source.uidNext != -1L) {
071                uidNext = source.uidNext; 
072            }
073            if (source.uidValidity != -1L) {
074                uidValidity = source.uidValidity; 
075            }
076            if (source.recentMessages != -1) {
077                recentMessages = source.recentMessages; 
078            }
079            if (source.unseenMessages != -1) {
080                unseenMessages = source.unseenMessages; 
081            }
082        }
083        
084        /**
085         * Merge in the FLAGS response from an EXAMINE or 
086         * SELECT mailbox command.
087         * 
088         * @param response The returned FLAGS item.
089         * 
090         * @exception MessagingException
091         */
092        public void mergeFlags(IMAPFlagsResponse response) throws MessagingException {
093            if (response != null) {
094                availableFlags = response.getFlags(); 
095            }
096        }
097        
098        
099        public void mergeSizeResponses(List responses) throws MessagingException  
100          {  
101            for (int i = 0; i < responses.size(); i++) {
102                mergeStatus((IMAPSizeResponse)responses.get(i)); 
103            }
104        }
105        
106        
107        public void mergeOkResponses(List responses) throws MessagingException {
108            for (int i = 0; i < responses.size(); i++) {
109                mergeStatus((IMAPOkResponse)responses.get(i)); 
110            }
111        }
112    
113        
114        /**
115         * Gather mailbox status information from mailbox status
116         * messages.  These messages come in as untagged messages in the
117         * form "* nnn NAME".
118         *
119         * @param source The parse message information.
120         *
121         * @exception MessagingException
122         */
123        public void mergeStatus(IMAPSizeResponse source) throws MessagingException {
124            if (source != null) {
125                String name = source.getKeyword(); 
126    
127                // untagged exists response
128                if (source.isKeyword("EXISTS")) {
129                    messages = source.getSize();
130                }
131                // untagged resent response
132                else if (source.isKeyword("RECENT")) {
133                    recentMessages = source.getSize();
134                }
135            }
136        }
137    
138        
139    
140        
141        /**
142         * Gather mailbox status information from mailbox status
143         * messages.  These messages come in as untagged messages in the
144         * form "* OK [NAME args]".
145         *
146         * @param source The parse message information.
147         *
148         * @exception MessagingException
149         */
150        public void mergeStatus(IMAPOkResponse source) throws MessagingException {
151            if (source != null) {
152                String name = source.getKeyword(); 
153    
154                // untagged UIDVALIDITY response 
155                if (source.isKeyword("UIDVALIDITY")) {
156                    List arguments = source.getStatus(); 
157                    uidValidity = ((Token)arguments.get(0)).getLong(); 
158                }
159                // untagged UIDNEXT response 
160                if (source.isKeyword("UIDNEXT")) {
161                    List arguments = source.getStatus(); 
162                    uidNext = ((Token)arguments.get(0)).getLong(); 
163                }
164                // untagged unseen response
165                else if (source.isKeyword("UNSEEN")) {
166                    List arguments = source.getStatus(); 
167                    uidValidity = ((Token)arguments.get(0)).getInteger(); 
168                }
169            }
170        }
171    
172        
173        /**
174         * Gather mailbox status information from mailbox status
175         * messages.  These messages come in as untagged messages in the
176         * form "* OK [NAME args]".
177         *
178         * @param source The parse message information.
179         *
180         * @exception MessagingException
181         */
182        public void mergeStatus(IMAPPermanentFlagsResponse source) throws MessagingException {
183            if (source != null) {
184                // this is already parsed.          
185                permanentFlags = source.flags; 
186            }
187        }
188    }