1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Firefox Distribution Customizations.
16 * The Initial Developer of the Original Code is Mozilla Foundation.
17 * Portions created by the Initial Developer are Copyright (C) 2007
18 * the Initial Developer. All Rights Reserved.
21 * Dan Mills <thunder@mozilla.com>
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 EXPORTED_SYMBOLS = [ "DistributionCustomizer" ];
39 const Ci = Components.interfaces;
40 const Cc = Components.classes;
41 const Cr = Components.results;
42 const Cu = Components.utils;
44 function DistributionCustomizer() {
45 this._distroDir = this._dirSvc.get("XCurProcD", Ci.nsIFile);
46 this._distroDir.append("distribution");
48 let iniFile = this._distroDir.clone();
49 iniFile.append("distribution.ini");
50 this._iniExists = iniFile.exists();
55 this._ini = Cc["@mozilla.org/xpcom/ini-parser-factory;1"].
56 getService(Ci.nsIINIParserFactory).createINIParser(iniFile);
58 this._prefs = this._prefSvc.getBranch(null);
59 this._locale = this._prefs.getCharPref("general.useragent.locale");
62 DistributionCustomizer.prototype = {
66 this.__bmSvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
67 getService(Ci.nsINavBookmarksService);
74 this.__annoSvc = Cc["@mozilla.org/browser/annotation-service;1"].
75 getService(Ci.nsIAnnotationService);
76 return this.__annoSvc;
82 this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"].
83 getService(Ci.nsIProperties);
90 this.__prefSvc = Cc["@mozilla.org/preferences-service;1"].
91 getService(Ci.nsIPrefService);
92 return this.__prefSvc;
98 this.__iosvc = Cc["@mozilla.org/network/io-service;1"].
99 getService(Ci.nsIIOService);
109 _makeURI: function DIST__makeURI(spec) {
110 return this._iosvc.newURI(spec, null, null);
112 _parseBookmarksSection: function DIST_parseBookmarksSection(parentId, section) {
114 for (let i in enumerate(this._ini.getKeys(section)))
118 let defaultItemId = -1;
121 for (let i = 0; i < keys.length; i++) {
122 let m = /^item\.(\d+)\.(\w+)\.?(\w*)/.exec(keys[i]);
124 let [foo, iid, iprop, ilocale] = m;
131 if (keys.indexOf(keys[i] + "." + this._locale) >= 0) {
132 items[iid][iprop] = this._ini.getString(section, keys[i] + "." +
135 items[iid][iprop] = this._ini.getString(section, keys[i]);
138 if (iprop == "type" && items[iid]["type"] == "default")
144 dump("Key did not match: " + keys[i] + "\n");
148 let prependIndex = 0;
149 for (let iid = 0; iid <= maxItemId; iid++) {
156 switch (items[iid]["type"]) {
161 if (iid < defaultItemId)
162 index = prependIndex++;
164 newId = this._bmSvc.createFolder(parentId, items[iid]["title"], index);
166 this._parseBookmarksSection(newId, "BookmarksFolder-" +
167 items[iid]["folderId"]);
169 if (items[iid]["description"])
170 this._annoSvc.setItemAnnotation(newId, "bookmarkProperties/description",
171 items[iid]["description"], 0,
172 this._annoSvc.EXPIRE_NEVER);
177 if (iid < defaultItemId)
178 index = prependIndex++;
179 this._bmSvc.insertSeparator(parentId, index);
184 if (iid < defaultItemId)
185 index = prependIndex++;
187 newId = this._bmSvc.insertBookmark(parentId,
188 this._makeURI(items[iid]["link"]),
189 index, items[iid]["title"]);
191 if (items[iid]["description"])
192 this._annoSvc.setItemAnnotation(newId, "bookmarkProperties/description",
193 items[iid]["description"], 0,
194 this._annoSvc.EXPIRE_NEVER);
200 applyCustomizations: function DIST_applyCustomizations() {
201 if (!this._iniExists)
204 // nsPrefService loads very early. Reload prefs so we can set
205 // distribution defaults during the prefservice:after-app-defaults
206 // notification (see applyPrefDefaults below)
207 this._prefSvc.QueryInterface(Ci.nsIObserver);
208 this._prefSvc.observe(null, "reload-default-prefs", null);
210 let sections = enumToObject(this._ini.getSections());
212 // The global section, and several of its fields, is required
213 // (we also check here to be consistent with applyPrefDefaults below)
214 if (!sections["Global"])
216 let globalPrefs = enumToObject(this._ini.getKeys("Global"));
217 if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"]))
220 let bmProcessed = false;
221 let bmProcessedPref = "distribution." +
222 this._ini.getString("Global", "id") + ".bookmarksProcessed";
224 bmProcessed = this._prefs.getBoolPref(bmProcessedPref);
228 if (sections["BookmarksMenu"])
229 this._parseBookmarksSection(this._bmSvc.bookmarksMenuFolder,
231 if (sections["BookmarksToolbar"])
232 this._parseBookmarksSection(this._bmSvc.toolbarFolder,
234 this._prefs.setBoolPref(bmProcessedPref, true);
237 applyPrefDefaults: function DIST_applyPrefDefaults() {
238 if (!this._iniExists)
241 let sections = enumToObject(this._ini.getSections());
243 // The global section, and several of its fields, is required
244 if (!sections["Global"])
246 let globalPrefs = enumToObject(this._ini.getKeys("Global"));
247 if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"]))
250 let defaults = this._prefSvc.getDefaultBranch(null);
252 // Global really contains info we set as prefs. They're only
253 // separate because they are "special" (read: required)
255 defaults.setCharPref("distribution.id", this._ini.getString("Global", "id"));
256 defaults.setCharPref("distribution.version",
257 this._ini.getString("Global", "version"));
259 let partnerAbout = Cc["@mozilla.org/supports-string;1"].
260 createInstance(Ci.nsISupportsString);
261 if (globalPrefs["about." + this._locale]) {
262 partnerAbout.data = this._ini.getString("Global", "about." + this._locale);
264 partnerAbout.data = this._ini.getString("Global", "about");
266 defaults.setComplexValue("distribution.about",
267 Ci.nsISupportsString, partnerAbout);
269 if (sections["Preferences"]) {
270 for (let key in enumerate(this._ini.getKeys("Preferences"))) {
272 let value = eval(this._ini.getString("Preferences", key));
273 switch (typeof value) {
275 defaults.setBoolPref(key, value);
278 defaults.setIntPref(key, value);
281 defaults.setCharPref(key, value);
284 defaults.setCharPref(key, value);
287 } catch (e) { /* ignore bad prefs and move on */ }
291 // We eval() the localizable prefs as well (even though they'll
292 // always get set as a string) to keep the INI format consistent:
293 // string prefs always need to be in quotes
295 let localizedStr = Cc["@mozilla.org/pref-localizedstring;1"].
296 createInstance(Ci.nsIPrefLocalizedString);
298 if (sections["LocalizablePreferences"]) {
299 for (let key in enumerate(this._ini.getKeys("LocalizablePreferences"))) {
301 let value = eval(this._ini.getString("LocalizablePreferences", key));
302 value = value.replace("%LOCALE%", this._locale, "g");
303 localizedStr.data = "data:text/plain," + key + "=" + value;
304 defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
305 } catch (e) { /* ignore bad prefs and move on */ }
309 if (sections["LocalizablePreferences-" + this._locale]) {
310 for (let key in enumerate(this._ini.getKeys("LocalizablePreferences-" + this._locale))) {
312 let value = eval(this._ini.getString("LocalizablePreferences-" + this._locale, key));
313 localizedStr.data = "data:text/plain," + key + "=" + value;
314 defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
315 } catch (e) { /* ignore bad prefs and move on */ }
321 function enumerate(UTF8Enumerator) {
322 while (UTF8Enumerator.hasMore())
323 yield UTF8Enumerator.getNext();
326 function enumToObject(UTF8Enumerator) {
328 for (let i in enumerate(UTF8Enumerator))