1 20 21 package org.jdesktop.jdic.desktop.internal.impl; 22 23 import java.io.IOException ; 24 import java.util.Iterator ; 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 36 public class GnomeMozMailer implements MailerService { 37 38 private String mozLocation; 39 40 43 public GnomeMozMailer() { 44 mozLocation = "mozilla"; 45 } 46 47 50 public GnomeMozMailer(String location) { 51 mozLocation = location; 52 } 53 54 59 public void open(Message msg) throws LaunchFailedException { 60 boolean HasInstance = false; 61 String [] cmdArray = new String [3]; 62 String tmp; 63 64 65 cmdArray[0] = mozLocation; 66 tmp = constructArgs(msg.getToAddrs(), msg.getCcAddrs(), msg.getBccAddrs(), msg.getSubject(), msg.getBody(), msg.getAttachments()); 67 68 69 try { 70 HasInstance = GnomeUtility.isMozillaRunning(mozLocation); 71 } catch (IOException e) { 72 throw new LaunchFailedException("mozilla -remote commandline failed:" + e.getMessage()); 73 } 74 75 if (!HasInstance) { 76 cmdArray[1] = "-compose"; 77 cmdArray[2] = tmp; 78 79 try { 80 Runtime.getRuntime().exec(cmdArray); 81 Thread.sleep(500); 82 } catch (IOException e) { 83 throw new LaunchFailedException("Cannot launch Mozilla composer via -compose commandline:" + e.getMessage()); 84 } catch (InterruptedException iE) { } 85 } else { 86 87 cmdArray[1] = "-remote"; 88 cmdArray[2] = "xfeDoCommand(composeMessage," + tmp + ")"; 89 90 try { 91 Runtime.getRuntime().exec(cmdArray); 92 } catch (IOException e) { 93 throw new LaunchFailedException("Cannot launch Mozilla composer via xremote commandline:" + e.getMessage()); 94 } 95 } 96 } 97 98 103 public void open() throws LaunchFailedException { 104 boolean HasInstance = false; 105 106 107 try { 108 HasInstance = GnomeUtility.isMozillaRunning(mozLocation); 109 } catch (IOException e) { 110 throw new LaunchFailedException("mozilla -remote commandline failed:" + e.getMessage()); 111 } 112 113 if (!HasInstance) { 114 String [] cmdArray = { mozLocation, "-compose" }; 115 try { 116 Runtime.getRuntime().exec(cmdArray); 117 Thread.sleep(500); 118 } catch (IOException e) { 119 throw new LaunchFailedException("Cannot launch Mozilla composer via -compose commandline:" + e.getMessage()); 120 } catch (InterruptedException iE) { } 121 } 122 else { 123 String [] cmdArray = { mozLocation, "-remote", "xfeDoCommand(composeMessage)" }; 124 try { 125 Runtime.getRuntime().exec(cmdArray); 126 } catch (IOException e) { 127 throw new LaunchFailedException("Cannot launch Mozilla composer via -compose commandline:" + e.getMessage()); 128 } 129 } 130 } 131 132 137 private String constructArgs(Iterator to, Iterator cc, Iterator bcc, String subject, String body, Iterator attach) { 138 String argString = new String (""); 139 String tmp = new String (); 140 141 if(to != null) { 142 while(to.hasNext()) { 143 tmp = tmp + ((String )to.next()) + ","; 144 } 145 argString = "to='" + tmp + "',"; 146 } 147 148 if(cc != null) { 149 tmp = ""; 150 while(cc.hasNext()) { 151 tmp = tmp + ((String )cc.next()) + ","; 152 } 153 argString = argString + "cc='" + tmp + "',"; 154 } 155 156 if(bcc != null) { 157 tmp = ""; 158 while(bcc.hasNext()) { 159 tmp = tmp + ((String )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 )attach.next() + ","; 173 } 174 argString = argString + "attachment='" + tmp + "'"; 175 } 176 177 return argString; 178 } 179 180 185 private String parseSubject(String inString) { 186 String tmp = inString; 187 188 tmp = tmp.replaceAll("%0a", "%20"); 190 191 return tmp; 192 } 193 194 199 private String parseBody(String inString) { 200 String tmp = inString; 201 202 tmp = tmp.replaceAll("%3c", "<"); 204 tmp = tmp.replaceAll("%3e", ">"); 205 206 tmp = tmp.replaceAll("%0a", "<br>"); 208 209 return tmp; 210 } 211 } 212 213 | Popular Tags |