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