aboutsummaryrefslogtreecommitdiff
path: root/chrome/js/redirectorprefs.js
blob: b456bacb463afe1c198c9ac6fde20b99527de8d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var EXPORTED_SYMBOLS = ['RedirectorPrefs'];

function RedirectorPrefs() {
	this.init();
}

RedirectorPrefs.prototype = {
	_listeners : null,

	watch : function(prop_name, cb) {
		Object.defineProperty(this, prop_name, {
			configurable: true,
			set: function(val) {
				cb(prop_name, null, val);
			},
			get: function() {
				let pb = this._prefBranch;
				switch (pb.getPrefType(prop_name)) {
				case pb.PREF_STRING:
					return pb.getCharPref(prop_name);
					break;
				case pb.PREF_INT:
					return pb.getIntPref(prop_name);
					break;
				case pb.PREF_BOOL:
					return pb.getBoolPref(prop_name);
					break;
				}
			}});
	},

	unwatch : function(prop_name) {
		delete this[prop_name];
	},

	init : function() {
		this._prefBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.redirector.");
		this.reload();
		this._listeners = [];
		this.service.addObserver('extensions.redirector', this, false);
	},

	dispose : function() {
		this._listeners = null;
		this.service.removeObserver('extensions.redirector', this);
	},

	reload : function(addWatch) {
		var b = this._prefBranch;
		for each (var name in b.getChildList('')){
			this.unwatch(name);
			// var type = b.getPrefType(name);
			// if (type == b.PREF_STRING) {
			// 	this[name] = b.getCharPref(name);
			// } else if (type == b.PREF_INT) {
			// 	this[name] = b.getIntPref(name);
			// } else if (type == b.PREF_BOOL) {
			// 	this[name] = b.getBoolPref(name);
			// }

			this.watch(name, function(id,oldval,newval) {
				var type = b.getPrefType(id);
				if (type == b.PREF_STRING) {
					b.setCharPref(id,newval);
				} else if (type == b.PREF_INT) {
					b.setIntPref(id, newval);
				} else if (type == b.PREF_BOOL) {
					dump(id+ ' ' + newval)
					b.setBoolPref(id, newval);
				}
				return newval;
			});
		}
	},

	get service() {
		return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranchInternal);
	},

	observe : function(subject, topic, data) {
		if (topic != 'nsPref:changed') {
			return;
		}
		this.reload(false);
		for each (var listener in this._listeners) {
			listener && listener.changedPrefs && listener.changedPrefs(this);
		}
	},

	addListener : function(listener) {
		this._listeners.push(listener);
	},

	removeListener : function(listener) {
		for (var i = 0; i < this._listeners.length; i++) {
				this._listeners.splice(i,1);
			if (this._listeners[i] == listener) {
				return;
			}
		}
	},
}