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