Back to index

lightning-sunbird  0.9+nobinonly
nsAESpyglassSuiteHandler.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  *   Simon Fraser <sfraser@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsMemory.h"
00040 
00041 #include "nsAESpyglassSuiteHandler.h"
00042 #include "nsCommandLineServiceMac.h"
00043 #include "nsDocLoadObserver.h"
00044 
00045 /*----------------------------------------------------------------------------
00046        AESpyglassSuiteHandler 
00047        
00048 ----------------------------------------------------------------------------*/
00049 AESpyglassSuiteHandler::AESpyglassSuiteHandler()
00050 :   mDocObserver(nsnull)
00051 {
00052 }
00053 
00054 /*----------------------------------------------------------------------------
00055        ~AESpyglassSuiteHandler 
00056        
00057 ----------------------------------------------------------------------------*/
00058 AESpyglassSuiteHandler::~AESpyglassSuiteHandler()
00059 {
00060     NS_IF_RELEASE(mDocObserver);
00061 }
00062 
00063 
00064 /*----------------------------------------------------------------------------
00065        HandleSpyglassSuiteEvent 
00066        
00067 ----------------------------------------------------------------------------*/
00068 void AESpyglassSuiteHandler::HandleSpyglassSuiteEvent(const AppleEvent *appleEvent, AppleEvent *reply)
00069 {
00070        OSErr         err = noErr;
00071        
00072        AEEventID            eventID;
00073        OSType        typeCode;
00074        Size                 actualSize    = 0L;
00075        
00076        // Get the event ID
00077        err = AEGetAttributePtr(appleEvent,       keyEventIDAttr, 
00078                                                                typeType, 
00079                                                                &typeCode, 
00080                                                                (Ptr)&eventID, 
00081                                                                sizeof(eventID), 
00082                                                                &actualSize);
00083        ThrowIfOSErr(err);
00084        
00085        try
00086        {
00087               switch (eventID)
00088               {
00089                      case kOpenURLEvent:
00090                             HandleOpenURLEvent(appleEvent, reply);
00091                             break;
00092                      
00093                      case kRegisterURLEchoEvent:
00094                             HandleRegisterURLEchoEvent(appleEvent, reply);
00095                             break;
00096                             
00097                      case kUnregisterURLEchoEvent:
00098                             HandleUnregisterURLEchoEvent(appleEvent, reply);
00099                             break;
00100                      
00101                      default:
00102                             ThrowOSErr(errAEEventNotHandled);
00103                             break;
00104               }
00105        }
00106        catch (OSErr catchErr)
00107        {
00108               PutReplyErrorNumber(reply, catchErr);
00109               throw;
00110        }
00111        catch ( ... )
00112        {
00113               PutReplyErrorNumber(reply, paramErr);
00114               throw;
00115        }
00116 }
00117 
00118 
00119 /*----------------------------------------------------------------------------
00120        HandleOpenURLEvent 
00121        
00122 ----------------------------------------------------------------------------*/
00123 void AESpyglassSuiteHandler::HandleOpenURLEvent(const AppleEvent *appleEvent, AppleEvent *reply)
00124 {
00125        StAEDesc             directParameter;
00126        FSSpec        saveToFile;
00127        Boolean              gotSaveToFile = false;
00128        SInt32        targetWindowID = -1;
00129        SInt32        openFlags = 0;
00130        OSErr         err;
00131        
00132        // extract the direct parameter (an object specifier)
00133        err = ::AEGetKeyDesc(appleEvent, keyDirectObject, typeWildCard, &directParameter);
00134        ThrowIfOSErr(err);
00135 
00136        // look for the save to file param
00137        StAEDesc             targetFileDesc;
00138        err = ::AEGetKeyDesc(appleEvent, kParamSaveToFileDest, typeFSS, &targetFileDesc);
00139        if (err != errAEDescNotFound)
00140        {
00141               targetFileDesc.GetFileSpec(saveToFile);
00142               gotSaveToFile = true;
00143        }
00144        
00145 #if 0
00146        // look for the target window param
00147        StAEDesc             openInWindowDesc;
00148        err = ::AEGetKeyDesc(appleEvent, kParamOpenInWindow, typeLongInteger, &openInWindowDesc);
00149        if (err != errAEDescNotFound)
00150               targetWindowID = openInWindowDesc.GetLong();
00151 
00152        // look for the open flags
00153        StAEDesc             openFlagsDesc;
00154        err = ::AEGetKeyDesc(appleEvent, kParamOpenFlags, typeLongInteger, &openFlagsDesc);
00155        if (err != errAEDescNotFound)
00156               openFlags = openFlagsDesc.GetLong();
00157 
00158         // do something with targetWindowID and openFlags...
00159 #endif
00160        
00161        long          dataSize = directParameter.GetDataSize();
00162        char*  urlString = (char *)nsMemory::Alloc(dataSize + 1);
00163        ThrowIfNil(urlString);
00164        
00165        directParameter.GetCString(urlString, dataSize + 1);
00166        
00167        nsMacCommandLine&  cmdLine = nsMacCommandLine::GetMacCommandLine();
00168        cmdLine.DispatchURLToNewBrowser(urlString);
00169        
00170        nsMemory::Free(urlString);  
00171 }
00172 
00173 
00174 
00175 /*----------------------------------------------------------------------------
00176        HandleRegisterURLEchoEvent 
00177        
00178 ----------------------------------------------------------------------------*/
00179 void AESpyglassSuiteHandler::HandleRegisterURLEchoEvent(const AppleEvent *appleEvent, AppleEvent *reply)
00180 {
00181        // extract the direct parameter (the requester's signature)
00182        StAEDesc directParameter;
00183        OSErr err = ::AEGetKeyDesc(appleEvent, keyDirectObject, typeType, &directParameter);
00184        ThrowIfOSErr(err);
00185 
00186        if (typeType == directParameter.descriptorType)
00187        {
00188            if (mDocObserver == nsnull) {
00189               mDocObserver = new nsDocLoadObserver;
00190               ThrowIfNil(mDocObserver);
00191               NS_ADDREF(mDocObserver);        // our owning ref
00192        }
00193        OSType requester;
00194        if (AEGetDescData(&directParameter, &requester, sizeof(requester)) == noErr)
00195               mDocObserver->AddEchoRequester(requester);
00196        }
00197 }
00198 
00199 /*----------------------------------------------------------------------------
00200        HandleUnregisterURLEchoEvent 
00201        
00202 ----------------------------------------------------------------------------*/
00203 void AESpyglassSuiteHandler::HandleUnregisterURLEchoEvent(const AppleEvent *appleEvent, AppleEvent *reply)
00204 {
00205        // extract the direct parameter (the requester's signature)
00206        StAEDesc directParameter;
00207        OSErr err = ::AEGetKeyDesc(appleEvent, keyDirectObject, typeType, &directParameter);
00208        ThrowIfOSErr(err);
00209 
00210        if (typeType == directParameter.descriptorType)
00211        {
00212            if (mDocObserver)
00213               mDocObserver->RemoveEchoRequester(**(OSType**)directParameter.dataHandle);
00214        }
00215 }