unit FormExport;

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
  ExtCtrls, Table, LazFileUtils, FormEx;

type

  { TFormExport }

  TFormExport = class(TFormEx)
    ButtonBrowse: TButton;
    ButtonCancel: TButton;
    ButtonExport: TButton;
    CheckBoxHumanReadableHeader: TCheckBox;
    ComboBoxOutputFormat: TComboBox;
    EditOutputFile: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    ListView1: TListView;
    SaveDialog1: TSaveDialog;
    ScrollBox1: TScrollBox;
    TimerRedraw: TTimer;
    procedure ButtonBrowseClick(Sender: TObject);
    procedure ButtonExportClick(Sender: TObject);
    procedure CheckBoxHumanReadableHeaderChange(Sender: TObject);
    procedure ComboBoxOutputFormatChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TimerRedrawTimer(Sender: TObject);
  private
    Table: TTable;
    RedrawPending: Boolean;
    procedure UpdateFileNameExt;
    procedure LoadConfig;
    procedure SaveConfig;
  end;


implementation

{$R *.lfm}

uses
  Core, Common, RegistryEx, VCardFile, VCard;

{ TFormExport }

procedure TFormExport.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  SaveConfig;
end;

procedure TFormExport.ButtonExportClick(Sender: TObject);
var
  TableFormat: TTableFormat;
begin
  TableFormat := TTableFormat(ComboBoxOutputFormat.Items.Objects[ComboBoxOutputFormat.ItemIndex]);
  TVCardFile(Core.Core.DataFile).VCard.ExportToFile(EditOutputFile.Text, TableFormat,
    CheckBoxHumanReadableHeader.Checked);
end;

procedure TFormExport.CheckBoxHumanReadableHeaderChange(Sender: TObject);
begin
  RedrawPending := True;
end;

procedure TFormExport.ComboBoxOutputFormatChange(Sender: TObject);
begin
  UpdateFileNameExt;
end;

procedure TFormExport.ButtonBrowseClick(Sender: TObject);
begin
  SaveDialog1.DefaultExt := '';
  SaveDialog1.InitialDir := ExtractFileDir(EditOutputFile.Text);
  SaveDialog1.FileName := ExtractFileName(EditOutputFile.Text);
  if SaveDialog1.Execute then begin
    EditOutputFile.Text := SaveDialog1.FileName;
  end;
end;

procedure TFormExport.FormCreate(Sender: TObject);
var
  TableFormat: TTableFormat;
begin
  ComboBoxOutputFormat.Items.BeginUpdate;
  try
    for TableFormat := Low(TTableFormat) to High(TTableFormat) do
    if TableFormat <> tfListView then
      ComboBoxOutputFormat.Items.AddObject(TableFormatText[TableFormat], TObject(TableFormat));
    if (ComboBoxOutputFormat.ItemIndex = -1) and (ComboBoxOutputFormat.Items.Count > 0) then
      ComboBoxOutputFormat.ItemIndex := 0;
  finally
    ComboBoxOutputFormat.Items.EndUpdate;
  end;

  Table := TTable.Create;
  LoadConfig;
  UpdateFileNameExt;
end;

procedure TFormExport.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Table);
end;

procedure TFormExport.FormShow(Sender: TObject);
begin
  RedrawPending := True;
end;

procedure TFormExport.TimerRedrawTimer(Sender: TObject);
begin
  if RedrawPending then begin
    TVCardFile(Core.Core.DataFile).VCard.ExportToTable(Table,
      CheckBoxHumanReadableHeader.Checked);
    Table.GetOutputListView(ListView1);
    RedrawPending := False;
  end;
end;

procedure TFormExport.UpdateFileNameExt;
var
  FileName: string;
begin
  FileName := EditOutputFile.Text;
  EditOutputFile.Text := Copy(FileName, 1, Length(FileName) - Length(ExtractFileExt(FileName))) +
    TableFormatExt[TTableFormat(ComboBoxOutputFormat.Items.Objects[ComboBoxOutputFormat.ItemIndex])];
end;

procedure TFormExport.LoadConfig;
var
  TableFormat: TTableFormat;
begin
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    EditOutputFile.Text := ReadStringWithDefault('ExportFileName', 'Export.txt');
    TableFormat := TTableFormat(ReadIntegerWithDefault('ExportTableFormat', Integer(tfCsv)));
    CheckBoxHumanReadableHeader.Checked := ReadBoolWithDefault('ExportHumanReadableColumns', CheckBoxHumanReadableHeader.Checked);
    ComboBoxOutputFormat.ItemIndex := ComboBoxOutputFormat.Items.IndexOfObject(TObject(TableFormat));
  finally
    Free;
  end;
end;

procedure TFormExport.SaveConfig;
begin
  with TRegistryEx.Create do
  try
    CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
    WriteString('ExportFileName', EditOutputFile.Text);
    WriteInteger('ExportTableFormat', Integer(ComboBoxOutputFormat.Items.Objects[ComboBoxOutputFormat.ItemIndex]));
    WriteBool('ExportHumanReadableColumns', CheckBoxHumanReadableHeader.Checked);
  finally
    Free;
  end;
end;

end.

