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 
23 package com.sun.star.wizards.web;
24 
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.wizards.common.FileAccess;
27 import com.sun.star.wizards.common.PropertyNames;
28 import com.sun.star.wizards.web.data.CGStyle;
29 
30 /**
31  * @author rpiterman
32  * the style preview, which is a OOo Document Preview in
33  * an Image Control.
34  * This class copies the files needed for this
35  * preview from the web wizard work directory
36  * to a given temporary directory, and updates them
37  * on request, according to the current style/background selection
38  * of the user.
39  */
40 public class StylePreview
41 {
42 
43     private FileAccess fileAccess;
44     /**
45      * the destination html url.
46      */
47     public String htmlFilename;
48     /**
49      * the destination css url
50      */
51     private String cssFilename;
52     /**
53      * destination directory
54      */
55     public String tempDir;
56     /**
57      * destination background file url
58      */
59     private String backgroundFilename;
60     /**
61      * web wizard work directory
62      */
63     private String wwRoot;
64 
65     /**
66      * copies the html file to the temp directory, and calculates the
67      * destination names of the background and css files.
68      * @param wwRoot is the root directory of the web wizard files (
69      * usually [oo]/share/template/[lang]/wizard/web
70      */
StylePreview(XMultiServiceFactory xmsf, String wwRoot_)71     public StylePreview(XMultiServiceFactory xmsf, String wwRoot_) throws Exception
72     {
73         fileAccess = new FileAccess(xmsf);
74 
75         tempDir = createTempDir(xmsf);
76 
77         htmlFilename = FileAccess.connectURLs(tempDir, "wwpreview.html");
78         cssFilename = FileAccess.connectURLs(tempDir, "style.css");
79         backgroundFilename = FileAccess.connectURLs(tempDir, "images/background.gif");
80 
81         wwRoot = wwRoot_;
82 
83         fileAccess.copy(FileAccess.connectURLs(wwRoot, "preview.html"), htmlFilename);
84     }
85 
86     /**
87      * copies the given style and background files to the temporary
88      * directory.
89      * @param style
90      * @param background
91      * @throws Exception
92      */
refresh(CGStyle style, String background)93     public void refresh(CGStyle style, String background) throws Exception
94     {
95 
96         String css = FileAccess.connectURLs(wwRoot, "styles/" + style.cp_CssHref);
97 
98         if (background == null || background.equals(PropertyNames.EMPTY_STRING))
99         {
100             //delete the background image
101             if (fileAccess.exists(backgroundFilename, false))
102             {
103                 fileAccess.delete(backgroundFilename);
104             }
105         }
106         else
107         {
108             // a solaris bug workaround
109             // TODO
110             //copy the background image to the temp directory.
111             fileAccess.copy(background, backgroundFilename);
112         }
113         //copy the actual css to the temp directory
114         fileAccess.copy(css, cssFilename);
115     }
116 
cleanup()117     public void cleanup()
118     {
119         try
120         {
121             removeTempDir();
122         }
123         catch (Exception ex)
124         {
125             ex.printStackTrace();
126         }
127     }
128 
129     /**
130      * creates a temporary directory.
131      * @param xmsf
132      * @return the url of the new directory.
133      * @throws Exception
134      */
createTempDir(XMultiServiceFactory xmsf)135     private String createTempDir(XMultiServiceFactory xmsf) throws Exception
136     {
137         String tempPath = FileAccess.getOfficePath(xmsf, "Temp", PropertyNames.EMPTY_STRING, PropertyNames.EMPTY_STRING);
138         String s = fileAccess.createNewDir(tempPath, "wwiz");
139         fileAccess.createNewDir(s, "images");
140         return s;
141     }
142 
143     /**
144      * deletes/removes the temporary directroy.
145      * @throws Exception
146      */
removeTempDir()147     private void removeTempDir() throws Exception
148     {
149         fileAccess.delete(tempDir);
150     }
151 }
152