KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
32  * Represents the Mozilla implementation of MailerService interface for Windows.
33  *
34  * @see MailerService
35  * @see WinMapiMailer
36  */

37 public class WinMozMailer implements MailerService {
38     // Location of mozilla
39
private String JavaDoc mozLocation;
40
41     /**
42      * Constructor, set default mozLocation as mozilla could be found in PATH
43      */

44     public WinMozMailer() {
45         mozLocation = "C:\\Program Files\\mozilla.org\\Mozilla\\mozilla.exe";
46     }
47
48     /**
49      * Constructor, initialize with a given path of mozilla location
50      */

51     public WinMozMailer(String JavaDoc location) {
52         mozLocation = location;
53     }
54
55     /**
56      * Launch mozilla message compose window with information contained in msg prefilled
57      *
58      * @throws LaunchFailedException if the process fails
59      */

60     public void open(Message msg) throws LaunchFailedException {
61         String JavaDoc[] cmdArray = new String JavaDoc[3];
62
63         /* construct the commandline to launch mozilla message compose window */
64         cmdArray[0] = mozLocation;
65         cmdArray[1] = "-compose";
66         cmdArray[2] = constructArgs(msg.getToAddrs(), msg.getCcAddrs(), msg.getBccAddrs(), msg.getSubject(), msg.getBody(), msg.getAttachments());
67
68         try {
69             Runtime.getRuntime().exec(cmdArray);
70         } catch (IOException JavaDoc e) {
71             throw new LaunchFailedException("Cannot launch Mozilla composer via -compose commandline:" + e.getMessage());
72         }
73     }
74                                    
75     /**
76      * launch a "blank" mozilla compose window
77      *
78      * @throws LaunchFailedException if the process fails
79      */

80     public void open() throws LaunchFailedException {
81         String JavaDoc[] cmdArray = { mozLocation, "-compose" };
82         try {
83             Runtime.getRuntime().exec(cmdArray);
84         } catch (IOException JavaDoc e) {
85             throw new LaunchFailedException("Cannot launch Mozilla composer via -compose commandline:" + e.getMessage());
86         }
87     }
88     
89     /**
90      * Constructs commandline Arguments according to the various fields' values
91      *
92      * @param to
93      * @param cc
94      * @param bcc
95      * @param subject
96      * @return argument string
97      */

98     private String JavaDoc constructArgs(Iterator JavaDoc to, Iterator JavaDoc cc, Iterator JavaDoc bcc, String JavaDoc subject, String JavaDoc body, Iterator JavaDoc attach) {
99         Iterator JavaDoc iter;
100         String JavaDoc argString = "";
101         String JavaDoc tmp = "";
102       
103         if(to != null) {
104             while(to.hasNext()) {
105                 tmp = tmp + ((String JavaDoc)to.next()) + ",";
106             }
107             argString = "to='" + tmp + "',";
108         }
109
110         if(cc != null) {
111             tmp = "";
112             while(cc.hasNext()) {
113                 tmp = tmp + ((String JavaDoc)cc.next()) + ",";
114             }
115             argString = argString + "cc='" + tmp + "',";
116         }
117
118         if(bcc != null) {
119             tmp = "";
120             while(bcc.hasNext()) {
121                 tmp = tmp + ((String JavaDoc)bcc.next()) + ",";
122             }
123             argString = argString + "bcc='" + tmp + "',";
124         }
125
126         if(subject != null)
127             argString = argString + "subject=" + parseSubject(URLUTF8Encoder.encode(subject)) + ",";
128         if(body != null)
129             argString = argString + "body=<body>" + parseBody(URLUTF8Encoder.encode(body)) + "</body>,";
130
131         if(attach != null) {
132             tmp = "";
133             while (attach.hasNext()) {
134                 tmp = tmp + "file://" + (String JavaDoc)attach.next() + ",";
135             }
136             argString = argString + "attachment='" + tmp + "'";
137         }
138         
139         return argString;
140     }
141
142     /**
143      * handle special characters in subject string
144      *
145      * @return subject string
146      */

147     private String JavaDoc parseSubject(String JavaDoc inString) {
148         String JavaDoc tmp = inString;
149
150         //convert line-feed characters contained in a subject to white space
151
tmp = tmp.replaceAll("%0a", "%20");
152    
153         return tmp;
154     }
155
156     /**
157      * handle special characters in body string
158      *
159      * @return body string
160      */

161     private String JavaDoc parseBody(String JavaDoc inString) {
162         String JavaDoc tmp = inString;
163
164         //encoding '<''>" pairs so that body wouldn't interpret them as HTML tag
165
tmp = tmp.replaceAll("%3c", "&#60");
166         tmp = tmp.replaceAll("%3e", "&#62");
167
168         //convert line-feed characters to HTML tag <br>
169
tmp = tmp.replaceAll("%0a", "<br>");
170        
171         return tmp;
172     }
173 }
174
175
Popular Tags