1 /*jslint 
  2 browser: true,
  3 nomen: false,
  4 debug: true,
  5 forin: true,
  6 undef: true,
  7 white: false,
  8 onevar: false 
  9  */
 10 var sc, Titanium, air, jQuery, Mojo;
 11 
 12 /**
 13  * @constant 
 14  */
 15 var SPAZCORE_PREFS_TI_KEY = 'preferences_json';
 16 
 17 /**
 18  * @constant 
 19  */
 20 var SPAZCORE_PREFS_AIR_FILENAME = 'preferences.json';
 21 
 22 /**
 23  * @constant 
 24  */
 25 var SPAZCORE_PREFS_MOJO_COOKIENAME = 'preferences.json';
 26 
 27 /**
 28  * @constant 
 29  */
 30 var SPAZCORE_PREFS_STANDARD_COOKIENAME = 'preferences_json';
 31  
 32 /**
 33  * A preferences lib for AIR JS apps. This requires the json2.js library
 34  * 
 35  * @param {object} defaults a JS object of key:value pairs used for the pref defaults. Example:
 36  * {
 37  * 	foo:124545,
 38  * 	bar:'Hello!',
 39  *  boo:false
 40  * };
 41  * @param {object} sanity_methods a JS object of key:object pairs that defines methods to be called when the pref is get() or set(). Example:
 42  * {
 43  * 	foo:{
 44  * 		onGet:function(key, value) {};
 45  * 		onSet:function(key, value) {};
 46  * 	},
 47  * 	bar:{
 48  * 		onGet:function(key, value) {};
 49  * 		onSet:function(key, value) {};
 50  * 	}
 51  * }
 52  * 
 53  * events raised:
 54  * 'spazprefs_loaded'
 55  * 
 56  * @TODO we need to pull out the platform-specifc stuff into the /platforms/... hierarchy
 57  * @class SpazPrefs
 58  * @constructor
 59  */
 60 function SpazPrefs(defaults, id, sanity_methods) {	
 61 
 62 	/*
 63 		init prefs
 64 	*/
 65 	this._prefs = {};
 66 	
 67 	/*
 68 		init sanity check methods
 69 		we use:
 70 		* onGet()
 71 		* onSet()
 72 	*/
 73 	this._sanity_methods = {};
 74 
 75 
 76 	if (sanity_methods) {
 77 		sch.debug('adding sanity methods to prefs');
 78 		this._sanity_methods = sanity_methods;
 79 	}
 80 	
 81 	if (id) {
 82 		this.id = id;
 83 	}
 84 	
 85 	if (defaults) {
 86 		this.setDefaults(defaults);
 87 		this._applyDefaults();
 88 	}
 89 	
 90 	this.loaded = false;
 91 }
 92 
 93 
 94 /**
 95  * sets the passed object of key:val pairs as the default preferences
 96  * @param {object} defaults
 97  */ 
 98 SpazPrefs.prototype.setDefaults = function(defaults) {
 99 	this._defaults = defaults;
100 };
101 
102 
103 /**
104  * this goes through the default prefs and applies them. It also will
105  * call the onSet sanity method if it is defined for a given pref keys.
106  */
107 SpazPrefs.prototype._applyDefaults = function() {
108 	var key;
109 	for (key in this._defaults) {
110 		sc.helpers.debug('Copying default "' + key + '":"' + this._defaults[key] + '" (' + typeof(this._defaults[key]) + ')');
111 		this._prefs[key] = this._defaults[key];
112 	}
113 };
114 
115 /**
116  * resets all prefs to defaults and saves 
117  */
118 SpazPrefs.prototype.resetPrefs = function() {
119 	
120 	this._applyDefaults();
121 	this.save();
122 };
123 
124 
125 
126 /**
127  * Get a preference
128  * Note that undefined is returned if the key does not exist
129  */
130 SpazPrefs.prototype.get = function(key, encrypted) {
131 	var value;
132 	
133 	if (encrypted) {
134 		value = this.getEncrypted(key);
135 	} else {
136 		sc.helpers.debug('Looking for pref "'+key+'"');
137 
138 		if (this._prefs[key] !== undefined) {
139 			sc.helpers.debug('Found pref "'+key+'" of value "'+this._prefs[key]+'" ('+typeof(this._prefs[key])+')');
140 			value = this._prefs[key];
141 		} else {
142 			value = undefined;
143 		}
144 	}
145 	
146 	if (this._sanity_methods[key] && this._sanity_methods[key].onGet) {
147 		sc.helpers.debug("Calling "+key+".onGet()");
148 		value = this._sanity_methods[key].onGet.call(this, key, value);
149 	}
150 		
151 	return value;
152 };
153 
154 
155 /**
156  * set a preference and save automatically
157  */
158 SpazPrefs.prototype.set = function(key, val, encrypted) {
159 	
160 	sc.helpers.debug('Setting and saving "'+key+'" to "'+val+'" ('+typeof(val)+')');
161 	
162 	if (this._sanity_methods[key] && this._sanity_methods[key].onSet) {
163 		sc.helpers.debug("Calling "+key+".onSet()");
164 		val = this._sanity_methods[key].onSet.call(this, key, val);
165 	}
166 	
167 	if (encrypted) {
168 		this.setEncrypted(key, val);
169 	} else {
170 		this._prefs[key] = val;
171 	}
172 
173 	
174 	
175 	this.save();
176 };
177 
178 
179 
180 
181 
182 
183 
184 /**
185  * @param {string} key the name of the pref
186  * @param {string} type the type of method. Currently either 'onGet' or 'onSet'
187  * @param {function} method the method definition
188  */
189 SpazPrefs.prototype.setSanityMethod = function(key, type, method) {
190 	
191 	if (type !== 'onGet' && type !== 'onSet') {
192 		sch.error('sanity method type must be onGet or onSet');
193 	}
194 	
195 	if (!this._sanity_methods[key]) {
196 		this._sanity_methods[key] = {};
197 	}
198 	
199 	this._sanity_methods[key][type] = method;
200 	
201 };
202 
203 
204 /**
205  * get an encrypted preference
206  * @todo
207  */
208 SpazPrefs.prototype.getEncrypted = function(key) {
209 	alert('not yet implemented');
210 };
211 
212 
213 /**
214  * Sets an encrypted pref
215  * @todo
216  */
217 SpazPrefs.prototype.setEncrypted = function(key, val) {
218 	alert('not yet implemented');
219 };
220 
221 
222 /**
223  * loads the prefs file and parses the prefs into this._prefs,
224  * or initializes the file and loads the defaults
225  * @stub
226  */
227 SpazPrefs.prototype.load = function(name) {
228 };
229 
230 
231 
232 
233 
234 
235 
236 /**
237  * saves the current preferences
238  * @todo
239  */
240 SpazPrefs.prototype.save = function() {
241 
242 
243 	
244 };
245 
246 
247 
248 /**
249  * shortcut for SpazPrefs
250  */
251 if (sc) {
252 	var scPrefs = SpazPrefs;
253 }
254