/*
 *  AppleEvents.PI.c
 *  PeekIt
 *
 *  Created by C.K. Haun on Sun Feb 16 2003.
 *  Copyright (c) 2003 Ravenware Software. All rights reserved.
 *
 */
#define __PIAES__
#include "PeekIt.h"

AEInteractAllowed gInteractLevel;

typedef struct newAEinstalls {
    AEEventClass theClass;
    AEEventID theEvent;
    ProcPtr theProc; // why is this here?
}newAEinstalls;
// typedef struct newAEinstalls newAEinstalls ;

/* InitAEStuff checks to see if this machine has AppleEvents and
*   does our setup.
*   If AppleEvents are not found, we alert and exit.
*   This is also the place where all the handlers for AppleEvents we deal
*   with are installed.  This includes the required AppleEvents, and the
*   Edition Manager specific ones used in this app.
*/
/* processOpenPrint handles ODOC and PDOC events.  Both events open a document, one prints it */
OSErr processOpenPrint(AppleEvent *messagein, Boolean printIt)
{
#pragma unused (printIt )
    OSErr myErr;
    AEDesc theDesc;
    FSSpec theFSS,theSpec;
    FileTrackerPtr theFT;
    LoopVar qq;
    AEKeyword ignoredKeyWord;
    DescType ignoredType;
    Size ignoredSize;
    long theCount;
//	extern SFReply reply;

    myErr = AEGetParamDesc(messagein, keyDirectObject, typeAEList, &theDesc);

    if(myErr==noErr){
      myErr=  AECountItems(
                     &theDesc,
                     &theCount);
      if(myErr==noErr){
        for(qq=0;qq<theCount;qq++){
            theFT = NewClearFileTracker();

            myErr = AEGetNthPtr(&theDesc, qq+1, typeFSS, &ignoredKeyWord, &ignoredType, theFT->fsSpec, sizeof(theFSS),&ignoredSize);
            if(myErr == noErr){
// send it elsewhere to be discussed
                theFT->fsSpecIsValid = true;
        myErr= FSpMakeFSRef (
                             theFT->fsSpec,
                             theFT->fsRef
                             );
        if(myErr == noErr){
            theFT->fsRefIsValid = true;
        } else {
// couldn't make an FSRef, but the FSSpec is still valid
            myErr = noErr;
        }
// dont open the file here, moron
//        myErr =  OpenFile(theFT);

        CreateOrShowPIMainWIND( theFT,true,true);
            } else {
// if the getnth failed then the file tracker needs to be killed
                DisposeFileTracker(theFT);
            }
        }}}
        


    return(myErr);
}



/* This is the standard Open Application event.  You'll get this as one of the (if not the ) */
/* first events in your application.  So, we open up a blank document */
 OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
{OSErr myErr=noErr;
#pragma unused (messagein,reply,refIn)
 PeekItWindowOpenFile();
    return(myErr);
}

/* end AEOpenHandler */

/* Open Doc, opens our documents.  Remember, this can happen at application start AND */
/* anytime else.  If your app is up and running and the user goes to the desktop, hilites one */
/* of your files, and double-clicks or selects Open from the finder File menu this event */
/* handler will get called. Which means you don't do any initialization of globals here, or */
/* anything else except open then doc.  */
/* SO-- Do NOT assume that you are at app start time in this */
/* routine, or bad things will surely happen to you. */

 OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
{OSErr myErr=noErr;
#pragma unused (reply, refIn)
    myErr=AEInteractWithUser(0xf,nil,nil);
    processOpenPrint(messagein, false);
    return(myErr);
}

 OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
{                                                           /* no printing handler in yet, so we'll ignore this */
/* the operation is functionally identical to the ODOC event, with the additon */
/* of calling your print routine.  */
#pragma unused (reply,refIn,messagein)

return(errAEEventNotHandled);
}

/* Standard Quit event handler, to handle a Quit event from the Finder, for example.  */
/* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life.  */
 OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
// •••• To Do:  Handle carbon event quit properly
{extern Boolean Quit;
#pragma unused (messagein,reply,  refIn)
//    Quit = true;

    return(noErr);

}





// remove appleevent and version checking
void InitAEStuff(void)
{
// {kCoreEventClass, kAEQuitApplication,       AEQuitHandler }

    newAEinstalls	HandlersToInstall[] = {

    { kCoreEventClass,kAEOpenApplication,AEOpenHandler },
    { kCoreEventClass,kAEOpenDocuments,AEOpenDocHandler },
    { kCoreEventClass,kAEPrintDocuments, AEPrintHandler }

    };

    OSErr aevtErr = noErr;
    register qq;
    for (qq = 0; qq < ((sizeof(HandlersToInstall) / sizeof(AEinstalls))); qq++) {
        aevtErr = AEInstallEventHandler(
                                        HandlersToInstall[qq].theClass,
                                        HandlersToInstall[qq].theEvent,
                                        NewAEEventHandlerUPP(HandlersToInstall[qq].theProc)
                                        /* HandlersToInstall[qq].theProc, */
                                        ,0,false
                                        );
	   }
	   /* now our coercion routines */
    AESetInteractionAllowed(kAEAlwaysInteract);

}

/* end InitAEStuff */









#undef __PIAES__