home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / DropShell 2.0 / DSUserProcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-12  |  11.9 KB  |  389 lines  |  [TEXT/R*ch]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSUserProcs.c
  5. **
  6. **   Description:    Specific AppleEvent handlers used by the DropBox
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **    MTC            Marshall Clow
  16. **    SCS            Stephan Somogyi
  17. **
  18. *******************************************************************************
  19. **                      R E V I S I O N   H I S T O R Y
  20. *******************************************************************************
  21. **
  22. **      Date        Time    Author    Description
  23. **    --------    -----    ------    ---------------------------------------------
  24. **    06/23/94            LDR        Added support for ProcessItem and ProcessFolder handling
  25. **    02/20/94            LDR        Modified Preflight & Postflight to take item count
  26. **    01/25/92            LDR        Removed the use of const on the userDataHandle
  27. **    12/09/91            LDR        Added the new SelectFile userProc
  28. **                                Added the new Install & DisposeUserGlobals procs
  29. **                                Modified PostFlight to only autoquit on odoc, not pdoc
  30. **    11/24/91            LDR        Added the userProcs for pdoc handler
  31. **                                Cleaned up the placement of braces
  32. **                                Added the passing of a userDataHandle
  33. **    10/29/91            SCS        Changes for THINK C 5
  34. **    10/28/91            LDR        Officially renamed DropShell (from QuickShell)
  35. **                                Added a bunch of comments for clarification
  36. **    10/06/91    00:02    MTC        Converted to MPW C
  37. **    04/09/91    00:02    LDR        Added to Projector
  38. **
  39. ******************************************************************************/
  40.  
  41. #include <StandardFile.h>
  42.  
  43. #include "DSGlobals.h"
  44. #include "DSUserProcs.h"
  45.  
  46. // Static Prototypes
  47. static OSErr ProcessFolder(FSSpecPtr myFSSPtr);
  48.  
  49.  
  50. /*
  51.     Uncomment this line if you want each item of a dropped folder processed
  52.     as an individual item
  53. */
  54. // #define qWalkFolders
  55.  
  56.  
  57. /*
  58.     This routine is called during init time.
  59.     
  60.     It allows you to install more AEVT Handlers beyond the standard four
  61. */
  62. #pragma segment Main
  63. pascal void InstallOtherEvents (void) {
  64. }
  65.  
  66.  
  67. /*    
  68.     This routine is called when an OAPP event is received.
  69.     
  70.     Currently, all it does is set the gOApped flag, so you know that
  71.     you were called initally with no docs, and therefore you shouldn't 
  72.     quit when done processing any following odocs.
  73. */
  74. #pragma segment Main
  75. pascal void OpenApp (void) {
  76.     gOApped = true;
  77. }
  78.  
  79.  
  80. /*    
  81.     This routine is called when an QUIT event is received.
  82.     
  83.     We simply set the global done flag so that the main event loop can
  84.     gracefully exit.  We DO NOT call ExitToShell for two reasons:
  85.     1) It is a pretty ugly thing to do, but more importantly
  86.     2) The Apple event manager will get REAL upset!
  87. */
  88. #pragma segment Main
  89. pascal void QuitApp (void) {
  90.     gDone = true;    /*    All Done! */
  91. }
  92.  
  93.  
  94. /*    
  95.     This routine is the first one called when an ODOC or PDOC event is received.
  96.     
  97.     In this routine you would place code used to setup structures, etc. 
  98.     which would be used in a 'for all docs' situation (like "Archive all
  99.     dropped files")
  100.  
  101.     Obviously, the opening boolean tells you whether you should be opening
  102.     or printing these files based on the type of event recieved.
  103.     
  104.     NEW IN 2.0!
  105.     The itemCount parameter is simply the number of items that were dropped on
  106.     the application and that you will be processing.  This gives you the ability
  107.     to do a single preflight for memory allocation needs, rather than doing it
  108.     once for each item as in previous versions.
  109.     
  110.     userDataHandle is a handle that you can create & use to store your own
  111.     data structs.  This dataHandle will be passed around to the other 
  112.     odoc/pdoc routines so that you can get at your data without using
  113.     globals - just like the new StandardFile.  
  114.     
  115.     We also return a boolean to tell the caller if you support this type
  116.     of event.  By default, our dropboxes don't support the pdoc, so when
  117.     opening is FALSE, we return FALSE to let the caller send back the
  118.     proper error code to the AEManager.
  119.  
  120.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  121. */
  122. #pragma segment Main
  123. pascal Boolean PreFlightDocs (Boolean opening, short itemCount, Handle *userDataHandle) {
  124. #pragma unused ( itemCount )
  125. #pragma unused ( userDataHandle )
  126.  
  127.     return opening;        // we support opening, but not printing - see above
  128. }
  129.  
  130.  
  131. /*    
  132.     This routine is called for each file passed in the ODOC event.
  133.     
  134.     In this routine you would place code for processing each file/folder/disk that
  135.     was dropped on top of you.
  136.     
  137.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  138. */
  139. #pragma segment Main
  140. pascal void OpenDoc ( FSSpecPtr myFSSPtr, Boolean opening, Handle userDataHandle ) {
  141. #pragma unused ( myFSSPtr )
  142. #pragma unused ( opening )
  143. #pragma unused ( userDataHandle )
  144.     OSErr    err = noErr;
  145.     
  146.     
  147.     #ifdef qWalkFolders
  148.     /*
  149.         For this case we need to determine if the FSSpec is a file or folder.
  150.         If it's a folder, we then need to process each item in that folder,
  151.         otherwise just process the item.
  152.     */
  153.     if (FSpIsFolder(myFSSPtr))
  154.         err = ProcessFolder(myFSSPtr);
  155.     else
  156.         err = ProcessItem(myFSSPtr);
  157.     #else
  158.     /*
  159.         For this case we just call ProcessItem on the FSSpec above.
  160.     */
  161.     err = ProcessItem(myFSSPtr);
  162.     #endif
  163.     
  164.     // you should probably do something if you get back an error ;)
  165. }
  166.  
  167.  
  168. /*    
  169.     This routine is the last routine called as part of an ODOC event.
  170.     
  171.     In this routine you would place code to process any structures, etc. 
  172.     that you setup in the PreflightDocs routine.
  173.  
  174.     NEW IN 2.0!
  175.     The itemCount parameter was the number of items that you processed.
  176.     It is passed here just in case you need it ;)  
  177.     
  178.     If you created a userDataHandle in the PreFlightDocs routines, this is
  179.     the place to dispose of it since the Shell will NOT do it for you!
  180.     
  181.     You will probably want to remove the #pragma unusued (currently there to fool the compiler!)
  182. */
  183. #pragma segment Main
  184. pascal void PostFlightDocs ( Boolean opening, short itemCount, Handle userDataHandle ) {
  185. #pragma unused ( opening )
  186. #pragma unused ( itemCount )
  187. #pragma unused ( userDataHandle )
  188.  
  189.     if ( (opening) && (!gOApped) )
  190.         gDone = true;    //    close everything up!
  191.  
  192.     /*
  193.         The reason we do not auto quit is based on a recommendation in the
  194.         Apple event Registry which specifically states that you should NOT
  195.         quit on a 'pdoc' as the Finder will send you a 'quit' when it is 
  196.         ready for you to do so.
  197.     */
  198. }
  199.  
  200.  
  201. #ifdef nodef
  202. /*
  203.     This routine gets called for each item (which could be either a file or a folder)
  204.     that the caller wants dropped.  The determining factor is the definition of the 
  205.     qWalkFolder compiler directive.   Either way, the item in question should be
  206.     processed as a single item and not "dissected" into component units (like subfiles
  207.     of a folder!)
  208. */
  209. OSErr ProcessItem(FSSpecPtr myFSSPtr)
  210. {
  211.     OSErr    err = noErr;
  212.     
  213.     // do something here
  214.     
  215.     return(err);
  216. }
  217. #endif
  218.  
  219. /*
  220.     This routine gets called for any folder (or disk) that the caller wants 
  221.     processed as a set of component items, instead of as a single entity.
  222.     The determining factor is the definition of the qWalkFolder compiler directive.
  223. */
  224. static OSErr ProcessFolder(FSSpecPtr myFSSPtr)
  225. {
  226.     OSErr        err = noErr;
  227.     short        index, oldIndex, localIndex;
  228.     FSSpec        localFSSpec, curFSSpec;
  229.     CInfoPBRec    cipb;
  230.     Str255        fName, vFName;
  231.     long        dirID, origDirID;
  232.     Boolean        foundPosition;
  233.  
  234.      // copy the source locally to avoid recursion problems
  235.      BlockMoveData(myFSSPtr, &localFSSpec, sizeof(FSSpec));
  236.      
  237.     //    get the dirID for THIS folder, not it's parent!
  238.     BlockMoveData(localFSSpec.name, fName, 32);
  239.     
  240.     cipb.hFileInfo.ioCompletion    = 0L;
  241.     cipb.hFileInfo.ioNamePtr    = fName;
  242.     cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  243.     cipb.hFileInfo.ioFDirIndex    = 0;    // use the dir & vRefNum;
  244.     cipb.hFileInfo.ioDirID        = localFSSpec.parID;
  245.     err = PBGetCatInfoSync(&cipb);
  246.     
  247.     if (!err) {        
  248.         origDirID = cipb.dirInfo.ioDrDirID; // copy the sucker
  249.         index = 1;
  250.                 
  251.         // index through all contents of this folder
  252.         while (err == noErr) {
  253.             dirID = origDirID;
  254.             localIndex = index;
  255.             fName [0] = 0;
  256.             cipb.hFileInfo.ioCompletion    = 0L;
  257.             cipb.hFileInfo.ioNamePtr    = fName;
  258.             cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  259.             cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  260.             cipb.hFileInfo.ioDirID        = dirID;
  261.             err = PBGetCatInfoSync(&cipb);
  262.  
  263.             if (!err) {
  264.                 BlockMoveData(fName, curFSSpec.name, 32);
  265.                 curFSSpec.vRefNum    = cipb.hFileInfo.ioVRefNum;
  266.                 curFSSpec.parID        = dirID;
  267.             
  268.                 /*    
  269.                     Check to see if this entry is a folder.
  270.                 */
  271.                 if (cipb.hFileInfo.ioFlAttrib & ioDirMask) {
  272.                     err = ProcessFolder(&curFSSpec);
  273.                  } else
  274.                     err = ProcessItem(&curFSSpec);
  275.             
  276.                 /*    If we've had an error, get out! */
  277.                 if (err)    break;
  278.  
  279.                 // dirID = origDirID;    
  280.                 localIndex = index;    
  281.  
  282.                 /*    
  283.                     Now take into account new files being created
  284.                     in the current directory & messing up our index.
  285.                     See Dev.CD Vol. XI:Tools & Apps (Moof!):Misc Utilities:
  286.                     Disinfectant & Source 2.5.1:Sample:Notes:Scan Alg    
  287.                 */
  288.                 vFName [0] = 0;
  289.                 cipb.hFileInfo.ioCompletion    = 0L;
  290.                 cipb.hFileInfo.ioNamePtr    = vFName;
  291.                 cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  292.                 cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  293.                 cipb.hFileInfo.ioDirID        = dirID;
  294.                 err = PBGetCatInfoSync(&cipb);
  295.                 oldIndex = index;
  296.                 if (!err) {
  297.                     /*    If they're equal - same place, go to next */
  298.                     if (EqualString (vFName, fName, false, false))
  299.                         index++;
  300.                 }
  301.                 
  302.                 /*    If we didn't advance, then perhaps a file was created or deleted */
  303.                 if (oldIndex == index) {
  304.                     oldIndex        = index;    /* save off the old */
  305.                     index            = 0;        /* and start at the beginning */
  306.                     err                = noErr;
  307.                     vFName [0]        = 0;
  308.                     foundPosition    = false;
  309.                     
  310.                     while (!foundPosition) {
  311.                         index++;
  312.                         vFName [0] = 0;
  313.                         cipb.hFileInfo.ioCompletion    = 0L;
  314.                         cipb.hFileInfo.ioNamePtr    = vFName;
  315.                         cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  316.                         cipb.hFileInfo.ioFDirIndex    = index;    /* now use a real index */
  317.                         cipb.hFileInfo.ioDirID        = dirID;
  318.                         err = PBGetCatInfoSync(&cipb);
  319.                         
  320.                         if (err == fnfErr) {  // we've just been deleted
  321.                             index = oldIndex;
  322.                             foundPosition = true;
  323.                             err = noErr;    // have to remember to reset this!
  324.                         }
  325.                         
  326.                     /*    found same file & same index position */
  327.                     /*    so try the next item */
  328.                         if ((!foundPosition) && EqualString(fName, vFName, false, false)) {
  329.                             index++;
  330.                             foundPosition = true;
  331.                         }
  332.                     }
  333.                 }
  334.             }
  335.         }
  336.     }
  337.     
  338.     return(err);
  339. }
  340.  
  341. /*
  342.     This routine is called when the user chooses "Select File…" from the
  343.     File Menu.
  344.     
  345.     Currently it simply calls the new StandardGetFile routine to have the
  346.     user select a single file (any type, numTypes = -1) and then calls the
  347.     SendODOCToSelf routine in order to process it.  
  348.             
  349.     The reason we send an odoc to ourselves is two fold: 1) it keeps the code
  350.     cleaner as all file openings go through the same process, and 2) if events
  351.     are ever recordable, the right things happen (this is called Factoring!)
  352.  
  353.     Modification of this routine to only select certain types of files, selection
  354.     of multiple files, and/or handling of folder & disk selection is left 
  355.     as an exercise to the reader.
  356. */
  357. pascal void SelectFile (void)
  358. {
  359.     StandardFileReply    stdReply;
  360.     SFTypeList            theTypeList;
  361.  
  362.     StandardGetFile(NULL, -1, theTypeList, &stdReply);
  363.     if (stdReply.sfGood)    // user did not cancel
  364.         SendODOCToSelf(&stdReply.sfFile);    // so send me an event!
  365. }
  366.  
  367. /*
  368.     This routine is called during the program's initialization and gives you
  369.     a chance to allocate or initialize any of your own globals that your
  370.     dropbox needs.
  371.     
  372.     You return a boolean value which determines if you were successful.
  373.     Returning false will cause DropShell to exit immediately.
  374. */
  375. pascal Boolean InitUserGlobals(void)
  376. {
  377.     return(true);    // nothing to do, it we must be successful!
  378. }
  379.  
  380. /*
  381.     This routine is called during the program's cleanup and gives you
  382.     a chance to deallocate any of your own globals that you allocated 
  383.     in the above routine.
  384. */
  385. pascal void DisposeUserGlobals(void)
  386. {
  387.     // nothing to do for our sample dropbox
  388. }
  389.