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