KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > Flags


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;
19
20 import org.apache.avalon.framework.activity.Initializable;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * The set of flags associated with a message. The \Seen flag is maintained
28  * on a per-user basis.
29  *
30  * <p>Reference: RFC 2060 - para 2.3
31  * @version 0.1 on 14 Dec 2000
32  */

33 public class Flags
34     implements Serializable JavaDoc, 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     // Array does not include seen flag
44
private boolean[] flags = {false, false, false, false, true};
45
46     //users who have seen this message
47
private Set JavaDoc users;
48
49     public Flags() {
50     }
51
52     /**
53      * Initialisation - only for object creation not on deserialisation.
54      */

55     public void initialize() {
56         users = new HashSet JavaDoc();
57     }
58
59     /**
60      * Returns IMAP formatted String of Flags for named user
61      */

62     public String JavaDoc getFlags(String JavaDoc user) {
63         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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     /**
76      * Sets Flags for message from IMAP-forammted string parameter.
77      * <BR> The FLAGS<list> form overwrites existing flags, ie sets all other
78      * flags to false.
79      * <BR> The +FLAGS<list> form adds the flags in list to the existing flags
80      * <BR> The -FLAGS<list> form removes the flags in list from the existing
81      * flags
82      * <BR> Note that the Recent flag cannot be set by user and is ignored by
83      * this method.
84      *
85      * @param flagString a string formatted according to
86      * RFC2060 store_att_flags
87      * @param user the String email address of the user
88      * @return true if successful, false if not (including uninterpretable
89      * argument)
90      */

91     public boolean setFlags(String JavaDoc flagString, String JavaDoc 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 JavaDoc user) {
177         if( newState) {
178             users.add(user);
179         } else {
180             users.remove(user);
181         }
182     }
183
184     public boolean isSeen(String JavaDoc user) {
185         return users.contains(user);
186     }
187 }
188
189
Popular Tags