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

Side by Side Diff: lib/prefs.js

Issue 10802049: Functional prefs implementation (Closed)
Patch Set: Cleaned up init.js a bit Created June 5, 2013, 9:44 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
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 update_url_release: "https://updates.adblockplus.org/%NAME%/update.json",
37 update_url_devbuild: "https://adblockplus.org/devbuilds/%s/update.json",
38 last_update: 0
36 }; 39 };
40
41 let values = Object.create(defaults);
42 let path = _fileSystem.resolve("prefs.json");
43 let listeners = [];
44 let isDirty = false;
45 let isSaving = false;
46
47 function defineProperty(key)
48 {
49 Prefs.__defineGetter__(key, function() values[key]);
50 Prefs.__defineSetter__(key, function(value)
51 {
52 if (typeof value != typeof defaults[key])
53 throw new Error("Attempt to change preference type");
54
55 if (value == defaults[key])
56 delete values[key];
57 else
58 values[key] = value;
59 save();
60
61 for each (let listener in listeners)
62 listener(key);
63 });
64 }
65
66 function load()
67 {
68 _fileSystem.read(path, function(result)
69 {
70 // prefs.json is expected to be missing, ignore errors reading file
71 if (!result.error)
72 {
73 try
74 {
75 let data = JSON.parse(result.content);
76 for (let key in data)
77 if (key in defaults)
78 values[key] = data[key];
79 }
80 catch (e)
81 {
82 Cu.reportError(e);
83 }
84 }
85
86 if (typeof Prefs._initListener == "function")
87 Prefs._initListener();
88 });
89 }
90
91 function save()
92 {
93 if (isSaving)
94 {
95 isDirty = true;
96 return;
97 }
98
99 isDirty = false;
100 isSaving = true;
101 _fileSystem.write(path, JSON.stringify(values), function()
102 {
103 isSaving = false;
104 if (isDirty)
105 save();
106 });
107 }
108
109 let Prefs = exports.Prefs = {
110 addListener: function(listener)
111 {
112 if (listeners.indexOf(listener) < 0)
113 listeners.push(listener);
114 },
115
116 removeListener: function(listener)
117 {
118 let index = listeners.indexOf(listener);
119 if (index >= 0)
120 listeners.splice(index, 1);
121 },
122 };
123
124 for (let key in defaults)
125 defineProperty(key);
126
127 load();
OLDNEW

Powered by Google App Engine
This is Rietveld