File: addAccountDialog.js

package info (click to toggle)
thunderbird 1%3A52.9.1-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,709,148 kB
  • sloc: cpp: 5,081,792; ansic: 2,051,951; python: 458,733; java: 241,615; xml: 193,600; asm: 178,649; sh: 81,881; makefile: 24,702; perl: 16,874; objc: 4,389; yacc: 1,816; ada: 1,697; lex: 1,257; pascal: 1,251; cs: 879; exp: 499; php: 436; lisp: 258; awk: 152; sed: 51; ruby: 47; csh: 27
file content (304 lines) | stat: -rw-r--r-- 10,334 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 ; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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/. */

var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;
var Cr = Components.results;

var kFormId = "provider-form";

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource:///modules/cloudFileAccounts.js");

function createAccountObserver() {};

createAccountObserver.prototype = {
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIRequestObserver]),
  onStartRequest: function(aRequest, aContext) {},
  onStopRequest: function(aRequest, aContext, aStatusCode) {
    if (aStatusCode == Cr.NS_OK
        && aContext instanceof Ci.nsIMsgCloudFileProvider) {
      let accountKey = aContext.accountKey;

      // For now, we'll just set the display name to be the name of the service
      cloudFileAccounts.setDisplayName(accountKey, aContext.displayName);

      window.arguments[0].accountKey = aContext.accountKey;
      window.close();
    }
    else {
      if (aContext instanceof Ci.nsIMsgCloudFileProvider) {
        cloudFileAccounts.removeAccount(aContext.accountKey);
      }
      else {
        // Something went seriously wrong here...
        Components.utils.reportError("Cloud account creation failed, and " +
                                     "provider instance missing!");
      }

      addAccountDialog._accept.disabled = false;
      addAccountDialog._messages.selectedPanel = addAccountDialog._error;
    }
  },
}

var addAccountDialog = {
  _settings: null,
  _settingsWrap: null,
  _accountType: null,
  _accept: null,
  _strings: Services.strings
                    .createBundle("chrome://messenger/locale/cloudfile/addAccountDialog.properties"),
  // This blacklist is for providers who no longer want to be offered
  // as an option for a new Filelink provider, but still wants to
  // exist as a Filelink provider for pre-existing users.
  _blacklist: new Set(["YouSendIt"]),

  onInit: function AAD_onInit() {
    this._settings = document.getElementById("accountSettings");
    this._accountType = document.getElementById("accountType");
    this._noAccountText = document.getElementById("noAccountText");
    this._accept = document.documentElement.getButton("accept");
    this._cancel = document.documentElement.getButton("cancel");
    this._messages = document.getElementById("messages");
    this._authSpinner = document.getElementById("authorizing");
    this._error = document.getElementById("error");
    this._createAccountText = document.getElementById("createAccountText");

    this.removeTitleMenuItem();

    // Determine whether any account types were added to the menulist,
    // if not, return early.
    if (this.addAccountTypes() == 0)
      return;

    // Hook up our onInput event handler
    this._settings.addEventListener("DOMContentLoaded", this, false);

    this._settings.addEventListener("overflow", this);

    // Hook up the selection handler.
    this._accountType.addEventListener("select", this);
    // Also call it to run it for the default selection.
    addAccountDialog.accountTypeSelected();

    // Hook up the default "Learn More..." link to the appropriate link.
    let learnMore = this._settings
                        .contentDocument
                        .querySelector('#learn-more > a[href=""]');
    if (learnMore)
      learnMore.href = Services.prefs
                               .getCharPref("mail.cloud_files.learn_more_url");
    // The default emptySettings.xhtml is already loaded into the IFrame
    // at this point, before we could attach our DOMContentLoaded event
    // listener, so we'll call the function here manually.
    this.onIFrameLoaded(null);

    addAccountDialog.fitIFrame();
  },

  onUnInit: function() {
    // Clean-up the event listeners.
    this._settings.removeEventListener("DOMContentLoaded", this, false);
    this._settings.removeEventListener("overflow", this);
    this._accountType.removeEventListener("select", this);

    return true;
  },

  handleEvent: function(aEvent) {
    switch (aEvent.type) {
      case "DOMContentLoaded": {
        this.onIFrameLoaded();
        break;
      }
      case "overflow": {
        if (this._settings.contentDocument.body)
          this.fitIFrame();
        break;
      }
      case "select": {
        this.accountTypeSelected();
        break;
      }
    }
  },

  onIFrameLoaded: function AAD_onIFrameLoaded(aEvent) {
    let doc = this._settings.contentDocument;

    let links = doc.getElementsByTagName("a");

    for (let link of links)
      link.addEventListener("click", this.onClickLink);

    let form = doc.getElementById(kFormId);

    if (form)
      form.addEventListener("input", this.onInput.bind(this));

    this.onInput();

    // Focus the first field in the form, if any, that does not have the
    // class "focus-filter".
    let firstField = doc.querySelector("form:not(.filter) input:not(.hidden)");
    if (firstField)
      firstField.focus();
  },

  fitIFrame: function() {
    // Determine the height of the accountSettings iframe, and adjust
    // the height of the window appropriately.

    // If no account is available, |.body| is undefined. In this case
    // return a minimum height of 16px without calling sizeToContent().
    if (!this._settings.contentDocument.body) {
      Cu.reportError("WARNING: addAccountDialog.js: fitFrame: There is no account and this._settings.contentDocument.body is undefined.");
      this._settings.style.height = this._settings.style.minHeight = "16px";
      return;
    }
    let newHeight = this._settings.contentDocument.body.offsetHeight;
    this._settings.style.height = this._settings.style.minHeight = newHeight + "px";
    window.sizeToContent();
  },

  removeTitleMenuItem: function AAD_removeTitleMenuItem() {
    let menuitem = this._accountType.querySelector('menuitem[value=""]');
    if (menuitem) {
      let index = this._accountType.getIndexOfItem(menuitem);
      this._accountType.removeItemAt(index);
    }
  },

  // Return number of additions to the menulist, zero if none happened.
  addAccountTypes: function AAD_addAccountTypes() {
    let accountTypeTotal = 0;
    for (let [key, provider] of cloudFileAccounts.enumerateProviders()) {
      // If we already have an account for this type, don't add it to the list.
      // This limitation will hopefully be removed in the future.
      if (cloudFileAccounts.getAccountsForType(key).length > 0)
        continue;

      if (this._blacklist.has(key))
        continue;

      let menuitem = document.createElement("menuitem");
      menuitem.setAttribute("label", provider.displayName);
      menuitem.setAttribute("value", key);

      if (provider.iconClass) {
        menuitem.setAttribute("class", "menuitem-iconic menuitem-with-favicon");
        menuitem.setAttribute("image", provider.iconClass);
      }

      this._accountType.menupopup.appendChild(menuitem);
      accountTypeTotal++;
    }

    // This block should go away when bug 748437 gets fixed, since we'll
    // be able to add an arbitrary number of accounts for each account type.
    if (this._accountType.itemCount == 0) {
      this._createAccountText.hidden = true;
      this._accountType.hidden = true;
      this._accept.disabled = true;
      this._noAccountText.hidden = false;
      this._settings.classList.remove("indent");
      this._settings.classList.add("small-indent")
      this._cancel.focus();
    }

    // If there's only one option, let's choose it for the user to avoid
    // a few clicks.
    if (this._accountType.itemCount == 1)
      this._accountType.selectedIndex = 0;

    return accountTypeTotal;
  },

  onOK: function AAD_onOK() {
    let accountType = this._accountType.value;
    let obs = new createAccountObserver();

    let extras = this.getExtraArgs();

    let provider = cloudFileAccounts.createAccount(accountType, obs, extras);
    this._accept.disabled = true;

    this._messages.selectedPanel = this._authSpinner;

    // Uninitialize the dialog before closing.
    this.onUnInit();
    return false;
  },

  getExtraArgs: function AAD_getExtraArgs() {
    if (!this._settings)
      return {};

    let func = this._settings.contentWindow
                   .wrappedJSObject
                   .extraArgs;
    if (!func)
      return {};

    return func();
  },

  accountTypeSelected: function AAD_accountTypeSelected() {
    let providerKey = this._accountType.selectedItem.value;
    if (!providerKey)
      return;

    let provider = cloudFileAccounts.getProviderForType(providerKey);
    if (!provider)
      return;

    // Reset the message display
    this._messages.selectedIndex = -1;

    // Load up the correct XHTML page for this provider.
    this._settings.contentDocument.location.href = provider.settingsURL;
  },

  onClickLink: function AAD_onClickLink(e) {
    e.preventDefault();
    let href = e.target.getAttribute("href");
    gProtocolService.loadUrl(Services.io.newURI(href, "UTF-8", null));
  },

  onInput: function AAD_onInput() {
    // Let's see if we have everything we need to make OK enabled...
    if (this._accountType.selectedIndex == -1) {
      // We have the "Select a service provider" menuitem selected, so we
      // shouldn't be able to click "Set up account"
      this._accept.disabled = true;
    }
    else {
      this._accept.disabled = !this.checkValidity();
    }
  },

  checkValidity: function AAD_checkValidity() {
    // If there's a form in the iframe, ensure that
    // it's checkValidity function passes.
    let form = this._settings
                   .contentWindow
                   .wrappedJSObject
                   .document
                   .getElementById(kFormId);

    if (form)
      return form.checkValidity();

    return true;
  },
}

XPCOMUtils.defineLazyServiceGetter(this, "gProtocolService",
                                   "@mozilla.org/uriloader/external-protocol-service;1",
                                   "nsIExternalProtocolService");