home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------
-
- CPluginManager.cpp
-
- This object initializes and keeps track of plugins in the plugin
- folder.
-
- ---------------------------------------------------------------*/
-
- #include "CPluginManager.h"
- #include <stdio.h>
- #include "IInterfaceProvider.h"
- #include "IPluginInfoIntf.h"
- #include <TArrayIterator.h>
- #include <Lists.h>
-
- Handle FSSpecToPath(FSSpec * fss);
-
-
- CPluginManager::CPluginManager()
- :mCurrIndex( NULL )
- {
- ReadPluginDir();
-
-
- }
-
- CPluginManager::~CPluginManager()
- {
- TArrayIterator<CPlugin*> iterator(mPluginList, LArrayIterator::from_End);
- CPlugin *tempPlugin;
- while (iterator.Previous(tempPlugin)) {
- mPluginList.RemoveItemsAt(1, iterator.GetCurrentIndex());
- delete tempPlugin;
- }
- }
-
- void CPluginManager::ReadPluginDir()
- {
- FSSpec pluginFolderSpec;
- OSErr err = ::FSMakeFSSpec ( 0, 0, "\pPlugins", &pluginFolderSpec );
- CreateFileList( pluginFolderSpec, false );
- }
-
- void CPluginManager::CreateFileList( FSSpec inDirSpec, bool inRecursive )
- {
- short index;
- OSErr err = noErr;
- CInfoPBRec pb;
- Cell cell = {0,0};
- Str255 theName = "\p";
- FSSpec spec,folderSpec;
-
- // some times this takes a while. change to a watch
- CursHandle watchCurs = ::GetCursor(watchCursor);
- SetCursor(*watchCurs);
-
- // To get a valid dir id we have to play with the fsspec.
- Handle thePath = FSSpecToPath(&inDirSpec); // turn it into a path
- short thesize = GetHandleSize(thePath);
- SetHandleSize(thePath,thesize + 6);
- HLock(thePath);
- BlockMove(":zxs~\0", (*thePath)+thesize, 6); // add a bogus file to the path
- c2pstr(*thePath);
- // make it back into a FSSpec
- err = ::FSMakeFSSpec ( inDirSpec.vRefNum, inDirSpec.parID, (StringPtr)(*thePath), &folderSpec );
- // at this point we get a fnfError which is ok, and par ID now contains the directory we
- // really want.
- :: DisposeHandle(thePath);
-
- // loop through the items in the directory we want to add
- index = 0;
- do {
- // get the file information for the next item in the directory
- ++index;
-
- pb.hFileInfo.ioCompletion = NULL; // no I/O completion routine
- pb.hFileInfo.ioNamePtr = (StringPtr) &theName;
- pb.hFileInfo.ioVRefNum = folderSpec.vRefNum;
- pb.hFileInfo.ioDirID = folderSpec.parID;
- pb.hFileInfo.ioFDirIndex = index;
-
- err = ::PBGetCatInfoSync(&pb);
-
- if (err == noErr) {
- // we found something
- // make it into a FSSPec
- err = FSMakeFSSpec(folderSpec.vRefNum, folderSpec.parID, theName, &spec);
-
- // do stuff to it based on what it is
- if (pb.hFileInfo.ioFlAttrib & ioDirMask) { // we have a folder
- err = ::FSMakeFSSpec(folderSpec.vRefNum, folderSpec.parID, theName, &spec);
- if (inRecursive) {
- // call outselves recursivly
- CreateFileList(spec, inRecursive);
- }
- } else { // it is a file
- ProcessFile(spec);
- }
- }
- } while ((err == noErr));
-
- SetCursor(&qd.arrow);
- }
-
- void CPluginManager::ProcessFile( FSSpec& inSpec )
- {
- CPlugin* thePlugin = new CPlugin( inSpec );
-
- if ( thePlugin->Valid() ) {
- // we need to only add plugins that are valid
- mPluginList.AddItem( thePlugin );
- }
- }
-
- void CPluginManager::GetPluginDescriptorbyIndex( short index, char* outName )
- {
- CPlugin* tempPlugin = mPluginList[index];
-
- PluginLoader tempLoader( tempPlugin );
-
- // +++ maybe we should check if it is loaded here.
-
- IPluginInfoIntf* tempInfoIntf;
-
- tempPlugin->GetInterfacePointer(IPluginInfo_ID, &tempInfoIntf );
-
- char pluginName[256];
- tempInfoIntf->GetPluginName( pluginName );
- /* short version;
- tempInfoIntf->GetPluginVersion( &version );
- */
- sprintf( outName, "%s", pluginName );
- }
-
- CPlugin* CPluginManager::GetCurrentPlugin()
- {
- return mPluginList[ mCurrIndex ];
- }
-
- Handle FSSpecToPath(FSSpec * fss)
- {
- CInfoPBRec cipb;
- unsigned char dirName[64];
- Handle pathName;
- Size pathSize;
- OSErr err;
-
- if (fss == nil) return(nil);
- pathName = NewHandleClear(0L);
- if (pathName != nil) {
- cipb.dirInfo.ioVRefNum = fss->vRefNum;
- cipb.dirInfo.ioDrParID = fss->parID;
- cipb.dirInfo.ioNamePtr = dirName;
- do {
- cipb.dirInfo.ioFDirIndex = -1;
- cipb.dirInfo.ioDrDirID = cipb.dirInfo.ioDrParID;
- err = PBGetCatInfoSync(&cipb);
- if (err == noErr) {
- dirName[++(*dirName)] = ':';
- pathSize = GetHandleSize(pathName);
- SetHandleSize(pathName, pathSize + (Size)*dirName);
- BlockMove(*pathName, *pathName + *dirName, pathSize);
- BlockMove(dirName + 1, *pathName, (long)*dirName);
- }
- } while ((err == noErr) && (cipb.dirInfo.ioDrDirID != 2));
- PtrAndHand(fss->name + 1, pathName, (long)*fss->name);
- }
- return(pathName);
- }
-