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