1*9a1eeea9SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9a1eeea9SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9a1eeea9SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9a1eeea9SAndrew Rist  * distributed with this work for additional information
6*9a1eeea9SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9a1eeea9SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9a1eeea9SAndrew Rist  * "License"); you may not use this file except in compliance
9*9a1eeea9SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9a1eeea9SAndrew Rist  *
11*9a1eeea9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9a1eeea9SAndrew Rist  *
13*9a1eeea9SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9a1eeea9SAndrew Rist  * software distributed under the License is distributed on an
15*9a1eeea9SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9a1eeea9SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9a1eeea9SAndrew Rist  * specific language governing permissions and limitations
18*9a1eeea9SAndrew Rist  * under the License.
19*9a1eeea9SAndrew Rist  *
20*9a1eeea9SAndrew Rist  *************************************************************/
21*9a1eeea9SAndrew Rist 
22*9a1eeea9SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.setup.Controller;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.awt.Dimension;
27cdf0e10cSrcweir import org.openoffice.setup.InstallData;
28cdf0e10cSrcweir import org.openoffice.setup.Installer.Installer;
29cdf0e10cSrcweir import org.openoffice.setup.Installer.InstallerFactory;
30cdf0e10cSrcweir import org.openoffice.setup.PanelController;
31cdf0e10cSrcweir import org.openoffice.setup.Panel.ChooseDirectory;
32cdf0e10cSrcweir import org.openoffice.setup.ResourceManager;
33cdf0e10cSrcweir import org.openoffice.setup.SetupData.SetupDataProvider;
34cdf0e10cSrcweir import org.openoffice.setup.Util.InstallChangeCtrl;
35cdf0e10cSrcweir import org.openoffice.setup.Util.LogManager;
36cdf0e10cSrcweir import org.openoffice.setup.Util.SystemManager;
37cdf0e10cSrcweir import java.io.File;
38cdf0e10cSrcweir import java.util.Vector;
39cdf0e10cSrcweir import javax.swing.JDialog;
40cdf0e10cSrcweir import javax.swing.JOptionPane;
41cdf0e10cSrcweir import org.openoffice.setup.Dialogs.DatabaseDialog;
42cdf0e10cSrcweir import org.openoffice.setup.SetupData.PackageDescription;
43cdf0e10cSrcweir import org.openoffice.setup.Util.Controller;
44cdf0e10cSrcweir import org.openoffice.setup.Util.Dumper;
45cdf0e10cSrcweir import org.openoffice.setup.Util.Informer;
46cdf0e10cSrcweir import org.openoffice.setup.Util.ModuleCtrl;
47cdf0e10cSrcweir import org.openoffice.setup.Util.PackageCollector;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir public class ChooseDirectoryCtrl extends PanelController {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     private String helpFile;
52cdf0e10cSrcweir 
ChooseDirectoryCtrl()53cdf0e10cSrcweir     public ChooseDirectoryCtrl() {
54cdf0e10cSrcweir         super("ChooseDirectory", new ChooseDirectory());
55cdf0e10cSrcweir         helpFile = "String_Helpfile_ChooseDirectory";
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
getNext()58cdf0e10cSrcweir     public String getNext() {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir         InstallData data = InstallData.getInstance();
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         if ( data.olderVersionExists() ) {
63cdf0e10cSrcweir             return new String("InstallationImminent");
64cdf0e10cSrcweir         } else if ( data.sameVersionExists() ) {
65cdf0e10cSrcweir             return new String("ChooseComponents");
66cdf0e10cSrcweir         } else {
67cdf0e10cSrcweir             return new String("ChooseInstallationType");
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
getPrevious()71cdf0e10cSrcweir     public String getPrevious() {
72cdf0e10cSrcweir         InstallData data = InstallData.getInstance();
73cdf0e10cSrcweir 
74cdf0e10cSrcweir         if ( data.hideEula() ) {
75cdf0e10cSrcweir             return new String("Prologue");
76cdf0e10cSrcweir         } else {
77cdf0e10cSrcweir             return new String("AcceptLicense");
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
getHelpFileName()81cdf0e10cSrcweir     public final String getHelpFileName () {
82cdf0e10cSrcweir         return this.helpFile;
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir 
removeEndingDelimiter(String s)85cdf0e10cSrcweir     private String removeEndingDelimiter(String s) {
86cdf0e10cSrcweir         s.trim();
87cdf0e10cSrcweir         if (( s.endsWith("/")) || ( s.endsWith("\\"))) {
88cdf0e10cSrcweir             // removing the last character
89cdf0e10cSrcweir             s = s.substring(0, s.length() - 1);
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir         // mask spaces
92cdf0e10cSrcweir         // s.replaceAll(" ", "\\ ");
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         return s;
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
beforeShow()97cdf0e10cSrcweir     public void beforeShow() {
98cdf0e10cSrcweir         ChooseDirectory panel = (ChooseDirectory)getPanel();
99cdf0e10cSrcweir         InstallData data = InstallData.getInstance();
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         if ( data.getInstallDir() == null ) {
102cdf0e10cSrcweir             String installDir = data.getDefaultDir();
103cdf0e10cSrcweir 
104cdf0e10cSrcweir             if ( data.isUserInstallation() ) {
105cdf0e10cSrcweir                 // System.getenv only supported in Java 1.5, property set in shell script
106cdf0e10cSrcweir                 // if (( System.getenv("HOME") != null ) && ( ! System.getenv("HOME").equals(""))) {
107cdf0e10cSrcweir                 //     rootDir = System.getenv("HOME");
108cdf0e10cSrcweir                 // }
109cdf0e10cSrcweir                 if (( System.getProperty("HOME") != null ) && ( ! System.getProperty("HOME").equals("") )) {
110cdf0e10cSrcweir                     installDir = System.getProperty("user.home");
111cdf0e10cSrcweir                 }
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir             data.setInstallDir(installDir);
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         panel.setDirectory(data.getInstallDir());
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
afterShow(boolean nextButtonPressed)119cdf0e10cSrcweir     public boolean afterShow(boolean nextButtonPressed) {
120cdf0e10cSrcweir         boolean repeatDialog = false;
121cdf0e10cSrcweir         ChooseDirectory panel = (ChooseDirectory)getPanel();
122cdf0e10cSrcweir         String dir = panel.getDirectory();
123cdf0e10cSrcweir         dir = removeEndingDelimiter(dir);
124cdf0e10cSrcweir         InstallData data = InstallData.getInstance();
125cdf0e10cSrcweir         data.setInstallDir(dir);
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         File installDefaultDir = new File(dir, data.getDefaultDir());
128cdf0e10cSrcweir         data.setInstallDefaultDir(installDefaultDir.getPath());
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         SetupDataProvider.setNewMacro("DIR", installDefaultDir.getPath()); // important for string replacement
131cdf0e10cSrcweir         // SetupDataProvider.dumpMacros();
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         // Check existence of directory. Try to create, if it does not exist.
134cdf0e10cSrcweir         // If successufully created, calculate available disc space
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         if ( nextButtonPressed ) {
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             // If the directory exists, is has to be tested, whether the user has write access
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             if ( SystemManager.exists_directory(dir) ) {
141cdf0e10cSrcweir             	if ( ! Controller.createdSubDirectory(dir) ) {
142cdf0e10cSrcweir             	    repeatDialog= true;
143cdf0e10cSrcweir             	}
144cdf0e10cSrcweir             }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir             // If the directory does not exist, is has to be tested, whether the user can create it
147cdf0e10cSrcweir 
148cdf0e10cSrcweir             if ( ! SystemManager.exists_directory(dir)) {
149cdf0e10cSrcweir                 String title = ResourceManager.getString("String_ChooseDirectory4_Question_Title");
150cdf0e10cSrcweir                 String message = null;
151cdf0e10cSrcweir                 message = ResourceManager.getString("String_ChooseDirectory5_Question_Message");
152cdf0e10cSrcweir 
153cdf0e10cSrcweir                 int returnValue = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir                 if (  returnValue == JOptionPane.YES_OPTION ) {
156cdf0e10cSrcweir                     if ( ! Controller.createdDirectory(dir) ) {
157cdf0e10cSrcweir             	        repeatDialog= true;
158cdf0e10cSrcweir             	    }
159cdf0e10cSrcweir                 }
160cdf0e10cSrcweir                 else if (returnValue == JOptionPane.NO_OPTION) {
161cdf0e10cSrcweir                     repeatDialog = true;
162cdf0e10cSrcweir                 }
163cdf0e10cSrcweir             }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir             // Additional tasks, if the directory is okay
166cdf0e10cSrcweir 
167cdf0e10cSrcweir             if ( ! repeatDialog ) {
168cdf0e10cSrcweir                 // Calculate available disc space
169cdf0e10cSrcweir                 int discSpace = SystemManager.calculateDiscSpace(dir);
170cdf0e10cSrcweir                 data.setAvailableDiscSpace(discSpace);
171cdf0e10cSrcweir 
172cdf0e10cSrcweir                 Installer installer = InstallerFactory.getInstance();
173cdf0e10cSrcweir 
174cdf0e10cSrcweir                 // Is this a new directory, or one that was already set before.
175cdf0e10cSrcweir                 // In this case, the database does not need to be evaluated again.
176cdf0e10cSrcweir                 // -> Testing, whether the database path has changed. This can
177cdf0e10cSrcweir                 // only happen in user installations.
178cdf0e10cSrcweir                 if ( data.isUserInstallation() ) {
179cdf0e10cSrcweir                     String oldDatabasePath = data.getDatabasePath();
180cdf0e10cSrcweir                     // Setting the database path
181cdf0e10cSrcweir                     installer.defineDatabasePath();
182cdf0e10cSrcweir 
183cdf0e10cSrcweir                     if (( oldDatabasePath == null ) || ( ! oldDatabasePath.equals(data.getDatabasePath()))) {
184cdf0e10cSrcweir                         data.setDatabaseAnalyzed(false);
185cdf0e10cSrcweir                         data.setDatabaseQueried(false);
186cdf0e10cSrcweir                     } else {
187cdf0e10cSrcweir                         data.setDatabaseAnalyzed(true);
188cdf0e10cSrcweir                     }
189cdf0e10cSrcweir                 }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir                 // In installations with user privileges, now it can be controlled,
192cdf0e10cSrcweir                 // if there are products installed in the selected directory.
193cdf0e10cSrcweir                 // Therefore the directory selection dialog has to be shown before.
194cdf0e10cSrcweir                 // In installations with root privileges, this can only be checked,
195cdf0e10cSrcweir                 // before the destination directory can be set, because it is fix.
196cdf0e10cSrcweir                 if ( data.isUserInstallation() ) {
197cdf0e10cSrcweir                     LogManager.setCommandsHeaderLine("Checking change installation");
198cdf0e10cSrcweir                     InstallChangeCtrl.checkInstallChange(data);
199cdf0e10cSrcweir                     // InstallChangeCtrl.checkInstallChange(data, panel);
200cdf0e10cSrcweir                 }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir                 // At this point it is clear, whether the update-Package is installed or not.
203cdf0e10cSrcweir                 // If it is installed, it is also clear, in which version it is installed.
204cdf0e10cSrcweir                 // Therefore the following dialog is also defined.
205cdf0e10cSrcweir 
206cdf0e10cSrcweir                 if ( data.newerVersionExists() ) {
207cdf0e10cSrcweir                     // This can happen only in installation with user privileges.
208cdf0e10cSrcweir                     // Installations with root privileges have cancelled installation
209cdf0e10cSrcweir                     // already at first dialog.
210cdf0e10cSrcweir                     // Possibility to select a new directory, in which no
211cdf0e10cSrcweir                     // newer product exists
212cdf0e10cSrcweir                     String message = ResourceManager.getString("String_Newer_Version_Installed_Found") + "\n" + data.getInstallDir() + "\n" +
213cdf0e10cSrcweir                                      ResourceManager.getString("String_Newer_Version_Database") + ": " + data.getDatabasePath() + "\n" +
214cdf0e10cSrcweir                                      ResourceManager.getString("String_Newer_Version_Tip");
215cdf0e10cSrcweir                     String title = ResourceManager.getString("String_Error");
216cdf0e10cSrcweir                     Informer.showErrorMessage(message, title);
217cdf0e10cSrcweir 
218cdf0e10cSrcweir                     repeatDialog = true;
219cdf0e10cSrcweir                 }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir                 // If an older version is found, and the update is forbidden, the user
222cdf0e10cSrcweir                 // can select another installation directory.
223cdf0e10cSrcweir 
224cdf0e10cSrcweir                 if ( data.olderVersionExists() && data.dontUpdate() ) {
225cdf0e10cSrcweir                     // This can happen only in installation with user privileges.
226cdf0e10cSrcweir                     // Installations with root privileges have cancelled installation
227cdf0e10cSrcweir                     // already at first dialog (InstallChangeCtrl.java).
228cdf0e10cSrcweir                     // Possibility to select a new directory, in which no
229cdf0e10cSrcweir                     // older product exists
230cdf0e10cSrcweir                     String message = ResourceManager.getString("String_Older_Version_Installed_Found") + "\n" + data.getInstallDir() + "\n" +
231cdf0e10cSrcweir                                      ResourceManager.getString("String_Newer_Version_Database") + ": " + data.getDatabasePath() + "\n" +
232cdf0e10cSrcweir                                      ResourceManager.getString("String_Newer_Version_Tip");
233cdf0e10cSrcweir                     String title = ResourceManager.getString("String_Error");
234cdf0e10cSrcweir                     Informer.showErrorMessage(message, title);
235cdf0e10cSrcweir 
236cdf0e10cSrcweir                     repeatDialog = true;
237cdf0e10cSrcweir                 }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir                 if ( ! repeatDialog ) {
240cdf0e10cSrcweir 
241cdf0e10cSrcweir                     if ( ! data.databaseAnalyzed()) {
242cdf0e10cSrcweir                         ModuleCtrl.defaultDatabaseAnalysis(data);
243cdf0e10cSrcweir                         data.setDatabaseAnalyzed(true);
244cdf0e10cSrcweir                     }
245cdf0e10cSrcweir                 }
246cdf0e10cSrcweir             }
247cdf0e10cSrcweir         }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir         return repeatDialog;
250cdf0e10cSrcweir     }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir }
253