1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * <p>cc.UserDefault acts as a tiny localStorage. You can save and get base type values by it. <br/> 29 * For example, setBoolForKey("played", true) will add a bool value true into the localStorage. <br/> 30 * Its key is "played". You can get the value of the key by getBoolForKey("played").</p> 31 * 32 * <p>It supports the following base types: <br/> 33 * bool, int, float, double, string</p> 34 * 35 * @class 36 * @extends cc.Class 37 */ 38 39 cc.UserDefault = cc.Class.extend(/** @lends cc.UserDefault# */{ 40 _db:null, 41 /* 42 * init user default 43 * */ 44 init:function () { 45 this._db = this._getLocalStorage(); 46 return true; 47 }, 48 49 _getLocalStorage:function () { 50 try { 51 if (!!sys.localStorage) { 52 return sys.localStorage; 53 } 54 } catch (e) { 55 return undefined; 56 } 57 }, 58 59 _getWebSqlDatabase:function () { 60 61 }, 62 63 /** 64 * Get bool value by key, if the key doesn't exist, a default value will return. <br/> 65 * You can set the default value, or it is false. 66 * 67 * @param {String} key 68 * @param {Boolean} defaultValue 69 * @return {Boolean} 70 */ 71 getBoolForKey:function (key, defaultValue) { 72 cc.log("getBoolForKey is deprecated. Use sys.localStorage.getItem instead."); 73 var value = this._getValueForKey(key); 74 var ret = defaultValue || false; 75 if (value == "true") { 76 return true; 77 } 78 else if (value == "false") { 79 return false; 80 } 81 else if (value) { 82 return Boolean(value); 83 } 84 85 return ret; 86 }, 87 88 /** 89 * Get integer value by key, if the key doesn't exist, a default value will return.<br/> 90 * You can set the default value, or it is 0. 91 * 92 * @param {String} key 93 * @param {Number} defaultValue 94 * @return {Number} 95 */ 96 getIntegerForKey:function (key, defaultValue) { 97 cc.log("getIntegerForKey is deprecated. Use sys.localStorage.getItem instead."); 98 var value = this._getValueForKey(key); 99 var ret = defaultValue || 0; 100 101 if (value) { 102 return parseInt(value); 103 } 104 105 return ret; 106 }, 107 108 /** 109 * Get float value by key, if the key doesn't exist, a default value will return.<br/> 110 * You can set the default value, or it is 0.0f. 111 * 112 * @param {String} key 113 * @param {Number} defaultValue 114 * @return {Number} 115 */ 116 getFloatForKey:function (key, defaultValue) { 117 cc.log("getFloatForKey is deprecated. Use sys.localStorage.getItem instead."); 118 var value = this._getValueForKey(key); 119 var ret = defaultValue || 0; 120 121 if (value) { 122 return parseFloat(value); 123 } 124 125 return ret; 126 }, 127 128 /** 129 * Get double value by key, if the key doesn't exist, a default value will return.<br/> 130 * You can set the default value, or it is 0.0. 131 * 132 * @param {String} key 133 * @param {Number} defaultValue 134 * @return {Number} 135 */ 136 getDoubleForKey:function (key, defaultValue) { 137 cc.log("getDoubleForKey is deprecated. Use sys.localStorage.getItem instead."); 138 return this.getFloatForKey(key, defaultValue); 139 }, 140 141 /** 142 * Get string value by key, if the key doesn't exist, a default value will return.<br/> 143 * You can set the default value, or it is "". 144 * 145 * @param {String} key 146 * @param {String} defaultValue 147 * @return {String} 148 */ 149 getStringForKey:function (key, defaultValue) { 150 cc.log("getStringForKey is deprecated. Use sys.localStorage.getItem instead."); 151 var value = this._getValueForKey(key); 152 var ret = defaultValue || ""; 153 154 if (value) { 155 return String(value); 156 } 157 158 return ret; 159 }, 160 161 _getValueForKey:function (key) { 162 var ret; 163 if (this._db) { 164 ret = this._db.getItem(key); 165 } 166 167 return ret; 168 }, 169 170 /** 171 * Set bool value by key. 172 * 173 * @param {String} key 174 * @param {Boolean} value 175 */ 176 setBoolForKey:function (key, value) { 177 cc.log("setBoolForKey is deprecated. Use sys.localStorage.setItem instead."); 178 // save bool value as sring 179 this.setStringForKey(key, String(value)); 180 }, 181 182 /** 183 * Set integer value by key. 184 * 185 * @param {String} key 186 * @param {Number} value 187 */ 188 setIntegerForKey:function (key, value) { 189 cc.log("setIntegerForKey is deprecated. Use sys.localStorage.setItem instead."); 190 // check key 191 if (!key) { 192 return; 193 } 194 195 this._setValueForKey(key, parseInt(value)); 196 }, 197 198 /** 199 * Set float value by key. 200 * 201 * @param {String} key 202 * @param {Number} value 203 */ 204 setFloatForKey:function (key, value) { 205 cc.log("setFloatForKey is deprecated. Use sys.localStorage.setItem instead."); 206 // check key 207 if (!key) { 208 return; 209 } 210 211 this._setValueForKey(key, parseFloat(value)); 212 }, 213 214 /** 215 * Set double value by key. 216 * 217 * @param {String} key 218 * @param {Number} value 219 */ 220 setDoubleForKey:function (key, value) { 221 cc.log("setDoubleForKey is deprecated. Use sys.localStorage.setItem instead."); 222 return this.setFloatForKey(key, value); 223 }, 224 225 /** 226 * Set string value by key. 227 * 228 * @param {String} key 229 * @param {String} value 230 */ 231 setStringForKey:function (key, value) { 232 cc.log("setStringForKey is deprecated. Use sys.localStorage.setItem instead."); 233 // check key 234 if (!key) { 235 return; 236 } 237 238 this._setValueForKey(key, String(value)); 239 }, 240 241 _setValueForKey:function (key, value) { 242 if (this._db) { 243 this._db.setItem(key, value); 244 } 245 } 246 }); 247 248 /** 249 * returns a shared instance of the UserDefault 250 * @function 251 * @return {cc.UserDefault|} 252 */ 253 cc.UserDefault.getInstance = function () { 254 cc.log("cc.UserDefault is deprecated. Use sys.localStorage instead."); 255 if (!this._sUserDefault) { 256 this._sUserDefault = new cc.UserDefault(); 257 this._sUserDefault.init(); 258 } 259 260 return this._sUserDefault; 261 }; 262 263 /** 264 * purge a shared instance of the UserDefault 265 * @function 266 * @return {cc.UserDefault|} 267 */ 268 cc.UserDefault.purgeInstanceUserDefault = function () { 269 if (cc.hasOwnProperty("Browser")) { //TODO: clear() is not implemented in JSB 270 if (this._db) { 271 this._db.clear(); 272 } 273 } 274 }; 275 276 cc.UserDefault._sUserDefault = null; 277 cc.UserDefault._isFilePathInitialized = false;