xref: /trunk/main/scripting/examples/java/Newsgroup/OfficeAttachment.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1*cd519653SAndrew Rist /**************************************************************
2*cd519653SAndrew Rist  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22cdf0e10cSrcweir //import com.sun.star.frame.XComponentLoader;
23cdf0e10cSrcweir import java.io.*;
24cdf0e10cSrcweir import com.sun.star.lang.XComponent;
25cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
26cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
27cdf0e10cSrcweir import com.sun.star.frame.XStorable;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir import com.sun.star.frame.XModel;
30cdf0e10cSrcweir import com.sun.star.script.framework.runtime.XScriptContext;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // for debug only
33cdf0e10cSrcweir import javax.swing.JOptionPane;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir public class OfficeAttachment
36cdf0e10cSrcweir {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir     private StatusWindow status = null;
39cdf0e10cSrcweir     private XStorable storedDoc = null;
40cdf0e10cSrcweir     private File htmlFile = null;
41cdf0e10cSrcweir     private File officeFile = null;
42cdf0e10cSrcweir     private boolean isHtmlDoc = false;
43cdf0e10cSrcweir     private boolean isOfficeDoc = false;
44cdf0e10cSrcweir     private String templocationURL = "";
45cdf0e10cSrcweir     private String templocationSystem = "";
46cdf0e10cSrcweir     private String attachmentName = "";
47cdf0e10cSrcweir     private String statusLine = "";
48cdf0e10cSrcweir 
OfficeAttachment( XScriptContext xsc, StatusWindow sw, boolean html, boolean office )49cdf0e10cSrcweir     public OfficeAttachment( XScriptContext xsc, StatusWindow sw, boolean html, boolean office )
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir         status = sw;
52cdf0e10cSrcweir         isHtmlDoc = html;
53cdf0e10cSrcweir         isOfficeDoc = office;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         templocationSystem = templocationURL = System.getProperty( "user.home" );
56cdf0e10cSrcweir         if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
57cdf0e10cSrcweir         {
58cdf0e10cSrcweir             while( templocationURL.indexOf( "\\" ) != -1 )
59cdf0e10cSrcweir             {
60cdf0e10cSrcweir                 int sepPos = templocationURL.indexOf( "\\" );
61cdf0e10cSrcweir                 String firstPart = templocationURL.substring( 0, sepPos );
62cdf0e10cSrcweir                 String lastPart = templocationURL.substring( sepPos + 1, templocationURL.length() );
63cdf0e10cSrcweir                 templocationURL = firstPart + "/" + lastPart;
64cdf0e10cSrcweir                 //JOptionPane.showMessageDialog( null, "Temp Location URL is: " + templocationURL + "\nfirstPart is: " + firstPart + "\nlastPart is: " + lastPart );
65cdf0e10cSrcweir             }
66cdf0e10cSrcweir         }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         try
69cdf0e10cSrcweir         {
70cdf0e10cSrcweir             statusLine = "Querying Office for current document";
71cdf0e10cSrcweir             status.setStatus( 1, statusLine );
72cdf0e10cSrcweir             XScriptContext scriptcontext = xsc;
73cdf0e10cSrcweir             XModel xmodel = scriptcontext.getDocument();
74cdf0e10cSrcweir             storedDoc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xmodel);
75cdf0e10cSrcweir             // find document name from storedDoc
76cdf0e10cSrcweir             attachmentName = storedDoc.getLocation();
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir         catch( Exception e )
79cdf0e10cSrcweir         {
80cdf0e10cSrcweir             //UNO error
81cdf0e10cSrcweir             status.setStatus( 1, "Error: " + statusLine );
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         if( attachmentName.equalsIgnoreCase( "" ) )
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             attachmentName = "Attachment";
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir         else
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             //int lastSep = attachmentName.lastIndexOf( System.getProperty( "file.separator" ) );
91cdf0e10cSrcweir             int lastSep = attachmentName.lastIndexOf( "/" );
92cdf0e10cSrcweir             attachmentName = attachmentName.substring( lastSep + 1, attachmentName.length() );
93cdf0e10cSrcweir             int dot = attachmentName.indexOf( "." );
94cdf0e10cSrcweir             attachmentName = attachmentName.substring( 0, dot );
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 
createTempDocs()99cdf0e10cSrcweir     public boolean createTempDocs()
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         String filenameURL = "file:///" + templocationURL +  "/" + attachmentName;
102cdf0e10cSrcweir         //String filenameSystem = templocationSystem + System.getProperty( "file.separator" ) + attachmentName;
103cdf0e10cSrcweir         //JOptionPane.showMessageDialog( null, "Filename URL " + filenameURL );
104cdf0e10cSrcweir         try
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             if( isHtmlDoc )
107cdf0e10cSrcweir             {
108cdf0e10cSrcweir                 //JOptionPane.showMessageDialog( null, "Saving doc in HTML format" );
109cdf0e10cSrcweir                 statusLine = "Saving doc in HTML format";
110cdf0e10cSrcweir                 status.setStatus( 4, statusLine );
111cdf0e10cSrcweir                 //System.out.print( "Saving attachment as " + filename + ".html..." );
112cdf0e10cSrcweir                     PropertyValue[] propertyvalue_html = new PropertyValue[2];
113cdf0e10cSrcweir                     propertyvalue_html[0] = new PropertyValue();
114cdf0e10cSrcweir                     propertyvalue_html[0].Name = new String("Overwrite");
115cdf0e10cSrcweir                     propertyvalue_html[0].Value = new Boolean(true);
116cdf0e10cSrcweir                     propertyvalue_html[1] = new PropertyValue();
117cdf0e10cSrcweir                     propertyvalue_html[1].Name = ("FilterName");
118cdf0e10cSrcweir //                  propertyvalue_html[1].Value = new String("scalc: HTML (StarCalc)");
119cdf0e10cSrcweir                     propertyvalue_html[1].Value = new String("swriter: HTML (StarWriter)");
120cdf0e10cSrcweir                 storedDoc.storeAsURL( filenameURL + ".html", propertyvalue_html);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir                 File homedir = new File( templocationSystem );
123cdf0e10cSrcweir                 //JOptionPane.showMessageDialog( null, "homedir (Java File): " + homedir.getPath() );
124cdf0e10cSrcweir                 File homefiles[] = homedir.listFiles();
125cdf0e10cSrcweir                 String file = "";
126cdf0e10cSrcweir                 for(int i=0; i < homefiles.length; i++ )
127cdf0e10cSrcweir                 {
128cdf0e10cSrcweir                     if( homefiles[i].getName().equals( attachmentName + ".html" ) )
129cdf0e10cSrcweir                     {
130cdf0e10cSrcweir                         //htmlFile = new File( homefiles[i].getAbsolutePath() );
131cdf0e10cSrcweir                         //JOptionPane.showMessageDialog( null, "Found HTML" );
132cdf0e10cSrcweir                         file = homefiles[i].getAbsolutePath();
133cdf0e10cSrcweir                     }
134cdf0e10cSrcweir                 }
135cdf0e10cSrcweir                 htmlFile = new File( file );
136cdf0e10cSrcweir                 //htmlFile = new File( filename + ".html" );
137cdf0e10cSrcweir                 //htmlFile = new File( storedDoc.getLocation() );
138cdf0e10cSrcweir             }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             if( isOfficeDoc )
141cdf0e10cSrcweir             {
142cdf0e10cSrcweir                 //JOptionPane.showMessageDialog( null, "Saving doc in .sxw format" );
143cdf0e10cSrcweir                 statusLine = "Saving doc in .sxw format";
144cdf0e10cSrcweir                 status.setStatus( 4, statusLine );
145cdf0e10cSrcweir                 //System.out.print( "Saving attachment as " + filename + ".sxw..." );
146cdf0e10cSrcweir                 PropertyValue[] propertyvalue_sxw = new PropertyValue[2];
147cdf0e10cSrcweir                     propertyvalue_sxw[0] = new PropertyValue();
148cdf0e10cSrcweir                     propertyvalue_sxw[0].Name = new String("Overwrite");
149cdf0e10cSrcweir                     propertyvalue_sxw[0].Value = new Boolean(true);
150cdf0e10cSrcweir                     propertyvalue_sxw[1] = new PropertyValue();
151cdf0e10cSrcweir                     propertyvalue_sxw[1].Name = new String("Overwrite");
152cdf0e10cSrcweir                     propertyvalue_sxw[1].Value = new Boolean(true);
153cdf0e10cSrcweir                     storedDoc.storeAsURL( filenameURL + ".sxw", propertyvalue_sxw);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir                 File homedir = new File( templocationSystem );
156cdf0e10cSrcweir 
157cdf0e10cSrcweir                 //JOptionPane.showMessageDialog( null, "homedir (Java File): " + homedir.getPath() );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir                                 File homefiles[] = homedir.listFiles();
160cdf0e10cSrcweir                 String file = "";
161cdf0e10cSrcweir                                 for(int i=0; i < homefiles.length; i++ )
162cdf0e10cSrcweir                                 {
163cdf0e10cSrcweir                                         if( homefiles[i].getName().equals( attachmentName + ".sxw" ) )
164cdf0e10cSrcweir                                         {
165cdf0e10cSrcweir                                                 //officeFile = new File( homefiles[i].getAbsolutePath() );
166cdf0e10cSrcweir                         //JOptionPane.showMessageDialog( null, "Found .sxw" );
167cdf0e10cSrcweir                         file = homefiles[i].getAbsolutePath();
168cdf0e10cSrcweir                                         }
169cdf0e10cSrcweir                                 }
170cdf0e10cSrcweir                 officeFile = new File( file );
171cdf0e10cSrcweir                 //officeFile = new File( filename + ".sxw" );
172cdf0e10cSrcweir                 //officeFile = new File (storedDoc.getLocation() );
173cdf0e10cSrcweir             }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir             //status.setStatus( 10, "Attachments successfully created" );
176cdf0e10cSrcweir 
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir         catch( SecurityException se )
179cdf0e10cSrcweir         {
180cdf0e10cSrcweir             status.setStatus( 4, "Error: " + statusLine );
181cdf0e10cSrcweir             System.out.println( "Security error while saving temporary Document(s). Check file permissions in home directory." );
182cdf0e10cSrcweir             se.printStackTrace();
183cdf0e10cSrcweir             htmlFile = null;
184cdf0e10cSrcweir             officeFile = null;
185cdf0e10cSrcweir             return false;
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir         catch( Exception e )
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             status.setStatus( 4, "Error: " + statusLine );
190cdf0e10cSrcweir             System.out.println( "Error saving temporary Document(s)" );
191cdf0e10cSrcweir             e.printStackTrace();
192cdf0e10cSrcweir             htmlFile = null;
193cdf0e10cSrcweir             officeFile = null;
194cdf0e10cSrcweir             return false;
195cdf0e10cSrcweir         }
196cdf0e10cSrcweir         return true;
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 
removeTempDocs()200cdf0e10cSrcweir     public boolean removeTempDocs()
201cdf0e10cSrcweir     {
202cdf0e10cSrcweir         /*
203cdf0e10cSrcweir         if( !htmlFile.exists() && !officeFile.exists() )
204cdf0e10cSrcweir                 {
205cdf0e10cSrcweir             System.out.println("Error: Document(s) have not been saved." );
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir         */
208cdf0e10cSrcweir 
209cdf0e10cSrcweir         statusLine = "Removing temp docs";
210cdf0e10cSrcweir         status.setStatus( 13, statusLine );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         try
213cdf0e10cSrcweir         {
214cdf0e10cSrcweir             if( isOfficeDoc && isHtmlDoc )
215cdf0e10cSrcweir             {
216cdf0e10cSrcweir                 //System.out.println( "Removing: " + htmlFile.getPath() + " " + officeFile.getPath() );
217cdf0e10cSrcweir                 //System.out.println( "htmlfile " + htmlFile.exists() + " officeFile " + officeFile.exists() );
218cdf0e10cSrcweir                 //JOptionPane.showMessageDialog( null, "Removing: " + htmlFile.getPath() + " " + officeFile.getPath() );
219cdf0e10cSrcweir                 //JOptionPane.showMessageDialog( null, "htmlfile " + htmlFile.exists() + " officeFile " + officeFile.exists() );
220cdf0e10cSrcweir                 htmlFile.delete();
221cdf0e10cSrcweir                 officeFile.delete();
222cdf0e10cSrcweir                 //JOptionPane.showMessageDialog( null, "htmlfile " + htmlFile.exists() + " officeFile " + officeFile.exists() );
223cdf0e10cSrcweir             }
224cdf0e10cSrcweir             else
225cdf0e10cSrcweir             {
226cdf0e10cSrcweir                 if( isOfficeDoc )
227cdf0e10cSrcweir                             {
228cdf0e10cSrcweir                     //System.out.println( "Removing: " + officeFile.getPath() );
229cdf0e10cSrcweir                     officeFile.delete();
230cdf0e10cSrcweir                 }
231cdf0e10cSrcweir                 else
232cdf0e10cSrcweir                 {
233cdf0e10cSrcweir                     //System.out.println( "Removing: " + htmlFile.getPath() );
234cdf0e10cSrcweir                     htmlFile.delete();
235cdf0e10cSrcweir                 }
236cdf0e10cSrcweir             }
237cdf0e10cSrcweir         }
238cdf0e10cSrcweir         catch( SecurityException se )
239cdf0e10cSrcweir         {
240cdf0e10cSrcweir             status.setStatus( 13, "Error: " + statusLine );
241cdf0e10cSrcweir             System.out.println( "Security Error while deleting temporary Document(s). Check file permissions in home directory." );
242cdf0e10cSrcweir             se.printStackTrace();
243cdf0e10cSrcweir             return false;
244cdf0e10cSrcweir         }
245cdf0e10cSrcweir         return true;
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 
cleanUpOnError()249cdf0e10cSrcweir     public void cleanUpOnError()
250cdf0e10cSrcweir     {
251cdf0e10cSrcweir         try
252cdf0e10cSrcweir         {
253cdf0e10cSrcweir             if( isOfficeDoc && isHtmlDoc )
254cdf0e10cSrcweir             {
255cdf0e10cSrcweir                 htmlFile.delete();
256cdf0e10cSrcweir                 officeFile.delete();
257cdf0e10cSrcweir             }
258cdf0e10cSrcweir             else
259cdf0e10cSrcweir             {
260cdf0e10cSrcweir                 if( isOfficeDoc )
261cdf0e10cSrcweir                             {
262cdf0e10cSrcweir                     officeFile.delete();
263cdf0e10cSrcweir                 }
264cdf0e10cSrcweir                 else
265cdf0e10cSrcweir                 {
266cdf0e10cSrcweir                     htmlFile.delete();
267cdf0e10cSrcweir                 }
268cdf0e10cSrcweir             }
269cdf0e10cSrcweir         }
270cdf0e10cSrcweir         catch( SecurityException se )
271cdf0e10cSrcweir         {
272cdf0e10cSrcweir             System.out.println( "Security Error while deleting temporary Document(s). Check file permissions in home directory." );
273cdf0e10cSrcweir             se.printStackTrace();
274cdf0e10cSrcweir         }
275cdf0e10cSrcweir     }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 
getAttachments()278cdf0e10cSrcweir     public File[] getAttachments()
279cdf0e10cSrcweir     {
280cdf0e10cSrcweir         /*
281cdf0e10cSrcweir         if( htmlFile == null && officeFile == null )
282cdf0e10cSrcweir         {
283cdf0e10cSrcweir             System.out.println( "Error: Document(s) have not been saved." );
284cdf0e10cSrcweir             return null;
285cdf0e10cSrcweir         }
286cdf0e10cSrcweir         */
287cdf0e10cSrcweir         //(officeDoc) ? (number = 2) : (number = 1);
288cdf0e10cSrcweir 
289cdf0e10cSrcweir         statusLine = "Retrieving temp docs";
290cdf0e10cSrcweir         status.setStatus( 8, statusLine );
291cdf0e10cSrcweir 
292cdf0e10cSrcweir         File attachments[] = null;
293cdf0e10cSrcweir         if( isOfficeDoc && isHtmlDoc )
294cdf0e10cSrcweir         {
295cdf0e10cSrcweir             attachments = new File[2];
296cdf0e10cSrcweir             attachments[0] = htmlFile;
297cdf0e10cSrcweir             attachments[1] = officeFile;
298cdf0e10cSrcweir         }
299cdf0e10cSrcweir         else
300cdf0e10cSrcweir         {
301cdf0e10cSrcweir             if( isOfficeDoc )
302cdf0e10cSrcweir             {
303cdf0e10cSrcweir                 attachments = new File[1];
304cdf0e10cSrcweir                 attachments[0] = officeFile;
305cdf0e10cSrcweir             }
306cdf0e10cSrcweir             else
307cdf0e10cSrcweir             {
308cdf0e10cSrcweir                 attachments = new File[1];
309cdf0e10cSrcweir                 attachments[0] = htmlFile;
310cdf0e10cSrcweir             }
311cdf0e10cSrcweir         }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir         return attachments;
314cdf0e10cSrcweir     }
315cdf0e10cSrcweir 
316cdf0e10cSrcweir 
isHtmlAttachment()317cdf0e10cSrcweir     public boolean isHtmlAttachment()
318cdf0e10cSrcweir     {
319cdf0e10cSrcweir         return isHtmlDoc;
320cdf0e10cSrcweir     }
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 
isOfficeAttachment()323cdf0e10cSrcweir     public boolean isOfficeAttachment()
324cdf0e10cSrcweir     {
325cdf0e10cSrcweir         return isOfficeDoc;
326cdf0e10cSrcweir     }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir }
329