KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > desktop > internal > impl > MacMailerService


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.desktop.internal.impl;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.jdesktop.jdic.desktop.Message;
27 import org.jdesktop.jdic.desktop.internal.LaunchFailedException;
28 import org.jdesktop.jdic.desktop.internal.MailerService;
29
30 /**
31  * Implements the MailerService interface for Mac OS X.
32  *
33  * @author Elliott Hughes <enh@acm.org>
34  */

35 public class MacMailerService implements MailerService {
36     /**
37      * Launches a compose window with information contained in 'msg' already
38      * filled in.
39      * <p>
40      * Note that Mac OS X's Mail.app does not support automatic attaching of
41      * attachments.
42      *
43      * @throws LaunchFailedException if the process fails
44      */

45     public void open(Message msg) throws LaunchFailedException {
46         openComposeWindow(constructMailto(msg));
47     }
48
49     /**
50      * Launches a blank compose window.
51      *
52      * @throws LaunchFailedException if the process fails
53      */

54     public void open() throws LaunchFailedException {
55         openComposeWindow("mailto:");
56     }
57
58     /**
59      * Opens a compose window using the given mailto URI.
60      */

61     private void openComposeWindow(String JavaDoc mailto) throws LaunchFailedException {
62         try {
63             Runtime.getRuntime().exec(new String JavaDoc[] { "open", mailto });
64             Thread.sleep(2000);
65         } catch (IOException JavaDoc ex) {
66             throw new LaunchFailedException("Cannot launch mail composer via " +
67                 "command line: " + ex);
68         } catch (InterruptedException JavaDoc interruptedException) {
69             interruptedException = interruptedException;
70         }
71     }
72
73     /**
74      * Constructs command-line arguments according to the various fields' values.
75      *
76      * @return mailto argument string
77      */

78     private String JavaDoc constructMailto(Message msg) {
79         String JavaDoc mailto = "mailto:?";
80         mailto += flatten("to=", msg.getToAddrs());
81         mailto += flatten("cc=", msg.getCcAddrs());
82         mailto += flatten("bcc=", msg.getBccAddrs());
83         mailto += encode("subject=", msg.getSubject());
84         mailto += encode("body=", msg.getBody());
85         mailto += flatten("attach=", msg.getAttachments());
86         return mailto;
87     }
88
89     /**
90      * URL-encodes 'value' with the given 'prefix'; returns the
91      * empty string if 'value' is null.
92      */

93     private String JavaDoc encode(String JavaDoc prefix, String JavaDoc value) {
94         if (value != null) {
95             return prefix + URLUTF8Encoder.encode(value) + "&";
96         }
97         return "";
98     }
99
100     /**
101      * Flattens the list iterated over by 'it' with the given 'prefix';
102      * returns the empty string if the iterator has no elements.
103      */

104     private String JavaDoc flatten(String JavaDoc prefix, Iterator JavaDoc it) {
105         String JavaDoc result = "";
106         if (it != null) {
107             while (it.hasNext()) {
108                 String JavaDoc item = (String JavaDoc) it.next();
109                 result += prefix + item + "&";
110             }
111         }
112         return result;
113     }
114 }
115
Popular Tags