home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / ICAppSourceKit1.0 / ICDocument.p < prev    next >
Encoding:
Text File  |  1994-11-27  |  11.7 KB  |  514 lines  |  [TEXT/PJMM]

  1. unit ICDocument;
  2.  
  3. interface
  4.  
  5.     uses
  6.         ICTypes;
  7.  
  8.     function DoNewDoc: OSErr;
  9.     function DoOpenDoc (fss: FSSpec): OSErr;
  10.     function DoCloseDocWindow (wp: WindowPtr): OSErr;
  11.     function DoCloseDoc: OSErr;
  12.     function DoQuit: OSErr;
  13.  
  14.     function DoSFOpen: OSErr;
  15.     function DoSave: OSErr;
  16.     function DoSaveAs: OSErr;
  17.     function DoOpenInternetPreferences: OSErr;
  18.  
  19.     function IsDocOpen: boolean;
  20.     function IsDocDirty: boolean;
  21.     function IsDocNew: boolean;
  22.  
  23.     function GetDocumentName: Str255;
  24.     function GetInstance: ICInstance;
  25.     procedure DirtyDocument;
  26.     function IsDocLocked: boolean;
  27.  
  28.     function DoOpenApp: OSErr;
  29.  
  30.     function InitICDocument: OSErr;
  31.     procedure TermICDocument;
  32.  
  33. implementation
  34.  
  35.     uses
  36.         Folders, 
  37.  
  38.         ICStrH, 
  39.  
  40.         ICTypes, ICAPI, ICKeys, 
  41.  
  42.         ICGlobals, ICMiscSubs, ICSubs, ICDialogs, ICWindowGlobals, ICWindows, ICStandardFile;
  43.  
  44.     var
  45.         instance: ICInstance;
  46.         current_file: FSSpec;
  47.         current_seed: longInt;
  48.         scratch_file: FSSpec;            (* open if name <> '' *)
  49.         new_document: boolean;
  50.         dirty_document: boolean;
  51.         locked_document: boolean;
  52.  
  53.     function CopyFileSafe (source, dest: FSSpec): OSErr;
  54.         var
  55.             temp: FSSpec;
  56.             err, junk: OSErr;
  57.             count: integer;
  58.     begin
  59.         temp := dest;
  60.         count := 0;
  61.         repeat
  62.             temp.name := concat('Internet Config Temp ', DecStr(count));
  63.             err := HCreate(temp.vRefNum, temp.parID, temp.name, ICcreator, ICfiletype);
  64.             count := count + 1;
  65.         until err <> dupFNErr;
  66.         if err = noErr then begin
  67.             err := CopyFile(source, temp);
  68.             if err = noErr then begin
  69.                 junk := HDelete(dest.vRefNum, dest.parID, dest.name);
  70.                 err := HRename(dest.vRefNum, dest.parID, temp.name, dest.name);
  71.             end;
  72.         end;
  73.         CopyFileSafe := err;
  74.     end;
  75.  
  76.     function GetInstance: ICInstance;
  77.     begin
  78.         GetInstance := instance;
  79.     end; (* GetInstance *)
  80.  
  81.     function IsDocOpen: boolean;
  82.     begin
  83.         IsDocOpen := (scratch_file.name <> '');
  84.     end; (* IsDocOpen *)
  85.  
  86.     function IsDocDirty: boolean;
  87.     begin
  88.         IsDocDirty := IsDocOpen and dirty_document;
  89.     end; (* IsDocDirty *)
  90.  
  91.     function IsDocNew: boolean;
  92.     begin
  93.         IsDocNew := IsDocOpen and new_document;
  94.     end; (* IsDocNew *)
  95.  
  96.     procedure DirtyDocument;
  97.     begin
  98.         dirty_document := true;
  99.     end; (* DirtyDocument *)
  100.  
  101.     function IsDocLocked: boolean;
  102.     begin
  103.         IsDocLocked := IsDocOpen and locked_document;
  104.     end; (* IsDocLocked *)
  105.  
  106.     function GetDocumentName: Str255;
  107.     begin
  108.         if current_file.name = '' then begin
  109.             GetDocumentName := GetAString(128, 1);
  110.         end
  111.         else begin
  112.             GetDocumentName := current_file.name;
  113.         end; (* if *)
  114.     end; (* GetDocumentName *)
  115.  
  116.     procedure AddDefaultPrefs;
  117.  
  118.         function PrefExists (key: str255): boolean;
  119.             var
  120.                 attr: ICAttr;
  121.                 count: longInt;
  122.         begin
  123.             PrefExists := ICGetPref(instance, key, attr, nil, count) = noErr;
  124.         end;
  125.  
  126.         procedure SetPrefHandle (key: str255; h: handle);
  127.             var
  128.                 junk: ICError;
  129.         begin
  130.             if not PrefExists(key) then begin
  131.                 junk := ICSetPrefHandle(instance, key, ICattr_no_change, h);
  132.             end;
  133.         end;
  134.  
  135.         procedure SetPrefStr (key: str255; data: str255);
  136.             var
  137.                 junk: ICError;
  138.         begin
  139.             if not PrefExists(key) then begin
  140.                 junk := ICSetPrefStr(instance, key, ICattr_no_change, data);
  141.             end;
  142.         end;
  143.  
  144.         procedure SetPref (key: str255; data: ptr; size: longInt);
  145.             var
  146.                 junk: ICError;
  147.         begin
  148.             if not PrefExists(key) then begin
  149.                 junk := ICSetPref(instance, key, ICattr_no_change, data, size);
  150.             end;
  151.         end;
  152.  
  153.         procedure CopyServerPref (id: integer; key: Str255);
  154.             var
  155.                 tmph: Handle;
  156.         begin
  157.             tmph := GetResource('STR#', id);
  158.             if tmph <> nil then begin
  159.                 SetPrefHandle(concat(key, 'All'), tmph);
  160.                 SetPrefStr(concat(key, 'Preferred'), GetIndStrH(tmph, 1));
  161.             end; (* if *)
  162.         end; (* CopyServerPref *)
  163.  
  164.         procedure CopyPrefH (source_type: ResType; source_id: integer; dest_key: Str255);
  165.             var
  166.                 tmph: Handle;
  167.         begin
  168.             tmph := GetResource(source_type, source_id);
  169.             if tmph <> nil then begin
  170.                 SetPrefHandle(dest_key, tmph);
  171.             end; (* if *)
  172.         end; (* CopyPrefH *)
  173.  
  174.         procedure CopyHelpers;
  175.             var
  176.                 x: integer;
  177.                 key: Str255;
  178.                 appspec: ICAppSpec;
  179.                 i: integer;
  180.         begin
  181.             x := 1;
  182.             for i := 1 to CountStrs(666) div 3 do begin
  183.                 key := GetIndStr(666, x);
  184.                 x := x + 1;
  185.                 appspec.fCreator := copy(concat(GetIndStr(666, x), '    '), 1, 4);
  186.                 x := x + 1;
  187.                 appspec.name := GetIndStr(666, x);
  188.                 x := x + 1;
  189.                 SetPref(concat(kICHelper, key), @appspec, sizeof(appspec));
  190.             end; (* for *)
  191.         end; (* CopyHelpers *)
  192.  
  193.         var
  194.             err: OSErr;
  195.             junk: OSErr;
  196.     begin
  197.         CopyServerPref(200, 'Archie');
  198.         CopyServerPref(201, 'InfoMac');
  199.         CopyServerPref(202, 'UMich');
  200.         CopyPrefH('TaBl', 128, kICCharacterSet);
  201.         CopyPrefH('MAP ', 128, kICMapping);
  202.         CopyPrefH('STR ', 128, kICQuotingString);
  203.         CopyPrefH('SERV', 128, kICServices);
  204.         SetPrefStr(kICRealName, GetOwnerName);
  205.         CopyHelpers;
  206.     end;
  207.  
  208.     function CreateScratchFile (protofile: FSSpecPtr): OSErr;
  209.         var
  210.             err: OSErr;
  211.             count: integer;
  212.     begin
  213.         err := FindFolder(kOnSystemDisk, kTemporaryFolderType, kCreateFolder, scratch_file.vRefNum, scratch_file.parID);
  214.         if err = noErr then begin
  215. (*    scratch_file.parID := 2; *)
  216.             count := 0;
  217.             repeat
  218.                 scratch_file.name := concat('Internet Config Temp ', DecStr(count));
  219.                 err := HCreate(scratch_file.vRefNum, scratch_file.parID, scratch_file.name, ICcreator, ICfiletype);
  220.                 count := count + 1;
  221.             until err <> dupFNErr;
  222.         end; (* if *)
  223.         if err = noErr then begin
  224.             if protofile = nil then begin
  225.                 HCreateResFile(scratch_file.vRefNum, scratch_file.parID, scratch_file.name);
  226.                 (* temporary workaround bug in ICAPI *)
  227.             end
  228.             else begin
  229.                 err := CopyFile(protofile^, scratch_file);
  230.                 if err = noErr then begin
  231.                     err := HRstFLock(scratch_file.vRefNum, scratch_file.parID, scratch_file.name);
  232.                 end; (* if *)
  233.             end;
  234.         end;
  235.         if err = noErr then begin
  236.             err := ICMapErr(ICSpecifyConfigFile(instance, scratch_file));
  237.         end; (* if *)
  238.         if err = noErr then begin
  239.             err := ICMapErr(ICBegin(instance, icReadWritePerm));
  240.             if err = noErr then begin
  241.                 AddDefaultPrefs;
  242.                 err := ICMapErr(ICEnd(instance));
  243.             end; (* if *)
  244.         end; (* if *)
  245.         CreateScratchFile := err;
  246.     end; (* CreateScratchFile *)
  247.  
  248.     function DoCloseDoc: OSErr;
  249.         var
  250.             but: integer;
  251.             err: OSErr;
  252.             junk: OSErr;
  253.     begin
  254.         err := WindowsCloseAll;
  255.         if (err = noErr) & IsDocDirty then begin
  256.             ParamText(GetDocumentName, '', '', '');
  257.             InitCursor;
  258.             but := CautionAlert(135, @CancelDiscardModalFilter);
  259.             case but of
  260.                 ok:  begin
  261.                     err := DoSave;
  262.                 end;
  263.                 cancel: 
  264.                     err := userCanceledErr;
  265.                 otherwise
  266.             end; (* case *)
  267.         end; (* if *)
  268.         if err = noErr then begin
  269.             err := WindowsClose(windowinfo[WT_Main].window);
  270.         end; (* if *)
  271.         if err = noErr then begin
  272.             junk := HDelete(scratch_file.vRefNum, scratch_file.parID, scratch_file.name);
  273.             scratch_file.name := '';
  274.         end; (* if *)
  275.         DoCloseDoc := err;
  276.     end; (* DoCloseDoc *)
  277.  
  278.     function DoCloseDocWindow (wp: WindowPtr): OSErr;
  279.         var
  280.             wt: WindowType;
  281.             but: integer;
  282.             confirmed: boolean;
  283.             err: OSErr;
  284.     begin
  285.         err := noErr;
  286.         wt := GetWindowType(wp);
  287.         case wt of
  288.             WT_None: 
  289.                 ;
  290.             WT_About: 
  291.                 HideWindow(wp);
  292.             WT_Main: 
  293.                 err := DoCloseDoc;
  294.             otherwise
  295.                 err := WindowsClose(wp);
  296.         end; (* case *)
  297.         DoCloseDocWindow := err;
  298.     end; (* DoCloseDocWindow *)
  299.  
  300.     function DoNewDoc: OSErr;
  301.         var
  302.             err: OSErr;
  303.             junk: OSErr;
  304.     begin
  305.         err := DoCloseDoc;
  306.         if err = noErr then begin
  307.             WindowsResetPositions;
  308.             new_document := true;
  309.             dirty_document := false;
  310.             locked_document := false;
  311.             err := CreateScratchFile(nil);
  312.             if err = noErr then begin
  313.                 err := WindowsOpen(WT_Main, 200);
  314.             end;
  315.             if err = noErr then begin
  316.                 WindowsSetTitle(WT_Main, GetAString(128, 1));
  317.                 current_file.name := '';                        (* make it untitled *)
  318.             end; (* if *)
  319.         end; (* if *)
  320.         DoNewDoc := err;
  321.     end; (* DoNewDoc *)
  322.  
  323.     procedure CurrentSeed (var seed: longInt);
  324.         var
  325.             err: OSErr;
  326.     begin
  327.         seed := 0;
  328.         if current_file.name <> '' then begin
  329.             err := ICSpecifyConfigFile(instance, current_file);
  330.             if err = noErr then begin
  331.                 err := ICGetSeed(instance, seed);
  332.             end;
  333.             err := ICSpecifyConfigFile(instance, scratch_file);
  334.         end;
  335.     end;
  336.  
  337.     function SameSeed (seed1, seed2: longInt): boolean;
  338.     begin
  339.         SameSeed := (seed1 = seed2) or (seed1 = 0) or (seed2 = 0);
  340.     end;
  341.  
  342.     function DoOpenDoc (fss: FSSpec): OSErr;
  343.         var
  344.             count: integer;
  345.             err: OSErr;
  346.             junk: OSErr;
  347.     begin
  348.         err := DoCloseDoc;
  349.         if err = noErr then begin
  350.             new_document := false;
  351.             dirty_document := false;
  352.             locked_document := FileLocked(fss);
  353.             err := CreateScratchFile(@fss);
  354.             if err = noErr then begin
  355.                 WindowsRestorePositions;
  356.                 err := WindowsOpen(WT_Main, 200);
  357.             end;
  358.             if err = noErr then begin
  359.                 WindowsSetTitle(WT_Main, fss.name);
  360.                 current_file := fss;
  361.             end; (* if *)
  362.             CurrentSeed(current_seed);
  363.         end; (* if *)
  364.         DoOpenDoc := err;
  365.     end; (* DoOpenDoc *)
  366.  
  367.     function DoQuit: OSErr;
  368.         var
  369.             err: OSErr;
  370.     begin
  371.         err := DoCloseDoc;
  372.         if err = noErr then begin
  373.             quitNow := true;
  374.         end; (* if *)
  375.         DoQuit := err;
  376.     end; (* DoQuit *)
  377.  
  378.     function DoSFOpen: OSErr;
  379.         var
  380.             err: OSErr;
  381.             fss: FSSpec;
  382.             info: FInfo;
  383.     begin
  384.         err := ICStandardGetFile(ICfiletype, fss, info);
  385.         if err = noErr then begin
  386.             err := DoOpenDoc(fss);
  387.         end; (* if *)
  388.         DoSFOpen := err;
  389.     end; (* DoSFOpen *)
  390.  
  391.     function FindInternetPreferences (var default_config: FSSpec): OSErr;
  392.         var
  393.             err: OSErr;
  394.             isfolder, wasalias: boolean;
  395.     begin
  396.         err := FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, default_config.vRefNum, default_config.parID);
  397.         if err = noErr then begin
  398.             err := ICMapErr(ICDefaultFileName(instance, default_config.name));
  399.         end; (* if *)
  400.         if err = noErr then begin
  401.             err := ResolveAliasFile(default_config, true, isfolder, wasalias);
  402.         end;
  403.         FindInternetPreferences := err;
  404.     end;
  405.  
  406.     function DoOpenInternetPreferences: OSErr;
  407.         var
  408.             default_config: FSSpec;
  409.             err: OSErr;
  410.     begin
  411.         err := FindInternetPreferences(default_config);
  412.         if err = noErr then begin
  413.             err := DoOpenDoc(default_config);
  414.         end; (* if *)
  415.         DoOpenInternetPreferences := err;
  416.     end;
  417.  
  418.     function InternalDoSave (fss: FSSpec): OSErr;
  419.         var
  420.             err: OSErr;
  421.     begin
  422.         err := noErr;
  423.         err := WindowsFlushAll;
  424.         if err = noErr then begin
  425.             WindowsSavePositions;
  426.             err := CopyFileSafe(scratch_file, fss);
  427.         end; (* if *)
  428.         if err = noErr then begin
  429.             WindowsSetTitle(WT_Main, fss.name);
  430.             new_document := false;
  431.             dirty_document := false;
  432.             current_file := fss;
  433.         end; (* if *)
  434.         CurrentSeed(current_seed);
  435.         InternalDoSave := err;
  436.     end; (* InternalDoSave *)
  437.  
  438.     function DoSave: OSErr;
  439.         var
  440.             err: OSErr;
  441.             seed: longInt;
  442.             a: integer;
  443.     begin
  444.         if current_file.name = '' then begin
  445.             err := DoSaveAs;
  446.         end
  447.         else begin
  448.             CurrentSeed(seed);
  449.             a := ok;
  450.             if not SameSeed(seed, current_seed) then begin
  451.                 a := CautionAlert(160, @CancelModalFilter);
  452.             end;
  453.             if a = ok then begin
  454.                 err := InternalDoSave(current_file);
  455.             end;
  456.         end; (* if *)
  457.         DoSave := err;
  458.     end; (* DoSave *)
  459.  
  460.     function DoSaveAs: OSErr;
  461.         var
  462.             err: OSErr;
  463.             fss: FSSpec;
  464.     begin
  465.         err := ICStandardPutFile('', GetDocumentName, fss);
  466.         if err = noErr then begin
  467.             err := InternalDoSave(fss);
  468.         end; (* if *)
  469.         DoSaveAs := err;
  470.     end; (* DoSaveAs *)
  471.  
  472.     function DoOpenApp: OSErr;
  473.         var
  474.             default_config: FSSpec;
  475.             err: OSErr;
  476.     begin
  477.         err := FindInternetPreferences(default_config);
  478.         if err = noErr then begin
  479.             err := DoOpenDoc(default_config);
  480.         end; (* if *)
  481.         if err = fnfErr then begin
  482.             err := DoNewDoc;
  483.             if err = noErr then begin
  484.                 err := InternalDoSave(default_config);
  485.             end; (* if *)
  486.         end; (* if *)
  487.         DoOpenApp := err;
  488.     end; (* DoOpenApp *)
  489.  
  490.     function InitICDocument: OSErr;
  491.         var
  492.             inst: ICInstance;
  493.             err: OSErr;
  494.     begin
  495.         instance := nil;
  496.         current_file.name := '';
  497.         scratch_file.name := '';
  498.         err := ICMapErr(ICStart(inst, ICcreator));
  499.         if err = noErr then begin
  500.             instance := inst;
  501.         end; (* if *)
  502.         InitICDocument := err;
  503.     end; (* InitICDocument *)
  504.  
  505.     procedure TermICDocument;
  506.         var
  507.             junk: ICError;
  508.     begin
  509.         if instance <> nil then begin
  510.             junk := ICStop(instance);
  511.         end; (* if *)
  512.     end; (* TermICDocument *)
  513.  
  514. end. (* ICDocument *)