(FILECREATED "21-Jan-87 12:43:16" {QV}<NOTECARDS>1.3K>NEXT>RGPATCH009.;1 13089  

      changes to:  (VARS RGPATCH009COMS))


(* Copyright (c) 1987 by Xerox Corporation. All rights reserved.)

(PRETTYCOMPRINT RGPATCH009COMS)

(RPAQQ RGPATCH009COMS ((* * rg 1/15/87 Moved code to NIL out NoteFile stream on close so that it 
			    does it for all NoteFiles, not just local)
			 (* * changes to NCDATABASE)
			 (FNS NC.CloseNoteFile)))
(* * rg 1/15/87 Moved code to NIL out NoteFile stream on close so that it does it for all 
NoteFiles, not just local)

(* * changes to NCDATABASE)

(DEFINEQ

(NC.CloseNoteFile
  [LAMBDA (NoteFile InterestedWindow Don'tCheckOperationInProgressFlg QuietFlg)
                                                             (* Randy.Gobbel "15-Jan-87 18:19")

          (* * Close a NoteFIle)



          (* * rht 10/23/84: Now gives user option of closing and saving all open cards on the screen.)



          (* * rht 11/8/84: Put RESETLST around NC.CacheTitles call.)



          (* * rht 1/9/85: Clear the NC.UncachingNotCompleted variable when close successfully completes.)



          (* * rht 1/31/85: Added call to checkpoint database. That in turn dumps the next nodeID and next linkID.)



          (* * rht 7/14/85: Replaced the call to reset the main menu with call to NC.ResetMainMenu. Also took out redundant 
	  reset of PSA.Database, since NC.ForceDatabaseClose is doing that.)



          (* * fgh 10/16/85 removed call to CacheTypesAndTitles because uncacheing now done automatically by cache 
	  mechanism.)



          (* * fkr 10/29/85: Now kills caching process from database streamprop.)



          (* * fkr 11/8/85 Updated to handle new NoteFile object and new CardID scheme.)



          (* * kirk 23Jan86 Changed to use NC.AskYesOrNo)



          (* * rht 3/26/86: Now searches for active cards over whole notefile not just among cards up on screen.
	  Uses NC.MapCards.)



          (* * kirk 28Apr86 Now returns NoteFile if successful.)



          (* * fgh 5/2/86 Cleaned up. Ask user to confirm only if there are cards on the screen, not if there are active, but
	  not displayed ones. Added calls to the NC.CloseNoteFileFns before and after the closeing.)



          (* * fgh 5/26/86 Revamp for device vector implementation.)



          (* * kef 7/24/86: Changed the last expression at the end that smashes the NoteFile device out of the NoteFile data 
	  structure. This is so that the Interface will not bomb trying to apply an OPENP function with a NIL Device.)



          (* * kef 8/4/86: Added something to obtain the write lock on the parts of the active NoteCards that deactivating 
	  will release. This is also so that any changes may be written to the server.)



          (* * fgh 8/31/86 Reimplemented changes in system made since 5/23/86 conversion. Reimplemented changes include: 
	  (* * fgh 6/4/86 Fixed so that shrunken cards are counted as open when asking for confirmation when there are open 
	  cards on screen.) (* * fgh 6/13/86 Now checks for card operations in progress and kills them if necessary.) 
	  (* * fgh 6/25/86 Added NC.ProtectedNoteFileOperation macro call. Added Don'tCheckOperationInProgressFlg args.) 
	  (* * rht 7/4/86: Added check for readonly notefile.) (* * rht 7/13/86: Added QuietFlg arg. Note that this will 
	  cause open cards on the screen to be closed and saved without asking user for confirmation.) 
	  (* * rpr 11/13/86: After closing active cards, checks to see if any special cards were made active and closes 
	  them.))



          (* * pmi 12/22/86 Made test for open notefile consistent with other NoteFile operations (ie. Abort Checkpoint,))


    (DECLARE (GLOBALVARS NC.MsgDelay NC.CloseNoteFileFns))
    (if (NC.ReadOnlyNoteFileP NoteFile)
	then (NC.AbortSession NoteFile InterestedWindow)
      else
       (ALLOW.BUTTON.EVENTS)
       (PROG ((FullFileName (fetch (NoteFile FullFileName) of NoteFile))
		CardTotal ActiveCards ReturnValue (OperationMsg ""))
	       [OR InterestedWindow (if (type? MENU (fetch (NoteFile Menu) of NoteFile))
					  then (SETQ InterestedWindow (WFROMMENU
						     (fetch (NoteFile Menu) of NoteFile]

          (* * Make sure NF is open)


	       (if [NULL (ERSETQ (SETQ ReturnValue (NC.NoteFileOpenP NoteFile]
		   then (SETQ ReturnValue (QUOTE OpenPFailed)))
	       (if (NULL ReturnValue)
		   then 

          (* * NoteFile is not open.)


			  (NC.PrintMsg InterestedWindow T "Can't close a closed notefile."
					 (CHARACTER 13))
			  (DISMISS NC.MsgDelay)
			  (NC.ClearMsg InterestedWindow T)
			  (RETURN NIL)
		 elseif (NOT (type? NoteFile ReturnValue))
		   then 

          (* * Error return from NoteFileOpenPFn)


			  (if [NULL (ERSETQ (NC.ReportError NIL (CONCAT "OpenP test on " 
										  FullFileName 
										"failed because "
										  ReturnValue
										  (CHARACTER 13)
										  
							"OK to continue Close. ↑ to abort Close."]
			      then (RETURN ReturnValue)))

          (* * Check to see if this NoteFile is busy doing something else)


	       (if [AND (NULL Don'tCheckOperationInProgressFlg)
			    (PROCESSP (NC.NoteFileProp NoteFile (QUOTE ProcessInProgress]
		   then (NC.PrintOperationInProgressMsg InterestedWindow (QUOTE Close% NoteFile)
							    (NC.NoteFileProp NoteFile (QUOTE
										 OperationInProgress))
							    )
			  (RETURN NIL))

          (* * Do the rest under an operation in progress lock.)


	       (RETURN
		 (NC.ProtectedNoteFileOperation
		   NoteFile Close% NoteFile
		   (PROG NIL
		           (RESETSAVE NIL (BQUOTE (NC.ClearMsg , InterestedWindow T)))

          (* * Delete the types and titles caching process if still alive. Have to do it now in order to make checking 
	  operations that follow suitably efficient. Note its a bit too early since we can still cancel this close.
	  But any harm done is loss of speed if NoteFile remains open when close iss cancelled.)


		           (DEL.PROCESS (fetch (NoteFile CachingProcess) of NoteFile))

          (* * See if any cards have operations in progress. If so, kill them after confirming with user.)


		           (OR QuietFlg (NC.PrintMsg InterestedWindow T (OR OperationMsg "")
							 
						   "Checking for card operations in progress ..."
							 (CHARACTER 13)))
		           (if (EQ (QUOTE ABORT)
				       (NC.CardOperationsInProgress NoteFile T))
			       then (RETURN NIL))
		           (NC.ClearMsg InterestedWindow NIL)

          (* * If NULL QuietFlg then look for cards on the screen. If there are active cards ask the user if they still want 
	  to close. When there's a non-NIL QuietFlg we just close the active cards.)


		           (if
			     (AND (NULL QuietFlg)
				    [for Window in (OPENWINDOWS)
				       thereis
					(LET (Card)
					     (AND [SETQ Card
						      (OR (NC.CardFromWindow Window)
							    (AND (WINDOWP (WINDOWPROP
										Window
										(QUOTE ICONFOR)))
								   (NC.CardFromWindow
								     (WINDOWPROP Window
										   (QUOTE ICONFOR]
						    (NC.SameNoteFileP NoteFile (fetch
									  (Card NoteFile)
										    of Card]
				    (NULL (NC.AskYesOrNo (CONCAT 
					"There are still cards on the screen from this NoteFile "
								       FullFileName "." (CHARACTER
									 13)
								       
								  "Want to close and save them? ")
							     " -- "
							     (QUOTE Yes)
							     NIL InterestedWindow NIL NIL)))
			       then (RETURN NIL))

          (* * Run through CloseNoteFileFns with param of BEFORE. Exit if any returns DON'T)


		           (if [for Function in NC.CloseNoteFileFns
				    thereis (OR (EQ Function (QUOTE DON'T))
						    (EQ (QUOTE DON'T)
							  (APPLY* Function NoteFile (QUOTE BEFORE]
			       then (RETURN NIL))

          (* * Close all the active cards)


		           (OR QuietFlg (NC.PrintMsg InterestedWindow T (OR OperationMsg "")
							 "Checking for active cards ..."
							 (CHARACTER 13)))
		           [if (SETQ ActiveCards (NC.MapCards NoteFile (FUNCTION [LAMBDA (Card)
								      Card])
								  (FUNCTION NC.ActiveCardP)))
			       then (if (NULL QuietFlg)
					  then (NC.PrintMsg InterestedWindow T 
							   "Closing and saving active cards ... "))
				      (RESETLST (RESETSAVE NC.ForceSourcesFlg NIL)
						  (RESETSAVE NC.ForceFilingFlg NIL)
						  (RESETSAVE NC.ForceTitlesFlg NIL)
						  (NC.CloseListOfActiveCards ActiveCards 
									       InterestedWindow 
									       QuietFlg)
						  (NC.CloseListOfActiveCards (for Card
										  in (
									     NC.FetchSpecialCards
											 NoteFile)
										  when
										   (NC.ActiveCardP
										     Card)
										  collect Card)
									       InterestedWindow 
									       QuietFlg))
				      (OR QuietFlg (NC.PrintMsg InterestedWindow NIL "Done."
								    (CHARACTER 13]

          (* * Checkpoint the NoteFile.)


		           (OR QuietFlg (NC.PrintMsg InterestedWindow T "Closing Notefile ... "
							 (CHARACTER 13)))
		           (if [NULL (CAR (ERSETQ (SETQ ReturnValue
							    (NC.CheckpointNoteFile NoteFile 
										     QuietFlg T T T 
										 InterestedWindow 
										     OperationMsg]
			       then (SETQ ReturnValue (QUOTE CheckpointFailed)))

          (* * Process error returns from in NC.CheckpointNoteFile)


		           (if (NOT (type? NoteFile ReturnValue))
			       then (if [NULL (ERSETQ (NC.ReportError NIL
										(CONCAT
										  
									"Checkpoint of NoteFile "
										  FullFileName 
									       " failed because "
										  ReturnValue "."
										  (CHARACTER 13)
										  
							"OK to continue Close. ↑ to abort Close."]
					  then (RETURN ReturnValue)))

          (* * Close the file.)


		           (if [NULL (CAR (ERSETQ (SETQ ReturnValue
							    (APPLY* (fetch (NoteFile 
										  CloseNoteFileFn)
									 of NoteFile)
								      NoteFile InterestedWindow]
			       then (SETQ ReturnValue (QUOTE CloseFailed)))

          (* * Process error returns from the close.)


		           [if (NOT (type? NoteFile ReturnValue))
			       then (SELECTQ
					ReturnValue
					(NoteFileNotOpen
					  (if [NULL (ERSETQ (NC.ReportError
								    NIL
								    (CONCAT "NoteFile" FullFileName 
									      " is not open."
									      (CHARACTER 13)
									      
							"OK to continue Close. ↑ to abort Close."]
					      then (RETURN ReturnValue)))
					(PROGN [ERSETQ (NC.ReportError NIL
									     (CONCAT 
									     "Close of NoteFile "
										       FullFileName 
									       " failed because "
										       ReturnValue 
										       "."
										       (CHARACTER
											 13]
						 (RETURN ReturnValue]

          (* * Run through CloseNoteFileFns with param of AFTER. Stop if any returns DON'T)


		           [for Function in NC.CloseNoteFileFns
			      thereis (EQ (QUOTE DON'T)
					      (APPLY* Function NoteFile (QUOTE AFTER]

          (* * Reset the interface and notify the user.)


		           (NC.ResetNoteFileInterface NoteFile)
		           (OR QuietFlg (NC.PrintMsg InterestedWindow T FullFileName " closed."))

          (* * * Cleanup a bit.)

                                                             (* Clean off the card cache's)
		           (ADD.PROCESS (LIST (FUNCTION NC.CleanupCardObjects)
						  (fetch (NoteFile HashArray) of NoteFile)))
                                                             (* Clean off the NoteFile object to remove any 
							     circularities.)
		           (create NoteFile smashing NoteFile Stream ← NIL UID ←
							 (fetch (NoteFile UID) of NoteFile)
							 FullFileName ← FullFileName Menu ←
							 (fetch (NoteFile Menu) of NoteFile)
							 NoteFileDevice ← (fetch (NoteFile 
										   NoteFileDevice)
									     of NoteFile))

          (* * Return the NF)


		           (RETURN NoteFile])
)
(PUTPROPS RGPATCH009 COPYRIGHT ("Xerox Corporation" 1987))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (603 13008 (NC.CloseNoteFile 613 . 13006)))))
STOP