home *** CD-ROM | disk | FTP | other *** search
- /*
- File: CMCtr.cpp
-
- Contains: Implementation of ODBentoContainer class.
-
- Owned by: Vincent Lo
-
- Copyright: © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <5> 7/25/96 DH Fixed ODBentoContainerAcquireDocument
- method to check the somClassReference
- allocation and throw if null (No bug #,
- approved by Bern).
- <4> 6/6/96 jpa T10020: Fixed uninitialized ODContainerName
- warning
- <3> 5/24/96 jpa 1246074: SOM_CATCH --> SOM_TRY..SOM_ENDTRY
-
- To Do:
- In Progress:
-
- */
-
- #define ODBentoContainer_Class_Source
-
- #define VARIABLE_MACROS
-
- #include <CMCtr.xih>
-
- #ifndef _PLFMDEF_
- #include "PlfmDef.h"
- #endif
-
- #ifndef _ODTYPES_
- #include "ODTypes.h"
- #endif
-
- #ifndef SOM_ODStorageSystem_xh
- #include <ODStor.xh>
- #endif
-
- #ifndef SOM_ODDocument_xh
- #include <Document.xh>
- #endif
-
- #ifndef SOM_CMDocument_xh
- #include <CMDoc.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef _SESSHDR_
- #include "SessHdr.h" // for sessionRoutinesMetahandler
- #endif
-
- #ifndef __CM_API__
- #include "CMAPI.h"
- #endif
-
- #ifndef _BENTOHDR_
- #include "BentoHdr.h"
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef SOM_Module_OpenDoc_Errors_defined
- #include <ErrorDef.xh>
- #endif
-
- #ifndef _ODNEW_
- #include <ODNew.h>
- #endif
-
- #ifndef SOM_ODNameSpaceManager_xh
- #include <NmSpcMg.xh>
- #endif
-
- #ifndef SOM_ODValueNameSpace_xh
- #include <ValueNS.xh>
- #endif
-
- #ifdef DebugRefCount
- #include <stdio.h>
- #endif
-
- #ifndef _ITEXT_
- #include <IText.h>
- #endif
-
- #ifndef _BARRAY_
- #include <BArray.h>
- #endif
-
- #ifndef _BENTODEF_
- #include "BentoDef.h"
- #endif
-
- #ifndef _ODMEMORY_
- #include "ODMemory.h" // Adkins -- added
- #endif
-
- #ifndef _ODDEBUG_
- #include "ODDebug.h" // Adkins -- added
- #endif
-
- #ifndef _NMSPCUTL_
- #include <NmSpcUtl.h>
- #endif
-
-
- #pragma segment BentoCtr
-
- //==============================================================================
- // Constants
- //==============================================================================
-
- const ODISOStr kODContainerSuitesNameSpace = "OpenDoc:Apple:Macintosh:Container Suites";
- const ODISOStr kODBentoContainerSuite = "OpenDoc:Apple:Macintosh:Container Suite:Bento";
- #define kODExpectedNumOfContainerSuites 10
-
- //==============================================================================
- // ODBentoContainer
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetStorageSystem
- //------------------------------------------------------------------------------
-
- SOM_Scope ODStorageSystem* SOMLINK ODBentoContainerGetStorageSystem(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","GetStorageSystem");
-
- return _fStorageSystem;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetID
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainerID SOMLINK ODBentoContainerGetID(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","GetID");
-
- ODContainerID id;
-
- SOM_TRY
-
- id = CopyByteArrayStruct(_fID);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- return id;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetName
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainerName SOMLINK ODBentoContainerGetName(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","GetName");
-
- WARN("A subclass should have overridden this method!");
- ODSetSOMException(ev,kODErrSubClassResponsibility, "SubClass Responsibility");
- ODContainerName dummy = {0,0,kODNULL};
- return dummy;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: SetName
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODBentoContainerSetName(ODBentoContainer *somSelf, Environment *ev,
- ODContainerName* name)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","SetName");
-
- WARN("A subclass should have overridden this method!");
- ODSetSOMException(ev,kODErrSubClassResponsibility, "SubClass Responsibility");
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: AcquireDocument
- //------------------------------------------------------------------------------
-
- SOM_Scope ODDocument* SOMLINK ODBentoContainerAcquireDocument(ODBentoContainer *somSelf, Environment *ev,
- ODDocumentID id)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","AcquireDocument");
-
- SOM_TRY
-
- if (_fDocument == kODNULL) {
-
- SOMClass* cmDocClass = somNewClassReference(CMDocument);
- THROW_IF_NULL( cmDocClass );
- ODULong size = cmDocClass->somGetInstanceSize();
- ODPtr buffer = ODNewPtr(size, somSelf->GetHeap(ev));
- ODDocument* document = (ODDocument*) cmDocClass->somRenew(buffer);
- somReleaseClassReference ( cmDocClass );
-
- if (document != kODNULL)
- {
- document->InitDocument(ev, somSelf, id);
- _fDocument = document;
- }
- }
- else {
- if (_fDocument->GetID(ev) != id)
- THROW(kODErrDocumentDoesNotExist);
- _fDocument->Acquire(ev);
- }
-
- return _fDocument;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- return (ODDocument*) kODNULL;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: Acquire
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODBentoContainerAcquire(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","Acquire");
-
- SOM_TRY
-
- ODBentoContainer_parent_ODContainer_Acquire(somSelf, ev);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: Release
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODBentoContainerRelease(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","Release");
-
- SOM_TRY
-
- ODBentoContainer_parent_ODContainer_Release(somSelf, ev);
-
- if (somSelf->GetRefCount(ev) == 0) {
- somSelf->GetStorageSystem(ev)->ReleaseContainer(ev, somSelf);
- }
-
- SOM_CATCH_ALL
-
- ODError err = ErrorCode();
-
- WARN("Error occurred in ODContainer::Release: %d %s", err, ErrorMessage() ?ErrorMessage() :"");
-
- // if (err == kODErrBentoErr)
- // SetErrorCode(kODErrFatalContainerError);
- // else if (err != kODErrFatalContainerError)
- SetErrorCode(kODNoError);
-
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: ~ODBentoContainer
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODBentoContainersomUninit(ODBentoContainer *somSelf)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","somUninit");
-
- #ifdef DebugRefCount
- printf("~ODBentoContainer %x RefCount %d\n", this, somSelf->GetRefCount(ev));
- fflush(stdout);
- #endif
-
- Environment* ev = somGetGlobalEnvironment();
-
- if (_fDocument != kODNULL) {
- #if ODDebug
- if (_fDocument->GetRefCount(ev) != 0)
- WARN("Document not ref-counted properly.");
- #endif
- delete _fDocument;
- _fDocument = kODNULL;
- }
-
- if (_fID)
- {
- ODDisposePtr(_fID->_buffer);
- ODDisposePtr(_fID);
- }
-
- ODBentoContainer_parent_ODContainer_somUninit(somSelf);
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: InitContainer
- //------------------------------------------------------------------------------
-
- #define kODcmAllocReserveSize (16 * 1204)
-
- SOM_Scope void SOMLINK ODBentoContainerInitContainer(ODBentoContainer *somSelf, Environment *ev,
- ODStorageSystem* storage, ODContainerID* id)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","InitContainer");
-
- SOM_TRY
-
- /* Moved from somInit. SOM itself sets fields to zero
- _fStorageSystem = kODNULL;
- _fID = 0;
- _fDocument = kODNULL;
- _fDirty = kODFalse;
- _fHeap = kDefaultHeapID;
- */
-
- _fStorageSystem = storage;
- if (_fStorageSystem == kODNULL)
- THROW(kODErrIllegalNullStorageSystemInput);
-
- ODBentoContainer_parent_ODContainer_InitContainer(somSelf, ev, storage, id);
-
- _fID = CopyByteArray(id);
- _fHeap = ODRecoverHeapID(somSelf);
-
- ODSession* session = _fStorageSystem->GetSession(ev);
- ODNameSpaceManager* nameSpaceManager = session->GetNameSpaceManager(ev);
- WASSERTM(nameSpaceManager != kODNULL, "No Name Space Manager");
-
- ODValueNameSpace* nameSpace = (ODValueNameSpace*) nameSpaceManager->HasNameSpace(ev, kODContainerSuitesNameSpace);
- if (nameSpace == kODNULL) {
- nameSpace = (ODValueNameSpace*) nameSpaceManager->CreateNameSpace(ev,
- kODContainerSuitesNameSpace,
- kODNULL,
- kODExpectedNumOfContainerSuites,
- kODNSDataTypeODValue);
- }
-
- CMSession cmSession = kODNULL;
- if (nameSpace->Exists(ev, kODBentoContainerSuite) == kODFalse) {
- ODSessionRefCon* sessionRefCon = new(somSelf->GetHeap(ev)) ODSessionRefCon;
-
- #ifdef ODDebugBentoSize
- {
- memset(sessionRefCon, 0, sizeof(ODSessionRefCon));
- SOM_TraceLevel = kODTrue;
- }
- #endif
-
- sessionRefCon->container = kODNULL;
- sessionRefCon->heap = somSelf->GetHeap(ev);
- sessionRefCon->cmAllocReserveBlock = kODNULL; // init to null in case alloc fails
- sessionRefCon->cmAllocReserveSize = kODcmAllocReserveSize;
- sessionRefCon->cmAllocReserveBlock = ODNewPtr(kODcmAllocReserveSize, sessionRefCon->heap);
-
- cmSession = CMStartSession(sessionRoutinesMetahandler, sessionRefCon);
- ValueNameSpaceRegister( nameSpace, ev,
- kODBentoContainerSuite,
- (ODPtr)&cmSession,
- sizeof(CMSession));
- }
- else {
- CMSession* cmSessionPtr;
- ODULong length = 0;
- if (ValueNameSpaceGetEntry(nameSpace, ev, kODBentoContainerSuite,
- (ODPtr*) &cmSessionPtr, &length))
- {
- cmSession = *cmSessionPtr;
- ODDeleteObject( cmSessionPtr );
- }
-
- WASSERTM(length == sizeof(CMSession), "No CMSession");
- }
- ASSERTM(cmSession!=kODNULL,kODErrCannotCreateContainer,"Can't find CMSession");
- _fCMSession = cmSession;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: Create
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainer* SOMLINK ODBentoContainerCreate(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","Create");
-
- WARN("A subclass should have overridden this method!");
- ODSetSOMException(ev,kODErrSubClassResponsibility, "SubClass Responsibility");
-
- return somSelf;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: Open
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainer* SOMLINK ODBentoContainerOpen(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","Open");
-
- WARN("A subclass should have overridden this method!");
- ODSetSOMException(ev,kODErrSubClassResponsibility, "SubClass Responsibility");
-
- return somSelf;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: Close
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainer* SOMLINK ODBentoContainerClose(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","Close");
-
- SOM_TRY
-
- return somSelf;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- return (ODContainer*) kODNULL;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: Purge
- //------------------------------------------------------------------------------
-
- SOM_Scope ODSize SOMLINK ODBentoContainerPurge(ODBentoContainer *somSelf, Environment *ev,
- ODSize size)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","Purge");
-
- ODSize purgedSize = 0; ODVolatile( purgedSize );
-
- SOM_TRY
-
- purgedSize = parent_Purge(somSelf, ev, size);
- if (_fDocument != kODNULL)
- purgedSize += _fDocument->Purge(ev, size);
-
- SOM_CATCH_ALL
- WARN("Error %ld trying to purge in ODBentoContainerPurge",ErrorCode());
- SetErrorCode(kODNoError); // Eat the exception; Purge should not
- // propagate it because clients function
- // fine whether memory was purged or not.
- SOM_ENDTRY
-
- return purgedSize;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: ReleaseDocument
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainer* SOMLINK ODBentoContainerReleaseDocument(ODBentoContainer *somSelf, Environment *ev,
- ODDocument* document)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","ReleaseDocument");
-
- return somSelf;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetCMContainer
- //------------------------------------------------------------------------------
-
- SOM_Scope CMContainer SOMLINK ODBentoContainerGetCMContainer(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","ODBentoContainerGetCMContainer");
-
- WARN("A subclass should have overridden this method!");
- ODSetSOMException(ev,kODErrSubClassResponsibility, "SubClass Responsibility");
-
- return (CMContainer) kODNULL;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetHandlers
- //------------------------------------------------------------------------------
-
- SOM_Scope ODBentoHandlers* SOMLINK ODBentoContainerGetHandlers(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","ODBentoContainerGetHandlers");
-
- WARN("A subclass should have overridden this method!");
- ODSetSOMException(ev,kODErrSubClassResponsibility, "SubClass Responsibility");
-
- return (ODBentoHandlers*) kODNULL;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetDirtyFlag
- //------------------------------------------------------------------------------
-
- SOM_Scope ODBoolean SOMLINK ODBentoContainerGetDirtyFlag(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","GetDirtyFlag");
-
- return _fDirty;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: SetDirtyFlag
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODBentoContainerSetDirtyFlag(ODBentoContainer *somSelf, Environment *ev, ODBoolean flag)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","SetDirtyFlag");
-
- _fDirty = flag;
- }
-
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetHeap
- //------------------------------------------------------------------------------
-
- SOM_Scope ODMemoryHeapID SOMLINK ODBentoContainerGetHeap(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","GetHeap");
-
- return _fHeap;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetCMSession
- //------------------------------------------------------------------------------
-
- SOM_Scope CMSession SOMLINK ODBentoContainerGetCMSession(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","GetHeap");
-
- if (_fCMSession == kODNULL)
- ODSetSOMException(ev,kODErrBentoNoSession);
-
- return _fCMSession;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetTargetContainer
- //------------------------------------------------------------------------------
-
- SOM_Scope ODBentoContainer* SOMLINK ODBentoContainerGetTargetContainer(ODBentoContainer *somSelf, Environment *ev)
- {
- // ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","GetTargetContainer");
-
- return kODNULL;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetTargetDocument
- //------------------------------------------------------------------------------
-
- SOM_Scope CMDocument* SOMLINK ODBentoContainerGetTargetDocument(ODBentoContainer *somSelf, Environment *ev)
- {
- // ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","GetTargetDocument");
-
- return kODNULL;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: GetModDate
- //------------------------------------------------------------------------------
-
- SOM_Scope ODTime SOMLINK ODBentoContainerGetModDate(ODBentoContainer *somSelf, Environment *ev)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","GetModDate");
-
- return _fModDate;
- }
-
- //------------------------------------------------------------------------------
- // ODBentoContainer: SetModDate
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODBentoContainerSetModDate(ODBentoContainer *somSelf, Environment *ev, ODTime timer)
- {
- ODBentoContainerData *somThis = ODBentoContainerGetData(somSelf);
- ODBentoContainerMethodDebug("ODBentoContainer","SetModDate");
-
- _fModDate = timer;
- }
-
-