{*************************************************************************}
{                                                                         }
{ RollbackUnit.pas - window to load backup copies of quests               }
{                                                                         }
{ This file is a part of the project:                                     }
{   Dizzy Quest Editor - the quest editor for Dizzy-like games            }
{                                                                         }
{ © Parfenov Alexey Andreevich, 2007                                      }
{                                                                         }
{ e-mail: AlkatrazStudio@yandex.ru                                        }
{                                                                         }
{ This program is free software; you can redistribute it and/or modify    }
{ it under the terms of the GNU General Public License as published by    }
{ the Free Software Foundation; either version 2 of the License, or       }
{ (at your option) any later version.                                     }
{                                                                         }
{ This program is distributed in the hope that it will be useful,         }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of          }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            }
{ GNU General Public License for more details.                            }
{                                                                         }
{ You may read GNU General Public License at:                             }
{   http://www.gnu.org/copyleft/gpl.html                                  }
{                                                                         }
{*************************************************************************}
unit RollbackUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls, Alkatraz;

type
  TForm_Rollback = class(TForm)
    Label1: TLabel;
    ListView1: TListView;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure ShowIt(FileName:string);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure Localize;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_Rollback: TForm_Rollback;

implementation

uses MainUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_Rollback.BitBtn1Click(Sender: TObject);
var
  s,b,d,wn:string;
begin
  if ListView1.Selected<>nil then
  begin
    wn:=MainUnit.CurFileName;
    s:=ListView1.Selected.SubItems[0];
    d:=ExtractFileDir(wn)+'\';
    b:=d+'_backup\';
    CopyFile(PAnsiChar(b+s),PAnsiChar(d+'tempworld.tmp'),false);
    if Form_Main_DQE.AskRewrite then
    begin
      Form_Main_DQE.LoadWorld(d+'tempworld.tmp',false);
      MainUnit.CurFileName:=wn;
      Form_Main_DQE.UpdateCaption;
      Close;
    end;
    DeleteFile(d+'tempworld.tmp');
  end;
end;

procedure TForm_Rollback.BitBtn2Click(Sender: TObject);
begin
  close;
end;

procedure TForm_Rollback.ShowIt(FileName: string);
var
  dir,fn:string;
  f:TSearchRec;
  sys:_SYSTEMTIME;
  dt:TDateTime;
  i:TListItem;
begin
  ListView1.Clear;
  dir:=ExtractFileDir(FileName);
  fn:=ExtractFileName(FileName);
  if FindFirst(dir+'\_backup\'+fn+'.~???',faAnyFile,f)=0 then
  repeat
    FileTimeToSystemTime(f.FindData.ftLastWriteTime,sys);
    dt:=SystemTimeToDateTime(sys);
    i:=ListView1.Items.Add;
    i.Caption:=FormatDateTime('ddddd tt',dt);
    i.SubItems.Add(f.Name);
  until FindNext(f)<>0;
  ShowModal;
end;

procedure TForm_Rollback.FormCreate(Sender: TObject);
begin
  SetLength(lng,5);
end;

procedure TForm_Rollback.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  Label1.Caption:=lng[1];
  ListView1.Columns[0].Caption:=lng[2];
  ListView1.Columns[1].Caption:=lng[3];
  BitBtn2.Caption:=lng[4];
end;

end.
