Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/prefs.js

Issue 11159032: Implemented prefs module properly, data is actually being persisted now (Closed)
Patch Set: Created July 19, 2013, 4:12 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 // 18 //
19 // The values are hardcoded for now. 19 // The values are hardcoded for now.
20 // 20 //
21 21
22 var Prefs = exports.Prefs = { 22 let defaults = {
23 __proto__: null,
23 enabled: true, 24 enabled: true,
24 patternsfile: "patterns.ini", 25 patternsfile: "patterns.ini",
25 patternsbackups: 5, 26 patternsbackups: 5,
26 patternsbackupinterval: 24, 27 patternsbackupinterval: 24,
27 data_directory: "", 28 data_directory: "",
28 savestats: false, 29 savestats: false,
29 privateBrowsing: false, 30 privateBrowsing: false,
30 subscriptions_fallbackerrors: 5, 31 subscriptions_fallbackerrors: 5,
31 subscriptions_fallbackurl: "https://adblockplus.org/getSubscription?version=%V ERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNE LSTATUS%&responseStatus=%RESPONSESTATUS%", 32 subscriptions_fallbackurl: "https://adblockplus.org/getSubscription?version=%V ERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNE LSTATUS%&responseStatus=%RESPONSESTATUS%",
32 subscriptions_autoupdate: true, 33 subscriptions_autoupdate: true,
33 subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/excep tionrules.txt", 34 subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/excep tionrules.txt",
34 documentation_link: "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%" , 35 documentation_link: "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%" ,
35 addListener: function() {}
36 }; 36 };
37
38 let listeners = [];
39
40 function defineProperty(key)
41 {
42 Prefs.__defineGetter__(key, function()
43 {
44 if (key in localStorage)
45 {
46 try
47 {
48 return JSON.parse(localStorage[key]);
49 }
50 catch(e)
51 {
52 Cu.reportError(e);
53 }
54 }
55 return defaults[key];
56 });
57 Prefs.__defineSetter__(key, function(value)
58 {
59 if (typeof value != typeof defaults[key])
60 throw new Error("Attempt to change preference type");
Thomas Greiner 2013/07/25 13:20:36 I'd also include a check for the existence of the
Wladimir Palant 2013/07/26 09:44:02 The property handler is only added for properties
61
62 let stringified = JSON.stringify(value);
63 if (stringified != JSON.stringify(defaults[key]))
64 localStorage[key] = stringified;
65 else
66 delete localStorage[key];
67 for each (let listener in listeners)
68 listener(key);
69 });
70 }
71
72
73 let Prefs = exports.Prefs = {
74 addListener: function(listener)
75 {
76 if (listeners.indexOf(listener) < 0)
77 listeners.push(listener);
78 },
79
80 removeListener: function(listener)
81 {
82 let index = listeners.indexOf(listener);
83 if (index >= 0)
84 listeners.splice(index, 1);
85 },
86 };
87
88 for (let key in defaults)
89 defineProperty(key);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld