1 17 18 package org.apache.james.imapserver.store; 19 20 21 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 private boolean[] flags = new boolean[6]; 41 42 43 public static final String ANSWERED = "\\ANSWERED"; 44 public static final String DELETED = "\\DELETED"; 45 public static final String DRAFT = "\\DRAFT"; 46 public static final String FLAGGED = "\\FLAGGED"; 47 public static final String SEEN = "\\SEEN"; 48 49 public MessageFlags() 50 { 51 resetAll(); 52 } 53 54 private void resetAll() 55 { 56 setAll( false ); 57 } 58 59 62 public String format() 63 { 64 StringBuffer buf = new StringBuffer (); 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 if ( buf.length() > 1 ) 86 { 87 buf.setLength( buf.length() - 1 ); 88 } 89 buf.append( ")" ); 90 return buf.toString(); 91 } 92 93 108 public boolean setFlags( String 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 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 |