summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/prefs/content/accountUtils.js
blob: 8af2e72cf36b70284dc333c19af2fee676577c11 (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
/* 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/. */

/* import-globals-from AccountManager.js */
/* globals openTab */ // From utilityOverlay.js
/* globals SelectFolder */ // From messageWindow.js or messenger.js.
/* globals MsgGetMessage */ // From mailWindowOverlay.js.

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var gAnyValidIdentity = false; // If there are no valid identities for any account
// returns the first account with an invalid server or identity

var gNewAccountToLoad = null; // used to load new messages if we come from the mail3pane

function getInvalidAccounts(accounts) {
  let invalidAccounts = [];
  for (let account of accounts) {
    try {
      if (!account.incomingServer.valid) {
        invalidAccounts.push(account);
        // skip to the next account
        continue;
      }
    } catch (ex) {
      // this account is busted, just keep going
      continue;
    }

    for (let identity of account.identities) {
      if (identity.valid) {
        gAnyValidIdentity = true;
      } else {
        invalidAccounts.push(account);
      }
    }
  }
  return invalidAccounts;
}

function showMailIntegrationDialog() {
  const nsIShellService = Ci.nsIShellService;

  try {
    var shellService =
      Cc["@mozilla.org/suite/shell-service;1"].getService(nsIShellService);
    var appTypesCheck =
      shellService.shouldBeDefaultClientFor &
      (nsIShellService.MAIL | nsIShellService.NEWS);

    // show the default client dialog only if we have at least one account,
    // if we should check for the default client, and we want to check if we are
    // the default for mail/news and are not the default client for mail/news
    if (
      appTypesCheck &&
      shellService.shouldCheckDefaultClient &&
      !shellService.isDefaultClient(true, appTypesCheck)
    ) {
      window.browsingContext.topChromeWindow.openDialog(
        "chrome://communicator/content/defaultClientDialog.xhtml",
        "DefaultClient",
        "modal,centerscreen,chrome,resizable=no"
      );
    }
  } catch (ex) {}
}

/**
 * Check that an account exists which requires Local Folders.
 *
 * @returns {boolean} - true if at least 1 account exists that requires
 *                      Local Folders, else false.
 */
function requireLocalFoldersAccount() {
  return MailServices.accounts.accounts.some(account =>
    ["imap", "pop3", "nntp"].includes(account.incomingServer?.type)
  );
}

/**
 * Open the Nntp Account Wizard, or focus it if it's already open.
 */
function openNewsgroupAccountWizard() {
  window.browsingContext.topChromeWindow.openDialog(
    "chrome://messenger/content/AccountWizard.xhtml",
    "AccountWizard",
    "chrome,modal,titlebar,centerscreen"
  );
}

function AddIMAccount() {
  window.browsingContext.topChromeWindow.openDialog(
    "chrome://messenger/content/chat/imAccountWizard.xhtml",
    "",
    "chrome,modal,titlebar,centerscreen"
  );
}

function AddFeedAccount() {
  window.browsingContext.topChromeWindow.openDialog(
    "chrome://messenger-newsblog/content/feedAccountWizard.xhtml",
    "",
    "chrome,modal,titlebar,centerscreen"
  );
}

/**
 * Opens Address Book tab and triggers the address book creation dialog based on
 * the passed type.
 *
 * @param {string} type - The address book type needing creation. Accepted types
 *   are "JS", "LDAP", and "CARDDAV".
 */
function addNewAddressBook(type) {
  window.browsingContext.topChromeWindow.toAddressBook({
    action: `create_ab_${type}`,
  });
}

function showCalendarWizard() {
  window.browsingContext.topChromeWindow.openDialog(
    "chrome://calendar/content/calendar-creation.xhtml",
    "caEditServer",
    "chrome,titlebar,resizable,centerscreen",
    {}
  );
}

/**
 * Opens the account settings window on the specified account
 * and page of settings. If the window is already open it is only focused.
 *
 * @param {?string} selectPage - The file name for the viewing page, or null for
 *   the account main page. Other pages are 'am-server.xhtml',
 *   'am-copies.xhtml', 'am-offline.xhtml', 'am-addressing.xhtml',
 *   'am-smtp.xhtml'
 * @param {nsIMsgIncomingServer} [server] - The server of the account to select.
 */
async function MsgAccountManager(selectPage, server) {
  let win = Services.wm.getMostRecentWindow("mail:3pane");
  if (!win) {
    // No window available, so force open a new one.
    openTab(
      "contentTab",
      {
        url: "about:accountsettings",
        onLoad(event, browser) {
          browser.contentDocument.documentElement.server = server;
          browser.contentDocument.documentElement.selectPage = selectPage;
          browser.contentDocument.getElementById("accounttree").focus();
        },
      },
      "window"
    );
    return;
  }

  let tabmail = win.document.getElementById("tabmail");
  // If the server wasn't specified, and we have the window open, try
  // and use the currently selected folder to work out the server to select.
  if (!server) {
    server = tabmail.currentAbout3Pane?.gFolder ?? null;
  }
  // If the server is still not found, account settings will default to
  // the first account.

  // If Account settings tab is already open, change the server
  // and the selected page, reload the tab and switch to the tab.
  for (let tabInfo of tabmail.tabInfo) {
    let tab = tabmail.getTabForBrowser(tabInfo.browser);
    if (tab?.urlbar?.value == "about:accountsettings") {
      tab.browser.contentDocument.documentElement.server = server;
      tab.browser.contentDocument.documentElement.selectPage = selectPage;
      tab.browser.contentWindow.onLoad();
      tabmail.switchToTab(tabInfo);
      return;
    }
  }

  // Else fallback to opening a new tab in the window.
  tabmail.openTab("contentTab", {
    url: "about:accountsettings",
    onLoad(event, browser) {
      browser.contentDocument.documentElement.server = server;
      browser.contentDocument.documentElement.selectPage = selectPage;
      browser.contentDocument.getElementById("accounttree").focus();
    },
  });
}

function loadInboxForNewAccount() {
  // gNewAccountToLoad is set in the final screen of the Account Wizard if a POP account
  // was created, the download messages box is checked, and the wizard was opened from the 3pane
  if (gNewAccountToLoad) {
    var rootMsgFolder = gNewAccountToLoad.incomingServer.rootMsgFolder;
    const kInboxFlag = Ci.nsMsgFolderFlags.Inbox;
    var inboxFolder = rootMsgFolder.getFolderWithFlags(kInboxFlag);
    SelectFolder(inboxFolder.URI);
    window.focus();
    setTimeout(MsgGetMessage, 0);
    gNewAccountToLoad = null;
  }
}

// returns true if we migrated - it knows this because 4.x did not have the
// pref mailnews.quotingPrefs.version, so if it's not set, we're either
// migrating from 4.x, or a much older version of Mozilla.
function migrateGlobalQuotingPrefs(allIdentities) {
  // if reply_on_top and auto_quote exist then, if non-default
  // migrate and delete, if default just delete.
  var reply_on_top = 0;
  var auto_quote = true;
  var quotingPrefs = Services.prefs.getIntPref(
    "mailnews.quotingPrefs.version",
    0
  );
  var migrated = false;

  // If the quotingPrefs version is 0 then we need to migrate our preferences
  if (quotingPrefs == 0) {
    migrated = true;
    try {
      reply_on_top = Services.prefs.getIntPref("mailnews.reply_on_top");
      auto_quote = Services.prefs.getBoolPref("mail.auto_quote");
    } catch (ex) {}

    if (!auto_quote || reply_on_top) {
      for (let identity of allIdentities) {
        if (identity.valid) {
          identity.autoQuote = auto_quote;
          identity.replyOnTop = reply_on_top;
        }
      }
    }
    Services.prefs.setIntPref("mailnews.quotingPrefs.version", 1);
  }
  return migrated;
}

/**
 * Open the Account Setup Tab or focus it if it's already open.
 */
function openAccountSetupTab() {
  let mail3Pane = Services.wm.getMostRecentWindow("mail:3pane");
  let tabmail = mail3Pane.document.getElementById("tabmail");

  // Switch to the account setup tab if it's already open.
  for (let tabInfo of tabmail.tabInfo) {
    let tab = tabmail.getTabForBrowser(tabInfo.browser);
    if (tab?.urlbar?.value == "about:accountsetup") {
      let accountSetup = tabInfo.browser.contentWindow.gAccountSetup;
      // Reset the entire UI only if the previously opened setup was completed.
      if (accountSetup._currentModename == "success") {
        accountSetup.resetSetup();
      }
      tabmail.switchToTab(tabInfo);
      return;
    }
  }

  tabmail.openTab("contentTab", { url: "about:accountsetup" });
}

/**
 * Open the account setup tab and switch to the success view to show the newly
 * created account, or show an error if the account wasn't created.
 *
 * @param {object} account - A newly created account.
 * @param {string} name - The account name defined in the provider's website.
 * @param {string} email - The newly created email address.
 */
function openAccountSetupTabWithAccount(account, name, email) {
  // Define which actions we need to take after the account setup tab has been
  // loaded and we have access to its objects.
  let onTabLoaded = function (event, browser, account) {
    let accountSetup = browser.contentWindow.gAccountSetup;

    if (account) {
      // Update the account setup variables before kicking off the success view
      // which will start fetching linked services with these values.
      accountSetup._realname = name;
      accountSetup._email = email;
      accountSetup._password = account.incomingServer.password;
      accountSetup.showSuccessView(account);
      return;
    }

    accountSetup.showErrorNotification("account-setup-provisioner-error");
  };

  let mail3Pane = Services.wm.getMostRecentWindow("mail:3pane");
  let tabmail = mail3Pane.document.getElementById("tabmail");

  // Switch to the account setup tab if it's already open.
  for (let tabInfo of tabmail.tabInfo) {
    let tab = tabmail.getTabForBrowser(tabInfo.browser);
    if (tab?.urlbar?.value == "about:accountsetup") {
      let accountSetup = tabInfo.browser.contentWindow.gAccountSetup;
      // Reset the entire UI only if the previously opened setup was completed.
      if (accountSetup._currentModename == "success") {
        accountSetup.resetSetup();
      }
      tabmail.switchToTab(tabInfo);
      onTabLoaded(null, tabInfo.browser, account);
      return;
    }
  }

  // Open the account setup tab.
  tabmail.openTab("contentTab", {
    url: "about:accountsetup",
    onLoad(event, browser) {
      onTabLoaded(event, browser, account);
    },
  });
}

/**
 * Open the Account Provisioner Tab or focus it if it's already open.
 */
function openAccountProvisionerTab() {
  let mail3Pane = Services.wm.getMostRecentWindow("mail:3pane");
  let tabmail = mail3Pane.document.getElementById("tabmail");

  // Switch to the account setup tab if it's already open.
  for (let tabInfo of tabmail.tabInfo) {
    let tab = tabmail.getTabForBrowser(tabInfo.browser);
    if (tab?.urlbar?.value == "about:accountprovisioner") {
      tabmail.switchToTab(tabInfo);
      return;
    }
  }

  tabmail.openTab("contentTab", { url: "about:accountprovisioner" });
}

/**
 * Reveal the Folder Pane after an account creation callback.
 */
function updateMailPaneUI() {
  // Nothing to update since no account has been created.
  if (MailServices.accounts.accounts.length == 0) {
    return;
  }

  let mail3Pane = Services.wm.getMostRecentWindow("mail:3pane");
  // Set the folderPaneVisible to true in the tabmail to prevent collapsing
  // on tab switch.
  let tabmail = mail3Pane.document.getElementById("tabmail");
  tabmail.tabInfo[0].folderPaneVisible = true;
}

/**
 * Open the OpenPGP Key Manager from outside the Account Settings.
 */
function openKeyManager() {
  window.browsingContext.topChromeWindow.openDialog(
    "chrome://openpgp/content/ui/enigmailKeyManager.xhtml",
    "enigmail:KeyManager",
    "dialog,centerscreen,resizable",
    {
      cancelCallback: null,
      okCallback: null,
    }
  );
}