options_xul.py 4.2 KB

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