(FILECREATED "13-Nov-86 13:08:38" {QV}<NOTECARDS>1.3K>NEXT>RHTPATCH145.;1 14639  

      changes to:  (VARS RHTPATCH145COMS)
		   (FNS NC.CloseListOfActiveCards))


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

(PRETTYCOMPRINT RHTPATCH145COMS)

(RPAQQ RHTPATCH145COMS ((* * Fix to bug reported by John Tang whereby new cards weren't filed in 
			     ToBeFiled at notefile close time.)
			  (* * Change to NCDATABASE)
			  (FNS NC.CloseNoteFile)
			  (* * New function for NCDATABASE)
			  (FNS NC.CloseListOfActiveCards)))
(* * Fix to bug reported by John Tang whereby new cards weren't filed in ToBeFiled at notefile
 close time.)

(* * Change to NCDATABASE)

(DEFINEQ

(NC.CloseNoteFile
  (LAMBDA (NoteFile InterestedWindow Don'tCheckOperationInProgressFlg QuietFlg)
                                                             (* rht: "13-Nov-86 13:05")

          (* * 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.))


    (DECLARE (GLOBALVARS NC.CloseNoteFileFns))
    (if (NC.ReadOnlyNoteFileP NoteFile)
	then (NC.AbortSession NoteFile InterestedWindow)
      else
       (ALLOW.BUTTON.EVENTS)
       (PROG (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 (APPLY* (fetch (NoteFile 
										  NoteFileOpenPFn)
									of NoteFile)
								     NoteFile))))
		   then (SETQ ReturnValue (QUOTE OpenPFailed)))
	       (if (NULL ReturnValue)
		   then 

          (* * NoteFile is not open.)


			  (NC.PrintMsg InterestedWindow T (fetch (NoteFile FullFileName)
							       of NoteFile)
					 " is not an open NoteFile!!!"
					 (CHARACTER 13))
			  (RETURN NIL)
		 elseif (NOT (type? NoteFile ReturnValue))
		   then 

          (* * Error return from NoteFileOpenPFn)


			  (if (NULL (ERSETQ (NC.ReportError NIL (CONCAT
								      "OpenP test on "
								      (fetch (NoteFile FullFileName)
									 of NoteFile)
								      "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 "
								       (fetch (NoteFile 
										     FullFileName)
									  of NoteFile)
								       "."
								       (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)
						  (NC.CloseListOfActiveCards (for Card
										  in (
									     NC.FetchSpecialCards
											 NoteFile)
										  when
										   (NC.ActiveCardP
										     Card)
										  collect Card)
									       InterestedWindow))
				      (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 "
									  (fetch (NoteFile 
										     FullFileName)
									     of NoteFile)
									  " 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"
									(fetch (NoteFile 
										     FullFileName)
									   of NoteFile)
									" is not open."
									(CHARACTER 13)
									
							"OK to continue Close. ↑ to abort Close."))))
					then (RETURN ReturnValue)))
				  (PROGN (ERSETQ (NC.ReportError NIL
								       (CONCAT "Close of NoteFile "
										 (fetch
										   (NoteFile 
										     FullFileName)
										    of NoteFile)
										 " 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 (fetch (NoteFile 
										     FullFileName)
									       of NoteFile)
							 " 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 UID ←(fetch (NoteFile UID)
									  of NoteFile)
							 FullFileName ←(fetch (NoteFile 
										     FullFileName)
									  of NoteFile)
							 Menu ←(fetch (NoteFile Menu) of NoteFile)
							 NoteFileDevice ←(fetch (NoteFile 
										   NoteFileDevice)
									    of NoteFile))

          (* * Return the NF)


		           (RETURN NoteFile))))))))
)
(* * New function for NCDATABASE)

(DEFINEQ

(NC.CloseListOfActiveCards
  (LAMBDA (ActiveCards InterestedWindow)                     (* rht: "13-Nov-86 13:01")

          (* * Close a list of active cards.)


    (for Card in ActiveCards bind Window
       do (for CardPart in (QUOTE (SUBSTANCE TOLINKS GLOBALTOLINKS PROPLIST))
	       do (OR (NC.ApplyFn ObtainWritePermissionFn Card CardPart)
			  (until (NC.ApplyFn ObtainWritePermissionFn Card CardPart)
			     do (OR QuietFlg (NC.PrintMsg InterestedWindow T
								(CONCAT 
						    "Waiting to obtain write permission for the "
									  CardPart " on card "
									  (NC.FetchTitle Card)
									  "...")))
				  (BLOCK)
			     finally (OR QuietFlg (NC.PrintMsg InterestedWindow NIL "done.")))))
	    (NC.QuitCard Card T NIL T T InterestedWindow NIL QuietFlg)
	    (if (SETQ Window (NC.FetchWindow Card))
		then (bind (Process ←(WINDOWPROP Window (QUOTE PROCESS)))
			  until (OR (NULL Process)
					(PROCESS.FINISHEDP Process))
			  do (BLOCK))))))
)
(PUTPROPS RHTPATCH145 COPYRIGHT ("Xerox Corporation" 1986))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (703 13392 (NC.CloseNoteFile 713 . 13390)) (13433 14557 (NC.CloseListOfActiveCards 13443
 . 14555)))))
STOP