summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/src/MsgProtocolInfo.sys.mjs
blob: 7c9088e12eb90420a790de2a6b28a3c8385c2e7e (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
/* 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/. */

/**
 * @see {nsIMsgProtocolInfo}
 */
export class MsgProtocolInfo {
  get defaultLocalPath() {
    let file = this._getFileValue(this.RELATIVE_PREF, this.ABSOLUTE_PREF);
    if (!file) {
      file = Services.dirsvc.get(this.DIR_SERVICE_PROP, Ci.nsIFile);
      this._setFileValue(this.RELATIVE_PREF, this.ABSOLUTE_PREF, file);
    }
    if (!file.exists()) {
      file.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o775);
    }
    file.normalize();
    return file;
  }

  set defaultLocalPath(value) {
    this._setFileValue(this.RELATIVE_PREF, this.ABSOLUTE_PREF, value);
  }

  _getFileValue(relPrefName, absPrefName) {
    try {
      return Services.prefs.getComplexValue(relPrefName, Ci.nsIRelativeFilePref)
        .file;
    } catch (e) {
      try {
        let file = Services.prefs.getComplexValue(absPrefName, Ci.nsIFile);
        Services.prefs.setComplexValue(relPrefName, Ci.nsIRelativeFilePref, {
          QueryInterface: ChromeUtils.generateQI(["nsIRelativeFilePref"]),
          file,
          relativeToKey: "ProfD",
        });
        return file;
      } catch (e) {
        return null;
      }
    }
  }

  _setFileValue(relPrefName, absPrefName, file) {
    Services.prefs.setComplexValue(relPrefName, Ci.nsIRelativeFilePref, {
      QueryInterface: ChromeUtils.generateQI(["nsIRelativeFilePref"]),
      file,
      relativeToKey: "ProfD",
    });
    Services.prefs.setComplexValue(absPrefName, Ci.nsIFile, file);
  }
}