Back to index

lightning-sunbird  0.9+nobinonly
nsASEApp.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Samir Gehani <sgehani@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039  
00040  
00041 #ifndef _NS_ASEAPP_H_
00042        #include "nsASEApp.h"
00043 #endif
00044 
00045 #include <AppleEvents.h>
00046 #include <Balloons.h>
00047 #include <MacTypes.h>
00048 
00049 #include "nsEventHandler.h"
00050 #include "nsAppleSingleEncoder.h"
00051 #include "nsAppleSingleDecoder.h"
00052 #include "MoreFilesExtras.h"
00053 
00054 Boolean gDone;
00055 
00056 nsASEApp::nsASEApp()
00057 {
00058        InitManagers();
00059        InitAEHandlers();
00060        mWindow = NULL;
00061        SetCompletionStatus(false);
00062        
00063        OSErr err = NavLoad();
00064        if (err!= noErr)
00065               FatalError(navLoadErr);
00066               
00067        MakeMenus();
00068 }
00069 
00070 nsASEApp::~nsASEApp()
00071 {
00072        NavUnload();
00073 }
00074 
00075 void
00076 nsASEApp::InitManagers(void)
00077 {
00078        MaxApplZone();
00079        MoreMasters(); MoreMasters(); MoreMasters();
00080        
00081        InitGraf(&qd.thePort);
00082        InitFonts();                
00083        InitWindows();
00084        InitMenus();
00085        TEInit();            
00086        InitDialogs(NULL);
00087        
00088        InitCursor();
00089        FlushEvents(everyEvent, 0); 
00090 }
00091 
00092 #pragma mark -
00093 #pragma mark *** Apple Event Handlers ***
00094 #pragma mark -
00095 
00096 pascal OSErr
00097 EncodeEvent(AppleEvent *appEvent, AppleEvent *reply, SInt32 handlerRefCon)
00098 {
00099        OSErr  err = noErr;
00100        FSSpec param;
00101        Boolean       result = false, isDir = false;
00102        AEDesc fileDesc;
00103        long   dummy;
00104        
00105        // extract FSSpec from params
00106        err = AEGetParamDesc(appEvent, keyDirectObject, typeFSS, &fileDesc);
00107        if (err != noErr)
00108               goto reply;
00109        BlockMoveData(*fileDesc.dataHandle, &param, sizeof(FSSpec));
00110        
00111        // param check
00112        err = nsASEApp::GotRequiredParams(appEvent);
00113        if (err != noErr)
00114               goto reply;
00115               
00116        FSpGetDirectoryID(&param, &dummy, &isDir);
00117        
00118        // if folder recursively traverse and encode contents
00119        if (isDir)
00120        {
00121               nsAppleSingleEncoder encoder;
00122               err = encoder.EncodeFolder(&param);
00123        }
00124        else
00125        {
00126               // it's a file not a folder so proceed as usual
00127               
00128               // check if given file has res fork (takes care of existence check)
00129               if (nsAppleSingleEncoder::HasResourceFork(&param))
00130               {
00131                      // encode given file
00132                      nsAppleSingleEncoder encoder;
00133                      err = encoder.Encode(&param);
00134               }
00135        }
00136        
00137        // if noErr thus far 
00138        if (err == noErr)
00139        {
00140               // then set result to true
00141               result = true;
00142        }
00143        
00144 reply:
00145        // package reply
00146        AEPutParamPtr(reply, keyDirectObject, typeBoolean, &result, sizeof(result));
00147        
00148        // boolean takes care of failures
00149        return noErr;
00150 }
00151 
00152 pascal OSErr
00153 DecodeEvent(AppleEvent *appEvent, AppleEvent *reply, SInt32 handlerRefCon)
00154 {
00155        OSErr  err = noErr;
00156        FSSpec param, outFile;
00157        Boolean       result = false, isDir = false;
00158        AEDesc fileDesc;
00159        long   dummy;
00160        
00161        // extract FSSpec from params
00162        err = AEGetParamDesc(appEvent, keyDirectObject, typeFSS, &fileDesc);
00163        if (err != noErr)
00164               goto reply;
00165        BlockMoveData(*fileDesc.dataHandle, &param, sizeof(FSSpec));
00166        
00167        // param check
00168        err = nsASEApp::GotRequiredParams(appEvent);
00169        if (err != noErr)
00170               goto reply;
00171                      
00172        FSpGetDirectoryID(&param, &dummy, &isDir);
00173        
00174        // if folder recursively traverse and encode contents
00175        if (isDir)
00176        {
00177               nsAppleSingleDecoder decoder;
00178               err = decoder.DecodeFolder(&param);
00179        }
00180        else
00181        {      
00182               // it's a file not a folder so proceed as usual
00183               
00184               // check if given file is in AS format (takes care of existence check)
00185               if (nsAppleSingleDecoder::IsAppleSingleFile(&param))
00186               {
00187                      // decode given file
00188                      nsAppleSingleDecoder decoder;
00189                      err = decoder.Decode(&param, &outFile);
00190               }
00191        }
00192        
00193        // if noErr thus far 
00194        if (err == noErr)
00195        {
00196               // then set result to true
00197               result = true;
00198        }
00199        
00200 reply:
00201        // package reply
00202        AEPutParamPtr(reply, keyDirectObject, typeBoolean, &result, sizeof(result));
00203        
00204        // boolean takes care of failures
00205        return noErr;
00206 }
00207 
00208 pascal OSErr
00209 QuitEvent(AppleEvent *appEvent, AppleEvent *reply, SInt32 handlerRefCon)
00210 {
00211        OSErr  err = noErr;
00212        
00213        nsASEApp::SetCompletionStatus(true);
00214        
00215        return err;
00216 }
00217 
00218 #pragma mark -
00219 
00220 void
00221 nsASEApp::InitAEHandlers()
00222 {
00223        OSErr                              err = noErr;
00224        
00225        mEncodeUPP = NewAEEventHandlerProc((ProcPtr) EncodeEvent);
00226        err = AEInstallEventHandler(kASEncoderEventClass, kAEEncode,
00227                                                         mEncodeUPP, 0L, false);
00228        if (err != noErr)
00229               ::CautionAlert(aeInitErr, nil);
00230        
00231        mDecodeUPP = NewAEEventHandlerProc((ProcPtr) DecodeEvent);
00232        err = AEInstallEventHandler(kASEncoderEventClass, kAEDecode,
00233                                                         mDecodeUPP, 0L, false);
00234        if (err != noErr)
00235               ::CautionAlert(aeInitErr, nil);
00236               
00237        mQuitUPP = NewAEEventHandlerProc((ProcPtr) QuitEvent);
00238        err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
00239                                                         mQuitUPP, 0L, false);
00240        if (err != noErr)
00241               ::CautionAlert(aeInitErr, nil);
00242 }
00243 OSErr
00244 nsASEApp::GotRequiredParams(AppleEvent *appEvent)
00245 {
00246        OSErr         err = noErr;
00247        DescType      returnedType;
00248        Size          actualSize;
00249 
00250        err = AEGetAttributePtr(appEvent, keyMissedKeywordAttr, typeWildCard,
00251                                                  &returnedType, NULL, 0, &actualSize);
00252 
00253        if (err == errAEDescNotFound)
00254               err = noErr;
00255        else if (err == noErr)
00256               err = errAEParamMissed;
00257               
00258        return err;
00259 }
00260 
00261 void
00262 nsASEApp::MakeMenus()
00263 {
00264     Handle           mbarHdl;
00265        MenuHandle    menuHdl;
00266 
00267        mbarHdl = ::GetNewMBar(rMenuBar);
00268        ::SetMenuBar(mbarHdl);
00269        
00270        if ((menuHdl = ::GetMenuHandle(rMenuApple))!=nil) 
00271        {
00272               ::AppendResMenu(menuHdl, 'DRVR');
00273        }
00274               
00275        if ((menuHdl = GetMenuHandle(rMenuEdit))!=nil)
00276               ::DisableItem(menuHdl, 0);
00277        
00278        ::HMGetHelpMenuHandle(&menuHdl);
00279        ::DisableItem(menuHdl, 0);
00280 
00281        ::DrawMenuBar();
00282 }
00283 
00284 void
00285 nsASEApp::SetCompletionStatus(Boolean aVal)
00286 {
00287        gDone = aVal;
00288 }
00289 
00290 Boolean 
00291 nsASEApp::GetCompletionStatus()
00292 {
00293        return gDone;
00294 }
00295 
00296 
00297 
00298 void
00299 nsASEApp::FatalError(short aErrID)
00300 {
00301        ::StopAlert(aErrID, nil);
00302        SetCompletionStatus(true);
00303 }
00304 
00305 OSErr
00306 nsASEApp::Run()
00307 {
00308        OSErr         err = noErr;
00309        EventRecord evt;
00310        nsEventHandler handler;
00311        
00312        while (!gDone)
00313        {
00314               if (::WaitNextEvent(everyEvent, &evt, 180, NULL))
00315               {
00316                      handler.HandleNextEvent(&evt);
00317               }
00318        }
00319               
00320        return err;
00321 }
00322 
00323 int
00324 main(void)
00325 {
00326        nsASEApp app;
00327        
00328        app.Run();
00329        
00330        return 0;
00331 }
00332