source: tags/1.1.0/Forms/UFormImport.pas

Last change on this file was 23, checked in by chronos, 8 years ago
  • Fixed: Build without synapse library.
File size: 4.2 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 ButtonImport: TButton;
18 ComboBoxDataFormat: TComboBox;
19 Label1: TLabel;
20 Memo1: TMemo;
21 OpenDialog1: TOpenDialog;
22 procedure ButtonImportClick(Sender: TObject);
23 procedure ButtonLoadFromFileClick(Sender: TObject);
24 private
25 procedure ImportMediaWiki;
26 procedure ImportCSV;
27 public
28 { public declarations }
29 end;
30
31var
32 FormImport: TFormImport;
33
34implementation
35
36{$R *.lfm}
37
38uses
39 UFormMain;
40
41resourcestring
42 SImportedNewAcronyms = 'Imported %d new acronyms.';
43
44
45{ TFormImport }
46
47procedure TFormImport.ButtonImportClick(Sender: TObject);
48begin
49 if ComboBoxDataFormat.ItemIndex = 0 then ImportCSV;
50 if ComboBoxDataFormat.ItemIndex = 1 then ImportMediaWiki;
51end;
52
53procedure TFormImport.ButtonLoadFromFileClick(Sender: TObject);
54begin
55 if OpenDialog1.Execute then
56 Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
57end;
58
59procedure TFormImport.ImportMediaWiki;
60var
61 I: Integer;
62 Line: string;
63 AcronymName: string;
64 AcronymMeaning: string;
65 AddedCount: Integer;
66 Acronym: TAcronym;
67 Meaning: TAcronymMeaning;
68begin
69 AddedCount := 0;
70 for I := 0 to Memo1.Lines.Count - 1 do begin
71 Line := Trim(Memo1.Lines[I]);
72 if Copy(Line, 1, 1) = ';' then begin
73 if Pos(':', Line) > 0 then begin
74 AcronymName := Trim(Copy(Line, 2, Pos(':', Line) - 2));
75 Line := Copy(Line, Pos(':', Line), Length(Line));
76 end else AcronymName := Trim(Copy(Line, 2, Length(Line)));
77 end;
78 if Copy(Line, 1, 1) = ':' then begin
79 AcronymMeaning := Trim(Copy(Line, 2, Length(Line)));
80 if (AcronymName <> '') and (AcronymMeaning <> '') then begin
81 Acronym := FormMain.AcronymDb.Acronyms.SearchByName(AcronymName);
82 if not Assigned(Acronym) then begin
83 Acronym := TAcronym.Create;
84 Acronym.Name := AcronymName;
85 FormMain.AcronymDb.Acronyms.Add(Acronym);
86 end;
87 Meaning := Acronym.Meanings.SearchByName(AcronymMeaning);
88 if not Assigned(Meaning) then begin
89 Meaning := TAcronymMeaning.Create;
90 Meaning.Name := AcronymMeaning;
91 Meaning.Acronym := Acronym;
92 Acronym.Meanings.Add(Meaning);
93 Inc(AddedCount)
94 end;
95 end;
96 end;
97 end;
98 if AddedCount > 0 then FormMain.AcronymDb.Modified := True;
99 ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
100end;
101
102procedure TFormImport.ImportCSV;
103var
104 I: Integer;
105 Line: string;
106 Columns: TStringList;
107 Acronym: TAcronym;
108 Meaning: TAcronymMeaning;
109 AcronymName: string;
110 AcronymMeaning: string;
111 AcronymDescription: string;
112 AddedCount: Integer;
113begin
114 AddedCount := 0;
115 Columns := TStringList.Create;
116 Columns.StrictDelimiter := True;
117 for I := 0 to Memo1.Lines.Count - 1 do begin
118 Line := Trim(Memo1.Lines[I]);
119 Columns.DelimitedText := Line;
120 if Columns.Count > 0 then AcronymName := Trim(Columns[0])
121 else AcronymName := '';
122 if Columns.Count > 1 then AcronymMeaning := Trim(Columns[1])
123 else AcronymMeaning := '';
124 if Columns.Count > 2 then AcronymDescription := Trim(Columns[2])
125 else AcronymDescription := '';
126 if (AcronymName <> '') and (AcronymDescription <> '') then begin
127 Acronym := FormMain.AcronymDb.Acronyms.SearchByName(AcronymName);
128 if not Assigned(Acronym) then begin
129 Acronym := TAcronym.Create;
130 Acronym.Name := AcronymName;
131 FormMain.AcronymDb.Acronyms.Add(Acronym);
132 end;
133 Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
134 if not Assigned(Meaning) then begin
135 Meaning := TAcronymMeaning.Create;
136 Meaning.Name := AcronymMeaning;
137 Meaning.Description := AcronymDescription;
138 Meaning.Acronym := Acronym;
139 Acronym.Meanings.Add(Meaning);
140 Inc(AddedCount)
141 end;
142 end;
143 end;
144 Columns.Free;
145 if AddedCount > 0 then FormMain.AcronymDb.Modified := True;
146 ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
147end;
148
149end.
150
Note: See TracBrowser for help on using the repository browser.