-
Notifications
You must be signed in to change notification settings - Fork 140
/
filepropDlg.pas
521 lines (454 loc) · 13.1 KB
/
filepropDlg.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
unit filepropDlg;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, ExtCtrls, CheckLst, utilLib, main, types, Grids,
ValEdit, strutils, hslib, math;
type
TfilepropFrm = class(TForm)
pages: TPageControl;
permTab: TTabSheet;
flagsTab: TTabSheet;
diffTab: TTabSheet;
commentTab: TTabSheet;
maskTab: TTabSheet;
hiddenChk: TCheckBox;
hidetreeChk: TCheckBox;
archivableChk: TCheckBox;
browsableChk: TCheckBox;
dontlogChk: TCheckBox;
nodlChk: TCheckBox;
dontconsiderChk: TCheckBox;
hideemptyChk: TCheckBox;
hideextChk: TCheckBox;
Panel1: TPanel;
okBtn: TButton;
cancelBtn: TButton;
difftplBox: TMemo;
commentBox: TMemo;
actionTabs: TTabControl;
newaccBtn: TButton;
anyAccChk: TCheckBox;
anonChk: TCheckBox;
allBtn: TButton;
accountsBox: TListView;
filesfilterBox: TLabeledEdit;
foldersfilterBox: TLabeledEdit;
deffileBox: TLabeledEdit;
uploadfilterBox: TLabeledEdit;
dontconsiderBox: TLabeledEdit;
otherTab: TTabSheet;
realmBox: TLabeledEdit;
anyoneChk: TCheckBox;
iconBox: TComboBoxEx;
Label1: TLabel;
addiconBtn: TButton;
goToAccountsBtn: TButton;
applyBtn: TButton;
procedure accountsBoxGetImageIndex(Sender: TObject; Item: TListItem);
procedure actionTabsChange(Sender: TObject);
procedure newaccBtnClick(Sender: TObject);
procedure allBtnClick(Sender: TObject);
procedure accountsBoxChange(Sender: TObject; Item: TListItem; Change: TItemChange);
procedure anonChkClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure textinputEnter(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure addiconBtnClick(Sender: TObject);
procedure goToAccountsBtnClick(Sender: TObject);
procedure applyBtnClick(Sender: TObject);
private
iconOfs: integer;
public
firstActionChange: boolean;
users: array [TfileAction] of TStringDynArray;
savePerm: array [TfileAction] of boolean; // should we apply/save permissions for this TfileAction ?
currAction, prevAction: TfileAction;
procedure updateAccountsBox;
end;
var
filepropFrm: TfilepropFrm;
implementation
uses optionsDlg;
{$R *.dfm}
procedure TfilepropFrm.accountsBoxChange(Sender: TObject; Item: TListItem; Change: TItemChange);
begin
if (change = ctState)
and (item.caption > '')
and (stringExists(item.caption, users[currAction]) <> item.checked) then
begin
savePerm[currAction]:=TRUE;
toggleString(item.caption, users[currAction])
end;
end;
procedure TfilepropFrm.accountsBoxGetImageIndex(Sender: TObject; Item: TListItem);
begin item.ImageIndex:=accountIcon(item.data) end;
function str2fileaction(s:string):TfileAction;
begin
for result:=low(result) to high(result) do
if FILEACTION2STR[result] = s then
exit;
result:=TfileAction(-1);
end; // str2fileaction
procedure TfilepropFrm.actionTabsChange(Sender: TObject);
var
l: TstringList;
i: integer;
ar: TstringDynArray;
begin
currAction:=str2fileaction(actionTabs.tabs[actionTabs.tabIndex]);
if not firstActionChange then
begin
// we must save current selection before updating the checkmarks
ar:=users[prevAction];
// now 'ar' is actually an alias, no duplication
setLength(ar, 0);
if anonChk.checked then addString(USER_ANONYMOUS, ar);
if anyAccChk.checked then addString(USER_ANY_ACCOUNT, ar);
if anyoneChk.checked then addString(USER_ANYONE, ar);
for i:=0 to accountsBox.Items.Count-1 do
with accountsBox.Items[i] do
if checked then
addString(caption, ar);
prevAction:=currAction;
end;
firstActionChange:=FALSE;
l:=arrayToList(users[currAction]);
try
for i:=0 to accountsBox.Items.Count-1 do
with accountsBox.Items[i] do
checked:=l.IndexOf(caption) >= 0;
anonChk.checked:=l.IndexOf(USER_ANONYMOUS) >= 0;
anyAccChk.checked:=l.indexOf(USER_ANY_ACCOUNT) >= 0;
anyoneChk.checked:=l.indexOf(USER_ANYONE) >= 0;
finally l.free end;
end;
procedure TfilepropFrm.addiconBtnClick(Sender: TObject);
var
fn: string;
i: integer;
begin
if not promptForFileName(fn) then exit;
i:=getImageIndexForFile(fn);
if i < 0 then exit;
iconBox.itemsEx.addItem(idx_label(i), i, i, -1, 0, NIL);
iconBox.itemIndex:=iconOfs+i;
end;
procedure TfilepropFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if (action = caHide) and (modalResult = mrOk) then
applyBtnClick(applyBtn);
end;
procedure TfilepropFrm.FormKeyPress(Sender: TObject; var Key: Char);
begin
if pages.focused then
if charInSet(key, ['1'..'9']) then
try pages.TabIndex:=ord(key)-ord('0')-1
except end;
end;
procedure TfilepropFrm.FormShow(Sender: TObject);
var
i: integer;
f: Tfile;
procedure setFlag(flag:TfileAttribute; cb:TCheckBox);
var
should: TCheckBoxState;
begin
cb.enabled:=TRUE;
if flag in f.flags then
should:=cbChecked
else
should:=cbUnchecked;
if i = 0 then
cb.state:=should
else
if (cb.state <> cbGrayed) and (cb.state <> should) then
cb.state:=cbGrayed;
end; // setFlag
procedure setText(var v:string; box:TCustomEdit);
const
COLOR = clInfoBk;
var
n: integer;
begin
n:=countSubstr(#0, box.hint);
box.enabled:=TRUE;
if n = 0 then
begin // init this edit box
box.text:=v;
box.hint:=box.hint+#0;
exit;
end;
if (pos(#0+v+#0, box.hint) > 0)
or (box.hint = #0) and (v = box.text) then
exit; // the value is already there
if n > 1 then
begin // add the value to the list of values
box.hint:=box.hint+v+#0;
exit;
end;
box.hint:=box.hint+box.text+#0+v+#0; // init the list of values
box.text:='(more values)'; // message to be shown
// these properties are unhappily kept unaccessible through TcustomEdit interface
try (box as Tlabelededit).color:=COLOR except end;
try (box as Tmemo).color:=COLOR except end;
end; // setText
procedure setCaption();
const
MAX = 2;
var
a: TStringDynArray;
i: integer;
begin
a:=NIL;
for i:=0 to min(mainFrm.filesBox.SelectionCount, MAX)-1 do
addString(mainFrm.filesBox.Selections[i].Text, a);
if mainFrm.filesBox.SelectionCount > MAX then
addString('...', a);
caption:='Properties for '+join(', ', a);
end; // setCaption
var
act: TfileAction;
actions: set of TfileAction;
begin
firstActionChange:=TRUE;
accountsBox.smallImages:=mainfrm.images;
updateAccountsBox();
maskTab.tabVisible:=FALSE;
diffTab.tabVisible:=FALSE;
iconBox.clear();
iconBox.Enabled:=FALSE;
addiconBtn.Enabled:=FALSE;
i:=if_(mainfrm.filesBox.SelectionCount > 1, -1, selectedFile.getIconForTreeview());
iconBox.itemsEx.addItem('Default', i, i, -1, 0, NIL);
iconOfs:=iconBox.ItemsEx.count;
for i:=0 to mainfrm.images.Count-1 do
iconBox.itemsEx.addItem(idx_label(i), i, i, -1, 0, NIL);
actions:=[FA_ACCESS];
for i:=0 to mainFrm.filesBox.SelectionCount-1 do
begin
f:=mainFrm.filesBox.Selections[i].data;
setText(f.comment, commentBox);
setText(f.realm, realmBox);
if f.isRealFolder() then
begin
include(actions, FA_UPLOAD);
setText(f.uploadFilterMask, uploadfilterBox);
end;
if f.isFileOrFolder() then
setFlag(FA_DONT_LOG, dontlogChk);
if f.isFile() or f.isRealFolder() then
setFlag(FA_DL_FORBIDDEN, nodlChk);
if not f.isRoot() then
begin
setFlag(FA_HIDDEN, hiddenChk);
if not iconBox.enabled then
begin
iconBox.enabled:=TRUE;
iconBox.itemIndex:=f.icon+iconOfs;
addiconBtn.Enabled:=TRUE;
end
else
if iconBox.itemIndex <> f.icon+iconOfs then
iconBox.itemIndex:=-1;
end;
if f.isFile() then
setFlag(FA_DONT_COUNT_AS_DL, dontconsiderChk);
if f.isFolder() then
begin
include(actions, FA_DELETE);
diffTab.tabVisible:=TRUE;
maskTab.tabVisible:=TRUE;
setText(f.filesFilter, filesfilterBox);
setText(f.foldersFilter, foldersfilterBox);
setText(f.defaultFileMask, deffileBox);
setText(f.dontCountAsDownloadMask, dontconsiderBox);
setText(f.diffTpl, difftplBox);
setFlag(FA_HIDDENTREE, hidetreeChk);
setFlag(FA_HIDE_EXT, hideextChk);
setFlag(FA_BROWSABLE, browsableChk);
setFlag(FA_ARCHIVABLE, archivableChk);
setFlag(FA_HIDE_EMPTY_FOLDERS, hideemptyChk);
end;
// collect usernames
for act:=low(act) to high(act) do
addUniqueArray(users[act], f.accounts[act]);
end;
for act:=low(act) to high(act) do
begin
savePerm[act]:=FALSE;
if act in actions then
actionTabs.tabs.add(FILEACTION2STR[act]);
end;
if easyMode then
onlyForExperts([browsableChk, commentTab, realmBox, dontconsiderChk, maskTab, dontlogChk, hideextChk]);
actionTabs.tabIndex:=0;
actionTabsChange(NIL);
setCaption();
pages.TabIndex:=0;
end;
procedure TfilepropFrm.goToAccountsBtnClick(Sender: TObject);
begin
showOptions(optionsFrm.accountsPage);
updateAccountsBox();
actionTabsChange(NIL);
end;
procedure TfilepropFrm.allBtnClick(Sender: TObject);
var
i: integer;
b: boolean;
begin
if accountsBox.items.Count = 0 then exit;
with accountsBox.Items[0] do
begin
b:=not checked;
checked:=b;
end;
for i:=1 to accountsBox.items.count-1 do
accountsBox.Items[i].checked:=b;
end;
procedure TfilepropFrm.anonChkClick(Sender: TObject);
var
s: string;
begin
savePerm[currAction]:=TRUE;
if sender = anonChk then s:=USER_ANONYMOUS
else if sender = anyAccChk then
begin
s:=USER_ANY_ACCOUNT;
accountsBox.enabled:=not anyAccChk.Checked;
end
else if sender = anyoneChk then
begin
s:=USER_ANYONE;
accountsBox.enabled:=not anyoneChk.Checked;
anonChk.enabled:=accountsBox.enabled;
anyAccChk.enabled:=accountsBox.enabled;
newaccBtn.Enabled:=accountsBox.enabled;
end;
allBtn.Enabled:=accountsBox.enabled;
with sender as Tcheckbox do
if checked then addUniqueString(s, users[currAction])
else removeString(s, users[currAction]);
end;
procedure TfilepropFrm.applyBtnClick(Sender: TObject);
var
i: integer;
f: Tfile;
act: TfileAction;
procedure applyFlag(flag:TfileAttribute; cb:TCheckBox);
begin
if (cb.State = cbGrayed)
or not cb.Enabled
or not cb.Visible then exit;
if cb.Checked then include(f.flags, flag)
else exclude(f.flags, flag);
end; // applyFlag
procedure applyText(var v:string; box:TCustomEdit);
begin
if box.modified then
v:=box.Text;
end; // applyText
begin
for act:=low(act) to high(act) do
sortArray(users[act]);
for i:=0 to mainFrm.filesBox.SelectionCount-1 do
begin
f:=mainFrm.filesBox.Selections[i].data;
for act:=low(act) to high(act) do
if savePerm[act]
and ((act <> FA_UPLOAD) or f.isRealFolder())
and ((act <> FA_DELETE) or f.isFolder()) then
begin
// The following is because we monitor every upload path
if (act = FA_UPLOAD)
and ((f.accounts[act] = NIL) <> (users[act] = NIL)) then // something has changed
// WARNING: toggleString() can't be used here, it's not equivalent
if users[act] <> NIL then addString(f.resource, uploadPaths)
else removeString(f.resource, uploadPaths);
f.accounts[act]:=users[act];
end;
applyText(f.comment, commentBox);
applyText(f.realm, realmBox);
if f.isFile() then
applyFlag(FA_DONT_COUNT_AS_DL, dontconsiderChk);
if f.isFolder() then
begin
applyText(f.diffTpl, difftplBox);
applyText(f.filesFilter, filesfilterBox);
applyText(f.foldersFilter, foldersfilterBox);
applyText(f.defaultFileMask, deffileBox);
applyText(f.dontCountAsDownloadMask, dontconsiderBox);
applyFlag(FA_HIDDENTREE, hidetreeChk);
applyFlag(FA_HIDE_EXT, hideextChk);
applyFlag(FA_HIDE_EMPTY_FOLDERS, hideemptyChk);
applyFlag(FA_BROWSABLE, browsableChk);
applyFlag(FA_ARCHIVABLE, archivableChk);
end;
if not f.isRoot() then
begin
applyFlag(FA_HIDDEN, hiddenChk);
if iconBox.itemIndex > -1 then
f.setupImage(iconBox.itemIndex-iconOfs);
end;
if f.isRealFolder() then
applyText(f.uploadFilterMask, uploadfilterBox);
if f.isFileOrFolder() then
applyFlag(FA_DONT_LOG, dontlogChk);
if f.isFile() or f.isRealFolder() then
applyFlag(FA_DL_FORBIDDEN, nodlChk);
end;
end;
procedure TfilepropFrm.newaccBtnClick(Sender: TObject);
var
acc: Paccount;
begin
acc:=createAccountOnTheFly();
if acc = NIL then exit;
with accountsBox.Items.add() do
begin
caption:=acc.user;
data:=acc;
checked:=TRUE;
end;
end;
procedure TfilepropFrm.textinputEnter(Sender: TObject);
function chooseValue(var s:string):boolean;
var
l: string;
begin
l:=s;
repeat s:=chop(#0, l)
until (s > '') or (l = '');
result:=TRUE;
end; // chooseValue
var
box: TcustomEdit;
s, h: string;
begin
box:=sender as TcustomEdit;
if countSubstr(#0, box.hint) < 2 then exit;
s:=box.hint;
h:=chop(#0, s);
if not chooseValue(s) then exit;
box.text:=s;
box.hint:=h;
// these properties are unhappily kept unaccessible through TcustomEdit interface
try (box as Tlabelededit).color:=clWindow except end;
try (box as Tmemo).color:=clWindow except end;
end;
procedure TfilepropFrm.updateAccountsBox;
var
i: integer;
a: Paccount;
begin
accountsBox.clear();
for i:=0 to length(accounts)-1 do
begin
a:=@accounts[i];
if not a.enabled then continue;
accountsBox.addItem(a.user, Tobject(a));
end;
end; // updateAccountsBox
end.