unit FileOverwriteConfirm;

{
[FileOverwriteConfirm] [1.0]
Delphi 2005
December 2005

LICENSE

The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
"http://www.mozilla.org/MPL/"

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is "[FileOverwriteConfirm.pas]".

The Initial Developer of the Original Code is Martin Holmes (Victoria,
BC, Canada, "http://www.mholmes.com/"). Copyright (C) 2005 Martin Holmes 
and the University of Victoria Computing and Media Centre. The code was 
co-developed for university and personal projects, and rights are shared
by Martin Holmes and the University of Victoria. All Rights Reserved.
}

{
 This unit written by Martin Holmes, November 2005.

 The purpose of this unit is to provide a flexible interface for informing
 users when an operation will overwrite multiple files, and allowing them
 to confirm the overwrite of each file individually. It would be used when
 (for example) saving a collection of interrelated files (XML, XSLT, CSS...)
 where the results of a previous save might have been customized by the user,
 so he/she would not want to overwrite some customized files.


 Dependencies:
  TntUnicodeControls (Troy Wolbrink)
  FormState
}


interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TntForms, FormState, StdCtrls, Buttons, TntButtons, CheckLst,
  TntCheckLst, TntStdCtrls, TntClasses, TntSysUtils;

type
  TufrmFileOverwriteConfirm = class(TTntForm)
    ulbExplanation: TTntLabel;
    uclbFIles: TTntCheckListBox;
    ubnOK: TTntBitBtn;
    ubnCancel: TTntBitBtn;
    procedure TntFormHide(Sender: TObject);
    procedure TntFormShow(Sender: TObject);
    procedure TntFormDestroy(Sender: TObject);
    procedure TntFormCreate(Sender: TObject);
  private
    { Private declarations }
    FFormStateSaver: TFormStateSaver;
    FFileList: TTntStringList;
  public
//This function initializes the list, checks for the existence of each of the
//files, and if there are files that will be overwritten, returns true.
//It also builds its checklist ready for showing by the calling function.
    function CheckForOverwrites(Files: TTntStringList): Boolean;
//This function would be called later to check whether the user OK'd the
//overwriting of a specific file
    function CanOverwrite(FName: WideString): Boolean;
    { Public declarations }
  end;

var
  ufrmFileOverwriteConfirm: TufrmFileOverwriteConfirm;

implementation

{$R *.dfm}

procedure TufrmFileOverwriteConfirm.TntFormCreate(Sender: TObject);
begin
  FFileList := TTntStringList.Create;

end;

procedure TufrmFileOverwriteConfirm.TntFormDestroy(Sender: TObject);
begin
  FreeAndNil(FFileList);
end;

function TufrmFileOverwriteConfirm.CheckForOverwrites(
  Files: TTntStringList): Boolean;
var
i: integer;
begin
  Result := False; //default
  if Files.Count > 0 then
    begin
      FFileList.Clear;
      uclbFiles.Clear;
      for i := 0 to Files.Count-1 do
        begin
          if WideFileExists(Files[i]) then
            begin
              FFileList.Add(Files[i]);
              uclbFiles.Items.Add(ExtractFileName(Files[i]));
              uclbFiles.Checked[uclbFiles.Items.Count-1] := True;
              Result := True;
            end;
        end;
    end;
end;

procedure TufrmFileOverwriteConfirm.TntFormShow(Sender: TObject);
begin
  FFormStateSaver := TFormStateSaver.Create(Self, True);
end;

function TufrmFileOverwriteConfirm.CanOverwrite(FName: WideString): Boolean;
var
ItemNum: integer;
begin
  Result := True; //Default -- allow overwriting if we know nothing about the file
  ItemNum :=  FFileList.IndexOf(FName);
  if ItemNum > -1 then
    if ItemNum < uclbFiles.Items.Count then
      Result := uclbFiles.Checked[ItemNum];
end;

procedure TufrmFileOverwriteConfirm.TntFormHide(Sender: TObject);
begin
  FreeAndNil(FFormStateSaver);
end;

end.
