KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > store > MessageFlags


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.imapserver.store;
19
20
21 /**
22  * The set of flags associated with a message.
23  * TODO - should store SEEN flag on a peruser basis (not required, but nice)
24  * TODO - why not use javax.mail.Flags instead of having our own.
25  *
26  * <p>Reference: RFC 2060 - para 2.3
27  * @version 0.1 on 14 Dec 2000
28  */

29 public class MessageFlags
30 {
31
32     public static final int ANSWERED_INDEX = 0;
33     public static final int DELETED_INDEX = 1;
34     public static final int DRAFT_INDEX = 2;
35     public static final int FLAGGED_INDEX = 3;
36     public static final int RECENT_INDEX = 4;
37     public static final int SEEN_INDEX = 5;
38
39     // Array does not include seen flag
40
private boolean[] flags = new boolean[6];
41
42
43     public static final String JavaDoc ANSWERED = "\\ANSWERED";
44     public static final String JavaDoc DELETED = "\\DELETED";
45     public static final String JavaDoc DRAFT = "\\DRAFT";
46     public static final String JavaDoc FLAGGED = "\\FLAGGED";
47     public static final String JavaDoc SEEN = "\\SEEN";
48
49     public MessageFlags()
50     {
51         resetAll();
52     }
53
54     private void resetAll()
55     {
56         setAll( false );
57     }
58
59     /**
60      * Returns IMAP formatted String of MessageFlags for named user
61      */

62     public String JavaDoc format()
63     {
64         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
65         buf.append( "(" );
66         if ( flags[ANSWERED_INDEX] ) {
67             buf.append( "\\Answered " );
68         }
69         if ( flags[DELETED_INDEX] ) {
70             buf.append( "\\Deleted " );
71         }
72         if ( flags[DRAFT_INDEX] ) {
73             buf.append( "\\Draft " );
74         }
75         if ( flags[FLAGGED_INDEX] ) {
76             buf.append( "\\Flagged " );
77         }
78         if ( flags[RECENT_INDEX] ) {
79             buf.append( "\\Recent " );
80         }
81         if ( flags[SEEN_INDEX] ) {
82             buf.append( "\\Seen " );
83         }
84         // Remove the trailing space, if necessary.
85
if ( buf.length() > 1 )
86         {
87             buf.setLength( buf.length() - 1 );
88         }
89         buf.append( ")" );
90         return buf.toString();
91     }
92
93     /**
94      * Sets MessageFlags for message from IMAP-forammted string parameter.
95      * <BR> The FLAGS<list> form overwrites existing flags, ie sets all other
96      * flags to false.
97      * <BR> The +FLAGS<list> form adds the flags in list to the existing flags
98      * <BR> The -FLAGS<list> form removes the flags in list from the existing
99      * flags
100      * <BR> Note that the Recent flag cannot be set by user and is ignored by
101      * this method.
102      *
103      * @param flagString a string formatted according to
104      * RFC2060 store_att_flags
105      * @return true if successful, false if not (including uninterpretable
106      * argument)
107      */

108     public boolean setFlags( String JavaDoc flagString )
109     {
110         flagString = flagString.toUpperCase();
111
112         boolean modValue;
113
114         if ( flagString.startsWith( "FLAGS" ) ) {
115             modValue = true;
116             resetAll();
117         }
118         else if ( flagString.startsWith( "+FLAGS" ) ) {
119             modValue = true;
120         }
121         else if ( flagString.startsWith( "-FLAGS" ) ) {
122             modValue = false;
123         }
124         else {
125             // Invalid flag string.
126
return false;
127         }
128
129         if ( flagString.indexOf( ANSWERED ) != -1 ) {
130             flags[ANSWERED_INDEX] = modValue;
131         }
132         if ( flagString.indexOf( DELETED ) != -1 ) {
133             flags[DELETED_INDEX] = modValue;
134         }
135         if ( flagString.indexOf( DRAFT ) != -1 ) {
136             flags[DRAFT_INDEX] = modValue;
137         }
138         if ( flagString.indexOf( FLAGGED ) != -1 ) {
139             flags[FLAGGED_INDEX] = modValue;
140         }
141         if ( flagString.indexOf( SEEN ) != -1 ) {
142             flags[SEEN_INDEX] = modValue;
143         }
144         return true;
145     }
146
147     public void setAnswered( boolean newState )
148     {
149         flags[ANSWERED_INDEX] = newState;
150     }
151
152     public boolean isAnswered()
153     {
154         return flags[ANSWERED_INDEX];
155     }
156
157     public void setDeleted( boolean newState )
158     {
159         flags[DELETED_INDEX] = newState;
160     }
161
162     public boolean isDeleted()
163     {
164         return flags[DELETED_INDEX];
165     }
166
167     public void setDraft( boolean newState )
168     {
169         flags[DRAFT_INDEX] = newState;
170     }
171
172     public boolean isDraft()
173     {
174         return flags[DRAFT_INDEX];
175     }
176
177     public void setFlagged( boolean newState )
178     {
179         flags[FLAGGED_INDEX] = newState;
180     }
181
182     public boolean isFlagged()
183     {
184         return flags[FLAGGED_INDEX];
185     }
186
187     public void setRecent( boolean newState )
188     {
189         flags[RECENT_INDEX] = newState;
190     }
191
192     public boolean isRecent()
193     {
194         return flags[RECENT_INDEX];
195     }
196
197     public void setSeen( boolean newState )
198     {
199         flags[SEEN_INDEX] = newState;
200     }
201
202     public boolean isSeen()
203     {
204         return flags[SEEN_INDEX];
205     }
206
207     public void setAll( boolean newState )
208     {
209         for ( int i = ANSWERED_INDEX; i <= SEEN_INDEX; i++ )
210         {
211             flags[i] = newState;
212         }
213     }
214
215     public void setAll( MessageFlags newFlags ) {
216         setAnswered( newFlags.isAnswered() );
217         setDeleted( newFlags.isDeleted() );
218         setDraft( newFlags.isDraft() );
219         setFlagged( newFlags.isFlagged() );
220         setSeen( newFlags.isSeen() );
221     }
222
223     public void addAll( MessageFlags toAdd ) {
224         setFlagState( toAdd, true );
225     }
226
227     public void removeAll (MessageFlags toRemove) {
228         setFlagState( toRemove, false );
229     }
230
231     private void setFlagState( MessageFlags changes, boolean setState )
232     {
233         if ( changes.isAnswered() ) {
234             setAnswered( setState );
235         }
236         if ( changes.isDeleted() ) {
237             setDeleted( setState );
238         }
239         if ( changes.isDraft() ) {
240             setDraft( setState );
241         }
242         if ( changes.isFlagged() ) {
243             setFlagged( setState );
244         }
245         if ( changes.isSeen() ) {
246             setSeen( setState );
247         }
248     }
249 }
250
251
Popular Tags