aboutsummaryrefslogtreecommitdiff
path: root/modules/firefox.home/chrome/utils/xPref.jsm
blob: dcb44b3cfebcf59c196537fd47732ca7e232a4ae (plain) (blame)
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
let EXPORTED_SYMBOLS = ['xPref'];

var xPref = {
  get: function(prefPath, def = false, valueIfUndefined, setDefault = true) {
    let sPrefs = def ?
      Services.prefs.getDefaultBranch(null) :
      Services.prefs;

    try {
      switch (sPrefs.getPrefType(prefPath)) {
        case 0:
          if (valueIfUndefined != undefined)
            return this.set(prefPath, valueIfUndefined, setDefault);
          else
            return undefined;
        case 32:
          return sPrefs.getStringPref(prefPath);
        case 64:
          return sPrefs.getIntPref(prefPath);
        case 128:
          return sPrefs.getBoolPref(prefPath);
      }
    } catch (ex) {
      return undefined;
    }
    return;
  },

  set: function(prefPath, value, def = false) {
    let sPrefs = def ?
      Services.prefs.getDefaultBranch(null) :
      Services.prefs;

    try {
      switch (typeof value) {
        case 'string':
          return sPrefs.setStringPref(prefPath, value) || value;
        case 'number':
          return sPrefs.setIntPref(prefPath, value) || value;
        case 'boolean':
          return sPrefs.setBoolPref(prefPath, value) || value;
      }
    } catch (e) { console.error(e) }
    return;
  },

  lock: function(prefPath, value) {
    let sPrefs = Services.prefs;
    this.lockedBackupDef[prefPath] = this.get(prefPath, true);
    if (sPrefs.prefIsLocked(prefPath))
      sPrefs.unlockPref(prefPath);

    this.set(prefPath, value, true);
    sPrefs.lockPref(prefPath);
  },

  lockedBackupDef: {},

  unlock: function(prefPath) {
    Services.prefs.unlockPref(prefPath);
    let bkp = this.lockedBackupDef[prefPath];
    if (bkp == undefined)
      Services.prefs.deleteBranch(prefPath);
    else
      this.set(prefPath, bkp, true);
  },

  clear: Services.prefs.clearUserPref,

  addListener: function(prefPath, trat) {
    this.observer = function(aSubject, aTopic, prefPath) {
      return trat(xPref.get(prefPath), prefPath);
    }

    Services.prefs.addObserver(prefPath, this.observer);
    return {
      prefPath,
      observer: this.observer
    };
  },

  removeListener: function(obs) {
    Services.prefs.removeObserver(obs.prefPath, obs.observer);
  }
}