summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/dialogs/translations.js
blob: 7af2412b442f0404290a895a0aa8792897994fa2 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
/* 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/. */

"use strict";

/**
 * The permission type to give to Services.perms for Translations.
 */
const TRANSLATIONS_PERMISSION = "translations";
/**
 * The list of BCP-47 language tags that will trigger auto-translate.
 */
const ALWAYS_TRANSLATE_LANGS_PREF =
  "browser.translations.alwaysTranslateLanguages";
/**
 * The list of BCP-47 language tags that will prevent showing Translations UI.
 */
const NEVER_TRANSLATE_LANGS_PREF =
  "browser.translations.neverTranslateLanguages";

ChromeUtils.defineESModuleGetters(this, {
  TranslationsParent: "resource://gre/actors/TranslationsParent.sys.mjs",
});

function Tree(aId, aData) {
  this._data = aData;
  this._tree = document.getElementById(aId);
  this._tree.view = this;
}

Tree.prototype = {
  get tree() {
    return this._tree;
  },
  get isEmpty() {
    return !this._data.length;
  },
  get hasSelection() {
    return this.selection.count > 0;
  },
  getSelectedItems() {
    let result = [];

    let rc = this.selection.getRangeCount();
    for (let i = 0; i < rc; ++i) {
      let min = {},
        max = {};
      this.selection.getRangeAt(i, min, max);
      for (let j = min.value; j <= max.value; ++j) {
        result.push(this._data[j]);
      }
    }

    return result;
  },

  // nsITreeView implementation
  get rowCount() {
    return this._data.length;
  },
  getCellText(aRow) {
    return this._data[aRow];
  },
  isSeparator() {
    return false;
  },
  isSorted() {
    return false;
  },
  isContainer() {
    return false;
  },
  setTree() {},
  getImageSrc() {},
  getCellValue() {},
  cycleHeader() {},
  getRowProperties() {
    return "";
  },
  getColumnProperties() {
    return "";
  },
  getCellProperties() {
    return "";
  },
  QueryInterface: ChromeUtils.generateQI(["nsITreeView"]),
};

function Lang(aCode, label) {
  this.langCode = aCode;
  this._label = label;
}

Lang.prototype = {
  toString() {
    return this._label;
  },
};

var gTranslationsSettings = {
  onLoad() {
    if (this._neverTranslateSiteTree) {
      // Re-using an open dialog, clear the old observers.
      this.removeObservers();
    }

    // Load site permissions into an array.
    this._neverTranslateSites = TranslationsParent.listNeverTranslateSites();

    // Load language tags into arrays.
    this._alwaysTranslateLangs = this.getAlwaysTranslateLanguages();
    this._neverTranslateLangs = this.getNeverTranslateLanguages();

    // Add observers for relevant prefs and permissions.
    Services.obs.addObserver(this, "perm-changed");
    Services.prefs.addObserver(ALWAYS_TRANSLATE_LANGS_PREF, this);
    Services.prefs.addObserver(NEVER_TRANSLATE_LANGS_PREF, this);

    // Build trees from the arrays.
    this._alwaysTranslateLangsTree = new Tree(
      "alwaysTranslateLanguagesTree",
      this._alwaysTranslateLangs
    );
    this._neverTranslateLangsTree = new Tree(
      "neverTranslateLanguagesTree",
      this._neverTranslateLangs
    );
    this._neverTranslateSiteTree = new Tree(
      "neverTranslateSitesTree",
      this._neverTranslateSites
    );

    // Ensure the UI for each group is in the correct state.
    this.onSelectAlwaysTranslateLanguage();
    this.onSelectNeverTranslateLanguage();
    this.onSelectNeverTranslateSite();
  },

  /**
   * Retrieves the value of a char-pref splits its value into an
   * array delimited by commas.
   *
   * This is used for the translations preferences which are comma-
   * separated lists of BCP-47 language tags.
   *
   * @param {string} pref
   * @returns {Array<string>}
   */
  getLangsFromPref(pref) {
    let rawLangs = Services.prefs.getCharPref(pref);
    if (!rawLangs) {
      return [];
    }

    let langArr = rawLangs.split(",");
    let displayNames = Services.intl.getLanguageDisplayNames(
      undefined,
      langArr
    );
    let langs = langArr.map((lang, i) => new Lang(lang, displayNames[i]));
    langs.sort();

    return langs;
  },

  /**
   * Retrieves the always-translate language tags as an array.
   * @returns {Array<string>}
   */
  getAlwaysTranslateLanguages() {
    return this.getLangsFromPref(ALWAYS_TRANSLATE_LANGS_PREF);
  },

  /**
   * Retrieves the never-translate language tags as an array.
   * @returns {Array<string>}
   */
  getNeverTranslateLanguages() {
    return this.getLangsFromPref(NEVER_TRANSLATE_LANGS_PREF);
  },

  /**
   * Handles updating the UI components on pref or permission changes.
   */
  observe(aSubject, aTopic, aData) {
    if (aTopic === "perm-changed") {
      if (aData === "cleared") {
        // Permissions have been cleared
        if (!this._neverTranslateSites.length) {
          // There were no sites with permissions set, nothing to do.
          return;
        }
        // Update the tree based on the amount of permissions removed.
        let removed = this._neverTranslateSites.splice(
          0,
          this._neverTranslateSites.length
        );
        this._neverTranslateSiteTree.tree.rowCountChanged(0, -removed.length);
      } else {
        let perm = aSubject.QueryInterface(Ci.nsIPermission);
        if (perm.type != TRANSLATIONS_PERMISSION) {
          // The updated permission was not for Translations, nothing to do.
          return;
        }
        if (aData === "added") {
          if (perm.capability != Services.perms.DENY_ACTION) {
            // We are only showing data for sites we should never translate.
            // If the permission is not DENY_ACTION, we don't care about it here.
            return;
          }
          this._neverTranslateSites.push(perm.principal.origin);
          this._neverTranslateSites.sort();
          let tree = this._neverTranslateSiteTree.tree;
          tree.rowCountChanged(0, 1);
          tree.invalidate();
        } else if (aData == "deleted") {
          let index = this._neverTranslateSites.indexOf(perm.principal.origin);
          if (index == -1) {
            // The deleted permission was not in the tree, nothing to do.
            return;
          }
          this._neverTranslateSites.splice(index, 1);
          this._neverTranslateSiteTree.tree.rowCountChanged(index, -1);
        }
      }
      // Ensure the UI updates to the changes.
      this.onSelectNeverTranslateSite();
    } else if (aTopic === "nsPref:changed") {
      switch (aData) {
        case ALWAYS_TRANSLATE_LANGS_PREF: {
          this._alwaysTranslateLangs = this.getAlwaysTranslateLanguages();

          let alwaysTranslateLangsChange =
            this._alwaysTranslateLangs.length -
            this._alwaysTranslateLangsTree.rowCount;

          this._alwaysTranslateLangsTree._data = this._alwaysTranslateLangs;
          let alwaysTranslateLangsTree = this._alwaysTranslateLangsTree.tree;

          if (alwaysTranslateLangsChange) {
            alwaysTranslateLangsTree.rowCountChanged(
              0,
              alwaysTranslateLangsChange
            );
          }

          alwaysTranslateLangsTree.invalidate();

          // Ensure the UI updates to the changes.
          this.onSelectAlwaysTranslateLanguage();
          break;
        }
        case NEVER_TRANSLATE_LANGS_PREF: {
          this._neverTranslateLangs = this.getNeverTranslateLanguages();

          let neverTranslateLangsChange =
            this._neverTranslateLangs.length -
            this._neverTranslateLangsTree.rowCount;

          this._neverTranslateLangsTree._data = this._neverTranslateLangs;
          let neverTranslateLangsTree = this._neverTranslateLangsTree.tree;

          if (neverTranslateLangsChange) {
            neverTranslateLangsTree.rowCountChanged(
              0,
              neverTranslateLangsChange
            );
          }

          neverTranslateLangsTree.invalidate();

          // Ensure the UI updates to the changes.
          this.onSelectNeverTranslateLanguage();
          break;
        }
      }
    }
  },

  /**
   * Ensures that buttons states are enabled/disabled accordingly based on the
   * content of the trees.
   *
   * The remove button should be enabled only if an item is selected.
   * The removeAll button should be enabled any time the tree has content.
   *
   * @param {Tree} aTree
   * @param {string} aIdPart
   */
  _handleButtonDisabling(aTree, aIdPart) {
    let empty = aTree.isEmpty;
    document.getElementById("removeAll" + aIdPart + "s").disabled = empty;
    document.getElementById("remove" + aIdPart).disabled =
      empty || !aTree.hasSelection;
  },

  /**
   * Updates the UI state for the always-translate languages section.
   */
  onSelectAlwaysTranslateLanguage() {
    this._handleButtonDisabling(
      this._alwaysTranslateLangsTree,
      "AlwaysTranslateLanguage"
    );
  },

  /**
   * Updates the UI state for the never-translate languages section.
   */
  onSelectNeverTranslateLanguage() {
    this._handleButtonDisabling(
      this._neverTranslateLangsTree,
      "NeverTranslateLanguage"
    );
  },

  /**
   * Updates the UI state for the never-translate sites section.
   */
  onSelectNeverTranslateSite() {
    this._handleButtonDisabling(
      this._neverTranslateSiteTree,
      "NeverTranslateSite"
    );
  },

  /**
   * Updates the value of a language pref to match when a language is removed
   * through the UI.
   *
   * @param {string} pref
   * @param {Tree} tree
   */
  _onRemoveLanguage(pref, tree) {
    let langs = Services.prefs.getCharPref(pref);
    if (!langs) {
      return;
    }

    let removed = tree.getSelectedItems().map(l => l.langCode);

    langs = langs.split(",").filter(l => !removed.includes(l));
    Services.prefs.setCharPref(pref, langs.join(","));
  },

  /**
   * Updates the never-translate language pref when a never-translate language
   * is removed via the UI.
   */
  onRemoveAlwaysTranslateLanguage() {
    this._onRemoveLanguage(
      ALWAYS_TRANSLATE_LANGS_PREF,
      this._alwaysTranslateLangsTree
    );
  },

  /**
   * Updates the always-translate language pref when a always-translate language
   * is removed via the UI.
   */
  onRemoveNeverTranslateLanguage() {
    this._onRemoveLanguage(
      NEVER_TRANSLATE_LANGS_PREF,
      this._neverTranslateLangsTree
    );
  },

  /**
   * Updates the permissions for a never-translate site when it is removed via the UI.
   */
  onRemoveNeverTranslateSite() {
    let removedNeverTranslateSites =
      this._neverTranslateSiteTree.getSelectedItems();
    for (let origin of removedNeverTranslateSites) {
      TranslationsParent.setNeverTranslateSiteByOrigin(false, origin);
    }
  },

  /**
   * Clears the always-translate languages pref when the list is cleared in the UI.
   */
  onRemoveAllAlwaysTranslateLanguages() {
    Services.prefs.setCharPref(ALWAYS_TRANSLATE_LANGS_PREF, "");
  },

  /**
   * Clears the never-translate languages pref when the list is cleared in the UI.
   */
  onRemoveAllNeverTranslateLanguages() {
    Services.prefs.setCharPref(NEVER_TRANSLATE_LANGS_PREF, "");
  },

  /**
   * Clears the never-translate sites pref when the list is cleared in the UI.
   */
  onRemoveAllNeverTranslateSites() {
    if (this._neverTranslateSiteTree.isEmpty) {
      return;
    }

    let removedNeverTranslateSites = this._neverTranslateSites.splice(
      0,
      this._neverTranslateSites.length
    );
    this._neverTranslateSiteTree.tree.rowCountChanged(
      0,
      -removedNeverTranslateSites.length
    );

    for (let origin of removedNeverTranslateSites) {
      TranslationsParent.setNeverTranslateSiteByOrigin(false, origin);
    }

    this.onSelectNeverTranslateSite();
  },

  /**
   * Handles removing a selected always-translate language via the keyboard.
   */
  onAlwaysTranslateLanguageKeyPress(aEvent) {
    if (aEvent.keyCode == KeyEvent.DOM_VK_DELETE) {
      this.onRemoveAlwaysTranslateLanguage();
    }
  },

  /**
   * Handles removing a selected never-translate language via the keyboard.
   */
  onNeverTranslateLanguageKeyPress(aEvent) {
    if (aEvent.keyCode == KeyEvent.DOM_VK_DELETE) {
      this.onRemoveNeverTranslateLanguage();
    }
  },

  /**
   * Handles removing a selected never-translate site via the keyboard.
   */
  onNeverTranslateSiteKeyPress(aEvent) {
    if (aEvent.keyCode == KeyEvent.DOM_VK_DELETE) {
      this.onRemoveNeverTranslateSite();
    }
  },

  /**
   * Removes any active preference and permissions observers.
   */
  removeObservers() {
    Services.obs.removeObserver(this, "perm-changed");
    Services.prefs.removeObserver(ALWAYS_TRANSLATE_LANGS_PREF, this);
    Services.prefs.removeObserver(NEVER_TRANSLATE_LANGS_PREF, this);
  },
};