option storage abstraction

This commit is contained in:
Rodolphe Breard 2012-12-18 16:27:29 +01:00
parent d250c46523
commit 6cc8a6f10c
2 changed files with 97 additions and 61 deletions

View file

@ -15,7 +15,6 @@
// //
var NsClient = function() { var NsClient = function() {
this.storage = chrome.storage.sync;
this.state = "actif"; this.state = "actif";
this.allowed_statuses = ["actif", "away", "idle", "lock"]; this.allowed_statuses = ["actif", "away", "idle", "lock"];
this.is_connected = false; this.is_connected = false;
@ -39,23 +38,21 @@ var NsClient = function() {
}; };
this.actions.msg.act = (function(elem) { this.actions.msg.act = (function(elem) {
return function(msg) { return function(msg) {
elem.storage.get('enable_msg', function(infos) { if ($cs.opts.get("enable_msg")) {
if (infos.enable_msg) { var mo = {}, exp = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| msg ([^ ]*)/;
var mo = {}, exp = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| msg ([^ ]*)/;
mch = exp.exec(msg); mch = exp.exec(msg);
if (mch !== null && typeof elem.msgHandler !== "undefined") { if (mch !== null && typeof elem.msgHandler !== "undefined") {
mo.socket = mch[1]; mo.socket = mch[1];
mo.login = mch[2]; mo.login = mch[2];
mo.host = mch[3]; mo.host = mch[3];
mo.location = elem.msgDecode(mch[4]); mo.location = elem.msgDecode(mch[4]);
mo.group = mch[5]; mo.group = mch[5];
mo.message = elem.msgDecode(mch[6]); mo.message = elem.msgDecode(mch[6]);
elem.msgHandler(mo); elem.msgHandler(mo);
} }
} }
});
}; };
})(this); })(this);
}; };
@ -73,43 +70,45 @@ NsClient.prototype.msgEncode = function(msg) {
}; };
NsClient.prototype.connect = function() { NsClient.prototype.connect = function() {
var login, pwd_socks;
if (!this.is_connected) { if (!this.is_connected) {
this.storage.get(null, (function(elem) { login = $cs.opts.get('login');
return function(infos) { pwd_socks = $cs.opts.get('pwd_socks');
if (typeof infos.login !== "undefined" && typeof infos.pwd_socks !== "undefined") { if (login !== null && pwd_socks !== null) {
chrome.socket.create('tcp', {}, function(sock_inf) { chrome.socket.create('tcp', {}, (function(elem) {
elem.socket = sock_inf.socketId; return function(sock_inf) {
chrome.socket.connect(elem.socket, "ns-server.epita.fr", 4242, function(res) { elem.socket = sock_inf.socketId;
chrome.socket.read(elem.socket, null, function(rd_inf) { chrome.socket.connect(elem.socket, "ns-server.epita.fr", 4242, function(res) {
if (rd_inf.resultCode > 0) { chrome.socket.read(elem.socket, null, function(rd_inf) {
var data = ab2str(rd_inf.data).split(' '), if (rd_inf.resultCode > 0) {
auth = "ext_user_log "; var data = ab2str(rd_inf.data).split(' '),
auth += infos.login + " "; auth = "ext_user_log ";
auth += hex_md5(data[2] + "-" + data[3] + "/" + data[4] + infos.pwd_socks) + " "; auth += login + " ";
auth += "chromesoul chromesoul\n"; auth += hex_md5(data[2] + "-" + data[3] + "/" + data[4] + pwd_socks) + " ";
chrome.socket.write(elem.socket, str2ab("auth_ag ext_user none none\n"), function(w_inf) { auth += "chromesoul chromesoul\n";
chrome.socket.read(elem.socket, null, function(rd_inf) { chrome.socket.write(elem.socket, str2ab("auth_ag ext_user none none\n"), function(w_inf) {
if (rd_inf.resultCode > 0) { chrome.socket.read(elem.socket, null, function(rd_inf) {
chrome.socket.write(elem.socket, str2ab(auth), function(w_inf) { if (rd_inf.resultCode > 0) {
chrome.socket.read(elem.socket, null, function(rd_inf) { chrome.socket.write(elem.socket, str2ab(auth), function(w_inf) {
if (rd_inf.resultCode > 0) { chrome.socket.read(elem.socket, null, function(rd_inf) {
elem.is_connected = true; if (rd_inf.resultCode > 0) {
console.info("connected to the netsoul server"); elem.is_connected = true;
elem.updateStatus(); console.info("connected to the netsoul server");
elem.daemonize(); elem.updateStatus();
} elem.daemonize();
}); }
}); });
} });
}); }
}); });
} });
}); }
}); });
}); });
} };
}; })(this));
})(this)); }
} }
}; };
@ -203,7 +202,7 @@ NsClient.prototype.init = function() {
}; };
status_update(this).apply(); status_update(this).apply();
connect(this).apply(); setTimeout(connect(this), 500);
setInterval(connect(this), 10000); setInterval(connect(this), 10000);
setInterval(status_update(this), 600000); setInterval(status_update(this), 600000);
}; };

View file

@ -16,9 +16,11 @@
var OptionsManager = function() { var OptionsManager = function() {
this.storage = chrome.storage.sync; this.storage = chrome.storage.sync;
this.pass_storage = chrome.storage.local;
this.status = null; this.status = null;
this.save_btn = null; this.save_btn = null;
this.opts = null; this.opts = null;
this.values = {};
}; };
OptionsManager.prototype.types = { OptionsManager.prototype.types = {
@ -50,6 +52,16 @@ OptionsManager.prototype.types = {
} }
}; };
OptionsManager.prototype.get = function(name) {
var ret = null;
if (typeof this.values[name] !== "undefined") {
ret = this.values[name];
}
return ret;
};
OptionsManager.prototype.init = function() { OptionsManager.prototype.init = function() {
this.status = document.getElementById("status"); this.status = document.getElementById("status");
this.save_btn = document.getElementById("save"); this.save_btn = document.getElementById("save");
@ -66,14 +78,24 @@ OptionsManager.prototype.init = function() {
} }
}; };
OptionsManager.prototype.save = function() { OptionsManager.prototype.savePart = function(pass) {
var i = 0, data = {}; var i = 0, data = {}, storage;
if (pass) {
storage = this.pass_storage;
} else {
storage = this.storage;
}
if (this.opts !== null) { if (this.opts !== null) {
for (i = this.opts.length - 1; i >= 0; --i) { for (i = this.opts.length - 1; i >= 0; --i) {
data[this.opts[i].id] = this.getElemValue(this.opts[i]); if ((pass && this.getElemType(this.opts[i]) === "password")
|| (!pass && this.getElemType(this.opts[i]) !== "password")) {
data[this.opts[i].id] = this.getElemValue(this.opts[i]);
this.values[this.opts[i].id] = data[this.opts[i].id];
}
} }
this.storage.set(data, (function(elem) { storage.set(data, (function(elem) {
return function () { return function () {
if (elem !== null) { if (elem !== null) {
elem.innerHTML = "Options saved."; elem.innerHTML = "Options saved.";
@ -86,23 +108,38 @@ OptionsManager.prototype.save = function() {
} }
}; };
OptionsManager.prototype.restore = function() { OptionsManager.prototype.save = function() {
var i, el; this.savePart(true);
this.savePart(false);
};
this.storage.get(null, (function(elem) { OptionsManager.prototype.restore = function() {
var i, el, cb = function(elem) {
return function(items) { return function(items) {
console.log('----------------------------------');
console.log('restoring...');
console.log(items);
for (i in items) { for (i in items) {
el = document.getElementById(i); el = document.getElementById(i);
if (el !== null) { if (el !== null) {
elem.values[i] = items[i];
elem.setElemValue(el, items[i]); elem.setElemValue(el, items[i]);
} }
} }
console.log('----------------------------------');
}; };
})(this)); };
this.storage.get(null, cb(this));
this.pass_storage.get(null, cb(this));
};
OptionsManager.prototype.getElemType = function(elem) {
return elem.getAttribute('type');
}; };
OptionsManager.prototype.getElemValue = function(elem) { OptionsManager.prototype.getElemValue = function(elem) {
var val = null, type = elem.getAttribute('type'); var val = null, type = this.getElemType(elem);
if (typeof this.types.get[type] !== "undefined") { if (typeof this.types.get[type] !== "undefined") {
val = this.types.get[type](elem); val = this.types.get[type](elem);