browser/base/content/sync/genericChange.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/sync/genericChange.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,235 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Ci = Components.interfaces;
     1.9 +const Cc = Components.classes;
    1.10 +
    1.11 +Components.utils.import("resource://services-sync/main.js");
    1.12 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.13 +
    1.14 +let Change = {
    1.15 +  _dialog: null,
    1.16 +  _dialogType: null,
    1.17 +  _status: null,
    1.18 +  _statusIcon: null,
    1.19 +  _firstBox: null,
    1.20 +  _secondBox: null,
    1.21 +
    1.22 +  get _passphraseBox() {
    1.23 +    delete this._passphraseBox;
    1.24 +    return this._passphraseBox = document.getElementById("passphraseBox");
    1.25 +  },
    1.26 +
    1.27 +  get _currentPasswordInvalid() {
    1.28 +    return Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED;
    1.29 +  },
    1.30 +
    1.31 +  get _updatingPassphrase() {
    1.32 +    return this._dialogType == "UpdatePassphrase";
    1.33 +  },
    1.34 +
    1.35 +  onLoad: function Change_onLoad() {
    1.36 +    /* Load labels */
    1.37 +    let introText = document.getElementById("introText");
    1.38 +    let introText2 = document.getElementById("introText2");
    1.39 +    let warningText = document.getElementById("warningText");
    1.40 +
    1.41 +    // load some other elements & info from the window
    1.42 +    this._dialog = document.getElementById("change-dialog");
    1.43 +    this._dialogType = window.arguments[0];
    1.44 +    this._duringSetup = window.arguments[1];
    1.45 +    this._status = document.getElementById("status");
    1.46 +    this._statusIcon = document.getElementById("statusIcon");
    1.47 +    this._statusRow = document.getElementById("statusRow");
    1.48 +    this._firstBox = document.getElementById("textBox1");
    1.49 +    this._secondBox = document.getElementById("textBox2");
    1.50 +
    1.51 +    this._dialog.getButton("finish").disabled = true;
    1.52 +    this._dialog.getButton("back").hidden = true;
    1.53 +
    1.54 +    this._stringBundle =
    1.55 +      Services.strings.createBundle("chrome://browser/locale/syncGenericChange.properties");
    1.56 +
    1.57 +    switch (this._dialogType) {
    1.58 +      case "UpdatePassphrase":
    1.59 +      case "ResetPassphrase":
    1.60 +        document.getElementById("textBox1Row").hidden = true;
    1.61 +        document.getElementById("textBox2Row").hidden = true;
    1.62 +        document.getElementById("passphraseLabel").value
    1.63 +          = this._str("new.recoverykey.label");
    1.64 +        document.getElementById("passphraseSpacer").hidden = false;
    1.65 +
    1.66 +        if (this._updatingPassphrase) {
    1.67 +          document.getElementById("passphraseHelpBox").hidden = false;
    1.68 +          document.title = this._str("new.recoverykey.title");
    1.69 +          introText.textContent = this._str("new.recoverykey.introText");
    1.70 +          this._dialog.getButton("finish").label
    1.71 +            = this._str("new.recoverykey.acceptButton");
    1.72 +        }
    1.73 +        else {
    1.74 +          document.getElementById("generatePassphraseButton").hidden = false;
    1.75 +          document.getElementById("passphraseBackupButtons").hidden = false;
    1.76 +          this._passphraseBox.setAttribute("readonly", "true");
    1.77 +          let pp = Weave.Service.identity.syncKey;
    1.78 +          if (Weave.Utils.isPassphrase(pp))
    1.79 +             pp = Weave.Utils.hyphenatePassphrase(pp);
    1.80 +          this._passphraseBox.value = pp;
    1.81 +          this._passphraseBox.focus();
    1.82 +          document.title = this._str("change.recoverykey.title");
    1.83 +          introText.textContent = this._str("change.synckey.introText2");
    1.84 +          warningText.textContent = this._str("change.recoverykey.warningText");
    1.85 +          this._dialog.getButton("finish").label
    1.86 +            = this._str("change.recoverykey.acceptButton");
    1.87 +          if (this._duringSetup) {
    1.88 +            this._dialog.getButton("finish").disabled = false;
    1.89 +          }
    1.90 +        }
    1.91 +        break;
    1.92 +      case "ChangePassword":
    1.93 +        document.getElementById("passphraseRow").hidden = true;
    1.94 +        let box1label = document.getElementById("textBox1Label");
    1.95 +        let box2label = document.getElementById("textBox2Label");
    1.96 +        box1label.value = this._str("new.password.label");
    1.97 +
    1.98 +        if (this._currentPasswordInvalid) {
    1.99 +          document.title = this._str("new.password.title");
   1.100 +          introText.textContent = this._str("new.password.introText");
   1.101 +          this._dialog.getButton("finish").label
   1.102 +            = this._str("new.password.acceptButton");
   1.103 +          document.getElementById("textBox2Row").hidden = true;
   1.104 +        }
   1.105 +        else {
   1.106 +          document.title = this._str("change.password.title");
   1.107 +          box2label.value = this._str("new.password.confirm");
   1.108 +          introText.textContent = this._str("change.password3.introText");
   1.109 +          warningText.textContent = this._str("change.password.warningText");
   1.110 +          this._dialog.getButton("finish").label
   1.111 +            = this._str("change.password.acceptButton");
   1.112 +        }
   1.113 +        break;
   1.114 +    }
   1.115 +    document.getElementById("change-page")
   1.116 +            .setAttribute("label", document.title);
   1.117 +  },
   1.118 +
   1.119 +  _clearStatus: function _clearStatus() {
   1.120 +    this._status.value = "";
   1.121 +    this._statusIcon.removeAttribute("status");
   1.122 +  },
   1.123 +
   1.124 +  _updateStatus: function Change__updateStatus(str, state) {
   1.125 +     this._updateStatusWithString(this._str(str), state);
   1.126 +  },
   1.127 +  
   1.128 +  _updateStatusWithString: function Change__updateStatusWithString(string, state) {
   1.129 +    this._statusRow.hidden = false;
   1.130 +    this._status.value = string;
   1.131 +    this._statusIcon.setAttribute("status", state);
   1.132 +
   1.133 +    let error = state == "error";
   1.134 +    this._dialog.getButton("cancel").disabled = !error;
   1.135 +    this._dialog.getButton("finish").disabled = !error;
   1.136 +    document.getElementById("printSyncKeyButton").disabled = !error;
   1.137 +    document.getElementById("saveSyncKeyButton").disabled = !error;
   1.138 +
   1.139 +    if (state == "success")
   1.140 +      window.setTimeout(window.close, 1500);
   1.141 +  },
   1.142 +
   1.143 +  onDialogAccept: function() {
   1.144 +    switch (this._dialogType) {
   1.145 +      case "UpdatePassphrase":
   1.146 +      case "ResetPassphrase":
   1.147 +        return this.doChangePassphrase();
   1.148 +        break;
   1.149 +      case "ChangePassword":
   1.150 +        return this.doChangePassword();
   1.151 +        break;
   1.152 +    }
   1.153 +  },
   1.154 +
   1.155 +  doGeneratePassphrase: function () {
   1.156 +    let passphrase = Weave.Utils.generatePassphrase();
   1.157 +    this._passphraseBox.value = Weave.Utils.hyphenatePassphrase(passphrase);
   1.158 +    this._dialog.getButton("finish").disabled = false;
   1.159 +  },
   1.160 +
   1.161 +  doChangePassphrase: function Change_doChangePassphrase() {
   1.162 +    let pp = Weave.Utils.normalizePassphrase(this._passphraseBox.value);
   1.163 +    if (this._updatingPassphrase) {
   1.164 +      Weave.Service.identity.syncKey = pp;
   1.165 +      if (Weave.Service.login()) {
   1.166 +        this._updateStatus("change.recoverykey.success", "success");
   1.167 +        Weave.Service.persistLogin();
   1.168 +        Weave.Service.scheduler.delayedAutoConnect(0);
   1.169 +      }
   1.170 +      else {
   1.171 +        this._updateStatus("new.passphrase.status.incorrect", "error");
   1.172 +      }
   1.173 +    }
   1.174 +    else {
   1.175 +      this._updateStatus("change.recoverykey.label", "active");
   1.176 +
   1.177 +      if (Weave.Service.changePassphrase(pp))
   1.178 +        this._updateStatus("change.recoverykey.success", "success");
   1.179 +      else
   1.180 +        this._updateStatus("change.recoverykey.error", "error");
   1.181 +    }
   1.182 +
   1.183 +    return false;
   1.184 +  },
   1.185 +
   1.186 +  doChangePassword: function Change_doChangePassword() {
   1.187 +    if (this._currentPasswordInvalid) {
   1.188 +      Weave.Service.identity.basicPassword = this._firstBox.value;
   1.189 +      if (Weave.Service.login()) {
   1.190 +        this._updateStatus("change.password.status.success", "success");
   1.191 +        Weave.Service.persistLogin();
   1.192 +      }
   1.193 +      else {
   1.194 +        this._updateStatus("new.password.status.incorrect", "error");
   1.195 +      }
   1.196 +    }
   1.197 +    else {
   1.198 +      this._updateStatus("change.password.status.active", "active");
   1.199 +
   1.200 +      if (Weave.Service.changePassword(this._firstBox.value))
   1.201 +        this._updateStatus("change.password.status.success", "success");
   1.202 +      else
   1.203 +        this._updateStatus("change.password.status.error", "error");
   1.204 +    }
   1.205 +
   1.206 +    return false;
   1.207 +  },
   1.208 +
   1.209 +  validate: function (event) {
   1.210 +    let valid = false;
   1.211 +    let errorString = "";
   1.212 +
   1.213 +    if (this._dialogType == "ChangePassword") {
   1.214 +      if (this._currentPasswordInvalid)
   1.215 +        [valid, errorString] = gSyncUtils.validatePassword(this._firstBox);
   1.216 +      else
   1.217 +        [valid, errorString] = gSyncUtils.validatePassword(this._firstBox, this._secondBox);
   1.218 +    }
   1.219 +    else {
   1.220 +      if (!this._updatingPassphrase)
   1.221 +        return;
   1.222 +
   1.223 +      valid = this._passphraseBox.value != "";
   1.224 +    }
   1.225 +
   1.226 +    if (errorString == "")
   1.227 +      this._clearStatus();
   1.228 +    else
   1.229 +      this._updateStatusWithString(errorString, "error");
   1.230 +
   1.231 +    this._statusRow.hidden = valid;
   1.232 +    this._dialog.getButton("finish").disabled = !valid;
   1.233 +  },
   1.234 +
   1.235 +  _str: function Change__string(str) {
   1.236 +    return this._stringBundle.GetStringFromName(str);
   1.237 +  }
   1.238 +};

mercurial