addon-sdk/source/python-lib/cuddlefish/options_xul.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4
michael@0 5 from xml.dom.minidom import Document
michael@0 6
michael@0 7 VALID_PREF_TYPES = ['bool', 'boolint', 'integer', 'string', 'color', 'file',
michael@0 8 'directory', 'control', 'menulist', 'radio']
michael@0 9
michael@0 10 class Error(Exception):
michael@0 11 pass
michael@0 12
michael@0 13 class BadPrefTypeError(Error):
michael@0 14 pass
michael@0 15
michael@0 16 class MissingPrefAttr(Error):
michael@0 17 pass
michael@0 18
michael@0 19 def validate_prefs(options):
michael@0 20 for pref in options:
michael@0 21 # Make sure there is a 'title'
michael@0 22 if ("title" not in pref):
michael@0 23 raise MissingPrefAttr("The '%s' pref requires a 'title'" % (pref["name"]))
michael@0 24
michael@0 25 # Make sure that the pref type is a valid inline pref type
michael@0 26 if (pref["type"] not in VALID_PREF_TYPES):
michael@0 27 raise BadPrefTypeError('%s is not a valid inline pref type' % (pref["type"]))
michael@0 28
michael@0 29 # Make sure the 'control' type has a 'label'
michael@0 30 if (pref["type"] == "control"):
michael@0 31 if ("label" not in pref):
michael@0 32 raise MissingPrefAttr("The 'control' inline pref type requires a 'label'")
michael@0 33
michael@0 34 # Make sure the 'menulist' type has a 'menulist'
michael@0 35 if (pref["type"] == "menulist" or pref["type"] == "radio"):
michael@0 36 if ("options" not in pref):
michael@0 37 raise MissingPrefAttr("The 'menulist' and the 'radio' inline pref types requires a 'options'")
michael@0 38
michael@0 39 # Make sure each option has a 'value' and a 'label'
michael@0 40 for item in pref["options"]:
michael@0 41 if ("value" not in item):
michael@0 42 raise MissingPrefAttr("'options' requires a 'value'")
michael@0 43 if ("label" not in item):
michael@0 44 raise MissingPrefAttr("'options' requires a 'label'")
michael@0 45
michael@0 46 # TODO: Check that pref["type"] matches default value type
michael@0 47
michael@0 48 def parse_options(options, jetpack_id, preferencesBranch):
michael@0 49 doc = Document()
michael@0 50 root = doc.createElement("vbox")
michael@0 51 root.setAttribute("xmlns", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
michael@0 52 doc.appendChild(root)
michael@0 53
michael@0 54 for pref in options:
michael@0 55 if ("hidden" in pref and pref["hidden"] == True):
michael@0 56 continue;
michael@0 57
michael@0 58 setting = doc.createElement("setting")
michael@0 59 setting.setAttribute("pref-name", pref["name"])
michael@0 60 setting.setAttribute("data-jetpack-id", jetpack_id)
michael@0 61 setting.setAttribute("pref", "extensions." + preferencesBranch + "." + pref["name"])
michael@0 62 setting.setAttribute("type", pref["type"])
michael@0 63 setting.setAttribute("title", pref["title"])
michael@0 64
michael@0 65 if ("description" in pref):
michael@0 66 setting.appendChild(doc.createTextNode(pref["description"]))
michael@0 67
michael@0 68 if (pref["type"] == "control"):
michael@0 69 button = doc.createElement("button")
michael@0 70 button.setAttribute("pref-name", pref["name"])
michael@0 71 button.setAttribute("data-jetpack-id", jetpack_id)
michael@0 72 button.setAttribute("label", pref["label"])
michael@0 73 button.setAttribute("oncommand", "Services.obs.notifyObservers(null, '" +
michael@0 74 jetpack_id + "-cmdPressed', '" +
michael@0 75 pref["name"] + "');");
michael@0 76 setting.appendChild(button)
michael@0 77 elif (pref["type"] == "boolint"):
michael@0 78 setting.setAttribute("on", pref["on"])
michael@0 79 setting.setAttribute("off", pref["off"])
michael@0 80 elif (pref["type"] == "menulist"):
michael@0 81 menulist = doc.createElement("menulist")
michael@0 82 menupopup = doc.createElement("menupopup")
michael@0 83 for item in pref["options"]:
michael@0 84 menuitem = doc.createElement("menuitem")
michael@0 85 menuitem.setAttribute("value", item["value"])
michael@0 86 menuitem.setAttribute("label", item["label"])
michael@0 87 menupopup.appendChild(menuitem)
michael@0 88 menulist.appendChild(menupopup)
michael@0 89 setting.appendChild(menulist)
michael@0 90 elif (pref["type"] == "radio"):
michael@0 91 radiogroup = doc.createElement("radiogroup")
michael@0 92 for item in pref["options"]:
michael@0 93 radio = doc.createElement("radio")
michael@0 94 radio.setAttribute("value", item["value"])
michael@0 95 radio.setAttribute("label", item["label"])
michael@0 96 radiogroup.appendChild(radio)
michael@0 97 setting.appendChild(radiogroup)
michael@0 98
michael@0 99 root.appendChild(setting)
michael@0 100
michael@0 101 return doc.toprettyxml(indent=" ")

mercurial