Bug 459485 - "Clicking on Growl alert doesn't always bring Thunderbird (and maybe SeaMonkey) in front of a native app." [r=joshmoz,r/sr=Standard8]
authorDavid Humphrey <david.humphrey@senecac.on.ca>
Tue, 18 Nov 2008 20:16:55 +0000
changeset 1144 832c859036ef205fe75e22c0c513828f721d1e89
parent 1143 be77b2aa036bae5d252522ae518cc9148f9922ac
child 1145 f78ce96ba4d68a0ee1462231a7a73b878227bf7b
push idunknown
push userunknown
push dateunknown
reviewersjoshmoz, r
bugs459485
Bug 459485 - "Clicking on Growl alert doesn't always bring Thunderbird (and maybe SeaMonkey) in front of a native app." [r=joshmoz,r/sr=Standard8]
mailnews/base/src/nsMessengerOSXIntegration.cpp
--- a/mailnews/base/src/nsMessengerOSXIntegration.cpp
+++ b/mailnews/base/src/nsMessengerOSXIntegration.cpp
@@ -69,16 +69,30 @@
 #include "nsToolkitCompsCID.h"
 #include "nsINotificationsList.h"
 
 #include <Carbon/Carbon.h>
 
 #define kNewMailAlertIcon "chrome://messenger/skin/icons/new-mail-alert.png"
 #define kBiffShowAlertPref "mail.biff.show_alert"
 
+// HACK: Limitations in Focus/SetFocus on Mac (see bug 465446)
+nsresult FocusAppNative()
+{
+  ProcessSerialNumber psn;
+
+  if (::GetCurrentProcess(&psn) != 0)
+   return NS_ERROR_FAILURE;
+
+  if (::SetFrontProcess(&psn) != 0)
+   return NS_ERROR_FAILURE;
+
+  return NS_OK;
+}
+
 static void openMailWindow(const nsACString& aFolderUri)
 {
   nsresult rv;
   nsCOMPtr<nsIMsgMailSession> mailSession ( do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv));
   if (NS_FAILED(rv))
     return;
 
   nsCOMPtr<nsIMsgWindow> topMostMsgWindow;
@@ -88,16 +102,17 @@ static void openMailWindow(const nsACStr
     if (!aFolderUri.IsEmpty())
     {
       nsCOMPtr<nsIMsgWindowCommands> windowCommands;
       topMostMsgWindow->GetWindowCommands(getter_AddRefs(windowCommands));
       if (windowCommands)
         windowCommands->SelectFolder(aFolderUri);
     }
     
+    FocusAppNative();
     nsCOMPtr<nsIDOMWindowInternal> domWindow;
     topMostMsgWindow->GetDomWindow(getter_AddRefs(domWindow));
     domWindow->Focus();
   }
   else
   {
     // the user doesn't have a mail window open already so open one for them...
     nsCOMPtr<nsIMessengerWindowService> messengerWindowService =
@@ -390,36 +405,20 @@ nsMessengerOSXIntegration::OnItemIntProp
   } // if the biff property changed
 
   return NS_OK;
 }
 
 nsresult
 nsMessengerOSXIntegration::OnAlertClicked()
 {
-#ifdef MOZ_THUNDERBIRD
-  nsresult rv;
-  nsCOMPtr<nsIMsgMailSession> mailSession = do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv,rv);
-  
-  nsCOMPtr<nsIMsgWindow> topMostMsgWindow;
-  rv = mailSession->GetTopmostMsgWindow(getter_AddRefs(topMostMsgWindow));
-  if (topMostMsgWindow)
-  {
-    nsCOMPtr<nsIDOMWindowInternal> domWindow;
-    rv = topMostMsgWindow->GetDomWindow(getter_AddRefs(domWindow));
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    domWindow->Focus();
-  }
-#else
   nsCString folderURI;
   GetFirstFolderWithNewMail(folderURI);
   openMailWindow(folderURI);
-#endif
+
   return NS_OK;
 }
 
 nsresult
 nsMessengerOSXIntegration::OnAlertFinished(const PRUnichar * aAlertCookie)
 {
   mAlertInProgress = PR_FALSE;
   return NS_OK;