summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/sanitizeDialog.js
blob: 457cd4dc45c859cde6bc95d8a91749b94519967d (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
/* 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/. */

/* import-globals-from sanitize.js */

var gSanitizePromptDialog = {
  get bundleBrowser() {
    if (!this._bundleBrowser) {
      this._bundleBrowser = document.getElementById("bundleBrowser");
    }
    return this._bundleBrowser;
  },

  get selectedTimespan() {
    var durList = document.getElementById("sanitizeDurationChoice");
    return parseInt(durList.value);
  },

  get warningBox() {
    return document.getElementById("sanitizeEverythingWarningBox");
  },

  init() {
    // This is used by selectByTimespan() to determine if the window has loaded.
    this._inited = true;

    var s = new Sanitizer();
    s.prefDomain = "privacy.cpd.";

    document.getElementById("sanitizeDurationChoice").value =
      Services.prefs.getIntPref("privacy.sanitize.timeSpan");

    let sanitizeItemList = document.querySelectorAll(
      "#historyGroup > [preference]"
    );
    for (let prefItem of sanitizeItemList) {
      let name = s.getNameFromPreference(prefItem.getAttribute("preference"));
      if (!s.canClearItem(name)) {
        prefItem.preference = null;
        prefItem.checked = false;
        prefItem.disabled = true;
      } else {
        prefItem.checked = Services.prefs.getBoolPref(
          prefItem.getAttribute("preference")
        );
      }
    }

    this.onReadGeneric();

    document.querySelector("dialog").getButton("accept").label =
      this.bundleBrowser.getString("sanitizeButtonOK");

    let warningIcon = document.getElementById("sanitizeEverythingWarningIcon");
    warningIcon.setAttribute(
      "src",
      "chrome://messenger/skin/icons/new/activity/warning.svg"
    );

    if (this.selectedTimespan === Sanitizer.TIMESPAN_EVERYTHING) {
      this.prepareWarning();
      this.warningBox.hidden = false;
      document.title = this.bundleBrowser.getString(
        "sanitizeDialog2.everything.title"
      );
    } else {
      this.warningBox.hidden = true;
    }
  },

  selectByTimespan() {
    // This method is the onselect handler for the duration dropdown.  As a
    // result it's called a couple of times before onload calls init().
    if (!this._inited) {
      return;
    }

    var warningBox = this.warningBox;

    // If clearing everything
    if (this.selectedTimespan === Sanitizer.TIMESPAN_EVERYTHING) {
      this.prepareWarning();
      if (warningBox.hidden) {
        warningBox.hidden = false;
      }
      window.sizeToContent();
      window.document.title = this.bundleBrowser.getString(
        "sanitizeDialog2.everything.title"
      );
      return;
    }

    // If clearing a specific time range
    if (!warningBox.hidden) {
      window.resizeBy(0, -warningBox.getBoundingClientRect().height);
      warningBox.hidden = true;
    }
    window.document.title =
      window.document.documentElement.getAttribute("noneverythingtitle");
  },

  sanitize() {
    // Update pref values before handing off to the sanitizer (bug 453440)
    this.updatePrefs();
    var s = new Sanitizer();
    s.prefDomain = "privacy.cpd.";

    s.range = Sanitizer.getClearRange(this.selectedTimespan);
    s.ignoreTimespan = !s.range;

    try {
      s.sanitize();
    } catch (er) {
      console.error("Exception during sanitize: " + er);
    }
  },

  /**
   * If the panel that displays a warning when the duration is "Everything" is
   * not set up, sets it up.  Otherwise does nothing.
   */
  prepareWarning() {
    // If the date and time-aware locale warning string is ever used again,
    // initialize it here.  Currently we use the no-visits warning string,
    // which does not include date and time.  See bug 480169 comment 48.

    var warningStringID;
    if (this.hasNonSelectedItems()) {
      warningStringID = "sanitizeSelectedWarning";
    } else {
      warningStringID = "sanitizeEverythingWarning2";
    }

    var warningDesc = document.getElementById("sanitizeEverythingWarning");
    warningDesc.textContent = this.bundleBrowser.getString(warningStringID);
  },

  /**
   * Called when the value of a preference element is synced from the actual
   * pref.  Enables or disables the OK button appropriately.
   */
  onReadGeneric() {
    var found = false;

    // Find any other pref that's checked and enabled.
    let sanitizeItemList = document.querySelectorAll(
      "#historyGroup > [preference]"
    );
    for (let prefItem of sanitizeItemList) {
      found = !prefItem.disabled && prefItem.checked;
      if (found) {
        break;
      }
    }

    try {
      document.querySelector("dialog").getButton("accept").disabled = !found;
    } catch (e) {}

    // Update the warning prompt if needed
    this.prepareWarning();

    return undefined;
  },

  /**
   * Sanitizer.prototype.sanitize() requires the prefs to be up-to-date.
   * Because the type of this prefwindow is "child" -- and that's needed because
   * without it the dialog has no OK and Cancel buttons -- the prefs are not
   * updated on dialogaccept on platforms that don't support instant-apply
   * (i.e., Windows).  We must therefore manually set the prefs from their
   * corresponding preference elements.
   */
  updatePrefs() {
    Sanitizer.prefs.setIntPref("timeSpan", this.selectedTimespan);

    // Now manually set the prefs from their corresponding preference elements.
    let sanitizeItemList = document.querySelectorAll(
      "#historyGroup > [preference]"
    );
    for (let prefItem of sanitizeItemList) {
      let prefName = prefItem.getAttribute("preference");
      Services.prefs.setBoolPref(prefName, prefItem.checked);
    }
  },

  /**
   * Check if all of the history items have been selected like the default status.
   */
  hasNonSelectedItems() {
    let sanitizeItemList = document.querySelectorAll(
      "#historyGroup > [preference]"
    );
    for (let prefItem of sanitizeItemList) {
      if (!prefItem.checked) {
        return true;
      }
    }
    return false;
  },
};

document.addEventListener("dialogaccept", () =>
  gSanitizePromptDialog.sanitize()
);