--ILTLoginImpl.mesa
--Created by
--   JFung.PASA	  	29-Sep-83 14:53:01
-- Modified version taken from Profile.mesa

--last edited by
--   JFung.PASA	   	  7-Oct-83  9:58:07


DIRECTORY
     FormSW USING [Display, FindItem, ItemHandle, ProcType],
     Heap USING [systemZone],
     LispToolOps,
     Process,
     Profile USING [
          debugging, GetDefaultDomain, GetDefaultOrganization, GetDefaultRegistry,
          GetLibrarian, GetLibrarianNames, GetUser, SetDebugging, SetDefaultDomain,
          SetDefaultOrganization, SetDefaultRegistry, SetLibrarian, SetUser],
     Put,
     String USING [CopyToNewString, EquivalentString, Replace],
     Supervisor USING [AgentProcedure, CreateSubsystem, SubsystemHandle];



ILTLoginImpl: PROGRAM
     IMPORTS FormSW, Heap, LispToolOps, Process, Profile, Put, String

     EXPORTS LispToolOps =

     BEGIN OPEN ILT: LispToolOps;


     z: UNCOUNTED ZONE = Heap.systemZone;

     ProfileItem: TYPE = {
          apply, user, password, registry, abort, domain, organization, debug,
          librarian, prefix, suffix};  -- if the positions of apply  and abort change, you must edit ToggleCommands

     dirty: PACKED ARRAY ProfileItem OF BOOLEAN ← ALL[FALSE];


     ProfileFields: TYPE = RECORD [
          userName: LONG STRING ← NIL,
          userPassword: LONG STRING ← NIL,
          domain: LONG STRING ← NIL,
          organization: LONG STRING ← NIL,
          registry: LONG STRING ← NIL,
          librarianName: LONG STRING ← NIL,
          librarianNamePrefix: LONG STRING ← NIL,
          librarianNameSuffix: LONG STRING ← NIL];


     ProfileData: TYPE = RECORD [
          --formSW: Window.Handle ← NIL,
          debugging: BOOLEAN ← NULL,
          strings: ARRAY ProfileItem OF LONG STRING ← ALL[NIL],
          busy: BOOLEAN ← FALSE];


     --agent: Supervisor.SubsystemHandle = Supervisor.CreateSubsystem[Notify];


     data: LONG POINTER TO ProfileData ← NIL;
     debug: BOOLEAN ← FALSE;

     profileInfo: ProfileFields ← [];


     GetUser: PROCEDURE [name, password: LONG STRING] = {
          profileInfo.userName ← String.CopyToNewString[name, z];
          profileInfo.userPassword ← String.CopyToNewString[password, z]};


     GetDefaultDomain: PROCEDURE [domain: LONG STRING] = {
          profileInfo.domain ← String.CopyToNewString[domain, z]};


     GetDefaultOrganization: PROCEDURE [organization: LONG STRING] = {
          profileInfo.organization ← String.CopyToNewString[organization, z]};


     GetDefaultRegistry: PROCEDURE [registry: LONG STRING] = {
          profileInfo.registry ← String.CopyToNewString[registry, z]};


     GetLibrarian: PROCEDURE [librarian: LONG STRING] = {
          profileInfo.librarianName ← String.CopyToNewString[librarian, z]};


     GetLibrarianNames: PROCEDURE [prefix, suffix: LONG STRING] = {
          profileInfo.librarianNamePrefix ← String.CopyToNewString[prefix, z];
          profileInfo.librarianNameSuffix ← String.CopyToNewString[suffix, z]};



     GetProfileInfo: PROCEDURE = {
          Profile.GetUser[GetUser];
          Profile.GetDefaultDomain[GetDefaultDomain];
          Profile.GetDefaultOrganization[GetDefaultOrganization];
          Profile.GetDefaultRegistry[GetDefaultRegistry];
          Profile.GetLibrarianNames[GetLibrarianNames];
          Profile.GetLibrarian[GetLibrarian]};


     FreeProfileInfo: PROCEDURE =
          BEGIN
          z.FREE[@profileInfo.userName];
          profileInfo.userName ← NIL;
          z.FREE[@profileInfo.userPassword];
          profileInfo.userPassword ← NIL;
          z.FREE[@profileInfo.domain];
          profileInfo.domain ← NIL;
          z.FREE[@profileInfo.organization];
          profileInfo.organization ← NIL;
          z.FREE[@profileInfo.registry];
          profileInfo.registry ← NIL;
          z.FREE[@profileInfo.librarianName];
          profileInfo.librarianName ← NIL;
          z.FREE[@profileInfo.librarianNamePrefix];
          profileInfo.librarianNamePrefix ← NIL;
          z.FREE[@profileInfo.librarianNameSuffix];
          profileInfo.librarianNameSuffix ← NIL;
          END;



     DataChanged: FormSW.ProcType =
          BEGIN
          dirty[VAL[index]] ← TRUE;
          IF ~commandsShowing THEN {
               FormSW.FindItem[sw, ORD[ProfileItem.abort]].flags.invisible ← FALSE;
               FormSW.FindItem[sw, ORD[ProfileItem.apply]].flags.invisible ← FALSE;
               FormSW.Display[sw]};
          commandsShowing ← TRUE;
          END;


     commandsShowing: BOOLEAN ← FALSE;
     -- since everything is done in the Notifier, no monitor is needed to protect this boolean



     LoginProc: PUBLIC PROCEDURE =
          BEGIN

          data ← Heap.systemZone.NEW[ProfileData];
          IF debug THEN {
               Put.Line[ILT.toolData.fileSW, "Enter LoginProc..."L];
               --Process.Pause[Process.SecondsToTicks[5]];
               };

          data.strings[user] ← ILT.toolData.userName;
          data.strings[password] ← ILT.toolData.userPassword;
          data.strings[domain] ← ILT.toolData.domainName;
          data.strings[organization] ← ILT.toolData.organizationName;
          Apply;
          Heap.systemZone.FREE[@data];
          END;


     Apply: PROCEDURE =
          BEGIN
          ENABLE UNWIND => FreeProfileInfo[];

          IF debug THEN {
               Put.Line[ILT.toolData.fileSW, "Enter Apply..."L];
               --Process.Pause[Process.SecondsToTicks[5]];
               };

          IF data = NIL THEN RETURN;
          IF debug THEN {
               Put.Line[ILT.toolData.fileSW, "GetProfileInfo..."L];
               --Process.Pause[Process.SecondsToTicks[5]];
               };

          GetProfileInfo[];
          IF debug THEN {
               Put.Line[ILT.toolData.fileSW, "UpdatePrimary..."L];
               --Process.Pause[Process.SecondsToTicks[5]];
               };

          IF ~Equal[profileInfo.userName, user]
               OR ~Equal[profileInfo.userPassword, password] THEN UpdatePrimary[];

          <<  	IF data.debugging # Profile.debugging THEN
      		Profile.SetDebugging[data.debugging];
  >>
          IF debug THEN {
               Put.Line[ILT.toolData.fileSW, "SetDefaultDomain..."L];
               --Process.Pause[Process.SecondsToTicks[5]];
               };

          IF ~Equal[profileInfo.domain, domain] THEN
               Profile.SetDefaultDomain[data.strings[domain]];

          IF debug THEN {
               Put.Line[ILT.toolData.fileSW, "SetDefaultOrganization..."L];
               --Process.Pause[Process.SecondsToTicks[5]];
               };

          IF ~Equal[profileInfo.organization, organization] THEN
               Profile.SetDefaultOrganization[data.strings[organization]];

          IF debug THEN {
               Put.Line[ILT.toolData.fileSW, "SetDefaultRegistry..."L];
               --Process.Pause[Process.SecondsToTicks[5]];
               };

          IF ~Equal[profileInfo.registry, registry] THEN
               Profile.SetDefaultRegistry[data.strings[registry]];

          IF debug THEN {
               Put.Line[ILT.toolData.fileSW, "UpdateLibrarian..."L];
               --Process.Pause[Process.SecondsToTicks[5]];
               };

          IF ~Equal[profileInfo.librarianName, librarian]
               OR ~Equal[profileInfo.librarianNamePrefix, prefix]
               OR ~Equal[profileInfo.librarianNameSuffix, suffix] THEN
               UpdateLibrarian[];

          IF debug THEN {
               Put.Line[ILT.toolData.fileSW, "FreeProfileInfo..."L];
               --Process.Pause[Process.SecondsToTicks[5]];
               };

          FreeProfileInfo[];
          END;



     UpdatePrimary: PROC =
          BEGIN Profile.SetUser[data.strings[user], data.strings[password]]; END;



     UpdateLibrarian: PROC =
          BEGIN
          Profile.SetLibrarian[
               data.strings[librarian], data.strings[prefix], data.strings[
               suffix]];
          END;



     ResetData: PROC [display: BOOLEAN] =
          BEGIN
          ENABLE UNWIND => FreeProfileInfo[];
          IF data = NIL OR data.busy THEN RETURN;
          GetProfileInfo[];
          Update[profileInfo.userName, user, display];
          Update[profileInfo.userPassword, password, display];
          Update[profileInfo.registry, registry, display];
          Update[profileInfo.domain, domain, display];
          Update[profileInfo.organization, organization, display];
          Update[profileInfo.librarianName, librarian, display];
          Update[profileInfo.librarianNamePrefix, prefix, display];
          Update[profileInfo.librarianNameSuffix, suffix, display];
          <<
    	IF data.debugging # Profile.debugging THEN {
      	data.debugging ← Profile.debugging;
        IF display THEN FormSW.DisplayItem[data.formSW, ORD[ProfileItem.debug]]};
>>
          FreeProfileInfo[];
          END;



     Equal: PROC [s: LONG STRING, index: ProfileItem] RETURNS [BOOLEAN] =
          BEGIN RETURN[String.EquivalentString[data.strings[index], s]]; END;


     Update: PROC [profile: LONG STRING, index: ProfileItem, display: BOOLEAN] =
          BEGIN
          IF Equal[profile, index] THEN RETURN;
          String.Replace[@data.strings[index], profile, z];
          --IF display THEN FormSW.DisplayItem[data.formSW, ORD[index]];
          END;


     --Notify: Supervisor.AgentProcedure = {ResetData[display: TRUE]};




     END.