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

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