provide installation instructions
[extjs.git] / air / src / FileProvider.js
1 /*\r
2  * Ext JS Library 0.30\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.air.FileProvider\r
11  * @extends Ext.state.Provider\r
12  * \r
13  * An Ext state provider implementation for Adobe AIR that stores state in the application \r
14  * storage directory.\r
15  * \r
16  * @constructor\r
17  * @param {Object} config\r
18  */\r
19 Ext.air.FileProvider = function(config){\r
20     Ext.air.FileProvider.superclass.constructor.call(this);\r
21         \r
22         this.defaultState = {\r
23                 mainWindow : {\r
24                         width:780,\r
25                         height:580,\r
26                         x:10,\r
27                         y:10\r
28                 }\r
29         };\r
30         \r
31     Ext.apply(this, config);\r
32     this.state = this.readState();\r
33         \r
34         var provider = this;\r
35         air.NativeApplication.nativeApplication.addEventListener('exiting', function(){\r
36                 provider.saveState();\r
37         });\r
38 };\r
39 \r
40 Ext.extend(Ext.air.FileProvider, Ext.state.Provider, {\r
41         /**\r
42          * @cfg {String} file\r
43          * The file name to use for the state file in the application storage directory\r
44          */\r
45         file: 'extstate.data',\r
46         \r
47         /**\r
48          * @cfg {Object} defaultState\r
49          * The default state if no state file can be found\r
50          */\r
51         // private\r
52     readState : function(){\r
53                 var stateFile = air.File.applicationStorageDirectory.resolvePath(this.file);\r
54                 if(!stateFile.exists){\r
55                         return this.defaultState || {};\r
56                 }\r
57                 \r
58                 var stream = new air.FileStream();\r
59                 stream.open(stateFile, air.FileMode.READ);\r
60                 \r
61                 var stateData = stream.readObject();\r
62                 stream.close();\r
63                 \r
64                 return stateData || this.defaultState || {};\r
65     },\r
66 \r
67     // private\r
68     saveState : function(name, value){\r
69         var stateFile = air.File.applicationStorageDirectory.resolvePath(this.file);\r
70                 var stream = new air.FileStream();\r
71                 stream.open(stateFile, air.FileMode.WRITE);\r
72                 stream.writeObject(this.state);\r
73                 stream.close();\r
74     }\r
75 });