source: oup/current/Tools/_TemplateFileList.pas@ 217

Last change on this file since 217 was 217, checked in by alloc, 17 years ago
File size: 10.5 KB
RevLine 
[217]1unit _TemplateFileList;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, _TemplateFile, StdCtrls, ExtCtrls, Menus, Buttons,
8 TypeDefs;
9
10type
11 TForm_TemplateFileList = class(TForm_TemplateFile)
12 panel_files: TPanel;
13 filelist: TListBox;
14 panel_extension: TPanel;
15 label_sort2: TLabel;
16 label_sort1: TLabel;
17 label_extension: TLabel;
18 btn_sort_id_asc: TSpeedButton;
19 btn_sort_id_desc: TSpeedButton;
20 btn_sort_name_asc: TSpeedButton;
21 btn_sort_name_desc: TSpeedButton;
22 btn_sort_ext_asc: TSpeedButton;
23 btn_sort_ext_desc: TSpeedButton;
24 combo_extension: TComboBox;
25 check_zerobyte: TCheckBox;
26 edit_filtername: TEdit;
27 check_filtername: TCheckBox;
28 bevel_filelist: TBevel;
29 splitter_content: TSplitter;
30 panel_content: TPanel;
31 filepopup: TPopupMenu;
32 popup_separator2: TMenuItem;
33 popup_linkshere: TMenuItem;
34 popup_separator: TMenuItem;
35 popup_import: TMenuItem;
36 popup_export: TMenuItem;
37 importd: TOpenDialog;
38 exportd: TSaveDialog;
39 private
40 FSortBy: TSortType;
41 FAllowedExts: String;
42 FAllowMultiSelect: Boolean;
43 procedure SetAllowedExts(exts: String);
44 procedure SetMultiSelect(allow: Boolean);
45
46 procedure check_filternameClick(Sender: TObject);
47 procedure check_zerobyteClick(Sender: TObject);
48 procedure combo_extensionClick(Sender: TObject);
49 procedure btn_sortClick(Sender: TObject);
50 procedure listClick(Sender: TObject);
51 procedure listMouseDown(Sender: TObject; Button: TMouseButton;
52 Shift: TShiftState; X, Y: Integer);
53
54 procedure popup_importClick(Sender: TObject);
55 procedure popup_exportClick(Sender: TObject);
56 procedure popup_opentool(Sender: TObject);
57 procedure popup_linkshereClick(Sender: TObject);
58 procedure filepopupPopup(Sender: TObject);
59 public
60 constructor Create(AOwner: TComponent); override;
61 procedure RecreateExtList;
62 procedure LoadFileNames;
63 procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean);
64 property AllowedExts: String read FAllowedExts write SetAllowedExts;
65 property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect;
66 end;
67
68var
69 Form_TemplateFileList: TForm_TemplateFileList;
70
71implementation
72{$R *.dfm}
73uses ConnectionManager, Exporters, Functions, StrUtils, WhatLinksHere, Main;
74
75
76procedure TForm_TemplateFileList.RecreateExtList;
77var
78 i: Integer;
79 exts: TStrings;
80begin
81 combo_extension.Items.Clear;
82 if FConnectionID > -1 then
83 begin
84 combo_extension.Items.Add('_All files_ (' +
85 IntToStr(ConManager.Connection[FConnectionID].GetFileCount) + ')');
86 exts := nil;
87 exts := ConManager.Connection[FConnectionID].GetExtensionsList(EF_ExtCount);
88 for i := 0 to exts.Count - 1 do
89 if Length(FAllowedExts) > 0 then
90 begin
91 if Pos(MidStr(exts.Strings[i],1,4), FAllowedExts) > 0 then
92 combo_extension.Items.Add(exts.Strings[i]);
93 end
94 else
95 combo_extension.Items.Add(exts.Strings[i]);
96 combo_extension.ItemIndex := 0;
97 combo_extensionClick(Self);
98 exts.Free;
99 end;
100end;
101
102procedure TForm_TemplateFileList.LoadFileNames;
103var
104 Extension: String;
105 no_zero_bytes: Boolean;
106 pattern: String;
107 files: TStrings;
108begin
109 if FConnectionID > -1 then
110 begin
111 Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4);
112 no_zero_bytes := not check_zerobyte.Checked;
113 pattern := '';
114 if check_filtername.Checked then
115 pattern := edit_filtername.Text;
116 if Extension = '_All' then
117 if Length(FAllowedExts) > 0 then
118 Extension := FAllowedExts
119 else
120 Extension := '';
121
122 files := nil;
123 files := ConManager.Connection[FConnectionID].GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
124
125 filelist.Visible := False;
126 filelist.Items.Clear;
127 if files.Count > 0 then
128 filelist.Items.AddStrings(files);
129 filelist.Visible := True;
130 end;
131end;
132
133
134procedure TForm_TemplateFileList.popup_exportClick(Sender: TObject);
135var
136 id: Integer;
137 ext: String;
138begin
139 id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
140 ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
141 exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*';
142 exportd.DefaultExt := ext;
143 if exportd.Execute then
144 ExportDatFile(FConnectionID, id, exportd.FileName);
145end;
146
147procedure TForm_TemplateFileList.popup_importClick(Sender: TObject);
148var
149 id: Integer;
150 finfo: TFileInfo;
151 fs: TFileStream;
152begin
153 if CR_EditDat in ConManager.Connection[FConnectionID].ChangeRights then
154 begin
155 id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
156 finfo := ConManager.Connection[FConnectionID].GetFileInfo(id);
157
158 importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
159 finfo.Extension + '|All files|*.*';
160 if importd.Execute then
161 begin
162 fs := TFileStream.Create(importd.FileName, fmOpenRead);
163 if fs.Size <> finfo.Size then
164 begin
165 if not (CR_ResizeDat in ConManager.Connection[FConnectionID].ChangeRights) then
166 begin
167 ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
168 ', file has to have same size as file in .dat with this backend.' + CrLf +
169 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
170 'Size of chosen file: ' + FormatFileSize(fs.Size));
171 Exit;
172 end else begin
173 if MessageBox(Self.Handle,
174 PChar('File has different size from the file in the .dat.' + CrLf +
175 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
176 'Size of chosen file: ' + FormatFileSize(fs.Size) + CrLf +
177 'Replace anyway?'), PChar('Different size'), MB_YESNO + MB_ICONWARNING) = ID_NO then
178 begin
179 Exit;
180 end;
181 end;
182 end;
183 ConManager.Connection[FConnectionID].UpdateDatFile(id, fs);
184 Self.listClick(Self);
185 fs.Free;
186 end;
187 end else begin
188 ShowMessage('Editing .dat-contents not allowed with this backend.');
189 end;
190end;
191
192
193procedure TForm_TemplateFileList.popup_linkshereClick(Sender: TObject);
194begin
195 Form_WhatLinksHere.ConID := FConnectionID;
196 Form_WhatLinksHere.FileID := FSelectedFile.ID;
197 Form_WhatLinksHere.SenderForm := Self;
198 Form_WhatLinksHere.Show;
199end;
200
201procedure TForm_TemplateFileList.popup_opentool(Sender: TObject);
202var
203 sender_name, context: String;
204 id: Integer;
205begin
206 sender_name := TComponent(Sender).Name;
207 id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
208 context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
209 Form_Main.open_child(context, FConnectionID, id);
210end;
211
212procedure TForm_TemplateFileList.filepopupPopup(Sender: TObject);
213var
214 ext: String;
215 i: Integer;
216begin
217 ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
218 for i := 0 to High(ToolList) do
219 begin
220 filepopup.Items.Items[i].Enabled := True;
221 if Length(ToolList[i].exts) > 0 then
222 if Pos(ext, ToolList[i].exts) = 0 then
223 filepopup.Items.Items[i].Enabled := False;
224 end;
225end;
226
227
228procedure TForm_TemplateFileList.check_zerobyteClick(Sender: TObject);
229begin
230 LoadFileNames;
231end;
232
233procedure TForm_TemplateFileList.btn_sortClick(Sender: TObject);
234begin
235 if btn_sort_id_asc.Down then
236 FSortBy := ST_IDAsc
237 else if btn_sort_id_desc.Down then
238 FSortBy := ST_IDDesc
239 else if btn_sort_name_asc.Down then
240 FSortBy := ST_NameAsc
241 else if btn_sort_name_desc.Down then
242 FSortBy := ST_NameDesc
243 else if btn_sort_ext_asc.Down then
244 FSortBy := ST_ExtAsc
245 else if btn_sort_ext_desc.Down then
246 FSortBy := ST_ExtDesc;
247 LoadFileNames;
248end;
249
250procedure TForm_TemplateFileList.check_filternameClick(Sender: TObject);
251begin
252 edit_filtername.Enabled := not check_filtername.Checked;
253 LoadFileNames;
254end;
255
256procedure TForm_TemplateFileList.combo_extensionClick(Sender: TObject);
257begin
258 LoadFileNames;
259end;
260
261procedure TForm_TemplateFileList.listClick(Sender: TObject);
262var
263 fileid: Integer;
264begin
265 if filelist.ItemIndex > -1 then
266 begin
267 fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName(
268 filelist.Items.Strings[filelist.ItemIndex]);
269 FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid);
270 if Assigned(FOnNewFileSelected) then
271 FOnNewFileSelected(FSelectedFile);
272 end;
273end;
274
275procedure TForm_TemplateFileList.listMouseDown(Sender: TObject; Button: TMouseButton;
276 Shift: TShiftState; X, Y: Integer);
277var
278 pt: TPoint;
279begin
280 pt.X := x;
281 pt.Y := y;
282 if Shift = [ssRight] then
283 begin
284 filelist.ItemIndex := filelist.ItemAtPos(pt, true);
285 Self.listClick(Self);
286 end;
287end;
288
289
290
291procedure TForm_TemplateFileList.SetAllowedExts(exts: String);
292begin
293 FAllowedExts := exts;
294 RecreateExtList;
295end;
296
297procedure TForm_TemplateFileList.SetFileFilters(pattern, extension: String;
298 zerobytes: Boolean);
299var
300 i: Integer;
301begin
302 if Length(pattern) > 0 then
303 Self.edit_filtername.Text := pattern;
304 Self.check_filtername.Checked := Length(pattern) > 0;
305 if Length(extension) > 0 then
306 begin
307 for i := 0 to Self.combo_extension.Items.Count - 1 do
308 if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then
309 Break;
310 if i < Self.combo_extension.Items.Count then
311 Self.combo_extension.ItemIndex := i
312 else
313 Self.combo_extension.ItemIndex := -1;
314 end;
315 Self.check_zerobyte.Checked := zerobytes;
316 Self.LoadFileNames;
317end;
318
319procedure TForm_TemplateFileList.SetMultiSelect(allow: Boolean);
320begin
321 FAllowMultiSelect := allow;
322 filelist.MultiSelect := allow;
323end;
324
325
326
327constructor TForm_TemplateFileList.Create(AOwner: TComponent);
328var
329 i: Integer;
330 item: TMenuItem;
331begin
332 inherited;
333 FAllowedExts := '';
334 FAllowMultiSelect := False;
335 if Length(ToolList) > 0 then
336 begin
337 for i := 0 to High(ToolList) do
338 begin
339 item := TMenuItem.Create(filepopup);
340 item.Name := 'popup_' + ToolList[i].context;
341 item.Caption := 'Open with ' + ToolList[i].name;
342 item.OnClick := Self.popup_opentool;
343 filepopup.Items.Insert(i, item);
344 end;
345 end;
346end;
347
348
349end.
Note: See TracBrowser for help on using the repository browser.