Back to index

lightning-sunbird  0.9+nobinonly
nsNativeAppSupportMac.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsString.h"
00039 
00040 #include <Gestalt.h>
00041 #include <Dialogs.h>
00042 #include <Resources.h>
00043 #include <TextUtils.h>
00044 #include <ControlDefinitions.h>
00045 
00046 #include "nsCOMPtr.h"
00047 #include "nsNativeAppSupportBase.h"
00048 
00049 #include "nsIAppShellService.h"
00050 #include "nsIAppStartup.h"
00051 #include "nsIBaseWindow.h"
00052 #include "nsICommandLineRunner.h"
00053 #include "nsIDOMWindowInternal.h"
00054 #include "nsIDocShellTreeItem.h"
00055 #include "nsIDocShellTreeOwner.h"
00056 #include "nsIInterfaceRequestorUtils.h"
00057 #include "nsIObserver.h"
00058 #include "nsIServiceManager.h"
00059 #include "nsIWebNavigation.h"
00060 #include "nsIWidget.h"
00061 #include "nsIWindowMediator.h"
00062 
00063 #include "nsXPFEComponentsCID.h"
00064 
00065 static Boolean VersGreaterThan4(const FSSpec *fSpec);
00066 
00067 const OSType kNSCreator = 'MOSS';
00068 const OSType kMozCreator = 'MOZZ';
00069 const SInt16 kNSCanRunStrArrayID = 1000;
00070 const SInt16 kAnotherVersionStrIndex = 1;
00071 
00072 nsresult
00073 GetNativeWindowPointerFromDOMWindow(nsIDOMWindowInternal *window, WindowRef *nativeWindow);
00074 
00075 const SInt16 kNSOSVersErrsStrArrayID = 1001;
00076 
00077 enum {
00078         eOSXVersTooOldErrIndex = 1,
00079         eOSXVersTooOldExplanationIndex,
00080         eContinueButtonTextIndex,
00081         eQuitButtonTextIndex,
00082         eCarbonLibVersTooOldIndex,
00083         eCarbonLibVersTooOldExplanationIndex
00084      };
00085 
00086 class nsNativeAppSupportMac : public nsNativeAppSupportBase
00087 {
00088 public:
00089   nsNativeAppSupportMac() :
00090     mCanShowUI(PR_FALSE) { }
00091 
00092   NS_IMETHOD Start(PRBool* aRetVal);
00093   NS_IMETHOD ReOpen();
00094   NS_IMETHOD Enable();
00095 
00096 private:
00097   PRBool mCanShowUI;
00098 
00099 };
00100 
00101 NS_IMETHODIMP
00102 nsNativeAppSupportMac::Enable()
00103 {
00104   mCanShowUI = PR_TRUE;
00105   return NS_OK;
00106 }
00107 
00108 /* boolean start (); */
00109 NS_IMETHODIMP nsNativeAppSupportMac::Start(PRBool *_retval)
00110 {
00111   Str255 str1;
00112   Str255 str2;
00113   SInt16 outItemHit;
00114   long   response = 0;
00115   OSErr  err = ::Gestalt (gestaltSystemVersion, &response);
00116   // check for at least MacOS 8.5
00117   if ( err || response < 0x850)
00118   {
00119     ::StopAlert (5000, NULL);
00120     *_retval = PR_FALSE;
00121     return NS_ERROR_FAILURE;
00122   }
00123   
00124 #if TARGET_CARBON
00125   // If we're running under Mac OS X check for at least Mac OS X 10.1
00126   // If that fails display a StandardAlert giving the user the option
00127   // to continue running the app or quitting
00128   if (response >= 0x00001000 && response < 0x00001010)
00129   {
00130     // put up error dialog
00131     Str255 continueButtonLabel;
00132     Str255 quitButtonLabel;
00133     ::GetIndString(str1, kNSOSVersErrsStrArrayID, eOSXVersTooOldErrIndex);
00134     ::GetIndString(str2, kNSOSVersErrsStrArrayID, eOSXVersTooOldExplanationIndex);
00135     ::GetIndString(continueButtonLabel, kNSOSVersErrsStrArrayID, eContinueButtonTextIndex);
00136     ::GetIndString(quitButtonLabel, kNSOSVersErrsStrArrayID, eQuitButtonTextIndex);
00137     if (StrLength(str1) && StrLength(str1) && StrLength(continueButtonLabel) && StrLength(quitButtonLabel))
00138     {
00139       AlertStdAlertParamRec pRec;
00140       
00141       pRec.movable      = nil;
00142       pRec.filterProc       = nil;
00143       pRec.defaultText  = continueButtonLabel;
00144       pRec.cancelText   = quitButtonLabel;
00145       pRec.otherText    = nil;
00146       pRec.helpButton   = nil;
00147       pRec.defaultButton = kAlertStdAlertOKButton;
00148       pRec.cancelButton  = kAlertStdAlertCancelButton;
00149       pRec.position      = 0;
00150       
00151       ::StandardAlert(kAlertNoteAlert, str1, str2, &pRec, &outItemHit);
00152       if (outItemHit == kAlertStdAlertCancelButton)
00153         return PR_FALSE;
00154     }
00155     else
00156       return PR_FALSE;
00157   }
00158   
00159   // We also check for CarbonLib version >= 1.4 if OS vers < 10.0
00160   // which is always cause for the app to quit
00161   if (response < 0x00001000)
00162   {
00163     err = ::Gestalt (gestaltCarbonVersion, &response);
00164     if (err || response < 0x00000140)
00165     {
00166       // put up error dialog
00167       ::GetIndString(str1, kNSOSVersErrsStrArrayID, eCarbonLibVersTooOldIndex);
00168       ::GetIndString(str2, kNSOSVersErrsStrArrayID, eCarbonLibVersTooOldExplanationIndex);
00169       if (StrLength(str1) && StrLength(str1))
00170       {
00171         ::StandardAlert(kAlertStopAlert, str1, str2, nil, &outItemHit);
00172       }
00173       return PR_FALSE;
00174     }
00175   }
00176 #endif
00177 
00178   *_retval = PR_TRUE;
00179   return NS_OK;
00180 }
00181 
00182 NS_IMETHODIMP
00183 nsNativeAppSupportMac::ReOpen()
00184 {
00185   if (!mCanShowUI)
00186     return NS_ERROR_FAILURE;
00187 
00188   PRBool haveUncollapsed = PR_FALSE;
00189   PRBool haveOpenWindows = PR_FALSE;
00190   PRBool done = PR_FALSE;
00191   
00192   nsCOMPtr<nsIWindowMediator> 
00193     wm(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
00194   if (!wm)
00195   {
00196     return NS_ERROR_FAILURE;
00197   } 
00198   else
00199   {
00200     nsCOMPtr<nsISimpleEnumerator> windowList;
00201     wm->GetXULWindowEnumerator(nsnull, getter_AddRefs(windowList));
00202     PRBool more;
00203     windowList->HasMoreElements(&more);
00204     while (more)
00205     {
00206       nsCOMPtr<nsISupports> nextWindow = nsnull;
00207       windowList->GetNext(getter_AddRefs(nextWindow));
00208       nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(nextWindow));
00209                 if (!baseWindow)
00210                 {
00211         windowList->HasMoreElements(&more);
00212         continue;
00213       }
00214       else
00215       {
00216         haveOpenWindows = PR_TRUE;
00217       }
00218 
00219       nsCOMPtr<nsIWidget> widget = nsnull;
00220       baseWindow->GetMainWidget(getter_AddRefs(widget));
00221       if (!widget)
00222       {
00223         windowList->HasMoreElements(&more);
00224         continue;
00225       }
00226       WindowRef windowRef = (WindowRef)widget->GetNativeData(NS_NATIVE_DISPLAY);
00227       if (!::IsWindowCollapsed(windowRef))
00228       {
00229         haveUncollapsed = PR_TRUE;
00230         break;  //have un-minimized windows, nothing to do
00231       } 
00232       windowList->HasMoreElements(&more);
00233     } // end while
00234         
00235     if (!haveUncollapsed)
00236     {
00237       //uncollapse the most recenty used window
00238       nsCOMPtr<nsIDOMWindowInternal> mru = nsnull;
00239       wm->GetMostRecentWindow(nsnull, getter_AddRefs(mru));
00240             
00241       if (mru) 
00242       {        
00243         WindowRef mruRef = nil;
00244         GetNativeWindowPointerFromDOMWindow(mru, &mruRef);
00245         if (mruRef)
00246         {
00247           ::CollapseWindow(mruRef, FALSE);
00248           ::SelectWindow(mruRef);
00249           done = PR_TRUE;
00250         }
00251       }
00252       
00253     } // end if have uncollapsed 
00254     
00255     if (!haveOpenWindows && !done)
00256     {
00257       char* argv[] = { nsnull };
00258     
00259       // use an empty command line to make the right kind(s) of window open
00260       nsCOMPtr<nsICommandLineRunner> cmdLine
00261         (do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
00262       NS_ENSURE_TRUE(cmdLine, NS_ERROR_FAILURE);
00263 
00264       nsresult rv;
00265       rv = cmdLine->Init(0, argv, nsnull,
00266                          nsICommandLine::STATE_REMOTE_EXPLICIT);
00267       NS_ENSURE_SUCCESS(rv, rv);
00268 
00269       return cmdLine->Run();
00270     }
00271     
00272   } // got window mediator
00273   return NS_OK;
00274 }
00275 
00276 nsresult
00277 GetNativeWindowPointerFromDOMWindow(nsIDOMWindowInternal *a_window, WindowRef *a_nativeWindow)
00278 {
00279     *a_nativeWindow = nil;
00280     if (!a_window) return NS_ERROR_INVALID_ARG;
00281     
00282     nsCOMPtr<nsIWebNavigation> mruWebNav(do_GetInterface(a_window));
00283     if (mruWebNav)
00284     {
00285       nsCOMPtr<nsIDocShellTreeItem> mruTreeItem(do_QueryInterface(mruWebNav));
00286       nsCOMPtr<nsIDocShellTreeOwner> mruTreeOwner = nsnull;
00287       mruTreeItem->GetTreeOwner(getter_AddRefs(mruTreeOwner));
00288       if(mruTreeOwner)
00289       {
00290         nsCOMPtr<nsIBaseWindow> mruBaseWindow(do_QueryInterface(mruTreeOwner));
00291         if (mruBaseWindow)
00292         {
00293           nsCOMPtr<nsIWidget> mruWidget = nsnull;
00294           mruBaseWindow->GetMainWidget(getter_AddRefs(mruWidget));
00295           if (mruWidget)
00296           {
00297             *a_nativeWindow = (WindowRef)mruWidget->GetNativeData(NS_NATIVE_DISPLAY);
00298           }
00299         }
00300       }
00301     }
00302     return NS_OK;
00303 }
00304 
00305 #pragma mark -
00306 
00307 // Create and return an instance of class nsNativeAppSupportMac.
00308 nsresult NS_CreateNativeAppSupport(nsINativeAppSupport**aResult)
00309 {
00310   *aResult = new nsNativeAppSupportMac;
00311   if (!*aResult) return NS_ERROR_OUT_OF_MEMORY;
00312 
00313   NS_ADDREF( *aResult );
00314   return NS_OK;
00315 }