Bug 342632 - Adapt callers to defaultAccount being possibly null - Seamonkey. r=frg
authoraceman <acelists@atlas.sk>
Mon, 19 Nov 2018 00:56:53 +0100
changeset 33735 6b3a824d8c82659a9792374ae51cba2a3141f1e7
parent 33734 d7cafc35543ea8021b3a095f3783f7283b1ae357
child 33736 2dd62f4bcac3a09938c917b53ca16790d61ea295
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersfrg
bugs342632
Bug 342632 - Adapt callers to defaultAccount being possibly null - Seamonkey. r=frg
suite/mailnews/compose/MsgComposeCommands.js
suite/mailnews/mail3PaneWindowCommands.js
suite/mailnews/mailWindowOverlay.js
suite/mailnews/msgMail3PaneWindow.js
suite/mailnews/search/FilterListDialog.js
--- a/suite/mailnews/compose/MsgComposeCommands.js
+++ b/suite/mailnews/compose/MsgComposeCommands.js
@@ -1249,19 +1249,22 @@ function ComposeStartup(aParams)
          composeFields.body = args.body;
     }
   }
 
   gComposeType = params.type;
 
   // An identity with no email is likely not valid.
   if (!params.identity || !params.identity.email) {
+    let identities;
     // no pre selected identity, so use the default account
-    var identities = gAccountManager.defaultAccount.identities;
-    if (identities.length == 0)
+    let defaultAccount = gAccountManager.defaultAccount;
+    if (defaultAccount)
+      identities = defaultAccount.identities;
+    if (!identities || identities.length == 0)
       identities = gAccountManager.allIdentities;
     params.identity = identities.queryElementAt(0, Ci.nsIMsgIdentity);
   }
 
   identityList.selectedItem =
     identityList.getElementsByAttribute("identitykey", params.identity.key)[0];
   if (params.composeFields.from)
     identityList.value = MailServices.headerParser.parseDecodedHeader(params.composeFields.from)[0].toString();
--- a/suite/mailnews/mail3PaneWindowCommands.js
+++ b/suite/mailnews/mail3PaneWindowCommands.js
@@ -842,20 +842,24 @@ function IsSendUnsentMsgsEnabled(folderR
 
   // 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 = null;
   let folders = GetSelectedMsgFolders();
   if (folders.length > 0)
     identity = getIdentityForServer(folders[0].server);
 
-  if (!identity)
-    identity = Cc["@mozilla.org/messenger/account-manager;1"]
-                 .getService(Ci.nsIMsgAccountManager)
-                 .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()
--- a/suite/mailnews/mailWindowOverlay.js
+++ b/suite/mailnews/mailWindowOverlay.js
@@ -543,20 +543,22 @@ function RemoveAllMessageTags()
 }
 
 function InitNewMsgMenu(aPopup)
 {
   var identity = null;
   var folder = GetFirstSelectedMsgFolder();
   if (folder)
     identity = getIdentityForServer(folder.server);
-  if (!identity)
-    identity = Cc["@mozilla.org/messenger/account-manager;1"]
-                 .getService(Ci.nsIMsgAccountManager)
-                 .defaultAccount.defaultIdentity;
+  if (!identity) {
+    defaultAccount = MailServices.accounts.defaultAccount;
+    if (defaultAccount)
+      identity = defaultAccount.defaultIdentity;
+  }
+
   // If the identity is not found, use the mail.html_compose pref to
   // determine the message compose type (HTML or PlainText).
   var composeHTML = identity ? identity.composeHtml
                              : Services.prefs.getBoolPref("mail.html_compose");
   const kIDs = {true: "button-newMsgHTML", false: "button-newMsgPlain"};
   document.getElementById(kIDs[composeHTML]).setAttribute("default", "true");
   document.getElementById(kIDs[!composeHTML]).removeAttribute("default");
 }
@@ -1519,28 +1521,23 @@ 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");
-            }
+            var defaultFolder = GetDefaultAccountRootFolder();
+            var checkCreateSubfolders = null;
+            if (defaultFolder)
+                checkCreateSubfolders = defaultFolder.canCreateSubfolders;
+
+            if (checkCreateSubfolders)
+                destinationFolder = defaultFolder;
         }
     }
     else
         destinationFolder = preselectedFolder;
 
     return destinationFolder;
 }
 
@@ -2324,23 +2321,19 @@ function PromptMessagesOffline(aPrefix)
       (Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0) +
       (Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1),
       gOfflinePromptsBundle.getString(aPrefix + 'MessagesOfflineGoButtonLabel'),
       null, null, null, checkValue);
 }
 
 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;
 }
 
 function GetFolderMessages()
 {
   var selectedFolders = GetSelectedMsgFolders();
   var defaultAccountRootFolder = GetDefaultAccountRootFolder();
--- a/suite/mailnews/msgMail3PaneWindow.js
+++ b/suite/mailnews/msgMail3PaneWindow.js
@@ -581,27 +581,29 @@ function SelectServer(server)
 // alter the folder pane selection when a server is removed
 // or changed (currently, when the real username or real hostname change)
 var gThreePaneIncomingServerListener = {
     onServerLoaded: function(server) {},
     onServerUnloaded: function(server) {
       var selectedFolders = GetSelectedMsgFolders();
       for (var i = 0; i < selectedFolders.length; i++) {
         if (ServerContainsFolder(server, selectedFolders[i])) {
-          SelectServer(accountManager.defaultAccount.incomingServer);
+          if (accountManager.defaultAccount)
+            SelectServer(accountManager.defaultAccount.incomingServer);
           // we've made a new selection, we're done
           return;
         }
       }
 
       // if nothing is selected at this point, better go select the default
       // this could happen if nothing was selected when the server was removed
       selectedFolders = GetSelectedMsgFolders();
       if (selectedFolders.length == 0) {
-        SelectServer(accountManager.defaultAccount.incomingServer);
+        if (accountManager.defaultAccount)
+          SelectServer(accountManager.defaultAccount.incomingServer);
       }
     },
     onServerChanged: function(server) {
       // if the current selected folder is on the server that changed
       // and that server is an imap or news server,
       // we need to update the selection.
       // on those server types, we'll be reconnecting to the server
       // and our currently selected folder will need to be reloaded
@@ -869,22 +871,22 @@ function Create3PaneGlobals()
 }
 
 function loadStartFolder(initialUri)
 {
     var defaultServer = null;
     var isLoginAtStartUpEnabled = false;
 
     //First get default account
-    try
+    if (!initialUri)
     {
-        if (!initialUri)
-        {
-            // Startup time.
-            defaultServer = accountManager.defaultAccount.incomingServer;
+        // Startup time.
+        defaultAccount = accountManager.defaultAccount;
+        if (defaultAccount) {
+            defaultServer = defaultAccount.incomingServer;
 
             // set the initialUri to the server, so we select it
             // so we'll get account central
             initialUri = defaultServer.serverURI;
 
             // 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
             // always go to server settings panel and turn off "Check for new mail at startup"
@@ -902,35 +904,36 @@ function loadStartFolder(initialUri)
             {
                 //now find Inbox
                 var rootMsgFolder = defaultServer.rootMsgFolder;
                 const kInboxFlag = Ci.nsMsgFolderFlags.Inbox;
                 var inboxFolder = rootMsgFolder.getFolderWithFlags(kInboxFlag);
                 if (inboxFolder)
                   initialUri = inboxFolder.URI;
             }
+        } else {
+            // If no default account then show account central page.
+            ShowAccountCentral();
         }
 
+   }
+
+   if (initialUri) {
         SelectFolder(initialUri);
 
         // Perform biff on the server to check for new mail, if:
         // the login at startup is enabled, and
         // this feature is not exceptionally overridden, and
         // the account is not deferred-to or deferred.
         if (isLoginAtStartUpEnabled &&
             gLoadStartFolder &&
             !defaultServer.isDeferredTo &&
             defaultServer.rootFolder == defaultServer.rootMsgFolder)
           defaultServer.performBiff(msgWindow);
     }
-    catch(ex)
-    {
-      // If no default account then show account central page.
-      ShowAccountCentral();
-    }
 
     MsgGetMessagesForAllServers(defaultServer);
 
     if (CheckForUnsentMessages() && !Services.io.offline)
     {
         InitPrompts();
         InitServices();
 
@@ -1453,23 +1456,18 @@ function GetFolderResource(tree, index)
 // 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) {
-      defaultAccount = null;
-    }
-    if (defaultAccount && defaultAccount.incomingServer)
+    var 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", Services.prefs.getIntPref(prefix + "manualMarkMode"));
       if (Services.prefs.prefHasUserValue(prefix + "spamLoggingEnabled"))
--- a/suite/mailnews/search/FilterListDialog.js
+++ b/suite/mailnews/search/FilterListDialog.js
@@ -510,21 +510,23 @@ function getFilterFolderForSelection()
  * 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()
 {
-    var 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
     var allServers = MailServices.accounts.allServers;
     var numServers = allServers.length;
     for (var index = 0; index < numServers; index++)
     {
         var currentServer =