Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const Ci = Components.interfaces;
6 const Cc = Components.classes;
8 Components.utils.import("resource://services-sync/main.js");
9 Components.utils.import("resource://gre/modules/Services.jsm");
11 let Change = {
12 _dialog: null,
13 _dialogType: null,
14 _status: null,
15 _statusIcon: null,
16 _firstBox: null,
17 _secondBox: null,
19 get _passphraseBox() {
20 delete this._passphraseBox;
21 return this._passphraseBox = document.getElementById("passphraseBox");
22 },
24 get _currentPasswordInvalid() {
25 return Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED;
26 },
28 get _updatingPassphrase() {
29 return this._dialogType == "UpdatePassphrase";
30 },
32 onLoad: function Change_onLoad() {
33 /* Load labels */
34 let introText = document.getElementById("introText");
35 let introText2 = document.getElementById("introText2");
36 let warningText = document.getElementById("warningText");
38 // load some other elements & info from the window
39 this._dialog = document.getElementById("change-dialog");
40 this._dialogType = window.arguments[0];
41 this._duringSetup = window.arguments[1];
42 this._status = document.getElementById("status");
43 this._statusIcon = document.getElementById("statusIcon");
44 this._statusRow = document.getElementById("statusRow");
45 this._firstBox = document.getElementById("textBox1");
46 this._secondBox = document.getElementById("textBox2");
48 this._dialog.getButton("finish").disabled = true;
49 this._dialog.getButton("back").hidden = true;
51 this._stringBundle =
52 Services.strings.createBundle("chrome://browser/locale/syncGenericChange.properties");
54 switch (this._dialogType) {
55 case "UpdatePassphrase":
56 case "ResetPassphrase":
57 document.getElementById("textBox1Row").hidden = true;
58 document.getElementById("textBox2Row").hidden = true;
59 document.getElementById("passphraseLabel").value
60 = this._str("new.recoverykey.label");
61 document.getElementById("passphraseSpacer").hidden = false;
63 if (this._updatingPassphrase) {
64 document.getElementById("passphraseHelpBox").hidden = false;
65 document.title = this._str("new.recoverykey.title");
66 introText.textContent = this._str("new.recoverykey.introText");
67 this._dialog.getButton("finish").label
68 = this._str("new.recoverykey.acceptButton");
69 }
70 else {
71 document.getElementById("generatePassphraseButton").hidden = false;
72 document.getElementById("passphraseBackupButtons").hidden = false;
73 this._passphraseBox.setAttribute("readonly", "true");
74 let pp = Weave.Service.identity.syncKey;
75 if (Weave.Utils.isPassphrase(pp))
76 pp = Weave.Utils.hyphenatePassphrase(pp);
77 this._passphraseBox.value = pp;
78 this._passphraseBox.focus();
79 document.title = this._str("change.recoverykey.title");
80 introText.textContent = this._str("change.synckey.introText2");
81 warningText.textContent = this._str("change.recoverykey.warningText");
82 this._dialog.getButton("finish").label
83 = this._str("change.recoverykey.acceptButton");
84 if (this._duringSetup) {
85 this._dialog.getButton("finish").disabled = false;
86 }
87 }
88 break;
89 case "ChangePassword":
90 document.getElementById("passphraseRow").hidden = true;
91 let box1label = document.getElementById("textBox1Label");
92 let box2label = document.getElementById("textBox2Label");
93 box1label.value = this._str("new.password.label");
95 if (this._currentPasswordInvalid) {
96 document.title = this._str("new.password.title");
97 introText.textContent = this._str("new.password.introText");
98 this._dialog.getButton("finish").label
99 = this._str("new.password.acceptButton");
100 document.getElementById("textBox2Row").hidden = true;
101 }
102 else {
103 document.title = this._str("change.password.title");
104 box2label.value = this._str("new.password.confirm");
105 introText.textContent = this._str("change.password3.introText");
106 warningText.textContent = this._str("change.password.warningText");
107 this._dialog.getButton("finish").label
108 = this._str("change.password.acceptButton");
109 }
110 break;
111 }
112 document.getElementById("change-page")
113 .setAttribute("label", document.title);
114 },
116 _clearStatus: function _clearStatus() {
117 this._status.value = "";
118 this._statusIcon.removeAttribute("status");
119 },
121 _updateStatus: function Change__updateStatus(str, state) {
122 this._updateStatusWithString(this._str(str), state);
123 },
125 _updateStatusWithString: function Change__updateStatusWithString(string, state) {
126 this._statusRow.hidden = false;
127 this._status.value = string;
128 this._statusIcon.setAttribute("status", state);
130 let error = state == "error";
131 this._dialog.getButton("cancel").disabled = !error;
132 this._dialog.getButton("finish").disabled = !error;
133 document.getElementById("printSyncKeyButton").disabled = !error;
134 document.getElementById("saveSyncKeyButton").disabled = !error;
136 if (state == "success")
137 window.setTimeout(window.close, 1500);
138 },
140 onDialogAccept: function() {
141 switch (this._dialogType) {
142 case "UpdatePassphrase":
143 case "ResetPassphrase":
144 return this.doChangePassphrase();
145 break;
146 case "ChangePassword":
147 return this.doChangePassword();
148 break;
149 }
150 },
152 doGeneratePassphrase: function () {
153 let passphrase = Weave.Utils.generatePassphrase();
154 this._passphraseBox.value = Weave.Utils.hyphenatePassphrase(passphrase);
155 this._dialog.getButton("finish").disabled = false;
156 },
158 doChangePassphrase: function Change_doChangePassphrase() {
159 let pp = Weave.Utils.normalizePassphrase(this._passphraseBox.value);
160 if (this._updatingPassphrase) {
161 Weave.Service.identity.syncKey = pp;
162 if (Weave.Service.login()) {
163 this._updateStatus("change.recoverykey.success", "success");
164 Weave.Service.persistLogin();
165 Weave.Service.scheduler.delayedAutoConnect(0);
166 }
167 else {
168 this._updateStatus("new.passphrase.status.incorrect", "error");
169 }
170 }
171 else {
172 this._updateStatus("change.recoverykey.label", "active");
174 if (Weave.Service.changePassphrase(pp))
175 this._updateStatus("change.recoverykey.success", "success");
176 else
177 this._updateStatus("change.recoverykey.error", "error");
178 }
180 return false;
181 },
183 doChangePassword: function Change_doChangePassword() {
184 if (this._currentPasswordInvalid) {
185 Weave.Service.identity.basicPassword = this._firstBox.value;
186 if (Weave.Service.login()) {
187 this._updateStatus("change.password.status.success", "success");
188 Weave.Service.persistLogin();
189 }
190 else {
191 this._updateStatus("new.password.status.incorrect", "error");
192 }
193 }
194 else {
195 this._updateStatus("change.password.status.active", "active");
197 if (Weave.Service.changePassword(this._firstBox.value))
198 this._updateStatus("change.password.status.success", "success");
199 else
200 this._updateStatus("change.password.status.error", "error");
201 }
203 return false;
204 },
206 validate: function (event) {
207 let valid = false;
208 let errorString = "";
210 if (this._dialogType == "ChangePassword") {
211 if (this._currentPasswordInvalid)
212 [valid, errorString] = gSyncUtils.validatePassword(this._firstBox);
213 else
214 [valid, errorString] = gSyncUtils.validatePassword(this._firstBox, this._secondBox);
215 }
216 else {
217 if (!this._updatingPassphrase)
218 return;
220 valid = this._passphraseBox.value != "";
221 }
223 if (errorString == "")
224 this._clearStatus();
225 else
226 this._updateStatusWithString(errorString, "error");
228 this._statusRow.hidden = valid;
229 this._dialog.getButton("finish").disabled = !valid;
230 },
232 _str: function Change__string(str) {
233 return this._stringBundle.GetStringFromName(str);
234 }
235 };