cid#1607171 Data race condition
[LibreOffice.git] / scripting / examples / java / Newsgroup / OfficeAttachment.java
blob75c0f4d458c8789b3a316c09cd74f841126628e6
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package org.libreoffice.example.java_scripts;
21 //import com.sun.star.frame.XComponentLoader;
22 import java.io.*;
23 import com.sun.star.lang.XComponent;
24 import com.sun.star.beans.PropertyValue;
25 import com.sun.star.beans.XPropertySet;
26 import com.sun.star.frame.XStorable;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.frame.XModel;
29 import com.sun.star.script.framework.runtime.XScriptContext;
31 // for debug only
32 import javax.swing.JOptionPane;
34 public class OfficeAttachment {
36 private StatusWindow status = null;
37 private XStorable storedDoc = null;
38 private File htmlFile = null;
39 private File officeFile = null;
40 private boolean isHtmlDoc = false;
41 private boolean isOfficeDoc = false;
42 private String templocationURL = "";
43 private String templocationSystem = "";
44 private String attachmentName = "";
45 private String statusLine = "";
47 public OfficeAttachment(XScriptContext xsc, StatusWindow sw, boolean html,
48 boolean office) {
49 status = sw;
50 isHtmlDoc = html;
51 isOfficeDoc = office;
53 templocationSystem = templocationURL = System.getProperty("user.home");
55 if (System.getProperty("os.name").indexOf("Windows") != -1) {
56 while (templocationURL.indexOf("\\") != -1) {
57 int sepPos = templocationURL.indexOf("\\");
58 String firstPart = templocationURL.substring(0, sepPos);
59 String lastPart = templocationURL.substring(sepPos + 1,
60 templocationURL.length());
61 templocationURL = firstPart + "/" + lastPart;
65 try {
66 statusLine = "Querying Office for current document";
67 status.setStatus(1, statusLine);
68 XScriptContext scriptcontext = xsc;
69 XModel xmodel = scriptcontext.getDocument();
70 storedDoc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xmodel);
71 // find document name from storedDoc
72 attachmentName = storedDoc.getLocation();
73 } catch (Exception e) {
74 //UNO error
75 status.setStatus(1, "Error: " + statusLine);
78 if (attachmentName.equalsIgnoreCase("")) {
79 attachmentName = "Attachment";
80 } else {
81 int lastSep = attachmentName.lastIndexOf("/");
82 attachmentName = attachmentName.substring(lastSep + 1, attachmentName.length());
83 int dot = attachmentName.indexOf(".");
84 attachmentName = attachmentName.substring(0, dot);
89 public boolean createTempDocs() {
90 String filenameURL = "file:///" + templocationURL + "/" + attachmentName;
92 try {
93 if (isHtmlDoc) {
94 statusLine = "Saving doc in HTML format";
95 status.setStatus(4, statusLine);
96 PropertyValue[] propertyvalue_html = new PropertyValue[2];
97 propertyvalue_html[0] = new PropertyValue();
98 propertyvalue_html[0].Name = "Overwrite";
99 propertyvalue_html[0].Value = Boolean.TRUE;
100 propertyvalue_html[1] = new PropertyValue();
101 propertyvalue_html[1].Name = ("FilterName");
102 propertyvalue_html[1].Value = "swriter: HTML (StarWriter)";
103 storedDoc.storeAsURL(filenameURL + ".html", propertyvalue_html);
105 File homedir = new File(templocationSystem);
106 File homefiles[] = homedir.listFiles();
107 String file = "";
109 for (int i = 0; i < homefiles.length; i++) {
110 if (homefiles[i].getName().equals(attachmentName + ".html")) {
111 file = homefiles[i].getAbsolutePath();
115 htmlFile = new File(file);
118 if (isOfficeDoc) {
119 statusLine = "Saving doc in .sxw format";
120 status.setStatus(4, statusLine);
121 PropertyValue[] propertyvalue_sxw = new PropertyValue[2];
122 propertyvalue_sxw[0] = new PropertyValue();
123 propertyvalue_sxw[0].Name = "Overwrite";
124 propertyvalue_sxw[0].Value = Boolean.TRUE;
125 propertyvalue_sxw[1] = new PropertyValue();
126 propertyvalue_sxw[1].Name = "Overwrite";
127 propertyvalue_sxw[1].Value = Boolean.TRUE;
128 storedDoc.storeAsURL(filenameURL + ".sxw", propertyvalue_sxw);
130 File homedir = new File(templocationSystem);
132 File homefiles[] = homedir.listFiles();
133 String file = "";
135 for (int i = 0; i < homefiles.length; i++) {
136 if (homefiles[i].getName().equals(attachmentName + ".sxw")) {
137 file = homefiles[i].getAbsolutePath();
141 officeFile = new File(file);
144 } catch (SecurityException se) {
145 status.setStatus(4, "Error: " + statusLine);
146 System.out.println("Security error while saving temporary Document(s). Check file permissions in home directory.");
147 se.printStackTrace();
148 htmlFile = null;
149 officeFile = null;
150 return false;
151 } catch (Exception e) {
152 status.setStatus(4, "Error: " + statusLine);
153 System.out.println("Error saving temporary Document(s)");
154 e.printStackTrace();
155 htmlFile = null;
156 officeFile = null;
157 return false;
160 return true;
164 public boolean removeTempDocs() {
166 if( !htmlFile.exists() && !officeFile.exists() )
168 System.out.println("Error: Document(s) have not been saved." );
172 statusLine = "Removing temp docs";
173 status.setStatus(13, statusLine);
175 try {
176 if (isOfficeDoc && isHtmlDoc) {
177 htmlFile.delete();
178 officeFile.delete();
179 } else {
180 if (isOfficeDoc) {
181 officeFile.delete();
182 } else {
183 htmlFile.delete();
186 } catch (SecurityException se) {
187 status.setStatus(13, "Error: " + statusLine);
188 System.out.println("Security Error while deleting temporary Document(s). Check file permissions in home directory.");
189 se.printStackTrace();
190 return false;
193 return true;
197 public void cleanUpOnError() {
198 try {
199 if (isOfficeDoc && isHtmlDoc) {
200 htmlFile.delete();
201 officeFile.delete();
202 } else {
203 if (isOfficeDoc) {
204 officeFile.delete();
205 } else {
206 htmlFile.delete();
209 } catch (SecurityException se) {
210 System.out.println("Security Error while deleting temporary Document(s). Check file permissions in home directory.");
211 se.printStackTrace();
216 public File[] getAttachments() {
218 statusLine = "Retrieving temp docs";
219 status.setStatus(8, statusLine);
221 File attachments[] = null;
223 if (isOfficeDoc && isHtmlDoc) {
224 attachments = new File[2];
225 attachments[0] = htmlFile;
226 attachments[1] = officeFile;
227 } else {
228 if (isOfficeDoc) {
229 attachments = new File[1];
230 attachments[0] = officeFile;
231 } else {
232 attachments = new File[1];
233 attachments[0] = htmlFile;
237 return attachments;
241 public boolean isHtmlAttachment() {
242 return isHtmlDoc;
246 public boolean isOfficeAttachment() {
247 return isOfficeDoc;