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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/python-lib/cuddlefish/options_xul.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +from xml.dom.minidom import Document
     1.9 +
    1.10 +VALID_PREF_TYPES = ['bool', 'boolint', 'integer', 'string', 'color', 'file',
    1.11 +                    'directory', 'control', 'menulist', 'radio']
    1.12 +
    1.13 +class Error(Exception):
    1.14 +    pass
    1.15 +
    1.16 +class BadPrefTypeError(Error):
    1.17 +    pass
    1.18 +
    1.19 +class MissingPrefAttr(Error):
    1.20 +    pass
    1.21 +
    1.22 +def validate_prefs(options):
    1.23 +    for pref in options:
    1.24 +        # Make sure there is a 'title'
    1.25 +        if ("title" not in pref):
    1.26 +            raise MissingPrefAttr("The '%s' pref requires a 'title'" % (pref["name"]))
    1.27 +
    1.28 +        # Make sure that the pref type is a valid inline pref type
    1.29 +        if (pref["type"] not in VALID_PREF_TYPES):
    1.30 +            raise BadPrefTypeError('%s is not a valid inline pref type' % (pref["type"]))
    1.31 +
    1.32 +        # Make sure the 'control' type has a 'label'
    1.33 +        if (pref["type"] == "control"):
    1.34 +            if ("label" not in pref):
    1.35 +                raise MissingPrefAttr("The 'control' inline pref type requires a 'label'")
    1.36 +
    1.37 +        # Make sure the 'menulist' type has a 'menulist'
    1.38 +        if (pref["type"] == "menulist" or pref["type"] == "radio"):
    1.39 +            if ("options" not in pref):
    1.40 +                raise MissingPrefAttr("The 'menulist' and the 'radio' inline pref types requires a 'options'")
    1.41 +
    1.42 +            # Make sure each option has a 'value' and a 'label'
    1.43 +            for item in pref["options"]:
    1.44 +                if ("value" not in item):
    1.45 +                    raise MissingPrefAttr("'options' requires a 'value'")
    1.46 +                if ("label" not in item):
    1.47 +                    raise MissingPrefAttr("'options' requires a 'label'")
    1.48 +
    1.49 +        # TODO: Check that pref["type"] matches default value type
    1.50 +
    1.51 +def parse_options(options, jetpack_id, preferencesBranch):
    1.52 +    doc = Document()
    1.53 +    root = doc.createElement("vbox")
    1.54 +    root.setAttribute("xmlns", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
    1.55 +    doc.appendChild(root)
    1.56 +
    1.57 +    for pref in options:
    1.58 +        if ("hidden" in pref and pref["hidden"] == True):
    1.59 +            continue;
    1.60 +
    1.61 +        setting = doc.createElement("setting")
    1.62 +        setting.setAttribute("pref-name", pref["name"])
    1.63 +        setting.setAttribute("data-jetpack-id", jetpack_id)
    1.64 +        setting.setAttribute("pref", "extensions." + preferencesBranch + "." + pref["name"])
    1.65 +        setting.setAttribute("type", pref["type"])
    1.66 +        setting.setAttribute("title", pref["title"])
    1.67 +
    1.68 +        if ("description" in pref):
    1.69 +            setting.appendChild(doc.createTextNode(pref["description"]))
    1.70 +
    1.71 +        if (pref["type"] == "control"):
    1.72 +            button = doc.createElement("button")
    1.73 +            button.setAttribute("pref-name", pref["name"])
    1.74 +            button.setAttribute("data-jetpack-id", jetpack_id)
    1.75 +            button.setAttribute("label", pref["label"])
    1.76 +            button.setAttribute("oncommand", "Services.obs.notifyObservers(null, '" +
    1.77 +                                              jetpack_id + "-cmdPressed', '" +
    1.78 +                                              pref["name"] + "');");
    1.79 +            setting.appendChild(button)
    1.80 +        elif (pref["type"] == "boolint"):
    1.81 +            setting.setAttribute("on", pref["on"])
    1.82 +            setting.setAttribute("off", pref["off"])
    1.83 +        elif (pref["type"] == "menulist"):
    1.84 +            menulist = doc.createElement("menulist")
    1.85 +            menupopup = doc.createElement("menupopup")
    1.86 +            for item in pref["options"]:
    1.87 +                menuitem = doc.createElement("menuitem")
    1.88 +                menuitem.setAttribute("value", item["value"])
    1.89 +                menuitem.setAttribute("label", item["label"])
    1.90 +                menupopup.appendChild(menuitem)
    1.91 +            menulist.appendChild(menupopup)
    1.92 +            setting.appendChild(menulist)
    1.93 +        elif (pref["type"] == "radio"):
    1.94 +            radiogroup = doc.createElement("radiogroup")
    1.95 +            for item in pref["options"]:
    1.96 +                radio = doc.createElement("radio")
    1.97 +                radio.setAttribute("value", item["value"])
    1.98 +                radio.setAttribute("label", item["label"])
    1.99 +                radiogroup.appendChild(radio)
   1.100 +            setting.appendChild(radiogroup)
   1.101 +
   1.102 +        root.appendChild(setting)
   1.103 +
   1.104 +    return doc.toprettyxml(indent="  ")

mercurial