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 MozMill Test code.
16 * The Initial Developer of the Original Code is Mozilla Foundation.
17 * Portions created by the Initial Developer are Copyright (C) 2009
18 * the Initial Developer. All Rights Reserved.
21 * Henrik Skupin <hskupin@mozilla.com>
22 * Clint Talbert <ctalbert@mozilla.com>
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 ***** */
40 * The PrefsAPI adds support for preferences related functions. It gives access
41 * to the preferences system and allows to handle the preferences dialog
46 // Include required modules
47 var modalDialog = require("modal-dialog");
48 var utils = require("utils");
51 const gTimeout = 5000;
53 // Preferences dialog element templates
54 const PREF_DIALOG_BUTTONS = '/{"type":"prefwindow"}/anon({"anonid":"dlg-buttons"})';
55 const PREF_DIALOG_DECK = '/{"type":"prefwindow"}/anon({"class":"paneDeckContainer"})/anon({"anonid":"paneDeck"})';
56 const PREF_DIALOG_SELECTOR = '/{"type":"prefwindow"}/anon({"orient":"vertical"})/anon({"anonid":"selector"})';
62 * @param {MozMillController} controller
63 * MozMill controller of the browser window to operate on.
65 function preferencesDialog(controller) {
66 this._controller = controller;
70 * Preferences dialog object to simplify the access to this dialog
72 preferencesDialog.prototype = {
74 * Returns the MozMill controller
76 * @returns Mozmill controller
77 * @type {MozMillController}
80 return this._controller;
84 * Retrieve the currently selected panel
86 * @returns The panel element
90 return this.getElement({type: "deck_pane"});
94 * Get the given pane id
97 // Check if the selector and the pane are consistent
98 var selector = this.getElement({type: "selector"});
100 this._controller.waitForEval("subject.selector.getAttribute('pane') == subject.dlg.selectedPane.getNode().id", gTimeout, 100,
101 {selector: selector.getNode().selectedItem, dlg: this});
103 return this.selectedPane.getNode().id;
107 * Set the given pane by id
109 * @param {string} id of the pane
112 var button = this.getElement({type: "selector_button", value: id});
113 this._controller.waitThenClick(button, gTimeout);
115 // Check if the correct selector is selected
116 var selector = this.getElement({type: "selector"});
117 this._controller.waitForEval("subject.selector.getAttribute('pane') == subject.newPane", gTimeout, 100,
118 {selector: selector.getNode().selectedItem, newPane: id});
123 * Close the preferences dialog
125 * @param {MozMillController} controller
126 * MozMillController of the window to operate on
127 * @param {boolean} saveChanges
128 * (Optional) If true the OK button is clicked on Windows which saves
129 * the changes. On OS X and Linux changes are applied immediately
131 close : function preferencesDialog_close(saveChanges) {
132 saveChanges = (saveChanges == undefined) ? false : saveChanges;
134 if (mozmill.isWindows) {
135 var button = this.getElement({type: "button", subtype: (saveChanges ? "accept" : "cancel")});
136 this._controller.click(button);
138 this._controller.keypress(null, 'w', {accelKey: true});
143 * Gets all the needed external DTD urls as an array
145 * @returns Array of external DTD urls
148 getDtds : function preferencesDialog_getDtds() {
153 * Retrieve an UI element based on the given spec
155 * @param {object} spec
156 * Information of the UI element which should be retrieved
157 * type: General type information
158 * subtype: Specific element or property
159 * value: Value of the element or property
160 * @returns Element which has been created
163 getElement : function aboutSessionRestore_getElement(spec) {
168 elem = new elementslib.Lookup(this._controller.window.document, PREF_DIALOG_BUTTONS +
169 '/{"dlgtype":"' + spec.subtype + '"}');
172 elem = new elementslib.Lookup(this._controller.window.document, PREF_DIALOG_DECK);
175 var deck = this.getElement({type: "deck"}).getNode();
177 // XXX: Bug 390724 - selectedPane is broken. So iterate through all elements
178 var panel = deck.boxObject.firstChild;
179 for (var ii = 0; ii < deck.selectedIndex; ii++)
180 panel = panel.nextSibling;
182 elem = new elementslib.Elem(panel);
185 elem = new elementslib.Lookup(this._controller.window.document, PREF_DIALOG_SELECTOR);
187 case "selector_button":
188 elem = new elementslib.Lookup(this._controller.window.document, PREF_DIALOG_SELECTOR +
189 '/{"pane":"' + spec.value + '"}');
192 throw new Error(arguments.callee.name + ": Unknown element type - " + spec.type);
200 * Preferences object to simplify the access to the nsIPrefBranch.
203 _prefService : Cc["@mozilla.org/preferences-service;1"].
204 getService(Ci.nsIPrefService),
207 * Use branch to access low level functions of nsIPrefBranch
209 * @return Instance of the preferences branch
210 * @type nsIPrefBranch
213 return this._prefService.QueryInterface(Ci.nsIPrefBranch);
217 * Use defaultPrefBranch to access low level functions of the default branch
219 * @return Instance of the preferences branch
220 * @type nsIPrefBranch
222 get defaultPrefBranch() {
223 return this._prefService.getDefaultBranch("");
227 * Use prefService to access low level functions of nsIPrefService
229 * @return Instance of the pref service
230 * @type nsIPrefService
233 return this._prefService;
237 * Clear a user set preference
239 * @param {string} prefName
240 * The user-set preference to clear
241 * @return False if the preference had the default value
244 clearUserPref : function preferences_clearUserPref(prefName) {
246 this.prefBranch.clearUserPref(prefName);
254 * Retrieve the value of an individual preference.
256 * @param {string} prefName
257 * The preference to get the value of.
258 * @param {boolean/number/string} defaultValue
259 * The default value if preference cannot be found.
260 * @param {boolean/number/string} defaultBranch
261 * If true the value will be read from the default branch (optional)
262 * @param {string} interfaceType
263 * Interface to use for the complex value (optional)
264 * (nsILocalFile, nsISupportsString, nsIPrefLocalizedString)
266 * @return The value of the requested preference
267 * @type boolean/int/string/complex
269 getPref : function preferences_getPref(prefName, defaultValue, defaultBranch,
272 branch = defaultBranch ? this.defaultPrefBranch : this.prefBranch;
274 // If interfaceType has been set, handle it differently
275 if (interfaceType != undefined) {
276 return branch.getComplexValue(prefName, interfaceType);
279 switch (typeof defaultValue) {
281 return branch.getBoolPref(prefName);
283 return branch.getCharPref(prefName);
285 return branch.getIntPref(prefName);
295 * Set the value of an individual preference.
297 * @param {string} prefName
298 * The preference to set the value of.
299 * @param {boolean/number/string/complex} value
300 * The value to set the preference to.
301 * @param {string} interfaceType
302 * Interface to use for the complex value
303 * (nsILocalFile, nsISupportsString, nsIPrefLocalizedString)
305 * @return Returns if the value was successfully set.
308 setPref : function preferences_setPref(prefName, value, interfaceType) {
310 switch (typeof value) {
312 this.prefBranch.setBoolPref(prefName, value);
315 this.prefBranch.setCharPref(prefName, value);
318 this.prefBranch.setIntPref(prefName, value);
321 this.prefBranch.setComplexValue(prefName, interfaceType, value);
332 * Open the preferences dialog and call the given handler
334 * @param {MozMillController} controller
335 * MozMillController which is the opener of the preferences dialog
336 * @param {function} callback
337 * The callback handler to use to interact with the preference dialog
338 * @param {function} launcher
339 * (Optional) A callback handler to launch the preference dialog
341 function openPreferencesDialog(controller, callback, launcher) {
343 throw new Error("No controller given for Preferences Dialog");
344 if (typeof callback != "function")
345 throw new Error("No callback given for Preferences Dialog");
347 if (mozmill.isWindows) {
348 // Preference dialog is modal on windows, set up our callback
349 var prefModal = new modalDialog.modalDialog(controller.window);
350 prefModal.start(callback);
353 // Launch the preference dialog
357 mozmill.getPreferencesController();
360 if (mozmill.isWindows) {
361 prefModal.waitForDialog();
363 // Get the window type of the preferences window depending on the application
364 var prefWindowType = null;
365 switch (mozmill.Application) {
367 prefWindowType = "Mail:Preferences";
370 prefWindowType = "Browser:Preferences";
373 utils.handleWindow("type", prefWindowType, callback);
377 // Export of variables
378 exports.preferences = preferences;
380 // Export of functions
381 exports.openPreferencesDialog = openPreferencesDialog;
384 exports.preferencesDialog = preferencesDialog;