KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

43     public GnomeMozMailer() {
44         mozLocation = "mozilla";
45     }
46
47     /**
48      * Constructor, initialize with a given path of mozilla location
49      */

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

59     public void open(Message msg) throws LaunchFailedException {
60         boolean HasInstance = false;
61         String JavaDoc[] cmdArray = new String JavaDoc[3];
62         String JavaDoc tmp;
63
64         /* construct the commandline to launch mozilla message compose window */
65         cmdArray[0] = mozLocation;
66         tmp = constructArgs(msg.getToAddrs(), msg.getCcAddrs(), msg.getBccAddrs(), msg.getSubject(), msg.getBody(), msg.getAttachments());
67
68         /* detect if there is already a running instance of mozilla */
69         try {
70             HasInstance = GnomeUtility.isMozillaRunning(mozLocation);
71         } catch (IOException JavaDoc e) {
72             throw new LaunchFailedException("mozilla -remote commandline failed:" + e.getMessage());
73         }
74
75         if (!HasInstance) { /* case that there is no running instance of Mozilla */
76             cmdArray[1] = "-compose";
77             cmdArray[2] = tmp;
78
79             try {
80                 Runtime.getRuntime().exec(cmdArray);
81                 Thread.sleep(500);
82             } catch (IOException JavaDoc e) {
83                 throw new LaunchFailedException("Cannot launch Mozilla composer via -compose commandline:" + e.getMessage());
84             } catch (InterruptedException JavaDoc iE) { }
85         } else {
86             /* case that there is already a running instance of Mozilla, use -remote */
87             cmdArray[1] = "-remote";
88             cmdArray[2] = "xfeDoCommand(composeMessage," + tmp + ")";
89   
90             try {
91                 Runtime.getRuntime().exec(cmdArray);
92             } catch (IOException JavaDoc e) {
93                 throw new LaunchFailedException("Cannot launch Mozilla composer via xremote commandline:" + e.getMessage());
94             }
95         }
96     }
97                                
98     /**
99      * Launches a "blank" mozilla compose window.
100      *
101      * @throws LaunchFailedException if the process fails.
102      */

103     public void open() throws LaunchFailedException {
104         boolean HasInstance = false;
105
106         /* detect if there is already a running instance of mozilla */
107         try {
108             HasInstance = GnomeUtility.isMozillaRunning(mozLocation);
109         } catch (IOException JavaDoc e) {
110             throw new LaunchFailedException("mozilla -remote commandline failed:" + e.getMessage());
111         }
112
113         if (!HasInstance) { /* case that there is no running instance of Mozilla */
114             String JavaDoc[] cmdArray = { mozLocation, "-compose" };
115             try {
116                 Runtime.getRuntime().exec(cmdArray);
117                 Thread.sleep(500);
118             } catch (IOException JavaDoc e) {
119                 throw new LaunchFailedException("Cannot launch Mozilla composer via -compose commandline:" + e.getMessage());
120             } catch (InterruptedException JavaDoc iE) { }
121         }
122         else { /* case that there is already a running instance of Mozilla, use -remote */
123             String JavaDoc[] cmdArray = { mozLocation, "-remote", "xfeDoCommand(composeMessage)" };
124             try {
125                 Runtime.getRuntime().exec(cmdArray);
126             } catch (IOException JavaDoc e) {
127                 throw new LaunchFailedException("Cannot launch Mozilla composer via -compose commandline:" + e.getMessage());
128             }
129         }
130     }
131     
132     /**
133      * Constructs commandline Arguments according to the various fields' values.
134      *
135      * @return argument string.
136      */

137     private String JavaDoc constructArgs(Iterator JavaDoc to, Iterator JavaDoc cc, Iterator JavaDoc bcc, String JavaDoc subject, String JavaDoc body, Iterator JavaDoc attach) {
138         String JavaDoc argString = new String JavaDoc("");
139         String JavaDoc tmp = new String JavaDoc();
140       
141         if(to != null) {
142             while(to.hasNext()) {
143                 tmp = tmp + ((String JavaDoc)to.next()) + ",";
144             }
145             argString = "to='" + tmp + "',";
146         }
147
148         if(cc != null) {
149             tmp = "";
150             while(cc.hasNext()) {
151                 tmp = tmp + ((String JavaDoc)cc.next()) + ",";
152             }
153             argString = argString + "cc='" + tmp + "',";
154         }
155
156         if(bcc != null) {
157             tmp = "";
158             while(bcc.hasNext()) {
159                 tmp = tmp + ((String JavaDoc)bcc.next()) + ",";
160             }
161             argString = argString + "bcc='" + tmp + "',";
162         }
163
164         if(subject != null)
165             argString = argString + "subject=" + parseSubject(URLUTF8Encoder.encode(subject)) + ",";
166         if(body != null)
167             argString = argString + "body=<body>" + parseBody(URLUTF8Encoder.encode(body)) + "</body>,";
168
169         if(attach != null) {
170             tmp = "";
171             while (attach.hasNext()) {
172                 tmp = tmp + "file://" + (String JavaDoc)attach.next() + ",";
173             }
174             argString = argString + "attachment='" + tmp + "'";
175         }
176         
177         return argString;
178     }
179
180     /**
181      * Handles special characters in subject string.
182      *
183      * @return subject string.
184      */

185     private String JavaDoc parseSubject(String JavaDoc inString) {
186         String JavaDoc tmp = inString;
187
188         //convert line-feed characters contained in a subject to white space
189
tmp = tmp.replaceAll("%0a", "%20");
190    
191         return tmp;
192     }
193
194     /**
195      * Handles special characters in body string.
196      *
197      * @return body string.
198      */

199     private String JavaDoc parseBody(String JavaDoc inString) {
200         String JavaDoc tmp = inString;
201
202         //encoding '<''>" pairs so that body wouldn't interpret them as HTML tag
203
tmp = tmp.replaceAll("%3c", "&#60");
204         tmp = tmp.replaceAll("%3e", "&#62");
205
206         //convert line-feed characters to HTML tag <br>
207
tmp = tmp.replaceAll("%0a", "<br>");
208        
209         return tmp;
210     }
211 }
212
213
Popular Tags