michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cc = Components.classes; michael@0: michael@0: Components.utils.import("resource://services-sync/main.js"); michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: let Change = { michael@0: _dialog: null, michael@0: _dialogType: null, michael@0: _status: null, michael@0: _statusIcon: null, michael@0: _firstBox: null, michael@0: _secondBox: null, michael@0: michael@0: get _passphraseBox() { michael@0: delete this._passphraseBox; michael@0: return this._passphraseBox = document.getElementById("passphraseBox"); michael@0: }, michael@0: michael@0: get _currentPasswordInvalid() { michael@0: return Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED; michael@0: }, michael@0: michael@0: get _updatingPassphrase() { michael@0: return this._dialogType == "UpdatePassphrase"; michael@0: }, michael@0: michael@0: onLoad: function Change_onLoad() { michael@0: /* Load labels */ michael@0: let introText = document.getElementById("introText"); michael@0: let introText2 = document.getElementById("introText2"); michael@0: let warningText = document.getElementById("warningText"); michael@0: michael@0: // load some other elements & info from the window michael@0: this._dialog = document.getElementById("change-dialog"); michael@0: this._dialogType = window.arguments[0]; michael@0: this._duringSetup = window.arguments[1]; michael@0: this._status = document.getElementById("status"); michael@0: this._statusIcon = document.getElementById("statusIcon"); michael@0: this._statusRow = document.getElementById("statusRow"); michael@0: this._firstBox = document.getElementById("textBox1"); michael@0: this._secondBox = document.getElementById("textBox2"); michael@0: michael@0: this._dialog.getButton("finish").disabled = true; michael@0: this._dialog.getButton("back").hidden = true; michael@0: michael@0: this._stringBundle = michael@0: Services.strings.createBundle("chrome://browser/locale/syncGenericChange.properties"); michael@0: michael@0: switch (this._dialogType) { michael@0: case "UpdatePassphrase": michael@0: case "ResetPassphrase": michael@0: document.getElementById("textBox1Row").hidden = true; michael@0: document.getElementById("textBox2Row").hidden = true; michael@0: document.getElementById("passphraseLabel").value michael@0: = this._str("new.recoverykey.label"); michael@0: document.getElementById("passphraseSpacer").hidden = false; michael@0: michael@0: if (this._updatingPassphrase) { michael@0: document.getElementById("passphraseHelpBox").hidden = false; michael@0: document.title = this._str("new.recoverykey.title"); michael@0: introText.textContent = this._str("new.recoverykey.introText"); michael@0: this._dialog.getButton("finish").label michael@0: = this._str("new.recoverykey.acceptButton"); michael@0: } michael@0: else { michael@0: document.getElementById("generatePassphraseButton").hidden = false; michael@0: document.getElementById("passphraseBackupButtons").hidden = false; michael@0: this._passphraseBox.setAttribute("readonly", "true"); michael@0: let pp = Weave.Service.identity.syncKey; michael@0: if (Weave.Utils.isPassphrase(pp)) michael@0: pp = Weave.Utils.hyphenatePassphrase(pp); michael@0: this._passphraseBox.value = pp; michael@0: this._passphraseBox.focus(); michael@0: document.title = this._str("change.recoverykey.title"); michael@0: introText.textContent = this._str("change.synckey.introText2"); michael@0: warningText.textContent = this._str("change.recoverykey.warningText"); michael@0: this._dialog.getButton("finish").label michael@0: = this._str("change.recoverykey.acceptButton"); michael@0: if (this._duringSetup) { michael@0: this._dialog.getButton("finish").disabled = false; michael@0: } michael@0: } michael@0: break; michael@0: case "ChangePassword": michael@0: document.getElementById("passphraseRow").hidden = true; michael@0: let box1label = document.getElementById("textBox1Label"); michael@0: let box2label = document.getElementById("textBox2Label"); michael@0: box1label.value = this._str("new.password.label"); michael@0: michael@0: if (this._currentPasswordInvalid) { michael@0: document.title = this._str("new.password.title"); michael@0: introText.textContent = this._str("new.password.introText"); michael@0: this._dialog.getButton("finish").label michael@0: = this._str("new.password.acceptButton"); michael@0: document.getElementById("textBox2Row").hidden = true; michael@0: } michael@0: else { michael@0: document.title = this._str("change.password.title"); michael@0: box2label.value = this._str("new.password.confirm"); michael@0: introText.textContent = this._str("change.password3.introText"); michael@0: warningText.textContent = this._str("change.password.warningText"); michael@0: this._dialog.getButton("finish").label michael@0: = this._str("change.password.acceptButton"); michael@0: } michael@0: break; michael@0: } michael@0: document.getElementById("change-page") michael@0: .setAttribute("label", document.title); michael@0: }, michael@0: michael@0: _clearStatus: function _clearStatus() { michael@0: this._status.value = ""; michael@0: this._statusIcon.removeAttribute("status"); michael@0: }, michael@0: michael@0: _updateStatus: function Change__updateStatus(str, state) { michael@0: this._updateStatusWithString(this._str(str), state); michael@0: }, michael@0: michael@0: _updateStatusWithString: function Change__updateStatusWithString(string, state) { michael@0: this._statusRow.hidden = false; michael@0: this._status.value = string; michael@0: this._statusIcon.setAttribute("status", state); michael@0: michael@0: let error = state == "error"; michael@0: this._dialog.getButton("cancel").disabled = !error; michael@0: this._dialog.getButton("finish").disabled = !error; michael@0: document.getElementById("printSyncKeyButton").disabled = !error; michael@0: document.getElementById("saveSyncKeyButton").disabled = !error; michael@0: michael@0: if (state == "success") michael@0: window.setTimeout(window.close, 1500); michael@0: }, michael@0: michael@0: onDialogAccept: function() { michael@0: switch (this._dialogType) { michael@0: case "UpdatePassphrase": michael@0: case "ResetPassphrase": michael@0: return this.doChangePassphrase(); michael@0: break; michael@0: case "ChangePassword": michael@0: return this.doChangePassword(); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: doGeneratePassphrase: function () { michael@0: let passphrase = Weave.Utils.generatePassphrase(); michael@0: this._passphraseBox.value = Weave.Utils.hyphenatePassphrase(passphrase); michael@0: this._dialog.getButton("finish").disabled = false; michael@0: }, michael@0: michael@0: doChangePassphrase: function Change_doChangePassphrase() { michael@0: let pp = Weave.Utils.normalizePassphrase(this._passphraseBox.value); michael@0: if (this._updatingPassphrase) { michael@0: Weave.Service.identity.syncKey = pp; michael@0: if (Weave.Service.login()) { michael@0: this._updateStatus("change.recoverykey.success", "success"); michael@0: Weave.Service.persistLogin(); michael@0: Weave.Service.scheduler.delayedAutoConnect(0); michael@0: } michael@0: else { michael@0: this._updateStatus("new.passphrase.status.incorrect", "error"); michael@0: } michael@0: } michael@0: else { michael@0: this._updateStatus("change.recoverykey.label", "active"); michael@0: michael@0: if (Weave.Service.changePassphrase(pp)) michael@0: this._updateStatus("change.recoverykey.success", "success"); michael@0: else michael@0: this._updateStatus("change.recoverykey.error", "error"); michael@0: } michael@0: michael@0: return false; michael@0: }, michael@0: michael@0: doChangePassword: function Change_doChangePassword() { michael@0: if (this._currentPasswordInvalid) { michael@0: Weave.Service.identity.basicPassword = this._firstBox.value; michael@0: if (Weave.Service.login()) { michael@0: this._updateStatus("change.password.status.success", "success"); michael@0: Weave.Service.persistLogin(); michael@0: } michael@0: else { michael@0: this._updateStatus("new.password.status.incorrect", "error"); michael@0: } michael@0: } michael@0: else { michael@0: this._updateStatus("change.password.status.active", "active"); michael@0: michael@0: if (Weave.Service.changePassword(this._firstBox.value)) michael@0: this._updateStatus("change.password.status.success", "success"); michael@0: else michael@0: this._updateStatus("change.password.status.error", "error"); michael@0: } michael@0: michael@0: return false; michael@0: }, michael@0: michael@0: validate: function (event) { michael@0: let valid = false; michael@0: let errorString = ""; michael@0: michael@0: if (this._dialogType == "ChangePassword") { michael@0: if (this._currentPasswordInvalid) michael@0: [valid, errorString] = gSyncUtils.validatePassword(this._firstBox); michael@0: else michael@0: [valid, errorString] = gSyncUtils.validatePassword(this._firstBox, this._secondBox); michael@0: } michael@0: else { michael@0: if (!this._updatingPassphrase) michael@0: return; michael@0: michael@0: valid = this._passphraseBox.value != ""; michael@0: } michael@0: michael@0: if (errorString == "") michael@0: this._clearStatus(); michael@0: else michael@0: this._updateStatusWithString(errorString, "error"); michael@0: michael@0: this._statusRow.hidden = valid; michael@0: this._dialog.getButton("finish").disabled = !valid; michael@0: }, michael@0: michael@0: _str: function Change__string(str) { michael@0: return this._stringBundle.GetStringFromName(str); michael@0: } michael@0: };