DIRECTORY FS USING [Error, StreamOpen], Booting USING [CheckpointProc, RegisterProcs, RollbackProc], Convert USING [RopeFromCard], DB, DBIcons, IO, Basics USING[LowHalf], IOClasses USING [CreateCommentFilterStream ], DBNames USING[ EntityToName, MakeName], Icons USING [NewIconFromFile, IconFlavor], List USING[ DotCons, DottedPair, AList ], MessageWindow USING [Append, Blink], Process USING[Ticks, SecondsToTicks, Detach, Pause], VM USING [AddressFault], Rope USING [Cat, Equal, ROPE], UserProfile USING [ Token ] ; DBIconsImpl: CEDAR MONITOR IMPORTS Basics, Booting, DBNames, FS, Convert, IO, MessageWindow, VM, Process, Rope, Icons, DB, List, UserProfile, IOClasses EXPORTS DBIcons = BEGIN OPEN DB, Rope; Failed: PUBLIC ERROR [why: DBIcons.Failure, reason: Rope.ROPE] = CODE; ROPE: TYPE = Rope.ROPE; IconRelation: DB.Relation; -- the relation giving the properties of an icon IconFileAttr: DB.Attribute; -- the name of the file for an icon IconIndexAttr: DB.Attribute; -- the index in the file IconNameAttr: DB.Attribute; -- the icon itself (the other attributes are treated as properties) iconCache: List.AList _ NIL; -- the internal cache of mappings from icon names to flavors IconDB: PUBLIC ROPE _ NIL; readOnly: PUBLIC BOOLEAN _ TRUE; -- true if the segment is readonly activity: BOOLEAN _ FALSE; -- true if a database operation has been performed recently ticksToWait: Process.Ticks _ Process.SecondsToTicks[5*60]; schemaSet: CONDITION; EstablishIconDB: PUBLIC PROC [file: Rope.ROPE _ NIL] = { IconDB _ IF file = NIL THEN UserProfile.Token[key: "Icons.Segment", default: "[Luther.Alpine]Icons.Segment"] ELSE file; ResetSchema[]; iconCache _ NIL }; WatchDBActivity: PROC[] = { WHILE TRUE DO Process.Pause[ticksToWait]; CheckConnection[] ENDLOOP }; CheckConnection: ENTRY PROC[] = { IF NOT activity THEN { trans: DB.Transaction = DB.TransactionOf[$Icons]; IF trans # NIL THEN DB.CloseTransaction[trans] }; -- don't keep the connection open too long activity _ FALSE; }; RegisterIcon: PUBLIC ENTRY PROC [iconName: Rope.ROPE, fileName: Rope.ROPE, index: CARDINAL] = { ENABLE UNWIND => NULL; [] _ InternalRegisterIcon[iconName, fileName, index] }; GetIcon: PUBLIC ENTRY PROC [iconName: Rope.ROPE, default: Icons.IconFlavor _ unInit] RETURNS [Icons.IconFlavor] = { ENABLE UNWIND => NULL; RETURN[InternalGetIcon[iconName, default]]; }; InternalGetIcon: INTERNAL PROC[iconName: Rope.ROPE, default: Icons.IconFlavor _ unInit] RETURNS [Icons.IconFlavor] = { cachedFlavor: REF ANY = CheckCache[iconName, iconCache]; flavor: Icons.IconFlavor _ unInit; success: BOOL; DoGetIcon: PROC[] = { attributeValue: DB.AttributeValue = [attribute: IconNameAttr, lo: S2V[iconName]]; iconRelship: DB.Relship = DeclareRelship[IconRelation, LIST[attributeValue], OldOnly]; IF iconRelship = NIL THEN {flavor _ default; RETURN}; { fileName: ROPE = V2S[ GetF[iconRelship, IconFileAttr] ]; index: CARDINAL = Basics.LowHalf[LOOPHOLE[V2I[GetF[iconRelship, IconIndexAttr]]]]; flavor _ Icons.NewIconFromFile[fileName, index ! FS.Error => IF default # unInit THEN flavor _ default ELSE ERROR Failed[fileNotFound, fileName]; VM.AddressFault => IF default # unInit THEN flavor _ default ELSE ERROR Failed[invalidIndex, Convert.RopeFromCard[index]]]; -- NewIconFromFile raises this if given badindex. } }; IF cachedFlavor # NIL THEN RETURN[NARROW[cachedFlavor, REF Icons.IconFlavor]^]; IF IconDB = NIL THEN RETURN[default]; success _ CarefullyApply[DoGetIcon]; IF NOT success THEN RETURN[default]; IF flavor # unInit THEN { iconCache _ Cache[ List.DotCons[iconName, NEW[Icons.IconFlavor _ flavor]], iconCache ]; RETURN[flavor] }; IF default # unInit THEN RETURN[default]; ERROR Failed[noSuchIcon, iconName] }; SetIcon: PUBLIC PROCEDURE[e: Entity, iconFile: ROPE, fileIndex: CARDINAL] = BEGIN name: ROPE = IF DB.IsSystemEntity[e] THEN DB.NameOf[e] ELSE DBNames.EntityToName[e, DB.SegmentOf[e]]; IF IsRegistered[name].file = NIL THEN RegisterIcon[ name, iconFile, fileIndex ] END; acorn: Icons.IconFlavor = Icons.NewIconFromFile["///Commands/DB.Icons", 0]; GetIconForEntity: PUBLIC ENTRY PROC[eName, domain: ROPE, segment: DB.Segment] RETURNS [Icons.IconFlavor] = { ENABLE UNWIND => NULL; IF eName = NIL OR domain = NIL OR segment = NIL THEN RETURN[acorn]; IF Rope.Equal[domain, "Domain"] THEN RETURN[InternalGetIcon[eName, acorn]]; BEGIN icon: Icons.IconFlavor _ unInit; fullName: ROPE = DBNames.MakeName[segment, domain, eName]; icon _ InternalGetIcon[fullName ! Failed => CONTINUE]; IF icon # unInit THEN RETURN[icon]; RETURN[InternalGetIcon[domain, acorn]] END }; Cache: INTERNAL PROC[ pair: List.DottedPair, aList: List.AList ] RETURNS[ List.AList ] = { x, x1: List.AList _ NIL; x _ aList; UNTIL x = NIL DO IF Rope.Equal[NARROW[x.first.key, ROPE], NARROW[pair.key, ROPE]] THEN BEGIN x.first.val _ pair.val; RETURN[aList]; END; x1_x; x _ x.rest; ENDLOOP; x _ CONS[pair, NIL]; IF x1 = NIL THEN RETURN[x] ELSE IF x1.rest = NIL THEN x1.rest _ x -- add at end ELSE ERROR ; -- defensive programming RETURN[aList]; }; CheckCache: INTERNAL PROC[ name: Rope.ROPE, in: List.AList ] RETURNS[ val: REF ANY ] = { UNTIL in = NIL DO IF Rope.Equal[NARROW[in.first.key, Rope.ROPE], name] THEN RETURN[in.first.val]; in _ in.rest; ENDLOOP; RETURN[NIL]}; IsRegistered: PUBLIC ENTRY PROC[iconName: Rope.ROPE _ NIL, fileName: Rope.ROPE _ NIL, index: CARDINAL _ LAST[CARDINAL]] RETURNS[name: Rope.ROPE, file: Rope.ROPE, i: CARDINAL] = { ENABLE UNWIND => NULL; DoRegisterCheck: PROC[] = { IF iconName # NIL THEN { attributeValue: DB.AttributeValue = [attribute: IconNameAttr, lo: S2V[iconName]]; iconRelship: DB.Relship = DeclareRelship[IconRelation, LIST[attributeValue], OldOnly]; IF iconRelship = NIL THEN { file _ NIL; name _ NIL; i _ 0 } ELSE { file _ V2S[ GetF[ iconRelship, IconFileAttr ] ]; i _ Basics.LowHalf[LOOPHOLE[V2I[GetF[iconRelship, IconIndexAttr]]]]; name _ iconName } } ELSE { iconRels: RelshipSet = RelationSubset[IconRelation, LIST[AttributeValue[IconFileAttr, S2V[fileName]], AttributeValue[IconIndexAttr, U2V[index]]]]; iconRel: Relship = NextRelship[iconRels]; ReleaseRelshipSet[iconRels]; IF iconRel = NIL THEN{ file _ NIL; name _ NIL; i _ 0 } ELSE { name _ V2S[GetF[iconRel, IconNameAttr]]; file _ fileName; i _ index } } }; IF NOT CarefullyApply[DoRegisterCheck] THEN RETURN[NIL, NIL, 0] }; InvalidateCache: PUBLIC ENTRY PROC [iconName: Rope.ROPE _ NIL] = { ENABLE UNWIND => NULL; IF iconName = NIL THEN iconCache _ NIL ELSE iconCache _ Cache[ List.DotCons[iconName, NIL], iconCache ] }; InternalRegisterIcon: INTERNAL PROC[iconName: Rope.ROPE, fileName: Rope.ROPE, index: CARDINAL] RETURNS[success: BOOLEAN] = { DoRegisterIcon: PROC[] = { attributeValue: DB.AttributeValue = [IconNameAttr, iconName]; iconRel: Relship = DeclareRelship[IconRelation, LIST[attributeValue]]; SetF[iconRel, IconFileAttr, S2V[fileName]]; SetF[iconRel, IconIndexAttr, U2V[index]] }; IF readOnly OR IconDB = NIL THEN RETURN; -- can't register things in this segment success _ CarefullyApply[DoRegisterIcon] }; Parse: INTERNAL PROCEDURE[ stream: IO.STREAM, errlog: IO.STREAM ] = { OPEN IO; error: BOOLEAN _ FALSE; { ENABLE IO.EndOfStream => GOTO Out; DO DBNameProc: IO.BreakProc = { IF char = ') THEN RETURN[break]; RETURN[IF char = CR THEN break ELSE other] }; whiteSpace: IO.BreakProc = { RETURN[ SELECT char FROM CR => break, IO.SP, IO.ESC, IO.LF, IO.TAB, ',, ':, '; => sepr, ENDCASE => other ] }; isACR: IO.BreakProc = { RETURN[IF char = CR THEN break ELSE other] }; tokenProc: IO.BreakProc = { RETURN[SELECT char FROM IO.SP, IO.TAB, ', => sepr, IO.CR => break, ENDCASE => other ]; }; iconName, fileName: Rope.ROPE; index: CARDINAL; [] _ IO.SkipWhitespace[stream]; IF stream.GetChar[] # '( THEN { error _ TRUE; Report[msg: Rope.Cat["Transaction Aborted at: ", Convert.RopeFromCard[stream.GetIndex[]]], errLog: errlog]; GOTO Out }; iconName _ IO.GetTokenRope[stream, DBNameProc].token; IF stream.GetChar[] # ') THEN { error _ TRUE; Report[msg: Rope.Cat["Transaction Aborted at: ", Convert.RopeFromCard[stream.GetIndex[]]], errLog: errlog]; GOTO Out }; [] _ IO.SkipWhitespace[stream]; -- skip any intervening whitespace fileName _ IO.GetTokenRope[stream, whiteSpace].token; index _ IO.GetCard[stream]; IF NOT InternalRegisterIcon[iconName, fileName, index] THEN { error _ TRUE; Report[msg: Rope.Cat["Transaction Aborted at: ", Convert.RopeFromCard[stream.GetIndex[]]], errLog: errlog]; GOTO Out } ENDLOOP; EXITS Out => NULL; }; IF error THEN { MessageWindow.Append["problems encountered in reading icon file."]; MessageWindow.Blink[]; IF errlog # NIL THEN errlog.Close[]; }; }; WriteCatalogue: PUBLIC ENTRY PROC [file: Rope.ROPE] = { ENABLE UNWIND => NULL; stream: IO.STREAM; InternalWrite: PROC[] = { OPEN IO; iconRels: RelshipSet = RelationSubset[IconRelation]; FOR r: Relship _ NextRelship[iconRels], NextRelship[iconRels] UNTIL r = NIL DO fileName: ROPE = V2S[ GetF[IconFileAttr, IconFileAttr] ]; iconName: ROPE = V2S[ GetF[IconFileAttr, IconNameAttr] ]; index: CARDINAL = Basics.LowHalf[LOOPHOLE[V2I[GetF[r, IconIndexAttr]]]]; stream.PutF["\n(%g) %g %d", rope[iconName], rope[fileName], int[index]]; ENDLOOP; stream.Close[] }; stream _ FS.StreamOpen[file, $append ! FS.Error => {stream _ NIL; CONTINUE}]; IF stream # NIL THEN [] _ CarefullyApply[InternalWrite] }; Report: INTERNAL PROCEDURE [msg: Rope.ROPE, errLog: IO.STREAM] = { OPEN IO; IF errLog # NIL THEN errLog.PutF["\n\n%g", rope[msg]]; }; OpenUp: Booting.RollbackProc = { DB.Initialize[nCachePages: 256]; InvalidateCache[] }; CloseTrans: Booting.CheckpointProc = { DB.CloseTransaction[trans: DB.TransactionOf[segment: $Icons]] }; ResetSchema: ENTRY PROC[] = { ENABLE UNWIND => NULL; success: BOOLEAN; activity _ TRUE; IF DB.TransactionOf[$Icons] # NIL THEN RETURN; [success, readOnly] _ SetUpSegment[]; IF NOT success THEN RETURN; IF NOT DB.Null[IconRelation] THEN { BROADCAST schemaSet; RETURN }; -- all is well, don't bother flushing cache IconRelation _ DeclareRelation[ "IconInfo", $Icons ]; IconFileAttr _ DeclareAttribute[IconRelation, "file", RopeType]; IconIndexAttr _ DeclareAttribute[IconRelation, "index", IntType]; IconNameAttr _ DeclareAttribute[IconRelation, "name", RopeType, Key]; [] _ DeclareIndex[IconRelation, LIST[IconNameAttr]]; iconCache _ NIL; BROADCAST schemaSet }; ReadCatalogue: PUBLIC ENTRY PROC[file: Rope.ROPE, errlog: IO.STREAM _ NIL] = TRUSTED { ENABLE UNWIND => NULL; stream: IO.STREAM; stream _ FS.StreamOpen[file ! FS.Error => { stream _ NIL; CONTINUE }]; IF stream = NIL THEN RETURN; stream _ IOClasses.CreateCommentFilterStream[stream]; Parse[ stream, errlog ]; DB.MarkTransaction[DB.TransactionOf[$Icons]] }; CarefullyApply: INTERNAL PROC [proc: PROC[]] RETURNS [succeeded: BOOL] ~ { ENABLE DB.Error, DB.Failure, DB.Aborted => {succeeded _ FALSE; GOTO Quit}; transactionToBeAborted: DB.Transaction _ NIL; succeeded _ TRUE; proc[ ! DB.Aborted => { transactionToBeAborted _ trans; succeeded _ FALSE; CONTINUE }; DB.Error => {IF code # TransactionNotOpen THEN REJECT; succeeded _ FALSE; CONTINUE} ]; IF succeeded THEN RETURN; -- no aborted occurred IF transactionToBeAborted # NIL THEN DB.AbortTransaction[transactionToBeAborted]; TRUSTED {Process.Detach[ FORK ResetSchema[] ]}; WAIT schemaSet; -- ResetSchema is doing its job since we forked it proc[]; -- don't bother trying to restart here -- succeeded _ TRUE; EXITS Quit => NULL; }; SetUpSegment: INTERNAL PROC [] RETURNS [success: BOOL, readOnly: BOOL] ~ { ENABLE DB.Aborted, DB.Failure, DB.Error => {success _ FALSE; CONTINUE}; segment: ATOM = $Icons; readOnly _ FALSE; success _ TRUE; DB.Initialize[nCachePages: 256]; IF DB.TransactionOf[segment] # NIL THEN {readOnly_ DB.GetSegmentInfo[segment].readOnly; RETURN}; DB.DeclareSegment[IconDB, segment, 140B, FALSE]; DB.OpenTransaction[segment ! DB.Error => TRUSTED { success_ FALSE; SELECT code FROM ProtectionViolation => {readOnly _ TRUE; CONTINUE}; ENDCASE => GOTO AlreadyDone} ]; IF success THEN GOTO AlreadyDone; DB.CloseTransaction[DB.TransactionOf[segment]]; DB.DeclareSegment[IconDB, segment, 140B, TRUE]; DB.OpenTransaction[segment]; IF NOT success THEN DB.CloseTransaction[DB.TransactionOf[segment]] EXITS AlreadyDone => NULL }; TRUSTED { Booting.RegisterProcs[c: CloseTrans, r: OpenUp]; EstablishIconDB[]; Process.Detach[ FORK WatchDBActivity[] ] }; END. ”File: DBIconsImpl.mesa -- Edited by Teitelman on July 18, 1984 9:12:44 am PDT -- Edited by: Donahue, August 15, 1984 9:10:47 am PDT -- Last Edited by: Widom, August 8, 1984 9:14:17 pm PDT Types Global variables for accessing the database The tuples in the relation have the structure IconRelation[ IconNameAttr: ROPE, IconFileAttr: ROPE, IconIndexAttr: INT ] Establishing the database Accessing Icons Changes RegisteredIcons.Catalogue to reflect the indicated association. This definition will replace any previous definition. comments are for inclusion in the catalogue to help the user figure out what the icon looks like. Obtain an icon flavor for the icon associated with iconName. Create a new icon flavor if one has not previously been created. If for some reason it could not obtain the icon, and default # unInit, returns the default. Otherwise, raises Error (defined below) with appropriate parameters. ****************** setting/getting icons for entities **************** -- works for Entities or Domains now try the name of the domain of the entity key not found on x Can be called with either iconName, or (fileName and index); does not force creation of icon flavor (only GetIcon does that). If an iconFlavor was previous created for this icon, discard the flavor, i.e. the next call to GetIcon will create a new flavor. If iconName = NIL, do this for all registered icons. For use by iconeditor. Parsing Icon Catalogues just scan to a right paren (or be prepared to quit at a CR if you don't find one) Reporting errors Opening, closing segment, making/reading catalogue readOnly must be true to get here; try opening the segment for reading Initialization สฎ˜Jšฯiœ™MJ™nJ™šฯk ˜ Jšžœžœ˜Jšœžœ/˜