source: trunk/Forms/FormImport.pas

Last change on this file was 170, checked in by chronos, 11 months ago
  • Fixed: Show only supported import formats.
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 Table.SetInput(TableFormat, LoadFileToStr(EditInputFile.Text));
139 Table.Title := ExtractFileNameWithoutExt(EditInputFile.Text);
140end;
141
142procedure TFormImport.UpdateTableFormat;
143var
144 FileExt: string;
145begin
146 FileExt := ExtractFileExt(EditInputFile.Text);
147 if FileExt = '.json' then
148 ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(tfJson))
149 else if FileExt = '.csv' then
150 ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(tfCsv))
151 else if FileExt = '.txt' then
152 ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(tfMediaWiki));
153end;
154
155procedure TFormImport.LoadConfig;
156var
157 TableFormat: TTableFormat;
158begin
159 with TRegistryEx.Create do
160 try
161 CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
162 EditInputFile.Text := ReadStringWithDefault('ImportFileName', 'Import.txt');
163 TableFormat := TTableFormat(ReadIntegerWithDefault('ImportTableFormat', Integer(tfCsv)));
164 CheckBoxHumanReadableHeader.Checked := ReadBoolWithDefault('ImportHumanReadableColumns', CheckBoxHumanReadableHeader.Checked);
165 ComboBoxInputFormat.ItemIndex := ComboBoxInputFormat.Items.IndexOfObject(TObject(TableFormat));
166 finally
167 Free;
168 end;
169end;
170
171procedure TFormImport.SaveConfig;
172begin
173 with TRegistryEx.Create do
174 try
175 CurrentContext := Core.Core.ApplicationInfo1.GetRegistryContext;
176 WriteString('ImportFileName', EditInputFile.Text);
177 WriteInteger('ImportTableFormat', Integer(ComboBoxInputFormat.Items.Objects[ComboBoxInputFormat.ItemIndex]));
178 WriteBool('ImportHumanReadableColumns', CheckBoxHumanReadableHeader.Checked);
179 finally
180 Free;
181 end;
182end;
183
184end.
185
Note: See TracBrowser for help on using the repository browser.