(FILECREATED " 6-Nov-86 12:47:59" {QV}<NOTECARDS>1.3K>NEXT>RHTPATCH137.;3 15767  

      changes to:  (FNS NC.ConvertNoteFileVersion2To3 NC.ProcessNoteFileNeedsConversionError)
		   (VARS RHTPATCH137COMS)

      previous date: " 6-Nov-86 12:05:15" {QV}<NOTECARDS>1.3K>NEXT>RHTPATCH137.;1)


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

(PRETTYCOMPRINT RHTPATCH137COMS)

(RPAQQ RHTPATCH137COMS ((* * Changes to converter so that user link labels won't get tossed.)
			  (FILES (FROM NOTECARDS)
				 NCCONVERTVERSION2TO3)
			  (* * Changes to NCCONVERTVERSION2TO3)
			  (FNS NC.GetAndPutVersion2Card NC.ConvertNoteFileVersion2To3)
			  (* * Add following to coms of NCCONVERTVERSION2TO3)
			  (P (NC.StoreAutoloadFnFile (FUNCTION NC.ScavengeDatabaseFile)
						     (QUOTE NCREPAIR)
						     (QUOTE NOTECARDSDIRECTORIES)))
			  (* * Changes to NCLOCALDEVICE)
			  (FNS NCLocalDevice.ConvertNoteFileFormat)
			  (* * Changes to NCDATABASE)
			  (FNS NC.ProcessNoteFileNeedsConversionError)))
(* * Changes to converter so that user link labels won't get tossed.)

(FILESLOAD (FROM NOTECARDS)
	   NCCONVERTVERSION2TO3)
(* * Changes to NCCONVERTVERSION2TO3)

(DEFINEQ

(NC.GetAndPutVersion2Card
  (LAMBDA (Stream ID Version2HashArray FromNoteFile ToNoteFile)
                                                             (* rht: " 6-Nov-86 11:54")

          (* * If ID has active status, then get its card parts off Stream and fill in the info into the corresponding card 
	  object. Find this object using Version2HashArray.)



          (* * rht 12/18/85: Now deactivates card after putting to file.)



          (* * fgh 5/20/86 Added extra Version2HashArray arg to call to NC.ReadVersion2Links to support GlobalLinkConversion.
	  Changed order in which Links and PropList are processed because global link conversion might need to put a NoSource
	  property on the prop list.)



          (* * rht 8/27/86: Now sets status to ACTIVE before reading card parts so that user code calling progintface 
	  functions will work okay, i.e. that card will be NC.ValidCardP.)



          (* * rht 11/6/86: Now writes down link labels card to notefile.)


    (LET ((Pointers (NC.ReadVersion2Ptrs ID Stream))
	  Card)
         (SELECTQ (fetch (POINTERLIST STATUS) of Pointers)
		    (ACTIVE                                  (* Recover card object from the hash array.)
			    (SETQ Card (GETHASH ID Version2HashArray))

          (* * Set status to be active.)


			    (NC.SetStatus Card (QUOTE ACTIVE))

          (* * Read main data and substance and fill in Card object.)


			    (SETFILEPTR Stream (fetch (POINTERLIST MAINPTR) of Pointers))
			    (NC.ReadVersion2MainCardData Stream ID Card Version2HashArray 
							   FromNoteFile ToNoteFile)

          (* * Read title and fill in Card object.)


			    (SETFILEPTR Stream (fetch (POINTERLIST TITLEPTR) of Pointers))
			    (NC.ReadVersion2Title Stream ID Card)

          (* * Read prop list and fill in Card object.)


			    (SETFILEPTR Stream (fetch (POINTERLIST PROPSPTR) of Pointers))
			    (NC.ReadVersion2PropList Stream ID Card)

          (* * Read global links and fill in Card object.)


			    (SETFILEPTR Stream (fetch (POINTERLIST LINKSPTR) of Pointers))
			    (NC.ReadVersion2Links Stream ID Card Version2HashArray)

          (* * If card is a browser, then first convert to version 3 format.)


			    (if (NC.IsSubTypeOfP (NC.FetchType Card)
						     (QUOTE Browser))
				then (NC.ConvertVersion2BrowserCard Card Version2HashArray))

          (* * Write down the card parts to the ToNoteFile.)


			    (NC.PutMainCardData Card NIL T)
			    (NC.PutLinks Card T)
			    (NC.PutTitle Card T)
			    (NC.PutPropList Card T))
		    (SPECIAL                                 (* Deal specially with link labels.)
			     (SETQ Card (NC.Version3CardFromVersion2ID ID Version2HashArray))
                                                             (* Go get the link labels from version 2 stream and 
							     write down to version 3 notefile.)
			     (SETFILEPTR Stream (fetch (POINTERLIST MAINPTR) of Pointers))
			     (NC.StoreLinkLabels ToNoteFile (NC.ReadVersion2LinkLabels Stream ID 
											   Card))

          (* * Write down the linklabels card to the ToNoteFile.)


			     (NC.PutMainCardData Card NIL T))
		    NIL)
         (AND (NC.CardP Card)
		(NC.DeactivateCard Card T))
     Card)))

(NC.ConvertNoteFileVersion2To3
  (LAMBDA (StreamOrFileName InterestedWindow)                (* rht: " 6-Nov-86 12:47")

          (* * Convert this notefile to version 3 outputting the version 3 notefile to new version with same filename.)



          (* * fgh 12/17/85 fixed call to CreateDatabaseFile to lop the version off of the file name.)



          (* * rht 12/18/85: Now clears Version2HashArray in case uncollectable garbage is left around.)



          (* * rht 5/7/86: No longer just passes IndexSizeInEntries on to NC.CreateDatabaseFile. Uses NextIndexNum and 
	  NC.DefaultIndexSizeInEntries to compute a reasonable hasharray size (hopefully smaller than 10240, the largest 
	  allowable.))



          (* * rht 7/16/86: Added InterestedWindow arg.)



          (* * rht 11/6/86: Now autoload apply's NC.ScavengeDatabaseFile in case NCREPAIR not loaded.)


    (DECLARE (GLOBALVARS NC.DefaultIndexSizeInEntries))
    (LET ((OperationMsg "Converting file to version 3.")
	  CardTotal FileName FromStream FromNoteFile ToNoteFile Version2HashArray Version 
	  NextIndexNum IndexSizeInEntries NextLinkNum CheckptPtr ToFileName)
         (if (STREAMP StreamOrFileName)
	     then (SETQ FromStream StreamOrFileName)
		    (SETQ FileName (FULLNAME StreamOrFileName))
	   else (SETQ FileName StreamOrFileName)
		  (SETQ FromStream (OPENSTREAM FileName (QUOTE BOTH))))
         (SETFILEPTR FromStream 7)
         (if (EQ (SETQ Version (NC.ReadPtr FromStream 1))
		     2)
	     then                                          (* FromStream is indeed version 2.0)
                                                             (* Get all the header info off the 1.2 stream.)
		    (SETFILEPTR FromStream 0)
		    (SETQ NextIndexNum (NC.ReadPtr FromStream 2))
		    (SETQ CardTotal (SUB1 NextIndexNum))
		    (SETQ IndexSizeInEntries (NC.ReadPtr FromStream 2))
		    (SETQ NextLinkNum (NC.ReadPtr FromStream 3)) 
                                                             (* Skip version number.)
		    (NC.ReadPtr FromStream 1)
		    (SETQ CheckptPtr (NC.ReadPtr FromStream 3)) 

          (* * Create a dummy notefile object for the 1.2 source notefile.)


		    (SETQ FromNoteFile (create NoteFile
						   UID ←(NC.MakeUID)
						   Stream ← FromStream
						   FullFileName ←(FULLNAME FileName)
						   NextIndexNum ← NextIndexNum
						   Version ← Version
						   NextLinkNum ← NextLinkNum
						   CheckptPtr ← CheckptPtr))
		    (SETQ ToNoteFile (NC.CreateDatabaseFile (PACKFILENAME (QUOTE VERSION)
										NIL
										(QUOTE BODY)
										FileName)
								(MAX NC.DefaultIndexSizeInEntries
								       (TIMES 2 NextIndexNum))
								OperationMsg T NIL NIL 
								InterestedWindow))
		    (SETQ ToFileName (fetch (NoteFile FullFileName) of ToNoteFile))
		    (if (AND (type? NoteFile ToNoteFile)
				 (NC.OpenDatabaseFile ToNoteFile NIL T T NIL NIL NIL NIL T NIL 
							InterestedWindow))
			then 

          (* * Fill in NewNoteFile's hash array with new UIDs. Return a hash array mapping version 2 style ID atoms to 
	  version 3 style Cards.)


			       (NC.PrintMsg InterestedWindow T 
					      "Reading index of version 2 notefile ...")
			       (SETQ Version2HashArray (NC.BuildVersion2HashArray FromNoteFile 
										      ToNoteFile 
										      CardTotal))

          (* * For each card in the old notefile, fill in a version 3 card object for it and put the filled in card down to 
	  NewNoteFile.)


			       (NC.PrintMsg InterestedWindow T OperationMsg (CHARACTER 13)
					      "Processing card ID 1 out of " CardTotal)
			       (for IDNum from 1 to CardTotal eachtime (BLOCK)
				  do (if (ZEROP (REMAINDER IDNum 10))
					   then (NC.PrintMsg InterestedWindow T OperationMsg
								 (CHARACTER 13)
								 "Processing card ID " IDNum 
								 " out of "
								 CardTotal))
				       (NC.GetAndPutVersion2Card FromStream (NC.IDFromNumber
								     IDNum)
								   Version2HashArray FromNoteFile 
								   ToNoteFile))
                                                             (* Clear hash array to be sure uncollectable garbage 
							     isn't left around.)
			       (CLRHASH Version2HashArray)
			       (NC.CheckpointDatabase ToNoteFile T)
			       (NC.ForceDatabaseClose ToNoteFile)
			       (CLOSEF FromStream)         (* Rebuild To and From links.)
			       (NC.AutoloadApply* (FUNCTION NC.ScavengeDatabaseFile)
						  ToNoteFile NIL NIL NIL InterestedWindow)
			       (NC.PrintMsg InterestedWindow T "Done.")
			       ToFileName)
	   else (NC.ReportError "NC.ConvertNoteFileVersion2To3" (CONCAT "Filename " FileName 
									      " is version "
									      Version 
						       ".  Can only convert version 2 notefiles."))
		  NIL))))
)
(* * Add following to coms of NCCONVERTVERSION2TO3)

(NC.StoreAutoloadFnFile (FUNCTION NC.ScavengeDatabaseFile)
			(QUOTE NCREPAIR)
			(QUOTE NOTECARDSDIRECTORIES))
(* * Changes to NCLOCALDEVICE)

(DEFINEQ

(NCLocalDevice.ConvertNoteFileFormat
  (LAMBDA (NoteFile PromptWindow)                            (* rht: " 6-Nov-86 12:00")

          (* * Convert a NoteFile from old version to curretn version format.)



          (* * fgh 5/25/86 First created on basis of old NC.CheckForNeededConversion.)



          (* * rht 11/6/86: Now passes an InterestedWindow argument to the converter.)


    (PROG ((Stream (fetch (NoteFile Stream) of NoteFile))
	     (Version (fetch (NoteFile Version) of NoteFile))
	     (FullFileName (fetch (NoteFile FullFileName) of NoteFile))
	     NewFileName)

          (* * Check if NoteFile is open. If so, its an error.)


	    (if (OPENP FullFileName)
		then (RETURN (QUOTE NoteFileAlreadyOpen)))

          (* * Is Version a sensible version number? If not, then just report an error.)


	    (if (OR (NOT (NUMBERP Version))
			(GEQ Version (CONSTANT (fetch (NoteFileVersion Version)
						      of (NCLocalDevice.CurrentVersion)))))
		then (RETURN (QUOTE BadVersionNumber)))

          (* * Is this a really old version. If so then we can't handle it.)


	    (if (LEQ Version 1)
		then (if (WINDOWP PromptWindow)
			   then (NC.PrintMsg PromptWindow T "Notefile " FullFileName 
						 " is  too old (version "
						 Version ")." (CHARACTER 13)
						 "  It must first be converted to version 2" 
						 "by opening in NoteCards release 1.2i."
						 (CHARACTER 13)
						 " Then we convert to version 3."
						 (CHARACTER 13)))
		       (RETURN (QUOTE NoteFileTooOld)))

          (* * Okay convert the NoteFile.)


	    (if (CAR (ERSETQ (SETQ NewFileName (NC.AutoloadApply* (FUNCTION 
								    NC.ConvertNoteFileVersion2To3)
									  FullFileName PromptWindow)))
			 )
		then 

          (* * Conversion went okay. Return the NoteFile with the new fullfilename.)


		       (replace (NoteFile FullFileName) of NoteFile with NewFileName)
		       (RETURN NoteFile)
	      else 

          (* * something went wrong during the conversion. return an error indicator.)


		     (CLOSEF? (fetch (NoteFile FullFileName) of NoteFile))
		     (RETURN (QUOTE NoteFileConversionFailed))))))
)
(* * Changes to NCDATABASE)

(DEFINEQ

(NC.ProcessNoteFileNeedsConversionError
  (LAMBDA (NoteFile NoteFilesHashArray Don'tCacheTypesAndTitlesFlg Don'tCreateFlg 
		    Convertw/oConfirmFlg Don'tCreateArrayFlg Can'tTruncateFlg Don'tCreateInterfaceFlg 
		    Don'tGetSpecialCardsFlg PromptWindow PublicOrPrivate 
		    Don'tCheckOperationsInProgressFlg MenuPosition QuietFlg ReadOnlyFlg 
		    Don'tCheckForTruncationFlg)              (* rht: " 6-Nov-86 12:03")

          (* * Discovered NoteFile needs version conversion when attempted to open it. Process that error.)



          (* * fgh 5/23/86 First created.)



          (* * fgh 9/1/86 Updated args to match NC.OpenNoteFile.)



          (* * rht 10/31/86: Added Don'tCheckForTruncationFlg arg.)



          (* * 11/6/86: Now passes PromptWindow down to conversion fn.)


    (LET (ConversionFn ReturnValue)

          (* * If appropriate, notify the user.)


         (if (WINDOWP PromptWindow)
	     then (NC.PrintMsg PromptWindow T "NoteFile " (fetch (NoteFile FullFileName)
								 of NoteFile)
				   "is an old format file."
				   (CHARACTER 13)))

          (* * If the device has a ConversionFn, then apply it and try to Open the NoteFile.)


         (if (SETQ ConversionFn (fetch (NoteFile ConvertNoteFileFormatFn) of NoteFile))
	     then 

          (* * ask the user if conversion should be done. If so, do it, else return an error msg,)


		    (if (OR Convertw/oConfirmFlg (NC.AskYesOrNo 
						  "Do you want to convert it to the new format? "
								      " -- " "Y" NIL PromptWindow))
			then 

          (* * go ahead and convert.)


			       (if (type? NoteFile (CAR (ERSETQ (SETQ ReturnValue
									  (APPLY* ConversionFn 
										    NoteFile 
										    PromptWindow)))))
				   then 

          (* * Conversion successful, Open the note file.)


					  (NC.OpenNoteFile NoteFile NoteFilesHashArray 
							     Don'tCacheTypesAndTitlesFlg 
							     Don'tCreateFlg Convertw/oConfirmFlg 
							     Don'tCreateArrayFlg Can'tTruncateFlg 
							     Don'tCreateInterfaceFlg 
							     Don'tGetSpecialCardsFlg PromptWindow 
							     PublicOrPrivate T MenuPosition QuietFlg 
							     ReadOnlyFlg Don'tCheckForTruncationFlg)
				 else 

          (* * Error during conversion fn. report and get out of here.)


					(SELECTQ ReturnValue
						   (PROGN (ERSETQ
							      (NC.ReportError
								NIL
								(CONCAT 
						"NoteFile format conversion failed for NoteFile "
									  (fetch (NoteFile 
										     FullFileName)
									     of NoteFile))))
							    (QUOTE NoteFileConversionFailed))))
		      else 

          (* * User said don't convert. Just return)


			     (QUOTE NoteFileNeedsConversion))
	   else 

          (* * No conversion fn. Tell the user if appropriate and return an error msg.)


		  (if (WINDOWP PromptWindow)
		      then (NC.PrintMsg PromptWindow NIL "No format conversion possible."
					    (CHARACTER 13)
					    "See a NoteCards wizard."
					    (CHARACTER 13)))
		  (QUOTE NoteFileNeedsConversion)))))
)
(PUTPROPS RHTPATCH137 COPYRIGHT ("Xerox Corporation" 1986))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (1197 9799 (NC.GetAndPutVersion2Card 1207 . 4693) (NC.ConvertNoteFileVersion2To3 4695 . 
9797)) (10007 12391 (NCLocalDevice.ConvertNoteFileFormat 10017 . 12389)) (12426 15685 (
NC.ProcessNoteFileNeedsConversionError 12436 . 15683)))))
STOP