1 17 18 package org.apache.james.imapserver; 19 20 import org.apache.avalon.framework.activity.Initializable; 21 22 import java.io.Serializable ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 33 public class Flags 34 implements Serializable , Initializable { 35 36 public static final int ANSWERED = 0; 37 public static final int DELETED = 1; 38 public static final int DRAFT = 2; 39 public static final int FLAGGED = 3; 40 public static final int RECENT = 4; 41 public static final int SEEN = 5; 42 43 private boolean[] flags = {false, false, false, false, true}; 45 46 private Set users; 48 49 public Flags() { 50 } 51 52 55 public void initialize() { 56 users = new HashSet (); 57 } 58 59 62 public String getFlags(String user) { 63 StringBuffer buf = new StringBuffer (); 64 buf.append("("); 65 if (flags[ANSWERED]) { buf.append("\\ANSWERED ");} 66 if (flags[DELETED]) { buf.append("\\DELETED ");} 67 if (flags[DRAFT]) { buf.append("\\DRAFT ");} 68 if (flags[FLAGGED]) { buf.append("\\FLAGGED ");} 69 if (flags[RECENT]) { buf.append("\\RECENT ");} 70 if (users.contains(user)) { buf.append("\\SEEN ");} 71 buf.append(")"); 72 return buf.toString(); 73 } 74 75 91 public boolean setFlags(String flagString, String user) { 92 flagString = flagString.toUpperCase(); 93 if (flagString.startsWith("FLAGS")) { 94 boolean [] newflags = new boolean[5]; 95 newflags[ANSWERED] 96 = (flagString.indexOf("\\ANSWERED") != -1) ? true : false; 97 newflags[DELETED] 98 = (flagString.indexOf("\\DELETED") != -1) ? true : false; 99 newflags[DRAFT] 100 = (flagString.indexOf("\\DRAFT") != -1) ? true : false; 101 newflags[FLAGGED] 102 = (flagString.indexOf("\\FLAGGED") != -1) ? true : false; 103 newflags[RECENT] = false; 104 if (flagString.indexOf("\\SEEN") != -1) { 105 users.add(user); 106 } 107 System.arraycopy(newflags, 0, flags, 0, newflags.length); 108 return true; 109 } else if (flagString.startsWith("+FLAGS") ||flagString.startsWith("-FLAGS") ) { 110 boolean mod = (flagString.startsWith("+") ? true : false); 111 if (flagString.indexOf("\\ANSWERED") != -1) { 112 flags[ANSWERED] = mod; 113 } 114 if (flagString.indexOf("\\DELETED") != -1) { 115 flags[DELETED] = mod; 116 } 117 if (flagString.indexOf("\\DRAFT") != -1) { 118 flags[DRAFT] = mod; 119 } 120 if (flagString.indexOf("\\FLAGGED") != -1) { 121 flags[FLAGGED] = mod; 122 } 123 if (flagString.indexOf("\\SEEN") != -1) { 124 if( mod) { 125 users.add(user); 126 } else { 127 users.remove(user); 128 } 129 } 130 return true; 131 } else { 132 return false; 133 } 134 } 135 136 public void setAnswered(boolean newState) { 137 flags[ANSWERED] = newState; 138 } 139 140 public boolean isAnswered() { 141 return flags[ANSWERED]; 142 } 143 144 public void setDeleted(boolean newState) { 145 flags[DELETED] = newState; 146 } 147 148 public boolean isDeleted() { 149 return flags[DELETED]; 150 } 151 152 public void setDraft(boolean newState) { 153 flags[DRAFT] = newState; 154 } 155 156 public boolean isDraft() { 157 return flags[DRAFT]; 158 } 159 160 public void setFlagged(boolean newState) { 161 flags[FLAGGED] = newState; 162 } 163 164 public boolean isFlagged() { 165 return flags[FLAGGED]; 166 } 167 168 public void setRecent(boolean newState) { 169 flags[RECENT] = newState; 170 } 171 172 public boolean isRecent() { 173 return flags[RECENT]; 174 } 175 176 public void setSeen(boolean newState, String user) { 177 if( newState) { 178 users.add(user); 179 } else { 180 users.remove(user); 181 } 182 } 183 184 public boolean isSeen(String user) { 185 return users.contains(user); 186 } 187 } 188 189 | Popular Tags |