source: oup/current/Tools/Template.pas@ 193

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