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>
22 * Marco Bonardo <mak77@bonardo.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 EXPORTED_SYMBOLS = [ "DistributionCustomizer" ];
40 const Ci = Components.interfaces;
41 const Cc = Components.classes;
42 const Cr = Components.results;
43 const Cu = Components.utils;
45 const DISTRIBUTION_CUSTOMIZATION_COMPLETE_TOPIC =
46 "distribution-customization-complete";
48 function DistributionCustomizer() {
49 let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
50 getService(Ci.nsIProperties);
51 let iniFile = dirSvc.get("XCurProcD", Ci.nsIFile);
52 iniFile.append("distribution");
53 iniFile.append("distribution.ini");
55 this._iniFile = iniFile;
58 DistributionCustomizer.prototype = {
62 let ini = Cc["@mozilla.org/xpcom/ini-parser-factory;1"].
63 getService(Ci.nsIINIParserFactory).
64 createINIParser(this._iniFile);
65 this.__defineGetter__("_ini", function() ini);
72 locale = this._prefs.getCharPref("general.useragent.locale");
77 this.__defineGetter__("_locale", function() locale);
82 let svc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
83 getService(Ci.nsINavBookmarksService);
84 this.__defineGetter__("_bmSvc", function() svc);
89 let svc = Cc["@mozilla.org/browser/annotation-service;1"].
90 getService(Ci.nsIAnnotationService);
91 this.__defineGetter__("_annoSvc", function() svc);
96 let svc = Cc["@mozilla.org/browser/livemark-service;2"].
97 getService(Ci.nsILivemarkService);
98 this.__defineGetter__("_livemarkSvc", function() svc);
99 return this._livemarkSvc;
103 let svc = Cc["@mozilla.org/preferences-service;1"].
104 getService(Ci.nsIPrefService);
105 this.__defineGetter__("_prefSvc", function() svc);
106 return this._prefSvc;
110 let branch = this._prefSvc.getBranch(null);
111 this.__defineGetter__("_prefs", function() branch);
116 let svc = Cc["@mozilla.org/network/io-service;1"].
117 getService(Ci.nsIIOService);
118 this.__defineGetter__("_ioSvc", function() svc);
122 _makeURI: function DIST__makeURI(spec) {
123 return this._ioSvc.newURI(spec, null, null);
126 _parseBookmarksSection:
127 function DIST_parseBookmarksSection(parentId, section) {
129 for (let i in enumerate(this._ini.getKeys(section)))
134 let defaultItemId = -1;
137 for (let i = 0; i < keys.length; i++) {
138 let m = /^item\.(\d+)\.(\w+)\.?(\w*)/.exec(keys[i]);
140 let [foo, iid, iprop, ilocale] = m;
148 if (keys.indexOf(keys[i] + "." + this._locale) >= 0) {
149 items[iid][iprop] = this._ini.getString(section, keys[i] + "." +
152 items[iid][iprop] = this._ini.getString(section, keys[i]);
155 if (iprop == "type" && items[iid]["type"] == "default")
161 dump("Key did not match: " + keys[i] + "\n");
165 let prependIndex = 0;
166 for (let iid = 0; iid <= maxItemId; iid++) {
170 let index = this._bmSvc.DEFAULT_INDEX;
173 switch (items[iid]["type"]) {
178 if (iid < defaultItemId)
179 index = prependIndex++;
181 newId = this._bmSvc.createFolder(parentId, items[iid]["title"], index);
183 this._parseBookmarksSection(newId, "BookmarksFolder-" +
184 items[iid]["folderId"]);
186 if (items[iid]["description"])
187 this._annoSvc.setItemAnnotation(newId,
188 "bookmarkProperties/description",
189 items[iid]["description"], 0,
190 this._annoSvc.EXPIRE_NEVER);
195 if (iid < defaultItemId)
196 index = prependIndex++;
197 this._bmSvc.insertSeparator(parentId, index);
201 if (iid < defaultItemId)
202 index = prependIndex++;
204 // Don't bother updating the livemark contents on creation.
205 newId = this._livemarkSvc.
206 createLivemarkFolderOnly(parentId,
208 this._makeURI(items[iid]["siteLink"]),
209 this._makeURI(items[iid]["feedLink"]),
215 if (iid < defaultItemId)
216 index = prependIndex++;
218 newId = this._bmSvc.insertBookmark(parentId,
219 this._makeURI(items[iid]["link"]),
220 index, items[iid]["title"]);
222 if (items[iid]["description"])
223 this._annoSvc.setItemAnnotation(newId,
224 "bookmarkProperties/description",
225 items[iid]["description"], 0,
226 this._annoSvc.EXPIRE_NEVER);
233 _customizationsApplied: false,
234 applyCustomizations: function DIST_applyCustomizations() {
235 this._customizationsApplied = true;
237 return this._checkCustomizationComplete();
239 // nsPrefService loads very early. Reload prefs so we can set
240 // distribution defaults during the prefservice:after-app-defaults
241 // notification (see applyPrefDefaults below)
242 this._prefSvc.QueryInterface(Ci.nsIObserver);
243 this._prefSvc.observe(null, "reload-default-prefs", null);
246 _bookmarksApplied: false,
247 applyBookmarks: function DIST_applyBookmarks() {
248 this._bookmarksApplied = true;
250 return this._checkCustomizationComplete();
252 let sections = enumToObject(this._ini.getSections());
254 // The global section, and several of its fields, is required
255 // (we also check here to be consistent with applyPrefDefaults below)
256 if (!sections["Global"])
257 return this._checkCustomizationComplete();
258 let globalPrefs = enumToObject(this._ini.getKeys("Global"));
259 if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"]))
260 return this._checkCustomizationComplete();
264 bmProcessedPref = this._ini.getString("Global",
265 "bookmarks.initialized.pref");
268 bmProcessedPref = "distribution." +
269 this._ini.getString("Global", "id") + ".bookmarksProcessed";
272 let bmProcessed = false;
274 bmProcessed = this._prefs.getBoolPref(bmProcessedPref);
279 if (sections["BookmarksMenu"])
280 this._parseBookmarksSection(this._bmSvc.bookmarksMenuFolder,
282 if (sections["BookmarksToolbar"])
283 this._parseBookmarksSection(this._bmSvc.toolbarFolder,
285 this._prefs.setBoolPref(bmProcessedPref, true);
287 return this._checkCustomizationComplete();
290 _prefDefaultsApplied: false,
291 applyPrefDefaults: function DIST_applyPrefDefaults() {
292 this._prefDefaultsApplied = true;
294 return this._checkCustomizationComplete();
296 let sections = enumToObject(this._ini.getSections());
298 // The global section, and several of its fields, is required
299 if (!sections["Global"])
300 return this._checkCustomizationComplete();
301 let globalPrefs = enumToObject(this._ini.getKeys("Global"));
302 if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"]))
303 return this._checkCustomizationComplete();
305 let defaults = this._prefSvc.getDefaultBranch(null);
307 // Global really contains info we set as prefs. They're only
308 // separate because they are "special" (read: required)
310 defaults.setCharPref("distribution.id", this._ini.getString("Global", "id"));
311 defaults.setCharPref("distribution.version",
312 this._ini.getString("Global", "version"));
314 let partnerAbout = Cc["@mozilla.org/supports-string;1"].
315 createInstance(Ci.nsISupportsString);
316 if (globalPrefs["about." + this._locale]) {
317 partnerAbout.data = this._ini.getString("Global", "about." + this._locale);
319 partnerAbout.data = this._ini.getString("Global", "about");
321 defaults.setComplexValue("distribution.about",
322 Ci.nsISupportsString, partnerAbout);
324 if (sections["Preferences"]) {
325 for (let key in enumerate(this._ini.getKeys("Preferences"))) {
327 let value = eval(this._ini.getString("Preferences", key));
328 switch (typeof value) {
330 defaults.setBoolPref(key, value);
333 defaults.setIntPref(key, value);
336 defaults.setCharPref(key, value);
339 defaults.setCharPref(key, value);
342 } catch (e) { /* ignore bad prefs and move on */ }
346 // We eval() the localizable prefs as well (even though they'll
347 // always get set as a string) to keep the INI format consistent:
348 // string prefs always need to be in quotes
350 let localizedStr = Cc["@mozilla.org/pref-localizedstring;1"].
351 createInstance(Ci.nsIPrefLocalizedString);
353 if (sections["LocalizablePreferences"]) {
354 for (let key in enumerate(this._ini.getKeys("LocalizablePreferences"))) {
356 let value = eval(this._ini.getString("LocalizablePreferences", key));
357 value = value.replace("%LOCALE%", this._locale, "g");
358 localizedStr.data = "data:text/plain," + key + "=" + value;
359 defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
360 } catch (e) { /* ignore bad prefs and move on */ }
364 if (sections["LocalizablePreferences-" + this._locale]) {
365 for (let key in enumerate(this._ini.getKeys("LocalizablePreferences-" + this._locale))) {
367 let value = eval(this._ini.getString("LocalizablePreferences-" + this._locale, key));
368 localizedStr.data = "data:text/plain," + key + "=" + value;
369 defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
370 } catch (e) { /* ignore bad prefs and move on */ }
374 return this._checkCustomizationComplete();
377 _checkCustomizationComplete: function DIST__checkCustomizationComplete() {
378 let prefDefaultsApplied = this._prefDefaultsApplied || !this._iniFile;
379 if (this._customizationsApplied && this._bookmarksApplied &&
380 prefDefaultsApplied) {
381 let os = Cc["@mozilla.org/observer-service;1"].
382 getService(Ci.nsIObserverService);
383 os.notifyObservers(null, DISTRIBUTION_CUSTOMIZATION_COMPLETE_TOPIC, null);
388 function enumerate(UTF8Enumerator) {
389 while (UTF8Enumerator.hasMore())
390 yield UTF8Enumerator.getNext();
393 function enumToObject(UTF8Enumerator) {
395 for (let i in enumerate(UTF8Enumerator))