1 package org.lucane.applications.jmail.base; 2 3 22 23 import java.awt.event.*; 24 import java.io.*; 25 import java.util.*; 26 import javax.mail.*; 27 import javax.swing.*; 28 29 30 final class AttachmentFrame extends JFrame 31 { 32 33 private ResourceBundle msgBundle; 34 35 private JPanel panel; 36 37 private JPopupMenu filePopup; 38 private JMenuItem openMenuItem; 39 private JMenuItem saveMenuItem; 40 41 private JList list; 42 private JScrollPane listPane; 43 44 private Vector names; 45 46 47 private Message mail; 48 49 private AttachmentListener listener; 50 51 55 protected AttachmentFrame(Message mail, ResourceBundle msgBundle) 56 { 57 super(msgBundle.getString("Attachment.frameTitle")); 58 59 this.msgBundle = msgBundle; 60 61 listener = new AttachmentListener(); 62 63 this.mail = mail; 64 65 panel = new JPanel(); 66 67 names = new Vector(); 68 names = getFiles(mail, names); 69 70 list = new JList(names); 71 list.addMouseListener(listener); 72 listPane = new JScrollPane(list); 73 panel.add(listPane); 74 75 filePopup = new JPopupMenu(); 76 77 openMenuItem = new JMenuItem(msgBundle.getString("common.open")); 78 openMenuItem.addActionListener(listener); 79 filePopup.add(openMenuItem); 80 81 saveMenuItem = new JMenuItem(msgBundle.getString("common.save")); 82 saveMenuItem.addActionListener(listener); 83 filePopup.add(saveMenuItem); 84 85 setContentPane(panel); 86 setSize(400, 200); 87 setVisible(true); 88 } 89 90 95 private Vector getFiles(Part p, Vector names) 96 { 97 try 98 { 99 if(p.isMimeType("multipart/mixed") || p.isMimeType("multipart/related") || p.isMimeType("multipart/signed") || p.isMimeType("multipart/report")) 100 { 101 Multipart mp = (Multipart)p.getContent(); 102 103 for(int i = 0; i < mp.getCount(); i++) 104 names = getFiles(mp.getBodyPart(i), names); 105 } 106 107 else if(p.isMimeType("multipart/alternative")) 108 { 109 Multipart mp = (Multipart)p.getContent(); 110 BodyPart bp = null; 111 112 for(int i = 0; i < mp.getCount(); i++) 113 { 114 bp = mp.getBodyPart(i); 115 116 if(bp.isMimeType("text/html")) 117 { 118 names.add("HTML version"); 119 break; 120 } 121 } 122 } 123 124 else if(p.isMimeType("message/rfc822")) 125 { 126 Message m = (Message)p.getContent(); 127 names = getFiles(m, names); 128 } 129 130 else 131 { 132 String name = p.getFileName(); 133 134 if(name != null) 135 { 136 137 int size = p.getSize(); 138 139 String sizeString = new String (); 140 141 if(size >= 1000000) 142 sizeString = (size / 1000000) + " Mb"; 143 144 else if(size >= 1000) 145 sizeString = (size / 1000) + " kb"; 146 147 else 148 sizeString = size + " b"; 149 150 names.add(name + " (" + sizeString + ")"); 151 } 152 153 else if(p.isMimeType("text/html")) 154 names.add("HTML version"); 155 156 else if(p.isMimeType("application/pgp-signature")) 157 names.add("PGP"); 158 } 159 } 160 161 catch(Exception e) 162 { 163 e.printStackTrace(); 164 } 165 166 return(names); 167 } 168 169 174 private Part getBodyPart(Multipart mp, String name) 175 { 176 try 177 { 178 for(int i = 0; i < mp.getCount(); i++) 179 { 180 BodyPart bp = mp.getBodyPart(i); 181 182 BodyPart result = null; 183 184 if(bp.isMimeType("multipart/mixed") || bp.isMimeType("multipart/alternative")) 185 result = (BodyPart)getBodyPart((Multipart)bp.getContent(), name); 186 187 if(result != null) 188 return(result); 189 190 if(name.compareTo("HTML version") == 0) 191 if(bp.isMimeType("text/html")) 192 return(bp); 193 194 else if(name.compareTo("PGP") == 0) 195 if(bp.isMimeType("application/pgp-signature")) 196 return(bp); 197 198 if(bp.isMimeType("message/rfc822")) 199 { 200 Message newMail = (Message)bp.getContent(); 201 202 if(newMail.isMimeType("text/html")) 203 return((Part)bp.getContent()); 204 205 else if(newMail.getContent() instanceof Multipart) 206 { 207 Multipart mp2 = (Multipart)newMail.getContent(); 208 209 BodyPart bp2 = (BodyPart)getBodyPart(mp2, name); 210 211 if(bp2 != null) 212 return(bp2); 213 } 214 } 215 216 217 String n = bp.getFileName(); 218 219 if(n != null) 220 if(name.startsWith(n)) 221 return(bp); 222 } 223 } 224 225 catch(UnsupportedEncodingException encodE) 226 { 227 JOptionPane.showMessageDialog(null, encodE.getMessage(), "UnsupportedEncodingException", JOptionPane.ERROR_MESSAGE); 228 return(null); 229 } 230 231 catch(Exception e) 232 { 233 e.printStackTrace(); 234 } 235 236 return(null); 237 } 238 239 240 private void view() 241 { 242 try 243 { 244 Object o = mail.getContent(); 245 246 if(o instanceof Multipart) 247 { 248 int i = list.getSelectedIndex(); 249 250 if(i != -1) 251 { 252 String name = (String )list.getSelectedValue(); 253 254 Multipart mp = (Multipart)mail.getContent(); 255 256 BodyPart bp = (BodyPart)getBodyPart(mp, name); 257 258 if(bp == null) 259 return; 260 261 String realName = bp.getFileName(); 262 263 if(realName == null) 264 realName = "attach.html"; 265 266 if(bp.isMimeType("image/gif") || bp.isMimeType("image/jpeg") || bp.isMimeType("image/jpg") || bp.isMimeType("image/pjpeg") || bp.isMimeType("image/x-png")) 267 { 268 File f = getIt(realName.toString(), bp); 269 270 ImageIcon img = new ImageIcon(f.getPath()); 271 JLabel label = new JLabel(img); 272 273 StringBuffer buf = new StringBuffer (msgBundle.getString("AttachedFile.frameTitle")); 274 buf.append(" ").append(realName); 275 276 new AttachedFileDialog(buf.toString(), label, msgBundle); 277 278 f.delete(); 279 } 280 281 else if(bp.isMimeType("text/plain")) 282 { 283 File f = getIt(realName, bp); 284 285 JTextArea area = new JTextArea(30, 35); 286 area.setEditable(false); 287 288 BufferedReader in = new BufferedReader(new FileReader(f)); 289 290 String line = new String (); 291 292 while((line = in.readLine()) != null) 293 area.append(line + "\n"); 294 295 in.close(); 296 297 area.setCaretPosition(0); 298 299 StringBuffer buf = new StringBuffer (msgBundle.getString("AttachedFile.frameTitle")); 300 buf.append(" ").append(realName); 301 302 new AttachedFileDialog(buf.toString(), area, msgBundle); 303 304 f.delete(); 305 } 306 307 else if(bp.isMimeType("text/html")) 308 { 309 JEditorPane editorPane = new JEditorPane("text/html", (String )bp.getContent()); 310 editorPane.setEditable(false); 311 312 new AttachedFileDialog(realName, editorPane, msgBundle); 313 } 314 315 else 316 { 317 String type[] = bp.getHeader("Content-Type"); 318 319 JOptionPane.showMessageDialog(null, msgBundle.getString("Attachment.undisplayableFilePart1Label") + " \"" + type[0] + "\" " + msgBundle.getString("Attachment.undisplayableFilePart2Label"), "Attachment", JOptionPane.ERROR_MESSAGE); 320 } 321 } 322 } 323 324 else 325 { 326 JEditorPane editorPane = new JEditorPane("text/html", (String )mail.getContent()); 327 editorPane.setEditable(false); 328 329 StringBuffer buf = new StringBuffer (msgBundle.getString("AttachedFile.frameTitle")); 330 buf.append(" attach.html"); 331 new AttachedFileDialog(buf.toString(), editorPane, msgBundle); 332 } 333 } 334 335 catch(Exception ex) 336 { 337 ex.printStackTrace(); 338 } 339 } 340 341 342 private void save() 343 { 344 int i = list.getSelectedIndex(); 345 346 if(i != -1) 347 { 348 try 349 { 350 String name = (String )list.getSelectedValue(); 351 352 Part bp = null; 353 String realName = new String (); 354 355 Object o = mail.getContent(); 356 357 if(o instanceof Multipart) 358 { 359 Multipart mp = (Multipart)mail.getContent(); 360 bp = getBodyPart(mp, name); 361 362 if(bp == null) 363 return; 364 365 realName = bp.getFileName(); 366 367 if(realName == null) 368 { 369 if(name.compareTo("HTML version") == 0) 370 realName = "attach.html"; 371 372 else if(name.compareTo("PGP") == 0) 373 realName = "sign.pgp"; 374 } 375 } 376 377 else 378 { 379 bp = (Part)mail; 380 realName = "attach.html"; 381 } 382 383 JFileChooser fc = new JFileChooser(); 384 385 String newName = new String (); 386 387 fc.setSelectedFile(new File(realName)); 388 389 int val = fc.showSaveDialog(null); 390 391 if(val == JFileChooser.APPROVE_OPTION) 392 { 393 File file = fc.getSelectedFile(); 394 395 if(file != null) 396 { 397 try 398 { 399 while(file.exists()) 400 { 401 int choice = JOptionPane.showConfirmDialog(null, msgBundle.getString("Attachment.fileExistsLabel"), "Attachment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 402 403 if(choice == JOptionPane.YES_OPTION) 404 break; 405 406 val = fc.showSaveDialog(null); 407 408 if(val == JFileChooser.APPROVE_OPTION) 409 file = fc.getSelectedFile(); 410 411 else 412 return; 413 } 414 415 InputStream in = bp.getInputStream(); 416 FileOutputStream out = new FileOutputStream(file); 417 418 int nb; 419 byte b[]; 420 421 b = new byte[256]; 422 nb = in.read(b); 423 424 while(nb == 256) 425 { 426 out.write(b, 0, nb); 427 b = new byte[256]; 428 nb = in.read(b); 429 } 430 431 if(nb != -1) 432 { 433 out.write(b, 0, nb); 434 } 435 436 in.close(); 437 out.close(); 438 } 439 440 catch(Exception ex) 441 { 442 ex.printStackTrace(); 443 } 444 } 445 } 446 } 447 448 catch(Exception ex) 449 { 450 ex.printStackTrace(); 451 } 452 } 453 } 454 455 460 private File getIt(String name, BodyPart bp) 461 { 462 try 463 { 464 File f = File.createTempFile(name, null); 465 466 InputStream in = bp.getInputStream(); 467 FileOutputStream out = new FileOutputStream(f); 468 469 int nb; 470 byte b[]; 471 472 do 473 { 474 b = new byte[256]; 475 nb = in.read(b); 476 out.write(b, 0, nb); 477 } while(nb != -1 && nb == 256); 478 479 in.close(); 480 out.close(); 481 482 return(f); 483 } 484 485 catch(Exception e) 486 { 487 e.printStackTrace(); 488 } 489 490 return(null); 491 } 492 493 494 private final class AttachmentListener extends MouseAdapter implements ActionListener 495 { 496 499 public final void mousePressed(MouseEvent e) 500 { 501 if(e.isPopupTrigger()) 502 { 503 if(list.getSelectedIndex() != -1) 504 filePopup.show(e.getComponent(), e.getX(), e.getY()); 505 } 506 507 else 508 { 509 int mods = e.getModifiers(); 510 511 if(mods == 4) 512 { 513 if(list.getSelectedIndex() != -1) 514 filePopup.show(e.getComponent(), e.getX(), e.getY()); 515 516 } 517 518 519 else if(mods == 16 && e.getClickCount() == 2) 520 { 521 if(list.getSelectedIndex() != -1) 522 view(); 523 } 524 } 525 } 526 527 530 public final void actionPerformed(ActionEvent e) 531 { 532 JMenuItem m = (JMenuItem)e.getSource(); 533 534 if(m == openMenuItem) 535 view(); 536 537 else if(m == saveMenuItem) 538 save(); 539 } 540 } 541 } 542 | Popular Tags |