michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: from xml.dom.minidom import Document michael@0: michael@0: VALID_PREF_TYPES = ['bool', 'boolint', 'integer', 'string', 'color', 'file', michael@0: 'directory', 'control', 'menulist', 'radio'] michael@0: michael@0: class Error(Exception): michael@0: pass michael@0: michael@0: class BadPrefTypeError(Error): michael@0: pass michael@0: michael@0: class MissingPrefAttr(Error): michael@0: pass michael@0: michael@0: def validate_prefs(options): michael@0: for pref in options: michael@0: # Make sure there is a 'title' michael@0: if ("title" not in pref): michael@0: raise MissingPrefAttr("The '%s' pref requires a 'title'" % (pref["name"])) michael@0: michael@0: # Make sure that the pref type is a valid inline pref type michael@0: if (pref["type"] not in VALID_PREF_TYPES): michael@0: raise BadPrefTypeError('%s is not a valid inline pref type' % (pref["type"])) michael@0: michael@0: # Make sure the 'control' type has a 'label' michael@0: if (pref["type"] == "control"): michael@0: if ("label" not in pref): michael@0: raise MissingPrefAttr("The 'control' inline pref type requires a 'label'") michael@0: michael@0: # Make sure the 'menulist' type has a 'menulist' michael@0: if (pref["type"] == "menulist" or pref["type"] == "radio"): michael@0: if ("options" not in pref): michael@0: raise MissingPrefAttr("The 'menulist' and the 'radio' inline pref types requires a 'options'") michael@0: michael@0: # Make sure each option has a 'value' and a 'label' michael@0: for item in pref["options"]: michael@0: if ("value" not in item): michael@0: raise MissingPrefAttr("'options' requires a 'value'") michael@0: if ("label" not in item): michael@0: raise MissingPrefAttr("'options' requires a 'label'") michael@0: michael@0: # TODO: Check that pref["type"] matches default value type michael@0: michael@0: def parse_options(options, jetpack_id, preferencesBranch): michael@0: doc = Document() michael@0: root = doc.createElement("vbox") michael@0: root.setAttribute("xmlns", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul") michael@0: doc.appendChild(root) michael@0: michael@0: for pref in options: michael@0: if ("hidden" in pref and pref["hidden"] == True): michael@0: continue; michael@0: michael@0: setting = doc.createElement("setting") michael@0: setting.setAttribute("pref-name", pref["name"]) michael@0: setting.setAttribute("data-jetpack-id", jetpack_id) michael@0: setting.setAttribute("pref", "extensions." + preferencesBranch + "." + pref["name"]) michael@0: setting.setAttribute("type", pref["type"]) michael@0: setting.setAttribute("title", pref["title"]) michael@0: michael@0: if ("description" in pref): michael@0: setting.appendChild(doc.createTextNode(pref["description"])) michael@0: michael@0: if (pref["type"] == "control"): michael@0: button = doc.createElement("button") michael@0: button.setAttribute("pref-name", pref["name"]) michael@0: button.setAttribute("data-jetpack-id", jetpack_id) michael@0: button.setAttribute("label", pref["label"]) michael@0: button.setAttribute("oncommand", "Services.obs.notifyObservers(null, '" + michael@0: jetpack_id + "-cmdPressed', '" + michael@0: pref["name"] + "');"); michael@0: setting.appendChild(button) michael@0: elif (pref["type"] == "boolint"): michael@0: setting.setAttribute("on", pref["on"]) michael@0: setting.setAttribute("off", pref["off"]) michael@0: elif (pref["type"] == "menulist"): michael@0: menulist = doc.createElement("menulist") michael@0: menupopup = doc.createElement("menupopup") michael@0: for item in pref["options"]: michael@0: menuitem = doc.createElement("menuitem") michael@0: menuitem.setAttribute("value", item["value"]) michael@0: menuitem.setAttribute("label", item["label"]) michael@0: menupopup.appendChild(menuitem) michael@0: menulist.appendChild(menupopup) michael@0: setting.appendChild(menulist) michael@0: elif (pref["type"] == "radio"): michael@0: radiogroup = doc.createElement("radiogroup") michael@0: for item in pref["options"]: michael@0: radio = doc.createElement("radio") michael@0: radio.setAttribute("value", item["value"]) michael@0: radio.setAttribute("label", item["label"]) michael@0: radiogroup.appendChild(radio) michael@0: setting.appendChild(radiogroup) michael@0: michael@0: root.appendChild(setting) michael@0: michael@0: return doc.toprettyxml(indent=" ")