home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 2010 Software/Programs / PCGuia_programas.iso / Software / Utils / Livebrush / Install-LivebrushLite.air / livebrush.swf / scripts / com / livebrush / data / FileManager.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  54.6 KB  |  1,484 lines

  1. package com.livebrush.data
  2. {
  3.    import com.adobe.images.PNGEncoder;
  4.    import com.livebrush.Main;
  5.    import com.livebrush.events.FileEvent;
  6.    import com.livebrush.graphics.canvas.Canvas;
  7.    import com.livebrush.styles.DecoAsset;
  8.    import com.livebrush.ui.UI;
  9.    import flash.data.EncryptedLocalStore;
  10.    import flash.desktop.NativeApplication;
  11.    import flash.display.BitmapData;
  12.    import flash.display.Loader;
  13.    import flash.events.Event;
  14.    import flash.events.EventDispatcher;
  15.    import flash.events.IOErrorEvent;
  16.    import flash.filesystem.*;
  17.    import flash.net.FileFilter;
  18.    import flash.net.URLLoader;
  19.    import flash.net.URLLoaderDataFormat;
  20.    import flash.net.URLRequest;
  21.    import flash.utils.ByteArray;
  22.    import flash.utils.setTimeout;
  23.    import org.casalib.util.NavigateUtil;
  24.    
  25.    public class FileManager extends EventDispatcher
  26.    {
  27.       private static var singleton:FileManager;
  28.       
  29.       public static const PROJECT:String = "project";
  30.       
  31.       public static const DECOSET:String = "decoList";
  32.       
  33.       public static const STYLE:String = "style";
  34.       
  35.       public static const DECO:String = "deco";
  36.       
  37.       public static const LAYER_IMAGE:String = "layerImage";
  38.       
  39.       public static const INPUT_SWF:String = "inputSWF";
  40.       
  41.       public static const SWF:String = "SWF";
  42.       
  43.       public static const SVG:String = "SVG";
  44.       
  45.       public static const ONLINE_VERSION_PATH:String = "http://www.livebrush.com/version.txt";
  46.       
  47.       private var _recentOpenDir:String = "";
  48.       
  49.       private var _bitmapData:BitmapData;
  50.       
  51.       private var showVersionCheckResult:Boolean = false;
  52.       
  53.       private var svgData:XML;
  54.       
  55.       private var help:Help;
  56.       
  57.       public var projectStyles:XML;
  58.       
  59.       private var appSettings:Settings;
  60.       
  61.       private var _projectIsTemp:Boolean;
  62.       
  63.       private var _canvasSizeIndex:int = -1;
  64.       
  65.       public var layerData:XML;
  66.       
  67.       private var decoFilesLoaded:Array;
  68.       
  69.       private var activeFile:File;
  70.       
  71.       private var tempProjectDir:String;
  72.       
  73.       private var decoAssetsLoaded:Array;
  74.       
  75.       private var firstRun:Boolean;
  76.       
  77.       public var decoSetData:XML;
  78.       
  79.       public var lastFileLoaded:String;
  80.       
  81.       private var sessionProjectDir:String;
  82.       
  83.       public var styleData:XML;
  84.       
  85.       private var _recentSaveDir:String = "";
  86.       
  87.       public function FileManager()
  88.       {
  89.          super();
  90.          this.help = Help.getInstance(this);
  91.       }
  92.       
  93.       public static function removeWhiteSpace(str:String, replace:String = "-") : String
  94.       {
  95.          return str.split(" ").join(replace);
  96.       }
  97.       
  98.       public static function loadAsset(path:String, listener:Function = null) : Loader
  99.       {
  100.          return FileManager.getInstance()._loadMedia(path,listener);
  101.       }
  102.       
  103.       public static function getInstance() : FileManager
  104.       {
  105.          var instance:FileManager = null;
  106.          if(singleton == null)
  107.          {
  108.             singleton = new FileManager();
  109.             instance = singleton;
  110.          }
  111.          else
  112.          {
  113.             instance = singleton;
  114.          }
  115.          return instance;
  116.       }
  117.       
  118.       public static function loadLayerImage(path:String = "", completeListener:Function = null, errorListener:Function = null) : Loader
  119.       {
  120.          return loadImage(FileManager.getInstance().currentProjectDir + "/Layer Images/" + path,completeListener,errorListener);
  121.       }
  122.       
  123.       public static function loadImage(path:String = "", completeListener:Function = null, errorListener:Function = null) : Loader
  124.       {
  125.          return FileManager.getInstance()._loadMedia(path,completeListener,errorListener);
  126.       }
  127.       
  128.       public static function createNameDup(list:Array = null, name:String = null, prop:String = null) : String
  129.       {
  130.          var dup:int = 0;
  131.          var rootName:String = name;
  132.          for(var i:int = 0; i < list.length; i++)
  133.          {
  134.             if(prop == null)
  135.             {
  136.                if(list[i] == name)
  137.                {
  138.                   dup++;
  139.                }
  140.             }
  141.             else if(list[i][prop] == name)
  142.             {
  143.                dup++;
  144.             }
  145.          }
  146.          if(dup > 0)
  147.          {
  148.             do
  149.             {
  150.                name = rootName + " (" + (dup + 1) + ")";
  151.                dup++;
  152.             }
  153.             while(name != createNameDup(list,name,prop));
  154.             
  155.          }
  156.          return name;
  157.       }
  158.       
  159.       public static function copyFileToUnique(fromFile:File, toFile:File, dupNameSuffix:String = "") : String
  160.       {
  161.          var newFile:File = null;
  162.          if(toFile.exists)
  163.          {
  164.             newFile = getUniqueFile(toFile);
  165.             fromFile.copyTo(newFile,true);
  166.             toFile = newFile;
  167.          }
  168.          else
  169.          {
  170.             fromFile.copyTo(toFile,true);
  171.          }
  172.          return toFile.name;
  173.       }
  174.       
  175.       public static function getUniqueFile(file:File, dupNameSuffix:String = "") : File
  176.       {
  177.          var dirFileNames:Array = null;
  178.          var list:Array = null;
  179.          var i:uint = 0;
  180.          var newFileName:String = file.name;
  181.          if(file.exists)
  182.          {
  183.             dirFileNames = [];
  184.             list = file.parent.getDirectoryListing();
  185.             for(i = 0; i < list.length; dirFileNames.push(FileManager.getInstance().removeExtension(list[i].name)),i++)
  186.             {
  187.             }
  188.             newFileName = FileManager.getInstance().createFileNameDup(dirFileNames,file);
  189.          }
  190.          return new File(file.parent.nativePath + "/" + newFileName);
  191.       }
  192.       
  193.       public static function get missingLayerFile() : File
  194.       {
  195.          return File.applicationDirectory.resolvePath("AppFiles/Assets/Layer Images/missingLayer.jpg");
  196.       }
  197.       
  198.       public static function checkForUpdates(showResult:Boolean = false) : void
  199.       {
  200.          getInstance().showVersionCheckResult = showResult;
  201.          FileManager.getInstance().checkVersionOnline();
  202.       }
  203.       
  204.       public static function getURL(s:String) : void
  205.       {
  206.          NavigateUtil.openUrl(s);
  207.       }
  208.       
  209.       private function openDecoListener(e:Event) : void
  210.       {
  211.          this.activeFile.removeEventListener(Event.SELECT,this.openDecoListener);
  212.          this.importDeco(File(e.target).nativePath,this.initDecoImportListener);
  213.       }
  214.       
  215.       public function exportImage(file:File) : void
  216.       {
  217.          if(file.extension != null)
  218.          {
  219.             file = file.resolvePath(this.removeExtension(file.nativePath));
  220.          }
  221.          file = new File(file.nativePath + ".png");
  222.          try
  223.          {
  224.             this.writeImage(this._bitmapData,file);
  225.          }
  226.          catch(e:Error)
  227.          {
  228.             UI.MAIN_UI.alert({
  229.                "message":"Error Saving: Out of memory.\nPlease close any other applications and try again.",
  230.                "id":"saveImageError"
  231.             });
  232.          }
  233.          finally
  234.          {
  235.             this._bitmapData.dispose();
  236.          }
  237.       }
  238.       
  239.       private function saveStyleListener(e:Event) : void
  240.       {
  241.          e.target.removeEventListener(Event.SELECT,this.saveStyleListener);
  242.          this.exportStyle(e.target as File);
  243.       }
  244.       
  245.       public function importStyle(path:String = "", dispatch:Boolean = true) : String
  246.       {
  247.          var styleData:String = null;
  248.          var styleXML:XML = null;
  249.          var decoChild:String = null;
  250.          var inputSWFFile:File = null;
  251.          var decoPath:String = null;
  252.          var sourceDecoFile:File = null;
  253.          var assetLoaded:String = null;
  254.          this.lastFileLoaded = STYLE;
  255.          styleData = this.loadXML(path);
  256.          styleXML = new XML(styleData);
  257.          var decoSetXML:XML = new XML(<decoList></decoList>);
  258.          if(styleXML.name() != STYLE)
  259.          {
  260.             dispatchEvent(new FileEvent(FileEvent.WRONG_FILE,true,false,FileEvent.OPEN,STYLE,styleXML.name()));
  261.          }
  262.          else
  263.          {
  264.             if(styleXML.line.inputSWF.length() > 0 && styleXML.line.inputSWF.toString() != "" && styleXML.line.inputSWF.toString() != "null")
  265.             {
  266.                inputSWFFile = new File().resolvePath(new File(path).parent.nativePath + "/Assets/" + styleXML.line.inputSWF);
  267.                styleXML.line.inputSWF = this.importInputSWF(inputSWFFile.nativePath,null,false);
  268.             }
  269.             for(decoChild in styleXML.deco.decoList.deco)
  270.             {
  271.                decoPath = styleXML.deco.decoList.deco[decoChild].@value;
  272.                sourceDecoFile = new File();
  273.                sourceDecoFile = sourceDecoFile.resolvePath(new File(path).parent.nativePath + "/Assets/" + decoPath);
  274.                assetLoaded = this.importDeco(sourceDecoFile.nativePath);
  275.                if(assetLoaded != FileEvent.FILE_NOT_FOUND)
  276.                {
  277.                   decoSetXML.appendChild(<deco value={assetLoaded}></deco>);
  278.                }
  279.             }
  280.             styleXML.deco.decoList = decoSetXML;
  281.             if(dispatch)
  282.             {
  283.                dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,FileEvent.OPEN,STYLE,styleXML.toString()));
  284.             }
  285.          }
  286.          return styleXML;
  287.       }
  288.       
  289.       private function get currentProjectDir() : String
  290.       {
  291.          return this.sessionProjectDir;
  292.       }
  293.       
  294.       public function importDecoSet(path:String = "", listener:Function = null) : void
  295.       {
  296.          var decoSetData:String = null;
  297.          var decoSetXML:XML = null;
  298.          var decoChild:String = null;
  299.          var decoPath:String = null;
  300.          var sourceDecoFile:File = null;
  301.          this.lastFileLoaded = DECOSET;
  302.          decoSetData = this.loadXML(path);
  303.          decoSetXML = new XML(decoSetData);
  304.          if(decoSetXML.name() != DECOSET)
  305.          {
  306.             dispatchEvent(new FileEvent(FileEvent.WRONG_FILE,true,false,FileEvent.OPEN,DECOSET,decoSetXML.name()));
  307.          }
  308.          else
  309.          {
  310.             for(decoChild in decoSetXML.deco)
  311.             {
  312.                decoPath = decoSetXML.deco[decoChild].@value;
  313.                sourceDecoFile = new File(new File(path).parent.nativePath + "/Assets/" + decoPath);
  314.                this.importDeco(sourceDecoFile.nativePath,listener);
  315.             }
  316.          }
  317.       }
  318.       
  319.       private function saveDecoSetListener(e:Event) : void
  320.       {
  321.          e.target.removeEventListener(Event.SELECT,this.saveDecoSetListener);
  322.          this.exportDecoSet(e.target as File);
  323.       }
  324.       
  325.       private function initOpenFile(path:String, title:String, listener:Function, fileFilters:Array) : void
  326.       {
  327.          if(path != "")
  328.          {
  329.             this.activeFile = new File(path).parent;
  330.          }
  331.          else
  332.          {
  333.             this.activeFile = new File(this._recentOpenDir == "" ? this.getRecentProjectPath() : this._recentOpenDir).parent;
  334.          }
  335.          this.activeFile.browseForOpen(title,fileFilters);
  336.          this.activeFile.addEventListener(Event.SELECT,this.setRecentOpenDir);
  337.          this.activeFile.addEventListener(Event.SELECT,listener);
  338.       }
  339.       
  340.       public function openDeco(path:String = null) : void
  341.       {
  342.          path = path == null ? this.openRecentProjectDir : path;
  343.          this.initOpenFile(path,"Import Decoration",this.openDecoListener,[new FileFilter("All Formats (*.jpg,*.gif,*.png,*.swf)","*.jpg;*.gif;*.png;*.swf","JPEG;jp2_;GIFF;SWFL")]);
  344.       }
  345.       
  346.       public function removeExtension(fileName:String) : String
  347.       {
  348.          return fileName.substring(0,fileName.lastIndexOf("."));
  349.       }
  350.       
  351.       public function importInputSWF(path:String = "", listener:Function = null, dispatch:Boolean = true) : String
  352.       {
  353.          var newFile:* = undefined;
  354.          var fileName:String = null;
  355.          var swfFile:File = new File(path);
  356.          var newName:String = swfFile.name;
  357.          if(swfFile.exists)
  358.          {
  359.             newFile = new File(this.currentProjectDir + "/Styles/Assets/" + swfFile.name);
  360.             fileName = copyFileToUnique(swfFile,newFile);
  361.             newFile = new File(this.currentProjectDir + "/Styles/Assets/" + fileName);
  362.             newName = newFile.name;
  363.             this.decoAssetsLoaded.push({
  364.                "fileName":newName,
  365.                "asset":null
  366.             });
  367.             if(dispatch)
  368.             {
  369.                dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,FileEvent.OPEN,INPUT_SWF,newFile));
  370.             }
  371.             return newName;
  372.          }
  373.          return "";
  374.       }
  375.       
  376.       private function _loadMedia(path:String, completeListener:Function = null, errorListener:Function = null) : Loader
  377.       {
  378.          var file:File = new File(path);
  379.          var loader:Loader = new Loader();
  380.          if(completeListener != null)
  381.          {
  382.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeListener);
  383.          }
  384.          if(errorListener != null)
  385.          {
  386.             loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorListener);
  387.          }
  388.          if(file.exists)
  389.          {
  390.             try
  391.             {
  392.                loader.load(new URLRequest(file.url));
  393.             }
  394.             catch(error:Error)
  395.             {
  396.             }
  397.             return loader;
  398.          }
  399.          loader.load(new URLRequest(file.url));
  400.          return loader;
  401.       }
  402.       
  403.       public function decoLoader(fileName:String) : Loader
  404.       {
  405.          this.decoFilesLoaded.push(fileName);
  406.          return this._loadMedia(this.currentProjectDir + "/Styles/Assets/" + fileName);
  407.       }
  408.       
  409.       private function exportDecoSet(file:File, createDirectory:Boolean = true) : File
  410.       {
  411.          var decoDestFile:File;
  412.          var decoSourceFile:File = null;
  413.          var decoChild:String = null;
  414.          if(createDirectory)
  415.          {
  416.             if(file.extension != null)
  417.             {
  418.                file = file.resolvePath(this.removeExtension(file.nativePath));
  419.             }
  420.             file.createDirectory();
  421.             file = new File(file.nativePath + "/" + file.name);
  422.             if(!file.extension || file.extension.toLowerCase() != "xml")
  423.             {
  424.                file.nativePath += ".xml";
  425.             }
  426.          }
  427.          decoDestFile = new File(file.parent.nativePath + "/Assets/" + file.name);
  428.          for(decoChild in this.decoSetData.deco)
  429.          {
  430.             decoSourceFile = new File(this.currentProjectDir + "/Styles/Assets/" + this.decoSetData.deco[decoChild].@value);
  431.             decoDestFile = new File(file.parent.nativePath + "/Assets/" + decoSourceFile.name);
  432.             try
  433.             {
  434.                decoSourceFile.copyTo(decoDestFile);
  435.             }
  436.             catch(e:Error)
  437.             {
  438.             }
  439.          }
  440.          if(createDirectory)
  441.          {
  442.             this.save(file,this.decoSetData);
  443.          }
  444.          return decoDestFile.parent;
  445.       }
  446.       
  447.       private function checkVersionOnline() : void
  448.       {
  449.          var versionLoader:URLLoader = new URLLoader();
  450.          versionLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
  451.          versionLoader.addEventListener(Event.COMPLETE,this.versionCheckComplete);
  452.          versionLoader.load(new URLRequest(ONLINE_VERSION_PATH));
  453.       }
  454.       
  455.       private function exportSVG(file:File) : void
  456.       {
  457.          if(!file.extension || file.extension.toLowerCase() != "xml")
  458.          {
  459.             file.nativePath += ".svg";
  460.          }
  461.          this.save(file,this.svgData);
  462.       }
  463.       
  464.       public function deleteSessionProject() : void
  465.       {
  466.          var sessionDir:File = new File(this.sessionProjectDir);
  467.          if(sessionDir.exists)
  468.          {
  469.             sessionDir.deleteDirectory(true);
  470.          }
  471.          this.saveLocalString("sessionProjectDir","deleted");
  472.       }
  473.       
  474.       public function saveDecoSet(decoSet:XML) : void
  475.       {
  476.          this.decoSetData = decoSet;
  477.          this.initSaveFile("Export Decoration Set",this.saveDecoSetListener);
  478.       }
  479.       
  480.       private function writeProject(file:File) : void
  481.       {
  482.          var recentProjectFile:File = null;
  483.          var newProjectFile:File = null;
  484.          var projectXML:XML = null;
  485.          var style:String = null;
  486.          var styleFile:File = null;
  487.          try
  488.          {
  489.             if(file.extension != null)
  490.             {
  491.                file = file.resolvePath(this.removeExtension(file.nativePath));
  492.             }
  493.             file.createDirectory();
  494.             recentProjectFile = new File(this.recentProject);
  495.             newProjectFile = recentProjectFile.name != file.name + ".lbp" ? this.renameFile(new File(this.currentProject),file.name + ".lbp") : new File(this.currentProject);
  496.             projectXML = new XML(<LiveBrushProject size={Canvas.SIZE_INDEX} width={Canvas.WIDTH} height={Canvas.HEIGHT} name={file.name} version="1"></LiveBrushProject>);
  497.             projectXML.appendChild(<styles></styles>);
  498.             projectXML.appendChild(this.layerData);
  499.             for(style in this.projectStyles.style)
  500.             {
  501.                styleFile = new File(this.currentProjectDir).resolvePath(this.currentProjectDir + "/Styles/" + this.projectStyles.style[style].@name + ".xml");
  502.                this.save(styleFile,this.projectStyles.style[style].toString());
  503.                projectXML.styles.appendChild(<style xml={styleFile.name}/>);
  504.             }
  505.             this.save(newProjectFile,projectXML.toString());
  506.             new File(this.currentProjectDir).copyTo(file,true);
  507.             this.setProject(file.resolvePath(file.name + ".lbp"));
  508.             this.deleteTempProject();
  509.             dispatchEvent(new FileEvent(FileEvent.SAVE,true,false,FileEvent.PROJECT_SAVED,PROJECT));
  510.          }
  511.          catch(error:Error)
  512.          {
  513.             writeProjectBackup();
  514.             dispatchEvent(new FileEvent(FileEvent.IO_ERROR,true,false,FileEvent.SAVE,PROJECT));
  515.          }
  516.          UI.setStatus("Ready");
  517.       }
  518.       
  519.       public function saveStyle(style:XML) : void
  520.       {
  521.          this.styleData = style;
  522.          this.initSaveFile("Export Style",this.saveStyleListener);
  523.       }
  524.       
  525.       private function checkValidSavePath(e:Event) : void
  526.       {
  527.          var file:File = e.target as File;
  528.          var projectDir:File = new File(this.recentProjectDir);
  529.          var layersDir:File = new File(this.recentProjectDir + "/Layer Images");
  530.          var stylesDir:File = new File(this.recentProjectDir + "/Styles");
  531.          var styleAssetsDir:File = new File(this.recentProjectDir + "/Styles/Assets");
  532.          if(file.parent.nativePath == projectDir.nativePath || file.parent.nativePath == layersDir.nativePath || file.parent.nativePath == stylesDir.nativePath || file.parent.nativePath == styleAssetsDir.nativePath)
  533.          {
  534.             e.stopImmediatePropagation();
  535.             UI.MAIN_UI.alert({
  536.                "message":"Save Location Error\nYou can\'t save to a project folder (or its subfolders). Please save to another location.",
  537.                "id":"saveLocationAlert"
  538.             });
  539.          }
  540.          else
  541.          {
  542.             this._recentOpenDir = e.target.nativePath;
  543.          }
  544.       }
  545.       
  546.       private function get recentProject() : String
  547.       {
  548.          return this.loadLocalString("recentProject");
  549.       }
  550.       
  551.       private function compareVersion(s:String = null) : String
  552.       {
  553.          var storedVersion:String = null;
  554.          var pointVersion:Array = null;
  555.          var pointStoredVersion:Array = null;
  556.          var version:String = "newer";
  557.          if(s == null)
  558.          {
  559.             storedVersion = this.loadLocalString("version");
  560.          }
  561.          else
  562.          {
  563.             storedVersion = s;
  564.          }
  565.          if(storedVersion != null)
  566.          {
  567.             pointVersion = this.appSettings.version.split(".");
  568.             pointStoredVersion = storedVersion.split(".");
  569.             if(pointVersion[0] > pointStoredVersion[0])
  570.             {
  571.                version = "newMajor";
  572.             }
  573.             else if(pointVersion[0] == pointStoredVersion[0])
  574.             {
  575.                if(pointVersion[1] > pointStoredVersion[1])
  576.                {
  577.                   version = "newer";
  578.                }
  579.                else if(pointVersion[1] == pointStoredVersion[1])
  580.                {
  581.                   if(pointVersion[2] > pointStoredVersion[2])
  582.                   {
  583.                      version = "newer";
  584.                   }
  585.                   else if(pointVersion[2] == pointStoredVersion[2])
  586.                   {
  587.                      version = "current";
  588.                   }
  589.                   else
  590.                   {
  591.                      version = "older";
  592.                   }
  593.                }
  594.                else
  595.                {
  596.                   version = "older";
  597.                }
  598.             }
  599.             else
  600.             {
  601.                version = "older";
  602.             }
  603.          }
  604.          return version;
  605.       }
  606.       
  607.       private function openProjectListener(e:Event) : void
  608.       {
  609.          dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,FileEvent.CLOSE,PROJECT));
  610.          this.activeFile.removeEventListener(Event.SELECT,this.openProjectListener);
  611.          this.loadProject(File(e.target).nativePath);
  612.       }
  613.       
  614.       public function loadHelp() : XML
  615.       {
  616.          return new XML(this.open(this.helpFile));
  617.       }
  618.       
  619.       private function openLayerImageListener(e:Event) : void
  620.       {
  621.          this.activeFile.removeEventListener(Event.SELECT,this.openLayerImageListener);
  622.          var fileName:String = copyFileToUnique(this.activeFile,new File(this.layerImagesDir + "/" + this.activeFile.name));
  623.          var type:String = this.activeFile.extension.toUpperCase();
  624.          type = type.indexOf("SWF") > -1 ? SWF : LAYER_IMAGE;
  625.          dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,FileEvent.OPEN,type,fileName));
  626.       }
  627.       
  628.       private function open(file:File) : String
  629.       {
  630.          var stream:FileStream = new FileStream();
  631.          stream.open(file,FileMode.READ);
  632.          var fileData:String = stream.readUTFBytes(stream.bytesAvailable);
  633.          stream.close();
  634.          return fileData;
  635.       }
  636.       
  637.       public function get projectName() : String
  638.       {
  639.          return this.removeExtension(new File(this.loadLocalString("recentProject")).name);
  640.       }
  641.       
  642.       private function createSessionProjectDir() : void
  643.       {
  644.          this.sessionProjectDir = this.loadLocalString("sessionProjectDir");
  645.          if(this.sessionProjectDir != "deleted" && this.sessionProjectDir != null)
  646.          {
  647.             this.cleanup();
  648.          }
  649.          this.saveLocalString("sessionProjectDir",File.createTempDirectory().nativePath);
  650.          this.sessionProjectDir = this.loadLocalString("sessionProjectDir");
  651.       }
  652.       
  653.       private function loadXML(path:String) : String
  654.       {
  655.          var file:File = new File(path);
  656.          var fileData:String = "no file: " + path;
  657.          if(file.exists)
  658.          {
  659.             fileData = this.open(file);
  660.          }
  661.          return fileData;
  662.       }
  663.       
  664.       public function loadProject(path:String) : String
  665.       {
  666.          var layerData:String = null;
  667.          var styleChild:String = null;
  668.          var stylePath:String = null;
  669.          var styleFile:File = null;
  670.          var styleData:String = null;
  671.          dispatchEvent(new FileEvent(FileEvent.BEGIN_LOAD,true,false,FileEvent.OPEN,PROJECT));
  672.          this.lastFileLoaded = PROJECT;
  673.          this.decoFilesLoaded = [];
  674.          this.decoAssetsLoaded = [];
  675.          var projectFile:File = new File(path);
  676.          this.setProject(projectFile);
  677.          this.createSessionProjectDir();
  678.          new File(this.recentProjectDir).copyTo(new File(this.sessionProjectDir),true);
  679.          layerData = this.loadXML(path);
  680.          var projectXML:XML = new XML(layerData);
  681.          if(this._canvasSizeIndex != -1)
  682.          {
  683.             projectXML.@size = this._canvasSizeIndex;
  684.             this._canvasSizeIndex = -1;
  685.          }
  686.          for(styleChild in projectXML.styles.style)
  687.          {
  688.             stylePath = projectXML.styles.style[styleChild].@xml;
  689.             styleFile = new File(this.currentProjectDir + "/Styles/" + stylePath);
  690.             if(styleFile.exists)
  691.             {
  692.                styleData = this.loadStyle(styleFile.nativePath);
  693.             }
  694.          }
  695.          layerData = projectXML.toXMLString();
  696.          dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,FileEvent.OPEN,PROJECT,layerData));
  697.          return layerData;
  698.       }
  699.       
  700.       private function setProject(file:File) : void
  701.       {
  702.          this.saveLocalString("recentProject",file.nativePath);
  703.       }
  704.       
  705.       public function newProject(sizeIndex:int = 2) : void
  706.       {
  707.          this._canvasSizeIndex = sizeIndex;
  708.          this.cleanup();
  709.          this.createBaseProject();
  710.          this.loadProject(this.getRecentProjectPath());
  711.       }
  712.       
  713.       private function get helpFile() : File
  714.       {
  715.          return File.applicationDirectory.resolvePath("AppFiles/Help.xml");
  716.       }
  717.       
  718.       public function loadInputSWF(fileName:String, completeListener:Function = null, errorListener:Function = null) : Loader
  719.       {
  720.          return this._loadMedia(this.currentProjectDir + "/Styles/Assets/" + fileName,completeListener,errorListener);
  721.       }
  722.       
  723.       public function get projectFileName() : String
  724.       {
  725.          return this.projectName + ".lbp";
  726.       }
  727.       
  728.       private function openStyleListener(e:Event) : void
  729.       {
  730.          this.activeFile.removeEventListener(Event.SELECT,this.openStyleListener);
  731.          this.importStyle(File(e.target).nativePath);
  732.       }
  733.       
  734.       private function get currentProject() : String
  735.       {
  736.          return this.sessionProjectDir + "/" + this.projectName + ".lbp";
  737.       }
  738.       
  739.       private function versionCheckComplete(e:Event) : void
  740.       {
  741.          var currentMajorVersion:int = int(e.target.data.currentMajorVersion);
  742.          var version:String = String(e.target.data["version" + Main.MAJOR_VERSION]);
  743.          var vString:String = this.compareVersion(version);
  744.          if(vString == "older")
  745.          {
  746.             dispatchEvent(new FileEvent(FileEvent.VERSION_UPDATE,false,false,null,FileEvent.UPDATE_VERSION,{
  747.                "currentVersion":this.appSettings.version,
  748.                "newVersion":version
  749.             }));
  750.          }
  751.          else if(vString == "current")
  752.          {
  753.             if(currentMajorVersion > Main.MAJOR_VERSION)
  754.             {
  755.                dispatchEvent(new FileEvent(FileEvent.VERSION_UPDATE,false,false,null,FileEvent.NEW_VERSION,{
  756.                   "currentVersion":Main.MAJOR_VERSION,
  757.                   "newVersion":currentMajorVersion
  758.                }));
  759.             }
  760.             else if(this.showVersionCheckResult)
  761.             {
  762.                dispatchEvent(new FileEvent(FileEvent.VERSION_UPDATE,false,false,null,FileEvent.CURRENT_VERSION,{
  763.                   "currentVersion":this.appSettings.version,
  764.                   "newVersion":version
  765.                }));
  766.             }
  767.          }
  768.          else if(vString == "newer")
  769.          {
  770.          }
  771.          this.showVersionCheckResult = false;
  772.       }
  773.       
  774.       public function copyAppFiles() : void
  775.       {
  776.          this.appFiles.copyTo(File.applicationStorageDirectory,true);
  777.       }
  778.       
  779.       private function createBaseProject() : void
  780.       {
  781.          var baseProjectDir:File = new File();
  782.          baseProjectDir.nativePath = File.applicationStorageDirectory.nativePath + "/Presets/BaseProject";
  783.          var newProjectDir:File = this.createTempProjectDir();
  784.          baseProjectDir.copyTo(newProjectDir,true);
  785.          var baseProjectFile:File = newProjectDir.resolvePath("BaseProject.lbp");
  786.          var newProjectFile:* = newProjectDir.resolvePath("New Project.lbp");
  787.          baseProjectFile.moveTo(newProjectFile);
  788.          this.setProject(newProjectFile);
  789.       }
  790.       
  791.       public function openProject(path:String = "") : void
  792.       {
  793.          if(this.projectIsTemp)
  794.          {
  795.             path = File.desktopDirectory.nativePath;
  796.          }
  797.          else
  798.          {
  799.             path = new File(this.getRecentProjectPath()).parent.nativePath;
  800.          }
  801.          this.initOpenFile(path,"Open A LiveBrush Project",this.openProjectListener,[new FileFilter("LiveBrush Project (*.lbp)","*.lbp;")]);
  802.       }
  803.       
  804.       public function loadStyle(path:String = "", dispatch:Boolean = true) : String
  805.       {
  806.          var styleData:String = null;
  807.          var styleXML:XML = null;
  808.          var checkStyleXML:XML = null;
  809.          var decoChild:String = null;
  810.          var decoPath:String = null;
  811.          if(new File(path).exists)
  812.          {
  813.             this.lastFileLoaded = STYLE;
  814.             styleData = this.loadXML(path);
  815.             styleXML = new XML(styleData);
  816.             checkStyleXML = new XML(styleData);
  817.             for(decoChild in styleXML.deco.decoList.deco)
  818.             {
  819.                decoPath = styleXML.deco.decoList.deco[decoChild].@value;
  820.             }
  821.             styleXML = checkStyleXML;
  822.             if(dispatch)
  823.             {
  824.                dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,FileEvent.OPEN,STYLE,styleXML.toString()));
  825.             }
  826.             return styleXML.toString();
  827.          }
  828.          dispatchEvent(new FileEvent(FileEvent.FILE_NOT_FOUND,true,false,FileEvent.OPEN,STYLE,"Styles/" + new File(path).name));
  829.          return FileEvent.FILE_NOT_FOUND;
  830.       }
  831.       
  832.       public function openLayerImage(path:String = null) : void
  833.       {
  834.          path = path == null ? this.openRecentProjectDir : path;
  835.          this.initOpenFile(path,"Import Layer Image",this.openLayerImageListener,[new FileFilter("All Formats (*.jpg,*.gif,*.png,*.swf)","*.jpg;*.gif;*.png;*.swf;","JPEG;jp2_;GIFF;")]);
  836.       }
  837.       
  838.       private function exportStyle(file:File, createDirectory:Boolean = true) : void
  839.       {
  840.          var styleAssetDir:File = null;
  841.          if(createDirectory)
  842.          {
  843.             if(file.extension != null)
  844.             {
  845.                file = file.resolvePath(this.removeExtension(file.nativePath));
  846.             }
  847.             file.createDirectory();
  848.             file = new File(file.nativePath + "/" + file.name);
  849.             if(!file.extension || file.extension.toLowerCase() != "xml")
  850.             {
  851.                file.nativePath += ".xml";
  852.             }
  853.          }
  854.          this.decoSetData = XML(this.styleData.deco.decoList);
  855.          if(createDirectory)
  856.          {
  857.             styleAssetDir = this.exportDecoSet(file.parent);
  858.          }
  859.          else
  860.          {
  861.             styleAssetDir = this.exportDecoSet(file,false);
  862.          }
  863.          if(this.styleData.line.inputSWF.length() > 0 && this.styleData.line.inputSWF.toString() != "" && this.styleData.line.inputSWF.toString() != "null")
  864.          {
  865.             this.exportDynamicInput(styleAssetDir,this.styleData.line.inputSWF);
  866.          }
  867.          this.save(file,this.styleData);
  868.       }
  869.       
  870.       private function saveSVGListener(e:Event) : void
  871.       {
  872.          e.target.removeEventListener(Event.SELECT,this.saveSVGListener);
  873.          this.exportSVG(e.target as File);
  874.       }
  875.       
  876.       private function save(file:File, data:String) : void
  877.       {
  878.          data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + data;
  879.          var stream:FileStream = new FileStream();
  880.          stream.open(file,FileMode.WRITE);
  881.          stream.writeUTFBytes(data);
  882.          stream.close();
  883.       }
  884.       
  885.       public function copyDecoToLayer(path:String, batch:Boolean = false) : void
  886.       {
  887.          var decoFile:File = new File(this.decoAssetsDir + "/" + path);
  888.          var fileName:String = copyFileToUnique(decoFile,new File(this.layerImagesDir + "/" + decoFile.name));
  889.          var type:String = decoFile.extension.toUpperCase();
  890.          type = type.indexOf("SWF") > -1 ? SWF : LAYER_IMAGE;
  891.          dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,batch ? FileEvent.BATCH_OPEN : FileEvent.OPEN,type,fileName));
  892.       }
  893.       
  894.       public function openStyle(path:String = null) : void
  895.       {
  896.          path = path == null ? this.openRecentProjectDir : path;
  897.          this.initOpenFile(path,"Import Style",this.openStyleListener,[new FileFilter("LiveBrush Style (*.xml)","*.xml;")]);
  898.       }
  899.       
  900.       private function importProjectListener(e:Event) : void
  901.       {
  902.          this.activeFile.removeEventListener(Event.SELECT,this.importProjectListener);
  903.          this._importProject(File(e.target).nativePath);
  904.       }
  905.       
  906.       public function getExtension(fileName:String) : String
  907.       {
  908.          return fileName.substr(fileName.lastIndexOf("."));
  909.       }
  910.       
  911.       public function saveAsProject(layerXML:XML, stylesXML:XML) : void
  912.       {
  913.          this.layerData = layerXML;
  914.          this.projectStyles = stylesXML;
  915.          this.initSaveFile("Save As Project",this.saveProjectListener);
  916.       }
  917.       
  918.       public function getDecoAsset(path:String, enabled:Boolean = true, forceNew:Boolean = false) : DecoAsset
  919.       {
  920.          var i:int = 0;
  921.          var decoAsset:DecoAsset = null;
  922.          if(this.decoAssetsLoaded.length == 0 || forceNew)
  923.          {
  924.             decoAsset = new DecoAsset(path);
  925.             decoAsset.enabled = enabled;
  926.             this.decoAssetsLoaded.push({
  927.                "fileName":path,
  928.                "asset":decoAsset
  929.             });
  930.          }
  931.          else
  932.          {
  933.             for(i = 0; i < this.decoAssetsLoaded.length; i++)
  934.             {
  935.                if(this.decoAssetsLoaded[i].fileName == path)
  936.                {
  937.                   decoAsset = this.decoAssetsLoaded[i].asset;
  938.                   break;
  939.                }
  940.             }
  941.             if(decoAsset == null)
  942.             {
  943.                decoAsset = new DecoAsset(path);
  944.                decoAsset.enabled = enabled;
  945.                this.decoAssetsLoaded.push({
  946.                   "fileName":path,
  947.                   "asset":decoAsset
  948.                });
  949.             }
  950.          }
  951.          return decoAsset;
  952.       }
  953.       
  954.       private function get recentProjectDir() : String
  955.       {
  956.          return new File(this.loadLocalString("recentProject")).parent.nativePath;
  957.       }
  958.       
  959.       public function revertProject() : void
  960.       {
  961.          if(!this.projectIsTemp)
  962.          {
  963.             dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,FileEvent.CLOSE,PROJECT));
  964.             this.loadProject(this.recentProject);
  965.          }
  966.       }
  967.       
  968.       private function loadLocalString(label:String) : String
  969.       {
  970.          var storedValue:ByteArray = EncryptedLocalStore.getItem(label);
  971.          if(storedValue != null)
  972.          {
  973.             return storedValue.readUTFBytes(storedValue.length);
  974.          }
  975.          return null;
  976.       }
  977.       
  978.       public function get projectIsTemp() : Boolean
  979.       {
  980.          return this._projectIsTemp;
  981.       }
  982.       
  983.       public function loadAppSettings() : void
  984.       {
  985.          var settingsXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
  986.          var ns:Namespace = settingsXML.namespace();
  987.          this.appSettings = new Settings();
  988.          this.appSettings.version = settingsXML.ns::version;
  989.          this.getGlobalSettings();
  990.       }
  991.       
  992.       private function exportDynamicInput(file:File, inputFileName:String) : void
  993.       {
  994.          try
  995.          {
  996.             new File(this.currentProjectDir + "/Styles/Assets/" + inputFileName).copyTo(new File(file.nativePath + "/" + inputFileName));
  997.          }
  998.          catch(e:Error)
  999.          {
  1000.          }
  1001.       }
  1002.       
  1003.       private function writeProjectBackup() : File
  1004.       {
  1005.          var desktopProject:File = null;
  1006.          var dup:int = 0;
  1007.          do
  1008.          {
  1009.             desktopProject = File.desktopDirectory.resolvePath(this.projectName + " Backup " + (dup + 1));
  1010.             dup++;
  1011.          }
  1012.          while(desktopProject.exists);
  1013.          
  1014.          desktopProject.createDirectory();
  1015.          new File(this.currentProjectDir).copyTo(desktopProject,true);
  1016.          return desktopProject;
  1017.       }
  1018.       
  1019.       private function get layerImagesDir() : String
  1020.       {
  1021.          return this.sessionProjectDir + "/Layer Images";
  1022.       }
  1023.       
  1024.       private function saveLocalString(label:String, data:String) : void
  1025.       {
  1026.          var bytes:ByteArray = new ByteArray();
  1027.          bytes.writeUTFBytes(data);
  1028.          EncryptedLocalStore.setItem(label,bytes);
  1029.       }
  1030.       
  1031.       public function saveSVG(svg:XML) : void
  1032.       {
  1033.          this.svgData = svg;
  1034.          this.initSaveFile("Export SVG",this.saveSVGListener);
  1035.       }
  1036.       
  1037.       public function cleanup() : void
  1038.       {
  1039.          this.deleteSessionProject();
  1040.          this.deleteTempProject();
  1041.       }
  1042.       
  1043.       private function saveProjectListener(e:Event) : void
  1044.       {
  1045.          e.target.removeEventListener(Event.SELECT,this.saveProjectListener);
  1046.          UI.setStatus("Saving Project...");
  1047.          setTimeout(this.writeProject,25,e.target as File);
  1048.       }
  1049.       
  1050.       private function get lbUserDocsDir() : File
  1051.       {
  1052.          return File.documentsDirectory.resolvePath("LiveBrush");
  1053.       }
  1054.       
  1055.       private function saveImageListener(e:Event) : void
  1056.       {
  1057.          e.target.removeEventListener(Event.SELECT,this.saveImageListener);
  1058.          this.exportImage(e.target as File);
  1059.       }
  1060.       
  1061.       public function loadDeco(path:String = "", listener:Function = null) : String
  1062.       {
  1063.          var loader:Loader = null;
  1064.          var decoFile:File = new File(path);
  1065.          if(decoFile.exists)
  1066.          {
  1067.             loader = this._loadMedia(decoFile.nativePath);
  1068.             if(listener != null)
  1069.             {
  1070.                loader.contentLoaderInfo.addEventListener(Event.INIT,listener);
  1071.             }
  1072.             return decoFile.name;
  1073.          }
  1074.          dispatchEvent(new FileEvent(FileEvent.FILE_NOT_FOUND,true,false,FileEvent.OPEN,DECO,"Assets/" + decoFile.name));
  1075.          return FileEvent.FILE_NOT_FOUND;
  1076.       }
  1077.       
  1078.       public function importProject(path:String = "") : void
  1079.       {
  1080.          path = new File(this.getRecentProjectPath()).parent.nativePath;
  1081.          this.initOpenFile(path,"Import A LiveBrush Project",this.importProjectListener,[new FileFilter("LiveBrush Project (*.lbp)","*.lbp;")]);
  1082.       }
  1083.       
  1084.       private function openDecoSetListener(e:Event) : void
  1085.       {
  1086.          this.activeFile.removeEventListener(Event.SELECT,this.openDecoSetListener);
  1087.          this.importDecoSet(File(e.target).nativePath,this.initDecoImportListener);
  1088.       }
  1089.       
  1090.       private function deleteLocalString(label:String) : void
  1091.       {
  1092.          EncryptedLocalStore.removeItem(label);
  1093.       }
  1094.       
  1095.       private function setRecentSaveDir(e:Event) : void
  1096.       {
  1097.          this.activeFile.removeEventListener(Event.SELECT,this.setRecentSaveDir);
  1098.          this._recentSaveDir = e.target.nativePath;
  1099.       }
  1100.       
  1101.       private function _importProject(path:String) : String
  1102.       {
  1103.          var layerData:String = null;
  1104.          var layer:String = null;
  1105.          var styleChild:String = null;
  1106.          var layerType:String = null;
  1107.          var layerFile:File = null;
  1108.          var newFile:File = null;
  1109.          var fileName:String = null;
  1110.          var stylePath:String = null;
  1111.          var styleFile:File = null;
  1112.          var styleData:String = null;
  1113.          this.lastFileLoaded = PROJECT;
  1114.          var projectFile:File = new File(path);
  1115.          layerData = this.loadXML(path);
  1116.          var projectXML:XML = new XML(layerData);
  1117.          for(layer in projectXML.layers.layer)
  1118.          {
  1119.             layerType = projectXML.layers.layer[layer].@type;
  1120.             if(layerType == "image" || layerType == "swf")
  1121.             {
  1122.                layerFile = new File(projectFile.parent.nativePath + "/Layer Images/" + projectXML.layers.layer[layer].solid.@src);
  1123.                newFile = new File(this.currentProjectDir + "/Layer Images/" + layerFile.name);
  1124.                if(layerFile.exists)
  1125.                {
  1126.                   fileName = copyFileToUnique(layerFile,newFile);
  1127.                   newFile = new File(this.currentProjectDir + "/Layer Images/" + fileName);
  1128.                }
  1129.                projectXML.layers.layer[layer].solid.@src = newFile.name;
  1130.                projectXML.layers.layer[layer].@label = newFile.name;
  1131.             }
  1132.          }
  1133.          layerData = projectXML.toXMLString();
  1134.          for(styleChild in projectXML.styles.style)
  1135.          {
  1136.             stylePath = projectXML.styles.style[styleChild].@xml;
  1137.             styleFile = new File(projectFile.parent.nativePath + "/Styles/" + stylePath);
  1138.             if(styleFile.exists)
  1139.             {
  1140.                styleData = this.importStyle(styleFile.nativePath);
  1141.             }
  1142.          }
  1143.          dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,FileEvent.IMPORT,PROJECT,layerData));
  1144.          return layerData;
  1145.       }
  1146.       
  1147.       private function initDecoImportListener(e:Event) : void
  1148.       {
  1149.          e.target.removeEventListener(Event.INIT,this.initDecoImportListener);
  1150.          dispatchEvent(new FileEvent(FileEvent.IO_EVENT,true,false,FileEvent.OPEN,DECO,e.target));
  1151.       }
  1152.       
  1153.       public function openInputSWF(path:String = null) : void
  1154.       {
  1155.          path = this.appFiles.nativePath + "/Presets/Behaviors/SmoothRandom-AS3-F9.swf";
  1156.          try
  1157.          {
  1158.             this.initOpenFile(path,"Import Flash Player 9 (ActionScript 3) File",this.importInputSWFListener,[new FileFilter("Flash Player 9 SWF (*.swf)","*.swf;")]);
  1159.          }
  1160.          catch(e:Error)
  1161.          {
  1162.             copyAppFiles();
  1163.             initOpenFile(path,"Import Flash Player 9 (ActionScript 3) File",importInputSWFListener,[new FileFilter("Flash Player 9 SWF (*.swf)","*.swf;")]);
  1164.          }
  1165.       }
  1166.       
  1167.       public function saveGlobalSettings() : void
  1168.       {
  1169.          this.saveLocalString("TEMP_CACHE_LAYERS",String(GlobalSettings.TEMP_CACHE_LAYERS));
  1170.          this.saveLocalString("CACHE_DECOS",String(GlobalSettings.CACHE_DECOS));
  1171.          this.saveLocalString("CACHE_DELAY",String(GlobalSettings.CACHE_DELAY));
  1172.          this.saveLocalString("CACHE_REALTIME",String(GlobalSettings.CACHE_REALTIME));
  1173.          this.saveLocalString("FIRST_SAVE",String(GlobalSettings.FIRST_SAVE));
  1174.          this.saveLocalString("FIRST_DECOSET_EXPORT",String(GlobalSettings.FIRST_DECOSET_EXPORT));
  1175.          this.saveLocalString("FIRST_STYLE_EXPORT",String(GlobalSettings.FIRST_STYLE_EXPORT));
  1176.          this.saveLocalString("CHECK_FOR_UPDATES",String(GlobalSettings.CHECK_FOR_UPDATES));
  1177.          this.saveLocalString("REGISTERED_EMAIL",String(GlobalSettings.REGISTERED_EMAIL));
  1178.          this.saveLocalString("SHOW_BUSY_WARNINGS",String(GlobalSettings.SHOW_BUSY_WARNINGS));
  1179.          this.saveLocalString("DRAW_MODE",String(GlobalSettings.DRAW_MODE));
  1180.          this.saveLocalString("STROKE_BUFFER",String(GlobalSettings.STROKE_BUFFER));
  1181.       }
  1182.       
  1183.       private function createTempProjectDir() : File
  1184.       {
  1185.          var tempDir:File = File.createTempDirectory();
  1186.          this.saveLocalString("tempProjectDir",tempDir.nativePath);
  1187.          this.tempProjectDir = this.loadLocalString("tempProjectDir");
  1188.          this._projectIsTemp = true;
  1189.          return tempDir;
  1190.       }
  1191.       
  1192.       private function exportProject(file:File) : void
  1193.       {
  1194.          var style:String = null;
  1195.          var layer:String = null;
  1196.          var styleFile:File = null;
  1197.          var layerType:String = null;
  1198.          var layerFile:File = null;
  1199.          var newFile:File = null;
  1200.          var fileName:String = null;
  1201.          var newProjectFile:File = file;
  1202.          var projectXML:XML = new XML(<LiveBrushProject size={Canvas.SIZE_INDEX} width={Canvas.WIDTH} height={Canvas.HEIGHT} name={file.name} date="08/03/82" version="1"></LiveBrushProject>);
  1203.          projectXML.appendChild(<styles></styles>);
  1204.          projectXML.appendChild(this.layerData);
  1205.          for(style in this.projectStyles.style)
  1206.          {
  1207.             styleFile = new File(this.currentProjectDir).resolvePath(this.currentProjectDir + "/Styles/" + this.projectStyles.style[style].@name + ".xml");
  1208.             this.styleData = this.projectStyles.style[style];
  1209.             this.exportStyle(new File(file.parent.nativePath + "/Styles/" + styleFile.name),false);
  1210.             projectXML.styles.appendChild(<style xml={styleFile.name}/>);
  1211.          }
  1212.          this.save(newProjectFile,projectXML.toString());
  1213.          new File(file.parent.nativePath + "/Layer Images").createDirectory();
  1214.          new File(file.parent.nativePath + "/Styles").createDirectory();
  1215.          new File(file.parent.nativePath + "/Styles/Assets").createDirectory();
  1216.          for(layer in projectXML.layers.layer)
  1217.          {
  1218.             layerType = projectXML.layers.layer[layer].@type;
  1219.             if(layerType == "image" || layerType == "swf")
  1220.             {
  1221.                layerFile = new File(this.currentProjectDir + "/Layer Images/" + projectXML.layers.layer[layer].solid.@src);
  1222.                newFile = new File(file.parent.nativePath + "/Layer Images/" + layerFile.name);
  1223.                if(layerFile.exists)
  1224.                {
  1225.                   try
  1226.                   {
  1227.                      fileName = copyFileToUnique(layerFile,newFile);
  1228.                   }
  1229.                   catch(e:Error)
  1230.                   {
  1231.                   }
  1232.                   continue;
  1233.                }
  1234.             }
  1235.          }
  1236.       }
  1237.       
  1238.       private function setRecentOpenDir(e:Event) : void
  1239.       {
  1240.          this.activeFile.removeEventListener(Event.SELECT,this.setRecentOpenDir);
  1241.          this._recentOpenDir = e.target.nativePath;
  1242.       }
  1243.       
  1244.       private function initSaveFile(title:String, listener:Function) : void
  1245.       {
  1246.          this.activeFile = this._recentSaveDir == "" ? File.desktopDirectory : new File(this._recentSaveDir).parent;
  1247.          this.activeFile.browseForSave(title);
  1248.          this.activeFile.addEventListener(Event.SELECT,this.checkValidSavePath);
  1249.          this.activeFile.addEventListener(Event.SELECT,listener);
  1250.       }
  1251.       
  1252.       public function openDecoSet(path:String = null) : void
  1253.       {
  1254.          path = path == null ? this.openRecentProjectDir : path;
  1255.          this.initOpenFile(path,"Import Decoration Set",this.openDecoSetListener,[new FileFilter("LiveBrush DecoSet (*.xml)","*.xml;")]);
  1256.       }
  1257.       
  1258.       public function getRecentProjectPath() : String
  1259.       {
  1260.          if(this.recentProject == null)
  1261.          {
  1262.             this.createBaseProject();
  1263.          }
  1264.          else if(!new File(this.recentProject).exists)
  1265.          {
  1266.             this.createBaseProject();
  1267.          }
  1268.          return this.recentProject;
  1269.       }
  1270.       
  1271.       public function saveFlattenedLayerImage(bmp:BitmapData, name:String, unique:Boolean = true) : String
  1272.       {
  1273.          var file:File = new File(this.layerImagesDir + "/" + name + ".png");
  1274.          if(unique)
  1275.          {
  1276.             file = getUniqueFile(file);
  1277.          }
  1278.          this.writeImage(bmp,file);
  1279.          bmp.dispose();
  1280.          return file.name;
  1281.       }
  1282.       
  1283.       public function copyLayerToDeco(path:String) : String
  1284.       {
  1285.          var layerFile:File = new File(this.layerImagesDir + "/" + path);
  1286.          return copyFileToUnique(layerFile,new File(this.decoAssetsDir + "/" + layerFile.name));
  1287.       }
  1288.       
  1289.       public function cleanupProject(layerXML:XML, stylesXML:XML) : void
  1290.       {
  1291.          var projectDir:File;
  1292.          var projectBackup:File;
  1293.          var tempDir:File = File.createTempDirectory();
  1294.          this.layerData = layerXML;
  1295.          this.projectStyles = stylesXML;
  1296.          this.exportProject(new File(tempDir.nativePath + "/" + this.projectFileName));
  1297.          projectDir = new File(this.recentProjectDir);
  1298.          projectBackup = this.writeProjectBackup();
  1299.          try
  1300.          {
  1301.             projectDir.deleteDirectory(true);
  1302.             projectDir.createDirectory();
  1303.             tempDir.copyTo(projectDir,true);
  1304.             tempDir.deleteDirectory(true);
  1305.          }
  1306.          catch(e:Error)
  1307.          {
  1308.             UI.MAIN_UI.showErrorDialog({"message":"CLEANUP ERROR\nProject assets open or in-use by another application.\nA backup has been saved to your desktop."});
  1309.          }
  1310.          this.revertProject();
  1311.          try
  1312.          {
  1313.             projectBackup.deleteDirectory(true);
  1314.          }
  1315.          catch(e:Error)
  1316.          {
  1317.          }
  1318.       }
  1319.       
  1320.       private function get appFiles() : File
  1321.       {
  1322.          return File.applicationDirectory.resolvePath("AppFiles");
  1323.       }
  1324.       
  1325.       private function get openRecentProjectDir() : String
  1326.       {
  1327.          return this.projectIsTemp ? File.documentsDirectory.nativePath : new File(this.loadLocalString("recentProject")).parent.nativePath;
  1328.       }
  1329.       
  1330.       private function renameFile(file:File, newName:String) : File
  1331.       {
  1332.          var newFile:File = file.resolvePath("../" + newName);
  1333.          file.moveTo(newFile,true);
  1334.          return newFile;
  1335.       }
  1336.       
  1337.       public function saveProject(layerXML:XML, stylesXML:XML) : void
  1338.       {
  1339.          if(this.projectIsTemp)
  1340.          {
  1341.             this.saveAsProject(layerXML,stylesXML);
  1342.          }
  1343.          else
  1344.          {
  1345.             this.layerData = layerXML;
  1346.             this.projectStyles = stylesXML;
  1347.             UI.setStatus("Saving Project...");
  1348.             setTimeout(this.writeProject,25,new File(this.recentProjectDir));
  1349.          }
  1350.       }
  1351.       
  1352.       private function get decoAssetsDir() : String
  1353.       {
  1354.          return this.sessionProjectDir + "/Styles/Assets";
  1355.       }
  1356.       
  1357.       public function checkFirstRun() : void
  1358.       {
  1359.          var versionType:String;
  1360.          this.firstRun = !File.applicationStorageDirectory.exists;
  1361.          versionType = this.compareVersion();
  1362.          if(this.firstRun)
  1363.          {
  1364.             this.copyAppFiles();
  1365.             this.saveLocalString("version",this.appSettings.version);
  1366.             setTimeout(UI.MAIN_UI.confirmActionDialog,20000,{
  1367.                "message":"Would like to automatically check for updates?\nYou can change this setting at any time.",
  1368.                "yesFunction":function():*
  1369.                {
  1370.                   GlobalSettings.CHECK_FOR_UPDATES = true;
  1371.                   checkForUpdates();
  1372.                },
  1373.                "noFunction":function():*
  1374.                {
  1375.                   GlobalSettings.CHECK_FOR_UPDATES = false;
  1376.                }
  1377.             });
  1378.          }
  1379.          else if(!this.firstRun && versionType == "newer")
  1380.          {
  1381.             this.copyAppFiles();
  1382.             this.saveLocalString("version",this.appSettings.version);
  1383.             if(GlobalSettings.CHECK_FOR_UPDATES)
  1384.             {
  1385.                setTimeout(checkForUpdates,20000);
  1386.             }
  1387.          }
  1388.          else if(!this.firstRun && versionType == "current")
  1389.          {
  1390.             if(GlobalSettings.CHECK_FOR_UPDATES)
  1391.             {
  1392.                setTimeout(checkForUpdates,20000);
  1393.             }
  1394.          }
  1395.          else if(!(!this.firstRun && versionType == "older"))
  1396.          {
  1397.             if(versionType == "newMajor")
  1398.             {
  1399.                if(GlobalSettings.CHECK_FOR_UPDATES)
  1400.                {
  1401.                   setTimeout(checkForUpdates,20000);
  1402.                }
  1403.             }
  1404.          }
  1405.       }
  1406.       
  1407.       public function saveImage(bmp:BitmapData) : void
  1408.       {
  1409.          this._bitmapData = bmp;
  1410.          this.initSaveFile("Save Image",this.saveImageListener);
  1411.       }
  1412.       
  1413.       private function writeImage(bmp:BitmapData, file:File) : void
  1414.       {
  1415.          var png:* = PNGEncoder.encode(bmp);
  1416.          var stream:FileStream = new FileStream();
  1417.          stream.open(file,FileMode.WRITE);
  1418.          stream.writeBytes(png,0,0);
  1419.          stream.close();
  1420.       }
  1421.       
  1422.       private function getGlobalSettings() : void
  1423.       {
  1424.          GlobalSettings.setProperty("CACHE_LAYERS",this.loadLocalString("TEMP_CACHE_LAYERS"));
  1425.          GlobalSettings.setProperty("CACHE_DECOS",this.loadLocalString("CACHE_DECOS"));
  1426.          GlobalSettings.setProperty("CACHE_DELAY",this.loadLocalString("CACHE_DELAY"));
  1427.          GlobalSettings.setProperty("CACHE_REALTIME",this.loadLocalString("CACHE_REALTIME"));
  1428.          GlobalSettings.setProperty("FIRST_SAVE",this.loadLocalString("FIRST_SAVE"));
  1429.          GlobalSettings.setProperty("FIRST_DECOSET_EXPORT",this.loadLocalString("FIRST_DECOSET_EXPORT"));
  1430.          GlobalSettings.setProperty("FIRST_STYLE_EXPORT",this.loadLocalString("FIRST_STYLE_EXPORT"));
  1431.          GlobalSettings.setProperty("CHECK_FOR_UPDATES",this.loadLocalString("CHECK_FOR_UPDATES"));
  1432.          GlobalSettings.setProperty("REGISTERED_EMAIL",this.loadLocalString("REGISTERED_EMAIL"));
  1433.          GlobalSettings.setProperty("SHOW_BUSY_WARNINGS",this.loadLocalString("SHOW_BUSY_WARNINGS"));
  1434.          GlobalSettings.setProperty("DRAW_MODE",this.loadLocalString("DRAW_MODE"));
  1435.          GlobalSettings.setProperty("STROKE_BUFFER",this.loadLocalString("STROKE_BUFFER"));
  1436.       }
  1437.       
  1438.       private function importInputSWFListener(e:Event) : void
  1439.       {
  1440.          e.target.removeEventListener(Event.INIT,this.importInputSWFListener);
  1441.          this.importInputSWF(File(e.target).nativePath);
  1442.       }
  1443.       
  1444.       public function importDeco(path:String = "", listener:Function = null) : String
  1445.       {
  1446.          var newFile:* = undefined;
  1447.          var fileName:String = null;
  1448.          this.lastFileLoaded = DECO;
  1449.          var decoFile:File = new File(path);
  1450.          if(decoFile.exists)
  1451.          {
  1452.             newFile = new File(this.currentProjectDir + "/Styles/Assets/" + decoFile.name);
  1453.             fileName = copyFileToUnique(decoFile,newFile);
  1454.             newFile = new File(this.currentProjectDir + "/Styles/Assets/" + fileName);
  1455.             this.loadDeco(newFile.nativePath,listener);
  1456.             return newFile.name;
  1457.          }
  1458.          return this.loadDeco(decoFile.nativePath);
  1459.       }
  1460.       
  1461.       public function createFileNameDup(list:Array, file:File, prop:String = null) : String
  1462.       {
  1463.          return createNameDup(list,this.removeExtension(file.name),prop) + this.getExtension(file.name);
  1464.       }
  1465.       
  1466.       public function deleteTempProject() : void
  1467.       {
  1468.          var tempDir:File = null;
  1469.          this.tempProjectDir = this.loadLocalString("tempProjectDir");
  1470.          this._projectIsTemp = false;
  1471.          if(this.tempProjectDir != "deleted" && this.tempProjectDir != null)
  1472.          {
  1473.             tempDir = new File(this.tempProjectDir);
  1474.             if(tempDir.exists)
  1475.             {
  1476.                tempDir.deleteDirectory(true);
  1477.             }
  1478.             this.saveLocalString("tempProjectDir","deleted");
  1479.          }
  1480.       }
  1481.    }
  1482. }
  1483.  
  1484.