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 
24 package org.openoffice.netbeans.modules.office.actions;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.beans.PropertyVetoException;
29 import java.util.Enumeration;
30 import java.util.Calendar;
31 
32 import org.openide.TopManager;
33 import org.openide.NotifyDescriptor;
34 import org.openide.windows.OutputWriter;
35 import org.openide.windows.InputOutput;
36 
37 import org.openide.ErrorManager;
38 import org.openide.nodes.Node;
39 import org.openide.filesystems.Repository;
40 import org.openide.filesystems.FileSystem;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.filesystems.FileEvent;
44 
45 import org.openoffice.idesupport.zip.ParcelZipper;
46 
47 import org.openoffice.netbeans.modules.office.options.OfficeSettings;
48 import org.openoffice.netbeans.modules.office.utils.NagDialog;
49 import org.openoffice.netbeans.modules.office.utils.ZipMounter;
50 import org.openoffice.netbeans.modules.office.utils.ManifestParser;
51 
52 public class ParcelSupport implements ParcelCookie
53 {
54     private FileObject fo;
55     private ParcelZipper zipper = ParcelZipper.getParcelZipper();
56     private String language = null;
57 
ParcelSupport(FileObject fo)58     public ParcelSupport(FileObject fo) {
59         this.fo = fo;
60     }
61 
getFile()62     public File getFile() {
63         return FileUtil.toFile(fo);
64     }
65 
getLanguage()66     public String getLanguage() {
67         if (language == null) {
68             try {
69                 language = zipper.getParcelLanguage(getFile());
70             }
71             catch (IOException ioe) {
72                 return null;
73             }
74         }
75         return language;
76     }
77 
mount()78     public void mount()
79     {
80         File parcel = FileUtil.toFile(fo);
81 
82         if (parcel != null) {
83             try {
84                 ZipMounter.getZipMounter().mountZipFile(parcel);
85             }
86             catch (IOException ioe) {
87                 ErrorManager.getDefault().notify(ioe);
88             }
89             catch (PropertyVetoException pve) {
90                 ErrorManager.getDefault().notify(pve);
91             }
92         }
93     }
94 
deploy(File target)95     public boolean deploy(File target) {
96         File source = FileUtil.toFile(fo);
97 
98         if (!target.isDirectory()) {
99             OfficeSettings settings = OfficeSettings.getDefault();
100             String message = "If you already have this document open in " +
101                 "Office, please close it before continuing. Click OK to " +
102                 "continue deployment.";
103 
104             if (settings.getWarnBeforeDocDeploy() == true) {
105                 NagDialog warning = NagDialog.createConfirmationDialog(
106                     message, "Show this message in future", true);
107 
108                 boolean result = warning.show();
109 
110                 if (warning.getState() == false)
111                     settings.setWarnBeforeDocDeploy(false);
112 
113                 if (result == false)
114                     return false;
115             }
116         }
117 
118         OutputWriter out =
119             getOutputWindowWriter(fo.getName() + " (deploying)");
120 
121         try {
122             if (zipper.isOverwriteNeeded(source, target) == true)
123                 if (promptForOverwrite(source, target) == false)
124                     return false;
125         }
126         catch (IOException ioe) {
127            out.println("DEPLOYMENT FAILED: reason: " +
128                ioe.getClass().getName() + ": "+ ioe.getMessage());
129            return false;
130         }
131 
132         try {
133             out.println("Deploying: " + fo.getName() +
134                 "\nTo: " + target.getAbsolutePath(), null);
135 
136             zipper.deployParcel(source, target);
137 
138             out.println("\nDEPLOYMENT SUCCESSFUL.");
139 
140             FileObject[] fileobjs = FileUtil.fromFile(target);
141             if (fileobjs != null) {
142                 for (int i = 0; i < fileobjs.length; i++)
143                     fileobjs[i].refresh(true);
144             }
145         }
146         catch (IOException ioe) {
147            out.println("DEPLOYMENT FAILED: reason: " +
148                ioe.getClass().getName() + ": "+ ioe.getMessage());
149            return false;
150         }
151         finally {
152             if( out != null)
153                 out.close();
154         }
155         return true;
156     }
157 
getOutputWindowWriter(String title)158     public static OutputWriter getOutputWindowWriter(String title) {
159         InputOutput io = TopManager.getDefault().getIO(title, false);
160         io.setFocusTaken(true);
161         io.setOutputVisible(true);
162 
163         OutputWriter out = io.getOut();
164         try {
165             out.reset();
166         }
167         catch( IOException e) {
168             e.printStackTrace(System.err);
169         }
170         out.println(Calendar.getInstance().getTime() + ":\n");
171         return out;
172     }
173 
promptForOverwrite(File source, File target)174     private boolean promptForOverwrite(File source, File target) {
175         String message = source.getName() + " has already been deployed " +
176             "to this target. Do you wish to overwrite it?";
177 
178         NotifyDescriptor d = new NotifyDescriptor.Confirmation(
179             message, NotifyDescriptor.OK_CANCEL_OPTION);
180         TopManager.getDefault().notify(d);
181 
182         if (d.getValue() == NotifyDescriptor.CANCEL_OPTION)
183             return false;
184         else
185             return true;
186     }
187 }
188