home *** CD-ROM | disk | FTP | other *** search
- /*
- file Utilities.c
-
- Description:
- This file contains the a number of utility routines used in the
- PackagTool example. they have been moved to this file
- to simplify the example.
-
- PackageTool is an application illustrating how to create application
- packages in Mac OS 9. It provides a simple interface for converting
- correctly formatted folders into packages and vice versa.
-
- by John Montbriand, 1999.
-
- Copyright: © 1999 by Apple Computer, Inc.
- all rights reserved.
-
- Disclaimer:
- You may incorporate this sample code into your applications without
- restriction, though the sample code has been provided "AS IS" and the
- responsibility for its operation is 100% yours. However, what you are
- not permitted to do is to redistribute the source as "DSC Sample Code"
- after having made changes. If you're going to re-distribute the source,
- we require that you make it clear in the source that the code was
- descended from Apple Sample Code, but that you've made changes.
-
- Change History (most recent first):
- 10/19/99 created by John Montbriand
- */
-
- #include "Utilities.h"
- #include <QuickDraw.h>
- #include <Gestalt.h>
- #include <Palettes.h>
- #include <Threads.h>
- #include <fp.h>
- #include <FinderRegistry.h>
- #include <Resources.h>
- #include <Processes.h>
- #include <string.h>
-
-
-
- /* ValidFSSpec verifies that *spec refers is formatted correctly, and it
- verifies that it refers to an existing file in an existing directory on
- and existing volume. If *spec is valid, the function returns noErr,
- otherwise an error is returned. */
- OSErr ValidFSSpec(FSSpec *spec) {
- FInfo fndrInfo;
- /* check the name's size */
- if (spec->name[0] + (sizeof(FSSpec) - sizeof(spec->name)) > sizeof(FSSpec)) return paramErr;
- /* ckeck if it refers to a file */
- return FSpGetFInfo(spec, &fndrInfo);
- }
-
-
- /* ResolveAliasQuietly resolves an alias using a fast search with no user interaction. Our main loop
- periodically resolves gFileAlias comparing the result to gTargetFile to keep the display up to date.
- As a result, we would like the resolve alias call to be as quick as possible AND since the application
- may be in the background when it is called, we don't want any user interaction. */
- OSErr ResolveAliasQuietly(ConstFSSpecPtr fromFile, AliasHandle alias, FSSpec *target, Boolean *wasChanged) {
- short aliasCount;
- Boolean needsUpdate;
- OSErr err;
- /* set up locals */
- aliasCount = 1;
- needsUpdate = false;
- *wasChanged = false;
- /* call MatchAlias to resolve the alias.
- kARMNoUI = no user interaction,
- kARMSearch = do a fast search. */
- err = MatchAlias(NULL, (kARMNoUI | kARMSearch), alias, &aliasCount, target, &needsUpdate, NULL, NULL);
- if (err == noErr) {
- /* if the alias was changed, update it. */
- err = UpdateAlias(fromFile, target, alias, wasChanged);
- }
- return err;
- }
-
-
-
-
- /* GrayOutBox grays out an area of the screen in the current grafport.
- *theBox is in local coordinates in the current grafport. This routine
- is for direct screen drawing only. */
- void GrayOutBox(Rect *theBox) {
- long response;
- Rect globalBox;
- GDHandle maxDevice;
- RGBColor rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF}, rgbBlack = {0, 0, 0}, sForground, sBackground;
- PenState penSave;
- /* save the current drawing state */
- GetPenState(&penSave);
- /* if no color quickdraw, fail...*/
- if (Gestalt(gestaltQuickdrawVersion, &response) != noErr) response = 0;
- if (response >= gestalt8BitQD) {
- /* get the device for the rectangle */
- globalBox = *theBox;
- LocalToGlobal((Point*) &globalBox.top);
- LocalToGlobal((Point*) &globalBox.bottom);
- maxDevice = GetMaxDevice(&globalBox);
- if (maxDevice != NULL) {
- /* calculate the best gray */
- if ( GetGray(maxDevice, &rgbWhite, &rgbBlack)) {
- /* draw over the area in gray using addMax transfer mode */
- GetForeColor(&sForground);
- GetBackColor(&sBackground);
- RGBForeColor(&rgbBlack);
- RGBBackColor(&rgbWhite);
- PenMode(addMax);
- PaintRect(theBox);
- RGBForeColor(&sForground);
- RGBBackColor(&sBackground);
- /* restore the pen state and leave */
- SetPenState(&penSave);
- return;
- }
- }
- }
- /* fall through to using the gray pattern */
- PenPat(&qd.gray);
- PenMode(notPatBic);
- PaintRect(theBox);
- SetPenState(&penSave);
- }
-
-
- /* ShowDragHiliteBox is called to hilite the drop box area in the
- main window. Here, we draw a 3 pixel wide border around *boxBounds. */
- OSErr ShowDragHiliteBox(DragReference theDragRef, Rect *boxBounds) {
- RgnHandle boxRegion, insetRegion;
- OSErr err;
- Rect box;
- /* set up locals */
- boxRegion = insetRegion = NULL;
- /* create the region */
- if ((boxRegion = NewRgn()) == NULL) { err = memFullErr; goto bail; }
- if ((insetRegion = NewRgn()) == NULL) { err = memFullErr; goto bail; }
- box = *boxBounds;
- InsetRect(&box, -5, -5);
- RectRgn(boxRegion, &box);
- InsetRect(&box, 3, 3);
- RectRgn(insetRegion, &box);
- DiffRgn(boxRegion, insetRegion, boxRegion);
- /* hilite the region */
- err = ShowDragHilite(theDragRef, boxRegion, true);
- bail:
- /* clean up and leave */
- if (boxRegion != NULL) DisposeRgn(boxRegion);
- if (insetRegion != NULL) DisposeRgn(insetRegion);
- return err;
- }
-
-
-
- /* UpdateRelativeAliasFile updates the alias file located at
- aliasDest referring to the targetFile. relative path
- information is stored in the new file. */
- OSErr UpdateRelativeAliasFile(FSSpec *theAliasFile, FSSpec *targetFile) {
- FInfo fndrInfo;
- AliasHandle theAlias;
- Boolean wasChanged;
- short rsrc;
- OSErr err;
- /* set up locals */
- rsrc = -1;
- /* set up our the alias' file information */
- err = FSpGetFInfo(targetFile, &fndrInfo);
- if (err != noErr) goto bail;
- if (fndrInfo.fdType == 'APPL')
- fndrInfo.fdType = kApplicationAliasType;
- fndrInfo.fdFlags = kIsAlias;
- /* set the file information or the new file */
- err = FSpSetFInfo(theAliasFile, &fndrInfo);
- if (err != noErr) goto bail;
- /* save the resource */
- rsrc = FSpOpenResFile(theAliasFile, fsRdWrPerm);
- if (rsrc == -1) { err = ResError(); goto bail; }
- UseResFile(rsrc);
- theAlias = (AliasHandle) Get1IndResource(rAliasType, 1);
- if (theAlias == NULL) { err = resNotFound; goto bail; }
- err = UpdateAlias(theAliasFile, targetFile, theAlias, &wasChanged);
- if (err != noErr) goto bail;
- if (wasChanged)
- ChangedResource((Handle) theAlias);
- CloseResFile(rsrc);
- rsrc = -1;
- /* done */
- return noErr;
- bail:
- if (rsrc != -1) CloseResFile(rsrc);
- return err;
- }
-
-
- enum {
- kFileSharingSignature = 'hhgg' /* Macintosh File Sharing */
- };
-
- /* FileSharingAppIsRunning returns true if the file sharing
- extension is running. */
- Boolean FileSharingAppIsRunning(void) {
- ProcessSerialNumber psn;
- ProcessInfoRec pinfo;
-
- psn.highLongOfPSN = psn.lowLongOfPSN = kNoProcess;
- pinfo.processInfoLength = sizeof(ProcessInfoRec);
- pinfo.processName = NULL;
- pinfo.processAppSpec = NULL;
- while (GetNextProcess(&psn) == noErr)
- if (GetProcessInformation(&psn, &pinfo) == noErr)
- if (pinfo.processSignature == kFileSharingSignature)
- return true;
- return false;
- }
-
-
- /* FSSpecIsInDirectory returns true if the file system object
- referred to by theSpec is somewhere in the directory referred
- to by (vRefNum, dirID) */
- Boolean FSSpecIsInDirectory(FSSpec *theSpec, short vRefNum, long dirID) {
- CInfoPBRec cat;
- HParamBlockRec fvol, dirvol;
- long currentDir;
- /* check the volumes */
- fvol.volumeParam.ioVRefNum = theSpec->vRefNum;
- fvol.volumeParam.ioNamePtr = NULL;
- fvol.volumeParam.ioVolIndex = 0;
- if (PBHGetVInfoSync(&fvol)) return false;
- dirvol.volumeParam.ioVRefNum = vRefNum;
- dirvol.volumeParam.ioNamePtr = NULL;
- dirvol.volumeParam.ioVolIndex = 0;
- if (PBHGetVInfoSync(&dirvol) != noErr) return false;
- if (fvol.volumeParam.ioVRefNum != dirvol.volumeParam.ioVRefNum) return false;
- /* check if the directory is one of the file's parents */
- currentDir = theSpec->parID;
- while (true) {
- if (dirID == currentDir) return true;
- if (currentDir == 2) return false;
- /* make sure it refers to a file in the package's directory */
- memset(&cat, 0, sizeof(cat));
- cat.dirInfo.ioNamePtr = NULL;
- cat.dirInfo.ioVRefNum = theSpec->vRefNum;
- cat.dirInfo.ioFDirIndex = -1;
- cat.dirInfo.ioDrDirID = currentDir;
- if (PBGetCatInfoSync(&cat) != noErr) return false;
- currentDir = cat.dirInfo.ioDrParID;
- }
- }
-
-
- /* FSSpecIsAFolder returns true if the FSSpec pointed
- to by target refers to a folder. */
- Boolean FSSpecIsAFolder(FSSpec *target) {
- CInfoPBRec cat;
- OSErr err;
- /* check the target's flags */
- cat.dirInfo.ioNamePtr = target->name;
- cat.dirInfo.ioVRefNum = target->vRefNum;
- cat.dirInfo.ioFDirIndex = 0;
- cat.dirInfo.ioDrDirID = target->parID;
- err = PBGetCatInfoSync(&cat);
- if (err != noErr) return false;
- return ((cat.dirInfo.ioFlAttrib & 16) != 0);
- }
-
-
- /* ShowChangesInFinderWindow asks the finder redraw a directory
- window by either sending a update container event to the
- finder if this facility exists, or by bumping the parent directorie's
- modification date */
- OSErr ShowChangesInFinderWindow(short vRefNum, long dirID) {
- OSErr err;
- long response;
- CInfoPBRec cat;
- Str255 name;
- /* get information about the directory */
- cat.hFileInfo.ioNamePtr = name;
- cat.hFileInfo.ioVRefNum = vRefNum;
- cat.hFileInfo.ioDirID = dirID;
- cat.hFileInfo.ioFDirIndex = -1;
- if ((err = PBGetCatInfoSync(&cat)) != noErr) return err;
- /* determine what we can do... */
- if (Gestalt(gestaltFinderAttr, &response) != noErr) response = 0;
- if ((response & (1<<gestaltOSLCompliantFinder)) != 0) {
- AppleEvent the_apple_event, the_reply;
- AEAddressDesc target_desc;
- FSSpec spec;
- OSType app_creator;
- long actualSize, the_error;
- DescType actualType;
-
- /* set up a recoverable state */
- AECreateDesc(typeNull, NULL, 0, &the_apple_event);
- AECreateDesc(typeNull, NULL, 0, &target_desc);
- AECreateDesc(typeNull, NULL, 0, &the_reply);
-
- /* create an update event targeted at the finder */
- app_creator = 'MACS';
- err = AECreateDesc(typeApplSignature, (Ptr) &app_creator,
- sizeof(app_creator), &target_desc);
- if (err != noErr) goto send_update_abort;
- err = AECreateAppleEvent(kAEFinderSuite, kAEUpdate,
- &target_desc, kAutoGenerateReturnID,
- kAnyTransactionID, &the_apple_event);
- if (err != noErr) goto send_update_abort;
-
- /* add the FSSpec parameter */
- err = FSMakeFSSpec(vRefNum, cat.dirInfo.ioDrParID, name, &spec);
- if (err != noErr) goto send_update_abort;
- err = AEPutParamPtr(&the_apple_event, keyDirectObject, typeFSS, &spec, sizeof(FSSpec));
- if (err != noErr) goto send_update_abort;
-
- /* send the apple event */
- err = AESend(&the_apple_event, &the_reply, kAENoReply,
- kAENormalPriority, kNoTimeOut, NULL, NULL);
-
- err = AEGetParamPtr(&the_reply, keyErrorNumber, typeLongInteger,
- &actualType, &the_error, 4, &actualSize);
- if (err == errAEDescNotFound)
- err = noErr;
- else if (err == noErr)
- err = the_error;
-
- send_update_abort:
- AEDisposeDesc(&the_apple_event);
- AEDisposeDesc(&target_desc);
- AEDisposeDesc(&the_reply);
-
- } else { /* bump the date for the directory so the finder notices */
- unsigned long secs;
- GetDateTime(&secs);
- cat.hFileInfo.ioFlMdDat = (secs == cat.hFileInfo.ioFlMdDat) ? (++secs) : (secs);
- cat.hFileInfo.ioDirID = cat.dirInfo.ioDrParID;
- err = PBSetCatInfoSync(&cat);
- }
- return err;
- }
-
-
-