summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/content/setting.xml
blob: c4eae1fd39a8a878f141c9ecc2ad441a6dd87540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<!DOCTYPE page [
<!ENTITY % extensionsDTD SYSTEM "chrome://mozapps/locale/extensions/extensions.dtd">
%extensionsDTD;
]>

<bindings xmlns="http://www.mozilla.org/xbl"
          xmlns:xbl="http://www.mozilla.org/xbl"
          xmlns:html="http://www.w3.org/1999/xhtml"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <binding id="settings">
    <content orient="vertical">
      <xul:label class="settings-title" xbl:inherits="xbl:text=label" flex="1"/>
      <children/>
    </content>
  </binding>

  <binding id="setting-base">
    <implementation>
      <constructor><![CDATA[
        this.preferenceChanged();

        this.addEventListener("keypress", function(event) {
          event.stopPropagation();
        }, false);
        
        if (this.usePref)
          Services.prefs.addObserver(this.pref, this._observer, true);
      ]]></constructor>

      <field name="_observer"><![CDATA[({
        _self: this,

        QueryInterface: function(aIID) {
          const Ci = Components.interfaces;
          if (aIID.equals(Ci.nsIObserver) ||
              aIID.equals(Ci.nsISupportsWeakReference) ||
              aIID.equals(Ci.nsISupports))
            return this;

          throw Components.Exception("No interface", Components.results.NS_ERROR_NO_INTERFACE);
        },

        observe: function(aSubject, aTopic, aPrefName) {
          if (aTopic != "nsPref:changed")
            return;

          if (this._self.pref == aPrefName)
            this._self.preferenceChanged();
        }
      })]]>
      </field>

      <method name="fireEvent">
        <parameter name="eventName"/>
        <parameter name="funcStr"/>
        <body>
          <![CDATA[
            let body = funcStr || this.getAttribute(eventName);
            if (!body)
              return;

            try {
              let event = document.createEvent("Events");
              event.initEvent(eventName, true, true);
              let f = new Function("event", body);
              f.call(this, event);
            }
            catch (e) {
              Cu.reportError(e);
            }
          ]]>
        </body>
      </method>

      <method name="valueFromPreference">
        <body>
        <![CDATA[
          // Should be code to set the from the preference input.value
          throw Components.Exception("No valueFromPreference implementation",
                                     Components.results.NS_ERROR_NOT_IMPLEMENTED);
        ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          // Should be code to set the input.value from the preference
          throw Components.Exception("No valueToPreference implementation",
                                     Components.results.NS_ERROR_NOT_IMPLEMENTED);
        ]]>
        </body>
      </method>

      <method name="inputChanged">
        <body>
        <![CDATA[
          if (this.usePref && !this._updatingInput) {
            this.valueToPreference();
            this.fireEvent("oninputchanged");
          }
        ]]>
        </body>
      </method>

      <method name="preferenceChanged">
        <body>
        <![CDATA[
          if (this.usePref) {
            this._updatingInput = true;
            try {
              this.valueFromPreference();
              this.fireEvent("onpreferencechanged");
            } catch (e) {}
            this._updatingInput = false;
          }
        ]]>
        </body>
      </method>

      <property name="usePref" readonly="true" onget="return this.hasAttribute('pref');"/>
      <property name="pref" readonly="true" onget="return this.getAttribute('pref');"/>
      <property name="type" readonly="true" onget="return this.getAttribute('type');"/>
      <property name="value" onget="return this.input.value;" onset="return this.input.value = val;"/>

      <field name="_updatingInput">false</field>
      <field name="input">document.getAnonymousElementByAttribute(this, "anonid", "input");</field>
      <field name="settings">
        this.parentNode.localName == "settings" ? this.parentNode : null;
      </field>
    </implementation>
  </binding>

  <binding id="setting-bool" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
        <xul:label class="preferences-learnmore text-link"
                   onclick="document.getBindingParent(this).openLearnMore()">&setting.learnmore;</xul:label>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <xul:checkbox anonid="input" xbl:inherits="disabled,onlabel,offlabel,label=checkboxlabel" oncommand="inputChanged();"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val = Services.prefs.getBoolPref(this.pref);
          this.value = this.inverted ? !val : val;
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          let val = this.value;
          Services.prefs.setBoolPref(this.pref, this.inverted ? !val : val);
        ]]>
        </body>
      </method>

      <property name="value" onget="return this.input.checked;" onset="return this.input.setChecked(val);"/>
      <property name="inverted" readonly="true" onget="return this.getAttribute('inverted');"/>

      <method name="openLearnMore">
        <body>
        <![CDATA[
          window.open(this.getAttribute("learnmore"), "_blank");
        ]]>
        </body>
      </method>
    </implementation>
  </binding>

  <binding id="setting-boolint" extends="chrome://mozapps/content/extensions/setting.xml#setting-bool">
    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val = Services.prefs.getIntPref(this.pref);
          this.value = (val == this.getAttribute("on"));
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          Services.prefs.setIntPref(this.pref, this.getAttribute(this.value ? "on" : "off"));
        ]]>
        </body>
      </method>
    </implementation>
  </binding>

  <binding id="setting-localized-bool" extends="chrome://mozapps/content/extensions/setting.xml#setting-bool">
    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val = Services.prefs.getComplexValue(this.pref, Components.interfaces.nsIPrefLocalizedString).data;
          if(this.inverted) val = !val;
          this.value = (val == "true");
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          let val = this.value;
          if(this.inverted) val = !val;
          let pref = Components.classes["@mozilla.org/pref-localizedstring;1"].createInstance(Components.interfaces.nsIPrefLocalizedString);
          pref.data = this.inverted ? (!val).toString() : val.toString();
          Services.prefs.setComplexValue(this.pref, Components.interfaces.nsIPrefLocalizedString, pref);
        ]]>
        </body>
      </method>
    </implementation>
  </binding>

  <binding id="setting-integer" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <xul:textbox type="number" anonid="input" oninput="inputChanged();" onchange="inputChanged();"
                     xbl:inherits="disabled,emptytext,min,max,increment,hidespinbuttons,wraparound,size"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val = Services.prefs.getIntPref(this.pref);
          this.value = val;
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          Services.prefs.setIntPref(this.pref, this.value);
        ]]>
        </body>
      </method>
    </implementation>
  </binding>

  <binding id="setting-control" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <children/>
      </xul:hbox>
    </content>
  </binding>

  <binding id="setting-string" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <xul:textbox anonid="input" flex="1" oninput="inputChanged();"
                     xbl:inherits="disabled,emptytext,type=inputtype,min,max,increment,hidespinbuttons,decimalplaces,wraparound"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          const nsISupportsString = Components.interfaces.nsISupportsString;
          this.value = Services.prefs.getComplexValue(this.pref, nsISupportsString).data;
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          const nsISupportsString = Components.interfaces.nsISupportsString;
          let iss = Components.classes["@mozilla.org/supports-string;1"].createInstance(nsISupportsString);
          iss.data = this.value;
          Services.prefs.setComplexValue(this.pref, nsISupportsString, iss);
        ]]>
        </body>
      </method>
    </implementation>
  </binding>

  <binding id="setting-color" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <xul:colorpicker type="button" anonid="input" xbl:inherits="disabled" onchange="document.getBindingParent(this).inputChanged();"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          // We must wait for the colorpicker's binding to be applied before setting the value
          if (!this.input.color)
            this.input.initialize();
          this.value = Services.prefs.getCharPref(this.pref);
        ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          Services.prefs.setCharPref(this.pref, this.value);
        ]]>
        </body>
      </method>

      <property name="value" onget="return this.input.color;" onset="return this.input.color = val;"/>
    </implementation>
  </binding>

  <binding id="setting-path" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <xul:button type="button" anonid="button" label="&settings.path.button.label;" xbl:inherits="disabled" oncommand="showPicker();"/>
        <xul:label anonid="input" flex="1" crop="center" xbl:inherits="disabled"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="showPicker">
        <body>
        <![CDATA[
          var filePicker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
          filePicker.init(window, this.getAttribute("title"),
                          this.type == "file" ? Ci.nsIFilePicker.modeOpen : Ci.nsIFilePicker.modeGetFolder);
          if (this.value) {
            try {
              let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
              file.initWithPath(this.value);
              filePicker.displayDirectory = this.type == "file" ? file.parent : file;
              if (this.type == "file") {
                filePicker.defaultString = file.leafName;
              }
            } catch (e) {}
          }
          if (filePicker.show() != Ci.nsIFilePicker.returnCancel) {
            this.value = filePicker.file.path;
            this.inputChanged();
          }
        ]]>
        </body>
      </method>

      <method name="valueFromPreference">
        <body>
        <![CDATA[
          this.value = Services.prefs.getCharPref(this.pref);
        ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          Services.prefs.setCharPref(this.pref, this.value);
        ]]>
        </body>
      </method>

      <field name="_value"></field>

      <property name="value">
        <getter>
        <![CDATA[
          return this._value;
        ]]>
        </getter>
        <setter>
        <![CDATA[
          this._value = val;
          let label = "";
          if (val) {
            try {
              let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
              file.initWithPath(val);
              label = this.hasAttribute("fullpath") ? file.path : file.leafName;
            } catch (e) {}
          }
          this.input.tooltipText = val;
          return this.input.value = label;
       ]]>
        </setter>
      </property>
    </implementation>
  </binding>

  <binding id="setting-multi" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <children includes="radiogroup|menulist"/>
      </xul:hbox>
    </content>

    <implementation>
      <constructor>
      <![CDATA[
        this.control.addEventListener("command", this.inputChanged.bind(this), false);
      ]]>
      </constructor>

      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val;
          switch (Services.prefs.getPrefType(this.pref)) {
            case Ci.nsIPrefBranch.PREF_STRING:
              val = Services.prefs.getCharPref(this.pref);
              break;
            case Ci.nsIPrefBranch.PREF_INT:
              val = Services.prefs.getIntPref(this.pref);
              break;
            case Ci.nsIPrefBranch.PREF_BOOL:
              val = Services.prefs.getBoolPref(this.pref).toString();
              break;
            default:
              return;
          }

          if ("itemCount" in this.control) {
            for (let i = 0; i < this.control.itemCount; i++) {
              if (this.control.getItemAtIndex(i).value == val) {
                this.control.selectedIndex = i;
                break;
              }
            }
          } else {
            this.control.setAttribute("value", val);
          }
        ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          // We might not have a pref already set, so we guess the type from the value attribute
          let val = this.control.selectedItem.value;
          if (val == "true" || val == "false")
            Services.prefs.setBoolPref(this.pref, val == "true");
          else if (/^-?\d+$/.test(val))
            Services.prefs.setIntPref(this.pref, val);
          else
            Services.prefs.setCharPref(this.pref, val);
        ]]>
        </body>
      </method>

      <field name="control">this.getElementsByTagName(this.getAttribute("type") == "radio" ? "radiogroup" : "menulist")[0];</field>
    </implementation>
  </binding>
</bindings>