Bug 342632 - Adapt callers to defaultAccount being possibly null - mailnews JS. r=mkmelin
authoraceman <acelists@atlas.sk>
Mon, 19 Nov 2018 00:56:49 +0100
changeset 33732 df8923d94a7af8755f16960a6e8f56e5ce68fe11
parent 33731 a6f1f63789af294ace5640de1e3d4cbeb9870877
child 33733 b5de626e716d37cb333bcd462faf6ef5f67064ec
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersmkmelin
bugs342632
Bug 342632 - Adapt callers to defaultAccount being possibly null - mailnews JS. r=mkmelin
mailnews/base/prefs/content/AccountManager.js
mailnews/base/prefs/content/AccountWizard.js
mailnews/base/search/content/searchWidgets.xml
mailnews/base/test/unit/test_accountMgr.js
mailnews/compose/test/unit/test_bug474774.js
mailnews/compose/test/unit/test_sendBackground.js
mailnews/compose/test/unit/test_sendMessageLater.js
mailnews/compose/test/unit/test_sendMessageLater2.js
mailnews/compose/test/unit/test_sendMessageLater3.js
mailnews/imap/test/unit/test_dontStatNoSelect.js
mailnews/imap/test/unit/test_imapHighWater.js
mailnews/imap/test/unit/test_nsIMsgFolderListenerIMAP.js
mailnews/test/resources/IMAPpump.js
--- a/mailnews/base/prefs/content/AccountManager.js
+++ b/mailnews/base/prefs/content/AccountManager.js
@@ -750,17 +750,17 @@ function setAccountLabel(aAccountKey, aA
 /**
  * Make currentAccount (currently selected in the account tree) the default one.
  */
 function onSetDefault(event) {
   // Make sure this function was not called while the control item is disabled
   if (event.target.getAttribute("disabled") == "true")
     return;
 
-  let previousDefault = getDefaultAccount();
+  let previousDefault = MailServices.accounts.defaultAccount;
   MailServices.accounts.defaultAccount = currentAccount;
   markDefaultServer(currentAccount, previousDefault);
 
   // This is only needed on Seamonkey which has this button.
   setEnabled(document.getElementById("setDefaultButton"), false);
 }
 
 function onRemoveAccount(event) {
@@ -817,17 +817,17 @@ function onRemoveAccount(event) {
   }
 
   if ((serverIndex >= 0) && (serverIndex < serverList.length))
     selectServer(serverList[serverIndex], null);
 
   // Either the default account was deleted so there is a new one
   // or the default account was not changed. Either way, there is
   // no need to unmark the old one.
-  markDefaultServer(getDefaultAccount(), null);
+  markDefaultServer(MailServices.accounts.defaultAccount, null);
 }
 
 function saveAccount(accountValues, account)
 {
   var identity = null;
   var server = null;
 
   if (account) {
@@ -974,17 +974,17 @@ function updateItems(tree, account, addA
 
   if (account && (tree.view.selection.count >= 1)) {
     // Only try to check properties if there was anything selected in the tree
     // and it belongs to an account.
     // Otherwise we have either selected a SMTP server, or there is some
     // problem. Either way, we don't want the user to act on it.
     let server = account.incomingServer;
 
-    if (account != getDefaultAccount() &&
+    if (account != MailServices.accounts.defaultAccount &&
         server.canBeDefaultServer && account.identities.length > 0)
       canSetDefault = true;
 
     canDelete = server.protocolInfo.canDelete || server.canDelete;
   }
 
   setEnabled(addAccountItem, true);
   setEnabled(setDefaultItem, canSetDefault);
@@ -1430,28 +1430,16 @@ function setFormElementValue(formElement
 
 // helper routine for account manager panels to get the current account for the selected server
 function getCurrentAccount()
 {
   return currentAccount;
 }
 
 /**
- * Returns the default account without throwing exception if there is none.
- * The account manager can be opened even if there are no account yet.
- */
-function getDefaultAccount() {
-  try {
-    return MailServices.accounts.defaultAccount;
-  } catch (e) {
-    return null; // No default account yet.
-  }
-}
-
-/**
  * Get the array of persisted form elements for the given page.
  */
 function getPageFormElements() {
   // Uses getElementsByAttribute() which returns a live NodeList which is usually
   // faster than e.g. querySelector().
   if ("getElementsByAttribute" in top.frames["contentFrame"].document)
     return top.frames["contentFrame"].document
               .getElementsByAttribute("wsm_persist", "true");
@@ -1654,17 +1642,17 @@ var gAccountTree = {
         treeitem.setAttribute("open", this._getAccountOpenState(accountKey));
         // Let the XULStore.json automatically save the 'open' state of the
         // account when it is changed.
         treeitem.setAttribute("persist", "open");
       }
       treeitem._account = account;
     }
 
-    markDefaultServer(getDefaultAccount(), null);
+    markDefaultServer(MailServices.accounts.defaultAccount, null);
 
     // Now add the outgoing server node.
     var treeitem = document.createElement("treeitem");
     mainTree.appendChild(treeitem);
     var treerow = document.createElement("treerow");
     treeitem.appendChild(treerow);
     var treecell = document.createElement("treecell");
     treerow.appendChild(treecell);
--- a/mailnews/base/prefs/content/AccountWizard.js
+++ b/mailnews/base/prefs/content/AccountWizard.js
@@ -48,17 +48,19 @@ var gPageData;
 
 var nsIMsgIdentity = Ci.nsIMsgIdentity;
 var nsIMsgIncomingServer = Ci.nsIMsgIncomingServer;
 var gPrefsBundle, gMessengerBundle;
 
 // the current nsIMsgAccount
 var gCurrentAccount;
 
-// default account
+// The default account before we create a new account.
+// We need to store this as just asking for the default account may switch
+// it to the newly created one if there was none before.
 var gDefaultAccount;
 
 // the current associative array that
 // will eventually be dumped into the account
 var gCurrentAccountData;
 
 // default picker mode for copies and folders
 var gDefaultSpecialFolderPickerMode = "0";
@@ -80,24 +82,19 @@ function onAccountWizardLoad() {
     {
       //dump("There is okCallback");
       top.okCallback = window.arguments[0].okCallback;
     }
   }
 
   checkForInvalidAccounts();
 
-  try {
-    gDefaultAccount = MailServices.accounts.defaultAccount;
-  }
-  catch (ex) {
-    // no default account, this is expected the first time you launch mail
-    // on a new profile
-    gDefaultAccount = null;
-  }
+  // It is fine if there is no default account, this is expected the first
+  // time you launch mail on a new profile.
+  gDefaultAccount = MailServices.accounts.defaultAccount;
 
   // Set default value for global inbox checkbox
   var checkGlobalInbox = document.getElementById("deferStorage");
   try {
     checkGlobalInbox.checked = Services.prefs.getBoolPref("mail.accountwizard.deferstorage");
   } catch(e) {}
 }
 
@@ -794,18 +791,17 @@ function getPreConfigDataForAccount(acco
     // (wizardSkipPanels, smtpRequiresUsername) are not defined.
   }
 
   return accountData;
 }
 
 function AccountToAccountData(account, defaultAccountData)
 {
-  dump("AccountToAccountData(" + account + ", " +
-       defaultAccountData + ")\n");
+  dump("AccountToAccountData(" + account + ", " + defaultAccountData + ")\n");
   var accountData = defaultAccountData;
   if (!accountData)
     accountData = new Object;
 
   accountData.incomingServer = account.incomingServer;
   accountData.identity = account.identities.queryElementAt(0, nsIMsgIdentity);
   accountData.smtp = MailServices.smtp.defaultServer;
 
@@ -961,32 +957,32 @@ function onFlush() {
 }
 
 /** If there are no default accounts..
   * this is will be the new default, so enable
   * check for mail at startup
   */
 function EnableCheckMailAtStartUpIfNeeded(newAccount)
 {
-  // Check if default account exists and if that account is alllowed to be
-  // a default account. If no such account, make this one as the default account
+  // Check if default account existed.
+  // If no such account, make this one the default account
   // and turn on the new mail check at startup for the current account
-  if (!(gDefaultAccount && gDefaultAccount.incomingServer.canBeDefaultServer)) {
+  if (!gDefaultAccount) {
     MailServices.accounts.defaultAccount = newAccount;
     newAccount.incomingServer.loginAtStartUp = true;
     newAccount.incomingServer.downloadOnBiff = true;
   }
 }
 
 function SetSmtpRequiresUsernameAttribute(accountData)
 {
   // If this is the default server, time to set the smtp user name
   // Set the generic attribute for requiring user name for smtp to true.
   // ISPs can override the pref via rdf files.
-  if (!(gDefaultAccount && gDefaultAccount.incomingServer.canBeDefaultServer)) {
+  if (!gDefaultAccount) {
     accountData.smtpRequiresUsername = true;
   }
 }
 
 function setNextPage(currentPageId, nextPageId) {
   var currentPage = document.getElementById(currentPageId);
   currentPage.next = nextPageId;
 }
--- a/mailnews/base/search/content/searchWidgets.xml
+++ b/mailnews/base/search/content/searchWidgets.xml
@@ -219,20 +219,22 @@
         <parameter name="populateTemplateList"/>
         <parameter name="templateMenuList"/>
         <body>
           <![CDATA[
             ChromeUtils.import("resource:///modules/iteratorUtils.jsm", this);
             let identitiesRaw = MailServices.accounts
               .getIdentitiesForServer(gFilterList.folder.server);
             let identities = Array.from(this.fixIterator(identitiesRaw,
-                                          Ci.nsIMsgIdentity));
+                                                         Ci.nsIMsgIdentity));
 
-            if (!identities.length) // typically if this is Local Folders
-              identities.push(MailServices.accounts.defaultAccount.defaultIdentity);
+            if (identities.length == 0) { // typically if this is Local Folders
+              if (MailServices.accounts.defaultAccount)
+                identities.push(MailServices.accounts.defaultAccount.defaultIdentity);
+            }
 
             let templateFound = false;
             let foldersScanned = [];
 
             for (let identity of identities) {
               let enumerator = null;
               let msgFolder;
               try {
--- a/mailnews/base/test/unit/test_accountMgr.js
+++ b/mailnews/base/test/unit/test_accountMgr.js
@@ -58,18 +58,17 @@ function run_test()
                "account1,account4,account5");
   let server5 = MailServices.accounts.getIncomingServer("server5")
                   .QueryInterface(Ci.nsIPop3IncomingServer);
   Assert.equal(server5.deferredToAccount, "account1");
 
   // Just make sure this doesn't throw an exception, because we did remove the
   // default account.
   let defaultAccount = MailServices.accounts.defaultAccount;
-  // GetDefaultAccount should have thrown an exception for null account.
-  Assert.notEqual(defaultAccount, null);
+  Assert.equal(defaultAccount, null);
 
   // Remove an account, and verify that the account list pref looks OK:
   let server = MailServices.accounts.getIncomingServer("server4");
 
   // We need to get the root folder to read from the folder cache
   // before it gets removed or else we'll assert, because we're
   // not completely initialized...
   let flags = server.rootFolder.flags;
--- a/mailnews/compose/test/unit/test_bug474774.js
+++ b/mailnews/compose/test/unit/test_bug474774.js
@@ -187,16 +187,17 @@ function run_test() {
   let incomingServer = MailServices.accounts.createIncomingServer("test", "localhost", "pop3");
 
   smtpServer = getBasicSmtpServer(0);
   identity = getSmtpIdentity(kIdentityMail, smtpServer);
 
   account.addIdentity(identity);
   account.defaultIdentity = identity;
   account.incomingServer = incomingServer;
+  MailServices.accounts.defaultAccount = account;
 
   sentFolder = localAccountUtils.rootFolder.createLocalSubfolder("Sent");
 
   identity.doFcc = false;
 
   // Now prepare to actually "send" the message later, i.e. dump it in the
   // unsent messages folder.
 
--- a/mailnews/compose/test/unit/test_sendBackground.js
+++ b/mailnews/compose/test/unit/test_sendBackground.js
@@ -112,16 +112,17 @@ function run_test() {
   server = setupServerDaemon();
   server.start();
   var smtpServer = getBasicSmtpServer(server.port);
   identity = getSmtpIdentity(kIdentityMail, smtpServer);
 
   account.addIdentity(identity);
   account.defaultIdentity = identity;
   account.incomingServer = incomingServer;
+  MailServices.accounts.defaultAccount = account;
 
   sentFolder = localAccountUtils.rootFolder.createLocalSubfolder("Sent");
 
   Assert.equal(identity.doFcc, true);
 
   // Now prepare to actually "send" the message later, i.e. dump it in the
   // unsent messages folder.
 
--- a/mailnews/compose/test/unit/test_sendMessageLater.js
+++ b/mailnews/compose/test/unit/test_sendMessageLater.js
@@ -193,16 +193,17 @@ function run_test() {
   let incomingServer = MailServices.accounts.createIncomingServer("test", "localhost", "pop3");
 
   smtpServer = getBasicSmtpServer(1);
   identity = getSmtpIdentity(kIdentityMail, smtpServer);
 
   account.addIdentity(identity);
   account.defaultIdentity = identity;
   account.incomingServer = incomingServer;
+  MailServices.accounts.defaultAccount = account;
 
   sentFolder = localAccountUtils.rootFolder.createLocalSubfolder("Sent");
 
   Assert.equal(identity.doFcc, true);
 
   // Now prepare to actually "send" the message later, i.e. dump it in the
   // unsent messages folder.
 
--- a/mailnews/compose/test/unit/test_sendMessageLater2.js
+++ b/mailnews/compose/test/unit/test_sendMessageLater2.js
@@ -247,16 +247,17 @@ function run_test() {
   let incomingServer = MailServices.accounts.createIncomingServer("test", "localhost", "pop3");
 
   smtpServer = getBasicSmtpServer(1);
   identity = getSmtpIdentity(kIdentityMail, smtpServer);
 
   account.addIdentity(identity);
   account.defaultIdentity = identity;
   account.incomingServer = incomingServer;
+  MailServices.accounts.defaultAccount = account;
 
   localAccountUtils.rootFolder.createLocalSubfolder("Sent");
 
   gSentFolder = msgSendLater.getUnsentMessagesFolder(identity);
 
   // Don't copy messages to sent folder for this test
   identity.doFcc = false;
 
--- a/mailnews/compose/test/unit/test_sendMessageLater3.js
+++ b/mailnews/compose/test/unit/test_sendMessageLater3.js
@@ -135,16 +135,17 @@ function run_test() {
   let incomingServer = MailServices.accounts.createIncomingServer("test", "localhost", "pop3");
 
   var smtpServer = getBasicSmtpServer();
   identity = getSmtpIdentity(kSender, smtpServer);
 
   account.addIdentity(identity);
   account.defaultIdentity = identity;
   account.incomingServer = incomingServer;
+  MailServices.accounts.defaultAccount = account;
 
   sentFolder = localAccountUtils.rootFolder.createLocalSubfolder("Sent");
 
   identity.doFcc = false;
 
   // Now prepare to actually "send" the message later, i.e. dump it in the
   // unsent messages folder.
 
--- a/mailnews/imap/test/unit/test_dontStatNoSelect.js
+++ b/mailnews/imap/test/unit/test_dontStatNoSelect.js
@@ -38,23 +38,23 @@ function run_test() {
   localAccountUtils.loadLocalMailAccount();
 
   // We need an identity so that updateFolder doesn't fail
   let localAccount = MailServices.accounts.createAccount();
   let identity = MailServices.accounts.createIdentity();
   localAccount.addIdentity(identity);
   localAccount.defaultIdentity = identity;
   localAccount.incomingServer = localAccountUtils.incomingServer;
-  MailServices.accounts.defaultAccount = localAccount;
 
   // Let's also have another account, using the same identity
   let imapAccount = MailServices.accounts.createAccount();
   imapAccount.addIdentity(identity);
   imapAccount.defaultIdentity = identity;
   imapAccount.incomingServer = gImapServer;
+  MailServices.accounts.defaultAccount = imapAccount;
 
   // Get the folder list...
   gImapServer.performExpand(null);
   gServer.performTest("SUBSCRIBE");
   // pref tuning: one connection only, turn off notifications
   // Make sure no biff notifications happen
   Services.prefs.setBoolPref("mail.biff.play_sound", false);
   Services.prefs.setBoolPref("mail.biff.show_alert", false);
--- a/mailnews/imap/test/unit/test_imapHighWater.js
+++ b/mailnews/imap/test/unit/test_imapHighWater.js
@@ -43,23 +43,23 @@ function run_test()
   gIMAPIncomingServer.maximumConnectionsNumber = 1;
 
   // We need an identity so that updateFolder doesn't fail
   let localAccount = MailServices.accounts.createAccount();
   let identity = MailServices.accounts.createIdentity();
   localAccount.addIdentity(identity);
   localAccount.defaultIdentity = identity;
   localAccount.incomingServer = localAccountUtils.incomingServer;
-  MailServices.accounts.defaultAccount = localAccount;
 
   // Let's also have another account, using the same identity
   let imapAccount = MailServices.accounts.createAccount();
   imapAccount.addIdentity(identity);
   imapAccount.defaultIdentity = identity;
   imapAccount.incomingServer = gIMAPIncomingServer;
+  MailServices.accounts.defaultAccount = imapAccount;
 
   // pref tuning: one connection only, turn off notifications
   Services.prefs.setBoolPref("mail.biff.play_sound", false);
   Services.prefs.setBoolPref("mail.biff.show_alert", false);
   Services.prefs.setBoolPref("mail.biff.show_tray_icon", false);
   Services.prefs.setBoolPref("mail.biff.animate_dock_icon", false);
   Services.prefs.setBoolPref("mail.server.default.autosync_offline_stores", false);
   // Don't prompt about offline download when going offline
--- a/mailnews/imap/test/unit/test_nsIMsgFolderListenerIMAP.js
+++ b/mailnews/imap/test/unit/test_nsIMsgFolderListenerIMAP.js
@@ -203,23 +203,23 @@ function run_test()
   localAccountUtils.loadLocalMailAccount();
 
   // We need an identity so that updateFolder doesn't fail
   let localAccount = MailServices.accounts.createAccount();
   let identity = MailServices.accounts.createIdentity();
   localAccount.addIdentity(identity);
   localAccount.defaultIdentity = identity;
   localAccount.incomingServer = localAccountUtils.incomingServer;
-  MailServices.accounts.defaultAccount = localAccount;
 
   // Let's also have another account, using the same identity
   let imapAccount = MailServices.accounts.createAccount();
   imapAccount.addIdentity(identity);
   imapAccount.defaultIdentity = identity;
   imapAccount.incomingServer = gIMAPIncomingServer;
+  MailServices.accounts.defaultAccount = imapAccount;
 
   // The server doesn't support more than one connection
   Services.prefs.setIntPref("mail.server.server1.max_cached_connections", 1);
   // Make sure no biff notifications happen
   Services.prefs.setBoolPref("mail.biff.play_sound", false);
   Services.prefs.setBoolPref("mail.biff.show_alert", false);
   Services.prefs.setBoolPref("mail.biff.show_tray_icon", false);
   Services.prefs.setBoolPref("mail.biff.animate_dock_icon", false);
--- a/mailnews/test/resources/IMAPpump.js
+++ b/mailnews/test/resources/IMAPpump.js
@@ -80,23 +80,23 @@ function setupIMAPPump(extensions)
     localAccountUtils.loadLocalMailAccount();
 
   // We need an identity so that updateFolder doesn't fail
   let localAccount = MailServices.accounts.createAccount();
   let identity = MailServices.accounts.createIdentity();
   localAccount.addIdentity(identity);
   localAccount.defaultIdentity = identity;
   localAccount.incomingServer = localAccountUtils.incomingServer;
-  MailServices.accounts.defaultAccount = localAccount;
 
   // Let's also have another account, using the same identity
   let imapAccount = MailServices.accounts.createAccount();
   imapAccount.addIdentity(identity);
   imapAccount.defaultIdentity = identity;
   imapAccount.incomingServer = IMAPPump.incomingServer;
+  MailServices.accounts.defaultAccount = imapAccount;
 
   // The server doesn't support more than one connection
   Services.prefs.setIntPref("mail.server.default.max_cached_connections", 1);
   // We aren't interested in downloading messages automatically
   Services.prefs.setBoolPref("mail.server.default.download_on_biff", false);
   Services.prefs.setBoolPref("mail.biff.play_sound", false);
   Services.prefs.setBoolPref("mail.biff.show_alert", false);
   Services.prefs.setBoolPref("mail.biff.show_tray_icon", false);