Bug 5478 fixed: Scilab could not be started with a non readable/writeable SCIHOME
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / utils / WindowsConfigurationManager.java
index 51ee9cd..3c9500a 100644 (file)
@@ -73,19 +73,30 @@ public class WindowsConfigurationManager implements XConfigurationListener {
 
     private static final String SCI = "SCI";
     private static final String WINDOWS_CONFIG_FILE = System.getenv(SCI) + "/modules/gui/etc/windowsConfiguration.xml";
-    private static final String USER_WINDOWS_CONFIG_FILE = ScilabConstants.SCIHOME.toString() + "/windowsConfiguration.xml";
+    private static final String DEFAULT_WINDOWS_CONFIG_FILE = System.getenv(SCI) + "/modules/gui/etc/integratedConfiguration.xml";
     private static final String NULLUUID = new UUID(0L, 0L).toString();
     private static final Map<SwingScilabTab, EndedRestoration> endedRestoration = new HashMap<SwingScilabTab, EndedRestoration>();
     private static final List<String> alreadyRestoredWindows = new ArrayList<String>();
     private static final Map<String, Object> defaultWinAttributes = new HashMap<String, Object>();
     private static final List<String> currentlyRestored = new ArrayList<String>();
 
+
+    private static String USER_WINDOWS_CONFIG_FILE = ScilabConstants.SCIHOME.toString() + "/windowsConfiguration.xml";
+
     private static boolean oneTry;
     private static Document doc;
 
     private static boolean mustInvalidate;
+    private static boolean mustSave = true;
 
     static {
+        if (ScilabConstants.SCIHOME != null && ScilabConstants.SCIHOME.canRead() && ScilabConstants.SCIHOME.canWrite()) {
+            USER_WINDOWS_CONFIG_FILE = ScilabConstants.SCIHOME.toString() + "/windowsConfiguration.xml";
+        } else {
+            USER_WINDOWS_CONFIG_FILE = DEFAULT_WINDOWS_CONFIG_FILE;
+            mustSave = false;
+        }
+
         new WindowsConfigurationManager();
         Runnable runnable = new Runnable() {
             public void run() {
@@ -131,10 +142,12 @@ public class WindowsConfigurationManager implements XConfigurationListener {
     public static String getLayoutFilePath() {
         try {
             Document doc = XConfiguration.getXConfigurationDocument();
-            XPath xp = XPathFactory.newInstance().newXPath();
-            NodeList nodes = (NodeList) xp.compile(LAYOUT_PATH + "/layout[@id=../@id]/@path").evaluate(doc, XPathConstants.NODESET);
-            if (nodes != null && nodes.getLength() > 0) {
-                return nodes.item(0).getNodeValue().replace("$SCI", System.getenv(SCI));
+            if (doc != null) {
+                XPath xp = XPathFactory.newInstance().newXPath();
+                NodeList nodes = (NodeList) xp.compile(LAYOUT_PATH + "/layout[@id=../@id]/@path").evaluate(doc, XPathConstants.NODESET);
+                if (nodes != null && nodes.getLength() > 0) {
+                    return nodes.item(0).getNodeValue().replace("$SCI", System.getenv(SCI));
+                }
             }
         } catch (Exception e) { }
 
@@ -145,7 +158,7 @@ public class WindowsConfigurationManager implements XConfigurationListener {
      * Create a copy of windows configuration file in the user directory
      */
     public static void createUserCopy() {
-        if (isCopyNeeded()) {
+        if (isCopyNeeded() && mustSave) {
             ScilabCommonsUtils.copyFile(new File(getLayoutFilePath()), new File(USER_WINDOWS_CONFIG_FILE));
             doc = null;
         }
@@ -180,7 +193,9 @@ public class WindowsConfigurationManager implements XConfigurationListener {
      * Write the document
      */
     private static void writeDocument() {
-        ScilabXMLUtilities.writeDocument(doc, USER_WINDOWS_CONFIG_FILE);
+        if (mustSave) {
+            ScilabXMLUtilities.writeDocument(doc, USER_WINDOWS_CONFIG_FILE);
+        }
     }
 
     /**