home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / Papers / aSEPiA example source / Application / CPluginManager.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-25  |  4.5 KB  |  171 lines  |  [TEXT/CWIE]

  1. /*---------------------------------------------------------------
  2.  
  3.     CPluginManager.cpp
  4.     
  5.     This object initializes and keeps track of plugins in the plugin
  6.     folder.
  7.     
  8. ---------------------------------------------------------------*/
  9.  
  10. #include "CPluginManager.h"
  11. #include <stdio.h>
  12. #include "IInterfaceProvider.h"
  13. #include "IPluginInfoIntf.h"
  14. #include <TArrayIterator.h>
  15. #include <Lists.h>
  16.  
  17. Handle FSSpecToPath(FSSpec * fss);
  18.  
  19.  
  20. CPluginManager::CPluginManager()
  21.     :mCurrIndex( NULL )
  22. {
  23.     ReadPluginDir();
  24.  
  25.  
  26. }
  27.  
  28. CPluginManager::~CPluginManager()
  29. {
  30.     TArrayIterator<CPlugin*> iterator(mPluginList, LArrayIterator::from_End);
  31.     CPlugin    *tempPlugin;
  32.     while (iterator.Previous(tempPlugin)) {
  33.         mPluginList.RemoveItemsAt(1, iterator.GetCurrentIndex());
  34.         delete tempPlugin;
  35.     }
  36. }
  37.                     
  38. void    CPluginManager::ReadPluginDir()
  39. {
  40.     FSSpec    pluginFolderSpec;
  41.     OSErr    err = ::FSMakeFSSpec ( 0, 0, "\pPlugins", &pluginFolderSpec );
  42.     CreateFileList( pluginFolderSpec, false );
  43. }
  44.  
  45. void CPluginManager::CreateFileList( FSSpec inDirSpec, bool inRecursive )
  46. {
  47.     short            index;
  48.     OSErr            err = noErr;
  49.     CInfoPBRec        pb;
  50.     Cell            cell = {0,0};
  51.     Str255            theName = "\p";
  52.     FSSpec            spec,folderSpec;
  53.  
  54.     // some times this takes a while.  change to a watch
  55.     CursHandle watchCurs = ::GetCursor(watchCursor);
  56.     SetCursor(*watchCurs);
  57.  
  58.     // To get a valid dir id we have to play with the fsspec.
  59.     Handle thePath = FSSpecToPath(&inDirSpec);            // turn it into a path
  60.     short thesize = GetHandleSize(thePath);
  61.     SetHandleSize(thePath,thesize + 6);
  62.     HLock(thePath);
  63.     BlockMove(":zxs~\0", (*thePath)+thesize, 6);    // add a bogus file to the path
  64.     c2pstr(*thePath);
  65.         // make it back into a FSSpec
  66.     err = ::FSMakeFSSpec ( inDirSpec.vRefNum, inDirSpec.parID, (StringPtr)(*thePath), &folderSpec );
  67.         // at this point we get a fnfError which is ok, and par ID now contains the directory we
  68.         //  really want.
  69.     :: DisposeHandle(thePath);
  70.     
  71.     // loop through the items in the directory we want to add
  72.     index = 0;
  73.     do {
  74.         // get the file information for the next item in the directory
  75.         ++index;
  76.                 
  77.         pb.hFileInfo.ioCompletion    =                 NULL;            // no I/O completion routine
  78.         pb.hFileInfo.ioNamePtr        = (StringPtr)    &theName;
  79.         pb.hFileInfo.ioVRefNum        =                 folderSpec.vRefNum;
  80.         pb.hFileInfo.ioDirID        =                 folderSpec.parID;
  81.         pb.hFileInfo.ioFDirIndex    =                 index;
  82.         
  83.         err = ::PBGetCatInfoSync(&pb);
  84.         
  85.         if (err == noErr) {
  86.             // we found something
  87.             // make it into a FSSPec
  88.             err = FSMakeFSSpec(folderSpec.vRefNum, folderSpec.parID, theName, &spec);
  89.             
  90.             // do stuff to it based on what it is            
  91.             if (pb.hFileInfo.ioFlAttrib & ioDirMask) {    // we have a folder
  92.                 err = ::FSMakeFSSpec(folderSpec.vRefNum, folderSpec.parID, theName, &spec);
  93.                 if (inRecursive) {
  94.                     // call outselves recursivly
  95.                     CreateFileList(spec, inRecursive);
  96.                 }
  97.             } else    { // it is a file
  98.                 ProcessFile(spec);
  99.             }
  100.         }
  101.     } while ((err == noErr));
  102.     
  103.     SetCursor(&qd.arrow);
  104. }
  105.  
  106. void CPluginManager::ProcessFile( FSSpec& inSpec )
  107. {
  108.     CPlugin*    thePlugin = new CPlugin( inSpec );
  109.  
  110.     if ( thePlugin->Valid() ) {
  111.         // we need to only add plugins that are valid
  112.         mPluginList.AddItem( thePlugin );
  113.     }
  114. }
  115.  
  116. void CPluginManager::GetPluginDescriptorbyIndex( short index, char* outName )
  117. {
  118.     CPlugin*    tempPlugin = mPluginList[index];
  119.     
  120.     PluginLoader    tempLoader( tempPlugin );
  121.  
  122.     // +++ maybe we should check if it is loaded here.
  123.         
  124.     IPluginInfoIntf* tempInfoIntf;
  125.  
  126.     tempPlugin->GetInterfacePointer(IPluginInfo_ID, &tempInfoIntf );
  127.         
  128.     char    pluginName[256];
  129.     tempInfoIntf->GetPluginName( pluginName );
  130. /*    short version;
  131.     tempInfoIntf->GetPluginVersion( &version );
  132. */    
  133.     sprintf( outName, "%s", pluginName );
  134. }
  135.  
  136. CPlugin*     CPluginManager::GetCurrentPlugin()
  137. {
  138.     return mPluginList[ mCurrIndex ];
  139. }
  140.  
  141. Handle FSSpecToPath(FSSpec * fss)
  142. {
  143.     CInfoPBRec        cipb;
  144.     unsigned char     dirName[64];
  145.     Handle            pathName;
  146.     Size            pathSize;
  147.     OSErr            err;
  148.     
  149.     if (fss == nil) return(nil);
  150.     pathName = NewHandleClear(0L);
  151.     if (pathName != nil) {
  152.         cipb.dirInfo.ioVRefNum = fss->vRefNum;
  153.         cipb.dirInfo.ioDrParID = fss->parID;
  154.         cipb.dirInfo.ioNamePtr = dirName;
  155.         do {
  156.             cipb.dirInfo.ioFDirIndex = -1;
  157.             cipb.dirInfo.ioDrDirID = cipb.dirInfo.ioDrParID;
  158.             err = PBGetCatInfoSync(&cipb);
  159.             if (err == noErr) {
  160.                 dirName[++(*dirName)] = ':';
  161.                 pathSize = GetHandleSize(pathName);
  162.                 SetHandleSize(pathName, pathSize + (Size)*dirName);
  163.                 BlockMove(*pathName, *pathName + *dirName, pathSize);
  164.                 BlockMove(dirName + 1, *pathName, (long)*dirName);
  165.             }
  166.         } while ((err == noErr) && (cipb.dirInfo.ioDrDirID != 2));
  167.         PtrAndHand(fss->name + 1, pathName, (long)*fss->name);
  168.     }
  169.     return(pathName);
  170. }
  171.