source: tags/1.4.0/Forms/UFormImport.pas

Last change on this file was 98, checked in by chronos, 8 years ago
  • Fixed: Wrong access to main thread from export jobs causing exception.
  • Fixed: Build under linux.
File size: 4.5 KB
Line 
1unit UFormImport;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 UAcronym;
10
11type
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 FormCreate(Sender: TObject);
25 private
26 procedure ImportMediaWiki;
27 procedure ImportCSV;
28 public
29 { public declarations }
30 end;
31
32var
33 FormImport: TFormImport;
34
35implementation
36
37{$R *.lfm}
38
39uses
40 UCore;
41
42resourcestring
43 SImportedNewAcronyms = 'Imported %d new acronyms.';
44
45
46{ TFormImport }
47
48procedure TFormImport.ButtonProcessClick(Sender: TObject);
49begin
50 if ComboBoxDataFormat.ItemIndex = 0 then ImportCSV;
51 if ComboBoxDataFormat.ItemIndex = 1 then ImportMediaWiki;
52end;
53
54procedure TFormImport.ButtonLoadFromFileClick(Sender: TObject);
55begin
56 if ComboBoxDataFormat.ItemIndex = 0 then OpenDialog1.DefaultExt := '.csv';
57 if ComboBoxDataFormat.ItemIndex = 1 then OpenDialog1.DefaultExt := '.txt';
58 if OpenDialog1.Execute then
59 Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
60end;
61
62procedure TFormImport.FormCreate(Sender: TObject);
63begin
64 Core.CoolTranslator1.TranslateComponentRecursive(Self);
65end;
66
67procedure TFormImport.ImportMediaWiki;
68var
69 I: Integer;
70 Line: string;
71 AcronymName: string;
72 AcronymMeaning: string;
73 AddedCount: Integer;
74 Acronym: TAcronym;
75 Meaning: TAcronymMeaning;
76begin
77 AddedCount := 0;
78 for I := 0 to Memo1.Lines.Count - 1 do begin
79 Line := Trim(Memo1.Lines[I]);
80 if Copy(Line, 1, 1) = ';' then begin
81 if Pos(':', Line) > 0 then begin
82 AcronymName := Trim(Copy(Line, 2, Pos(':', Line) - 2));
83 Line := Copy(Line, Pos(':', Line), Length(Line));
84 end else AcronymName := Trim(Copy(Line, 2, Length(Line)));
85 end;
86 if Copy(Line, 1, 1) = ':' then begin
87 AcronymMeaning := Trim(Copy(Line, 2, Length(Line)));
88 if (AcronymName <> '') and (AcronymMeaning <> '') then begin
89 Acronym := Core.AcronymDb.Acronyms.SearchByName(AcronymName);
90 if not Assigned(Acronym) then begin
91 Acronym := TAcronym.Create;
92 Acronym.Name := AcronymName;
93 Core.AcronymDb.Acronyms.Add(Acronym);
94 end;
95 Meaning := Acronym.Meanings.SearchByName(AcronymMeaning);
96 if not Assigned(Meaning) then begin
97 Meaning := TAcronymMeaning.Create;
98 Meaning.Name := AcronymMeaning;
99 Meaning.Acronym := Acronym;
100 Acronym.Meanings.Add(Meaning);
101 Inc(AddedCount)
102 end;
103 end;
104 end;
105 end;
106 if AddedCount > 0 then Core.AcronymDb.Modified := True;
107 ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
108end;
109
110procedure TFormImport.ImportCSV;
111var
112 I: Integer;
113 Line: string;
114 Columns: TStringList;
115 Acronym: TAcronym;
116 Meaning: TAcronymMeaning;
117 AcronymName: string;
118 AcronymMeaning: string;
119 AcronymDescription: string;
120 AddedCount: Integer;
121begin
122 AddedCount := 0;
123 Columns := TStringList.Create;
124 Columns.StrictDelimiter := True;
125 for I := 0 to Memo1.Lines.Count - 1 do begin
126 Line := Trim(Memo1.Lines[I]);
127 Columns.DelimitedText := Line;
128 if Columns.Count > 0 then AcronymName := Trim(Columns[0])
129 else AcronymName := '';
130 if Columns.Count > 1 then AcronymMeaning := Trim(Columns[1])
131 else AcronymMeaning := '';
132 if Columns.Count > 2 then AcronymDescription := Trim(Columns[2])
133 else AcronymDescription := '';
134 if (AcronymName <> '') and (AcronymDescription <> '') then begin
135 Acronym := Core.AcronymDb.Acronyms.SearchByName(AcronymName);
136 if not Assigned(Acronym) then begin
137 Acronym := TAcronym.Create;
138 Acronym.Name := AcronymName;
139 Core.AcronymDb.Acronyms.Add(Acronym);
140 end;
141 Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
142 if not Assigned(Meaning) then begin
143 Meaning := TAcronymMeaning.Create;
144 Meaning.Name := AcronymMeaning;
145 Meaning.Description := AcronymDescription;
146 Meaning.Acronym := Acronym;
147 Acronym.Meanings.Add(Meaning);
148 Inc(AddedCount)
149 end;
150 end;
151 end;
152 Columns.Free;
153 if AddedCount > 0 then Core.AcronymDb.Modified := True;
154 ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
155end;
156
157end.
158
Note: See TracBrowser for help on using the repository browser.