Commits

watermelon committed 77e2e4e

add license to every file, put settings storage to utils

Comments (0)

Files changed (1)

src/main/client/util/Settings.js

+//
+//  Copyright (C) 2011
+//    Mario Gastegger <mario DOT gastegger AT gmx DOT net>, uniqx
+//    Metalab, 1010 Vienna, Austria
+//
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU Affero General Public License as published
+//  by the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU Affero General Public License for more details.
+//
+//  You should have received a copy of the GNU Affero General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+
+/**
+ * bookmaak.util.SETTING_TYPE = USER | GLOBAL
+ * 
+ * Enumeration of all setting types.
+ **/
+bookmaak.util.SETTING_TYPE = {
+	
+	USER:	"user_setting",
+	GLOBAL:	"global_setting"
+};
+
+/**
+ * bookmaak.util.SETTING_KEY = LOGIN | PRIVATE_KEY | PUBLIC_KEY
+ * 
+ * Enumeration of all storable settings
+ **/
+bookmaak.util.SETTING_KEY = {
+	
+	LOGIN:			"login",
+	PRIVATE_KEY:	"private_key",
+	PUBLIC_KEY:		"public_key"
+};
+
+/**
+ * bookmaak.util.addSetting(String, SETTING_KEY, ANY, SETTING_TYPE) -> Boolean
+ * 
+ * Adds a setting by key/value, according to _type.
+ * Assumes as state in http://dev.w3.org/html5/webstorage/#storage
+ * 
+ * **_login** the user<br/>
+ * **_key** the key<br/>
+ * **_value** the value, my be of any type<br/>
+ * **_type** the type<br/>
+ * returns true on success, otherwise false.
+ **/
+bookmaak.util.addSetting = function(_login, _key, _value, _type){
+	
+	if(typeof _login == 'undefined')
+		throw "_login is undefined.";
+	else if(Object.prototype.toString.call(_login) == "[object String]")
+			var login = _login;
+	else
+		throw "illegal argument _login.";
+	
+	if(typeof _key == 'undefined')
+		throw "_key is undefined.";
+	else if(Object.prototype.toString.call(_key) == "[object String]") {
+		//check _key
+		switch(_key) {
+			case bookmaak.util.SETTING_KEY.LOGIN:
+			case bookmaak.util.SETTING_KEY.PRIVATE_KEY:
+			case bookmaak.util.SETTING_KEY.PUBLIC_KEY:
+				var key = _key;
+				break;
+			default:
+				throw "illegal key.";
+		}
+	}
+	else
+		throw "illegal argument _key.";
+	
+	if(typeof _type == 'undefined')
+		throw "_type is undefined.";
+	else if(Object.prototype.toString.call(_type) == "[object String]") {
+		//check _type
+		switch(_type) {
+			case bookmaak.util.SETTING_TYPE.USER:
+			case bookmaak.util.SETTING_TYPE.GLOBAL:
+				var type = _type;
+				break;
+			default:
+				throw "illegal type.";
+		}
+	}
+	else
+		throw "illegal argument _type.";
+	
+	if(typeof _value == 'undefined')
+		throw "_value is undefined.";
+	else
+		var value = _value;
+	
+	return addSetting(tpye, key+"_"+login, value);
+};
+
+/**
+ * bookmaak.util.getSetting(String, SETTING_KEY, SETTING_TYPE) -> ANY | null
+ * 
+ * Retrieves a setting by key, according to _type.
+ * Assumes as state in http://dev.w3.org/html5/webstorage/#storage
+ * 
+ * **_login** the user<br/>
+ * **_key** the key<br/>
+ * **_type** the type<br/>
+ * returns true on success, otherwise false.
+ **/
+bookmaak.util.getSetting = function(_login, _key, _type) {
+	
+	if(typeof _login == 'undefined')
+		throw "_login is undefined.";
+	else if(Object.prototype.toString.call(_login) == "[object String]")
+			var login = _login;
+	else
+		throw "illegal argument _login.";
+	
+	if(typeof _key == 'undefined')
+		throw "_key is undefined.";
+	else if(Object.prototype.toString.call(_key) == "[object String]") {
+		//check _key
+		switch(_key) {
+			case bookmaak.util.SETTING_KEY.LOGIN:
+			case bookmaak.util.SETTING_KEY.PRIVATE_KEY:
+			case bookmaak.util.SETTING_KEY.PUBLIC_KEY:
+				var key = _key;
+				break;
+			default:
+				throw "illegal key.";
+		}
+	}
+	else
+		throw "illegal argument _key.";
+	
+	if(typeof _type == 'undefined')
+		throw "_type is undefined.";
+	else if(Object.prototype.toString.call(_type) == "[object String]") {
+		//check _type
+		switch(_type) {
+			case bookmaak.util.SETTING_TYPE.USER:
+			case bookmaak.util.SETTING_TYPE.GLOBAL:
+				var type = _type;
+				break;
+			default:
+				throw "illegal type.";
+		}
+	}
+	else
+		throw "illegal argument _type.";
+	
+	
+	return getSetting(type, key+"_"+login);
+	
+};
+
+/**
+ * bookmaak.util.removeSetting(String, SETTING_KEY, SETTING_TYPE) -> Boolean
+ * 
+ * Removes a setting by key, according to _type.
+ * Assumes as state in http://dev.w3.org/html5/webstorage/#storage
+ * 
+ * **_login** the user<br/>
+ * **_key** the key<br/>
+ * **_type** the type<br/>
+ * returns true on success, otherwise false.
+ **/
+bookmaak.util.removeSetting = function(_login, _key, _type) {
+	
+	if(typeof _login == 'undefined')
+		throw "_login is undefined.";
+	else if(Object.prototype.toString.call(_login) == "[object String]")
+			var login = _login;
+	else
+		throw "illegal argument _login.";
+	
+	if(typeof _key == 'undefined')
+		throw "_key is undefined.";
+	else if(Object.prototype.toString.call(_key) == "[object String]") {
+		//check _key
+		switch(_key) {
+			case bookmaak.util.SETTING_KEY.LOGIN:
+			case bookmaak.util.SETTING_KEY.PRIVATE_KEY:
+			case bookmaak.util.SETTING_KEY.PUBLIC_KEY:
+				var key = _key;
+				break;
+			default:
+				throw "illegal key.";
+		}
+	}
+	else
+		throw "illegal argument _key.";
+	
+	if(typeof _type == 'undefined')
+		throw "_type is undefined.";
+	else if(Object.prototype.toString.call(_type) == "[object String]") {
+		//check _type
+		switch(_type) {
+			case bookmaak.util.SETTING_TYPE.USER:
+			case bookmaak.util.SETTING_TYPE.GLOBAL:
+				var type = _type;
+				break;
+			default:
+				throw "illegal type.";
+		}
+	}
+	else
+		throw "illegal argument _type.";
+	
+	//remove the key
+	return removeSetting(type, key+"_"+login);
+};
+
+
+function addSetting(key_prefix, key, value) {
+	
+	var prefixed_key = key_prefix + "_" + key;
+	var serialized_value = JSON.stringify(value);
+	
+	console.log("trying to add key: "+prefixed_key+" value: "+serialized_value);
+	
+	try {
+		
+		//send to localstore
+		localStorage.setItem(prefixed_key, serialized_value);
+		console.log("successfully stored key");
+		return true;
+	}
+	catch(e) {//QuotaExceededError
+		
+		console.log("failed to store key");
+		return false;
+	}
+}
+
+function getSetting(key_prefix, key) {
+	
+	var prefixed_key = key_prefix + "_" + key;
+	
+	console.log("trying to get key: "+prefixed_key);
+	
+	//get from localstore
+	var serialized_value = localStorage.getItem(prefixed_key);
+	
+	console.log("got value: "+serialized_value);
+	console.log("got value: "+JSON.pares(serialized_value));
+	
+	if(serialized_value == null)
+		return true;
+	else
+		return JSON.parse(serialized_value);
+}
+
+function removeSetting(key_prefix, key) {
+	
+	var prefixed_key = key_prefix + "_" + key;
+	
+	console.log("removing key: "+prefixed_key);
+	
+	//delete from localstore
+	localStorage.removeItem(prefixed_key);
+	
+	return true;
+}