1 package org.columba.mail.gui.composer; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.net.URI ; 21 import java.net.URISyntaxException ; 22 import java.nio.charset.Charset ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Vector ; 26 import java.util.logging.Logger ; 27 import java.util.regex.Pattern ; 28 29 import javax.swing.event.EventListenerList ; 30 31 import org.columba.core.desktop.ColumbaDesktop; 32 import org.columba.mail.command.MailFolderCommandReference; 33 import org.columba.mail.config.AccountItem; 34 import org.columba.mail.config.MailConfig; 35 import org.columba.mail.message.ColumbaMessage; 36 import org.columba.mail.message.IColumbaMessage; 37 import org.columba.mail.parser.ListBuilder; 38 import org.columba.mail.parser.ListParser; 39 import org.columba.mail.parser.NormalizeRecipientListParser; 40 import org.columba.ristretto.io.FileSource; 41 import org.columba.ristretto.message.Address; 42 import org.columba.ristretto.message.Header; 43 import org.columba.ristretto.message.LocalMimePart; 44 import org.columba.ristretto.message.MimeHeader; 45 import org.columba.ristretto.message.StreamableMimePart; 46 47 53 public class ComposerModel { 54 55 56 private static final Logger LOG = Logger 57 .getLogger("org.columba.mail.gui.composer"); 59 private ColumbaMessage message; 60 61 private AccountItem accountItem; 62 63 private String bodytext; 64 65 private Charset charset; 66 67 private List attachments; 68 69 private List <String > toList; 70 71 private List <String > ccList; 72 73 private List <String > bccList; 74 75 private boolean signMessage; 76 77 private boolean encryptMessage; 78 79 89 private static final String emailRegExp = "[a-zA-Z0-9]+([_\\.-][a-zA-Z0-9]+)*@([a-zA-Z0-9]+([\\.-][a-zA-Z0-9]+)*)+\\.[a-zA-Z]{2,}"; 90 91 93 private static final Pattern emailPattern = Pattern.compile(emailRegExp); 94 95 101 private MailFolderCommandReference ref; 102 103 107 private boolean isHtmlMessage; 108 109 private EventListenerList listenerList = new EventListenerList (); 110 111 114 public ComposerModel() { 115 this(null, false); } 117 118 124 public ComposerModel(ColumbaMessage message) { 125 this(message, false); 126 } 127 128 134 public ComposerModel(boolean html) { 135 this(null, html); 136 } 137 138 144 public ComposerModel(Map <String ,String > messageOptions) { 145 this(); 146 setMessageOptions(messageOptions); 147 } 148 149 157 public ComposerModel(ColumbaMessage message, boolean html) { 158 if (message == null) { 160 message = new ColumbaMessage(); 161 } 162 163 this.message = message; 164 165 isHtmlMessage = html; 167 168 toList = new Vector (); 170 ccList = new Vector (); 171 bccList = new Vector (); 172 attachments = new Vector (); 173 } 174 175 183 public void setSourceReference(MailFolderCommandReference ref) { 184 this.ref = ref; 185 } 186 187 194 public MailFolderCommandReference getSourceReference() { 195 return ref; 196 } 197 198 204 public void setTo(Address[] a) { 205 getToList().clear(); 206 207 for (int i = 0; i < a.length; i++) { 208 getToList().add(a[i].toString()); 209 } 210 211 } 212 213 219 public void setCc(Address[] a) { 220 getCcList().clear(); 221 for (int i = 0; i < a.length; i++) { 222 getCcList().add(a[i].toString()); 223 } 224 } 225 226 232 public void setBcc(Address[] a) { 233 getBccList().clear(); 234 for (int i = 0; i < a.length; i++) { 235 getBccList().add(a[i].toString()); 236 } 237 } 238 239 public void setTo(String s) { 240 LOG.fine("to-headerfield:" + s); 241 242 if (s == null) { 243 return; 244 } 245 246 if (s.length() == 0) { 247 return; 248 } 249 250 List v = ListParser.createListFromString(s); 251 toList = v; 252 } 253 254 public void setHeaderField(String key, String value) { 255 message.getHeader().set(key, value); 256 } 257 258 public void setHeader(Header header) { 259 message.setHeader(header); 260 } 261 262 public String getHeaderField(String key) { 263 return (String ) message.getHeader().get(key); 264 } 265 266 public void setToList(List <String > v) { 267 this.toList = v; 268 } 269 270 public void setCcList(List <String > v) { 271 this.ccList = v; 272 } 273 274 public void setBccList(List <String > v) { 275 this.bccList = v; 276 } 277 278 public List <String > getToList() { 279 return toList; 280 } 281 282 public List <String > getCcList() { 283 return ccList; 284 } 285 286 public List <String > getBccList() { 287 return bccList; 288 } 289 290 public void setAccountItem(AccountItem item) { 291 this.accountItem = item; 292 } 293 294 public AccountItem getAccountItem() { 295 if (accountItem == null) { 296 return MailConfig.getInstance().getAccountList().get(0); 297 } else { 298 return accountItem; 299 } 300 } 301 302 public void setMessage(ColumbaMessage message) { 303 this.message = message; 304 } 305 306 public IColumbaMessage getMessage() { 307 return message; 308 } 309 310 public String getHeader(String key) { 311 return (String ) message.getHeader().get(key); 312 } 313 314 public void addMimePart(StreamableMimePart mp) { 315 attachments.add(mp); 316 317 } 319 320 public void addFileAttachment(File file) { 321 if (file.isFile()) { 322 323 String mimetype = ColumbaDesktop.getInstance().getMimeType(file); 324 325 MimeHeader header = new MimeHeader(mimetype.substring(0, mimetype 326 .indexOf('/')), mimetype 327 .substring(mimetype.indexOf('/') + 1)); 328 header.putContentParameter("name", file.getName()); 329 header.setContentDisposition("attachment"); 330 header.putDispositionParameter("filename", file.getName()); 331 header.setContentTransferEncoding("base64"); 332 333 try { 334 LocalMimePart mimePart = new LocalMimePart(header, 335 new FileSource(file)); 336 337 attachments.add(mimePart); 338 } catch (IOException e) { 339 LOG.warning("Could not add the file '" + file 340 + "' to the attachment list, due to:" + e); 341 } 342 } 343 344 } 345 346 public void setBodyText(String str) { 347 this.bodytext = str; 348 349 } 351 352 public String getSignature() { 353 return "signature"; 354 } 355 356 public String getBodyText() { 357 return bodytext; 358 } 359 360 public String getSubject() { 361 return (String ) message.getHeader().get("Subject"); 362 } 363 364 public void setSubject(String s) { 365 message.getHeader().set("Subject", s); 366 } 367 368 public List getAttachments() { 369 return attachments; 370 } 371 372 public void setAccountItem(String host, String address) { 373 setAccountItem(MailConfig.getInstance().getAccountList() 374 .hostGetAccount(host, address)); 375 } 376 377 382 public Charset getCharset() { 383 if (charset == null) { 384 charset = Charset.forName(System.getProperty("file.encoding")); 385 } 386 387 return charset; 388 } 389 390 396 public void setCharset(Charset charset) { 397 this.charset = charset; 398 } 399 400 405 public boolean isSignMessage() { 406 return signMessage; 407 } 408 409 415 public void setSignMessage(boolean signMessage) { 416 this.signMessage = signMessage; 417 } 418 419 424 public boolean isEncryptMessage() { 425 return encryptMessage; 426 } 427 428 434 public void setEncryptMessage(boolean encryptMessage) { 435 this.encryptMessage = encryptMessage; 436 } 437 438 public String getPriority() { 439 if (message.getHeader().get("X-Priority") == null) { 440 return "Normal"; 441 } else { 442 return (String ) message.getHeader().get("X-Priority"); 443 } 444 } 445 446 public void setPriority(String s) { 447 message.getHeader().set("X-Priority", s); 448 } 449 450 455 public boolean isHtml() { 456 return isHtmlMessage; 457 } 458 459 465 public void setHtml(boolean html) { 466 isHtmlMessage = html; 467 } 468 469 public List getRCPTVector() { 470 List <String > output = new Vector <String >(); 471 472 List <String > l = new NormalizeRecipientListParser() 473 .normalizeRCPTVector(ListBuilder.createFlatList(getToList())); 474 if (l != null) 475 output.addAll(l); 476 477 l = new NormalizeRecipientListParser().normalizeRCPTVector(ListBuilder 478 .createFlatList(getCcList())); 479 if (l != null) 480 output.addAll(l); 481 l = new NormalizeRecipientListParser().normalizeRCPTVector(ListBuilder 482 .createFlatList(getBccList())); 483 if (l != null) 484 output.addAll(l); 485 486 return output; 487 } 488 489 public void setMessageOptions(Map <String ,String > options) { 490 491 addAddresses(options, "to"); 492 addAddresses(options, "cc"); 493 addAddresses(options, "bcc"); 494 495 if (options.get("subject") != null) { 496 setSubject((String ) options.get("subject")); 497 } 498 499 if (options.get("body") != null) { 500 String body = (String ) options.get("body"); 501 506 boolean html = false; 507 String lcase = body.toLowerCase(); 508 509 if ((lcase.indexOf("<html>") != -1) 510 && (lcase.indexOf("</html>") != -1)) { 511 html = true; 512 } 513 514 setHtml(html); 515 516 setBodyText(body); 518 } 519 520 if (options.get("attachment") != null) { 521 if (options.get("attachment") instanceof String ) { 522 String s = (String ) options.get("attachment"); 523 try { 524 URI uri = new URI (s); 525 addFileAttachment(new File (uri)); 526 } catch (URISyntaxException e) { 527 addFileAttachment(new File (s)); 529 } 530 } 531 } 532 533 } 534 535 538 private void addAddresses(Map options, String type) { 539 List list; 540 541 if (type.equals("to")) { 542 list = getToList(); 543 } else if (type.equals("cc")) { 544 list = getCcList(); 545 } else { 546 list = getBccList(); 547 } 548 549 if (options.get(type) != null) { 550 if (options.get(type) instanceof String ) { 551 list.add((String ) options.get(type)); 552 } else { 553 String [] addresses = (String []) options.get(type); 554 555 for (int i = 0; i < addresses.length; i++) { 556 list.add(addresses[i]); 557 } 558 } 559 560 } 561 } 562 563 564 565 568 public void addModelChangedListener(IComposerModelChangedListener listener) { 569 listenerList.add(IComposerModelChangedListener.class, listener); 570 } 571 572 575 public void removeModelChangedListener( 576 IComposerModelChangedListener listener) { 577 listenerList.remove(IComposerModelChangedListener.class, listener); 578 } 579 580 583 public void fireModelChanged() { 584 ComposerModelChangedEvent e = new ComposerModelChangedEvent(this); 585 Object [] listeners = listenerList.getListenerList(); 587 588 for (int i = listeners.length - 2; i >= 0; i -= 2) { 591 if (listeners[i] == IComposerModelChangedListener.class) { 592 ((IComposerModelChangedListener) listeners[i + 1]) 593 .modelChanged(e); 594 } 595 } 596 } 597 598 601 public void fireHtmlModelChanged(boolean htmlEnabled) { 602 ComposerModelChangedEvent e = new ComposerModelChangedEvent(this, htmlEnabled); 603 Object [] listeners = listenerList.getListenerList(); 605 606 for (int i = listeners.length - 2; i >= 0; i -= 2) { 609 if (listeners[i] == IComposerModelChangedListener.class) { 610 ((IComposerModelChangedListener) listeners[i + 1]) 611 .htmlModeChanged(e); 612 } 613 } 614 } 615 } | Popular Tags |