home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-08-27 | 4.7 KB | 184 lines | [TEXT/CWIE] |
- // ===========================================================================
- // LFolderWatcher.cp © 1995, Éric Forget. All rights reserved.
- // ===========================================================================
- //
- // ************************************************************************
- // * *
- // * Before using this code you should read the "License Agreement" *
- // * document and agree with it. *
- // * *
- // ************************************************************************
- //
- // LFolderWatcher, as its name tell, watch a folder for any modification in
- // it. LFolderWatcher is a subclass of LSortedList.
- //
- // ---------------------------------------------------------------------------
- //
- // Instruction Notes:
- // -----------------
- //
- // 1) Create a LFolderWatcher object: inFileCreator and inFileType are optional;
- //
- // 2) Do: if(myWatcher->Update()) {
- //
- // // The contains of the folder has changed!
- //
- // LListIterator iterator(*myWatcher, iterate_FromStart);
- // FSSpec theSpec;
- // while (iterator.Next(&theSpec)) {
- //
- // // Do what you need with theSpec
- // }
- // }
- //
- // ---------------------------------------------------------------------------
- //
- // Usage Notes:
- // -----------
- //
- // If you specify inFileCreator or InFileType with "fileType_Default"
- // LFolderWatcher will interpret it as any type.
- //
- // ---------------------------------------------------------------------------
-
-
- #include "LFolderWatcher.h"
- #include "LFSSpecComparator.h"
-
-
- // ---------------------------------------------------------------------------
- // • Constants
- // ---------------------------------------------------------------------------
-
- const char Mask_IsDirectory = 0x0010;
- const char kIsAlias = 0x8000;
-
-
- // ---------------------------------------------------------------------------
- // • LFolderWatcher
- // ---------------------------------------------------------------------------
-
- LFolderWatcher::LFolderWatcher(
- Int32 inDirID,
- Int16 inVRefNum,
- OSType inFileCreator,
- OSType inFileType)
- : LSortedList(sizeof(FSSpec))
- {
- mDirID = inDirID;
- mVRefNum = inVRefNum;
- mFileType = inFileType;
- mFileCreator = inFileCreator;
-
- mLastModificationDate = 0;
-
-
- SetComparator(new LFSSpecComparator);
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~LFolderWatcher
- // ---------------------------------------------------------------------------
-
- LFolderWatcher::~LFolderWatcher()
- {
- }
-
-
- // ---------------------------------------------------------------------------
- // • Update
- // ---------------------------------------------------------------------------
-
- Boolean
- LFolderWatcher::Update()
- {
- OSErr err;
- CInfoPBRec cBlock;
- Str63 stringName;
- Boolean rval = false;
-
-
- cBlock.dirInfo.ioCompletion = nil;
- cBlock.dirInfo.ioVRefNum = mVRefNum;
- cBlock.dirInfo.ioDrDirID = mDirID;
- cBlock.dirInfo.ioNamePtr = stringName;
- cBlock.dirInfo.ioFDirIndex = -1;
-
- err = ::PBGetCatInfoSync(&cBlock);
-
- if((err == noErr) && (cBlock.dirInfo.ioDrMdDat != mLastModificationDate)) {
-
- // The contains of the folder has changed!
-
- Reset();
- mLastModificationDate = cBlock.dirInfo.ioDrMdDat;
- rval = true;
- }
-
- return rval;
- }
-
-
- // ---------------------------------------------------------------------------
- // • Reset
- // ---------------------------------------------------------------------------
-
- void
- LFolderWatcher::Reset()
- {
- OSErr err;
- CInfoPBRec cBlock;
- Str255 fileName;
- FSSpec fileSpec;
- FInfo fndrInfo;
-
-
- cBlock.hFileInfo.ioCompletion = nil;
- cBlock.hFileInfo.ioNamePtr = fileName;
- cBlock.hFileInfo.ioFDirIndex = 1;
-
- RemoveItemsAt(GetCount(), 1);
-
- do {
-
- cBlock.hFileInfo.ioVRefNum = mVRefNum;
- cBlock.hFileInfo.ioDirID = mDirID;
-
- err = ::PBGetCatInfoSync(&cBlock);
- cBlock.hFileInfo.ioFDirIndex++;
-
- if(err == noErr) {
-
- if(!(cBlock.dirInfo.ioFlAttrib & Mask_IsDirectory)) {
-
- if((err = ::FSMakeFSSpec(mVRefNum, mDirID, fileName, &fileSpec)) == noErr) {
-
- Boolean targetIsFolder, wasAliased;
-
-
- fndrInfo = cBlock.hFileInfo.ioFlFndrInfo;
-
- if((err = ::ResolveAliasFile(&fileSpec, true, &targetIsFolder, &wasAliased)) != noErr) {
-
- if(wasAliased) {
-
- err = ::FSpGetFInfo(&fileSpec, &fndrInfo);
- }
- }
-
- if((err == noErr) &&
- (mFileCreator == fileType_Default || fndrInfo.fdCreator == mFileCreator) &&
- (mFileType == fileType_Default || fndrInfo.fdType == mFileType)) {
-
- AddItem(&fileSpec);
- }
- }
- }
-
- err = noErr;
- }
-
- } while(err == noErr);
- }
-