summaryrefslogtreecommitdiff
path: root/scripting/examples/java/Newsgroup/OfficeAttachment.java
blob: 8e36d1fb47f1e562120e87114dc64b45f105652a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package org.libreoffice.example.java_scripts;

//import com.sun.star.frame.XComponentLoader;
import java.io.*;
import com.sun.star.lang.XComponent;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XStorable;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XModel;
import com.sun.star.script.framework.runtime.XScriptContext;

// for debug only
import javax.swing.JOptionPane;

public class OfficeAttachment
{

    private StatusWindow status = null;
    private XStorable storedDoc = null;
    private File htmlFile = null;
    private File officeFile = null;
    private boolean isHtmlDoc = false;
    private boolean isOfficeDoc = false;
    private String templocationURL = "";
    private String templocationSystem = "";
    private String attachmentName = "";
    private String statusLine = "";

    public OfficeAttachment( XScriptContext xsc, StatusWindow sw, boolean html, boolean office )
    {
        status = sw;
        isHtmlDoc = html;
        isOfficeDoc = office;

        templocationSystem = templocationURL = System.getProperty( "user.home" );
        if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
        {
            while( templocationURL.indexOf( "\\" ) != -1 )
            {
                int sepPos = templocationURL.indexOf( "\\" );
                String firstPart = templocationURL.substring( 0, sepPos );
                String lastPart = templocationURL.substring( sepPos + 1, templocationURL.length() );
                templocationURL = firstPart + "/" + lastPart;
            }
        }

        try
        {
            statusLine = "Querying Office for current document";
            status.setStatus( 1, statusLine );
            XScriptContext scriptcontext = xsc;
            XModel xmodel = scriptcontext.getDocument();
            storedDoc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xmodel);
            // find document name from storedDoc
            attachmentName = storedDoc.getLocation();
        }
        catch( Exception e )
        {
            //UNO error
            status.setStatus( 1, "Error: " + statusLine );
        }

        if( attachmentName.equalsIgnoreCase( "" ) )
        {
            attachmentName = "Attachment";
        }
        else
        {
            int lastSep = attachmentName.lastIndexOf( "/" );
            attachmentName = attachmentName.substring( lastSep + 1, attachmentName.length() );
            int dot = attachmentName.indexOf( "." );
            attachmentName = attachmentName.substring( 0, dot );
        }
    }


    public boolean createTempDocs()
    {
        String filenameURL = "file:///" + templocationURL +  "/" + attachmentName;
        try
        {
            if( isHtmlDoc )
            {
                statusLine = "Saving doc in HTML format";
                status.setStatus( 4, statusLine );
                       PropertyValue[] propertyvalue_html = new PropertyValue[2];
                    propertyvalue_html[0] = new PropertyValue();
                        propertyvalue_html[0].Name = new String("Overwrite");
                    propertyvalue_html[0].Value = Boolean.TRUE;
                       propertyvalue_html[1] = new PropertyValue();
                    propertyvalue_html[1].Name = ("FilterName");
                    propertyvalue_html[1].Value = new String("swriter: HTML (StarWriter)");
                storedDoc.storeAsURL( filenameURL + ".html", propertyvalue_html);

                File homedir = new File( templocationSystem );
                File homefiles[] = homedir.listFiles();
                String file = "";
                for(int i=0; i < homefiles.length; i++ )
                {
                    if( homefiles[i].getName().equals( attachmentName + ".html" ) )
                    {
                        file = homefiles[i].getAbsolutePath();
                    }
                }
                htmlFile = new File( file );
            }

            if( isOfficeDoc )
            {
                statusLine = "Saving doc in .sxw format";
                status.setStatus( 4, statusLine );
                PropertyValue[] propertyvalue_sxw = new PropertyValue[2];
                    propertyvalue_sxw[0] = new PropertyValue();
                    propertyvalue_sxw[0].Name = new String("Overwrite");
                    propertyvalue_sxw[0].Value = Boolean.TRUE;
                    propertyvalue_sxw[1] = new PropertyValue();
                    propertyvalue_sxw[1].Name = new String("Overwrite");
                    propertyvalue_sxw[1].Value = Boolean.TRUE;
                    storedDoc.storeAsURL( filenameURL + ".sxw", propertyvalue_sxw);

                File homedir = new File( templocationSystem );

                                File homefiles[] = homedir.listFiles();
                String file = "";
                                for(int i=0; i < homefiles.length; i++ )
                                {
                                        if( homefiles[i].getName().equals( attachmentName + ".sxw" ) )
                                        {
                        file = homefiles[i].getAbsolutePath();
                                        }
                                }
                officeFile = new File( file );
            }

        }
        catch( SecurityException se )
        {
            status.setStatus( 4, "Error: " + statusLine );
            System.out.println( "Security error while saving temporary Document(s). Check file permissions in home directory." );
            se.printStackTrace();
            htmlFile = null;
            officeFile = null;
            return false;
        }
        catch( Exception e )
        {
            status.setStatus( 4, "Error: " + statusLine );
            System.out.println( "Error saving temporary Document(s)" );
            e.printStackTrace();
            htmlFile = null;
            officeFile = null;
            return false;
        }
        return true;
    }


    public boolean removeTempDocs()
    {
        /*
        if( !htmlFile.exists() && !officeFile.exists() )
                {
            System.out.println("Error: Document(s) have not been saved." );
        }
        */

        statusLine = "Removing temp docs";
        status.setStatus( 13, statusLine );

        try
        {
            if( isOfficeDoc && isHtmlDoc )
            {
                htmlFile.delete();
                officeFile.delete();
            }
            else
            {
                if( isOfficeDoc )
                               {
                    officeFile.delete();
                }
                else
                {
                    htmlFile.delete();
                }
            }
        }
        catch( SecurityException se )
        {
            status.setStatus( 13, "Error: " + statusLine );
            System.out.println( "Security Error while deleting temporary Document(s). Check file permissions in home directory." );
            se.printStackTrace();
            return false;
        }
        return true;
    }


    public void cleanUpOnError()
    {
        try
        {
            if( isOfficeDoc && isHtmlDoc )
            {
                htmlFile.delete();
                officeFile.delete();
            }
            else
            {
                if( isOfficeDoc )
                               {
                    officeFile.delete();
                }
                else
                {
                    htmlFile.delete();
                }
            }
        }
        catch( SecurityException se )
        {
            System.out.println( "Security Error while deleting temporary Document(s). Check file permissions in home directory." );
            se.printStackTrace();
        }
    }


    public File[] getAttachments()
    {

        statusLine = "Retrieving temp docs";
        status.setStatus( 8, statusLine );

        File attachments[] = null;
        if( isOfficeDoc && isHtmlDoc )
        {
            attachments = new File[2];
            attachments[0] = htmlFile;
            attachments[1] = officeFile;
        }
        else
        {
            if( isOfficeDoc )
            {
                attachments = new File[1];
                attachments[0] = officeFile;
            }
            else
            {
                attachments = new File[1];
                attachments[0] = htmlFile;
            }
        }

        return attachments;
    }


    public boolean isHtmlAttachment()
    {
        return isHtmlDoc;
    }


    public boolean isOfficeAttachment()
    {
        return isOfficeDoc;
    }

}