source: trunk/Forms/FormImport.pas

Last change on this file was 172, checked in by chronos, 5 months ago
  • Fixed: Avoid error in case of invalid import data.
  • Modified: Updated Common package.
File size: 5.1 KB
Line 
1unit FormImport;
2
3interface
4
5uses
6 Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
7 ExtCtrls, Table, LazFileUtils, FormEx;
8
9type
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
45implementation
46
47{$R *.lfm}
48
49uses
50 Core, Common, RegistryEx, VCardFile, VCard;
51
52{ TFormImport }
53
54procedure TFormImport.FormClose(Sender: TObject; var CloseAction: TCloseAction);
55begin
56 SaveConfig;
57end;
58
59procedure TFormImport.ButtonImportClick(Sender: TObject);
60var
61 TableFormat: TTableFormat;
62begin
63 TableFormat := TTableFormat(ComboBoxInputFormat.Items.Objects[ComboBoxInputFormat.ItemIndex]);
64 TVCardFile(Core.Core.DataFile).VCard.ImportFromFile(EditInputFile.Text, TableFormat,
65 CheckBoxHumanReadableHeader.Checked);
66end;
67
68procedure TFormImport.ComboBoxInputFormatChange(Sender: TObject);
69begin
70 RedrawPending := True;
71end;
72
73procedure TFormImport.EditInputFileChange(Sender: TObject);
74begin
75 RedrawPending := True;
76 UpdateTableFormat;
77end;
78
79procedure TFormImport.ButtonBrowseClick(Sender: TObject);
80begin
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;
87end;
88
89procedure TFormImport.FormCreate(Sender: TObject);
90var
91 TableFormat: TTableFormat;
92 TableFormats: TTableFormats;
93begin
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;
110end;
111
112procedure TFormImport.FormDestroy(Sender: TObject);
113begin
114 FreeAndNil(Table);
115end;
116
117procedure TFormImport.FormShow(Sender: TObject);
118begin
119 RedrawPending := True;
120end;
121
122procedure TFormImport.TimerRedrawTimer(Sender: TObject);
123begin
124 if RedrawPending then begin
125 ImportTable;
126 Table.GetOutputListView(ListView1);
127 RedrawPending := False;
128 end;
129end;
130
131procedure TFormImport.ImportTable;
132var
133 TableFormat: TTableFormat;
134begin
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;
144end;
145
146procedure TFormImport.UpdateTableFormat;
147var
148 FileExt: string;
149begin
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));
157end;
158
159procedure TFormImport.LoadConfig;
160var
161 TableFormat: TTableFormat;
162begin
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;
173end;
174
175procedure TFormImport.SaveConfig;
176begin
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;
186end;
187
188end.
189
Note: See TracBrowser for help on using the repository browser.