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