Bug 342632 - Adapt callers to defaultAccount being possibly null - mail/. r=mkmelin
authoraceman <acelists@atlas.sk>
Mon, 19 Nov 2018 00:56:51 +0100
changeset 33734 d7cafc35543ea8021b3a095f3783f7283b1ae357
parent 33733 b5de626e716d37cb333bcd462faf6ef5f67064ec
child 33735 6b3a824d8c82659a9792374ae51cba2a3141f1e7
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 - mail/. r=mkmelin
mail/base/content/FilterListDialog.js
mail/base/content/folderPane.js
mail/base/content/mail3PaneWindowCommands.js
mail/base/content/mailCommands.js
mail/base/content/mailWindowOverlay.js
mail/base/content/msgMail3PaneWindow.js
mail/base/modules/MailInstrumentation.jsm
mail/components/compose/content/MsgComposeCommands.js
--- a/mail/base/content/FilterListDialog.js
+++ b/mail/base/content/FilterListDialog.js
@@ -833,20 +833,23 @@ function getFilterFolderForSelection(aFo
 /**
  * If the selected server cannot have filters, get the default server.
  * If the default server cannot have filters, check all accounts
  * and get a server that can have filters.
  *
  * @returns an nsIMsgIncomingServer
  */
 function getServerThatCanHaveFilters() {
-    let defaultIncomingServer = MailServices.accounts.defaultAccount.incomingServer;
-    // Check to see if default server can have filters.
-    if (defaultIncomingServer.canHaveFilters)
-      return defaultIncomingServer;
+    let defaultAccount = MailServices.accounts.defaultAccount;
+    if (defaultAccount) {
+      let defaultIncomingServer = defaultAccount.incomingServer;
+      // Check to see if default server can have filters.
+      if (defaultIncomingServer.canHaveFilters)
+        return defaultIncomingServer;
+    }
 
     // If it cannot, check all accounts to find a server
     // that can have filters.
     let allServers = MailServices.accounts.allServers;
     for (let currentServer of fixIterator(allServers, Ci.nsIMsgIncomingServer)) {
       if (currentServer.canHaveFilters)
         return currentServer;
     }
--- a/mail/base/content/folderPane.js
+++ b/mail/base/content/folderPane.js
@@ -2460,16 +2460,19 @@ var gFolderTreeController = {
       // Check if we can create them at the root
       let rootMsgFolder = folder.server.rootMsgFolder;
       if (rootMsgFolder.canCreateSubfolders)
         folder = rootMsgFolder;
       else // just use the default account
         folder = GetDefaultAccountRootFolder();
     }
 
+    if (!folder)
+      return;
+
     let dualUseFolders = true;
     if (folder.server instanceof Ci.nsIImapIncomingServer)
       dualUseFolders = folder.server.dualUseFolders;
 
     function newFolderCallback(aName, aFolder) {
       // createSubfolder can throw an exception, causing the newFolder dialog
       // to not close and wait for another input.
       // TODO: Rewrite this logic and also move the opening of alert dialogs from
@@ -2718,19 +2721,20 @@ var gFolderTreeController = {
   /**
    * Opens the dialog to create a new virtual folder
    *
    * @param aName - the default name for the new folder
    * @param aSearchTerms - the search terms associated with the folder
    * @param aParent - the folder to run the search terms on
    */
   newVirtualFolder(aName, aSearchTerms, aParent) {
-    let folder = aParent || gFolderTreeView.getSelectedFolders()[0];
+    let folder = aParent || gFolderTreeView.getSelectedFolders()[0] ||
+                 GetDefaultAccountRootFolder();
     if (!folder)
-      folder = GetDefaultAccountRootFolder();
+      return;
 
     let name = folder.prettyName;
     if (aName)
       name += "-" + aName;
 
     window.openDialog("chrome://messenger/content/virtualFolderProperties.xul",
                       "",
                       "chrome,modal,centerscreen",
--- a/mail/base/content/mail3PaneWindowCommands.js
+++ b/mail/base/content/mail3PaneWindowCommands.js
@@ -1170,31 +1170,36 @@ function IsSendUnsentMsgsEnabled(unsentM
 
   // Otherwise, we don't know where we are, so use the current identity and
   // find out if we have messages or not via that.
   let identity;
   let folders = GetSelectedMsgFolders();
   if (folders.length > 0)
     identity = getIdentityForServer(folders[0].server);
 
-  if (!identity)
-    identity = MailServices.accounts.defaultAccount.defaultIdentity;
+  if (!identity) {
+    let defaultAccount = MailServices.accounts.defaultAccount;
+    if (defaultAccount)
+      identity = defaultAccount.defaultIdentity;
+
+    if (!identity)
+      return false;
+  }
 
   return msgSendlater.hasUnsentMessages(identity);
 }
 
 /**
  * Determine whether there exists any server for which to show the Subscribe dialog.
  */
 function IsSubscribeEnabled() {
   // If there are any IMAP or News servers, we can show the dialog any time and
   // it will properly show those.
   let servers = MailServices.accounts.allServers;
-  for (let server of fixIterator(servers,
-                                 Ci.nsIMsgIncomingServer)) {
+  for (let server of fixIterator(servers, Ci.nsIMsgIncomingServer)) {
     if (server.type == "imap" || server.type == "nntp")
       return true;
   }
 
   // RSS accounts use a separate Subscribe dialog that we can only show when
   // such an account is selected.
   let preselectedFolder = GetFirstSelectedMsgFolder();
   if (preselectedFolder && preselectedFolder.server.type == "rss")
--- a/mail/base/content/mailCommands.js
+++ b/mail/base/content/mailCommands.js
@@ -36,20 +36,17 @@ function getBestIdentity(identities, opt
             hints[i].includes("<" + identity.email.toLowerCase() + ">"))
           return identity;
       }
     }
   }
 
   // Still no matches? Give up and pick the default or the first one.
   if (useDefault) {
-    let defaultAccount = null;
-    try {
-      defaultAccount = accountManager.defaultAccount;
-    } catch (ex) {}
+    let defaultAccount = accountManager.defaultAccount;
     if (defaultAccount && defaultAccount.defaultIdentity)
       return defaultAccount.defaultIdentity;
   }
 
   return identities.queryElementAt(0, Ci.nsIMsgIdentity);
 }
 
 function getIdentityForServer(server, optionalHint) {
--- a/mail/base/content/mailWindowOverlay.js
+++ b/mail/base/content/mailWindowOverlay.js
@@ -781,16 +781,19 @@ function SetGetMsgButtonTooltip() {
 
   var selectedFolders = GetSelectedMsgFolders();
   var folders;
   if (selectedFolders.length)
     folders = selectedFolders;
   else
     folders = [ GetDefaultAccountRootFolder() ];
 
+  if (!folders[0])
+    return;
+
   var bundle = document.getElementById("bundle_messenger");
   var listSeparator = bundle.getString("getMsgButtonTooltip.listSeparator");
 
   // Push the usernames through a Set() to remove duplicates.
   var names = new Set(folders.map(v => v.server.prettyName));
   var tooltipNames = Array.from(names).join(listSeparator);
   msgButton.tooltipText = bundle.getFormattedString("getMsgButtonTooltip",
                                                     [ tooltipNames ]);
@@ -1933,30 +1936,27 @@ function getDestinationFolder(preselecte
 
     var verifyCreateSubfolders = null;
     if (destinationFolder)
       verifyCreateSubfolders = destinationFolder.canCreateSubfolders;
 
     // In case the server cannot have subfolders, get default account and set
     // its incoming server as parent folder.
     if (!verifyCreateSubfolders) {
-      try {
-        var defaultFolder = GetDefaultAccountRootFolder();
-        var checkCreateSubfolders = null;
-        if (defaultFolder)
-          checkCreateSubfolders = defaultFolder.canCreateSubfolders;
-
-        if (checkCreateSubfolders)
-          destinationFolder = defaultFolder;
-
-      } catch (e) {
-        dump("Exception: defaultAccount Not Available\n");
-      }
+      let defaultFolder = GetDefaultAccountRootFolder();
+      let checkCreateSubfolders = null;
+      if (defaultFolder)
+        checkCreateSubfolders = defaultFolder.canCreateSubfolders;
+
+      if (checkCreateSubfolders)
+        destinationFolder = defaultFolder;
     }
   } else {
+    // XXX TODO: why do we select the preselectedFolder
+    // even if it can't create subfolders?
     destinationFolder = preselectedFolder;
   }
 
   return destinationFolder;
 }
 
 /** Open subscribe window. */
 function MsgSubscribe() {
@@ -2545,36 +2545,33 @@ function IsAccountOfflineEnabled() {
   var selectedFolders = GetSelectedMsgFolders();
 
   if (selectedFolders && (selectedFolders.length == 1))
       return selectedFolders[0].supportsOffline;
   return false;
 }
 
 function GetDefaultAccountRootFolder() {
-  try {
-    var account = accountManager.defaultAccount;
-    var defaultServer = account.incomingServer;
-    var defaultFolder = defaultServer.rootMsgFolder;
-    return defaultFolder;
-  } catch (ex) {
-  }
+  var account = accountManager.defaultAccount;
+  if (account)
+    return account.incomingServer.rootMsgFolder;
+
   return null;
 }
 
 /**
  * Check for new messages for all selected folders, or for the default account
  * in case no folders are selected.
  */
 function GetFolderMessages() {
   var selectedFolders = GetSelectedMsgFolders();
   var defaultAccountRootFolder = GetDefaultAccountRootFolder();
 
-  // if no default account, get msg isn't going do anything anyways
-  // so bail out
+  // If no default account, GetNewMsgs isn't going to do anything anyways
+  // so bail out.
   if (!defaultAccountRootFolder)
     return;
 
   // if nothing selected, use the default
   var folders = (selectedFolders.length) ? selectedFolders : [defaultAccountRootFolder];
   for (var i = 0; i < folders.length; i++) {
     var serverType = folders[i].server.type;
     if (folders[i].isServer && (serverType == "nntp")) {
--- a/mail/base/content/msgMail3PaneWindow.js
+++ b/mail/base/content/msgMail3PaneWindow.js
@@ -208,24 +208,23 @@ function SelectServer(server) {
 }
 
 // we have this incoming server listener in case we need to
 // alter the folder pane selection when a server is removed
 // or changed (currently, when the real username or real hostname change)
 var gThreePaneIncomingServerListener = {
     onServerLoaded(server) {},
     onServerUnloaded(server) {
-      let defaultServer;
-      try {
-        defaultServer = accountManager.defaultAccount.incomingServer;
-      } catch (e) {
+      let defaultAccount = accountManager.defaultAccount;
+      if (!defaultAccount) {
        // If there is no default server we have nothing to do.
        return;
       }
 
+      let defaultServer = defaultAccount.incomingServer;
       var selectedFolders = GetSelectedMsgFolders();
       for (var i = 0; i < selectedFolders.length; i++) {
         if (ServerContainsFolder(server, selectedFolders[i])) {
           SelectServer(defaultServer);
           // we've made a new selection, we're done
           return;
         }
       }
@@ -575,21 +574,20 @@ function LoadPostAccountWizard() {
   }
 
   function completeStartup() {
     // Check whether we need to show the default client dialog
     // First, check the shell service
     var nsIShellService = Ci.nsIShellService;
     if (nsIShellService) {
       var shellService;
-      var defaultAccount;
       try {
         shellService = Cc["@mozilla.org/mail/shell-service;1"].getService(nsIShellService);
-        defaultAccount = accountManager.defaultAccount;
       } catch (ex) {}
+      let defaultAccount = accountManager.defaultAccount;
 
       // Next, try loading the search integration module
       // We'll get a null SearchIntegration if we don't have one
       ChromeUtils.import("resource:///modules/SearchIntegration.jsm");
 
       // Show the default client dialog only if
       // EITHER: we have at least one account, and we aren't already the default
       // for mail,
@@ -844,21 +842,19 @@ function loadStartFolder(initialUri) {
     if (initialUri)
       loadFolder = true;
 
     // First get default account
     try {
         if (initialUri) {
             startFolder = MailUtils.getFolderForURI(initialUri);
         } else {
-            try {
-                var defaultAccount = accountManager.defaultAccount;
-            } catch (x) {
-                return; // exception caused by no default account, ignore it.
-            }
+            let defaultAccount = accountManager.defaultAccount;
+            if (!defaultAccount)
+                return;
 
             defaultServer = defaultAccount.incomingServer;
             var rootMsgFolder = defaultServer.rootMsgFolder;
 
             startFolder = rootMsgFolder;
 
             // Enable check new mail once by turning checkmail pref 'on' to bring
             // all users to one plane. This allows all users to go to Inbox. User can
@@ -1211,21 +1207,18 @@ function ReloadMessage() {
 // Some of the per account junk mail settings have been
 // converted to global prefs. Let's try to migrate some
 // of those settings from the default account.
 function MigrateJunkMailSettings() {
   var junkMailSettingsVersion = Services.prefs.getIntPref("mail.spam.version");
   if (!junkMailSettingsVersion) {
     // Get the default account, check to see if we have values for our
     // globally migrated prefs.
-    var defaultAccount;
-    try {
-      defaultAccount = accountManager.defaultAccount;
-    } catch (ex) {}
-    if (defaultAccount && defaultAccount.incomingServer) {
+    let defaultAccount = accountManager.defaultAccount;
+    if (defaultAccount) {
       // we only care about
       var prefix = "mail.server." + defaultAccount.incomingServer.key + ".";
       if (Services.prefs.prefHasUserValue(prefix + "manualMark")) {
         Services.prefs.setBoolPref("mail.spam.manualMark",
           Services.prefs.getBoolPref(prefix + "manualMark"));
       }
       if (Services.prefs.prefHasUserValue(prefix + "manualMarkMode")) {
         Services.prefs.setIntPref("mail.spam.manualMarkMode",
--- a/mail/base/modules/MailInstrumentation.jsm
+++ b/mail/base/modules/MailInstrumentation.jsm
@@ -88,30 +88,23 @@ var MailInstrumentation = {
     }
     this._currentState = this._createStateObject();
   },
 
   /**
    * Writes the state object to disk.
    */
   _postStateObject() {
-    // Getting defaultAccount will throw an exception if no account is set up.
     // This method runs for the smtp server before the account has been set up.
     if (MailServices.accounts.accounts.length == 0)
       return;
 
-    let defaultAccount;
-    try {
-      defaultAccount = MailServices.accounts.defaultAccount;
-    } catch (e) {
-      // Only report this failure if there are some accounts but fetching
-      // the default one failed. That is a problem.
-      logException(e);
+    let defaultAccount = MailServices.accounts.defaultAccount;
+    if (!defaultAccount)
       return;
-    }
 
       if (!this._currentState.userEmailHash) {
         let identity = defaultAccount.defaultIdentity;
         if (identity) // When we have only a feed account, there is no identity.
           this._currentState.userEmailHash = this._hashEmailAddress(identity.email);
       }
       let data = JSON.stringify(this._currentState);
       // post data only if state changed since last write.
--- a/mail/components/compose/content/MsgComposeCommands.js
+++ b/mail/components/compose/content/MsgComposeCommands.js
@@ -2744,22 +2744,26 @@ function ComposeStartup(aParams) {
           suitableCount++;
           if (suitableCount > 1)
             break; // No need to find more, it's already not unique.
         }
       }
     }
 
     if (!params.identity || !params.identity.email) {
+      let identity = null;
       // No preset identity and no match, so use the default account.
-      let identity = MailServices.accounts.defaultAccount.defaultIdentity;
-      if (!identity) {
-        let identities = MailServices.accounts.allIdentities;
-        if (identities.length > 0)
-          identity = identities.queryElementAt(0, Ci.nsIMsgIdentity);
+      let defaultAccount = MailServices.accounts.defaultAccount;
+      if (defaultAccount) {
+        identity = defaultAccount.defaultIdentity;
+        if (!identity) {
+          let identities = MailServices.accounts.allIdentities;
+          if (identities.length > 0)
+            identity = identities.queryElementAt(0, Ci.nsIMsgIdentity);
+        }
       }
       params.identity = identity;
     }
 
     // Warn if no or more than one match was found.
     // But don't warn for +suffix additions (a+b@c.com).
     if (from && (suitableCount > 1 ||
         (suitableCount == 0 && !emailSimilar(from, params.identity.email))))