home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / files / filesharingon / filesharingon.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  10.4 KB  |  506 lines

  1. /*
  2.     File:        FileSharingOn.c
  3.     
  4.     Description:This snippet demonstrates how to determine whether or not File Sharing is on.
  5.  
  6.     Author:        VMC
  7.     
  8.     Copyright:     Copyright: © 1996-1999 by Apple Computer, Inc.
  9.                 all rights reserved.
  10.     
  11.     Disclaimer:    You may incorporate this sample code into your applications without
  12.                 restriction, though the sample code has been provided "AS IS" and the
  13.                 responsibility for its operation is 100% yours.  However, what you are
  14.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  15.                 after having made changes. If you're going to re-distribute the source,
  16.                 we require that you make it clear in the source that the code was
  17.                 descended from Apple Sample Code, but that you've made changes.
  18.     
  19.     Change History (most recent first):
  20.                 6/25/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  21.  
  22. */
  23. #include <StandardFile.h>
  24. #include "FileSharingOn.h"
  25.  
  26. /*****  globals *****/
  27. Boolean                    gQuitTheApp = false;        /* true = quit program */
  28. /*****    main() *****/
  29.  
  30. void main()
  31. {
  32.     short            sleepTime  =  60;
  33.     EventRecord        myEvent;
  34.  
  35.     
  36.     initApp();                                /* call initialization routine */
  37.     setUpMenus();
  38.     do
  39.     {
  40.         if (WaitNextEvent(everyEvent, &myEvent, sleepTime, NIL))
  41.             switch (myEvent.what)
  42.             {
  43.                 case mouseDown:
  44.                     doMousedown(&myEvent);
  45.                     break;
  46.                     
  47.                 case keyDown:
  48.                 case autoKey:
  49.                     doKey(&myEvent);
  50.                     break;
  51.  
  52.                 case kHighLevelEvent:
  53.                     doHighLevel(&myEvent);
  54.                     break;
  55.  
  56.                 case activateEvt:
  57.                     break;
  58.                 
  59.                 case updateEvt:
  60.                     doUpdate(&myEvent);
  61.                     break; 
  62.                 
  63.                 case diskEvt:
  64.                     break;
  65.             
  66.                 case osEvt:                /*  I don't really care yet. */
  67.                     break;
  68.             }
  69.     }  while (!gQuitTheApp);        
  70.     
  71. }                 /* end of main */
  72.  
  73.  
  74. /*****    initApp() *****/
  75.  
  76. void initApp()
  77. {
  78.     
  79.     /* Memory specific initializations. */
  80.     
  81.     MaxApplZone();                /* grow the heap to its maximum size */
  82.     MoreMasters();                   /* create more master pointers         */
  83.     MoreMasters();
  84.     MoreMasters();
  85.     MoreMasters();
  86.     MoreMasters();
  87.  
  88.     /* Initializing the ROM Managers */
  89.     
  90.     InitGraf((Ptr) &qd.thePort);
  91.     InitFonts();
  92.     InitWindows();
  93.     InitMenus();
  94.     FlushEvents(everyEvent,0);
  95.     TEInit();
  96.     InitDialogs(NIL);
  97.     InitCursor();
  98.     
  99.     if (!checkGestaltFeatures())
  100.         ExitToShell();
  101.     
  102.     /* Install the required Apple Event Handlers */
  103.     if (!installAEHandlers())
  104.         ExitToShell();
  105. }                /* end of initApp() */
  106.  
  107.  
  108.  
  109. /*****  checkGestaltFeatures() *****/
  110. Boolean checkGestaltFeatures()
  111. {
  112.     long        gestaltFeature;
  113.     OSErr        myErr;
  114.  
  115.     myErr = Gestalt(gestaltSystemVersion, &gestaltFeature);     /* which SysVersion present? */
  116.     if (myErr == noErr)
  117.     {
  118.         gestaltFeature = (gestaltFeature >> 8) & 0xf; 
  119.                                         /* shift result over & mask out major version number */
  120.         if (gestaltFeature < 7)         /* This is a System 7+ shell.  We quit otherwise. */
  121.         {
  122.             StopAlert(BADSYSTEMID, nil);
  123.             return(false);
  124.         }
  125.     }
  126.     
  127.     if (myErr == noErr)
  128.     {
  129.         myErr = Gestalt(gestaltQuickdrawVersion, &gestaltFeature);
  130.                                                     /* we want color QD cuz we're spoiled */
  131.         if (myErr == noErr)
  132.         {
  133.             if(gestaltFeature < gestalt32BitQD)
  134.             {
  135.                 StopAlert(BADQUICKDRAWID, nil);
  136.                 return(false);
  137.             }
  138.         }
  139.     }
  140.     
  141.     
  142.     
  143.     if (myErr == noErr)
  144.     {
  145.         myErr = Gestalt(gestaltAppleEventsAttr, &gestaltFeature);
  146.         if (myErr == noErr)
  147.         {
  148.             if (!(gestaltFeature & 0xf))
  149.             {
  150.                 StopAlert(NOAPPLEEVENTS, nil);
  151.                 return(false);
  152.             }
  153.         }
  154.     }
  155.     if (!myErr)        
  156.         return(true);            /* if there was an error we cannot continue */
  157.     else
  158.     {
  159.         return(false);            /* we made it through without a hitch */
  160.     }
  161. }
  162.  
  163. /*****    installAEHandlers *****/
  164. Boolean installAEHandlers()
  165. {
  166.     OSErr        myErr;
  167.     
  168.     myErr = AEInstallEventHandler ( kCoreEventClass, 
  169.                     kAEOpenApplication, NewAEEventHandlerProc(DoOpenAppAE), 0L, false );
  170.     if (myErr == noErr)
  171.         myErr = AEInstallEventHandler ( kCoreEventClass,
  172.                     kAEOpenDocuments, NewAEEventHandlerProc(DoOpenDocAE), 0L, false );
  173.     if (myErr == noErr)
  174.         myErr = AEInstallEventHandler ( kCoreEventClass,
  175.                     kAEPrintDocuments, NewAEEventHandlerProc(DoPrintDocAE), 0L, false );
  176.     if (myErr == noErr)
  177.         myErr = AEInstallEventHandler ( kCoreEventClass,
  178.                     kAEQuitApplication, NewAEEventHandlerProc(DoQuitAppAE), 0L, false );
  179.     if (myErr)
  180.         return(false);
  181.     else
  182.         return(true);
  183. }
  184.  
  185. /*****    setUpMenus() 
  186.             This will put up our menus.  Be sure to read the comments by the
  187.             AppendMenu() and SetItem() calls to see how different options
  188.             behave.        
  189. *****/
  190. void setUpMenus()
  191. {
  192.     short            n;
  193.     MenuHandle        theMenu;
  194.     
  195.     for (n = 0; n < MAXMENUS; n++)
  196.     {
  197.         theMenu = GetMenu(FIRSTMENUID + n);
  198.         if (theMenu)                  
  199.             InsertMenu(theMenu, 0);
  200.     }
  201.  
  202.     theMenu = GetMenuHandle(APPLEMENU);
  203.     if (theMenu)
  204.         AppendResMenu(theMenu, 'DRVR');
  205.     
  206.     DrawMenuBar();
  207.     return;
  208. }                    /* end of setUpMenus()  */
  209.  
  210. /*****    setUpWindow() *****/
  211.  
  212. void setUpWindow( void )
  213. {
  214.     WindowPtr    myWind;
  215.  
  216.     myWind = GetNewCWindow(WINDOWID, nil, PUTINFRONT);
  217.     if (myWind != nil)
  218.     {
  219.         ShowWindow(myWind);
  220.     }
  221. }
  222.  
  223.  
  224. /*****    doMousedown()
  225.             We figure out where the user has clicked the mouse.  If the user
  226.             clicks anywhere but the menu bar, we beep.  Otherwise, we figure
  227.             out which menu they have clicked and dispatch.
  228. *****/
  229. void doMousedown(EventRecord *eventRec)
  230. {
  231.     short        windPart;
  232.     WindowPtr    myWind;
  233.     long        menuResult;
  234.     
  235.     windPart = FindWindow(eventRec->where, &myWind);
  236.     
  237.     switch(windPart)
  238.     {
  239.         case inContent:
  240.             SelectWindow(myWind);
  241.             break;
  242.  
  243.         case inDrag:
  244.             DragWindow( myWind, eventRec->where, &qd.screenBits.bounds );
  245.             break;
  246.  
  247.         case inMenuBar:
  248.             menuResult = MenuSelect(eventRec->where);
  249.             dispatch(menuResult);
  250.             break;
  251.  
  252.         case inSysWindow:
  253.             SystemClick( eventRec, myWind );
  254.             break;
  255.             
  256.         case inGoAway:
  257.             gQuitTheApp = true;
  258.             break;
  259.             
  260.         default:
  261.             break;
  262.     }
  263.     return;
  264. }                 /* end of doMousedown */
  265.  
  266.  
  267. /*****     doKey()
  268.             We ignore keys pressed unless they are accompanied by a 
  269.             command key.  Then we dispatch.
  270. *****/
  271. void doKey(EventRecord *eventRec)
  272. {
  273.     char    keyPressed;
  274.     long    menuResult;
  275.     
  276.     keyPressed = (char) (eventRec->message & charCodeMask);
  277.     
  278.     if((eventRec->modifiers & cmdKey) != 0)
  279.     {
  280.         menuResult = MenuKey(keyPressed);
  281.         dispatch(menuResult);    
  282.     }
  283.     return;
  284. }                 /* end of do_key */
  285.  
  286.  
  287. /*****    doHighLevel() *****/
  288. void doHighLevel(EventRecord *eventRec)
  289. {
  290.     OSErr myErr;
  291.     myErr = AEProcessAppleEvent(eventRec);
  292. }
  293.  
  294.  
  295. pascal OSErr    DoOpenAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  296. {
  297.     #pragma unused(theAppleEvent, reply, refCon)
  298.     
  299.     setUpWindow();
  300.     return(noErr);
  301. }
  302. pascal OSErr    DoOpenDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  303. {
  304.     #pragma unused(theAppleEvent, reply, refCon)
  305.     
  306.     return(noErr);
  307. }
  308. pascal OSErr    DoPrintDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  309. {
  310.     #pragma unused(theAppleEvent, reply, refCon)
  311.     
  312.     return(noErr);
  313. }
  314. pascal OSErr    DoQuitAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  315. {
  316.     #pragma unused(theAppleEvent, reply, refCon)
  317.     
  318.     gQuitTheApp = true;
  319.     return(noErr);
  320. }
  321.  
  322.  
  323. /***** doUpdate() *****/
  324. void    doUpdate(EventRecord *eventRec)
  325. {
  326.     GrafPtr        savedPort;
  327.     WindowPtr    theWindow;
  328.     theWindow = (WindowPtr) eventRec->message;
  329.     
  330.     GetPort(&savedPort);
  331.     SetPort((GrafPtr) theWindow);
  332.     BeginUpdate(theWindow);
  333.     EndUpdate(theWindow);
  334.     SetPort(savedPort);
  335. }
  336.  
  337.  
  338. /*****     dispatch()
  339.             We determine which menu the user has chosen (either with mouse
  340.             or with command keys) and jump to the routine that handles
  341.             that menu's commands.
  342. *****/
  343. void dispatch(long menuResult)
  344. {
  345.     short        theMenu;            /* menu selected */
  346.     short        theItem;            /* item selected */
  347.     
  348.     theMenu = HiWord (menuResult);        /* menuID selected */
  349.     theItem = LoWord (menuResult);        /* item# selected */
  350.     
  351.     switch (theMenu)
  352.     {
  353.         case APPLEMENU:
  354.             doAppleCmds(theItem);
  355.             break;
  356.  
  357.         case FILEMENU:
  358.             doFileCmds(theItem);
  359.             break;
  360.             
  361.         case EDITMENU:
  362.             break;
  363.  
  364.         case TESTMENU:
  365.             doTestCmds(theItem);
  366.             break;
  367.     }
  368.     HiliteMenu(0);
  369. }                /* end of dispatch */
  370.  
  371.  
  372.  
  373. /*****     doAppleCmds() 
  374.             When the user chooses the "About MyMenuText" item, we display the
  375.             About box.  If the user chooses a DA, we open the DA.
  376. *****/
  377. void doAppleCmds(short theItem)
  378. {    
  379.     MenuHandle        myMenu;
  380.     Str255            name;
  381.     short            dummy;
  382.     
  383.     if(theItem == appleABOUT)
  384.     {
  385.         Alert(ABOUTID, (ModalFilterUPP) NIL);
  386.     }
  387.     else
  388.     {
  389.         myMenu = GetMenuHandle (APPLEMENU);
  390.         if (myMenu)
  391.         {
  392.             GetMenuItemText(myMenu, theItem, name);
  393.             dummy = OpenDeskAcc(name);
  394.         }
  395.     }
  396.     return;
  397. }                 /* end of doAppleCmds */
  398.  
  399.  
  400.  
  401. /*****    doFileCmds() 
  402.             When the user chooses Quit on the File menu, we quit.
  403. *****/
  404. void doFileCmds (short theItem)
  405. {
  406.     switch (theItem)
  407.     {            
  408.         case fileQUIT:
  409.             gQuitTheApp = true;
  410.             break;
  411.     }
  412.     return;
  413. } /* end of doFileCmds */
  414.  
  415.  
  416.  
  417. /*****    doTestCmds()
  418.             When the user chooses the item in the Test menu, they
  419.             will get to choose a folder with a custom icon
  420. *****/
  421. void doTestCmds (short theItem)
  422. {    
  423.     
  424.     switch (theItem)
  425.     {
  426.         case testDOTHETEST:
  427.             theTest();
  428.             break;            
  429.     }    
  430.     return;
  431. }                /* end of doTestCmds */
  432.  
  433.  
  434. void theTest()
  435. {
  436.     Boolean        sharingOn;
  437.     
  438.     sharingOn = sharingIsOn();
  439.  
  440.     if (sharingOn)
  441.         DebugStr("\pSharing is on.");
  442. }
  443.  
  444.  
  445. /*
  446.     This is a C version of code in Inside Mac:Files for determining
  447.     if FileSharing is active on a particular Macintosh.
  448.     sharingIsOn indexes through all the local volumes on
  449.     a Macintosh with PBHGetVInfo(), so we can obtain true volume
  450.     reference number information.  We can take the vRefNum we obtain
  451.     and pass it to the volIsSharable() routine until we find a volume
  452.     for which FileSharing is truly active.  If we get an error, for
  453.     example, if we run out of volumes and get an error -35 (nsvErr), 
  454.     or if we find that FileSharing is active on a volume, then we drop
  455.     out of our while loop and return the results.
  456. */
  457. Boolean sharingIsOn()
  458. {
  459.     Boolean            sharing = false;
  460.     HParamBlockRec    myHPB;
  461.     OSErr            myErr;
  462.     short            volIndex = 1;
  463.     
  464.     do
  465.     {
  466.         myHPB.volumeParam.ioNamePtr = nil;
  467.         myHPB.volumeParam.ioVolIndex = volIndex;
  468.         
  469.         myErr = PBHGetVInfoSync(&myHPB);
  470.         if (myErr == noErr)
  471.             sharing = volIsSharable(myHPB.volumeParam.ioVRefNum);
  472.         volIndex++;
  473.     } while ((myErr == noErr) && (!sharing));
  474.     
  475.     return(sharing);
  476. }
  477.  
  478. /*
  479.     volIsSharable lets us know if the bHasPersonalAccessPrivileges bit
  480.     of the vMAttrib field of the GetVolParmsInfoBuffer is set, which 
  481.     indicates that FileSharing is active.
  482. */
  483. Boolean volIsSharable(short vRefNum)
  484. {
  485.     HParamBlockRec            myHPB;
  486.     GetVolParmsInfoBuffer    myInfoBuffer;
  487.     OSErr                    myErr;
  488.     
  489.     myHPB.ioParam.ioNamePtr = nil;
  490.     myHPB.ioParam.ioVRefNum = vRefNum;
  491.     myHPB.ioParam.ioBuffer = (Ptr) &myInfoBuffer;
  492.     myHPB.ioParam.ioReqCount = sizeof(GetVolParmsInfoBuffer);
  493.     
  494.  
  495.     myErr = PBHGetVolParmsSync(&myHPB);
  496.  
  497.     if (myErr == noErr)
  498.     {
  499.         if (((myInfoBuffer.vMAttrib) & (1L << bHasPersonalAccessPrivileges)) != 0)
  500.             return(true);
  501.         else
  502.             return(false);
  503.     }
  504.     else
  505.         return(false);
  506. }