| 1 | unit FormImport;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
|
|---|
| 7 | ExtCtrls, Table, LazFileUtils, FormEx;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 10 |
|
|---|
| 11 | { TFormImport }
|
|---|
| 12 |
|
|---|
| 13 | TFormImport = class(TFormEx)
|
|---|
| 14 | ButtonBrowse: TButton;
|
|---|
| 15 | ButtonCancel: TButton;
|
|---|
| 16 | ButtonImport: TButton;
|
|---|
| 17 | CheckBoxHumanReadableHeader: TCheckBox;
|
|---|
| 18 | ComboBoxInputFormat: TComboBox;
|
|---|
| 19 | EditInputFile: TEdit;
|
|---|
| 20 | Label1: TLabel;
|
|---|
| 21 | Label2: TLabel;
|
|---|
| 22 | ListView1: TListView;
|
|---|
| 23 | OpenDialog1: TOpenDialog;
|
|---|
| 24 | ScrollBox1: TScrollBox;
|
|---|
| 25 | TimerRedraw: TTimer;
|
|---|
| 26 | procedure ButtonBrowseClick(Sender: TObject);
|
|---|
| 27 | procedure ButtonImportClick(Sender: TObject);
|
|---|
| 28 | procedure ComboBoxInputFormatChange(Sender: TObject);
|
|---|
| 29 | procedure EditInputFileChange(Sender: TObject);
|
|---|
| 30 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
|---|
| 31 | procedure FormCreate(Sender: TObject);
|
|---|
| 32 | procedure FormDestroy(Sender: TObject);
|
|---|
| 33 | procedure FormShow(Sender: TObject);
|
|---|
| 34 | procedure TimerRedrawTimer(Sender: TObject);
|
|---|
| 35 | private
|
|---|
| 36 | Table: TTable;
|
|---|
| 37 | RedrawPending: Boolean;
|
|---|
| 38 | procedure ImportTable;
|
|---|
| 39 | procedure UpdateTableFormat;
|
|---|
| 40 | procedure LoadConfig;
|
|---|
| 41 | procedure SaveConfig;
|
|---|
| 42 | end;
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | implementation
|
|---|
| 46 |
|
|---|
| 47 | {$R *.lfm}
|
|---|
| 48 |
|
|---|
| 49 | uses
|
|---|
| 50 | Core, Common, RegistryEx, VCardFile, VCard;
|
|---|
| 51 |
|
|---|
| 52 | { TFormImport }
|
|---|
| 53 |
|
|---|
| 54 | procedure TFormImport.FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
|---|
| 55 | begin
|
|---|
| 56 | SaveConfig;
|
|---|
| 57 | end;
|
|---|
| 58 |
|
|---|
| 59 | procedure TFormImport.ButtonImportClick(Sender: TObject);
|
|---|
| 60 | var
|
|---|
| 61 | TableFormat: TTableFormat;
|
|---|
| 62 | begin
|
|---|
| 63 | TableFormat := TTableFormat(ComboBoxInputFormat.Items.Objects[ComboBoxInputFormat.ItemIndex]);
|
|---|
| 64 | TVCardFile(Core.Core.DataFile).VCard.ImportFromFile(EditInputFile.Text, TableFormat,
|
|---|
| 65 | CheckBoxHumanReadableHeader.Checked);
|
|---|
| 66 | end;
|
|---|
| 67 |
|
|---|
| 68 | procedure TFormImport.ComboBoxInputFormatChange(Sender: TObject);
|
|---|
| 69 | begin
|
|---|
| 70 | RedrawPending := True;
|
|---|
| 71 | end;
|
|---|
| 72 |
|
|---|
| 73 | procedure TFormImport.EditInputFileChange(Sender: TObject);
|
|---|
| 74 | begin
|
|---|
| 75 | RedrawPending := True;
|
|---|
| 76 | UpdateTableFormat;
|
|---|
| 77 | end;
|
|---|
| 78 |
|
|---|
| 79 | procedure TFormImport.ButtonBrowseClick(Sender: TObject);
|
|---|
| 80 | begin
|
|---|
| 81 | OpenDialog1.DefaultExt := '';
|
|---|
| 82 | OpenDialog1.InitialDir := ExtractFileDir(EditInputFile.Text);
|
|---|
| 83 | OpenDialog1.FileName := ExtractFileName(EditInputFile.Text);
|
|---|
| 84 | if OpenDialog1.Execute then begin
|
|---|
| 85 | EditInputFile.Text := OpenDialog1.FileName;
|
|---|
| 86 | end;
|
|---|
| 87 | end;
|
|---|
| 88 |
|
|---|
| 89 | procedure TFormImport.FormCreate(Sender: TObject);
|
|---|
| 90 | var
|
|---|
| 91 | TableFormat: TTableFormat;
|
|---|
| 92 | TableFormats: TTableFormats;
|
|---|
| 93 | begin
|
|---|
| 94 | Table := TTable.Create;
|
|---|
| 95 |
|
|---|
| 96 | TableFormats := Table.GetInputFormats;
|
|---|
| 97 | ComboBoxInputFormat.Items.BeginUpdate;
|
|---|
| 98 | try
|
|---|
| 99 | for TableFormat := Low(TTableFormat) to High(TTableFormat) do
|
|---|
| 100 | if TableFormat in TableFormats then
|
|---|
| 101 | ComboBoxInputFormat.Items.AddObject(TableFormatText[TableFormat], TObject(TableFormat));
|
|---|
| 102 | if (ComboBoxInputFormat.ItemIndex = -1) and (ComboBoxInputFormat.Items.Count > 0) then
|
|---|
| 103 | ComboBoxInputFormat.ItemIndex := 0;
|
|---|
| 104 | finally
|
|---|
| 105 | ComboBoxInputFormat.Items.EndUpdate;
|
|---|
| 106 | end;
|
|---|
| 107 |
|
|---|
| 108 | LoadConfig;
|
|---|
| 109 | UpdateTableFormat;
|
|---|
| 110 | end;
|
|---|
| 111 |
|
|---|
| 112 | procedure TFormImport.FormDestroy(Sender: TObject);
|
|---|
| 113 | begin
|
|---|
| 114 | FreeAndNil(Table);
|
|---|
| 115 | end;
|
|---|
| 116 |
|
|---|
| 117 | procedure TFormImport.FormShow(Sender: TObject);
|
|---|
| 118 | begin
|
|---|
| 119 | RedrawPending := True;
|
|---|
| 120 | end;
|
|---|
| 121 |
|
|---|
| 122 | procedure TFormImport.TimerRedrawTimer(Sender: TObject);
|
|---|
| 123 | begin
|
|---|
| 124 | if RedrawPending then begin
|
|---|
| 125 | ImportTable;
|
|---|
| 126 | Table.GetOutputListView(ListView1);
|
|---|
| 127 | RedrawPending := False;
|
|---|
| 128 | end;
|
|---|
| 129 | end;
|
|---|
| 130 |
|
|---|
| 131 | procedure TFormImport.ImportTable;
|
|---|
| 132 | var
|
|---|
| 133 | TableFormat: TTableFormat;
|
|---|
| 134 | begin
|
|---|
| 135 | TableFormat := TTableFormat(ComboBoxInputFormat.Items.Objects[ComboBoxInputFormat.ItemIndex]);
|
|---|
| 136 | if not FileExists(EditInputFile.Text) then Exit;
|
|---|
| 137 |
|
|---|
| 138 | try
|
|---|
| 139 | Table.SetInput(TableFormat, LoadFileToStr(EditInputFile.Text));
|
|---|
| 140 | Table.Title := ExtractFileNameWithoutExt(EditInputFile.Text);
|
|---|
| 141 | except
|
|---|
| 142 | // It may fail due to invalid format
|
|---|
| 143 | end;
|
|---|
| 144 | end;
|
|---|
| 145 |
|
|---|
| 146 | procedure TFormImport.UpdateTableFormat;
|
|---|
| 147 | var
|
|---|
| 148 | FileExt: string;
|
|---|
| 149 | begin
|
|---|
| 150 | FileExt := ExtractFileExt(EditInputFile.Text);
|
|---|
| 151 | if FileExt = '.json' then
|
|---|
| 152 | ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(tfJson))
|
|---|
| 153 | else if FileExt = '.csv' then
|
|---|
| 154 | ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(tfCsv))
|
|---|
| 155 | else if FileExt = '.txt' then
|
|---|
| 156 | ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(tfMediaWiki));
|
|---|
| 157 | end;
|
|---|
| 158 |
|
|---|
| 159 | procedure TFormImport.LoadConfig;
|
|---|
| 160 | var
|
|---|
| 161 | TableFormat: TTableFormat;
|
|---|
| 162 | begin
|
|---|
| 163 | with TRegistryEx.Create do
|
|---|
| 164 | try
|
|---|
| 165 | CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
|
|---|
| 166 | EditInputFile.Text := ReadStringWithDefault('ImportFileName', 'Import.txt');
|
|---|
| 167 | TableFormat := TTableFormat(ReadIntegerWithDefault('ImportTableFormat', Integer(tfCsv)));
|
|---|
| 168 | CheckBoxHumanReadableHeader.Checked := ReadBoolWithDefault('ImportHumanReadableColumns', CheckBoxHumanReadableHeader.Checked);
|
|---|
| 169 | ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(TableFormat));
|
|---|
| 170 | finally
|
|---|
| 171 | Free;
|
|---|
| 172 | end;
|
|---|
| 173 | end;
|
|---|
| 174 |
|
|---|
| 175 | procedure TFormImport.SaveConfig;
|
|---|
| 176 | begin
|
|---|
| 177 | with TRegistryEx.Create do
|
|---|
| 178 | try
|
|---|
| 179 | CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
|
|---|
| 180 | WriteString('ImportFileName', EditInputFile.Text);
|
|---|
| 181 | WriteInteger('ImportTableFormat', Integer(ComboBoxInputFormat.Items.Objects[ComboBoxInputFormat.ItemIndex]));
|
|---|
| 182 | WriteBool('ImportHumanReadableColumns', CheckBoxHumanReadableHeader.Checked);
|
|---|
| 183 | finally
|
|---|
| 184 | Free;
|
|---|
| 185 | end;
|
|---|
| 186 | end;
|
|---|
| 187 |
|
|---|
| 188 | end.
|
|---|
| 189 |
|
|---|