KickJava   Java API By Example, From Geeks To Geeks.

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


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 Evolution implementation of MailerService interface for Gnome.
32  *
33  * @see GnomeMozMailer
34  */

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

42     public GnomeEvoMailer() {
43         evoLocation = "evolution";
44     }
45
46     /**
47      * Constructor, initialize with a given path of Evolution location
48      */

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

58     public void open(Message msg) throws LaunchFailedException {
59         String JavaDoc[] cmdArray = new String JavaDoc[2];
60         cmdArray[0] = evoLocation;
61         cmdArray[1] = "mailto:?";
62
63         cmdArray[1] += constructMailto(msg.getToAddrs(), msg.getCcAddrs(), msg.getBccAddrs(), msg.getSubject(), msg.getBody(), msg.getAttachments());
64
65         try {
66             Runtime.getRuntime().exec(cmdArray);
67             Thread.sleep(2000);
68         } catch (IOException JavaDoc e1) {
69             throw new LaunchFailedException("Cannot launch Evolution composer via commandline:" + e1);
70         } catch (InterruptedException JavaDoc iE) {}
71     }
72                                    
73     /**
74      * Launches a "blank" Evolution compose window
75      *
76      * @throws LaunchFailedException if the process fails
77      */

78     public void open() throws LaunchFailedException {
79         String JavaDoc[] cmdArray = new String JavaDoc[2];
80         cmdArray[0] = evoLocation;
81         cmdArray[1] = "mailto:?";
82     
83         try {
84             Runtime.getRuntime().exec(cmdArray);
85             Thread.sleep(2000);
86         } catch (IOException JavaDoc e2) {
87             throw new LaunchFailedException("Cannot launch Evolution composer via commandline:" + e2);
88         } catch (InterruptedException JavaDoc iE) {}
89     }
90     
91     /**
92      * Constructs commandline Arguments according to the various fields' values
93      *
94      * @return mailto argument string
95      */

96     private String JavaDoc constructMailto(Iterator JavaDoc to, Iterator JavaDoc cc, Iterator JavaDoc bcc, String JavaDoc subject, String JavaDoc body, Iterator JavaDoc attach) {
97         String JavaDoc mailto = new String JavaDoc();
98       
99         if(to != null) {
100             while(to.hasNext()) {
101                 mailto = mailto + "to=" + ((String JavaDoc)to.next()) + "&";
102             }
103         }
104     
105         if(cc != null) {
106             while(cc.hasNext()) {
107                 mailto = mailto + "cc=" + ((String JavaDoc)cc.next()) + "&";
108             }
109         }
110     
111         if(bcc != null) {
112             while (bcc.hasNext()) {
113                 mailto = mailto + "bcc=" + ((String JavaDoc)bcc.next()) + "&";
114             }
115         }
116             
117         if(subject != null)
118             mailto = mailto + "subject=" + URLUTF8Encoder.encode(subject) + "&";
119         if(body != null)
120             mailto = mailto + "body=" + URLUTF8Encoder.encode(body) + "&";
121     
122         if(attach != null) {
123             while(attach.hasNext()) {
124                 mailto = mailto + "attach=" + ((String JavaDoc)attach.next()) + "&";
125             }
126         }
127     
128         return mailto;
129      }
130 }
131
132
Popular Tags