1 |
unit Template; |
2 |
|
3 |
interface |
4 |
|
5 |
uses |
6 |
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, |
7 |
Dialogs, ExtCtrls, StdCtrls, StrUtils, |
8 |
OniDataClass, Functions, Data, Menus, Buttons; |
9 |
|
10 |
type |
11 |
TNewFileSelectedEvent = procedure(fileinfo: TFileInfo) of object; |
12 |
TNewConnectionEvent = procedure(connection: TOniData) of object; |
13 |
|
14 |
TForm_ToolTemplate = class(TForm) |
15 |
panel_files: TPanel; |
16 |
filelist: TListBox; |
17 |
panel_extension: TPanel; |
18 |
label_ext: TLabel; |
19 |
combo_extension: TComboBox; |
20 |
check_zerobyte: TCheckBox; |
21 |
edit_filtername: TEdit; |
22 |
check_filtername: TCheckBox; |
23 |
Splitter1: TSplitter; |
24 |
content: TPanel; |
25 |
filepopup: TPopupMenu; |
26 |
popup_import: TMenuItem; |
27 |
popup_export: TMenuItem; |
28 |
popup_separator: TMenuItem; |
29 |
importd: TOpenDialog; |
30 |
exportd: TSaveDialog; |
31 |
btn_sort_id_asc: TSpeedButton; |
32 |
btn_sort_id_desc: TSpeedButton; |
33 |
btn_sort_name_asc: TSpeedButton; |
34 |
btn_sort_name_desc: TSpeedButton; |
35 |
btn_sort_ext_asc: TSpeedButton; |
36 |
btn_sort_ext_desc: TSpeedButton; |
37 |
Label1: TLabel; |
38 |
Label2: TLabel; |
39 |
Label3: TLabel; |
40 |
combo_connection: TComboBox; |
41 |
Bevel1: TBevel; |
42 |
procedure RecreateList; |
43 |
procedure UpdateList; |
44 |
procedure RecreateExtList; |
45 |
procedure LoadFileNames; |
46 |
procedure SelectFileName(filename: String); |
47 |
procedure SelectFileID(id: Integer); |
48 |
procedure check_filternameClick(Sender: TObject); |
49 |
procedure check_zerobyteClick(Sender: TObject); |
50 |
procedure combo_extensionClick(Sender: TObject); |
51 |
procedure listClick(Sender: TObject); |
52 |
procedure listMouseDown(Sender: TObject; Button: TMouseButton; |
53 |
Shift: TShiftState; X, Y: Integer); |
54 |
|
55 |
procedure FormResize(Sender: TObject); |
56 |
procedure FormCreate(Sender: TObject); |
57 |
procedure FormClose(Sender: TObject; var Action: TCloseAction); |
58 |
procedure popup_importClick(Sender: TObject); |
59 |
procedure popup_exportClick(Sender: TObject); |
60 |
procedure popup_opentool(Sender: TObject); |
61 |
procedure filepopupPopup(Sender: TObject); |
62 |
procedure btn_sortClick(Sender: TObject); |
63 |
procedure FormActivate(Sender: TObject); |
64 |
procedure combo_connectionChange(Sender: TObject); |
65 |
private |
66 |
FSortBy: TSortType; |
67 |
FOnNewFileSelected: TNewFileSelectedEvent; |
68 |
FOnNewConnection: TNewConnectionEvent; |
69 |
FAllowedExts: String; |
70 |
FAllowMultiSelect: Boolean; |
71 |
FSelectedFile: TFileInfo; |
72 |
FConnection: TOniData; |
73 |
procedure SetAllowedExts(exts: String); |
74 |
procedure SetMultiSelect(allow: Boolean); |
75 |
public |
76 |
constructor Create(AOwner: TComponent); override; |
77 |
procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean); |
78 |
published |
79 |
property OnNewFileSelected: TNewFileSelectedEvent read FOnNewFileSelected write FOnNewFileSelected; |
80 |
property OnNewConnection: TNewConnectionEvent read FOnNewConnection write FOnNewConnection; |
81 |
property AllowedExts: String read FAllowedExts write SetAllowedExts; |
82 |
property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect; |
83 |
property SelectedFile: TFileInfo read FSelectedFile; |
84 |
property Connection: TOniData read FConnection; |
85 |
end; |
86 |
|
87 |
var |
88 |
ToolList: TToolList; |
89 |
procedure AddToolListEntry(context, name, exts: String); |
90 |
|
91 |
implementation |
92 |
{$R *.dfm} |
93 |
uses Main, Exporters; |
94 |
|
95 |
|
96 |
procedure TForm_ToolTemplate.UpdateList; |
97 |
var |
98 |
i: Integer; |
99 |
fn, datatype, boxstring: String; |
100 |
level: Integer; |
101 |
oldcon: String; |
102 |
begin |
103 |
oldcon := combo_connection.Items.Strings[combo_connection.ItemIndex]; |
104 |
combo_connection.ItemIndex := -1; |
105 |
combo_connection.Items.Clear; |
106 |
if Length(DataConnections) > 0 then |
107 |
begin |
108 |
for i := 0 to High(DataConnections) do |
109 |
begin |
110 |
level := DataConnections[i].LevelInfo.LevelNumber; |
111 |
fn := ExtractFileName(DataConnections[i].FileName); |
112 |
if DataConnections[i].Backend = ODB_Dat then |
113 |
datatype := 'ONI-.dat: ' |
114 |
else if DataConnections[i].Backend = ODB_ADB then |
115 |
datatype := 'OUP-DB: ' |
116 |
else |
117 |
datatype := 'Unknown: '; |
118 |
boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ')'; |
119 |
combo_connection.Items.Add(boxstring); |
120 |
if oldcon = boxstring then |
121 |
combo_connection.ItemIndex := combo_connection.Items.Count - 1; |
122 |
end; |
123 |
if combo_connection.ItemIndex = -1 then |
124 |
begin |
125 |
combo_connection.ItemIndex := 0; |
126 |
combo_connectionChange(Self); |
127 |
end; |
128 |
end |
129 |
else |
130 |
begin |
131 |
FConnection := nil; |
132 |
filelist.Items.Clear; |
133 |
combo_extension.Items.Clear; |
134 |
combo_connectionChange(Self); |
135 |
FSelectedFile := GetEmptyFileInfo; |
136 |
if Assigned(FOnNewFileSelected) then |
137 |
FOnNewFileSelected(FSelectedFile); |
138 |
end; |
139 |
end; |
140 |
|
141 |
procedure TForm_ToolTemplate.RecreateList; |
142 |
var |
143 |
i: Integer; |
144 |
fn, datatype: String; |
145 |
level: Integer; |
146 |
begin |
147 |
combo_connection.Items.Clear; |
148 |
if Length(DataConnections) > 0 then |
149 |
begin |
150 |
for i := 0 to High(DataConnections) do |
151 |
begin |
152 |
level := DataConnections[i].LevelInfo.LevelNumber; |
153 |
fn := ExtractFileName(DataConnections[i].FileName); |
154 |
if DataConnections[i].Backend = ODB_Dat then |
155 |
datatype := 'ONI-.dat: ' |
156 |
else if DataConnections[i].Backend = ODB_ADB then |
157 |
datatype := 'OUP-DB: ' |
158 |
else |
159 |
datatype := 'Unknown: '; |
160 |
combo_connection.Items.Add(datatype + fn + ' (Level: ' + IntToStr(level) + ')'); |
161 |
end; |
162 |
FConnection := DataConnections[0]; |
163 |
combo_connection.ItemIndex := 0; |
164 |
combo_connectionChange(Self); |
165 |
end |
166 |
else |
167 |
begin |
168 |
FConnection := nil; |
169 |
filelist.Items.Clear; |
170 |
combo_extension.Items.Clear; |
171 |
combo_connectionChange(Self); |
172 |
end; |
173 |
end; |
174 |
|
175 |
procedure TForm_ToolTemplate.RecreateExtList; |
176 |
var |
177 |
i: LongWord; |
178 |
exts: TStringArray; |
179 |
begin |
180 |
combo_extension.Items.Clear; |
181 |
if FConnection <> nil then |
182 |
begin |
183 |
combo_extension.Items.Add('_All files_ (' + |
184 |
IntToStr(FConnection.GetFilesCount) + ')'); |
185 |
exts := FConnection.GetExtensionsList; |
186 |
for i := 0 to High(exts) do |
187 |
if Length(FAllowedExts) > 0 then |
188 |
begin |
189 |
if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then |
190 |
begin |
191 |
combo_extension.Items.Add(exts[i]); |
192 |
end; |
193 |
end else |
194 |
combo_extension.Items.Add(exts[i]); |
195 |
combo_extension.ItemIndex := 0; |
196 |
combo_extensionClick(Self); |
197 |
end; |
198 |
end; |
199 |
|
200 |
|
201 |
|
202 |
|
203 |
procedure TForm_ToolTemplate.LoadFileNames; |
204 |
var |
205 |
Extension: String; |
206 |
no_zero_bytes: Boolean; |
207 |
pattern: String; |
208 |
files: TStringArray; |
209 |
i: LongWord; |
210 |
begin |
211 |
if FConnection <> nil then |
212 |
begin |
213 |
Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4); |
214 |
no_zero_bytes := not check_zerobyte.Checked; |
215 |
pattern := ''; |
216 |
if check_filtername.Checked then |
217 |
pattern := edit_filtername.Text; |
218 |
if Extension = '_All' then |
219 |
if Length(FAllowedExts) > 0 then |
220 |
Extension := FAllowedExts |
221 |
else |
222 |
Extension := ''; |
223 |
|
224 |
files := FConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy); |
225 |
|
226 |
filelist.Visible := False; |
227 |
filelist.Items.Clear; |
228 |
if Length(files) > 0 then |
229 |
for i := 0 to High(files) do |
230 |
filelist.Items.Add(files[i]); |
231 |
filelist.Visible := True; |
232 |
end; |
233 |
end; |
234 |
|
235 |
|
236 |
procedure TForm_ToolTemplate.popup_exportClick(Sender: TObject); |
237 |
var |
238 |
id: Integer; |
239 |
ext: String; |
240 |
begin |
241 |
id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]); |
242 |
ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4); |
243 |
exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*'; |
244 |
exportd.DefaultExt := ext; |
245 |
if exportd.Execute then |
246 |
ExportDatFile(FConnection, id, exportd.FileName); |
247 |
end; |
248 |
|
249 |
procedure TForm_ToolTemplate.popup_importClick(Sender: TObject); |
250 |
var |
251 |
id: Integer; |
252 |
finfo: TFileInfo; |
253 |
fs: TFileStream; |
254 |
data: TData; |
255 |
begin |
256 |
id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]); |
257 |
finfo := FConnection.GetFileInfo(id); |
258 |
|
259 |
importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' + |
260 |
finfo.Extension + '|All files|*.*'; |
261 |
if importd.Execute then |
262 |
begin |
263 |
fs := TFileStream.Create(importd.FileName, fmOpenRead); |
264 |
if fs.Size <> finfo.Size then |
265 |
ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) + |
266 |
', file has to have same size as file in .dat.' + CrLf + |
267 |
'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf + |
268 |
'Size of chosen file: ' + FormatFileSize(fs.Size)) |
269 |
else begin |
270 |
SetLength(data, fs.Size); |
271 |
fs.Read(data[0], fs.Size); |
272 |
FConnection.UpdateDatFile(id, data); |
273 |
Self.listClick(Self); |
274 |
end; |
275 |
fs.Free; |
276 |
end; |
277 |
end; |
278 |
|
279 |
procedure TForm_ToolTemplate.popup_opentool(Sender: TObject); |
280 |
var |
281 |
sender_name, context: String; |
282 |
id: Integer; |
283 |
begin |
284 |
sender_name := TComponent(Sender).Name; |
285 |
id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]); |
286 |
context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name)); |
287 |
Form_Main.open_child(context, id); |
288 |
end; |
289 |
|
290 |
procedure TForm_ToolTemplate.combo_connectionChange(Sender: TObject); |
291 |
var |
292 |
name: String; |
293 |
nstart, nend: Integer; |
294 |
i: Integer; |
295 |
begin |
296 |
if combo_connection.ItemIndex >= 0 then |
297 |
begin |
298 |
name := combo_connection.Items.Strings[combo_connection.ItemIndex]; |
299 |
nstart := Pos(' ', name) + 1; |
300 |
nend := Pos('(', name) - 1; |
301 |
name := MidStr(name, nstart, nend - nstart); |
302 |
|
303 |
for i := 0 to High(DataConnections) do |
304 |
begin |
305 |
if ExtractFileName(DataConnections[i].FileName) = name then |
306 |
begin |
307 |
FConnection := DataConnections[i]; |
308 |
Break; |
309 |
end; |
310 |
end; |
311 |
if i = Length(DataConnections) then |
312 |
FConnection := nil; |
313 |
|
314 |
RecreateExtList; |
315 |
if Assigned(FOnNewConnection) then |
316 |
FOnNewConnection(FConnection); |
317 |
end; |
318 |
end; |
319 |
|
320 |
procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject); |
321 |
begin |
322 |
LoadFileNames; |
323 |
end; |
324 |
|
325 |
|
326 |
constructor TForm_ToolTemplate.Create(AOwner: TComponent); |
327 |
var |
328 |
i: Integer; |
329 |
item: TMenuItem; |
330 |
begin |
331 |
inherited; |
332 |
RecreateList; |
333 |
FSelectedFile := GetEmptyFileInfo; |
334 |
if Length(ToolList) > 0 then |
335 |
begin |
336 |
for i := 0 to High(ToolList) do |
337 |
begin |
338 |
item := TMenuItem.Create(filepopup); |
339 |
item.Name := 'popup_' + ToolList[i].context; |
340 |
item.Caption := 'Open with ' + ToolList[i].name; |
341 |
item.OnClick := Self.popup_opentool; |
342 |
filepopup.Items.Insert(i, item); |
343 |
end; |
344 |
end; |
345 |
end; |
346 |
|
347 |
procedure TForm_ToolTemplate.filepopupPopup(Sender: TObject); |
348 |
var |
349 |
ext: String; |
350 |
i: Integer; |
351 |
begin |
352 |
ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4); |
353 |
for i := 0 to High(ToolList) do |
354 |
begin |
355 |
filepopup.Items.Items[i].Enabled := True; |
356 |
if Length(ToolList[i].exts) > 0 then |
357 |
if Pos(ext, ToolList[i].exts) = 0 then |
358 |
filepopup.Items.Items[i].Enabled := False; |
359 |
end; |
360 |
end; |
361 |
|
362 |
procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject); |
363 |
begin |
364 |
LoadFileNames; |
365 |
end; |
366 |
|
367 |
procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject); |
368 |
begin |
369 |
if btn_sort_id_asc.Down then |
370 |
FSortBy := stIDAsc |
371 |
else if btn_sort_id_desc.Down then |
372 |
FSortBy := stIDDesc |
373 |
else if btn_sort_name_asc.Down then |
374 |
FSortBy := stNameAsc |
375 |
else if btn_sort_name_desc.Down then |
376 |
FSortBy := stNameDesc |
377 |
else if btn_sort_ext_asc.Down then |
378 |
FSortBy := stExtAsc |
379 |
else if btn_sort_ext_desc.Down then |
380 |
FSortBy := stExtDesc; |
381 |
LoadFileNames; |
382 |
end; |
383 |
|
384 |
procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject); |
385 |
begin |
386 |
edit_filtername.Enabled := not check_filtername.Checked; |
387 |
LoadFileNames; |
388 |
end; |
389 |
|
390 |
procedure TForm_ToolTemplate.listClick(Sender: TObject); |
391 |
var |
392 |
fileid: Integer; |
393 |
begin |
394 |
if filelist.ItemIndex > -1 then |
395 |
begin |
396 |
fileid := FConnection.ExtractFileID( |
397 |
filelist.Items.Strings[filelist.ItemIndex]); |
398 |
FSelectedFile := FConnection.GetFileInfo(fileid); |
399 |
if Assigned(FOnNewFileSelected) then |
400 |
FOnNewFileSelected(FSelectedFile); |
401 |
end; |
402 |
end; |
403 |
|
404 |
procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton; |
405 |
Shift: TShiftState; X, Y: Integer); |
406 |
var |
407 |
pt: TPoint; |
408 |
begin |
409 |
pt.X := x; |
410 |
pt.Y := y; |
411 |
// filelist.ItemIndex := filelist.ItemAtPos(pt, true); |
412 |
// Self.listClick(Self); |
413 |
end; |
414 |
|
415 |
|
416 |
|
417 |
procedure TForm_ToolTemplate.SelectFileID(id: Integer); |
418 |
var |
419 |
i: Integer; |
420 |
begin |
421 |
filelist.ItemIndex := -1; |
422 |
if filelist.Items.Count > 0 then |
423 |
for i := 0 to filelist.Items.Count - 1 do |
424 |
if FConnection.ExtractFileID(filelist.Items.Strings[i]) = id then |
425 |
begin |
426 |
filelist.ItemIndex := i; |
427 |
Break; |
428 |
end; |
429 |
Self.listClick(Self); |
430 |
end; |
431 |
|
432 |
procedure TForm_ToolTemplate.SelectFileName(filename: String); |
433 |
var |
434 |
i: Integer; |
435 |
begin |
436 |
filelist.ItemIndex := -1; |
437 |
if filelist.Items.Count > 0 then |
438 |
for i := 0 to filelist.Items.Count - 1 do |
439 |
if filelist.Items.Strings[i] = filename then |
440 |
filelist.ItemIndex := i; |
441 |
Self.listClick(Self); |
442 |
end; |
443 |
|
444 |
procedure TForm_ToolTemplate.SetAllowedExts(exts: String); |
445 |
begin |
446 |
FAllowedExts := exts; |
447 |
RecreateExtList; |
448 |
end; |
449 |
|
450 |
procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String; |
451 |
zerobytes: Boolean); |
452 |
var |
453 |
i: Integer; |
454 |
begin |
455 |
if Length(pattern) > 0 then |
456 |
Self.edit_filtername.Text := pattern; |
457 |
Self.check_filtername.Checked := Length(pattern) > 0; |
458 |
if Length(extension) > 0 then |
459 |
begin |
460 |
for i := 0 to Self.combo_extension.Items.Count - 1 do |
461 |
if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then |
462 |
Break; |
463 |
if i < Self.combo_extension.Items.Count then |
464 |
Self.combo_extension.ItemIndex := i |
465 |
else |
466 |
Self.combo_extension.ItemIndex := -1; |
467 |
end; |
468 |
Self.check_zerobyte.Checked := zerobytes; |
469 |
Self.LoadFileNames; |
470 |
end; |
471 |
|
472 |
procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean); |
473 |
begin |
474 |
FAllowMultiSelect := allow; |
475 |
filelist.MultiSelect := allow; |
476 |
end; |
477 |
|
478 |
|
479 |
procedure TForm_ToolTemplate.FormResize(Sender: TObject); |
480 |
begin |
481 |
if Self.Width < 300 then |
482 |
Self.Width := 300; |
483 |
if Self.Height < 200 then |
484 |
Self.Height := 200; |
485 |
end; |
486 |
|
487 |
|
488 |
|
489 |
procedure TForm_ToolTemplate.FormCreate(Sender: TObject); |
490 |
begin |
491 |
Self.Width := 260; |
492 |
Self.Height := 300; |
493 |
FOnNewFileSelected := nil; |
494 |
FOnNewConnection := nil; |
495 |
FAllowedExts := ''; |
496 |
FAllowMultiSelect := False; |
497 |
end; |
498 |
|
499 |
procedure TForm_ToolTemplate.FormActivate(Sender: TObject); |
500 |
begin |
501 |
if edit_filtername.CanFocus then |
502 |
edit_filtername.SetFocus |
503 |
else |
504 |
if content.CanFocus then |
505 |
content.SetFocus; |
506 |
end; |
507 |
|
508 |
procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction); |
509 |
begin |
510 |
Action := caFree; |
511 |
end; |
512 |
|
513 |
|
514 |
procedure AddToolListEntryExt(context, ext: String); |
515 |
var |
516 |
i: Integer; |
517 |
begin |
518 |
for i := 0 to High(ToolList) do |
519 |
if ToolList[i].context = context then |
520 |
begin |
521 |
if Pos(ext, ToolList[i].exts) = 0 then |
522 |
begin |
523 |
if Length(ToolList[i].exts) = 0 then |
524 |
ToolList[i].exts := ext |
525 |
else |
526 |
ToolList[i].exts := ToolList[i].exts + ',' + ext; |
527 |
end; |
528 |
Exit; |
529 |
end; |
530 |
end; |
531 |
|
532 |
procedure AddToolListEntry(context, name, exts: String); |
533 |
var |
534 |
i: Integer; |
535 |
begin |
536 |
if Length(ToolList) > 0 then |
537 |
begin |
538 |
for i := 0 to High(ToolList) do |
539 |
if ToolList[i].context = context then |
540 |
begin |
541 |
if (Length(ToolList[i].name) = 0) and (Length(name) > 0) then |
542 |
ToolList[i].name := name; |
543 |
if Length(exts) > 0 then |
544 |
AddToolListEntryExt(context, exts); |
545 |
Exit; |
546 |
end; |
547 |
end; |
548 |
SetLength(ToolList, Length(ToolList) + 1); |
549 |
for i := High(ToolList) downto 1 do |
550 |
if ToolList[i - 1].name > name then |
551 |
ToolList[i] := ToolList[i - 1] |
552 |
else |
553 |
Break; |
554 |
ToolList[i].context := context; |
555 |
ToolList[i].name := name; |
556 |
ToolList[i].exts := exts; |
557 |
end; |
558 |
|
559 |
end. |