1 22 package gnu.mail.providers.pop3; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import javax.mail.FetchProfile ; 30 import javax.mail.Flags ; 31 import javax.mail.Folder ; 32 import javax.mail.IllegalWriteException ; 33 import javax.mail.Message ; 34 import javax.mail.MessagingException ; 35 import javax.mail.Store ; 36 import javax.mail.event.ConnectionEvent ; 37 38 45 public final class POP3Folder 46 extends Folder 47 { 48 49 Map messages = new HashMap (); 50 boolean readonly = false, open = false; 51 int type; 52 53 Folder inbox; 54 55 56 62 protected POP3Folder(Store store, int type) 63 { 64 super(store); 65 this.type = type; 66 } 67 68 69 74 public String getName() 75 { 76 switch (type) { 77 case HOLDS_FOLDERS: 78 return "/"; 79 case HOLDS_MESSAGES: 80 return "INBOX"; 81 default: 82 return "(Unknown)"; 83 } 84 } 85 86 87 92 public String getFullName() 93 { 94 return getName(); 95 } 96 97 98 104 public int getType() 105 throws MessagingException 106 { 107 return type; 108 } 109 110 111 117 public boolean exists() 118 throws MessagingException 119 { 120 return (type == HOLDS_MESSAGES); 121 } 122 123 124 130 public boolean hasNewMessages() 131 throws MessagingException 132 { 133 return getNewMessageCount() > 0; 134 } 135 136 137 143 public void open(int mode) 144 throws MessagingException 145 { 146 switch (mode) { 147 case READ_WRITE: 148 readonly = false; 149 break; 150 case READ_ONLY: 151 readonly = true; 152 break; 153 } 154 open = true; 155 notifyConnectionListeners(ConnectionEvent.OPENED); 156 } 157 158 159 165 public void close(boolean expunge) 166 throws MessagingException 167 { 168 if (!open) { 169 throw new MessagingException ("Folder is not open"); 170 } 171 if (expunge) { 172 expunge(); 173 } 174 open = false; 175 notifyConnectionListeners(ConnectionEvent.CLOSED); 176 } 177 178 179 186 public Message [] expunge() 187 throws MessagingException 188 { 189 if (!open) { 190 throw new MessagingException ("Folder is not open"); 191 } 192 if (readonly) { 193 throw new MessagingException ("Folder was opened read-only"); 194 } 195 POP3Store pstore = (POP3Store) store; 196 List acc = new ArrayList (messages.size()); 197 for (Iterator i = messages.values().iterator(); i.hasNext(); ) { 198 Message message = (Message ) i.next(); 199 Flags flags = message.getFlags(); 200 if (flags.contains(Flags.Flag.DELETED)) { 201 acc.add(message); 202 pstore.delete(message.getMessageNumber()); 203 } 204 } 205 messages.clear(); 206 207 Message [] d = new Message [acc.size()]; 208 acc.toArray(d); 209 210 return d; 211 } 212 213 214 219 public boolean isOpen() 220 { 221 return open; 222 } 223 224 225 230 public Flags getPermanentFlags() 231 { 232 return new Flags (); 233 } 234 235 236 244 public int getMessageCount() 245 throws MessagingException 246 { 247 return ((POP3Store) store).getMessageCount(); 248 } 249 250 251 262 public Message getMessage(int msgnum) 263 throws MessagingException 264 { 265 if (!open) { 266 throw new MessagingException ("Folder is not open"); 267 } 268 Message message = (Message ) messages.get(new Integer (msgnum)); 269 if (message == null) { 270 message = ((POP3Store) store).getMessage(this, msgnum); 271 messages.put(new Integer (msgnum), message); 272 } 273 return message; 274 } 275 276 277 283 public void appendMessages(Message [] messages) 284 throws MessagingException 285 { 286 throw new IllegalWriteException (); 287 } 288 289 290 299 public void fetch(Message [] messages, FetchProfile fp) 300 throws MessagingException 301 { 302 } 303 304 305 311 public Folder [] list() 312 throws MessagingException 313 { 314 switch (type) { 315 case HOLDS_FOLDERS: 316 if (inbox == null) { 317 inbox = new POP3Folder(store, HOLDS_MESSAGES); 318 } 319 Folder [] folders = {inbox}; 320 return folders; 321 default: 322 throw new MessagingException ("This folder can't contain subfolders"); 323 } 324 } 325 326 327 334 public Folder [] list(String pattern) 335 throws MessagingException 336 { 337 return list(); 338 } 339 340 341 347 public Folder getParent() 348 throws MessagingException 349 { 350 switch (type) { 351 case HOLDS_MESSAGES: 352 return ((POP3Store) store).root; 353 default: 354 throw new MessagingException ("Root folders can't have a parent"); 355 } 356 } 357 358 359 366 public Folder getFolder(String s) 367 throws MessagingException 368 { 369 switch (type) { 370 case HOLDS_FOLDERS: 371 if (inbox == null) { 372 inbox = new POP3Folder(store, HOLDS_MESSAGES); 373 } 374 return inbox; 375 default: 376 throw new MessagingException ("This folder can't contain subfolders"); 377 } 378 } 379 380 381 387 public char getSeparator() 388 throws MessagingException 389 { 390 return '\u0000'; 391 } 392 393 394 396 403 public boolean create(int i) 404 throws MessagingException 405 { 406 throw new IllegalWriteException (); 407 } 408 409 410 417 public boolean delete(boolean flag) 418 throws MessagingException 419 { 420 throw new IllegalWriteException ("Folder can't be deleted"); 421 } 422 423 424 431 public boolean renameTo(Folder folder) 432 throws MessagingException 433 { 434 throw new IllegalWriteException ("Folder can't be renamed"); 435 } 436 437 } 438 439 | Popular Tags |