1 | unit UFormImport;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | UAcronym;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TFormImport }
|
---|
14 |
|
---|
15 | TFormImport = class(TForm)
|
---|
16 | ButtonLoadFromFile: TButton;
|
---|
17 | ButtonProcess: TButton;
|
---|
18 | ComboBoxDataFormat: TComboBox;
|
---|
19 | Label1: TLabel;
|
---|
20 | Memo1: TMemo;
|
---|
21 | OpenDialog1: TOpenDialog;
|
---|
22 | procedure ButtonProcessClick(Sender: TObject);
|
---|
23 | procedure ButtonLoadFromFileClick(Sender: TObject);
|
---|
24 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
25 | procedure FormCreate(Sender: TObject);
|
---|
26 | procedure FormShow(Sender: TObject);
|
---|
27 | private
|
---|
28 | procedure ImportMediaWiki;
|
---|
29 | procedure ImportCSV;
|
---|
30 | public
|
---|
31 | { public declarations }
|
---|
32 | end;
|
---|
33 |
|
---|
34 | var
|
---|
35 | FormImport: TFormImport;
|
---|
36 |
|
---|
37 | implementation
|
---|
38 |
|
---|
39 | {$R *.lfm}
|
---|
40 |
|
---|
41 | uses
|
---|
42 | UCore;
|
---|
43 |
|
---|
44 | resourcestring
|
---|
45 | SImportedNewAcronyms = 'Imported %d new acronyms.';
|
---|
46 |
|
---|
47 |
|
---|
48 | { TFormImport }
|
---|
49 |
|
---|
50 | procedure TFormImport.ButtonProcessClick(Sender: TObject);
|
---|
51 | begin
|
---|
52 | if ComboBoxDataFormat.ItemIndex = 0 then ImportCSV;
|
---|
53 | if ComboBoxDataFormat.ItemIndex = 1 then ImportMediaWiki;
|
---|
54 | end;
|
---|
55 |
|
---|
56 | procedure TFormImport.ButtonLoadFromFileClick(Sender: TObject);
|
---|
57 | begin
|
---|
58 | if ComboBoxDataFormat.ItemIndex = 0 then OpenDialog1.DefaultExt := '.csv';
|
---|
59 | if ComboBoxDataFormat.ItemIndex = 1 then OpenDialog1.DefaultExt := '.txt';
|
---|
60 | if OpenDialog1.Execute then
|
---|
61 | Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
|
---|
62 | end;
|
---|
63 |
|
---|
64 | procedure TFormImport.FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
65 | begin
|
---|
66 | Core.PersistentForm1.Save(Self);
|
---|
67 | end;
|
---|
68 |
|
---|
69 | procedure TFormImport.FormCreate(Sender: TObject);
|
---|
70 | begin
|
---|
71 | Core.Translator.TranslateComponentRecursive(Self);
|
---|
72 | Core.ThemeManager.UseTheme(Self);
|
---|
73 | end;
|
---|
74 |
|
---|
75 | procedure TFormImport.FormShow(Sender: TObject);
|
---|
76 | begin
|
---|
77 | Core.PersistentForm1.Load(Self);
|
---|
78 | end;
|
---|
79 |
|
---|
80 | procedure TFormImport.ImportMediaWiki;
|
---|
81 | var
|
---|
82 | I: Integer;
|
---|
83 | Line: string;
|
---|
84 | AcronymName: string;
|
---|
85 | AcronymMeaning: string;
|
---|
86 | AddedCount: Integer;
|
---|
87 | Acronym: TAcronym;
|
---|
88 | Meaning: TAcronymMeaning;
|
---|
89 | begin
|
---|
90 | AcronymName := '';
|
---|
91 | AddedCount := 0;
|
---|
92 | for I := 0 to Memo1.Lines.Count - 1 do begin
|
---|
93 | Line := Trim(Memo1.Lines[I]);
|
---|
94 | if Copy(Line, 1, 1) = ';' then begin
|
---|
95 | if Pos(':', Line) > 0 then begin
|
---|
96 | AcronymName := Trim(Copy(Line, 2, Pos(':', Line) - 2));
|
---|
97 | Line := Copy(Line, Pos(':', Line), Length(Line));
|
---|
98 | end else AcronymName := Trim(Copy(Line, 2, Length(Line)));
|
---|
99 | end;
|
---|
100 | if Copy(Line, 1, 1) = ':' then begin
|
---|
101 | AcronymMeaning := Trim(Copy(Line, 2, Length(Line)));
|
---|
102 | if (AcronymName <> '') and (AcronymMeaning <> '') then begin
|
---|
103 | Acronym := Core.AcronymDb.Acronyms.SearchByName(AcronymName);
|
---|
104 | if not Assigned(Acronym) then begin
|
---|
105 | Acronym := TAcronym.Create;
|
---|
106 | Acronym.Name := AcronymName;
|
---|
107 | Core.AcronymDb.Acronyms.Add(Acronym);
|
---|
108 | end;
|
---|
109 | Meaning := Acronym.Meanings.SearchByName(AcronymMeaning);
|
---|
110 | if not Assigned(Meaning) then begin
|
---|
111 | Meaning := TAcronymMeaning.Create;
|
---|
112 | Meaning.Name := AcronymMeaning;
|
---|
113 | Meaning.Acronym := Acronym;
|
---|
114 | Acronym.Meanings.Add(Meaning);
|
---|
115 | Inc(AddedCount)
|
---|
116 | end;
|
---|
117 | end;
|
---|
118 | end;
|
---|
119 | end;
|
---|
120 | if AddedCount > 0 then Core.AcronymDb.Modified := True;
|
---|
121 | ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
|
---|
122 | end;
|
---|
123 |
|
---|
124 | procedure TFormImport.ImportCSV;
|
---|
125 | var
|
---|
126 | I: Integer;
|
---|
127 | Line: string;
|
---|
128 | Columns: TStringList;
|
---|
129 | Acronym: TAcronym;
|
---|
130 | Meaning: TAcronymMeaning;
|
---|
131 | AcronymName: string;
|
---|
132 | AcronymMeaning: string;
|
---|
133 | AcronymDescription: string;
|
---|
134 | AddedCount: Integer;
|
---|
135 | begin
|
---|
136 | AddedCount := 0;
|
---|
137 | Columns := TStringList.Create;
|
---|
138 | Columns.StrictDelimiter := True;
|
---|
139 | for I := 0 to Memo1.Lines.Count - 1 do begin
|
---|
140 | Line := Trim(Memo1.Lines[I]);
|
---|
141 | Columns.DelimitedText := Line;
|
---|
142 | if Columns.Count > 0 then AcronymName := Trim(Columns[0])
|
---|
143 | else AcronymName := '';
|
---|
144 | if Columns.Count > 1 then AcronymMeaning := Trim(Columns[1])
|
---|
145 | else AcronymMeaning := '';
|
---|
146 | if Columns.Count > 2 then AcronymDescription := Trim(Columns[2])
|
---|
147 | else AcronymDescription := '';
|
---|
148 | if (AcronymName <> '') and (AcronymDescription <> '') then begin
|
---|
149 | Acronym := Core.AcronymDb.Acronyms.SearchByName(AcronymName);
|
---|
150 | if not Assigned(Acronym) then begin
|
---|
151 | Acronym := TAcronym.Create;
|
---|
152 | Acronym.Name := AcronymName;
|
---|
153 | Core.AcronymDb.Acronyms.Add(Acronym);
|
---|
154 | end;
|
---|
155 | Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
|
---|
156 | if not Assigned(Meaning) then begin
|
---|
157 | Meaning := TAcronymMeaning.Create;
|
---|
158 | Meaning.Name := AcronymMeaning;
|
---|
159 | Meaning.Description := AcronymDescription;
|
---|
160 | Meaning.Acronym := Acronym;
|
---|
161 | Acronym.Meanings.Add(Meaning);
|
---|
162 | Inc(AddedCount)
|
---|
163 | end;
|
---|
164 | end;
|
---|
165 | end;
|
---|
166 | Columns.Free;
|
---|
167 | if AddedCount > 0 then Core.AcronymDb.Modified := True;
|
---|
168 | ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
|
---|
169 | end;
|
---|
170 |
|
---|
171 | end.
|
---|
172 |
|
---|