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