home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Tracker Client Folder / CMyApplication.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-10  |  8.0 KB  |  334 lines  |  [TEXT/KAHL]

  1. /* CMyApplication.c */
  2.  
  3. #include "CMyApplication.h"
  4. #include "MenuController.h"
  5. #include "CMyDocument.h"
  6. #include "CSack.h"
  7. #include "Memory.h"
  8. #include "Alert.h"
  9. #include "StandardFile.h"
  10. #include "File.h"
  11.  
  12.  
  13. #define AppleMenuID (256)
  14. #define FileMenuID (257)
  15. #define EditMenuID (258)
  16. #define StuffMenuID (259)
  17.  
  18. #define NotEnoughMemoryID (130L*65536L + 2)
  19.  
  20. #define CantOpenMultipleSongListsID (5120L*65536L + 3)
  21.  
  22. #define CantFindServer (4096L*65536L + 1)
  23. #define CantSearchForServer (4096L*65536L + 2)
  24. #define CantLaunchServer (4096L*65536L + 4)
  25.  
  26. #define RemoteFatalErrorTemplate (4097L*65536L)
  27.  
  28.  
  29. /* */            CMyApplication::CMyApplication()
  30.     {
  31.         FSSpec            PrefsFile;
  32.         short                VRefNum;
  33.         long                DirID;
  34.         short                Error;
  35.         short                LocalRefNum;
  36.         long                SizeOfAlias;
  37.         Handle            Temp;
  38.  
  39.         Error = AEInstallEventHandler(kCoreEventClass,
  40.             kAEApplicationDied,&MyHandleOBIT,0,False);
  41.  
  42.         Error = AEInstallEventHandler(ControlEventClass,
  43.             ErrorEvent,&MyHandleERROR,0,False);
  44.  
  45.     }
  46.  
  47.  
  48. void            CMyApplication::InitMenuBar(void)
  49.     {
  50.         APRINT(("+CMyApplication::InitMenuBar"));
  51.         PostMenuToBar(AppleMenuID);
  52.         PostMenuToBar(FileMenuID);
  53.         PostMenuToBar(EditMenuID);
  54.         PostMenuToBar(StuffMenuID);
  55.         APRINT(("-CMyApplication::InitMenuBar"));
  56.     }
  57.  
  58.  
  59. void            CMyApplication::DispatchOpenDocument(FSSpec* TheFSSpec)
  60.     {
  61.         CMyDocument*    ADoc;
  62.         FInfo                    FileInfo;
  63.  
  64.         /* identifying document */
  65.         FSpGetFInfo(TheFSSpec,&FileInfo);
  66.         if (FileInfo.fdType == FILETYPE1)
  67.             {
  68.                 /* it's one of our preferences files */
  69.                 if (ListOfDocuments->NumElements() == 0)
  70.                     {
  71.                         /* make a new one */
  72.                         if (!FirstMemCacheValid())
  73.                             {
  74.                                 AlertError(NotEnoughMemoryID,NIL);
  75.                                 return;
  76.                             }
  77.                         ADoc = new CMyDocument;  /* create the document */
  78.                         ADoc->DoOpenFile(TheFSSpec);  /* make it link to a file */
  79.                     }
  80.                  else
  81.                     {
  82.                         AlertError(CantOpenMultipleSongListsID,NIL);
  83.                     }
  84.             }
  85.          else
  86.             {
  87.                 /* assume it is a tracker file */
  88.                 if (ListOfDocuments->NumElements() == 0)
  89.                     {
  90.                         /* no preference file, create a new one */
  91.                         DoMenuNew();
  92.                     }
  93.                 /* dispatch it to the document */
  94.                 ListOfDocuments->ResetScan();
  95.                 ListOfDocuments->GetNext(&ADoc);
  96.                 ADoc->AddSong(TheFSSpec);
  97.             }
  98.     }
  99.  
  100.  
  101. /* open a file using the standard file things */
  102. void                CMyApplication::DoMenuOpen(void)
  103.     {
  104.         FSSpec            FileInfo;
  105.  
  106.         if (ListOfDocuments->NumElements() == 0)
  107.             {
  108.                 inherited::DoMenuOpen();
  109.             }
  110.          else
  111.             {
  112.                 if (!FirstMemCacheValid())
  113.                     {
  114.                         AlertError(NotEnoughMemoryID,NIL);
  115.                         return;
  116.                     }
  117.                 if (FGetFile(&FileInfo,NIL,NIL,-1))
  118.                     {
  119.                         DispatchOpenDocument(&FileInfo);
  120.                     }
  121.             }
  122.     }
  123.  
  124.  
  125. void                CMyApplication::DoMenuNew(void)
  126.     {
  127.         if (ListOfDocuments->NumElements() == 0)
  128.             {
  129.                 inherited::DoMenuNew();
  130.             }
  131.     }
  132.  
  133.  
  134. void                CMyApplication::EnableMenuItems(void)
  135.     {
  136.         if (ListOfDocuments->NumElements() == 0)
  137.             {
  138.                 MyEnableItem(mFileNew);
  139.             }
  140.         MyEnableItem(mFileOpen);
  141.         MyEnableItem(mAppleAbout);
  142.         MyEnableItem(mFileQuit);
  143.     }
  144.  
  145.  
  146. void                CMyApplication::LaunchTracker(OSType TrackerCreator)
  147.     {
  148.         FSSpec                                Tracker;
  149.         LaunchParamBlockRec        Launcher;
  150.         DTPBRec                                DesktopParamBlock;
  151.         PString                                NameOut;
  152.         short                                    Error;
  153.         short                                    DesktopRefNum;
  154.         short                                    CurResFileValue;
  155.  
  156.         StackSizeTest();
  157.         /* opening database */
  158.         NameOut[0] = 0;
  159.         DesktopParamBlock.ioNamePtr = NameOut;
  160.         CurResFileValue = CurResFile();
  161.         Error = GetVRefNum(CurResFileValue,&DesktopParamBlock.ioVRefNum);
  162.         PBDTGetPath(&DesktopParamBlock);
  163.         Error = DesktopParamBlock.ioResult;
  164.         if (Error != noErr)
  165.             {
  166.                 CMyDocument*        Document;
  167.  
  168.                 ListOfDocuments->ResetScan();
  169.                 ListOfDocuments->GetNext(&Document);
  170.                 /* must call stop first otherwise a nasty race condition develops */
  171.                 /* because the document would still get idle events & try to call */
  172.                 /* this procedure again */
  173.                 Document->DoStop();
  174.                 AlertError(CantSearchForServer,NIL);
  175.                 return;
  176.             }
  177.         DesktopRefNum = DesktopParamBlock.ioDTRefNum;
  178.  
  179.         /* finding tracker program */
  180.         DesktopParamBlock.ioCompletion = NIL;
  181.         DesktopParamBlock.ioNamePtr = NameOut;
  182.         DesktopParamBlock.ioDTRefNum = DesktopRefNum;
  183.         DesktopParamBlock.ioIndex = 0;
  184.         DesktopParamBlock.ioFileCreator = TrackerCreator;
  185.         PBDTGetAPPLSync(&DesktopParamBlock);
  186.         Error = DesktopParamBlock.ioResult;
  187.         if (Error != noErr)
  188.             {
  189.                 CMyDocument*        Document;
  190.  
  191.                 ListOfDocuments->ResetScan();
  192.                 ListOfDocuments->GetNext(&Document);
  193.                 Document->DoStop();
  194.                 AlertError(CantFindServer,NIL);
  195.                 return;
  196.             }
  197.         FMakeFSSpec(0,DesktopParamBlock.ioAPPLParID,NameOut,&Tracker);
  198.  
  199.         Launcher.launchBlockID = extendedBlock;
  200.         Launcher.launchEPBLength = extendedBlockLen;
  201.         Launcher.launchFileFlags = 0;
  202.         Launcher.launchControlFlags = launchNoFileFlags | launchContinue
  203.             | launchUseMinimum | launchDontSwitch;
  204.         Launcher.launchAppSpec = &Tracker;
  205.         Launcher.launchAppParameters = NIL;
  206.         Error = LaunchApplication(&Launcher);
  207.         if (Error != noErr)
  208.             {
  209.                 CMyDocument*        Document;
  210.  
  211.                 ListOfDocuments->ResetScan();
  212.                 ListOfDocuments->GetNext(&Document);
  213.                 Document->DoStop();
  214.                 AlertError(CantLaunchServer,NIL);
  215.                 return;
  216.             }
  217.          else
  218.             {
  219.                 CMyDocument*        Document;
  220.  
  221.                 ListOfDocuments->ResetScan();
  222.                 ListOfDocuments->GetNext(&Document);
  223.                 Document->PlayerLaunchedNotification(Launcher.launchProcessSN);
  224.             }
  225.     }
  226.  
  227.  
  228. void            CMyApplication::SendMessage(ProcessSerialNumber PlayerSN, AppleEvent* Message)
  229.     {
  230.         AppleEvent            Reply;
  231.         OSErr                        Error;
  232.  
  233.         StackSizeTest();
  234.         Error = AESend(Message,&Reply,kAENoReply,kAENormalPriority,kNoTimeOut,NIL,NIL);
  235.     }
  236.  
  237.  
  238. void            CMyApplication::KillPlayer(ProcessSerialNumber PlayerSN)
  239.     {
  240.         AEAddressDesc            AddressDescriptor;
  241.         OSErr                            Error;
  242.         AppleEvent                Event;
  243.  
  244.         Error = AECreateDesc(typeProcessSerialNumber,(void*)&PlayerSN,
  245.             sizeof(ProcessSerialNumber),&AddressDescriptor);
  246.         Error = AECreateAppleEvent(kCoreEventClass,kAEQuitApplication,&AddressDescriptor,
  247.             kAutoGenerateReturnID,kAnyTransactionID,&Event);
  248.         SendMessage(PlayerSN,&Event);
  249.         Error = AEDisposeDesc(&AddressDescriptor);
  250.         Error = AEDisposeDesc(&Event);
  251.     }
  252.  
  253.  
  254. static    pascal    OSErr    CMyApplication::MyHandleOBIT(AppleEvent* theAppleEvent,
  255.                                                 AppleEvent* reply, long handlerRefcon)
  256.     {
  257.         long                                    RemoteError;
  258.         DescType                            Stupid;
  259.         OSErr                                    Error;
  260.         long                                    ActualStupidSize;
  261.         ProcessSerialNumber        PSN;
  262.  
  263.         Error = AEGetParamPtr(theAppleEvent,keyErrorNumber,typeLongInteger,
  264.             &Stupid,(void*)&RemoteError,sizeof(long),&ActualStupidSize);
  265.  
  266.         Error = AEGetParamPtr(theAppleEvent,keyProcessSerialNumber,typeProcessSerialNumber,
  267.             &Stupid,(void*)&PSN,sizeof(ProcessSerialNumber),&ActualStupidSize);
  268.  
  269.         Error = MyGotRequiredParams(theAppleEvent);
  270.  
  271.         if (Error != noErr)
  272.             {
  273.                 return Error;
  274.             }
  275.          else
  276.             {
  277.                 if (Application->ListOfDocuments->NumElements() != 0)
  278.                     {
  279.                         CMyDocument*            Document;
  280.  
  281.                         Application->ListOfDocuments->ResetScan();
  282.                         Application->ListOfDocuments->GetNext(&Document);
  283.                         Document->PlayerDiedNotification();
  284.                     }
  285.                 return noErr;
  286.             }
  287.     }
  288.  
  289.  
  290. static    pascal    OSErr    CMyApplication::MyHandleERROR(AppleEvent* theAppleEvent,
  291.                                                 AppleEvent* reply, long handlerRefcon)
  292.     {
  293.         short                                    RemoteError;
  294.         DescType                            Stupid;
  295.         OSErr                                    Error;
  296.         long                                    ActualStupidSize;
  297.  
  298.         Error = AEGetParamPtr(theAppleEvent,keyErrorIDNum,typeShortInteger,
  299.             &Stupid,(void*)&RemoteError,sizeof(short),&ActualStupidSize);
  300.  
  301.         Error = MyGotRequiredParams(theAppleEvent);
  302.  
  303.         if (Error != noErr)
  304.             {
  305.                 return Error;
  306.             }
  307.          else
  308.             {
  309.                 CMyDocument*        Document;
  310.  
  311.                 Application->ListOfDocuments->ResetScan();
  312.                 if (Application->ListOfDocuments->GetNext(&Document))
  313.                     {
  314.                         Document->DoStop();
  315.                     }
  316.                 switch (RemoteError)
  317.                     {
  318.                         case FatalErrorOutOfMemory:
  319.                         case FatalErrorInternalError:
  320.                         case FatalErrorCantOpenCompressedFiles:
  321.                         case FatalErrorCouldntOpenFile:
  322.                         case FatalErrorCouldntCloseFile:
  323.                         case FatalErrorNotASong:
  324.                         case FatalError68020NeededID:
  325.                             AlertError(RemoteFatalErrorTemplate + RemoteError,NIL);
  326.                             break;
  327.                         default:
  328.                             AlertError(RemoteFatalErrorTemplate + FatalErrorUnknown,NIL);
  329.                             break;
  330.                     }
  331.                 return noErr;
  332.             }
  333.     }
  334.