home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 March / macformat-035.iso / Shareware City / Developers / ICAppSourceKit1.2 / ICDocument.p < prev    next >
Encoding:
Text File  |  1995-11-07  |  11.9 KB  |  513 lines  |  [TEXT/CWIE]

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