Author: jerome
Date: 2008-12-15 10:47:22 +0100 (Mon, 15 Dec 2008)
New Revision: 3126

Modified:
   
software_suite_v2/software/control_center/branches/new_paths/control_center/.classpath
   
software_suite_v2/software/control_center/branches/new_paths/control_center/sources/com/tuxdroid/cc/settings/Settings.java
   
software_suite_v2/software/control_center/branches/new_paths/control_center/sources/com/tuxdroid/cc/settings/XMLWriter.java
Log:
* Added backup function that store current settings file before writing changes.
* Added restore function to restore settings file from a previous backup in 
case of write errors.

Modified: 
software_suite_v2/software/control_center/branches/new_paths/control_center/.classpath
===================================================================
--- 
software_suite_v2/software/control_center/branches/new_paths/control_center/.classpath
      2008-12-15 09:29:17 UTC (rev 3125)
+++ 
software_suite_v2/software/control_center/branches/new_paths/control_center/.classpath
      2008-12-15 09:47:22 UTC (rev 3126)
@@ -9,11 +9,8 @@
        <classpathentry kind="lib" path="C:/Program 
Files/Kysoh/Tuxdroid/softwares/control_center/deps/commons-logging-1.1.1.jar"/>
        <classpathentry kind="lib" path="C:/Program 
Files/Kysoh/Tuxdroid/softwares/control_center/deps/javassist-3.7.ga.jar"/>
        <classpathentry kind="lib" path="C:/Program 
Files/Kysoh/Tuxdroid/softwares/control_center/deps/jdic_stub.jar"/>
-       <classpathentry kind="lib" path="C:/Program 
Files/Kysoh/Tuxdroid/softwares/control_center/deps/karmalab-commons-1.2.jar"/>
        <classpathentry kind="lib" path="C:/Program 
Files/Kysoh/Tuxdroid/softwares/control_center/deps/quartz-all-1.6.1-RC1.jar"/>
-       <classpathentry kind="lib" path="C:/Program 
Files/Kysoh/Tuxdroid/softwares/control_center/deps/tuxdroid-gadget-framework-0.1.jar"/>
        <classpathentry kind="lib" path="C:/Program 
Files/Kysoh/Tuxdroid/softwares/control_center/deps/tuxdroid-installer-paths.jar"/>
-       <classpathentry kind="lib" path="C:/Program 
Files/Kysoh/Tuxdroid/softwares/control_center/deps/tuxdroid-java-api-0.1.jar"/>
        <classpathentry kind="lib" path="C:/Program 
Files/Kysoh/Tuxdroid/softwares/control_center/deps/jdic.jar"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>

Modified: 
software_suite_v2/software/control_center/branches/new_paths/control_center/sources/com/tuxdroid/cc/settings/Settings.java
===================================================================
--- 
software_suite_v2/software/control_center/branches/new_paths/control_center/sources/com/tuxdroid/cc/settings/Settings.java
  2008-12-15 09:29:17 UTC (rev 3125)
+++ 
software_suite_v2/software/control_center/branches/new_paths/control_center/sources/com/tuxdroid/cc/settings/Settings.java
  2008-12-15 09:47:22 UTC (rev 3126)
@@ -22,6 +22,7 @@
 
 package com.tuxdroid.cc.settings;
 
+import java.io.File;
 import java.io.IOException;
 import java.util.Locale;
 import java.util.Vector;
@@ -29,6 +30,7 @@
 import com.kysoh.tuxdroid.gadget.framework.container.GadgetInstance;
 import com.kysoh.tuxdroid.gadget.framework.container.GadgetInstanceParameters;
 import com.tuxdroid.cc.CcCommons;
+import com.tuxdroid.cc.Utils.FileUtils;
 import com.tuxdroid.cc.gadget.GadgetObject;
 
 public class Settings {
@@ -39,7 +41,89 @@
        /** Reader object **/
        private XMLReader reader;
        /** Writer object **/
-       private XMLWriter writer;
+       private XMLWriter writer;
+       
+       /** Backup file **/
+       public static File backup;
+       
+       /**
+        * Backup settings.xml file.
+        */
+       public static boolean backup()
+       {
+               //File exists then create backup.
+               if(CcCommons.TuxDroidSettingsFile.exists())
+               {
+                       CcCommons.logger.append("Creating backup file", true);
+                       Settings.backup = new 
File(CcCommons.TuxDroidSettingsDirectory.getAbsolutePath() 
+                                                                  + 
File.separator + "settings.xml.bak");
+                       try 
+                       {
+                               CcCommons.logger.append("Copying datas to the 
backup file.", true);
+                               FileUtils.copy(CcCommons.TuxDroidSettingsFile, 
Settings.backup);
+                               //Force to parse settings file.
+                               XMLReader.edited = true;
+                               CcCommons.logger.append("Backup done!", true);
+                               return true;
+                       } 
+                       catch (IOException e) 
+                       {
+                               CcCommons.logger.append("Cannot create backup 
file.", true);
+                       }
+               }
+               else
+               {
+                       CcCommons.logger.append("Settings file doesn't exists, 
can not create a backup file", true);
+               }
+               return false;
+       }
+       
+       
+       /**
+        * Restor settings .xml file.
+        */
+       public static boolean restore()
+       {
+               
+               //File exists then restore backup.
+               if((Settings.backup != null) && (Settings.backup.exists()))
+               {
+                       CcCommons.logger.append("Restore settings.", true);
+                       if(CcCommons.TuxDroidSettingsFile.exists())
+                       {
+                               CcCommons.logger.append("Deleting old 
settings", true);
+                               CcCommons.TuxDroidSettingsFile.delete();
+                       }
+                       
+                       CcCommons.logger.append("Creating a new settings file", 
true);
+                       CcCommons.TuxDroidSettingsFile = new 
File(CcCommons.TuxDroidSettingsDirectory.getAbsolutePath()+
+                                                                               
                          File.separator + "settings.xml");
+                       
+                       try 
+                       {
+                               CcCommons.logger.append("Copying datas from 
backup to new settings file.", true);
+                               FileUtils.copy(Settings.backup, 
CcCommons.TuxDroidSettingsFile);
+                               //No the settings file has changed, force to 
parse again the new settings file.
+                               XMLReader.edited = true;
+                               CcCommons.logger.append("Restore completed.", 
true);
+                               return true;
+                       } 
+                       
+                       catch (IOException e) 
+                       {
+                               //Cannot restor backup from file, so quit 
application, a new settings file will be
+                               //created at cc startup.
+                               CcCommons.logger.append("Cannot restore from 
backup file.", true);
+                               System.exit(-1);
+                               e.printStackTrace();
+                       }
+               }
+               else
+               {
+                       CcCommons.logger.append("No backup file was found.", 
true);
+               }
+               return false;
+       }
        
        
        /**

Modified: 
software_suite_v2/software/control_center/branches/new_paths/control_center/sources/com/tuxdroid/cc/settings/XMLWriter.java
===================================================================
--- 
software_suite_v2/software/control_center/branches/new_paths/control_center/sources/com/tuxdroid/cc/settings/XMLWriter.java
 2008-12-15 09:29:17 UTC (rev 3125)
+++ 
software_suite_v2/software/control_center/branches/new_paths/control_center/sources/com/tuxdroid/cc/settings/XMLWriter.java
 2008-12-15 09:47:22 UTC (rev 3126)
@@ -157,6 +157,8 @@
         * @param document : Complete modified document.
         */
        private void registerChanges(Document document){
+               CcCommons.logger.append("Creating a backup from settings 
file.", true);
+               Settings.backup();
                CcCommons.logger.append("Registering settings changes", true);
                try {
             // Create Dom source.
@@ -180,7 +182,10 @@
                catch(Exception e)
                {
                        CcCommons.logger.appendError(e.getStackTrace());
-               e.printStackTrace();
+               e.printStackTrace();
+            CcCommons.logger.append(" -->> ERROR: An error occured trying to 
register changes.", true);
+            CcCommons.logger.append("Trying to restore from a previous backup 
file.", true);
+            Settings.restore();
         }
        }
        


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to